blob: 3408a854d561d1d22c17154bc789029b75c8a94c [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant / Control interface (shared code for all backends)
Dmitry Shmidt04949592012-07-19 12:16:46 -07003 * Copyright (c) 2004-2012, 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{
53 int ret;
54 size_t i, num_ssid;
55 struct wpa_ssid *ssid;
56 struct wpa_driver_scan_params params;
57
58 if (wpa_s->pno)
59 return 0;
60
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080061 if (wpa_s->wpa_state == WPA_SCANNING) {
62 wpa_supplicant_cancel_sched_scan(wpa_s);
63 wpa_supplicant_cancel_scan(wpa_s);
64 }
65
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080066 os_memset(&params, 0, sizeof(params));
67
68 num_ssid = 0;
69 ssid = wpa_s->conf->ssid;
70 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -070071 if (!wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080072 num_ssid++;
73 ssid = ssid->next;
74 }
75 if (num_ssid > WPAS_MAX_SCAN_SSIDS) {
76 wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
77 "%u", WPAS_MAX_SCAN_SSIDS, (unsigned int) num_ssid);
78 num_ssid = WPAS_MAX_SCAN_SSIDS;
79 }
80
81 if (num_ssid == 0) {
82 wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
83 return -1;
84 }
85
86 params.filter_ssids = os_malloc(sizeof(struct wpa_driver_scan_filter) *
87 num_ssid);
88 if (params.filter_ssids == NULL)
89 return -1;
90 i = 0;
91 ssid = wpa_s->conf->ssid;
92 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -070093 if (!wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080094 params.ssids[i].ssid = ssid->ssid;
95 params.ssids[i].ssid_len = ssid->ssid_len;
96 params.num_ssids++;
97 os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
98 ssid->ssid_len);
99 params.filter_ssids[i].ssid_len = ssid->ssid_len;
100 params.num_filter_ssids++;
101 i++;
102 if (i == num_ssid)
103 break;
104 }
105 ssid = ssid->next;
106 }
107
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700108 if (wpa_s->conf->filter_rssi)
109 params.filter_rssi = wpa_s->conf->filter_rssi;
110
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800111 ret = wpa_drv_sched_scan(wpa_s, &params, 10 * 1000);
112 os_free(params.filter_ssids);
113 if (ret == 0)
114 wpa_s->pno = 1;
115 return ret;
116}
117
118
119static int pno_stop(struct wpa_supplicant *wpa_s)
120{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800121 int ret = 0;
122
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800123 if (wpa_s->pno) {
124 wpa_s->pno = 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800125 ret = wpa_drv_stop_sched_scan(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800126 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800127
128 if (wpa_s->wpa_state == WPA_SCANNING)
129 wpa_supplicant_req_scan(wpa_s, 0, 0);
130
131 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800132}
133
134
Dmitry Shmidt04949592012-07-19 12:16:46 -0700135static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
136{
137 char *pos;
138 u8 addr[ETH_ALEN], *filter = NULL, *n;
139 size_t count = 0;
140
141 pos = val;
142 while (pos) {
143 if (*pos == '\0')
144 break;
145 if (hwaddr_aton(pos, addr)) {
146 os_free(filter);
147 return -1;
148 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700149 n = os_realloc_array(filter, count + 1, ETH_ALEN);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700150 if (n == NULL) {
151 os_free(filter);
152 return -1;
153 }
154 filter = n;
155 os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
156 count++;
157
158 pos = os_strchr(pos, ' ');
159 if (pos)
160 pos++;
161 }
162
163 wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
164 os_free(wpa_s->bssid_filter);
165 wpa_s->bssid_filter = filter;
166 wpa_s->bssid_filter_count = count;
167
168 return 0;
169}
170
171
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800172static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
173{
174 char *pos;
175 u8 addr[ETH_ALEN], *bssid = NULL, *n;
176 struct wpa_ssid_value *ssid = NULL, *ns;
177 size_t count = 0, ssid_count = 0;
178 struct wpa_ssid *c;
179
180 /*
181 * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | “”
182 * SSID_SPEC ::= ssid <SSID_HEX>
183 * BSSID_SPEC ::= bssid <BSSID_HEX>
184 */
185
186 pos = val;
187 while (pos) {
188 if (*pos == '\0')
189 break;
190 if (os_strncmp(pos, "bssid ", 6) == 0) {
191 int res;
192 pos += 6;
193 res = hwaddr_aton2(pos, addr);
194 if (res < 0) {
195 os_free(ssid);
196 os_free(bssid);
197 wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
198 "BSSID value '%s'", pos);
199 return -1;
200 }
201 pos += res;
202 n = os_realloc_array(bssid, count + 1, ETH_ALEN);
203 if (n == NULL) {
204 os_free(ssid);
205 os_free(bssid);
206 return -1;
207 }
208 bssid = n;
209 os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
210 count++;
211 } else if (os_strncmp(pos, "ssid ", 5) == 0) {
212 char *end;
213 pos += 5;
214
215 end = pos;
216 while (*end) {
217 if (*end == '\0' || *end == ' ')
218 break;
219 end++;
220 }
221
222 ns = os_realloc_array(ssid, ssid_count + 1,
223 sizeof(struct wpa_ssid_value));
224 if (ns == NULL) {
225 os_free(ssid);
226 os_free(bssid);
227 return -1;
228 }
229 ssid = ns;
230
231 if ((end - pos) & 0x01 || end - pos > 2 * 32 ||
232 hexstr2bin(pos, ssid[ssid_count].ssid,
233 (end - pos) / 2) < 0) {
234 os_free(ssid);
235 os_free(bssid);
236 wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
237 "SSID value '%s'", pos);
238 return -1;
239 }
240 ssid[ssid_count].ssid_len = (end - pos) / 2;
241 wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
242 ssid[ssid_count].ssid,
243 ssid[ssid_count].ssid_len);
244 ssid_count++;
245 pos = end;
246 } else {
247 wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
248 "'%s'", pos);
249 os_free(ssid);
250 os_free(bssid);
251 return -1;
252 }
253
254 pos = os_strchr(pos, ' ');
255 if (pos)
256 pos++;
257 }
258
259 wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
260 os_free(wpa_s->disallow_aps_bssid);
261 wpa_s->disallow_aps_bssid = bssid;
262 wpa_s->disallow_aps_bssid_count = count;
263
264 wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
265 os_free(wpa_s->disallow_aps_ssid);
266 wpa_s->disallow_aps_ssid = ssid;
267 wpa_s->disallow_aps_ssid_count = ssid_count;
268
269 if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
270 return 0;
271
272 c = wpa_s->current_ssid;
273 if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
274 return 0;
275
276 if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
277 !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
278 return 0;
279
280 wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
281 "because current AP was marked disallowed");
282
283#ifdef CONFIG_SME
284 wpa_s->sme.prev_bssid_set = 0;
285#endif /* CONFIG_SME */
286 wpa_s->reassociate = 1;
287 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
288 wpa_supplicant_req_scan(wpa_s, 0, 0);
289
290 return 0;
291}
292
293
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700294static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
295 char *cmd)
296{
297 char *value;
298 int ret = 0;
299
300 value = os_strchr(cmd, ' ');
301 if (value == NULL)
302 return -1;
303 *value++ = '\0';
304
305 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
306 if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
307 eapol_sm_configure(wpa_s->eapol,
308 atoi(value), -1, -1, -1);
309 } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
310 eapol_sm_configure(wpa_s->eapol,
311 -1, atoi(value), -1, -1);
312 } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
313 eapol_sm_configure(wpa_s->eapol,
314 -1, -1, atoi(value), -1);
315 } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
316 eapol_sm_configure(wpa_s->eapol,
317 -1, -1, -1, atoi(value));
318 } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
319 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
320 atoi(value)))
321 ret = -1;
322 } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
323 0) {
324 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
325 atoi(value)))
326 ret = -1;
327 } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
328 if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
329 ret = -1;
330 } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
331 wpa_s->wps_fragment_size = atoi(value);
332#ifdef CONFIG_WPS_TESTING
333 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
334 long int val;
335 val = strtol(value, NULL, 0);
336 if (val < 0 || val > 0xff) {
337 ret = -1;
338 wpa_printf(MSG_DEBUG, "WPS: Invalid "
339 "wps_version_number %ld", val);
340 } else {
341 wps_version_number = val;
342 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
343 "version %u.%u",
344 (wps_version_number & 0xf0) >> 4,
345 wps_version_number & 0x0f);
346 }
347 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
348 wps_testing_dummy_cred = atoi(value);
349 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
350 wps_testing_dummy_cred);
351#endif /* CONFIG_WPS_TESTING */
352 } else if (os_strcasecmp(cmd, "ampdu") == 0) {
353 if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
354 ret = -1;
355#ifdef CONFIG_TDLS_TESTING
356 } else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
357 extern unsigned int tdls_testing;
358 tdls_testing = strtol(value, NULL, 0);
359 wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
360#endif /* CONFIG_TDLS_TESTING */
361#ifdef CONFIG_TDLS
362 } else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
363 int disabled = atoi(value);
364 wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
365 if (disabled) {
366 if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
367 ret = -1;
368 } else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
369 ret = -1;
370 wpa_tdls_enable(wpa_s->wpa, !disabled);
371#endif /* CONFIG_TDLS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800372 } else if (os_strcasecmp(cmd, "pno") == 0) {
373 if (atoi(value))
374 ret = pno_start(wpa_s);
375 else
376 ret = pno_stop(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700377 } else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
378 int disabled = atoi(value);
379 if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
380 ret = -1;
381 else if (disabled)
382 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
383 } else if (os_strcasecmp(cmd, "uapsd") == 0) {
384 if (os_strcmp(value, "disable") == 0)
385 wpa_s->set_sta_uapsd = 0;
386 else {
387 int be, bk, vi, vo;
388 char *pos;
389 /* format: BE,BK,VI,VO;max SP Length */
390 be = atoi(value);
391 pos = os_strchr(value, ',');
392 if (pos == NULL)
393 return -1;
394 pos++;
395 bk = atoi(pos);
396 pos = os_strchr(pos, ',');
397 if (pos == NULL)
398 return -1;
399 pos++;
400 vi = atoi(pos);
401 pos = os_strchr(pos, ',');
402 if (pos == NULL)
403 return -1;
404 pos++;
405 vo = atoi(pos);
406 /* ignore max SP Length for now */
407
408 wpa_s->set_sta_uapsd = 1;
409 wpa_s->sta_uapsd = 0;
410 if (be)
411 wpa_s->sta_uapsd |= BIT(0);
412 if (bk)
413 wpa_s->sta_uapsd |= BIT(1);
414 if (vi)
415 wpa_s->sta_uapsd |= BIT(2);
416 if (vo)
417 wpa_s->sta_uapsd |= BIT(3);
418 }
Jouni Malinen21d6bc82012-04-10 16:17:59 -0700419 } else if (os_strcasecmp(cmd, "ps") == 0) {
420 ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700421#ifdef CONFIG_WIFI_DISPLAY
422 } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
423 wifi_display_enable(wpa_s->global, !!atoi(value));
424#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700425 } else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
426 ret = set_bssid_filter(wpa_s, value);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800427 } else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
428 ret = set_disallow_aps(wpa_s, value);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800429 } else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
430 wpa_s->no_keep_alive = !!atoi(value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700431 } else {
432 value[-1] = '=';
433 ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
434 if (ret == 0)
435 wpa_supplicant_update_config(wpa_s);
436 }
437
438 return ret;
439}
440
441
442static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
443 char *cmd, char *buf, size_t buflen)
444{
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700445 int res = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700446
447 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
448
449 if (os_strcmp(cmd, "version") == 0) {
450 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700451 } else if (os_strcasecmp(cmd, "country") == 0) {
452 if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
453 res = os_snprintf(buf, buflen, "%c%c",
454 wpa_s->conf->country[0],
455 wpa_s->conf->country[1]);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700456#ifdef CONFIG_WIFI_DISPLAY
457 } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
458 res = os_snprintf(buf, buflen, "%d",
459 wpa_s->global->wifi_display);
460 if (res < 0 || (unsigned int) res >= buflen)
461 return -1;
462 return res;
463#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700464 }
465
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700466 if (res < 0 || (unsigned int) res >= buflen)
467 return -1;
468 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700469}
470
471
472#ifdef IEEE8021X_EAPOL
473static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
474 char *addr)
475{
476 u8 bssid[ETH_ALEN];
477 struct wpa_ssid *ssid = wpa_s->current_ssid;
478
479 if (hwaddr_aton(addr, bssid)) {
480 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
481 "'%s'", addr);
482 return -1;
483 }
484
485 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
486 rsn_preauth_deinit(wpa_s->wpa);
487 if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
488 return -1;
489
490 return 0;
491}
492#endif /* IEEE8021X_EAPOL */
493
494
495#ifdef CONFIG_PEERKEY
496/* MLME-STKSTART.request(peer) */
497static int wpa_supplicant_ctrl_iface_stkstart(
498 struct wpa_supplicant *wpa_s, char *addr)
499{
500 u8 peer[ETH_ALEN];
501
502 if (hwaddr_aton(addr, peer)) {
503 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
504 "address '%s'", addr);
505 return -1;
506 }
507
508 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
509 MAC2STR(peer));
510
511 return wpa_sm_stkstart(wpa_s->wpa, peer);
512}
513#endif /* CONFIG_PEERKEY */
514
515
516#ifdef CONFIG_TDLS
517
518static int wpa_supplicant_ctrl_iface_tdls_discover(
519 struct wpa_supplicant *wpa_s, char *addr)
520{
521 u8 peer[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800522 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700523
524 if (hwaddr_aton(addr, peer)) {
525 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
526 "address '%s'", addr);
527 return -1;
528 }
529
530 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
531 MAC2STR(peer));
532
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800533 if (wpa_tdls_is_external_setup(wpa_s->wpa))
534 ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
535 else
536 ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
537
538 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700539}
540
541
542static int wpa_supplicant_ctrl_iface_tdls_setup(
543 struct wpa_supplicant *wpa_s, char *addr)
544{
545 u8 peer[ETH_ALEN];
546 int ret;
547
548 if (hwaddr_aton(addr, peer)) {
549 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
550 "address '%s'", addr);
551 return -1;
552 }
553
554 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
555 MAC2STR(peer));
556
557 ret = wpa_tdls_reneg(wpa_s->wpa, peer);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800558 if (ret) {
559 if (wpa_tdls_is_external_setup(wpa_s->wpa))
560 ret = wpa_tdls_start(wpa_s->wpa, peer);
561 else
562 ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
563 }
564
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700565 return ret;
566}
567
568
569static int wpa_supplicant_ctrl_iface_tdls_teardown(
570 struct wpa_supplicant *wpa_s, char *addr)
571{
572 u8 peer[ETH_ALEN];
573
574 if (hwaddr_aton(addr, peer)) {
575 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
576 "address '%s'", addr);
577 return -1;
578 }
579
580 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
581 MAC2STR(peer));
582
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800583 return wpa_tdls_teardown_link(wpa_s->wpa, peer,
584 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700585}
586
587#endif /* CONFIG_TDLS */
588
589
590#ifdef CONFIG_IEEE80211R
591static int wpa_supplicant_ctrl_iface_ft_ds(
592 struct wpa_supplicant *wpa_s, char *addr)
593{
594 u8 target_ap[ETH_ALEN];
595 struct wpa_bss *bss;
596 const u8 *mdie;
597
598 if (hwaddr_aton(addr, target_ap)) {
599 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
600 "address '%s'", addr);
601 return -1;
602 }
603
604 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
605
606 bss = wpa_bss_get_bssid(wpa_s, target_ap);
607 if (bss)
608 mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
609 else
610 mdie = NULL;
611
612 return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
613}
614#endif /* CONFIG_IEEE80211R */
615
616
617#ifdef CONFIG_WPS
618static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
619 char *cmd)
620{
621 u8 bssid[ETH_ALEN], *_bssid = bssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700622#ifdef CONFIG_P2P
623 u8 p2p_dev_addr[ETH_ALEN];
624#endif /* CONFIG_P2P */
625#ifdef CONFIG_AP
626 u8 *_p2p_dev_addr = NULL;
627#endif /* CONFIG_AP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800628
Irfan Sheriffa7534b92012-09-06 18:30:39 -0700629 if (cmd == NULL || os_strcmp(cmd, "any") == 0 || cmd[0] == '\0') {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700630 _bssid = NULL;
631#ifdef CONFIG_P2P
632 } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
633 if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
634 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
635 "P2P Device Address '%s'",
636 cmd + 13);
637 return -1;
638 }
639 _p2p_dev_addr = p2p_dev_addr;
640#endif /* CONFIG_P2P */
641 } else if (hwaddr_aton(cmd, bssid)) {
642 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
643 cmd);
644 return -1;
645 }
646
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800647#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700648 if (wpa_s->ap_iface)
649 return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
650#endif /* CONFIG_AP */
651
652 return wpas_wps_start_pbc(wpa_s, _bssid, 0);
653}
654
655
656static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
657 char *cmd, char *buf,
658 size_t buflen)
659{
660 u8 bssid[ETH_ALEN], *_bssid = bssid;
661 char *pin;
662 int ret;
663
664 pin = os_strchr(cmd, ' ');
665 if (pin)
666 *pin++ = '\0';
667
668 if (os_strcmp(cmd, "any") == 0)
669 _bssid = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800670 else if (os_strcmp(cmd, "get") == 0) {
671 ret = wps_generate_pin();
672 goto done;
673 } else if (hwaddr_aton(cmd, bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700674 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
675 cmd);
676 return -1;
677 }
678
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800679#ifdef CONFIG_AP
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800680 if (wpa_s->ap_iface) {
681 int timeout = 0;
682 char *pos;
683
684 if (pin) {
685 pos = os_strchr(pin, ' ');
686 if (pos) {
687 *pos++ = '\0';
688 timeout = atoi(pos);
689 }
690 }
691
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700692 return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800693 buf, buflen, timeout);
694 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700695#endif /* CONFIG_AP */
696
697 if (pin) {
698 ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
699 DEV_PW_DEFAULT);
700 if (ret < 0)
701 return -1;
702 ret = os_snprintf(buf, buflen, "%s", pin);
703 if (ret < 0 || (size_t) ret >= buflen)
704 return -1;
705 return ret;
706 }
707
708 ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
709 if (ret < 0)
710 return -1;
711
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800712done:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700713 /* Return the generated PIN */
714 ret = os_snprintf(buf, buflen, "%08d", ret);
715 if (ret < 0 || (size_t) ret >= buflen)
716 return -1;
717 return ret;
718}
719
720
721static int wpa_supplicant_ctrl_iface_wps_check_pin(
722 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
723{
724 char pin[9];
725 size_t len;
726 char *pos;
727 int ret;
728
729 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
730 (u8 *) cmd, os_strlen(cmd));
731 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
732 if (*pos < '0' || *pos > '9')
733 continue;
734 pin[len++] = *pos;
735 if (len == 9) {
736 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
737 return -1;
738 }
739 }
740 if (len != 4 && len != 8) {
741 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
742 return -1;
743 }
744 pin[len] = '\0';
745
746 if (len == 8) {
747 unsigned int pin_val;
748 pin_val = atoi(pin);
749 if (!wps_pin_valid(pin_val)) {
750 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
751 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
752 if (ret < 0 || (size_t) ret >= buflen)
753 return -1;
754 return ret;
755 }
756 }
757
758 ret = os_snprintf(buf, buflen, "%s", pin);
759 if (ret < 0 || (size_t) ret >= buflen)
760 return -1;
761
762 return ret;
763}
764
765
Dmitry Shmidt04949592012-07-19 12:16:46 -0700766#ifdef CONFIG_WPS_NFC
767
768static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
769 char *cmd)
770{
771 u8 bssid[ETH_ALEN], *_bssid = bssid;
772
773 if (cmd == NULL || cmd[0] == '\0')
774 _bssid = NULL;
775 else if (hwaddr_aton(cmd, bssid))
776 return -1;
777
778 return wpas_wps_start_nfc(wpa_s, _bssid);
779}
780
781
782static int wpa_supplicant_ctrl_iface_wps_nfc_token(
783 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
784{
785 int ndef;
786 struct wpabuf *buf;
787 int res;
788
789 if (os_strcmp(cmd, "WPS") == 0)
790 ndef = 0;
791 else if (os_strcmp(cmd, "NDEF") == 0)
792 ndef = 1;
793 else
794 return -1;
795
796 buf = wpas_wps_nfc_token(wpa_s, ndef);
797 if (buf == NULL)
798 return -1;
799
800 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
801 wpabuf_len(buf));
802 reply[res++] = '\n';
803 reply[res] = '\0';
804
805 wpabuf_free(buf);
806
807 return res;
808}
809
810
811static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
812 struct wpa_supplicant *wpa_s, char *pos)
813{
814 size_t len;
815 struct wpabuf *buf;
816 int ret;
817
818 len = os_strlen(pos);
819 if (len & 0x01)
820 return -1;
821 len /= 2;
822
823 buf = wpabuf_alloc(len);
824 if (buf == NULL)
825 return -1;
826 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
827 wpabuf_free(buf);
828 return -1;
829 }
830
831 ret = wpas_wps_nfc_tag_read(wpa_s, buf);
832 wpabuf_free(buf);
833
834 return ret;
835}
836
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800837
838static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
839 char *reply, size_t max_len)
840{
841 struct wpabuf *buf;
842 int res;
843
844 buf = wpas_wps_nfc_handover_req(wpa_s);
845 if (buf == NULL)
846 return -1;
847
848 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
849 wpabuf_len(buf));
850 reply[res++] = '\n';
851 reply[res] = '\0';
852
853 wpabuf_free(buf);
854
855 return res;
856}
857
858
859static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
860 char *cmd, char *reply,
861 size_t max_len)
862{
863 char *pos;
864
865 pos = os_strchr(cmd, ' ');
866 if (pos == NULL)
867 return -1;
868 *pos++ = '\0';
869
870 if (os_strcmp(cmd, "NDEF") != 0)
871 return -1;
872
873 if (os_strcmp(pos, "WPS") == 0) {
874 return wpas_ctrl_nfc_get_handover_req_wps(wpa_s, reply,
875 max_len);
876 }
877
878 return -1;
879}
880
881
882static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
883 char *reply, size_t max_len)
884{
885 struct wpabuf *buf;
886 int res;
887
888 buf = wpas_wps_nfc_handover_sel(wpa_s);
889 if (buf == NULL)
890 return -1;
891
892 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
893 wpabuf_len(buf));
894 reply[res++] = '\n';
895 reply[res] = '\0';
896
897 wpabuf_free(buf);
898
899 return res;
900}
901
902
903static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
904 char *cmd, char *reply,
905 size_t max_len)
906{
907 char *pos;
908
909 pos = os_strchr(cmd, ' ');
910 if (pos == NULL)
911 return -1;
912 *pos++ = '\0';
913
914 if (os_strcmp(cmd, "NDEF") != 0)
915 return -1;
916
917 if (os_strcmp(pos, "WPS") == 0) {
918 return wpas_ctrl_nfc_get_handover_sel_wps(wpa_s, reply,
919 max_len);
920 }
921
922 return -1;
923}
924
925
926static int wpas_ctrl_nfc_rx_handover_req(struct wpa_supplicant *wpa_s,
927 char *cmd, char *reply,
928 size_t max_len)
929{
930 size_t len;
931 struct wpabuf *buf;
932 int ret;
933
934 len = os_strlen(cmd);
935 if (len & 0x01)
936 return -1;
937 len /= 2;
938
939 buf = wpabuf_alloc(len);
940 if (buf == NULL)
941 return -1;
942 if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
943 wpabuf_free(buf);
944 return -1;
945 }
946
947 ret = wpas_wps_nfc_rx_handover_req(wpa_s, buf);
948 wpabuf_free(buf);
949
950 return ret;
951}
952
953
954static int wpas_ctrl_nfc_rx_handover_sel(struct wpa_supplicant *wpa_s,
955 char *cmd)
956{
957 size_t len;
958 struct wpabuf *buf;
959 int ret;
960
961 len = os_strlen(cmd);
962 if (len & 0x01)
963 return -1;
964 len /= 2;
965
966 buf = wpabuf_alloc(len);
967 if (buf == NULL)
968 return -1;
969 if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
970 wpabuf_free(buf);
971 return -1;
972 }
973
974 ret = wpas_wps_nfc_rx_handover_sel(wpa_s, buf);
975 wpabuf_free(buf);
976
977 return ret;
978}
979
Dmitry Shmidt04949592012-07-19 12:16:46 -0700980#endif /* CONFIG_WPS_NFC */
981
982
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700983static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
984 char *cmd)
985{
986 u8 bssid[ETH_ALEN];
987 char *pin;
988 char *new_ssid;
989 char *new_auth;
990 char *new_encr;
991 char *new_key;
992 struct wps_new_ap_settings ap;
993
994 pin = os_strchr(cmd, ' ');
995 if (pin == NULL)
996 return -1;
997 *pin++ = '\0';
998
999 if (hwaddr_aton(cmd, bssid)) {
1000 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
1001 cmd);
1002 return -1;
1003 }
1004
1005 new_ssid = os_strchr(pin, ' ');
1006 if (new_ssid == NULL)
1007 return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
1008 *new_ssid++ = '\0';
1009
1010 new_auth = os_strchr(new_ssid, ' ');
1011 if (new_auth == NULL)
1012 return -1;
1013 *new_auth++ = '\0';
1014
1015 new_encr = os_strchr(new_auth, ' ');
1016 if (new_encr == NULL)
1017 return -1;
1018 *new_encr++ = '\0';
1019
1020 new_key = os_strchr(new_encr, ' ');
1021 if (new_key == NULL)
1022 return -1;
1023 *new_key++ = '\0';
1024
1025 os_memset(&ap, 0, sizeof(ap));
1026 ap.ssid_hex = new_ssid;
1027 ap.auth = new_auth;
1028 ap.encr = new_encr;
1029 ap.key_hex = new_key;
1030 return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
1031}
1032
1033
1034#ifdef CONFIG_AP
1035static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
1036 char *cmd, char *buf,
1037 size_t buflen)
1038{
1039 int timeout = 300;
1040 char *pos;
1041 const char *pin_txt;
1042
1043 if (!wpa_s->ap_iface)
1044 return -1;
1045
1046 pos = os_strchr(cmd, ' ');
1047 if (pos)
1048 *pos++ = '\0';
1049
1050 if (os_strcmp(cmd, "disable") == 0) {
1051 wpas_wps_ap_pin_disable(wpa_s);
1052 return os_snprintf(buf, buflen, "OK\n");
1053 }
1054
1055 if (os_strcmp(cmd, "random") == 0) {
1056 if (pos)
1057 timeout = atoi(pos);
1058 pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
1059 if (pin_txt == NULL)
1060 return -1;
1061 return os_snprintf(buf, buflen, "%s", pin_txt);
1062 }
1063
1064 if (os_strcmp(cmd, "get") == 0) {
1065 pin_txt = wpas_wps_ap_pin_get(wpa_s);
1066 if (pin_txt == NULL)
1067 return -1;
1068 return os_snprintf(buf, buflen, "%s", pin_txt);
1069 }
1070
1071 if (os_strcmp(cmd, "set") == 0) {
1072 char *pin;
1073 if (pos == NULL)
1074 return -1;
1075 pin = pos;
1076 pos = os_strchr(pos, ' ');
1077 if (pos) {
1078 *pos++ = '\0';
1079 timeout = atoi(pos);
1080 }
1081 if (os_strlen(pin) > buflen)
1082 return -1;
1083 if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
1084 return -1;
1085 return os_snprintf(buf, buflen, "%s", pin);
1086 }
1087
1088 return -1;
1089}
1090#endif /* CONFIG_AP */
1091
1092
1093#ifdef CONFIG_WPS_ER
1094static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
1095 char *cmd)
1096{
1097 char *uuid = cmd, *pin, *pos;
1098 u8 addr_buf[ETH_ALEN], *addr = NULL;
1099 pin = os_strchr(uuid, ' ');
1100 if (pin == NULL)
1101 return -1;
1102 *pin++ = '\0';
1103 pos = os_strchr(pin, ' ');
1104 if (pos) {
1105 *pos++ = '\0';
1106 if (hwaddr_aton(pos, addr_buf) == 0)
1107 addr = addr_buf;
1108 }
1109 return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
1110}
1111
1112
1113static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
1114 char *cmd)
1115{
1116 char *uuid = cmd, *pin;
1117 pin = os_strchr(uuid, ' ');
1118 if (pin == NULL)
1119 return -1;
1120 *pin++ = '\0';
1121 return wpas_wps_er_learn(wpa_s, uuid, pin);
1122}
1123
1124
1125static int wpa_supplicant_ctrl_iface_wps_er_set_config(
1126 struct wpa_supplicant *wpa_s, char *cmd)
1127{
1128 char *uuid = cmd, *id;
1129 id = os_strchr(uuid, ' ');
1130 if (id == NULL)
1131 return -1;
1132 *id++ = '\0';
1133 return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
1134}
1135
1136
1137static int wpa_supplicant_ctrl_iface_wps_er_config(
1138 struct wpa_supplicant *wpa_s, char *cmd)
1139{
1140 char *pin;
1141 char *new_ssid;
1142 char *new_auth;
1143 char *new_encr;
1144 char *new_key;
1145 struct wps_new_ap_settings ap;
1146
1147 pin = os_strchr(cmd, ' ');
1148 if (pin == NULL)
1149 return -1;
1150 *pin++ = '\0';
1151
1152 new_ssid = os_strchr(pin, ' ');
1153 if (new_ssid == NULL)
1154 return -1;
1155 *new_ssid++ = '\0';
1156
1157 new_auth = os_strchr(new_ssid, ' ');
1158 if (new_auth == NULL)
1159 return -1;
1160 *new_auth++ = '\0';
1161
1162 new_encr = os_strchr(new_auth, ' ');
1163 if (new_encr == NULL)
1164 return -1;
1165 *new_encr++ = '\0';
1166
1167 new_key = os_strchr(new_encr, ' ');
1168 if (new_key == NULL)
1169 return -1;
1170 *new_key++ = '\0';
1171
1172 os_memset(&ap, 0, sizeof(ap));
1173 ap.ssid_hex = new_ssid;
1174 ap.auth = new_auth;
1175 ap.encr = new_encr;
1176 ap.key_hex = new_key;
1177 return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
1178}
Dmitry Shmidt04949592012-07-19 12:16:46 -07001179
1180
1181#ifdef CONFIG_WPS_NFC
1182static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
1183 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1184{
1185 int ndef;
1186 struct wpabuf *buf;
1187 int res;
1188 char *uuid;
1189
1190 uuid = os_strchr(cmd, ' ');
1191 if (uuid == NULL)
1192 return -1;
1193 *uuid++ = '\0';
1194
1195 if (os_strcmp(cmd, "WPS") == 0)
1196 ndef = 0;
1197 else if (os_strcmp(cmd, "NDEF") == 0)
1198 ndef = 1;
1199 else
1200 return -1;
1201
1202 buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
1203 if (buf == NULL)
1204 return -1;
1205
1206 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1207 wpabuf_len(buf));
1208 reply[res++] = '\n';
1209 reply[res] = '\0';
1210
1211 wpabuf_free(buf);
1212
1213 return res;
1214}
1215#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001216#endif /* CONFIG_WPS_ER */
1217
1218#endif /* CONFIG_WPS */
1219
1220
1221#ifdef CONFIG_IBSS_RSN
1222static int wpa_supplicant_ctrl_iface_ibss_rsn(
1223 struct wpa_supplicant *wpa_s, char *addr)
1224{
1225 u8 peer[ETH_ALEN];
1226
1227 if (hwaddr_aton(addr, peer)) {
1228 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
1229 "address '%s'", addr);
1230 return -1;
1231 }
1232
1233 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
1234 MAC2STR(peer));
1235
1236 return ibss_rsn_start(wpa_s->ibss_rsn, peer);
1237}
1238#endif /* CONFIG_IBSS_RSN */
1239
1240
1241static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
1242 char *rsp)
1243{
1244#ifdef IEEE8021X_EAPOL
1245 char *pos, *id_pos;
1246 int id;
1247 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001248
1249 pos = os_strchr(rsp, '-');
1250 if (pos == NULL)
1251 return -1;
1252 *pos++ = '\0';
1253 id_pos = pos;
1254 pos = os_strchr(pos, ':');
1255 if (pos == NULL)
1256 return -1;
1257 *pos++ = '\0';
1258 id = atoi(id_pos);
1259 wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
1260 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
1261 (u8 *) pos, os_strlen(pos));
1262
1263 ssid = wpa_config_get_network(wpa_s->conf, id);
1264 if (ssid == NULL) {
1265 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
1266 "to update", id);
1267 return -1;
1268 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001269
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001270 return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
1271 pos);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001272#else /* IEEE8021X_EAPOL */
1273 wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
1274 return -1;
1275#endif /* IEEE8021X_EAPOL */
1276}
1277
1278
1279static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
1280 const char *params,
1281 char *buf, size_t buflen)
1282{
1283 char *pos, *end, tmp[30];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001284 int res, verbose, wps, ret;
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001285
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001286 verbose = os_strcmp(params, "-VERBOSE") == 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001287 wps = os_strcmp(params, "-WPS") == 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001288 pos = buf;
1289 end = buf + buflen;
1290 if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1291 struct wpa_ssid *ssid = wpa_s->current_ssid;
1292 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
1293 MAC2STR(wpa_s->bssid));
1294 if (ret < 0 || ret >= end - pos)
1295 return pos - buf;
1296 pos += ret;
1297 if (ssid) {
1298 u8 *_ssid = ssid->ssid;
1299 size_t ssid_len = ssid->ssid_len;
1300 u8 ssid_buf[MAX_SSID_LEN];
1301 if (ssid_len == 0) {
1302 int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
1303 if (_res < 0)
1304 ssid_len = 0;
1305 else
1306 ssid_len = _res;
1307 _ssid = ssid_buf;
1308 }
1309 ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
1310 wpa_ssid_txt(_ssid, ssid_len),
1311 ssid->id);
1312 if (ret < 0 || ret >= end - pos)
1313 return pos - buf;
1314 pos += ret;
1315
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001316 if (wps && ssid->passphrase &&
1317 wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
1318 (ssid->mode == WPAS_MODE_AP ||
1319 ssid->mode == WPAS_MODE_P2P_GO)) {
1320 ret = os_snprintf(pos, end - pos,
1321 "passphrase=%s\n",
1322 ssid->passphrase);
1323 if (ret < 0 || ret >= end - pos)
1324 return pos - buf;
1325 pos += ret;
1326 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001327 if (ssid->id_str) {
1328 ret = os_snprintf(pos, end - pos,
1329 "id_str=%s\n",
1330 ssid->id_str);
1331 if (ret < 0 || ret >= end - pos)
1332 return pos - buf;
1333 pos += ret;
1334 }
1335
1336 switch (ssid->mode) {
1337 case WPAS_MODE_INFRA:
1338 ret = os_snprintf(pos, end - pos,
1339 "mode=station\n");
1340 break;
1341 case WPAS_MODE_IBSS:
1342 ret = os_snprintf(pos, end - pos,
1343 "mode=IBSS\n");
1344 break;
1345 case WPAS_MODE_AP:
1346 ret = os_snprintf(pos, end - pos,
1347 "mode=AP\n");
1348 break;
1349 case WPAS_MODE_P2P_GO:
1350 ret = os_snprintf(pos, end - pos,
1351 "mode=P2P GO\n");
1352 break;
1353 case WPAS_MODE_P2P_GROUP_FORMATION:
1354 ret = os_snprintf(pos, end - pos,
1355 "mode=P2P GO - group "
1356 "formation\n");
1357 break;
1358 default:
1359 ret = 0;
1360 break;
1361 }
1362 if (ret < 0 || ret >= end - pos)
1363 return pos - buf;
1364 pos += ret;
1365 }
1366
1367#ifdef CONFIG_AP
1368 if (wpa_s->ap_iface) {
1369 pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
1370 end - pos,
1371 verbose);
1372 } else
1373#endif /* CONFIG_AP */
1374 pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
1375 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001376#ifdef CONFIG_SAE
1377 if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
1378 wpa_s->sme.sae.state == SAE_ACCEPTED && !wpa_s->ap_iface) {
1379 ret = os_snprintf(pos, end - pos, "sae_group=%d\n",
1380 wpa_s->sme.sae.group);
1381 if (ret < 0 || ret >= end - pos)
1382 return pos - buf;
1383 pos += ret;
1384 }
1385#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001386 ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
1387 wpa_supplicant_state_txt(wpa_s->wpa_state));
1388 if (ret < 0 || ret >= end - pos)
1389 return pos - buf;
1390 pos += ret;
1391
1392 if (wpa_s->l2 &&
1393 l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
1394 ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
1395 if (ret < 0 || ret >= end - pos)
1396 return pos - buf;
1397 pos += ret;
1398 }
1399
1400#ifdef CONFIG_P2P
1401 if (wpa_s->global->p2p) {
1402 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
1403 "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
1404 if (ret < 0 || ret >= end - pos)
1405 return pos - buf;
1406 pos += ret;
1407 }
1408#endif /* CONFIG_P2P */
1409
1410 ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
1411 MAC2STR(wpa_s->own_addr));
1412 if (ret < 0 || ret >= end - pos)
1413 return pos - buf;
1414 pos += ret;
1415
Dmitry Shmidt04949592012-07-19 12:16:46 -07001416#ifdef CONFIG_HS20
1417 if (wpa_s->current_bss &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001418 wpa_bss_get_vendor_ie(wpa_s->current_bss, HS20_IE_VENDOR_TYPE) &&
1419 wpa_s->wpa_proto == WPA_PROTO_RSN &&
1420 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001421 ret = os_snprintf(pos, end - pos, "hs20=1\n");
1422 if (ret < 0 || ret >= end - pos)
1423 return pos - buf;
1424 pos += ret;
1425 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001426
1427 if (wpa_s->current_ssid) {
1428 struct wpa_cred *cred;
1429 char *type;
1430
1431 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1432 if (wpa_s->current_ssid->parent_cred != cred)
1433 continue;
1434 if (!cred->domain)
1435 continue;
1436
1437 ret = os_snprintf(pos, end - pos, "home_sp=%s\n",
1438 cred->domain);
1439 if (ret < 0 || ret >= end - pos)
1440 return pos - buf;
1441 pos += ret;
1442
1443 if (wpa_s->current_bss == NULL ||
1444 wpa_s->current_bss->anqp == NULL)
1445 res = -1;
1446 else
1447 res = interworking_home_sp_cred(
1448 wpa_s, cred,
1449 wpa_s->current_bss->anqp->domain_name);
1450 if (res > 0)
1451 type = "home";
1452 else if (res == 0)
1453 type = "roaming";
1454 else
1455 type = "unknown";
1456
1457 ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
1458 if (ret < 0 || ret >= end - pos)
1459 return pos - buf;
1460 pos += ret;
1461
1462 break;
1463 }
1464 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001465#endif /* CONFIG_HS20 */
1466
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001467 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
1468 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1469 res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
1470 verbose);
1471 if (res >= 0)
1472 pos += res;
1473 }
1474
1475 res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
1476 if (res >= 0)
1477 pos += res;
1478
Dmitry Shmidt2fd7fa62011-12-19 11:19:09 -08001479#ifdef ANDROID
1480 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
Irfan Sherifff20a4432012-04-16 16:48:34 -07001481 "id=%d state=%d BSSID=" MACSTR " SSID=%s",
Dmitry Shmidt2fd7fa62011-12-19 11:19:09 -08001482 wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
Irfan Sherifff20a4432012-04-16 16:48:34 -07001483 wpa_s->wpa_state,
Irfan Sheriffe78e7672012-08-01 11:10:15 -07001484 MAC2STR(wpa_s->bssid),
andy2_kuo5b5fb022012-05-22 11:53:07 -07001485 wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
1486 wpa_ssid_txt(wpa_s->current_ssid->ssid,
Irfan Sheriff10294772012-05-11 11:23:35 -07001487 wpa_s->current_ssid->ssid_len) : "");
Irfan Sheriffbf5edf42012-01-11 16:54:57 -08001488 if (wpa_s->wpa_state == WPA_COMPLETED) {
1489 struct wpa_ssid *ssid = wpa_s->current_ssid;
1490 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- connection to "
1491 MACSTR " completed %s [id=%d id_str=%s]",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001492 MAC2STR(wpa_s->bssid), "(auth)",
Irfan Sheriffbf5edf42012-01-11 16:54:57 -08001493 ssid ? ssid->id : -1,
1494 ssid && ssid->id_str ? ssid->id_str : "");
1495 }
Dmitry Shmidt2fd7fa62011-12-19 11:19:09 -08001496#endif /* ANDROID */
1497
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001498 return pos - buf;
1499}
1500
1501
1502static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
1503 char *cmd)
1504{
1505 char *pos;
1506 int id;
1507 struct wpa_ssid *ssid;
1508 u8 bssid[ETH_ALEN];
1509
1510 /* cmd: "<network id> <BSSID>" */
1511 pos = os_strchr(cmd, ' ');
1512 if (pos == NULL)
1513 return -1;
1514 *pos++ = '\0';
1515 id = atoi(cmd);
1516 wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
1517 if (hwaddr_aton(pos, bssid)) {
1518 wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
1519 return -1;
1520 }
1521
1522 ssid = wpa_config_get_network(wpa_s->conf, id);
1523 if (ssid == NULL) {
1524 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
1525 "to update", id);
1526 return -1;
1527 }
1528
1529 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
1530 ssid->bssid_set = !is_zero_ether_addr(bssid);
1531
1532 return 0;
1533}
1534
1535
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001536static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001537 char *cmd, char *buf,
1538 size_t buflen)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001539{
1540 u8 bssid[ETH_ALEN];
1541 struct wpa_blacklist *e;
1542 char *pos, *end;
1543 int ret;
1544
1545 /* cmd: "BLACKLIST [<BSSID>]" */
1546 if (*cmd == '\0') {
1547 pos = buf;
1548 end = buf + buflen;
1549 e = wpa_s->blacklist;
1550 while (e) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001551 ret = os_snprintf(pos, end - pos, MACSTR "\n",
1552 MAC2STR(e->bssid));
1553 if (ret < 0 || ret >= end - pos)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001554 return pos - buf;
1555 pos += ret;
1556 e = e->next;
1557 }
1558 return pos - buf;
1559 }
1560
1561 cmd++;
1562 if (os_strncmp(cmd, "clear", 5) == 0) {
1563 wpa_blacklist_clear(wpa_s);
1564 os_memcpy(buf, "OK\n", 3);
1565 return 3;
1566 }
1567
1568 wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
1569 if (hwaddr_aton(cmd, bssid)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001570 wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001571 return -1;
1572 }
1573
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001574 /*
1575 * Add the BSSID twice, so its count will be 2, causing it to be
1576 * skipped when processing scan results.
1577 */
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001578 ret = wpa_blacklist_add(wpa_s, bssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001579 if (ret != 0)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001580 return -1;
1581 ret = wpa_blacklist_add(wpa_s, bssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001582 if (ret != 0)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001583 return -1;
1584 os_memcpy(buf, "OK\n", 3);
1585 return 3;
1586}
1587
1588
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001589extern int wpa_debug_level;
1590extern int wpa_debug_timestamp;
1591
1592static const char * debug_level_str(int level)
1593{
1594 switch (level) {
1595 case MSG_EXCESSIVE:
1596 return "EXCESSIVE";
1597 case MSG_MSGDUMP:
1598 return "MSGDUMP";
1599 case MSG_DEBUG:
1600 return "DEBUG";
1601 case MSG_INFO:
1602 return "INFO";
1603 case MSG_WARNING:
1604 return "WARNING";
1605 case MSG_ERROR:
1606 return "ERROR";
1607 default:
1608 return "?";
1609 }
1610}
1611
1612
1613static int str_to_debug_level(const char *s)
1614{
1615 if (os_strcasecmp(s, "EXCESSIVE") == 0)
1616 return MSG_EXCESSIVE;
1617 if (os_strcasecmp(s, "MSGDUMP") == 0)
1618 return MSG_MSGDUMP;
1619 if (os_strcasecmp(s, "DEBUG") == 0)
1620 return MSG_DEBUG;
1621 if (os_strcasecmp(s, "INFO") == 0)
1622 return MSG_INFO;
1623 if (os_strcasecmp(s, "WARNING") == 0)
1624 return MSG_WARNING;
1625 if (os_strcasecmp(s, "ERROR") == 0)
1626 return MSG_ERROR;
1627 return -1;
1628}
1629
1630
1631static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
1632 char *cmd, char *buf,
1633 size_t buflen)
1634{
1635 char *pos, *end, *stamp;
1636 int ret;
1637
1638 if (cmd == NULL) {
1639 return -1;
1640 }
1641
1642 /* cmd: "LOG_LEVEL [<level>]" */
1643 if (*cmd == '\0') {
1644 pos = buf;
1645 end = buf + buflen;
1646 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
1647 "Timestamp: %d\n",
1648 debug_level_str(wpa_debug_level),
1649 wpa_debug_timestamp);
1650 if (ret < 0 || ret >= end - pos)
1651 ret = 0;
1652
1653 return ret;
1654 }
1655
1656 while (*cmd == ' ')
1657 cmd++;
1658
1659 stamp = os_strchr(cmd, ' ');
1660 if (stamp) {
1661 *stamp++ = '\0';
1662 while (*stamp == ' ') {
1663 stamp++;
1664 }
1665 }
1666
1667 if (cmd && os_strlen(cmd)) {
1668 int level = str_to_debug_level(cmd);
1669 if (level < 0)
1670 return -1;
1671 wpa_debug_level = level;
1672 }
1673
1674 if (stamp && os_strlen(stamp))
1675 wpa_debug_timestamp = atoi(stamp);
1676
1677 os_memcpy(buf, "OK\n", 3);
1678 return 3;
1679}
1680
1681
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001682static int wpa_supplicant_ctrl_iface_list_networks(
1683 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
1684{
1685 char *pos, *end;
1686 struct wpa_ssid *ssid;
1687 int ret;
1688
1689 pos = buf;
1690 end = buf + buflen;
1691 ret = os_snprintf(pos, end - pos,
1692 "network id / ssid / bssid / flags\n");
1693 if (ret < 0 || ret >= end - pos)
1694 return pos - buf;
1695 pos += ret;
1696
1697 ssid = wpa_s->conf->ssid;
1698 while (ssid) {
1699 ret = os_snprintf(pos, end - pos, "%d\t%s",
1700 ssid->id,
1701 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1702 if (ret < 0 || ret >= end - pos)
1703 return pos - buf;
1704 pos += ret;
1705 if (ssid->bssid_set) {
1706 ret = os_snprintf(pos, end - pos, "\t" MACSTR,
1707 MAC2STR(ssid->bssid));
1708 } else {
1709 ret = os_snprintf(pos, end - pos, "\tany");
1710 }
1711 if (ret < 0 || ret >= end - pos)
1712 return pos - buf;
1713 pos += ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001714 ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001715 ssid == wpa_s->current_ssid ?
1716 "[CURRENT]" : "",
1717 ssid->disabled ? "[DISABLED]" : "",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001718 ssid->disabled_until.sec ?
1719 "[TEMP-DISABLED]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001720 ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
1721 "");
1722 if (ret < 0 || ret >= end - pos)
1723 return pos - buf;
1724 pos += ret;
1725 ret = os_snprintf(pos, end - pos, "\n");
1726 if (ret < 0 || ret >= end - pos)
1727 return pos - buf;
1728 pos += ret;
1729
1730 ssid = ssid->next;
1731 }
1732
1733 return pos - buf;
1734}
1735
1736
1737static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
1738{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001739 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001740 ret = os_snprintf(pos, end - pos, "-");
1741 if (ret < 0 || ret >= end - pos)
1742 return pos;
1743 pos += ret;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001744 ret = wpa_write_ciphers(pos, end, cipher, "+");
1745 if (ret < 0)
1746 return pos;
1747 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001748 return pos;
1749}
1750
1751
1752static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
1753 const u8 *ie, size_t ie_len)
1754{
1755 struct wpa_ie_data data;
1756 int first, ret;
1757
1758 ret = os_snprintf(pos, end - pos, "[%s-", proto);
1759 if (ret < 0 || ret >= end - pos)
1760 return pos;
1761 pos += ret;
1762
1763 if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
1764 ret = os_snprintf(pos, end - pos, "?]");
1765 if (ret < 0 || ret >= end - pos)
1766 return pos;
1767 pos += ret;
1768 return pos;
1769 }
1770
1771 first = 1;
1772 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
1773 ret = os_snprintf(pos, end - pos, "%sEAP", first ? "" : "+");
1774 if (ret < 0 || ret >= end - pos)
1775 return pos;
1776 pos += ret;
1777 first = 0;
1778 }
1779 if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
1780 ret = os_snprintf(pos, end - pos, "%sPSK", first ? "" : "+");
1781 if (ret < 0 || ret >= end - pos)
1782 return pos;
1783 pos += ret;
1784 first = 0;
1785 }
1786 if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
1787 ret = os_snprintf(pos, end - pos, "%sNone", first ? "" : "+");
1788 if (ret < 0 || ret >= end - pos)
1789 return pos;
1790 pos += ret;
1791 first = 0;
1792 }
1793#ifdef CONFIG_IEEE80211R
1794 if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
1795 ret = os_snprintf(pos, end - pos, "%sFT/EAP",
1796 first ? "" : "+");
1797 if (ret < 0 || ret >= end - pos)
1798 return pos;
1799 pos += ret;
1800 first = 0;
1801 }
1802 if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
1803 ret = os_snprintf(pos, end - pos, "%sFT/PSK",
1804 first ? "" : "+");
1805 if (ret < 0 || ret >= end - pos)
1806 return pos;
1807 pos += ret;
1808 first = 0;
1809 }
1810#endif /* CONFIG_IEEE80211R */
1811#ifdef CONFIG_IEEE80211W
1812 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1813 ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
1814 first ? "" : "+");
1815 if (ret < 0 || ret >= end - pos)
1816 return pos;
1817 pos += ret;
1818 first = 0;
1819 }
1820 if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
1821 ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
1822 first ? "" : "+");
1823 if (ret < 0 || ret >= end - pos)
1824 return pos;
1825 pos += ret;
1826 first = 0;
1827 }
1828#endif /* CONFIG_IEEE80211W */
1829
1830 pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
1831
1832 if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
1833 ret = os_snprintf(pos, end - pos, "-preauth");
1834 if (ret < 0 || ret >= end - pos)
1835 return pos;
1836 pos += ret;
1837 }
1838
1839 ret = os_snprintf(pos, end - pos, "]");
1840 if (ret < 0 || ret >= end - pos)
1841 return pos;
1842 pos += ret;
1843
1844 return pos;
1845}
1846
1847
1848#ifdef CONFIG_WPS
1849static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
1850 char *pos, char *end,
1851 struct wpabuf *wps_ie)
1852{
1853 int ret;
1854 const char *txt;
1855
1856 if (wps_ie == NULL)
1857 return pos;
1858 if (wps_is_selected_pbc_registrar(wps_ie))
1859 txt = "[WPS-PBC]";
1860#ifdef CONFIG_WPS2
1861 else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
1862 txt = "[WPS-AUTH]";
1863#endif /* CONFIG_WPS2 */
1864 else if (wps_is_selected_pin_registrar(wps_ie))
1865 txt = "[WPS-PIN]";
1866 else
1867 txt = "[WPS]";
1868
1869 ret = os_snprintf(pos, end - pos, "%s", txt);
1870 if (ret >= 0 && ret < end - pos)
1871 pos += ret;
1872 wpabuf_free(wps_ie);
1873 return pos;
1874}
1875#endif /* CONFIG_WPS */
1876
1877
1878static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
1879 char *pos, char *end,
1880 const struct wpa_bss *bss)
1881{
1882#ifdef CONFIG_WPS
1883 struct wpabuf *wps_ie;
1884 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1885 return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
1886#else /* CONFIG_WPS */
1887 return pos;
1888#endif /* CONFIG_WPS */
1889}
1890
1891
1892/* Format one result on one text line into a buffer. */
1893static int wpa_supplicant_ctrl_iface_scan_result(
1894 struct wpa_supplicant *wpa_s,
1895 const struct wpa_bss *bss, char *buf, size_t buflen)
1896{
1897 char *pos, *end;
1898 int ret;
1899 const u8 *ie, *ie2, *p2p;
1900
1901 p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
1902 if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
1903 os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
1904 0)
1905 return 0; /* Do not show P2P listen discovery results here */
1906
1907 pos = buf;
1908 end = buf + buflen;
1909
1910 ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
1911 MAC2STR(bss->bssid), bss->freq, bss->level);
1912 if (ret < 0 || ret >= end - pos)
1913 return -1;
1914 pos += ret;
1915 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
1916 if (ie)
1917 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
1918 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
1919 if (ie2)
1920 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
1921 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
1922 if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
1923 ret = os_snprintf(pos, end - pos, "[WEP]");
1924 if (ret < 0 || ret >= end - pos)
1925 return -1;
1926 pos += ret;
1927 }
1928 if (bss->caps & IEEE80211_CAP_IBSS) {
1929 ret = os_snprintf(pos, end - pos, "[IBSS]");
1930 if (ret < 0 || ret >= end - pos)
1931 return -1;
1932 pos += ret;
1933 }
1934 if (bss->caps & IEEE80211_CAP_ESS) {
1935 ret = os_snprintf(pos, end - pos, "[ESS]");
1936 if (ret < 0 || ret >= end - pos)
1937 return -1;
1938 pos += ret;
1939 }
1940 if (p2p) {
1941 ret = os_snprintf(pos, end - pos, "[P2P]");
1942 if (ret < 0 || ret >= end - pos)
1943 return -1;
1944 pos += ret;
1945 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001946#ifdef CONFIG_HS20
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001947 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001948 ret = os_snprintf(pos, end - pos, "[HS20]");
1949 if (ret < 0 || ret >= end - pos)
1950 return -1;
1951 pos += ret;
1952 }
1953#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001954
1955 ret = os_snprintf(pos, end - pos, "\t%s",
1956 wpa_ssid_txt(bss->ssid, bss->ssid_len));
1957 if (ret < 0 || ret >= end - pos)
1958 return -1;
1959 pos += ret;
1960
1961 ret = os_snprintf(pos, end - pos, "\n");
1962 if (ret < 0 || ret >= end - pos)
1963 return -1;
1964 pos += ret;
1965
1966 return pos - buf;
1967}
1968
1969
1970static int wpa_supplicant_ctrl_iface_scan_results(
1971 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
1972{
1973 char *pos, *end;
1974 struct wpa_bss *bss;
1975 int ret;
1976
1977 pos = buf;
1978 end = buf + buflen;
1979 ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
1980 "flags / ssid\n");
1981 if (ret < 0 || ret >= end - pos)
1982 return pos - buf;
1983 pos += ret;
1984
1985 dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
1986 ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
1987 end - pos);
1988 if (ret < 0 || ret >= end - pos)
1989 return pos - buf;
1990 pos += ret;
1991 }
1992
1993 return pos - buf;
1994}
1995
1996
1997static int wpa_supplicant_ctrl_iface_select_network(
1998 struct wpa_supplicant *wpa_s, char *cmd)
1999{
2000 int id;
2001 struct wpa_ssid *ssid;
2002
2003 /* cmd: "<network id>" or "any" */
2004 if (os_strcmp(cmd, "any") == 0) {
2005 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
2006 ssid = NULL;
2007 } else {
2008 id = atoi(cmd);
2009 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
2010
2011 ssid = wpa_config_get_network(wpa_s->conf, id);
2012 if (ssid == NULL) {
2013 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2014 "network id=%d", id);
2015 return -1;
2016 }
2017 if (ssid->disabled == 2) {
2018 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2019 "SELECT_NETWORK with persistent P2P group");
2020 return -1;
2021 }
2022 }
2023
2024 wpa_supplicant_select_network(wpa_s, ssid);
2025
2026 return 0;
2027}
2028
2029
2030static int wpa_supplicant_ctrl_iface_enable_network(
2031 struct wpa_supplicant *wpa_s, char *cmd)
2032{
2033 int id;
2034 struct wpa_ssid *ssid;
2035
2036 /* cmd: "<network id>" or "all" */
2037 if (os_strcmp(cmd, "all") == 0) {
2038 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
2039 ssid = NULL;
2040 } else {
2041 id = atoi(cmd);
2042 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
2043
2044 ssid = wpa_config_get_network(wpa_s->conf, id);
2045 if (ssid == NULL) {
2046 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2047 "network id=%d", id);
2048 return -1;
2049 }
2050 if (ssid->disabled == 2) {
2051 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2052 "ENABLE_NETWORK with persistent P2P group");
2053 return -1;
2054 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002055
2056 if (os_strstr(cmd, " no-connect")) {
2057 ssid->disabled = 0;
2058 return 0;
2059 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002060 }
2061 wpa_supplicant_enable_network(wpa_s, ssid);
2062
2063 return 0;
2064}
2065
2066
2067static int wpa_supplicant_ctrl_iface_disable_network(
2068 struct wpa_supplicant *wpa_s, char *cmd)
2069{
2070 int id;
2071 struct wpa_ssid *ssid;
2072
2073 /* cmd: "<network id>" or "all" */
2074 if (os_strcmp(cmd, "all") == 0) {
2075 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
2076 ssid = NULL;
2077 } else {
2078 id = atoi(cmd);
2079 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
2080
2081 ssid = wpa_config_get_network(wpa_s->conf, id);
2082 if (ssid == NULL) {
2083 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2084 "network id=%d", id);
2085 return -1;
2086 }
2087 if (ssid->disabled == 2) {
2088 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2089 "DISABLE_NETWORK with persistent P2P "
2090 "group");
2091 return -1;
2092 }
2093 }
2094 wpa_supplicant_disable_network(wpa_s, ssid);
2095
2096 return 0;
2097}
2098
2099
2100static int wpa_supplicant_ctrl_iface_add_network(
2101 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
2102{
2103 struct wpa_ssid *ssid;
2104 int ret;
2105
2106 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
2107
2108 ssid = wpa_config_add_network(wpa_s->conf);
2109 if (ssid == NULL)
2110 return -1;
2111
2112 wpas_notify_network_added(wpa_s, ssid);
2113
2114 ssid->disabled = 1;
2115 wpa_config_set_network_defaults(ssid);
2116
2117 ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
2118 if (ret < 0 || (size_t) ret >= buflen)
2119 return -1;
2120 return ret;
2121}
2122
2123
2124static int wpa_supplicant_ctrl_iface_remove_network(
2125 struct wpa_supplicant *wpa_s, char *cmd)
2126{
2127 int id;
2128 struct wpa_ssid *ssid;
2129
2130 /* cmd: "<network id>" or "all" */
2131 if (os_strcmp(cmd, "all") == 0) {
2132 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
2133 ssid = wpa_s->conf->ssid;
2134 while (ssid) {
2135 struct wpa_ssid *remove_ssid = ssid;
2136 id = ssid->id;
2137 ssid = ssid->next;
2138 wpas_notify_network_removed(wpa_s, remove_ssid);
2139 wpa_config_remove_network(wpa_s->conf, id);
2140 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002141 eapol_sm_invalidate_cached_session(wpa_s->eapol);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002142 if (wpa_s->current_ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002143#ifdef CONFIG_SME
2144 wpa_s->sme.prev_bssid_set = 0;
2145#endif /* CONFIG_SME */
Jouni Malinen75ecf522011-06-27 15:19:46 -07002146 wpa_sm_set_config(wpa_s->wpa, NULL);
2147 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002148 wpa_supplicant_deauthenticate(
2149 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002150 }
2151 return 0;
2152 }
2153
2154 id = atoi(cmd);
2155 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
2156
2157 ssid = wpa_config_get_network(wpa_s->conf, id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002158 if (ssid)
2159 wpas_notify_network_removed(wpa_s, ssid);
Deepthi Gowria831d782012-09-03 11:55:38 +03002160 if (ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002161 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2162 "id=%d", id);
2163 return -1;
2164 }
2165
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002166 if (ssid == wpa_s->current_ssid || wpa_s->current_ssid == NULL) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002167#ifdef CONFIG_SME
2168 wpa_s->sme.prev_bssid_set = 0;
2169#endif /* CONFIG_SME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002170 /*
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002171 * Invalidate the EAP session cache if the current or
2172 * previously used network is removed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002173 */
2174 eapol_sm_invalidate_cached_session(wpa_s->eapol);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002175 }
2176
2177 if (ssid == wpa_s->current_ssid) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07002178 wpa_sm_set_config(wpa_s->wpa, NULL);
2179 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002180
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002181 wpa_supplicant_deauthenticate(wpa_s,
2182 WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002183 }
2184
Deepthi Gowria831d782012-09-03 11:55:38 +03002185 if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
2186 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
2187 "network id=%d", id);
2188 return -1;
2189 }
2190
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002191 return 0;
2192}
2193
2194
2195static int wpa_supplicant_ctrl_iface_set_network(
2196 struct wpa_supplicant *wpa_s, char *cmd)
2197{
2198 int id;
2199 struct wpa_ssid *ssid;
2200 char *name, *value;
2201
2202 /* cmd: "<network id> <variable name> <value>" */
2203 name = os_strchr(cmd, ' ');
2204 if (name == NULL)
2205 return -1;
2206 *name++ = '\0';
2207
2208 value = os_strchr(name, ' ');
2209 if (value == NULL)
2210 return -1;
2211 *value++ = '\0';
2212
2213 id = atoi(cmd);
2214 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
2215 id, name);
2216 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2217 (u8 *) value, os_strlen(value));
2218
2219 ssid = wpa_config_get_network(wpa_s->conf, id);
2220 if (ssid == NULL) {
2221 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2222 "id=%d", id);
2223 return -1;
2224 }
2225
2226 if (wpa_config_set(ssid, name, value, 0) < 0) {
2227 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
2228 "variable '%s'", name);
2229 return -1;
2230 }
2231
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002232 if (os_strcmp(name, "bssid") != 0 &&
2233 os_strcmp(name, "priority") != 0)
2234 wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002235
2236 if (wpa_s->current_ssid == ssid || wpa_s->current_ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002237 /*
2238 * Invalidate the EAP session cache if anything in the current
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002239 * or previously used configuration changes.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002240 */
2241 eapol_sm_invalidate_cached_session(wpa_s->eapol);
2242 }
2243
2244 if ((os_strcmp(name, "psk") == 0 &&
2245 value[0] == '"' && ssid->ssid_len) ||
2246 (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
2247 wpa_config_update_psk(ssid);
2248 else if (os_strcmp(name, "priority") == 0)
2249 wpa_config_update_prio_list(wpa_s->conf);
2250
2251 return 0;
2252}
2253
2254
2255static int wpa_supplicant_ctrl_iface_get_network(
2256 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
2257{
2258 int id;
2259 size_t res;
2260 struct wpa_ssid *ssid;
2261 char *name, *value;
2262
2263 /* cmd: "<network id> <variable name>" */
2264 name = os_strchr(cmd, ' ');
2265 if (name == NULL || buflen == 0)
2266 return -1;
2267 *name++ = '\0';
2268
2269 id = atoi(cmd);
2270 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
2271 id, name);
2272
2273 ssid = wpa_config_get_network(wpa_s->conf, id);
2274 if (ssid == NULL) {
2275 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2276 "id=%d", id);
2277 return -1;
2278 }
2279
2280 value = wpa_config_get_no_key(ssid, name);
2281 if (value == NULL) {
2282 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
2283 "variable '%s'", name);
2284 return -1;
2285 }
2286
2287 res = os_strlcpy(buf, value, buflen);
2288 if (res >= buflen) {
2289 os_free(value);
2290 return -1;
2291 }
2292
2293 os_free(value);
2294
2295 return res;
2296}
2297
2298
Dmitry Shmidt04949592012-07-19 12:16:46 -07002299static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
2300 char *buf, size_t buflen)
2301{
2302 char *pos, *end;
2303 struct wpa_cred *cred;
2304 int ret;
2305
2306 pos = buf;
2307 end = buf + buflen;
2308 ret = os_snprintf(pos, end - pos,
2309 "cred id / realm / username / domain / imsi\n");
2310 if (ret < 0 || ret >= end - pos)
2311 return pos - buf;
2312 pos += ret;
2313
2314 cred = wpa_s->conf->cred;
2315 while (cred) {
2316 ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
2317 cred->id, cred->realm ? cred->realm : "",
2318 cred->username ? cred->username : "",
2319 cred->domain ? cred->domain : "",
2320 cred->imsi ? cred->imsi : "");
2321 if (ret < 0 || ret >= end - pos)
2322 return pos - buf;
2323 pos += ret;
2324
2325 cred = cred->next;
2326 }
2327
2328 return pos - buf;
2329}
2330
2331
2332static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
2333 char *buf, size_t buflen)
2334{
2335 struct wpa_cred *cred;
2336 int ret;
2337
2338 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
2339
2340 cred = wpa_config_add_cred(wpa_s->conf);
2341 if (cred == NULL)
2342 return -1;
2343
2344 ret = os_snprintf(buf, buflen, "%d\n", cred->id);
2345 if (ret < 0 || (size_t) ret >= buflen)
2346 return -1;
2347 return ret;
2348}
2349
2350
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002351static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
2352 struct wpa_cred *cred)
2353{
2354 struct wpa_ssid *ssid;
2355 char str[20];
2356
2357 if (cred == NULL || wpa_config_remove_cred(wpa_s->conf, cred->id) < 0) {
2358 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
2359 return -1;
2360 }
2361
2362 /* Remove any network entry created based on the removed credential */
2363 ssid = wpa_s->conf->ssid;
2364 while (ssid) {
2365 if (ssid->parent_cred == cred) {
2366 wpa_printf(MSG_DEBUG, "Remove network id %d since it "
2367 "used the removed credential", ssid->id);
2368 os_snprintf(str, sizeof(str), "%d", ssid->id);
2369 ssid = ssid->next;
2370 wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
2371 } else
2372 ssid = ssid->next;
2373 }
2374
2375 return 0;
2376}
2377
2378
Dmitry Shmidt04949592012-07-19 12:16:46 -07002379static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
2380 char *cmd)
2381{
2382 int id;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002383 struct wpa_cred *cred, *prev;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002384
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002385 /* cmd: "<cred id>", "all", or "sp_fqdn=<FQDN>" */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002386 if (os_strcmp(cmd, "all") == 0) {
2387 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
2388 cred = wpa_s->conf->cred;
2389 while (cred) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002390 prev = cred;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002391 cred = cred->next;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002392 wpas_ctrl_remove_cred(wpa_s, prev);
2393 }
2394 return 0;
2395 }
2396
2397 if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
2398 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
2399 cmd + 8);
2400 cred = wpa_s->conf->cred;
2401 while (cred) {
2402 prev = cred;
2403 cred = cred->next;
2404 if (prev->domain &&
2405 os_strcmp(prev->domain, cmd + 8) == 0)
2406 wpas_ctrl_remove_cred(wpa_s, prev);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002407 }
2408 return 0;
2409 }
2410
2411 id = atoi(cmd);
2412 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
2413
2414 cred = wpa_config_get_cred(wpa_s->conf, id);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002415 return wpas_ctrl_remove_cred(wpa_s, cred);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002416}
2417
2418
2419static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
2420 char *cmd)
2421{
2422 int id;
2423 struct wpa_cred *cred;
2424 char *name, *value;
2425
2426 /* cmd: "<cred id> <variable name> <value>" */
2427 name = os_strchr(cmd, ' ');
2428 if (name == NULL)
2429 return -1;
2430 *name++ = '\0';
2431
2432 value = os_strchr(name, ' ');
2433 if (value == NULL)
2434 return -1;
2435 *value++ = '\0';
2436
2437 id = atoi(cmd);
2438 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
2439 id, name);
2440 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2441 (u8 *) value, os_strlen(value));
2442
2443 cred = wpa_config_get_cred(wpa_s->conf, id);
2444 if (cred == NULL) {
2445 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
2446 id);
2447 return -1;
2448 }
2449
2450 if (wpa_config_set_cred(cred, name, value, 0) < 0) {
2451 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
2452 "variable '%s'", name);
2453 return -1;
2454 }
2455
2456 return 0;
2457}
2458
2459
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002460#ifndef CONFIG_NO_CONFIG_WRITE
2461static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
2462{
2463 int ret;
2464
2465 if (!wpa_s->conf->update_config) {
2466 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
2467 "to update configuration (update_config=0)");
2468 return -1;
2469 }
2470
2471 ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
2472 if (ret) {
2473 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
2474 "update configuration");
2475 } else {
2476 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
2477 " updated");
2478 }
2479
2480 return ret;
2481}
2482#endif /* CONFIG_NO_CONFIG_WRITE */
2483
2484
2485static int ctrl_iface_get_capability_pairwise(int res, char *strict,
2486 struct wpa_driver_capa *capa,
2487 char *buf, size_t buflen)
2488{
2489 int ret, first = 1;
2490 char *pos, *end;
2491 size_t len;
2492
2493 pos = buf;
2494 end = pos + buflen;
2495
2496 if (res < 0) {
2497 if (strict)
2498 return 0;
2499 len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
2500 if (len >= buflen)
2501 return -1;
2502 return len;
2503 }
2504
2505 if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
2506 ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
2507 if (ret < 0 || ret >= end - pos)
2508 return pos - buf;
2509 pos += ret;
2510 first = 0;
2511 }
2512
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002513 if (capa->enc & WPA_DRIVER_CAPA_ENC_GCMP) {
2514 ret = os_snprintf(pos, end - pos, "%sGCMP", first ? "" : " ");
2515 if (ret < 0 || ret >= end - pos)
2516 return pos - buf;
2517 pos += ret;
2518 first = 0;
2519 }
2520
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002521 if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
2522 ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
2523 if (ret < 0 || ret >= end - pos)
2524 return pos - buf;
2525 pos += ret;
2526 first = 0;
2527 }
2528
2529 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
2530 ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : " ");
2531 if (ret < 0 || ret >= end - pos)
2532 return pos - buf;
2533 pos += ret;
2534 first = 0;
2535 }
2536
2537 return pos - buf;
2538}
2539
2540
2541static int ctrl_iface_get_capability_group(int res, char *strict,
2542 struct wpa_driver_capa *capa,
2543 char *buf, size_t buflen)
2544{
2545 int ret, first = 1;
2546 char *pos, *end;
2547 size_t len;
2548
2549 pos = buf;
2550 end = pos + buflen;
2551
2552 if (res < 0) {
2553 if (strict)
2554 return 0;
2555 len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
2556 if (len >= buflen)
2557 return -1;
2558 return len;
2559 }
2560
2561 if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
2562 ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
2563 if (ret < 0 || ret >= end - pos)
2564 return pos - buf;
2565 pos += ret;
2566 first = 0;
2567 }
2568
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002569 if (capa->enc & WPA_DRIVER_CAPA_ENC_GCMP) {
2570 ret = os_snprintf(pos, end - pos, "%sGCMP", first ? "" : " ");
2571 if (ret < 0 || ret >= end - pos)
2572 return pos - buf;
2573 pos += ret;
2574 first = 0;
2575 }
2576
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002577 if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
2578 ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
2579 if (ret < 0 || ret >= end - pos)
2580 return pos - buf;
2581 pos += ret;
2582 first = 0;
2583 }
2584
2585 if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP104) {
2586 ret = os_snprintf(pos, end - pos, "%sWEP104",
2587 first ? "" : " ");
2588 if (ret < 0 || ret >= end - pos)
2589 return pos - buf;
2590 pos += ret;
2591 first = 0;
2592 }
2593
2594 if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP40) {
2595 ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : " ");
2596 if (ret < 0 || ret >= end - pos)
2597 return pos - buf;
2598 pos += ret;
2599 first = 0;
2600 }
2601
2602 return pos - buf;
2603}
2604
2605
2606static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
2607 struct wpa_driver_capa *capa,
2608 char *buf, size_t buflen)
2609{
2610 int ret;
2611 char *pos, *end;
2612 size_t len;
2613
2614 pos = buf;
2615 end = pos + buflen;
2616
2617 if (res < 0) {
2618 if (strict)
2619 return 0;
2620 len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
2621 "NONE", buflen);
2622 if (len >= buflen)
2623 return -1;
2624 return len;
2625 }
2626
2627 ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
2628 if (ret < 0 || ret >= end - pos)
2629 return pos - buf;
2630 pos += ret;
2631
2632 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2633 WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
2634 ret = os_snprintf(pos, end - pos, " WPA-EAP");
2635 if (ret < 0 || ret >= end - pos)
2636 return pos - buf;
2637 pos += ret;
2638 }
2639
2640 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
2641 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
2642 ret = os_snprintf(pos, end - pos, " WPA-PSK");
2643 if (ret < 0 || ret >= end - pos)
2644 return pos - buf;
2645 pos += ret;
2646 }
2647
2648 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
2649 ret = os_snprintf(pos, end - pos, " WPA-NONE");
2650 if (ret < 0 || ret >= end - pos)
2651 return pos - buf;
2652 pos += ret;
2653 }
2654
2655 return pos - buf;
2656}
2657
2658
2659static int ctrl_iface_get_capability_proto(int res, char *strict,
2660 struct wpa_driver_capa *capa,
2661 char *buf, size_t buflen)
2662{
2663 int ret, first = 1;
2664 char *pos, *end;
2665 size_t len;
2666
2667 pos = buf;
2668 end = pos + buflen;
2669
2670 if (res < 0) {
2671 if (strict)
2672 return 0;
2673 len = os_strlcpy(buf, "RSN WPA", buflen);
2674 if (len >= buflen)
2675 return -1;
2676 return len;
2677 }
2678
2679 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
2680 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
2681 ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
2682 if (ret < 0 || ret >= end - pos)
2683 return pos - buf;
2684 pos += ret;
2685 first = 0;
2686 }
2687
2688 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2689 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
2690 ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
2691 if (ret < 0 || ret >= end - pos)
2692 return pos - buf;
2693 pos += ret;
2694 first = 0;
2695 }
2696
2697 return pos - buf;
2698}
2699
2700
2701static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
2702 struct wpa_driver_capa *capa,
2703 char *buf, size_t buflen)
2704{
2705 int ret, first = 1;
2706 char *pos, *end;
2707 size_t len;
2708
2709 pos = buf;
2710 end = pos + buflen;
2711
2712 if (res < 0) {
2713 if (strict)
2714 return 0;
2715 len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
2716 if (len >= buflen)
2717 return -1;
2718 return len;
2719 }
2720
2721 if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
2722 ret = os_snprintf(pos, end - pos, "%sOPEN", first ? "" : " ");
2723 if (ret < 0 || ret >= end - pos)
2724 return pos - buf;
2725 pos += ret;
2726 first = 0;
2727 }
2728
2729 if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
2730 ret = os_snprintf(pos, end - pos, "%sSHARED",
2731 first ? "" : " ");
2732 if (ret < 0 || ret >= end - pos)
2733 return pos - buf;
2734 pos += ret;
2735 first = 0;
2736 }
2737
2738 if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
2739 ret = os_snprintf(pos, end - pos, "%sLEAP", first ? "" : " ");
2740 if (ret < 0 || ret >= end - pos)
2741 return pos - buf;
2742 pos += ret;
2743 first = 0;
2744 }
2745
2746 return pos - buf;
2747}
2748
2749
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07002750static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
2751 char *buf, size_t buflen)
2752{
2753 struct hostapd_channel_data *chnl;
2754 int ret, i, j;
2755 char *pos, *end, *hmode;
2756
2757 pos = buf;
2758 end = pos + buflen;
2759
2760 for (j = 0; j < wpa_s->hw.num_modes; j++) {
2761 switch (wpa_s->hw.modes[j].mode) {
2762 case HOSTAPD_MODE_IEEE80211B:
2763 hmode = "B";
2764 break;
2765 case HOSTAPD_MODE_IEEE80211G:
2766 hmode = "G";
2767 break;
2768 case HOSTAPD_MODE_IEEE80211A:
2769 hmode = "A";
2770 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002771 case HOSTAPD_MODE_IEEE80211AD:
2772 hmode = "AD";
2773 break;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07002774 default:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002775 continue;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07002776 }
2777 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
2778 if (ret < 0 || ret >= end - pos)
2779 return pos - buf;
2780 pos += ret;
2781 chnl = wpa_s->hw.modes[j].channels;
2782 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002783 if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
2784 continue;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07002785 ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
2786 if (ret < 0 || ret >= end - pos)
2787 return pos - buf;
2788 pos += ret;
2789 }
2790 ret = os_snprintf(pos, end - pos, "\n");
2791 if (ret < 0 || ret >= end - pos)
2792 return pos - buf;
2793 pos += ret;
2794 }
2795
2796 return pos - buf;
2797}
2798
2799
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002800static int wpa_supplicant_ctrl_iface_get_capability(
2801 struct wpa_supplicant *wpa_s, const char *_field, char *buf,
2802 size_t buflen)
2803{
2804 struct wpa_driver_capa capa;
2805 int res;
2806 char *strict;
2807 char field[30];
2808 size_t len;
2809
2810 /* Determine whether or not strict checking was requested */
2811 len = os_strlcpy(field, _field, sizeof(field));
2812 if (len >= sizeof(field))
2813 return -1;
2814 strict = os_strchr(field, ' ');
2815 if (strict != NULL) {
2816 *strict++ = '\0';
2817 if (os_strcmp(strict, "strict") != 0)
2818 return -1;
2819 }
2820
2821 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
2822 field, strict ? strict : "");
2823
2824 if (os_strcmp(field, "eap") == 0) {
2825 return eap_get_names(buf, buflen);
2826 }
2827
2828 res = wpa_drv_get_capa(wpa_s, &capa);
2829
2830 if (os_strcmp(field, "pairwise") == 0)
2831 return ctrl_iface_get_capability_pairwise(res, strict, &capa,
2832 buf, buflen);
2833
2834 if (os_strcmp(field, "group") == 0)
2835 return ctrl_iface_get_capability_group(res, strict, &capa,
2836 buf, buflen);
2837
2838 if (os_strcmp(field, "key_mgmt") == 0)
2839 return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
2840 buf, buflen);
2841
2842 if (os_strcmp(field, "proto") == 0)
2843 return ctrl_iface_get_capability_proto(res, strict, &capa,
2844 buf, buflen);
2845
2846 if (os_strcmp(field, "auth_alg") == 0)
2847 return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
2848 buf, buflen);
2849
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07002850 if (os_strcmp(field, "channels") == 0)
2851 return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
2852
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002853 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
2854 field);
2855
2856 return -1;
2857}
2858
2859
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002860#ifdef CONFIG_INTERWORKING
2861static char * anqp_add_hex(char *pos, char *end, const char *title,
2862 struct wpabuf *data)
2863{
2864 char *start = pos;
2865 size_t i;
2866 int ret;
2867 const u8 *d;
2868
2869 if (data == NULL)
2870 return start;
2871
2872 ret = os_snprintf(pos, end - pos, "%s=", title);
2873 if (ret < 0 || ret >= end - pos)
2874 return start;
2875 pos += ret;
2876
2877 d = wpabuf_head_u8(data);
2878 for (i = 0; i < wpabuf_len(data); i++) {
2879 ret = os_snprintf(pos, end - pos, "%02x", *d++);
2880 if (ret < 0 || ret >= end - pos)
2881 return start;
2882 pos += ret;
2883 }
2884
2885 ret = os_snprintf(pos, end - pos, "\n");
2886 if (ret < 0 || ret >= end - pos)
2887 return start;
2888 pos += ret;
2889
2890 return pos;
2891}
2892#endif /* CONFIG_INTERWORKING */
2893
2894
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07002895static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
2896 unsigned long mask, char *buf, size_t buflen)
2897{
2898 size_t i;
2899 int ret;
2900 char *pos, *end;
2901 const u8 *ie, *ie2;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07002902
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07002903 pos = buf;
2904 end = buf + buflen;
2905
2906 if (mask & WPA_BSS_MASK_ID) {
2907 ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
2908 if (ret < 0 || ret >= end - pos)
2909 return 0;
2910 pos += ret;
2911 }
2912
2913 if (mask & WPA_BSS_MASK_BSSID) {
2914 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
2915 MAC2STR(bss->bssid));
2916 if (ret < 0 || ret >= end - pos)
2917 return 0;
2918 pos += ret;
2919 }
2920
2921 if (mask & WPA_BSS_MASK_FREQ) {
2922 ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
2923 if (ret < 0 || ret >= end - pos)
2924 return 0;
2925 pos += ret;
2926 }
2927
2928 if (mask & WPA_BSS_MASK_BEACON_INT) {
2929 ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
2930 bss->beacon_int);
2931 if (ret < 0 || ret >= end - pos)
2932 return 0;
2933 pos += ret;
2934 }
2935
2936 if (mask & WPA_BSS_MASK_CAPABILITIES) {
2937 ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
2938 bss->caps);
2939 if (ret < 0 || ret >= end - pos)
2940 return 0;
2941 pos += ret;
2942 }
2943
2944 if (mask & WPA_BSS_MASK_QUAL) {
2945 ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
2946 if (ret < 0 || ret >= end - pos)
2947 return 0;
2948 pos += ret;
2949 }
2950
2951 if (mask & WPA_BSS_MASK_NOISE) {
2952 ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
2953 if (ret < 0 || ret >= end - pos)
2954 return 0;
2955 pos += ret;
2956 }
2957
2958 if (mask & WPA_BSS_MASK_LEVEL) {
2959 ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
2960 if (ret < 0 || ret >= end - pos)
2961 return 0;
2962 pos += ret;
2963 }
2964
2965 if (mask & WPA_BSS_MASK_TSF) {
2966 ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
2967 (unsigned long long) bss->tsf);
2968 if (ret < 0 || ret >= end - pos)
2969 return 0;
2970 pos += ret;
2971 }
2972
2973 if (mask & WPA_BSS_MASK_AGE) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002974 struct os_time now;
2975
2976 os_get_time(&now);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07002977 ret = os_snprintf(pos, end - pos, "age=%d\n",
2978 (int) (now.sec - bss->last_update.sec));
2979 if (ret < 0 || ret >= end - pos)
2980 return 0;
2981 pos += ret;
2982 }
2983
2984 if (mask & WPA_BSS_MASK_IE) {
2985 ret = os_snprintf(pos, end - pos, "ie=");
2986 if (ret < 0 || ret >= end - pos)
2987 return 0;
2988 pos += ret;
2989
2990 ie = (const u8 *) (bss + 1);
2991 for (i = 0; i < bss->ie_len; i++) {
2992 ret = os_snprintf(pos, end - pos, "%02x", *ie++);
2993 if (ret < 0 || ret >= end - pos)
2994 return 0;
2995 pos += ret;
2996 }
2997
2998 ret = os_snprintf(pos, end - pos, "\n");
2999 if (ret < 0 || ret >= end - pos)
3000 return 0;
3001 pos += ret;
3002 }
3003
3004 if (mask & WPA_BSS_MASK_FLAGS) {
3005 ret = os_snprintf(pos, end - pos, "flags=");
3006 if (ret < 0 || ret >= end - pos)
3007 return 0;
3008 pos += ret;
3009
3010 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
3011 if (ie)
3012 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
3013 2 + ie[1]);
3014 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
3015 if (ie2)
3016 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
3017 2 + ie2[1]);
3018 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
3019 if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
3020 ret = os_snprintf(pos, end - pos, "[WEP]");
3021 if (ret < 0 || ret >= end - pos)
3022 return 0;
3023 pos += ret;
3024 }
3025 if (bss->caps & IEEE80211_CAP_IBSS) {
3026 ret = os_snprintf(pos, end - pos, "[IBSS]");
3027 if (ret < 0 || ret >= end - pos)
3028 return 0;
3029 pos += ret;
3030 }
3031 if (bss->caps & IEEE80211_CAP_ESS) {
3032 ret = os_snprintf(pos, end - pos, "[ESS]");
3033 if (ret < 0 || ret >= end - pos)
3034 return 0;
3035 pos += ret;
3036 }
3037 if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE)) {
3038 ret = os_snprintf(pos, end - pos, "[P2P]");
3039 if (ret < 0 || ret >= end - pos)
3040 return 0;
3041 pos += ret;
3042 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003043#ifdef CONFIG_HS20
3044 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
3045 ret = os_snprintf(pos, end - pos, "[HS20]");
3046 if (ret < 0 || ret >= end - pos)
3047 return -1;
3048 pos += ret;
3049 }
3050#endif /* CONFIG_HS20 */
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003051
3052 ret = os_snprintf(pos, end - pos, "\n");
3053 if (ret < 0 || ret >= end - pos)
3054 return 0;
3055 pos += ret;
3056 }
3057
3058 if (mask & WPA_BSS_MASK_SSID) {
3059 ret = os_snprintf(pos, end - pos, "ssid=%s\n",
3060 wpa_ssid_txt(bss->ssid, bss->ssid_len));
3061 if (ret < 0 || ret >= end - pos)
3062 return 0;
3063 pos += ret;
3064 }
3065
3066#ifdef CONFIG_WPS
3067 if (mask & WPA_BSS_MASK_WPS_SCAN) {
3068 ie = (const u8 *) (bss + 1);
3069 ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
3070 if (ret < 0 || ret >= end - pos)
3071 return 0;
3072 pos += ret;
3073 }
3074#endif /* CONFIG_WPS */
3075
3076#ifdef CONFIG_P2P
3077 if (mask & WPA_BSS_MASK_P2P_SCAN) {
3078 ie = (const u8 *) (bss + 1);
3079 ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
3080 if (ret < 0 || ret >= end - pos)
3081 return 0;
3082 pos += ret;
3083 }
3084#endif /* CONFIG_P2P */
3085
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003086#ifdef CONFIG_WIFI_DISPLAY
3087 if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
3088 struct wpabuf *wfd;
3089 ie = (const u8 *) (bss + 1);
3090 wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
3091 WFD_IE_VENDOR_TYPE);
3092 if (wfd) {
3093 ret = os_snprintf(pos, end - pos, "wfd_subelems=");
3094 if (ret < 0 || ret >= end - pos)
3095 return pos - buf;
3096 pos += ret;
3097
3098 pos += wpa_snprintf_hex(pos, end - pos,
3099 wpabuf_head(wfd),
3100 wpabuf_len(wfd));
3101 wpabuf_free(wfd);
3102
3103 ret = os_snprintf(pos, end - pos, "\n");
3104 if (ret < 0 || ret >= end - pos)
3105 return pos - buf;
3106 pos += ret;
3107 }
3108 }
3109#endif /* CONFIG_WIFI_DISPLAY */
3110
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003111#ifdef CONFIG_INTERWORKING
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003112 if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
3113 struct wpa_bss_anqp *anqp = bss->anqp;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003114 pos = anqp_add_hex(pos, end, "anqp_venue_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003115 anqp->venue_name);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003116 pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003117 anqp->network_auth_type);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003118 pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003119 anqp->roaming_consortium);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003120 pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003121 anqp->ip_addr_type_availability);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003122 pos = anqp_add_hex(pos, end, "anqp_nai_realm",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003123 anqp->nai_realm);
3124 pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003125 pos = anqp_add_hex(pos, end, "anqp_domain_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003126 anqp->domain_name);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003127#ifdef CONFIG_HS20
3128 pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003129 anqp->hs20_operator_friendly_name);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003130 pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003131 anqp->hs20_wan_metrics);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003132 pos = anqp_add_hex(pos, end, "hs20_connection_capability",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003133 anqp->hs20_connection_capability);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003134#endif /* CONFIG_HS20 */
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003135 }
3136#endif /* CONFIG_INTERWORKING */
3137
Irfan Sheriffe2ea0082012-08-13 10:56:16 -07003138#ifdef ANDROID
3139 ret = os_snprintf(pos, end - pos, "====\n");
3140 if (ret < 0 || ret >= end - pos)
3141 return 0;
3142 pos += ret;
3143#endif
3144
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003145 return pos - buf;
3146}
3147
Dmitry Shmidt04949592012-07-19 12:16:46 -07003148
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003149static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
3150 const char *cmd, char *buf,
3151 size_t buflen)
3152{
3153 u8 bssid[ETH_ALEN];
3154 size_t i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003155 struct wpa_bss *bss;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003156 struct wpa_bss *bsslast = NULL;
3157 struct dl_list *next;
3158 int ret = 0;
3159 int len;
3160 char *ctmp;
3161 unsigned long mask = WPA_BSS_MASK_ALL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003162
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003163 if (os_strncmp(cmd, "RANGE=", 6) == 0) {
3164 if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
3165 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003166 list_id);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003167 bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
3168 list_id);
3169 } else { /* N1-N2 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003170 unsigned int id1, id2;
3171
3172 if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
3173 wpa_printf(MSG_INFO, "Wrong BSS range "
3174 "format");
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003175 return 0;
3176 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003177
3178 id1 = atoi(cmd + 6);
3179 bss = wpa_bss_get_id(wpa_s, id1);
3180 id2 = atoi(ctmp + 1);
3181 if (id2 == 0)
3182 bsslast = dl_list_last(&wpa_s->bss_id,
3183 struct wpa_bss,
3184 list_id);
3185 else {
3186 bsslast = wpa_bss_get_id(wpa_s, id2);
3187 if (bsslast == NULL && bss && id2 > id1) {
3188 struct wpa_bss *tmp = bss;
3189 for (;;) {
3190 next = tmp->list_id.next;
3191 if (next == &wpa_s->bss_id)
3192 break;
3193 tmp = dl_list_entry(
3194 next, struct wpa_bss,
3195 list_id);
3196 if (tmp->id > id2)
3197 break;
3198 bsslast = tmp;
3199 }
3200 }
3201 }
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003202 }
3203 } else if (os_strcmp(cmd, "FIRST") == 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -07003204 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003205 else if (os_strncmp(cmd, "ID-", 3) == 0) {
3206 i = atoi(cmd + 3);
3207 bss = wpa_bss_get_id(wpa_s, i);
3208 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
3209 i = atoi(cmd + 5);
3210 bss = wpa_bss_get_id(wpa_s, i);
3211 if (bss) {
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003212 next = bss->list_id.next;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003213 if (next == &wpa_s->bss_id)
3214 bss = NULL;
3215 else
3216 bss = dl_list_entry(next, struct wpa_bss,
3217 list_id);
3218 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003219#ifdef CONFIG_P2P
3220 } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
3221 if (hwaddr_aton(cmd + 13, bssid) == 0)
3222 bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
3223 else
3224 bss = NULL;
3225#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003226 } else if (hwaddr_aton(cmd, bssid) == 0)
3227 bss = wpa_bss_get_bssid(wpa_s, bssid);
3228 else {
3229 struct wpa_bss *tmp;
3230 i = atoi(cmd);
3231 bss = NULL;
3232 dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
3233 {
3234 if (i-- == 0) {
3235 bss = tmp;
3236 break;
3237 }
3238 }
3239 }
3240
Dmitry Shmidt04949592012-07-19 12:16:46 -07003241 if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
3242 mask = strtoul(ctmp + 5, NULL, 0x10);
3243 if (mask == 0)
3244 mask = WPA_BSS_MASK_ALL;
3245 }
3246
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003247 if (bss == NULL)
3248 return 0;
3249
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003250 if (bsslast == NULL)
3251 bsslast = bss;
3252 do {
3253 len = print_bss_info(wpa_s, bss, mask, buf, buflen);
3254 ret += len;
3255 buf += len;
3256 buflen -= len;
3257 if (bss == bsslast)
3258 break;
3259 next = bss->list_id.next;
3260 if (next == &wpa_s->bss_id)
3261 break;
3262 bss = dl_list_entry(next, struct wpa_bss, list_id);
3263 } while (bss && len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003264
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003265 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003266}
3267
3268
3269static int wpa_supplicant_ctrl_iface_ap_scan(
3270 struct wpa_supplicant *wpa_s, char *cmd)
3271{
3272 int ap_scan = atoi(cmd);
3273 return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
3274}
3275
3276
3277static int wpa_supplicant_ctrl_iface_scan_interval(
3278 struct wpa_supplicant *wpa_s, char *cmd)
3279{
3280 int scan_int = atoi(cmd);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003281 return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003282}
3283
3284
3285static int wpa_supplicant_ctrl_iface_bss_expire_age(
3286 struct wpa_supplicant *wpa_s, char *cmd)
3287{
3288 int expire_age = atoi(cmd);
3289 return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
3290}
3291
3292
3293static int wpa_supplicant_ctrl_iface_bss_expire_count(
3294 struct wpa_supplicant *wpa_s, char *cmd)
3295{
3296 int expire_count = atoi(cmd);
3297 return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
3298}
3299
3300
Dmitry Shmidtf48e4f92012-08-24 11:14:44 -07003301static int wpa_supplicant_ctrl_iface_bss_flush(
3302 struct wpa_supplicant *wpa_s, char *cmd)
3303{
3304 int flush_age = atoi(cmd);
3305
3306 if (flush_age == 0)
3307 wpa_bss_flush(wpa_s);
3308 else
3309 wpa_bss_flush_by_age(wpa_s, flush_age);
3310 return 0;
3311}
3312
3313
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003314static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
3315{
3316 wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
3317 /* MLME-DELETEKEYS.request */
3318 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
3319 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
3320 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
3321 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
3322#ifdef CONFIG_IEEE80211W
3323 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
3324 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
3325#endif /* CONFIG_IEEE80211W */
3326
3327 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
3328 0);
3329 /* MLME-SETPROTECTION.request(None) */
3330 wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
3331 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
3332 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
3333 wpa_sm_drop_sa(wpa_s->wpa);
3334}
3335
3336
3337static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
3338 char *addr)
3339{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003340#ifdef CONFIG_NO_SCAN_PROCESSING
3341 return -1;
3342#else /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003343 u8 bssid[ETH_ALEN];
3344 struct wpa_bss *bss;
3345 struct wpa_ssid *ssid = wpa_s->current_ssid;
3346
3347 if (hwaddr_aton(addr, bssid)) {
3348 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
3349 "address '%s'", addr);
3350 return -1;
3351 }
3352
3353 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
3354
3355 bss = wpa_bss_get_bssid(wpa_s, bssid);
3356 if (!bss) {
3357 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
3358 "from BSS table");
3359 return -1;
3360 }
3361
3362 /*
3363 * TODO: Find best network configuration block from configuration to
3364 * allow roaming to other networks
3365 */
3366
3367 if (!ssid) {
3368 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
3369 "configuration known for the target AP");
3370 return -1;
3371 }
3372
3373 wpa_s->reassociate = 1;
3374 wpa_supplicant_connect(wpa_s, bss, ssid);
3375
3376 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003377#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003378}
3379
3380
3381#ifdef CONFIG_P2P
3382static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
3383{
3384 unsigned int timeout = atoi(cmd);
3385 enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003386 u8 dev_id[ETH_ALEN], *_dev_id = NULL;
3387 char *pos;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003388 unsigned int search_delay;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003389
3390 if (os_strstr(cmd, "type=social"))
3391 type = P2P_FIND_ONLY_SOCIAL;
3392 else if (os_strstr(cmd, "type=progressive"))
3393 type = P2P_FIND_PROGRESSIVE;
3394
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003395 pos = os_strstr(cmd, "dev_id=");
3396 if (pos) {
3397 pos += 7;
3398 if (hwaddr_aton(pos, dev_id))
3399 return -1;
3400 _dev_id = dev_id;
3401 }
3402
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003403 pos = os_strstr(cmd, "delay=");
3404 if (pos) {
3405 pos += 6;
3406 search_delay = atoi(pos);
3407 } else
3408 search_delay = wpas_p2p_search_delay(wpa_s);
3409
3410 return wpas_p2p_find(wpa_s, timeout, type, 0, NULL, _dev_id,
3411 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003412}
3413
3414
3415static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
3416 char *buf, size_t buflen)
3417{
3418 u8 addr[ETH_ALEN];
3419 char *pos, *pos2;
3420 char *pin = NULL;
3421 enum p2p_wps_method wps_method;
3422 int new_pin;
3423 int ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003424 int persistent_group, persistent_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003425 int join;
3426 int auth;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003427 int automatic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003428 int go_intent = -1;
3429 int freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003430 int pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003431 int ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003432
Dmitry Shmidt04949592012-07-19 12:16:46 -07003433 /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad]
3434 * [persistent|persistent=<network id>]
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003435 * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
3436 * [ht40] */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003437
3438 if (hwaddr_aton(cmd, addr))
3439 return -1;
3440
3441 pos = cmd + 17;
3442 if (*pos != ' ')
3443 return -1;
3444 pos++;
3445
3446 persistent_group = os_strstr(pos, " persistent") != NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003447 pos2 = os_strstr(pos, " persistent=");
3448 if (pos2) {
3449 struct wpa_ssid *ssid;
3450 persistent_id = atoi(pos2 + 12);
3451 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3452 if (ssid == NULL || ssid->disabled != 2 ||
3453 ssid->mode != WPAS_MODE_P2P_GO) {
3454 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3455 "SSID id=%d for persistent P2P group (GO)",
3456 persistent_id);
3457 return -1;
3458 }
3459 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003460 join = os_strstr(pos, " join") != NULL;
3461 auth = os_strstr(pos, " auth") != NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003462 automatic = os_strstr(pos, " auto") != NULL;
3463 pd = os_strstr(pos, " provdisc") != NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003464 ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003465
3466 pos2 = os_strstr(pos, " go_intent=");
3467 if (pos2) {
3468 pos2 += 11;
3469 go_intent = atoi(pos2);
3470 if (go_intent < 0 || go_intent > 15)
3471 return -1;
3472 }
3473
3474 pos2 = os_strstr(pos, " freq=");
3475 if (pos2) {
3476 pos2 += 6;
3477 freq = atoi(pos2);
3478 if (freq <= 0)
3479 return -1;
3480 }
3481
3482 if (os_strncmp(pos, "pin", 3) == 0) {
3483 /* Request random PIN (to be displayed) and enable the PIN */
3484 wps_method = WPS_PIN_DISPLAY;
3485 } else if (os_strncmp(pos, "pbc", 3) == 0) {
3486 wps_method = WPS_PBC;
3487 } else {
3488 pin = pos;
3489 pos = os_strchr(pin, ' ');
3490 wps_method = WPS_PIN_KEYPAD;
3491 if (pos) {
3492 *pos++ = '\0';
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003493 if (os_strncmp(pos, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003494 wps_method = WPS_PIN_DISPLAY;
3495 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003496 if (!wps_pin_str_valid(pin)) {
3497 os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
3498 return 17;
3499 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003500 }
3501
3502 new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003503 persistent_group, automatic, join,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003504 auth, go_intent, freq, persistent_id, pd,
3505 ht40);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003506 if (new_pin == -2) {
3507 os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
3508 return 25;
3509 }
3510 if (new_pin == -3) {
3511 os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
3512 return 25;
3513 }
3514 if (new_pin < 0)
3515 return -1;
3516 if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
3517 ret = os_snprintf(buf, buflen, "%08d", new_pin);
3518 if (ret < 0 || (size_t) ret >= buflen)
3519 return -1;
3520 return ret;
3521 }
3522
3523 os_memcpy(buf, "OK\n", 3);
3524 return 3;
3525}
3526
3527
3528static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
3529{
3530 unsigned int timeout = atoi(cmd);
3531 return wpas_p2p_listen(wpa_s, timeout);
3532}
3533
3534
3535static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
3536{
3537 u8 addr[ETH_ALEN];
3538 char *pos;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003539 enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003540
Dmitry Shmidt04949592012-07-19 12:16:46 -07003541 /* <addr> <config method> [join|auto] */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003542
3543 if (hwaddr_aton(cmd, addr))
3544 return -1;
3545
3546 pos = cmd + 17;
3547 if (*pos != ' ')
3548 return -1;
3549 pos++;
3550
Dmitry Shmidt04949592012-07-19 12:16:46 -07003551 if (os_strstr(pos, " join") != NULL)
3552 use = WPAS_P2P_PD_FOR_JOIN;
3553 else if (os_strstr(pos, " auto") != NULL)
3554 use = WPAS_P2P_PD_AUTO;
3555
3556 return wpas_p2p_prov_disc(wpa_s, addr, pos, use);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003557}
3558
3559
3560static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
3561 size_t buflen)
3562{
3563 struct wpa_ssid *ssid = wpa_s->current_ssid;
3564
3565 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3566 ssid->passphrase == NULL)
3567 return -1;
3568
3569 os_strlcpy(buf, ssid->passphrase, buflen);
3570 return os_strlen(buf);
3571}
3572
3573
3574static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
3575 char *buf, size_t buflen)
3576{
3577 u64 ref;
3578 int res;
3579 u8 dst_buf[ETH_ALEN], *dst;
3580 struct wpabuf *tlvs;
3581 char *pos;
3582 size_t len;
3583
3584 if (hwaddr_aton(cmd, dst_buf))
3585 return -1;
3586 dst = dst_buf;
3587 if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
3588 dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
3589 dst = NULL;
3590 pos = cmd + 17;
3591 if (*pos != ' ')
3592 return -1;
3593 pos++;
3594
3595 if (os_strncmp(pos, "upnp ", 5) == 0) {
3596 u8 version;
3597 pos += 5;
3598 if (hexstr2bin(pos, &version, 1) < 0)
3599 return -1;
3600 pos += 2;
3601 if (*pos != ' ')
3602 return -1;
3603 pos++;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003604 ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003605#ifdef CONFIG_WIFI_DISPLAY
3606 } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
3607 ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
3608#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003609 } else {
3610 len = os_strlen(pos);
3611 if (len & 1)
3612 return -1;
3613 len /= 2;
3614 tlvs = wpabuf_alloc(len);
3615 if (tlvs == NULL)
3616 return -1;
3617 if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
3618 wpabuf_free(tlvs);
3619 return -1;
3620 }
3621
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003622 ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003623 wpabuf_free(tlvs);
3624 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003625 if (ref == 0)
3626 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003627 res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
3628 if (res < 0 || (unsigned) res >= buflen)
3629 return -1;
3630 return res;
3631}
3632
3633
3634static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
3635 char *cmd)
3636{
3637 long long unsigned val;
3638 u64 req;
3639 if (sscanf(cmd, "%llx", &val) != 1)
3640 return -1;
3641 req = val;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003642 return wpas_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003643}
3644
3645
3646static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
3647{
3648 int freq;
3649 u8 dst[ETH_ALEN];
3650 u8 dialog_token;
3651 struct wpabuf *resp_tlvs;
3652 char *pos, *pos2;
3653 size_t len;
3654
3655 pos = os_strchr(cmd, ' ');
3656 if (pos == NULL)
3657 return -1;
3658 *pos++ = '\0';
3659 freq = atoi(cmd);
3660 if (freq == 0)
3661 return -1;
3662
3663 if (hwaddr_aton(pos, dst))
3664 return -1;
3665 pos += 17;
3666 if (*pos != ' ')
3667 return -1;
3668 pos++;
3669
3670 pos2 = os_strchr(pos, ' ');
3671 if (pos2 == NULL)
3672 return -1;
3673 *pos2++ = '\0';
3674 dialog_token = atoi(pos);
3675
3676 len = os_strlen(pos2);
3677 if (len & 1)
3678 return -1;
3679 len /= 2;
3680 resp_tlvs = wpabuf_alloc(len);
3681 if (resp_tlvs == NULL)
3682 return -1;
3683 if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
3684 wpabuf_free(resp_tlvs);
3685 return -1;
3686 }
3687
3688 wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
3689 wpabuf_free(resp_tlvs);
3690 return 0;
3691}
3692
3693
3694static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
3695 char *cmd)
3696{
Dmitry Shmidt04949592012-07-19 12:16:46 -07003697 if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
3698 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003699 wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
3700 return 0;
3701}
3702
3703
3704static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
3705 char *cmd)
3706{
3707 char *pos;
3708 size_t len;
3709 struct wpabuf *query, *resp;
3710
3711 pos = os_strchr(cmd, ' ');
3712 if (pos == NULL)
3713 return -1;
3714 *pos++ = '\0';
3715
3716 len = os_strlen(cmd);
3717 if (len & 1)
3718 return -1;
3719 len /= 2;
3720 query = wpabuf_alloc(len);
3721 if (query == NULL)
3722 return -1;
3723 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
3724 wpabuf_free(query);
3725 return -1;
3726 }
3727
3728 len = os_strlen(pos);
3729 if (len & 1) {
3730 wpabuf_free(query);
3731 return -1;
3732 }
3733 len /= 2;
3734 resp = wpabuf_alloc(len);
3735 if (resp == NULL) {
3736 wpabuf_free(query);
3737 return -1;
3738 }
3739 if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
3740 wpabuf_free(query);
3741 wpabuf_free(resp);
3742 return -1;
3743 }
3744
3745 if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
3746 wpabuf_free(query);
3747 wpabuf_free(resp);
3748 return -1;
3749 }
3750 return 0;
3751}
3752
3753
3754static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
3755{
3756 char *pos;
3757 u8 version;
3758
3759 pos = os_strchr(cmd, ' ');
3760 if (pos == NULL)
3761 return -1;
3762 *pos++ = '\0';
3763
3764 if (hexstr2bin(cmd, &version, 1) < 0)
3765 return -1;
3766
3767 return wpas_p2p_service_add_upnp(wpa_s, version, pos);
3768}
3769
3770
3771static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
3772{
3773 char *pos;
3774
3775 pos = os_strchr(cmd, ' ');
3776 if (pos == NULL)
3777 return -1;
3778 *pos++ = '\0';
3779
3780 if (os_strcmp(cmd, "bonjour") == 0)
3781 return p2p_ctrl_service_add_bonjour(wpa_s, pos);
3782 if (os_strcmp(cmd, "upnp") == 0)
3783 return p2p_ctrl_service_add_upnp(wpa_s, pos);
3784 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
3785 return -1;
3786}
3787
3788
3789static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
3790 char *cmd)
3791{
3792 size_t len;
3793 struct wpabuf *query;
3794 int ret;
3795
3796 len = os_strlen(cmd);
3797 if (len & 1)
3798 return -1;
3799 len /= 2;
3800 query = wpabuf_alloc(len);
3801 if (query == NULL)
3802 return -1;
3803 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
3804 wpabuf_free(query);
3805 return -1;
3806 }
3807
3808 ret = wpas_p2p_service_del_bonjour(wpa_s, query);
3809 wpabuf_free(query);
3810 return ret;
3811}
3812
3813
3814static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
3815{
3816 char *pos;
3817 u8 version;
3818
3819 pos = os_strchr(cmd, ' ');
3820 if (pos == NULL)
3821 return -1;
3822 *pos++ = '\0';
3823
3824 if (hexstr2bin(cmd, &version, 1) < 0)
3825 return -1;
3826
3827 return wpas_p2p_service_del_upnp(wpa_s, version, pos);
3828}
3829
3830
3831static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
3832{
3833 char *pos;
3834
3835 pos = os_strchr(cmd, ' ');
3836 if (pos == NULL)
3837 return -1;
3838 *pos++ = '\0';
3839
3840 if (os_strcmp(cmd, "bonjour") == 0)
3841 return p2p_ctrl_service_del_bonjour(wpa_s, pos);
3842 if (os_strcmp(cmd, "upnp") == 0)
3843 return p2p_ctrl_service_del_upnp(wpa_s, pos);
3844 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
3845 return -1;
3846}
3847
3848
3849static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
3850{
3851 u8 addr[ETH_ALEN];
3852
3853 /* <addr> */
3854
3855 if (hwaddr_aton(cmd, addr))
3856 return -1;
3857
3858 return wpas_p2p_reject(wpa_s, addr);
3859}
3860
3861
3862static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
3863{
3864 char *pos;
3865 int id;
3866 struct wpa_ssid *ssid;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07003867 u8 *_peer = NULL, peer[ETH_ALEN];
Jouni Malinen31be0a42012-08-31 21:20:51 +03003868 int freq = 0;
3869 int ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003870
3871 id = atoi(cmd);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07003872 pos = os_strstr(cmd, " peer=");
3873 if (pos) {
3874 pos += 6;
3875 if (hwaddr_aton(pos, peer))
3876 return -1;
3877 _peer = peer;
3878 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003879 ssid = wpa_config_get_network(wpa_s->conf, id);
3880 if (ssid == NULL || ssid->disabled != 2) {
3881 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
3882 "for persistent P2P group",
3883 id);
3884 return -1;
3885 }
3886
Jouni Malinen31be0a42012-08-31 21:20:51 +03003887 pos = os_strstr(cmd, " freq=");
3888 if (pos) {
3889 pos += 6;
3890 freq = atoi(pos);
3891 if (freq <= 0)
3892 return -1;
3893 }
3894
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003895 ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40;
Jouni Malinen31be0a42012-08-31 21:20:51 +03003896
Dmitry Shmidtaa532512012-09-24 10:35:31 -07003897 return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, ht40);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003898}
3899
3900
3901static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
3902{
3903 char *pos;
3904 u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
3905
3906 pos = os_strstr(cmd, " peer=");
3907 if (!pos)
3908 return -1;
3909
3910 *pos = '\0';
3911 pos += 6;
3912 if (hwaddr_aton(pos, peer)) {
3913 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
3914 return -1;
3915 }
3916
3917 pos = os_strstr(pos, " go_dev_addr=");
3918 if (pos) {
3919 pos += 13;
3920 if (hwaddr_aton(pos, go_dev_addr)) {
3921 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
3922 pos);
3923 return -1;
3924 }
3925 go_dev = go_dev_addr;
3926 }
3927
3928 return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
3929}
3930
3931
3932static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
3933{
3934 if (os_strncmp(cmd, "persistent=", 11) == 0)
3935 return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
3936 if (os_strncmp(cmd, "group=", 6) == 0)
3937 return p2p_ctrl_invite_group(wpa_s, cmd + 6);
3938
3939 return -1;
3940}
3941
3942
3943static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003944 char *cmd, int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003945{
3946 int id;
3947 struct wpa_ssid *ssid;
3948
3949 id = atoi(cmd);
3950 ssid = wpa_config_get_network(wpa_s->conf, id);
3951 if (ssid == NULL || ssid->disabled != 2) {
3952 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
3953 "for persistent P2P group",
3954 id);
3955 return -1;
3956 }
3957
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003958 return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq, ht40);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003959}
3960
3961
3962static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
3963{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003964 int freq = 0, ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003965 char *pos;
3966
3967 pos = os_strstr(cmd, "freq=");
3968 if (pos)
3969 freq = atoi(pos + 5);
3970
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003971 ht40 = (os_strstr(cmd, "ht40") != NULL) || wpa_s->conf->p2p_go_ht40;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003972
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003973 if (os_strncmp(cmd, "persistent=", 11) == 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003974 return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq,
3975 ht40);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003976 if (os_strcmp(cmd, "persistent") == 0 ||
3977 os_strncmp(cmd, "persistent ", 11) == 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003978 return wpas_p2p_group_add(wpa_s, 1, freq, ht40);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003979 if (os_strncmp(cmd, "freq=", 5) == 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003980 return wpas_p2p_group_add(wpa_s, 0, freq, ht40);
3981 if (ht40)
3982 return wpas_p2p_group_add(wpa_s, 0, freq, ht40);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003983
3984 wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
3985 cmd);
3986 return -1;
3987}
3988
3989
3990static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
3991 char *buf, size_t buflen)
3992{
3993 u8 addr[ETH_ALEN], *addr_ptr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003994 int next, res;
3995 const struct p2p_peer_info *info;
3996 char *pos, *end;
3997 char devtype[WPS_DEV_TYPE_BUFSIZE];
3998 struct wpa_ssid *ssid;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003999 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004000
4001 if (!wpa_s->global->p2p)
4002 return -1;
4003
4004 if (os_strcmp(cmd, "FIRST") == 0) {
4005 addr_ptr = NULL;
4006 next = 0;
4007 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
4008 if (hwaddr_aton(cmd + 5, addr) < 0)
4009 return -1;
4010 addr_ptr = addr;
4011 next = 1;
4012 } else {
4013 if (hwaddr_aton(cmd, addr) < 0)
4014 return -1;
4015 addr_ptr = addr;
4016 next = 0;
4017 }
4018
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004019 info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
4020 if (info == NULL)
4021 return -1;
4022
4023 pos = buf;
4024 end = buf + buflen;
4025
4026 res = os_snprintf(pos, end - pos, MACSTR "\n"
4027 "pri_dev_type=%s\n"
4028 "device_name=%s\n"
4029 "manufacturer=%s\n"
4030 "model_name=%s\n"
4031 "model_number=%s\n"
4032 "serial_number=%s\n"
4033 "config_methods=0x%x\n"
4034 "dev_capab=0x%x\n"
4035 "group_capab=0x%x\n"
4036 "level=%d\n",
4037 MAC2STR(info->p2p_device_addr),
4038 wps_dev_type_bin2str(info->pri_dev_type,
4039 devtype, sizeof(devtype)),
4040 info->device_name,
4041 info->manufacturer,
4042 info->model_name,
4043 info->model_number,
4044 info->serial_number,
4045 info->config_methods,
4046 info->dev_capab,
4047 info->group_capab,
4048 info->level);
4049 if (res < 0 || res >= end - pos)
4050 return pos - buf;
4051 pos += res;
4052
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004053 for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
4054 {
4055 const u8 *t;
4056 t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
4057 res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
4058 wps_dev_type_bin2str(t, devtype,
4059 sizeof(devtype)));
4060 if (res < 0 || res >= end - pos)
4061 return pos - buf;
4062 pos += res;
4063 }
4064
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004065 ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004066 if (ssid) {
4067 res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
4068 if (res < 0 || res >= end - pos)
4069 return pos - buf;
4070 pos += res;
4071 }
4072
4073 res = p2p_get_peer_info_txt(info, pos, end - pos);
4074 if (res < 0)
4075 return pos - buf;
4076 pos += res;
4077
4078 return pos - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004079}
4080
4081
Dmitry Shmidt04949592012-07-19 12:16:46 -07004082static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
4083 const char *param)
4084{
4085 struct wpa_freq_range *freq = NULL, *n;
4086 unsigned int count = 0, i;
4087 const char *pos, *pos2, *pos3;
4088
4089 if (wpa_s->global->p2p == NULL)
4090 return -1;
4091
4092 /*
4093 * param includes comma separated frequency range.
4094 * For example: 2412-2432,2462,5000-6000
4095 */
4096 pos = param;
4097 while (pos && pos[0]) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004098 n = os_realloc_array(freq, count + 1,
4099 sizeof(struct wpa_freq_range));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004100 if (n == NULL) {
4101 os_free(freq);
4102 return -1;
4103 }
4104 freq = n;
4105 freq[count].min = atoi(pos);
4106 pos2 = os_strchr(pos, '-');
4107 pos3 = os_strchr(pos, ',');
4108 if (pos2 && (!pos3 || pos2 < pos3)) {
4109 pos2++;
4110 freq[count].max = atoi(pos2);
4111 } else
4112 freq[count].max = freq[count].min;
4113 pos = pos3;
4114 if (pos)
4115 pos++;
4116 count++;
4117 }
4118
4119 for (i = 0; i < count; i++) {
4120 wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
4121 freq[i].min, freq[i].max);
4122 }
4123
4124 os_free(wpa_s->global->p2p_disallow_freq);
4125 wpa_s->global->p2p_disallow_freq = freq;
4126 wpa_s->global->num_p2p_disallow_freq = count;
4127 wpas_p2p_update_channel_list(wpa_s);
4128 return 0;
4129}
4130
4131
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004132static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
4133{
4134 char *param;
4135
4136 if (wpa_s->global->p2p == NULL)
4137 return -1;
4138
4139 param = os_strchr(cmd, ' ');
4140 if (param == NULL)
4141 return -1;
4142 *param++ = '\0';
4143
4144 if (os_strcmp(cmd, "discoverability") == 0) {
4145 p2p_set_client_discoverability(wpa_s->global->p2p,
4146 atoi(param));
4147 return 0;
4148 }
4149
4150 if (os_strcmp(cmd, "managed") == 0) {
4151 p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
4152 return 0;
4153 }
4154
4155 if (os_strcmp(cmd, "listen_channel") == 0) {
4156 return p2p_set_listen_channel(wpa_s->global->p2p, 81,
4157 atoi(param));
4158 }
4159
4160 if (os_strcmp(cmd, "ssid_postfix") == 0) {
4161 return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
4162 os_strlen(param));
4163 }
4164
4165 if (os_strcmp(cmd, "noa") == 0) {
4166 char *pos;
4167 int count, start, duration;
4168 /* GO NoA parameters: count,start_offset(ms),duration(ms) */
4169 count = atoi(param);
4170 pos = os_strchr(param, ',');
4171 if (pos == NULL)
4172 return -1;
4173 pos++;
4174 start = atoi(pos);
4175 pos = os_strchr(pos, ',');
4176 if (pos == NULL)
4177 return -1;
4178 pos++;
4179 duration = atoi(pos);
4180 if (count < 0 || count > 255 || start < 0 || duration < 0)
4181 return -1;
4182 if (count == 0 && duration > 0)
4183 return -1;
4184 wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
4185 "start=%d duration=%d", count, start, duration);
4186 return wpas_p2p_set_noa(wpa_s, count, start, duration);
4187 }
4188
4189 if (os_strcmp(cmd, "ps") == 0)
4190 return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
4191
4192 if (os_strcmp(cmd, "oppps") == 0)
4193 return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
4194
4195 if (os_strcmp(cmd, "ctwindow") == 0)
4196 return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
4197
4198 if (os_strcmp(cmd, "disabled") == 0) {
4199 wpa_s->global->p2p_disabled = atoi(param);
4200 wpa_printf(MSG_DEBUG, "P2P functionality %s",
4201 wpa_s->global->p2p_disabled ?
4202 "disabled" : "enabled");
4203 if (wpa_s->global->p2p_disabled) {
4204 wpas_p2p_stop_find(wpa_s);
4205 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
4206 p2p_flush(wpa_s->global->p2p);
4207 }
4208 return 0;
4209 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004210
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07004211 if (os_strcmp(cmd, "conc_pref") == 0) {
4212 if (os_strcmp(param, "sta") == 0)
4213 wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
4214 else if (os_strcmp(param, "p2p") == 0)
4215 wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004216 else {
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07004217 wpa_printf(MSG_INFO, "Invalid conc_pref value");
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004218 return -1;
4219 }
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07004220 wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
Dmitry Shmidt04949592012-07-19 12:16:46 -07004221 "%s", param);
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004222 return 0;
4223 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004224
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004225 if (os_strcmp(cmd, "force_long_sd") == 0) {
4226 wpa_s->force_long_sd = atoi(param);
4227 return 0;
4228 }
4229
4230 if (os_strcmp(cmd, "peer_filter") == 0) {
4231 u8 addr[ETH_ALEN];
4232 if (hwaddr_aton(param, addr))
4233 return -1;
4234 p2p_set_peer_filter(wpa_s->global->p2p, addr);
4235 return 0;
4236 }
4237
4238 if (os_strcmp(cmd, "cross_connect") == 0)
4239 return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
4240
4241 if (os_strcmp(cmd, "go_apsd") == 0) {
4242 if (os_strcmp(param, "disable") == 0)
4243 wpa_s->set_ap_uapsd = 0;
4244 else {
4245 wpa_s->set_ap_uapsd = 1;
4246 wpa_s->ap_uapsd = atoi(param);
4247 }
4248 return 0;
4249 }
4250
4251 if (os_strcmp(cmd, "client_apsd") == 0) {
4252 if (os_strcmp(param, "disable") == 0)
4253 wpa_s->set_sta_uapsd = 0;
4254 else {
4255 int be, bk, vi, vo;
4256 char *pos;
4257 /* format: BE,BK,VI,VO;max SP Length */
4258 be = atoi(param);
4259 pos = os_strchr(param, ',');
4260 if (pos == NULL)
4261 return -1;
4262 pos++;
4263 bk = atoi(pos);
4264 pos = os_strchr(pos, ',');
4265 if (pos == NULL)
4266 return -1;
4267 pos++;
4268 vi = atoi(pos);
4269 pos = os_strchr(pos, ',');
4270 if (pos == NULL)
4271 return -1;
4272 pos++;
4273 vo = atoi(pos);
4274 /* ignore max SP Length for now */
4275
4276 wpa_s->set_sta_uapsd = 1;
4277 wpa_s->sta_uapsd = 0;
4278 if (be)
4279 wpa_s->sta_uapsd |= BIT(0);
4280 if (bk)
4281 wpa_s->sta_uapsd |= BIT(1);
4282 if (vi)
4283 wpa_s->sta_uapsd |= BIT(2);
4284 if (vo)
4285 wpa_s->sta_uapsd |= BIT(3);
4286 }
4287 return 0;
4288 }
4289
Dmitry Shmidt04949592012-07-19 12:16:46 -07004290 if (os_strcmp(cmd, "disallow_freq") == 0)
4291 return p2p_ctrl_disallow_freq(wpa_s, param);
4292
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004293 if (os_strcmp(cmd, "disc_int") == 0) {
4294 int min_disc_int, max_disc_int, max_disc_tu;
4295 char *pos;
4296
4297 pos = param;
4298
4299 min_disc_int = atoi(pos);
4300 pos = os_strchr(pos, ' ');
4301 if (pos == NULL)
4302 return -1;
4303 *pos++ = '\0';
4304
4305 max_disc_int = atoi(pos);
4306 pos = os_strchr(pos, ' ');
4307 if (pos == NULL)
4308 return -1;
4309 *pos++ = '\0';
4310
4311 max_disc_tu = atoi(pos);
4312
4313 return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
4314 max_disc_int, max_disc_tu);
4315 }
4316
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004317 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
4318 cmd);
4319
4320 return -1;
4321}
4322
4323
4324static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
4325{
4326 char *pos, *pos2;
4327 unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
4328
4329 if (cmd[0]) {
4330 pos = os_strchr(cmd, ' ');
4331 if (pos == NULL)
4332 return -1;
4333 *pos++ = '\0';
4334 dur1 = atoi(cmd);
4335
4336 pos2 = os_strchr(pos, ' ');
4337 if (pos2)
4338 *pos2++ = '\0';
4339 int1 = atoi(pos);
4340 } else
4341 pos2 = NULL;
4342
4343 if (pos2) {
4344 pos = os_strchr(pos2, ' ');
4345 if (pos == NULL)
4346 return -1;
4347 *pos++ = '\0';
4348 dur2 = atoi(pos2);
4349 int2 = atoi(pos);
4350 }
4351
4352 return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
4353}
4354
4355
4356static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
4357{
4358 char *pos;
4359 unsigned int period = 0, interval = 0;
4360
4361 if (cmd[0]) {
4362 pos = os_strchr(cmd, ' ');
4363 if (pos == NULL)
4364 return -1;
4365 *pos++ = '\0';
4366 period = atoi(cmd);
4367 interval = atoi(pos);
4368 }
4369
4370 return wpas_p2p_ext_listen(wpa_s, period, interval);
4371}
4372
4373#endif /* CONFIG_P2P */
4374
4375
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004376#ifdef CONFIG_INTERWORKING
4377static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst)
4378{
4379 u8 bssid[ETH_ALEN];
4380 struct wpa_bss *bss;
4381
4382 if (hwaddr_aton(dst, bssid)) {
4383 wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
4384 return -1;
4385 }
4386
4387 bss = wpa_bss_get_bssid(wpa_s, bssid);
4388 if (bss == NULL) {
4389 wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
4390 MAC2STR(bssid));
4391 return -1;
4392 }
4393
4394 return interworking_connect(wpa_s, bss);
4395}
4396
4397
4398static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
4399{
4400 u8 dst_addr[ETH_ALEN];
4401 int used;
4402 char *pos;
4403#define MAX_ANQP_INFO_ID 100
4404 u16 id[MAX_ANQP_INFO_ID];
4405 size_t num_id = 0;
4406
4407 used = hwaddr_aton2(dst, dst_addr);
4408 if (used < 0)
4409 return -1;
4410 pos = dst + used;
4411 while (num_id < MAX_ANQP_INFO_ID) {
4412 id[num_id] = atoi(pos);
4413 if (id[num_id])
4414 num_id++;
4415 pos = os_strchr(pos + 1, ',');
4416 if (pos == NULL)
4417 break;
4418 pos++;
4419 }
4420
4421 if (num_id == 0)
4422 return -1;
4423
4424 return anqp_send_req(wpa_s, dst_addr, id, num_id);
4425}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004426
4427
4428static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
4429{
4430 u8 dst_addr[ETH_ALEN];
4431 struct wpabuf *advproto, *query = NULL;
4432 int used, ret = -1;
4433 char *pos, *end;
4434 size_t len;
4435
4436 used = hwaddr_aton2(cmd, dst_addr);
4437 if (used < 0)
4438 return -1;
4439
4440 pos = cmd + used;
4441 while (*pos == ' ')
4442 pos++;
4443
4444 /* Advertisement Protocol ID */
4445 end = os_strchr(pos, ' ');
4446 if (end)
4447 len = end - pos;
4448 else
4449 len = os_strlen(pos);
4450 if (len & 0x01)
4451 return -1;
4452 len /= 2;
4453 if (len == 0)
4454 return -1;
4455 advproto = wpabuf_alloc(len);
4456 if (advproto == NULL)
4457 return -1;
4458 if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
4459 goto fail;
4460
4461 if (end) {
4462 /* Optional Query Request */
4463 pos = end + 1;
4464 while (*pos == ' ')
4465 pos++;
4466
4467 len = os_strlen(pos);
4468 if (len) {
4469 if (len & 0x01)
4470 goto fail;
4471 len /= 2;
4472 if (len == 0)
4473 goto fail;
4474 query = wpabuf_alloc(len);
4475 if (query == NULL)
4476 goto fail;
4477 if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
4478 goto fail;
4479 }
4480 }
4481
4482 ret = gas_send_request(wpa_s, dst_addr, advproto, query);
4483
4484fail:
4485 wpabuf_free(advproto);
4486 wpabuf_free(query);
4487
4488 return ret;
4489}
4490
4491
4492static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
4493 size_t buflen)
4494{
4495 u8 addr[ETH_ALEN];
4496 int dialog_token;
4497 int used;
4498 char *pos;
4499 size_t resp_len, start, requested_len;
4500
4501 if (!wpa_s->last_gas_resp)
4502 return -1;
4503
4504 used = hwaddr_aton2(cmd, addr);
4505 if (used < 0)
4506 return -1;
4507
4508 pos = cmd + used;
4509 while (*pos == ' ')
4510 pos++;
4511 dialog_token = atoi(pos);
4512
4513 if (os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) != 0 ||
4514 dialog_token != wpa_s->last_gas_dialog_token)
4515 return -1;
4516
4517 resp_len = wpabuf_len(wpa_s->last_gas_resp);
4518 start = 0;
4519 requested_len = resp_len;
4520
4521 pos = os_strchr(pos, ' ');
4522 if (pos) {
4523 start = atoi(pos);
4524 if (start > resp_len)
4525 return os_snprintf(buf, buflen, "FAIL-Invalid range");
4526 pos = os_strchr(pos, ',');
4527 if (pos == NULL)
4528 return -1;
4529 pos++;
4530 requested_len = atoi(pos);
4531 if (start + requested_len > resp_len)
4532 return os_snprintf(buf, buflen, "FAIL-Invalid range");
4533 }
4534
4535 if (requested_len * 2 + 1 > buflen)
4536 return os_snprintf(buf, buflen, "FAIL-Too long response");
4537
4538 return wpa_snprintf_hex(buf, buflen,
4539 wpabuf_head_u8(wpa_s->last_gas_resp) + start,
4540 requested_len);
4541}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004542#endif /* CONFIG_INTERWORKING */
4543
4544
Dmitry Shmidt04949592012-07-19 12:16:46 -07004545#ifdef CONFIG_HS20
4546
4547static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
4548{
4549 u8 dst_addr[ETH_ALEN];
4550 int used;
4551 char *pos;
4552 u32 subtypes = 0;
4553
4554 used = hwaddr_aton2(dst, dst_addr);
4555 if (used < 0)
4556 return -1;
4557 pos = dst + used;
4558 for (;;) {
4559 int num = atoi(pos);
4560 if (num <= 0 || num > 31)
4561 return -1;
4562 subtypes |= BIT(num);
4563 pos = os_strchr(pos + 1, ',');
4564 if (pos == NULL)
4565 break;
4566 pos++;
4567 }
4568
4569 if (subtypes == 0)
4570 return -1;
4571
4572 return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0);
4573}
4574
4575
4576static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
4577 const u8 *addr, const char *realm)
4578{
4579 u8 *buf;
4580 size_t rlen, len;
4581 int ret;
4582
4583 rlen = os_strlen(realm);
4584 len = 3 + rlen;
4585 buf = os_malloc(len);
4586 if (buf == NULL)
4587 return -1;
4588 buf[0] = 1; /* NAI Home Realm Count */
4589 buf[1] = 0; /* Formatted in accordance with RFC 4282 */
4590 buf[2] = rlen;
4591 os_memcpy(buf + 3, realm, rlen);
4592
4593 ret = hs20_anqp_send_req(wpa_s, addr,
4594 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
4595 buf, len);
4596
4597 os_free(buf);
4598
4599 return ret;
4600}
4601
4602
4603static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
4604 char *dst)
4605{
4606 struct wpa_cred *cred = wpa_s->conf->cred;
4607 u8 dst_addr[ETH_ALEN];
4608 int used;
4609 u8 *buf;
4610 size_t len;
4611 int ret;
4612
4613 used = hwaddr_aton2(dst, dst_addr);
4614 if (used < 0)
4615 return -1;
4616
4617 while (dst[used] == ' ')
4618 used++;
4619 if (os_strncmp(dst + used, "realm=", 6) == 0)
4620 return hs20_nai_home_realm_list(wpa_s, dst_addr,
4621 dst + used + 6);
4622
4623 len = os_strlen(dst + used);
4624
4625 if (len == 0 && cred && cred->realm)
4626 return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
4627
4628 if (len % 1)
4629 return -1;
4630 len /= 2;
4631 buf = os_malloc(len);
4632 if (buf == NULL)
4633 return -1;
4634 if (hexstr2bin(dst + used, buf, len) < 0) {
4635 os_free(buf);
4636 return -1;
4637 }
4638
4639 ret = hs20_anqp_send_req(wpa_s, dst_addr,
4640 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
4641 buf, len);
4642 os_free(buf);
4643
4644 return ret;
4645}
4646
4647#endif /* CONFIG_HS20 */
4648
4649
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004650static int wpa_supplicant_ctrl_iface_sta_autoconnect(
4651 struct wpa_supplicant *wpa_s, char *cmd)
4652{
4653 wpa_s->auto_reconnect_disabled = atoi(cmd) == 0 ? 1 : 0;
4654 return 0;
4655}
4656
4657
Dmitry Shmidt04949592012-07-19 12:16:46 -07004658#ifdef CONFIG_AUTOSCAN
4659
4660static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
4661 char *cmd)
4662{
4663 enum wpa_states state = wpa_s->wpa_state;
4664 char *new_params = NULL;
4665
4666 if (os_strlen(cmd) > 0) {
4667 new_params = os_strdup(cmd);
4668 if (new_params == NULL)
4669 return -1;
4670 }
4671
4672 os_free(wpa_s->conf->autoscan);
4673 wpa_s->conf->autoscan = new_params;
4674
4675 if (wpa_s->conf->autoscan == NULL)
4676 autoscan_deinit(wpa_s);
4677 else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
4678 autoscan_init(wpa_s, 1);
4679 else if (state == WPA_SCANNING)
4680 wpa_supplicant_reinit_autoscan(wpa_s);
4681
4682 return 0;
4683}
4684
4685#endif /* CONFIG_AUTOSCAN */
4686
4687
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004688#ifdef CONFIG_WNM
4689
4690static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
4691{
4692 int enter;
4693 int intval = 0;
4694 char *pos;
4695 int ret;
4696 struct wpabuf *tfs_req = NULL;
4697
4698 if (os_strncmp(cmd, "enter", 5) == 0)
4699 enter = 1;
4700 else if (os_strncmp(cmd, "exit", 4) == 0)
4701 enter = 0;
4702 else
4703 return -1;
4704
4705 pos = os_strstr(cmd, " interval=");
4706 if (pos)
4707 intval = atoi(pos + 10);
4708
4709 pos = os_strstr(cmd, " tfs_req=");
4710 if (pos) {
4711 char *end;
4712 size_t len;
4713 pos += 9;
4714 end = os_strchr(pos, ' ');
4715 if (end)
4716 len = end - pos;
4717 else
4718 len = os_strlen(pos);
4719 if (len & 1)
4720 return -1;
4721 len /= 2;
4722 tfs_req = wpabuf_alloc(len);
4723 if (tfs_req == NULL)
4724 return -1;
4725 if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
4726 wpabuf_free(tfs_req);
4727 return -1;
4728 }
4729 }
4730
4731 ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
4732 WNM_SLEEP_MODE_EXIT, intval,
4733 tfs_req);
4734 wpabuf_free(tfs_req);
4735
4736 return ret;
4737}
4738
4739#endif /* CONFIG_WNM */
4740
4741
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004742static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
4743 size_t buflen)
4744{
4745 struct wpa_signal_info si;
4746 int ret;
4747
4748 ret = wpa_drv_signal_poll(wpa_s, &si);
4749 if (ret)
4750 return -1;
4751
4752 ret = os_snprintf(buf, buflen, "RSSI=%d\nLINKSPEED=%d\n"
4753 "NOISE=%d\nFREQUENCY=%u\n",
4754 si.current_signal, si.current_txrate / 1000,
4755 si.current_noise, si.frequency);
4756 if (ret < 0 || (unsigned int) ret > buflen)
4757 return -1;
4758 return ret;
4759}
4760
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03004761
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07004762static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
4763 size_t buflen)
4764{
4765 struct hostap_sta_driver_data sta;
4766 int ret;
4767
4768 ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
4769 if (ret)
4770 return -1;
4771
4772 ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03004773 sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
4774 if (ret < 0 || (size_t) ret > buflen)
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07004775 return -1;
4776 return ret;
4777}
4778
4779
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004780#ifdef ANDROID
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07004781static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
4782 char *buf, size_t buflen)
4783{
4784 int ret;
4785
4786 ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
4787 if (ret == 0)
4788 ret = sprintf(buf, "%s\n", "OK");
4789 return ret;
4790}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004791#endif
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07004792
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004793
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004794char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
4795 char *buf, size_t *resp_len)
4796{
4797 char *reply;
4798 const int reply_size = 4096;
4799 int ctrl_rsp = 0;
4800 int reply_len;
4801
4802 if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004803 os_strncmp(buf, "SET_NETWORK ", 12) == 0 ||
4804 os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
4805 os_strncmp(buf, "NFC_RX_HANDOVER_SEL", 19) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004806 wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
4807 (const u8 *) buf, os_strlen(buf));
4808 } else {
4809 int level = MSG_DEBUG;
4810 if (os_strcmp(buf, "PING") == 0)
4811 level = MSG_EXCESSIVE;
4812 wpa_hexdump_ascii(level, "RX ctrl_iface",
4813 (const u8 *) buf, os_strlen(buf));
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004814 wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004815 }
4816
4817 reply = os_malloc(reply_size);
4818 if (reply == NULL) {
4819 *resp_len = 1;
4820 return NULL;
4821 }
4822
4823 os_memcpy(reply, "OK\n", 3);
4824 reply_len = 3;
4825
4826 if (os_strcmp(buf, "PING") == 0) {
4827 os_memcpy(reply, "PONG\n", 5);
4828 reply_len = 5;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004829 } else if (os_strcmp(buf, "IFNAME") == 0) {
4830 reply_len = os_strlen(wpa_s->ifname);
4831 os_memcpy(reply, wpa_s->ifname, reply_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004832 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
4833 if (wpa_debug_reopen_file() < 0)
4834 reply_len = -1;
4835 } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
4836 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
4837 } else if (os_strcmp(buf, "MIB") == 0) {
4838 reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
4839 if (reply_len >= 0) {
4840 int res;
4841 res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
4842 reply_size - reply_len);
4843 if (res < 0)
4844 reply_len = -1;
4845 else
4846 reply_len += res;
4847 }
4848 } else if (os_strncmp(buf, "STATUS", 6) == 0) {
4849 reply_len = wpa_supplicant_ctrl_iface_status(
4850 wpa_s, buf + 6, reply, reply_size);
4851 } else if (os_strcmp(buf, "PMKSA") == 0) {
4852 reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
4853 reply_size);
4854 } else if (os_strncmp(buf, "SET ", 4) == 0) {
4855 if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
4856 reply_len = -1;
4857 } else if (os_strncmp(buf, "GET ", 4) == 0) {
4858 reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
4859 reply, reply_size);
4860 } else if (os_strcmp(buf, "LOGON") == 0) {
4861 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
4862 } else if (os_strcmp(buf, "LOGOFF") == 0) {
4863 eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
4864 } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
4865 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
4866 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004867 else
4868 wpas_request_connection(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004869 } else if (os_strcmp(buf, "RECONNECT") == 0) {
4870 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
4871 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004872 else if (wpa_s->disconnected)
4873 wpas_request_connection(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004874#ifdef IEEE8021X_EAPOL
4875 } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
4876 if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
4877 reply_len = -1;
4878#endif /* IEEE8021X_EAPOL */
4879#ifdef CONFIG_PEERKEY
4880 } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
4881 if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
4882 reply_len = -1;
4883#endif /* CONFIG_PEERKEY */
4884#ifdef CONFIG_IEEE80211R
4885 } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
4886 if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
4887 reply_len = -1;
4888#endif /* CONFIG_IEEE80211R */
4889#ifdef CONFIG_WPS
4890 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
4891 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
4892 if (res == -2) {
4893 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
4894 reply_len = 17;
4895 } else if (res)
4896 reply_len = -1;
4897 } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
4898 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
4899 if (res == -2) {
4900 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
4901 reply_len = 17;
4902 } else if (res)
4903 reply_len = -1;
4904 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
4905 reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
4906 reply,
4907 reply_size);
4908 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
4909 reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
4910 wpa_s, buf + 14, reply, reply_size);
4911 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
4912 if (wpas_wps_cancel(wpa_s))
4913 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004914#ifdef CONFIG_WPS_NFC
4915 } else if (os_strcmp(buf, "WPS_NFC") == 0) {
4916 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
4917 reply_len = -1;
4918 } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
4919 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
4920 reply_len = -1;
4921 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
4922 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
4923 wpa_s, buf + 14, reply, reply_size);
4924 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
4925 if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
4926 buf + 17))
4927 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004928 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
4929 reply_len = wpas_ctrl_nfc_get_handover_req(
4930 wpa_s, buf + 21, reply, reply_size);
4931 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
4932 reply_len = wpas_ctrl_nfc_get_handover_sel(
4933 wpa_s, buf + 21, reply, reply_size);
4934 } else if (os_strncmp(buf, "NFC_RX_HANDOVER_REQ ", 20) == 0) {
4935 reply_len = wpas_ctrl_nfc_rx_handover_req(
4936 wpa_s, buf + 20, reply, reply_size);
4937 } else if (os_strncmp(buf, "NFC_RX_HANDOVER_SEL ", 20) == 0) {
4938 if (wpas_ctrl_nfc_rx_handover_sel(wpa_s, buf + 20))
4939 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004940#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004941 } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
4942 if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
4943 reply_len = -1;
4944#ifdef CONFIG_AP
4945 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
4946 reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
4947 wpa_s, buf + 11, reply, reply_size);
4948#endif /* CONFIG_AP */
4949#ifdef CONFIG_WPS_ER
4950 } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
4951 if (wpas_wps_er_start(wpa_s, NULL))
4952 reply_len = -1;
4953 } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
4954 if (wpas_wps_er_start(wpa_s, buf + 13))
4955 reply_len = -1;
4956 } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
4957 if (wpas_wps_er_stop(wpa_s))
4958 reply_len = -1;
4959 } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
4960 if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
4961 reply_len = -1;
4962 } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
4963 int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
4964 if (ret == -2) {
4965 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
4966 reply_len = 17;
4967 } else if (ret == -3) {
4968 os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
4969 reply_len = 18;
4970 } else if (ret == -4) {
4971 os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
4972 reply_len = 20;
4973 } else if (ret)
4974 reply_len = -1;
4975 } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
4976 if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
4977 reply_len = -1;
4978 } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
4979 if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
4980 buf + 18))
4981 reply_len = -1;
4982 } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
4983 if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
4984 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004985#ifdef CONFIG_WPS_NFC
4986 } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
4987 reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
4988 wpa_s, buf + 24, reply, reply_size);
4989#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004990#endif /* CONFIG_WPS_ER */
4991#endif /* CONFIG_WPS */
4992#ifdef CONFIG_IBSS_RSN
4993 } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
4994 if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
4995 reply_len = -1;
4996#endif /* CONFIG_IBSS_RSN */
4997#ifdef CONFIG_P2P
4998 } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
4999 if (p2p_ctrl_find(wpa_s, buf + 9))
5000 reply_len = -1;
5001 } else if (os_strcmp(buf, "P2P_FIND") == 0) {
5002 if (p2p_ctrl_find(wpa_s, ""))
5003 reply_len = -1;
5004 } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
5005 wpas_p2p_stop_find(wpa_s);
5006 } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
5007 reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
5008 reply_size);
5009 } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
5010 if (p2p_ctrl_listen(wpa_s, buf + 11))
5011 reply_len = -1;
5012 } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
5013 if (p2p_ctrl_listen(wpa_s, ""))
5014 reply_len = -1;
5015 } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
5016 if (wpas_p2p_group_remove(wpa_s, buf + 17))
5017 reply_len = -1;
5018 } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005019 if (wpas_p2p_group_add(wpa_s, 0, 0, 0))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005020 reply_len = -1;
5021 } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
5022 if (p2p_ctrl_group_add(wpa_s, buf + 14))
5023 reply_len = -1;
5024 } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
5025 if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
5026 reply_len = -1;
5027 } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
5028 reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
5029 } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
5030 reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
5031 reply_size);
5032 } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
5033 if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
5034 reply_len = -1;
5035 } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
5036 if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
5037 reply_len = -1;
5038 } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07005039#ifdef ANDROID_P2P
5040 wpas_p2p_sd_service_update(wpa_s, SRV_UPDATE);
5041#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005042 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07005043#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005044 } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
5045 if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
5046 reply_len = -1;
5047 } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
5048 wpas_p2p_service_flush(wpa_s);
5049 } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
5050 if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
5051 reply_len = -1;
5052 } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
5053 if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
5054 reply_len = -1;
5055 } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
5056 if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
5057 reply_len = -1;
5058 } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
5059 if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
5060 reply_len = -1;
5061 } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
5062 reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
5063 reply_size);
5064 } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
5065 if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
5066 reply_len = -1;
5067 } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
5068 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
5069 wpa_s->force_long_sd = 0;
5070 if (wpa_s->global->p2p)
5071 p2p_flush(wpa_s->global->p2p);
5072 } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
5073 if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
5074 reply_len = -1;
5075 } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
5076 if (wpas_p2p_cancel(wpa_s))
5077 reply_len = -1;
5078 } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
5079 if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
5080 reply_len = -1;
5081 } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
5082 if (p2p_ctrl_presence_req(wpa_s, "") < 0)
5083 reply_len = -1;
5084 } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
5085 if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
5086 reply_len = -1;
5087 } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
5088 if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
5089 reply_len = -1;
5090#endif /* CONFIG_P2P */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005091#ifdef CONFIG_WIFI_DISPLAY
5092 } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
5093 if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
5094 reply_len = -1;
5095 } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
5096 reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
5097 reply, reply_size);
5098#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005099#ifdef CONFIG_INTERWORKING
5100 } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
5101 if (interworking_fetch_anqp(wpa_s) < 0)
5102 reply_len = -1;
5103 } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
5104 interworking_stop_fetch_anqp(wpa_s);
5105 } else if (os_strncmp(buf, "INTERWORKING_SELECT", 19) == 0) {
5106 if (interworking_select(wpa_s, os_strstr(buf + 19, "auto") !=
5107 NULL) < 0)
5108 reply_len = -1;
5109 } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
5110 if (ctrl_interworking_connect(wpa_s, buf + 21) < 0)
5111 reply_len = -1;
5112 } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
5113 if (get_anqp(wpa_s, buf + 9) < 0)
5114 reply_len = -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005115 } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
5116 if (gas_request(wpa_s, buf + 12) < 0)
5117 reply_len = -1;
5118 } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
5119 reply_len = gas_response_get(wpa_s, buf + 17, reply,
5120 reply_size);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005121#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt04949592012-07-19 12:16:46 -07005122#ifdef CONFIG_HS20
5123 } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
5124 if (get_hs20_anqp(wpa_s, buf + 14) < 0)
5125 reply_len = -1;
5126 } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
5127 if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
5128 reply_len = -1;
5129#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005130 } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
5131 {
5132 if (wpa_supplicant_ctrl_iface_ctrl_rsp(
5133 wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
5134 reply_len = -1;
5135 else
5136 ctrl_rsp = 1;
5137 } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
5138 if (wpa_supplicant_reload_configuration(wpa_s))
5139 reply_len = -1;
5140 } else if (os_strcmp(buf, "TERMINATE") == 0) {
5141 wpa_supplicant_terminate_proc(wpa_s->global);
5142 } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
5143 if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
5144 reply_len = -1;
Dmitry Shmidte19501d2011-03-16 14:32:18 -07005145 } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005146 reply_len = wpa_supplicant_ctrl_iface_blacklist(
5147 wpa_s, buf + 9, reply, reply_size);
5148 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
5149 reply_len = wpa_supplicant_ctrl_iface_log_level(
5150 wpa_s, buf + 9, reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005151 } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
5152 reply_len = wpa_supplicant_ctrl_iface_list_networks(
5153 wpa_s, reply, reply_size);
5154 } else if (os_strcmp(buf, "DISCONNECT") == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005155#ifdef CONFIG_SME
5156 wpa_s->sme.prev_bssid_set = 0;
5157#endif /* CONFIG_SME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005158 wpa_s->reassociate = 0;
5159 wpa_s->disconnected = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005160 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005161 wpa_supplicant_cancel_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005162 wpa_supplicant_deauthenticate(wpa_s,
5163 WLAN_REASON_DEAUTH_LEAVING);
5164 } else if (os_strcmp(buf, "SCAN") == 0) {
5165 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
5166 reply_len = -1;
5167 else {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005168 if (!wpa_s->sched_scanning && !wpa_s->scanning &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005169 ((wpa_s->wpa_state <= WPA_SCANNING) ||
5170 (wpa_s->wpa_state == WPA_COMPLETED))) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005171 wpa_s->normal_scans = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005172 wpa_s->scan_req = MANUAL_SCAN_REQ;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005173 wpa_supplicant_req_scan(wpa_s, 0, 0);
5174 } else if (wpa_s->sched_scanning) {
5175 wpa_printf(MSG_DEBUG, "Stop ongoing "
5176 "sched_scan to allow requested "
5177 "full scan to proceed");
5178 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005179 wpa_s->scan_req = MANUAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005180 wpa_supplicant_req_scan(wpa_s, 0, 0);
5181 } else {
5182 wpa_printf(MSG_DEBUG, "Ongoing scan action - "
5183 "reject new request");
5184 reply_len = os_snprintf(reply, reply_size,
5185 "FAIL-BUSY\n");
5186 }
5187 }
5188 } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
5189 reply_len = wpa_supplicant_ctrl_iface_scan_results(
5190 wpa_s, reply, reply_size);
5191 } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
5192 if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
5193 reply_len = -1;
5194 } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
5195 if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
5196 reply_len = -1;
5197 } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
5198 if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
5199 reply_len = -1;
5200 } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
5201 reply_len = wpa_supplicant_ctrl_iface_add_network(
5202 wpa_s, reply, reply_size);
5203 } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
5204 if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
5205 reply_len = -1;
5206 } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
5207 if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
5208 reply_len = -1;
5209 } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
5210 reply_len = wpa_supplicant_ctrl_iface_get_network(
5211 wpa_s, buf + 12, reply, reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005212 } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
5213 reply_len = wpa_supplicant_ctrl_iface_list_creds(
5214 wpa_s, reply, reply_size);
5215 } else if (os_strcmp(buf, "ADD_CRED") == 0) {
5216 reply_len = wpa_supplicant_ctrl_iface_add_cred(
5217 wpa_s, reply, reply_size);
5218 } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
5219 if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
5220 reply_len = -1;
5221 } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
5222 if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
5223 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005224#ifndef CONFIG_NO_CONFIG_WRITE
5225 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
5226 if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
5227 reply_len = -1;
5228#endif /* CONFIG_NO_CONFIG_WRITE */
5229 } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
5230 reply_len = wpa_supplicant_ctrl_iface_get_capability(
5231 wpa_s, buf + 15, reply, reply_size);
5232 } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
5233 if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
5234 reply_len = -1;
5235 } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
5236 if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
5237 reply_len = -1;
5238 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
5239 reply_len = wpa_supplicant_global_iface_list(
5240 wpa_s->global, reply, reply_size);
5241 } else if (os_strcmp(buf, "INTERFACES") == 0) {
5242 reply_len = wpa_supplicant_global_iface_interfaces(
5243 wpa_s->global, reply, reply_size);
5244 } else if (os_strncmp(buf, "BSS ", 4) == 0) {
5245 reply_len = wpa_supplicant_ctrl_iface_bss(
5246 wpa_s, buf + 4, reply, reply_size);
5247#ifdef CONFIG_AP
5248 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
5249 reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
5250 } else if (os_strncmp(buf, "STA ", 4) == 0) {
5251 reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
5252 reply_size);
5253 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
5254 reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
5255 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005256 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
5257 if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
5258 reply_len = -1;
5259 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
5260 if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
5261 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005262#endif /* CONFIG_AP */
5263 } else if (os_strcmp(buf, "SUSPEND") == 0) {
5264 wpas_notify_suspend(wpa_s->global);
5265 } else if (os_strcmp(buf, "RESUME") == 0) {
5266 wpas_notify_resume(wpa_s->global);
5267 } else if (os_strcmp(buf, "DROP_SA") == 0) {
5268 wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
5269 } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
5270 if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
5271 reply_len = -1;
5272 } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
5273 if (wpa_supplicant_ctrl_iface_sta_autoconnect(wpa_s, buf + 16))
5274 reply_len = -1;
5275 } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
5276 if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
5277 reply_len = -1;
5278 } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
5279 if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
5280 buf + 17))
5281 reply_len = -1;
Dmitry Shmidtf48e4f92012-08-24 11:14:44 -07005282 } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
5283 if (wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10))
5284 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005285#ifdef CONFIG_TDLS
5286 } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
5287 if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
5288 reply_len = -1;
5289 } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
5290 if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
5291 reply_len = -1;
5292 } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
5293 if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
5294 reply_len = -1;
5295#endif /* CONFIG_TDLS */
5296 } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
5297 reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
5298 reply_size);
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07005299 } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
5300 reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
5301 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005302#ifdef CONFIG_AUTOSCAN
5303 } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
5304 if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
5305 reply_len = -1;
5306#endif /* CONFIG_AUTOSCAN */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005307#ifdef ANDROID
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07005308 } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
5309 reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
5310 reply_size);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005311#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005312 } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005313 pmksa_cache_clear_current(wpa_s->wpa);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005314 eapol_sm_request_reauth(wpa_s->eapol);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005315#ifdef CONFIG_WNM
5316 } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
5317 if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
5318 reply_len = -1;
5319#endif /* CONFIG_WNM */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005320 } else {
5321 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
5322 reply_len = 16;
5323 }
5324
5325 if (reply_len < 0) {
5326 os_memcpy(reply, "FAIL\n", 5);
5327 reply_len = 5;
5328 }
5329
5330 if (ctrl_rsp)
5331 eapol_sm_notify_ctrl_response(wpa_s->eapol);
5332
5333 *resp_len = reply_len;
5334 return reply;
5335}
5336
5337
5338static int wpa_supplicant_global_iface_add(struct wpa_global *global,
5339 char *cmd)
5340{
5341 struct wpa_interface iface;
5342 char *pos;
5343
5344 /*
5345 * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
5346 * TAB<bridge_ifname>
5347 */
5348 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
5349
5350 os_memset(&iface, 0, sizeof(iface));
5351
5352 do {
5353 iface.ifname = pos = cmd;
5354 pos = os_strchr(pos, '\t');
5355 if (pos)
5356 *pos++ = '\0';
5357 if (iface.ifname[0] == '\0')
5358 return -1;
5359 if (pos == NULL)
5360 break;
5361
5362 iface.confname = pos;
5363 pos = os_strchr(pos, '\t');
5364 if (pos)
5365 *pos++ = '\0';
5366 if (iface.confname[0] == '\0')
5367 iface.confname = NULL;
5368 if (pos == NULL)
5369 break;
5370
5371 iface.driver = pos;
5372 pos = os_strchr(pos, '\t');
5373 if (pos)
5374 *pos++ = '\0';
5375 if (iface.driver[0] == '\0')
5376 iface.driver = NULL;
5377 if (pos == NULL)
5378 break;
5379
5380 iface.ctrl_interface = pos;
5381 pos = os_strchr(pos, '\t');
5382 if (pos)
5383 *pos++ = '\0';
5384 if (iface.ctrl_interface[0] == '\0')
5385 iface.ctrl_interface = NULL;
5386 if (pos == NULL)
5387 break;
5388
5389 iface.driver_param = pos;
5390 pos = os_strchr(pos, '\t');
5391 if (pos)
5392 *pos++ = '\0';
5393 if (iface.driver_param[0] == '\0')
5394 iface.driver_param = NULL;
5395 if (pos == NULL)
5396 break;
5397
5398 iface.bridge_ifname = pos;
5399 pos = os_strchr(pos, '\t');
5400 if (pos)
5401 *pos++ = '\0';
5402 if (iface.bridge_ifname[0] == '\0')
5403 iface.bridge_ifname = NULL;
5404 if (pos == NULL)
5405 break;
5406 } while (0);
5407
5408 if (wpa_supplicant_get_iface(global, iface.ifname))
5409 return -1;
5410
5411 return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
5412}
5413
5414
5415static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
5416 char *cmd)
5417{
5418 struct wpa_supplicant *wpa_s;
5419
5420 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
5421
5422 wpa_s = wpa_supplicant_get_iface(global, cmd);
5423 if (wpa_s == NULL)
5424 return -1;
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07005425 return wpa_supplicant_remove_iface(global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005426}
5427
5428
5429static void wpa_free_iface_info(struct wpa_interface_info *iface)
5430{
5431 struct wpa_interface_info *prev;
5432
5433 while (iface) {
5434 prev = iface;
5435 iface = iface->next;
5436
5437 os_free(prev->ifname);
5438 os_free(prev->desc);
5439 os_free(prev);
5440 }
5441}
5442
5443
5444static int wpa_supplicant_global_iface_list(struct wpa_global *global,
5445 char *buf, int len)
5446{
5447 int i, res;
5448 struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
5449 char *pos, *end;
5450
5451 for (i = 0; wpa_drivers[i]; i++) {
5452 struct wpa_driver_ops *drv = wpa_drivers[i];
5453 if (drv->get_interfaces == NULL)
5454 continue;
5455 tmp = drv->get_interfaces(global->drv_priv[i]);
5456 if (tmp == NULL)
5457 continue;
5458
5459 if (last == NULL)
5460 iface = last = tmp;
5461 else
5462 last->next = tmp;
5463 while (last->next)
5464 last = last->next;
5465 }
5466
5467 pos = buf;
5468 end = buf + len;
5469 for (tmp = iface; tmp; tmp = tmp->next) {
5470 res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
5471 tmp->drv_name, tmp->ifname,
5472 tmp->desc ? tmp->desc : "");
5473 if (res < 0 || res >= end - pos) {
5474 *pos = '\0';
5475 break;
5476 }
5477 pos += res;
5478 }
5479
5480 wpa_free_iface_info(iface);
5481
5482 return pos - buf;
5483}
5484
5485
5486static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
5487 char *buf, int len)
5488{
5489 int res;
5490 char *pos, *end;
5491 struct wpa_supplicant *wpa_s;
5492
5493 wpa_s = global->ifaces;
5494 pos = buf;
5495 end = buf + len;
5496
5497 while (wpa_s) {
5498 res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
5499 if (res < 0 || res >= end - pos) {
5500 *pos = '\0';
5501 break;
5502 }
5503 pos += res;
5504 wpa_s = wpa_s->next;
5505 }
5506 return pos - buf;
5507}
5508
5509
5510char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
5511 char *buf, size_t *resp_len)
5512{
5513 char *reply;
5514 const int reply_size = 2048;
5515 int reply_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005516 int level = MSG_DEBUG;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005517
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005518 if (os_strcmp(buf, "PING") == 0)
5519 level = MSG_EXCESSIVE;
5520 wpa_hexdump_ascii(level, "RX global ctrl_iface",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005521 (const u8 *) buf, os_strlen(buf));
5522
5523 reply = os_malloc(reply_size);
5524 if (reply == NULL) {
5525 *resp_len = 1;
5526 return NULL;
5527 }
5528
5529 os_memcpy(reply, "OK\n", 3);
5530 reply_len = 3;
5531
5532 if (os_strcmp(buf, "PING") == 0) {
5533 os_memcpy(reply, "PONG\n", 5);
5534 reply_len = 5;
5535 } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
5536 if (wpa_supplicant_global_iface_add(global, buf + 14))
5537 reply_len = -1;
5538 } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
5539 if (wpa_supplicant_global_iface_remove(global, buf + 17))
5540 reply_len = -1;
5541 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
5542 reply_len = wpa_supplicant_global_iface_list(
5543 global, reply, reply_size);
5544 } else if (os_strcmp(buf, "INTERFACES") == 0) {
5545 reply_len = wpa_supplicant_global_iface_interfaces(
5546 global, reply, reply_size);
5547 } else if (os_strcmp(buf, "TERMINATE") == 0) {
5548 wpa_supplicant_terminate_proc(global);
5549 } else if (os_strcmp(buf, "SUSPEND") == 0) {
5550 wpas_notify_suspend(global);
5551 } else if (os_strcmp(buf, "RESUME") == 0) {
5552 wpas_notify_resume(global);
5553 } else {
5554 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
5555 reply_len = 16;
5556 }
5557
5558 if (reply_len < 0) {
5559 os_memcpy(reply, "FAIL\n", 5);
5560 reply_len = 5;
5561 }
5562
5563 *resp_len = reply_len;
5564 return reply;
5565}