blob: 793faec5880921da1d13a77df5353ba5e0497964 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant / Control interface (shared code for all backends)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003 * Copyright (c) 2004-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
12#include "utils/eloop.h"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080013#include "utils/uuid.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014#include "common/version.h"
15#include "common/ieee802_11_defs.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070016#include "common/ieee802_11_common.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070017#include "common/wpa_ctrl.h"
18#include "eap_peer/eap.h"
19#include "eapol_supp/eapol_supp_sm.h"
20#include "rsn_supp/wpa.h"
21#include "rsn_supp/preauth.h"
22#include "rsn_supp/pmksa_cache.h"
23#include "l2_packet/l2_packet.h"
24#include "wps/wps.h"
25#include "config.h"
26#include "wpa_supplicant_i.h"
27#include "driver_i.h"
28#include "wps_supplicant.h"
29#include "ibss_rsn.h"
30#include "ap.h"
31#include "p2p_supplicant.h"
32#include "p2p/p2p.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070033#include "hs20_supplicant.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070034#include "wifi_display.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070035#include "notify.h"
36#include "bss.h"
37#include "scan.h"
38#include "ctrl_iface.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080039#include "interworking.h"
Dmitry Shmidte19501d2011-03-16 14:32:18 -070040#include "blacklist.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070041#include "autoscan.h"
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080042#include "wnm_sta.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070043
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070044static int wpa_supplicant_global_iface_list(struct wpa_global *global,
45 char *buf, int len);
46static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
47 char *buf, int len);
48
49
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080050static int pno_start(struct wpa_supplicant *wpa_s)
51{
Dmitry Shmidt56052862013-10-04 10:23:25 -070052 int ret, interval;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080053 size_t i, num_ssid;
54 struct wpa_ssid *ssid;
55 struct wpa_driver_scan_params params;
56
Dmitry Shmidt18463232014-01-24 12:29:41 -080057 if (wpa_s->pno || wpa_s->pno_sched_pending)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080058 return 0;
59
Dmitry Shmidt4d9ecd82013-11-05 18:08:27 -080060 if ((wpa_s->wpa_state > WPA_SCANNING) &&
61 (wpa_s->wpa_state <= WPA_COMPLETED)) {
62 wpa_printf(MSG_ERROR, "PNO: In assoc process");
63 return -EAGAIN;
64 }
65
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080066 if (wpa_s->wpa_state == WPA_SCANNING) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080067 wpa_supplicant_cancel_scan(wpa_s);
Dmitry Shmidt18463232014-01-24 12:29:41 -080068 if (wpa_s->sched_scanning) {
69 wpa_printf(MSG_DEBUG, "Schedule PNO on completion of "
70 "ongoing sched scan");
71 wpa_supplicant_cancel_sched_scan(wpa_s);
72 wpa_s->pno_sched_pending = 1;
73 return 0;
74 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080075 }
76
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080077 os_memset(&params, 0, sizeof(params));
78
79 num_ssid = 0;
80 ssid = wpa_s->conf->ssid;
81 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -070082 if (!wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080083 num_ssid++;
84 ssid = ssid->next;
85 }
86 if (num_ssid > WPAS_MAX_SCAN_SSIDS) {
87 wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
88 "%u", WPAS_MAX_SCAN_SSIDS, (unsigned int) num_ssid);
89 num_ssid = WPAS_MAX_SCAN_SSIDS;
90 }
91
92 if (num_ssid == 0) {
93 wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
94 return -1;
95 }
96
97 params.filter_ssids = os_malloc(sizeof(struct wpa_driver_scan_filter) *
98 num_ssid);
99 if (params.filter_ssids == NULL)
100 return -1;
101 i = 0;
102 ssid = wpa_s->conf->ssid;
103 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700104 if (!wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800105 params.ssids[i].ssid = ssid->ssid;
106 params.ssids[i].ssid_len = ssid->ssid_len;
107 params.num_ssids++;
108 os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
109 ssid->ssid_len);
110 params.filter_ssids[i].ssid_len = ssid->ssid_len;
111 params.num_filter_ssids++;
112 i++;
113 if (i == num_ssid)
114 break;
115 }
116 ssid = ssid->next;
117 }
118
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700119 if (wpa_s->conf->filter_rssi)
120 params.filter_rssi = wpa_s->conf->filter_rssi;
121
Dmitry Shmidt56052862013-10-04 10:23:25 -0700122 interval = wpa_s->conf->sched_scan_interval ?
123 wpa_s->conf->sched_scan_interval : 10;
124
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800125 ret = wpa_supplicant_start_sched_scan(wpa_s, &params, interval);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800126 os_free(params.filter_ssids);
127 if (ret == 0)
128 wpa_s->pno = 1;
129 return ret;
130}
131
132
133static int pno_stop(struct wpa_supplicant *wpa_s)
134{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800135 int ret = 0;
136
Dmitry Shmidt18463232014-01-24 12:29:41 -0800137 if (wpa_s->pno || wpa_s->sched_scanning) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800138 wpa_s->pno = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800139 ret = wpa_supplicant_stop_sched_scan(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800140 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800141
Dmitry Shmidt18463232014-01-24 12:29:41 -0800142 wpa_s->pno_sched_pending = 0;
143
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800144 if (wpa_s->wpa_state == WPA_SCANNING)
145 wpa_supplicant_req_scan(wpa_s, 0, 0);
146
147 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800148}
149
150
Dmitry Shmidt04949592012-07-19 12:16:46 -0700151static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
152{
153 char *pos;
154 u8 addr[ETH_ALEN], *filter = NULL, *n;
155 size_t count = 0;
156
157 pos = val;
158 while (pos) {
159 if (*pos == '\0')
160 break;
161 if (hwaddr_aton(pos, addr)) {
162 os_free(filter);
163 return -1;
164 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700165 n = os_realloc_array(filter, count + 1, ETH_ALEN);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700166 if (n == NULL) {
167 os_free(filter);
168 return -1;
169 }
170 filter = n;
171 os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
172 count++;
173
174 pos = os_strchr(pos, ' ');
175 if (pos)
176 pos++;
177 }
178
179 wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
180 os_free(wpa_s->bssid_filter);
181 wpa_s->bssid_filter = filter;
182 wpa_s->bssid_filter_count = count;
183
184 return 0;
185}
186
187
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800188static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
189{
190 char *pos;
191 u8 addr[ETH_ALEN], *bssid = NULL, *n;
192 struct wpa_ssid_value *ssid = NULL, *ns;
193 size_t count = 0, ssid_count = 0;
194 struct wpa_ssid *c;
195
196 /*
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800197 * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | ""
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800198 * SSID_SPEC ::= ssid <SSID_HEX>
199 * BSSID_SPEC ::= bssid <BSSID_HEX>
200 */
201
202 pos = val;
203 while (pos) {
204 if (*pos == '\0')
205 break;
206 if (os_strncmp(pos, "bssid ", 6) == 0) {
207 int res;
208 pos += 6;
209 res = hwaddr_aton2(pos, addr);
210 if (res < 0) {
211 os_free(ssid);
212 os_free(bssid);
213 wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
214 "BSSID value '%s'", pos);
215 return -1;
216 }
217 pos += res;
218 n = os_realloc_array(bssid, count + 1, ETH_ALEN);
219 if (n == NULL) {
220 os_free(ssid);
221 os_free(bssid);
222 return -1;
223 }
224 bssid = n;
225 os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
226 count++;
227 } else if (os_strncmp(pos, "ssid ", 5) == 0) {
228 char *end;
229 pos += 5;
230
231 end = pos;
232 while (*end) {
233 if (*end == '\0' || *end == ' ')
234 break;
235 end++;
236 }
237
238 ns = os_realloc_array(ssid, ssid_count + 1,
239 sizeof(struct wpa_ssid_value));
240 if (ns == NULL) {
241 os_free(ssid);
242 os_free(bssid);
243 return -1;
244 }
245 ssid = ns;
246
247 if ((end - pos) & 0x01 || end - pos > 2 * 32 ||
248 hexstr2bin(pos, ssid[ssid_count].ssid,
249 (end - pos) / 2) < 0) {
250 os_free(ssid);
251 os_free(bssid);
252 wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
253 "SSID value '%s'", pos);
254 return -1;
255 }
256 ssid[ssid_count].ssid_len = (end - pos) / 2;
257 wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
258 ssid[ssid_count].ssid,
259 ssid[ssid_count].ssid_len);
260 ssid_count++;
261 pos = end;
262 } else {
263 wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
264 "'%s'", pos);
265 os_free(ssid);
266 os_free(bssid);
267 return -1;
268 }
269
270 pos = os_strchr(pos, ' ');
271 if (pos)
272 pos++;
273 }
274
275 wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
276 os_free(wpa_s->disallow_aps_bssid);
277 wpa_s->disallow_aps_bssid = bssid;
278 wpa_s->disallow_aps_bssid_count = count;
279
280 wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
281 os_free(wpa_s->disallow_aps_ssid);
282 wpa_s->disallow_aps_ssid = ssid;
283 wpa_s->disallow_aps_ssid_count = ssid_count;
284
285 if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
286 return 0;
287
288 c = wpa_s->current_ssid;
289 if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
290 return 0;
291
292 if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
293 !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
294 return 0;
295
296 wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
297 "because current AP was marked disallowed");
298
299#ifdef CONFIG_SME
300 wpa_s->sme.prev_bssid_set = 0;
301#endif /* CONFIG_SME */
302 wpa_s->reassociate = 1;
303 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
304 wpa_supplicant_req_scan(wpa_s, 0, 0);
305
306 return 0;
307}
308
309
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700310static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
311 char *cmd)
312{
313 char *value;
314 int ret = 0;
315
316 value = os_strchr(cmd, ' ');
317 if (value == NULL)
318 return -1;
319 *value++ = '\0';
320
321 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
322 if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
323 eapol_sm_configure(wpa_s->eapol,
324 atoi(value), -1, -1, -1);
325 } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
326 eapol_sm_configure(wpa_s->eapol,
327 -1, atoi(value), -1, -1);
328 } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
329 eapol_sm_configure(wpa_s->eapol,
330 -1, -1, atoi(value), -1);
331 } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
332 eapol_sm_configure(wpa_s->eapol,
333 -1, -1, -1, atoi(value));
334 } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
335 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
336 atoi(value)))
337 ret = -1;
338 } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
339 0) {
340 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
341 atoi(value)))
342 ret = -1;
343 } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
344 if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
345 ret = -1;
346 } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
347 wpa_s->wps_fragment_size = atoi(value);
348#ifdef CONFIG_WPS_TESTING
349 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
350 long int val;
351 val = strtol(value, NULL, 0);
352 if (val < 0 || val > 0xff) {
353 ret = -1;
354 wpa_printf(MSG_DEBUG, "WPS: Invalid "
355 "wps_version_number %ld", val);
356 } else {
357 wps_version_number = val;
358 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
359 "version %u.%u",
360 (wps_version_number & 0xf0) >> 4,
361 wps_version_number & 0x0f);
362 }
363 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
364 wps_testing_dummy_cred = atoi(value);
365 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
366 wps_testing_dummy_cred);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800367 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
368 wps_corrupt_pkhash = atoi(value);
369 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
370 wps_corrupt_pkhash);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700371#endif /* CONFIG_WPS_TESTING */
372 } else if (os_strcasecmp(cmd, "ampdu") == 0) {
373 if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
374 ret = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800375#ifdef CONFIG_TDLS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700376#ifdef CONFIG_TDLS_TESTING
377 } else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
378 extern unsigned int tdls_testing;
379 tdls_testing = strtol(value, NULL, 0);
380 wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
381#endif /* CONFIG_TDLS_TESTING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700382 } else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
383 int disabled = atoi(value);
384 wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
385 if (disabled) {
386 if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
387 ret = -1;
388 } else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
389 ret = -1;
390 wpa_tdls_enable(wpa_s->wpa, !disabled);
391#endif /* CONFIG_TDLS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800392 } else if (os_strcasecmp(cmd, "pno") == 0) {
393 if (atoi(value))
394 ret = pno_start(wpa_s);
395 else
396 ret = pno_stop(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700397 } else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
398 int disabled = atoi(value);
399 if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
400 ret = -1;
401 else if (disabled)
402 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
403 } else if (os_strcasecmp(cmd, "uapsd") == 0) {
404 if (os_strcmp(value, "disable") == 0)
405 wpa_s->set_sta_uapsd = 0;
406 else {
407 int be, bk, vi, vo;
408 char *pos;
409 /* format: BE,BK,VI,VO;max SP Length */
410 be = atoi(value);
411 pos = os_strchr(value, ',');
412 if (pos == NULL)
413 return -1;
414 pos++;
415 bk = atoi(pos);
416 pos = os_strchr(pos, ',');
417 if (pos == NULL)
418 return -1;
419 pos++;
420 vi = atoi(pos);
421 pos = os_strchr(pos, ',');
422 if (pos == NULL)
423 return -1;
424 pos++;
425 vo = atoi(pos);
426 /* ignore max SP Length for now */
427
428 wpa_s->set_sta_uapsd = 1;
429 wpa_s->sta_uapsd = 0;
430 if (be)
431 wpa_s->sta_uapsd |= BIT(0);
432 if (bk)
433 wpa_s->sta_uapsd |= BIT(1);
434 if (vi)
435 wpa_s->sta_uapsd |= BIT(2);
436 if (vo)
437 wpa_s->sta_uapsd |= BIT(3);
438 }
Jouni Malinen21d6bc82012-04-10 16:17:59 -0700439 } else if (os_strcasecmp(cmd, "ps") == 0) {
440 ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700441#ifdef CONFIG_WIFI_DISPLAY
442 } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
Dmitry Shmidted003d22014-02-06 10:09:12 -0800443 int enabled = !!atoi(value);
444 if (enabled && !wpa_s->global->p2p)
445 ret = -1;
446 else
447 wifi_display_enable(wpa_s->global, enabled);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700448#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700449 } else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
450 ret = set_bssid_filter(wpa_s, value);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800451 } else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
452 ret = set_disallow_aps(wpa_s, value);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800453 } else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
454 wpa_s->no_keep_alive = !!atoi(value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700455 } else {
456 value[-1] = '=';
457 ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
458 if (ret == 0)
459 wpa_supplicant_update_config(wpa_s);
460 }
461
462 return ret;
463}
464
465
466static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
467 char *cmd, char *buf, size_t buflen)
468{
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700469 int res = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700470
471 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
472
473 if (os_strcmp(cmd, "version") == 0) {
474 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700475 } else if (os_strcasecmp(cmd, "country") == 0) {
476 if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
477 res = os_snprintf(buf, buflen, "%c%c",
478 wpa_s->conf->country[0],
479 wpa_s->conf->country[1]);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700480#ifdef CONFIG_WIFI_DISPLAY
481 } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
Dmitry Shmidted003d22014-02-06 10:09:12 -0800482 int enabled;
483 if (wpa_s->global->p2p == NULL ||
484 wpa_s->global->p2p_disabled)
485 enabled = 0;
486 else
487 enabled = wpa_s->global->wifi_display;
488 res = os_snprintf(buf, buflen, "%d", enabled);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700489 if (res < 0 || (unsigned int) res >= buflen)
490 return -1;
491 return res;
492#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700493#ifdef CONFIG_TESTING_GET_GTK
494 } else if (os_strcmp(cmd, "gtk") == 0) {
495 if (wpa_s->last_gtk_len == 0)
496 return -1;
497 res = wpa_snprintf_hex(buf, buflen, wpa_s->last_gtk,
498 wpa_s->last_gtk_len);
499 return res;
500#endif /* CONFIG_TESTING_GET_GTK */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700501 }
502
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700503 if (res < 0 || (unsigned int) res >= buflen)
504 return -1;
505 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700506}
507
508
509#ifdef IEEE8021X_EAPOL
510static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
511 char *addr)
512{
513 u8 bssid[ETH_ALEN];
514 struct wpa_ssid *ssid = wpa_s->current_ssid;
515
516 if (hwaddr_aton(addr, bssid)) {
517 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
518 "'%s'", addr);
519 return -1;
520 }
521
522 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
523 rsn_preauth_deinit(wpa_s->wpa);
524 if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
525 return -1;
526
527 return 0;
528}
529#endif /* IEEE8021X_EAPOL */
530
531
532#ifdef CONFIG_PEERKEY
533/* MLME-STKSTART.request(peer) */
534static int wpa_supplicant_ctrl_iface_stkstart(
535 struct wpa_supplicant *wpa_s, char *addr)
536{
537 u8 peer[ETH_ALEN];
538
539 if (hwaddr_aton(addr, peer)) {
540 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
541 "address '%s'", addr);
542 return -1;
543 }
544
545 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
546 MAC2STR(peer));
547
548 return wpa_sm_stkstart(wpa_s->wpa, peer);
549}
550#endif /* CONFIG_PEERKEY */
551
552
553#ifdef CONFIG_TDLS
554
555static int wpa_supplicant_ctrl_iface_tdls_discover(
556 struct wpa_supplicant *wpa_s, char *addr)
557{
558 u8 peer[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800559 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700560
561 if (hwaddr_aton(addr, peer)) {
562 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
563 "address '%s'", addr);
564 return -1;
565 }
566
567 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
568 MAC2STR(peer));
569
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800570 if (wpa_tdls_is_external_setup(wpa_s->wpa))
571 ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
572 else
573 ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
574
575 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700576}
577
578
579static int wpa_supplicant_ctrl_iface_tdls_setup(
580 struct wpa_supplicant *wpa_s, char *addr)
581{
582 u8 peer[ETH_ALEN];
583 int ret;
584
585 if (hwaddr_aton(addr, peer)) {
586 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
587 "address '%s'", addr);
588 return -1;
589 }
590
591 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
592 MAC2STR(peer));
593
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800594 if ((wpa_s->conf->tdls_external_control) &&
595 wpa_tdls_is_external_setup(wpa_s->wpa))
596 return wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
597
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800598 wpa_tdls_remove(wpa_s->wpa, peer);
599
600 if (wpa_tdls_is_external_setup(wpa_s->wpa))
601 ret = wpa_tdls_start(wpa_s->wpa, peer);
602 else
603 ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800604
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700605 return ret;
606}
607
608
609static int wpa_supplicant_ctrl_iface_tdls_teardown(
610 struct wpa_supplicant *wpa_s, char *addr)
611{
612 u8 peer[ETH_ALEN];
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700613 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700614
615 if (hwaddr_aton(addr, peer)) {
616 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
617 "address '%s'", addr);
618 return -1;
619 }
620
621 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
622 MAC2STR(peer));
623
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800624 if ((wpa_s->conf->tdls_external_control) &&
625 wpa_tdls_is_external_setup(wpa_s->wpa))
626 return wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
627
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700628 if (wpa_tdls_is_external_setup(wpa_s->wpa))
629 ret = wpa_tdls_teardown_link(
630 wpa_s->wpa, peer,
631 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
632 else
633 ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
634
635 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700636}
637
638#endif /* CONFIG_TDLS */
639
640
641#ifdef CONFIG_IEEE80211R
642static int wpa_supplicant_ctrl_iface_ft_ds(
643 struct wpa_supplicant *wpa_s, char *addr)
644{
645 u8 target_ap[ETH_ALEN];
646 struct wpa_bss *bss;
647 const u8 *mdie;
648
649 if (hwaddr_aton(addr, target_ap)) {
650 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
651 "address '%s'", addr);
652 return -1;
653 }
654
655 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
656
657 bss = wpa_bss_get_bssid(wpa_s, target_ap);
658 if (bss)
659 mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
660 else
661 mdie = NULL;
662
663 return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
664}
665#endif /* CONFIG_IEEE80211R */
666
667
668#ifdef CONFIG_WPS
669static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
670 char *cmd)
671{
672 u8 bssid[ETH_ALEN], *_bssid = bssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700673#ifdef CONFIG_P2P
674 u8 p2p_dev_addr[ETH_ALEN];
675#endif /* CONFIG_P2P */
676#ifdef CONFIG_AP
677 u8 *_p2p_dev_addr = NULL;
678#endif /* CONFIG_AP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800679
Dmitry Shmidtc0a8db02013-11-08 11:18:33 -0800680 if (cmd == NULL || os_strcmp(cmd, "any") == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700681 _bssid = NULL;
682#ifdef CONFIG_P2P
683 } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
684 if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
685 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
686 "P2P Device Address '%s'",
687 cmd + 13);
688 return -1;
689 }
690 _p2p_dev_addr = p2p_dev_addr;
691#endif /* CONFIG_P2P */
692 } else if (hwaddr_aton(cmd, bssid)) {
693 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
694 cmd);
695 return -1;
696 }
697
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800698#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700699 if (wpa_s->ap_iface)
700 return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
701#endif /* CONFIG_AP */
702
703 return wpas_wps_start_pbc(wpa_s, _bssid, 0);
704}
705
706
707static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
708 char *cmd, char *buf,
709 size_t buflen)
710{
711 u8 bssid[ETH_ALEN], *_bssid = bssid;
712 char *pin;
713 int ret;
714
715 pin = os_strchr(cmd, ' ');
716 if (pin)
717 *pin++ = '\0';
718
719 if (os_strcmp(cmd, "any") == 0)
720 _bssid = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800721 else if (os_strcmp(cmd, "get") == 0) {
722 ret = wps_generate_pin();
723 goto done;
724 } else if (hwaddr_aton(cmd, bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700725 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
726 cmd);
727 return -1;
728 }
729
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800730#ifdef CONFIG_AP
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800731 if (wpa_s->ap_iface) {
732 int timeout = 0;
733 char *pos;
734
735 if (pin) {
736 pos = os_strchr(pin, ' ');
737 if (pos) {
738 *pos++ = '\0';
739 timeout = atoi(pos);
740 }
741 }
742
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700743 return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800744 buf, buflen, timeout);
745 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700746#endif /* CONFIG_AP */
747
748 if (pin) {
749 ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
750 DEV_PW_DEFAULT);
751 if (ret < 0)
752 return -1;
753 ret = os_snprintf(buf, buflen, "%s", pin);
754 if (ret < 0 || (size_t) ret >= buflen)
755 return -1;
756 return ret;
757 }
758
759 ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
760 if (ret < 0)
761 return -1;
762
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800763done:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700764 /* Return the generated PIN */
765 ret = os_snprintf(buf, buflen, "%08d", ret);
766 if (ret < 0 || (size_t) ret >= buflen)
767 return -1;
768 return ret;
769}
770
771
772static int wpa_supplicant_ctrl_iface_wps_check_pin(
773 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
774{
775 char pin[9];
776 size_t len;
777 char *pos;
778 int ret;
779
780 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
781 (u8 *) cmd, os_strlen(cmd));
782 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
783 if (*pos < '0' || *pos > '9')
784 continue;
785 pin[len++] = *pos;
786 if (len == 9) {
787 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
788 return -1;
789 }
790 }
791 if (len != 4 && len != 8) {
792 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
793 return -1;
794 }
795 pin[len] = '\0';
796
797 if (len == 8) {
798 unsigned int pin_val;
799 pin_val = atoi(pin);
800 if (!wps_pin_valid(pin_val)) {
801 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
802 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
803 if (ret < 0 || (size_t) ret >= buflen)
804 return -1;
805 return ret;
806 }
807 }
808
809 ret = os_snprintf(buf, buflen, "%s", pin);
810 if (ret < 0 || (size_t) ret >= buflen)
811 return -1;
812
813 return ret;
814}
815
816
Dmitry Shmidt04949592012-07-19 12:16:46 -0700817#ifdef CONFIG_WPS_NFC
818
819static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
820 char *cmd)
821{
822 u8 bssid[ETH_ALEN], *_bssid = bssid;
823
824 if (cmd == NULL || cmd[0] == '\0')
825 _bssid = NULL;
826 else if (hwaddr_aton(cmd, bssid))
827 return -1;
828
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800829 return wpas_wps_start_nfc(wpa_s, NULL, _bssid, NULL, 0, 0, NULL, NULL,
830 0, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700831}
832
833
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800834static int wpa_supplicant_ctrl_iface_wps_nfc_config_token(
835 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
836{
837 int ndef;
838 struct wpabuf *buf;
839 int res;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700840 char *pos;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800841
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700842 pos = os_strchr(cmd, ' ');
843 if (pos)
844 *pos++ = '\0';
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800845 if (os_strcmp(cmd, "WPS") == 0)
846 ndef = 0;
847 else if (os_strcmp(cmd, "NDEF") == 0)
848 ndef = 1;
849 else
850 return -1;
851
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700852 buf = wpas_wps_nfc_config_token(wpa_s, ndef, pos);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800853 if (buf == NULL)
854 return -1;
855
856 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
857 wpabuf_len(buf));
858 reply[res++] = '\n';
859 reply[res] = '\0';
860
861 wpabuf_free(buf);
862
863 return res;
864}
865
866
Dmitry Shmidt04949592012-07-19 12:16:46 -0700867static int wpa_supplicant_ctrl_iface_wps_nfc_token(
868 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
869{
870 int ndef;
871 struct wpabuf *buf;
872 int res;
873
874 if (os_strcmp(cmd, "WPS") == 0)
875 ndef = 0;
876 else if (os_strcmp(cmd, "NDEF") == 0)
877 ndef = 1;
878 else
879 return -1;
880
881 buf = wpas_wps_nfc_token(wpa_s, ndef);
882 if (buf == NULL)
883 return -1;
884
885 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
886 wpabuf_len(buf));
887 reply[res++] = '\n';
888 reply[res] = '\0';
889
890 wpabuf_free(buf);
891
892 return res;
893}
894
895
896static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
897 struct wpa_supplicant *wpa_s, char *pos)
898{
899 size_t len;
900 struct wpabuf *buf;
901 int ret;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800902 char *freq;
903 int forced_freq = 0;
904
905 freq = strstr(pos, " freq=");
906 if (freq) {
907 *freq = '\0';
908 freq += 6;
909 forced_freq = atoi(freq);
910 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700911
912 len = os_strlen(pos);
913 if (len & 0x01)
914 return -1;
915 len /= 2;
916
917 buf = wpabuf_alloc(len);
918 if (buf == NULL)
919 return -1;
920 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
921 wpabuf_free(buf);
922 return -1;
923 }
924
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800925 ret = wpas_wps_nfc_tag_read(wpa_s, buf, forced_freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700926 wpabuf_free(buf);
927
928 return ret;
929}
930
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800931
932static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800933 char *reply, size_t max_len,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800934 int ndef)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800935{
936 struct wpabuf *buf;
937 int res;
938
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800939 buf = wpas_wps_nfc_handover_req(wpa_s, ndef);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800940 if (buf == NULL)
941 return -1;
942
943 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
944 wpabuf_len(buf));
945 reply[res++] = '\n';
946 reply[res] = '\0';
947
948 wpabuf_free(buf);
949
950 return res;
951}
952
953
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800954#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800955static int wpas_ctrl_nfc_get_handover_req_p2p(struct wpa_supplicant *wpa_s,
956 char *reply, size_t max_len,
957 int ndef)
958{
959 struct wpabuf *buf;
960 int res;
961
962 buf = wpas_p2p_nfc_handover_req(wpa_s, ndef);
963 if (buf == NULL) {
964 wpa_printf(MSG_DEBUG, "P2P: Could not generate NFC handover request");
965 return -1;
966 }
967
968 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
969 wpabuf_len(buf));
970 reply[res++] = '\n';
971 reply[res] = '\0';
972
973 wpabuf_free(buf);
974
975 return res;
976}
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800977#endif /* CONFIG_P2P */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800978
979
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800980static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
981 char *cmd, char *reply,
982 size_t max_len)
983{
984 char *pos;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800985 int ndef;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800986
987 pos = os_strchr(cmd, ' ');
988 if (pos == NULL)
989 return -1;
990 *pos++ = '\0';
991
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800992 if (os_strcmp(cmd, "WPS") == 0)
993 ndef = 0;
994 else if (os_strcmp(cmd, "NDEF") == 0)
995 ndef = 1;
996 else
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800997 return -1;
998
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800999 if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001000 if (!ndef)
1001 return -1;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001002 return wpas_ctrl_nfc_get_handover_req_wps(
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001003 wpa_s, reply, max_len, ndef);
1004 }
1005
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001006#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001007 if (os_strcmp(pos, "P2P-CR") == 0) {
1008 return wpas_ctrl_nfc_get_handover_req_p2p(
1009 wpa_s, reply, max_len, ndef);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001010 }
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001011#endif /* CONFIG_P2P */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001012
1013 return -1;
1014}
1015
1016
1017static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001018 char *reply, size_t max_len,
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001019 int ndef, int cr, char *uuid)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001020{
1021 struct wpabuf *buf;
1022 int res;
1023
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001024 buf = wpas_wps_nfc_handover_sel(wpa_s, ndef, cr, uuid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001025 if (buf == NULL)
1026 return -1;
1027
1028 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1029 wpabuf_len(buf));
1030 reply[res++] = '\n';
1031 reply[res] = '\0';
1032
1033 wpabuf_free(buf);
1034
1035 return res;
1036}
1037
1038
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001039#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001040static int wpas_ctrl_nfc_get_handover_sel_p2p(struct wpa_supplicant *wpa_s,
1041 char *reply, size_t max_len,
1042 int ndef, int tag)
1043{
1044 struct wpabuf *buf;
1045 int res;
1046
1047 buf = wpas_p2p_nfc_handover_sel(wpa_s, ndef, tag);
1048 if (buf == NULL)
1049 return -1;
1050
1051 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1052 wpabuf_len(buf));
1053 reply[res++] = '\n';
1054 reply[res] = '\0';
1055
1056 wpabuf_free(buf);
1057
1058 return res;
1059}
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001060#endif /* CONFIG_P2P */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001061
1062
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001063static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
1064 char *cmd, char *reply,
1065 size_t max_len)
1066{
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001067 char *pos, *pos2;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001068 int ndef;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001069
1070 pos = os_strchr(cmd, ' ');
1071 if (pos == NULL)
1072 return -1;
1073 *pos++ = '\0';
1074
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001075 if (os_strcmp(cmd, "WPS") == 0)
1076 ndef = 0;
1077 else if (os_strcmp(cmd, "NDEF") == 0)
1078 ndef = 1;
1079 else
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001080 return -1;
1081
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001082 pos2 = os_strchr(pos, ' ');
1083 if (pos2)
1084 *pos2++ = '\0';
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001085 if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001086 if (!ndef)
1087 return -1;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001088 return wpas_ctrl_nfc_get_handover_sel_wps(
1089 wpa_s, reply, max_len, ndef,
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001090 os_strcmp(pos, "WPS-CR") == 0, pos2);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001091 }
1092
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001093#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001094 if (os_strcmp(pos, "P2P-CR") == 0) {
1095 return wpas_ctrl_nfc_get_handover_sel_p2p(
1096 wpa_s, reply, max_len, ndef, 0);
1097 }
1098
1099 if (os_strcmp(pos, "P2P-CR-TAG") == 0) {
1100 return wpas_ctrl_nfc_get_handover_sel_p2p(
1101 wpa_s, reply, max_len, ndef, 1);
1102 }
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001103#endif /* CONFIG_P2P */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001104
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001105 return -1;
1106}
1107
1108
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001109static int wpas_ctrl_nfc_rx_handover_sel(struct wpa_supplicant *wpa_s,
1110 char *cmd)
1111{
1112 size_t len;
1113 struct wpabuf *buf;
1114 int ret;
1115
1116 len = os_strlen(cmd);
1117 if (len & 0x01)
1118 return -1;
1119 len /= 2;
1120
1121 buf = wpabuf_alloc(len);
1122 if (buf == NULL)
1123 return -1;
1124 if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
1125 wpabuf_free(buf);
1126 return -1;
1127 }
1128
1129 ret = wpas_wps_nfc_rx_handover_sel(wpa_s, buf);
1130 wpabuf_free(buf);
1131
1132 return ret;
1133}
1134
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001135
1136static int wpas_ctrl_nfc_report_handover(struct wpa_supplicant *wpa_s,
1137 char *cmd)
1138{
1139 size_t len;
1140 struct wpabuf *req, *sel;
1141 int ret;
1142 char *pos, *role, *type, *pos2;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001143#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001144 char *freq;
1145 int forced_freq = 0;
1146
1147 freq = strstr(cmd, " freq=");
1148 if (freq) {
1149 *freq = '\0';
1150 freq += 6;
1151 forced_freq = atoi(freq);
1152 }
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001153#endif /* CONFIG_P2P */
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001154
1155 role = cmd;
1156 pos = os_strchr(role, ' ');
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001157 if (pos == NULL) {
1158 wpa_printf(MSG_DEBUG, "NFC: Missing type in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001159 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001160 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001161 *pos++ = '\0';
1162
1163 type = pos;
1164 pos = os_strchr(type, ' ');
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001165 if (pos == NULL) {
1166 wpa_printf(MSG_DEBUG, "NFC: Missing request message in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001167 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001168 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001169 *pos++ = '\0';
1170
1171 pos2 = os_strchr(pos, ' ');
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001172 if (pos2 == NULL) {
1173 wpa_printf(MSG_DEBUG, "NFC: Missing select message in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001174 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001175 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001176 *pos2++ = '\0';
1177
1178 len = os_strlen(pos);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001179 if (len & 0x01) {
1180 wpa_printf(MSG_DEBUG, "NFC: Invalid request message length in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001181 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001182 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001183 len /= 2;
1184
1185 req = wpabuf_alloc(len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001186 if (req == NULL) {
1187 wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for request message");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001188 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001189 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001190 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001191 wpa_printf(MSG_DEBUG, "NFC: Invalid request message hexdump in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001192 wpabuf_free(req);
1193 return -1;
1194 }
1195
1196 len = os_strlen(pos2);
1197 if (len & 0x01) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001198 wpa_printf(MSG_DEBUG, "NFC: Invalid select message length in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001199 wpabuf_free(req);
1200 return -1;
1201 }
1202 len /= 2;
1203
1204 sel = wpabuf_alloc(len);
1205 if (sel == NULL) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001206 wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for select message");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001207 wpabuf_free(req);
1208 return -1;
1209 }
1210 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001211 wpa_printf(MSG_DEBUG, "NFC: Invalid select message hexdump in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001212 wpabuf_free(req);
1213 wpabuf_free(sel);
1214 return -1;
1215 }
1216
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001217 wpa_printf(MSG_DEBUG, "NFC: Connection handover reported - role=%s type=%s req_len=%d sel_len=%d",
1218 role, type, (int) wpabuf_len(req), (int) wpabuf_len(sel));
1219
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001220 if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "WPS") == 0) {
1221 ret = wpas_wps_nfc_report_handover(wpa_s, req, sel);
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001222#ifdef CONFIG_AP
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001223 } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0)
1224 {
1225 ret = wpas_ap_wps_nfc_report_handover(wpa_s, req, sel);
1226 if (ret < 0)
1227 ret = wpas_er_wps_nfc_report_handover(wpa_s, req, sel);
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001228#endif /* CONFIG_AP */
1229#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001230 } else if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "P2P") == 0)
1231 {
1232 ret = wpas_p2p_nfc_report_handover(wpa_s, 1, req, sel, 0);
1233 } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "P2P") == 0)
1234 {
1235 ret = wpas_p2p_nfc_report_handover(wpa_s, 0, req, sel,
1236 forced_freq);
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001237#endif /* CONFIG_P2P */
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001238 } else {
1239 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
1240 "reported: role=%s type=%s", role, type);
1241 ret = -1;
1242 }
1243 wpabuf_free(req);
1244 wpabuf_free(sel);
1245
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001246 if (ret)
1247 wpa_printf(MSG_DEBUG, "NFC: Failed to process reported handover messages");
1248
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001249 return ret;
1250}
1251
Dmitry Shmidt04949592012-07-19 12:16:46 -07001252#endif /* CONFIG_WPS_NFC */
1253
1254
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001255static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
1256 char *cmd)
1257{
1258 u8 bssid[ETH_ALEN];
1259 char *pin;
1260 char *new_ssid;
1261 char *new_auth;
1262 char *new_encr;
1263 char *new_key;
1264 struct wps_new_ap_settings ap;
1265
1266 pin = os_strchr(cmd, ' ');
1267 if (pin == NULL)
1268 return -1;
1269 *pin++ = '\0';
1270
1271 if (hwaddr_aton(cmd, bssid)) {
1272 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
1273 cmd);
1274 return -1;
1275 }
1276
1277 new_ssid = os_strchr(pin, ' ');
1278 if (new_ssid == NULL)
1279 return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
1280 *new_ssid++ = '\0';
1281
1282 new_auth = os_strchr(new_ssid, ' ');
1283 if (new_auth == NULL)
1284 return -1;
1285 *new_auth++ = '\0';
1286
1287 new_encr = os_strchr(new_auth, ' ');
1288 if (new_encr == NULL)
1289 return -1;
1290 *new_encr++ = '\0';
1291
1292 new_key = os_strchr(new_encr, ' ');
1293 if (new_key == NULL)
1294 return -1;
1295 *new_key++ = '\0';
1296
1297 os_memset(&ap, 0, sizeof(ap));
1298 ap.ssid_hex = new_ssid;
1299 ap.auth = new_auth;
1300 ap.encr = new_encr;
1301 ap.key_hex = new_key;
1302 return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
1303}
1304
1305
1306#ifdef CONFIG_AP
1307static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
1308 char *cmd, char *buf,
1309 size_t buflen)
1310{
1311 int timeout = 300;
1312 char *pos;
1313 const char *pin_txt;
1314
1315 if (!wpa_s->ap_iface)
1316 return -1;
1317
1318 pos = os_strchr(cmd, ' ');
1319 if (pos)
1320 *pos++ = '\0';
1321
1322 if (os_strcmp(cmd, "disable") == 0) {
1323 wpas_wps_ap_pin_disable(wpa_s);
1324 return os_snprintf(buf, buflen, "OK\n");
1325 }
1326
1327 if (os_strcmp(cmd, "random") == 0) {
1328 if (pos)
1329 timeout = atoi(pos);
1330 pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
1331 if (pin_txt == NULL)
1332 return -1;
1333 return os_snprintf(buf, buflen, "%s", pin_txt);
1334 }
1335
1336 if (os_strcmp(cmd, "get") == 0) {
1337 pin_txt = wpas_wps_ap_pin_get(wpa_s);
1338 if (pin_txt == NULL)
1339 return -1;
1340 return os_snprintf(buf, buflen, "%s", pin_txt);
1341 }
1342
1343 if (os_strcmp(cmd, "set") == 0) {
1344 char *pin;
1345 if (pos == NULL)
1346 return -1;
1347 pin = pos;
1348 pos = os_strchr(pos, ' ');
1349 if (pos) {
1350 *pos++ = '\0';
1351 timeout = atoi(pos);
1352 }
1353 if (os_strlen(pin) > buflen)
1354 return -1;
1355 if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
1356 return -1;
1357 return os_snprintf(buf, buflen, "%s", pin);
1358 }
1359
1360 return -1;
1361}
1362#endif /* CONFIG_AP */
1363
1364
1365#ifdef CONFIG_WPS_ER
1366static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
1367 char *cmd)
1368{
1369 char *uuid = cmd, *pin, *pos;
1370 u8 addr_buf[ETH_ALEN], *addr = NULL;
1371 pin = os_strchr(uuid, ' ');
1372 if (pin == NULL)
1373 return -1;
1374 *pin++ = '\0';
1375 pos = os_strchr(pin, ' ');
1376 if (pos) {
1377 *pos++ = '\0';
1378 if (hwaddr_aton(pos, addr_buf) == 0)
1379 addr = addr_buf;
1380 }
1381 return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
1382}
1383
1384
1385static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
1386 char *cmd)
1387{
1388 char *uuid = cmd, *pin;
1389 pin = os_strchr(uuid, ' ');
1390 if (pin == NULL)
1391 return -1;
1392 *pin++ = '\0';
1393 return wpas_wps_er_learn(wpa_s, uuid, pin);
1394}
1395
1396
1397static int wpa_supplicant_ctrl_iface_wps_er_set_config(
1398 struct wpa_supplicant *wpa_s, char *cmd)
1399{
1400 char *uuid = cmd, *id;
1401 id = os_strchr(uuid, ' ');
1402 if (id == NULL)
1403 return -1;
1404 *id++ = '\0';
1405 return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
1406}
1407
1408
1409static int wpa_supplicant_ctrl_iface_wps_er_config(
1410 struct wpa_supplicant *wpa_s, char *cmd)
1411{
1412 char *pin;
1413 char *new_ssid;
1414 char *new_auth;
1415 char *new_encr;
1416 char *new_key;
1417 struct wps_new_ap_settings ap;
1418
1419 pin = os_strchr(cmd, ' ');
1420 if (pin == NULL)
1421 return -1;
1422 *pin++ = '\0';
1423
1424 new_ssid = os_strchr(pin, ' ');
1425 if (new_ssid == NULL)
1426 return -1;
1427 *new_ssid++ = '\0';
1428
1429 new_auth = os_strchr(new_ssid, ' ');
1430 if (new_auth == NULL)
1431 return -1;
1432 *new_auth++ = '\0';
1433
1434 new_encr = os_strchr(new_auth, ' ');
1435 if (new_encr == NULL)
1436 return -1;
1437 *new_encr++ = '\0';
1438
1439 new_key = os_strchr(new_encr, ' ');
1440 if (new_key == NULL)
1441 return -1;
1442 *new_key++ = '\0';
1443
1444 os_memset(&ap, 0, sizeof(ap));
1445 ap.ssid_hex = new_ssid;
1446 ap.auth = new_auth;
1447 ap.encr = new_encr;
1448 ap.key_hex = new_key;
1449 return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
1450}
Dmitry Shmidt04949592012-07-19 12:16:46 -07001451
1452
1453#ifdef CONFIG_WPS_NFC
1454static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
1455 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1456{
1457 int ndef;
1458 struct wpabuf *buf;
1459 int res;
1460 char *uuid;
1461
1462 uuid = os_strchr(cmd, ' ');
1463 if (uuid == NULL)
1464 return -1;
1465 *uuid++ = '\0';
1466
1467 if (os_strcmp(cmd, "WPS") == 0)
1468 ndef = 0;
1469 else if (os_strcmp(cmd, "NDEF") == 0)
1470 ndef = 1;
1471 else
1472 return -1;
1473
1474 buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
1475 if (buf == NULL)
1476 return -1;
1477
1478 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1479 wpabuf_len(buf));
1480 reply[res++] = '\n';
1481 reply[res] = '\0';
1482
1483 wpabuf_free(buf);
1484
1485 return res;
1486}
1487#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001488#endif /* CONFIG_WPS_ER */
1489
1490#endif /* CONFIG_WPS */
1491
1492
1493#ifdef CONFIG_IBSS_RSN
1494static int wpa_supplicant_ctrl_iface_ibss_rsn(
1495 struct wpa_supplicant *wpa_s, char *addr)
1496{
1497 u8 peer[ETH_ALEN];
1498
1499 if (hwaddr_aton(addr, peer)) {
1500 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
1501 "address '%s'", addr);
1502 return -1;
1503 }
1504
1505 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
1506 MAC2STR(peer));
1507
1508 return ibss_rsn_start(wpa_s->ibss_rsn, peer);
1509}
1510#endif /* CONFIG_IBSS_RSN */
1511
1512
1513static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
1514 char *rsp)
1515{
1516#ifdef IEEE8021X_EAPOL
1517 char *pos, *id_pos;
1518 int id;
1519 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001520
1521 pos = os_strchr(rsp, '-');
1522 if (pos == NULL)
1523 return -1;
1524 *pos++ = '\0';
1525 id_pos = pos;
1526 pos = os_strchr(pos, ':');
1527 if (pos == NULL)
1528 return -1;
1529 *pos++ = '\0';
1530 id = atoi(id_pos);
1531 wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
1532 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
1533 (u8 *) pos, os_strlen(pos));
1534
1535 ssid = wpa_config_get_network(wpa_s->conf, id);
1536 if (ssid == NULL) {
1537 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
1538 "to update", id);
1539 return -1;
1540 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001541
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001542 return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
1543 pos);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001544#else /* IEEE8021X_EAPOL */
1545 wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
1546 return -1;
1547#endif /* IEEE8021X_EAPOL */
1548}
1549
1550
1551static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
1552 const char *params,
1553 char *buf, size_t buflen)
1554{
1555 char *pos, *end, tmp[30];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001556 int res, verbose, wps, ret;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001557#ifdef CONFIG_HS20
1558 const u8 *hs20;
1559#endif /* CONFIG_HS20 */
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001560
Dmitry Shmidt56052862013-10-04 10:23:25 -07001561 if (os_strcmp(params, "-DRIVER") == 0)
1562 return wpa_drv_status(wpa_s, buf, buflen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001563 verbose = os_strcmp(params, "-VERBOSE") == 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001564 wps = os_strcmp(params, "-WPS") == 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001565 pos = buf;
1566 end = buf + buflen;
1567 if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1568 struct wpa_ssid *ssid = wpa_s->current_ssid;
1569 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
1570 MAC2STR(wpa_s->bssid));
1571 if (ret < 0 || ret >= end - pos)
1572 return pos - buf;
1573 pos += ret;
1574 if (ssid) {
1575 u8 *_ssid = ssid->ssid;
1576 size_t ssid_len = ssid->ssid_len;
1577 u8 ssid_buf[MAX_SSID_LEN];
1578 if (ssid_len == 0) {
1579 int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
1580 if (_res < 0)
1581 ssid_len = 0;
1582 else
1583 ssid_len = _res;
1584 _ssid = ssid_buf;
1585 }
1586 ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
1587 wpa_ssid_txt(_ssid, ssid_len),
1588 ssid->id);
1589 if (ret < 0 || ret >= end - pos)
1590 return pos - buf;
1591 pos += ret;
1592
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001593 if (wps && ssid->passphrase &&
1594 wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
1595 (ssid->mode == WPAS_MODE_AP ||
1596 ssid->mode == WPAS_MODE_P2P_GO)) {
1597 ret = os_snprintf(pos, end - pos,
1598 "passphrase=%s\n",
1599 ssid->passphrase);
1600 if (ret < 0 || ret >= end - pos)
1601 return pos - buf;
1602 pos += ret;
1603 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001604 if (ssid->id_str) {
1605 ret = os_snprintf(pos, end - pos,
1606 "id_str=%s\n",
1607 ssid->id_str);
1608 if (ret < 0 || ret >= end - pos)
1609 return pos - buf;
1610 pos += ret;
1611 }
1612
1613 switch (ssid->mode) {
1614 case WPAS_MODE_INFRA:
1615 ret = os_snprintf(pos, end - pos,
1616 "mode=station\n");
1617 break;
1618 case WPAS_MODE_IBSS:
1619 ret = os_snprintf(pos, end - pos,
1620 "mode=IBSS\n");
1621 break;
1622 case WPAS_MODE_AP:
1623 ret = os_snprintf(pos, end - pos,
1624 "mode=AP\n");
1625 break;
1626 case WPAS_MODE_P2P_GO:
1627 ret = os_snprintf(pos, end - pos,
1628 "mode=P2P GO\n");
1629 break;
1630 case WPAS_MODE_P2P_GROUP_FORMATION:
1631 ret = os_snprintf(pos, end - pos,
1632 "mode=P2P GO - group "
1633 "formation\n");
1634 break;
1635 default:
1636 ret = 0;
1637 break;
1638 }
1639 if (ret < 0 || ret >= end - pos)
1640 return pos - buf;
1641 pos += ret;
1642 }
1643
1644#ifdef CONFIG_AP
1645 if (wpa_s->ap_iface) {
1646 pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
1647 end - pos,
1648 verbose);
1649 } else
1650#endif /* CONFIG_AP */
1651 pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
1652 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001653#ifdef CONFIG_SAE
1654 if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001655#ifdef CONFIG_AP
1656 !wpa_s->ap_iface &&
1657#endif /* CONFIG_AP */
1658 wpa_s->sme.sae.state == SAE_ACCEPTED) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001659 ret = os_snprintf(pos, end - pos, "sae_group=%d\n",
1660 wpa_s->sme.sae.group);
1661 if (ret < 0 || ret >= end - pos)
1662 return pos - buf;
1663 pos += ret;
1664 }
1665#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001666 ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
1667 wpa_supplicant_state_txt(wpa_s->wpa_state));
1668 if (ret < 0 || ret >= end - pos)
1669 return pos - buf;
1670 pos += ret;
1671
1672 if (wpa_s->l2 &&
1673 l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
1674 ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
1675 if (ret < 0 || ret >= end - pos)
1676 return pos - buf;
1677 pos += ret;
1678 }
1679
1680#ifdef CONFIG_P2P
1681 if (wpa_s->global->p2p) {
1682 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
1683 "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
1684 if (ret < 0 || ret >= end - pos)
1685 return pos - buf;
1686 pos += ret;
1687 }
1688#endif /* CONFIG_P2P */
1689
1690 ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
1691 MAC2STR(wpa_s->own_addr));
1692 if (ret < 0 || ret >= end - pos)
1693 return pos - buf;
1694 pos += ret;
1695
Dmitry Shmidt04949592012-07-19 12:16:46 -07001696#ifdef CONFIG_HS20
1697 if (wpa_s->current_bss &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001698 (hs20 = wpa_bss_get_vendor_ie(wpa_s->current_bss,
1699 HS20_IE_VENDOR_TYPE)) &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001700 wpa_s->wpa_proto == WPA_PROTO_RSN &&
1701 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001702 int release = 1;
1703 if (hs20[1] >= 5) {
1704 u8 rel_num = (hs20[6] & 0xf0) >> 4;
1705 release = rel_num + 1;
1706 }
1707 ret = os_snprintf(pos, end - pos, "hs20=%d\n", release);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001708 if (ret < 0 || ret >= end - pos)
1709 return pos - buf;
1710 pos += ret;
1711 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001712
1713 if (wpa_s->current_ssid) {
1714 struct wpa_cred *cred;
1715 char *type;
1716
1717 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001718 size_t i;
1719
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001720 if (wpa_s->current_ssid->parent_cred != cred)
1721 continue;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001722
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001723 if (cred->provisioning_sp) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001724 ret = os_snprintf(pos, end - pos,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001725 "provisioning_sp=%s\n",
1726 cred->provisioning_sp);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001727 if (ret < 0 || ret >= end - pos)
1728 return pos - buf;
1729 pos += ret;
1730 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001731
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001732 if (!cred->domain)
1733 goto no_domain;
1734
1735 i = 0;
1736 if (wpa_s->current_bss && wpa_s->current_bss->anqp) {
1737 struct wpabuf *names =
1738 wpa_s->current_bss->anqp->domain_name;
1739 for (i = 0; names && i < cred->num_domain; i++)
1740 {
1741 if (domain_name_list_contains(
1742 names, cred->domain[i], 1))
1743 break;
1744 }
1745 if (i == cred->num_domain)
1746 i = 0; /* show first entry by default */
1747 }
1748 ret = os_snprintf(pos, end - pos, "home_sp=%s\n",
1749 cred->domain[i]);
1750 if (ret < 0 || ret >= end - pos)
1751 return pos - buf;
1752 pos += ret;
1753
1754 no_domain:
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001755 if (wpa_s->current_bss == NULL ||
1756 wpa_s->current_bss->anqp == NULL)
1757 res = -1;
1758 else
1759 res = interworking_home_sp_cred(
1760 wpa_s, cred,
1761 wpa_s->current_bss->anqp->domain_name);
1762 if (res > 0)
1763 type = "home";
1764 else if (res == 0)
1765 type = "roaming";
1766 else
1767 type = "unknown";
1768
1769 ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
1770 if (ret < 0 || ret >= end - pos)
1771 return pos - buf;
1772 pos += ret;
1773
1774 break;
1775 }
1776 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001777#endif /* CONFIG_HS20 */
1778
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001779 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
1780 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1781 res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
1782 verbose);
1783 if (res >= 0)
1784 pos += res;
1785 }
1786
1787 res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
1788 if (res >= 0)
1789 pos += res;
1790
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001791#ifdef CONFIG_WPS
1792 {
1793 char uuid_str[100];
1794 uuid_bin2str(wpa_s->wps->uuid, uuid_str, sizeof(uuid_str));
1795 ret = os_snprintf(pos, end - pos, "uuid=%s\n", uuid_str);
1796 if (ret < 0 || ret >= end - pos)
1797 return pos - buf;
1798 pos += ret;
1799 }
1800#endif /* CONFIG_WPS */
1801
Dmitry Shmidt2fd7fa62011-12-19 11:19:09 -08001802#ifdef ANDROID
1803 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
Irfan Sherifff20a4432012-04-16 16:48:34 -07001804 "id=%d state=%d BSSID=" MACSTR " SSID=%s",
Dmitry Shmidt2fd7fa62011-12-19 11:19:09 -08001805 wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
Irfan Sherifff20a4432012-04-16 16:48:34 -07001806 wpa_s->wpa_state,
Irfan Sheriffe78e7672012-08-01 11:10:15 -07001807 MAC2STR(wpa_s->bssid),
andy2_kuo5b5fb022012-05-22 11:53:07 -07001808 wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
1809 wpa_ssid_txt(wpa_s->current_ssid->ssid,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001810 wpa_s->current_ssid->ssid_len) : "");
Irfan Sheriffbf5edf42012-01-11 16:54:57 -08001811 if (wpa_s->wpa_state == WPA_COMPLETED) {
1812 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001813 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED
1814 "- connection to " MACSTR
1815 " completed %s [id=%d id_str=%s]",
1816 MAC2STR(wpa_s->bssid), "(auth)",
1817 ssid ? ssid->id : -1,
1818 ssid && ssid->id_str ? ssid->id_str : "");
Irfan Sheriffbf5edf42012-01-11 16:54:57 -08001819 }
Dmitry Shmidt2fd7fa62011-12-19 11:19:09 -08001820#endif /* ANDROID */
1821
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001822 return pos - buf;
1823}
1824
1825
1826static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
1827 char *cmd)
1828{
1829 char *pos;
1830 int id;
1831 struct wpa_ssid *ssid;
1832 u8 bssid[ETH_ALEN];
1833
1834 /* cmd: "<network id> <BSSID>" */
1835 pos = os_strchr(cmd, ' ');
1836 if (pos == NULL)
1837 return -1;
1838 *pos++ = '\0';
1839 id = atoi(cmd);
1840 wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
1841 if (hwaddr_aton(pos, bssid)) {
1842 wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
1843 return -1;
1844 }
1845
1846 ssid = wpa_config_get_network(wpa_s->conf, id);
1847 if (ssid == NULL) {
1848 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
1849 "to update", id);
1850 return -1;
1851 }
1852
1853 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
1854 ssid->bssid_set = !is_zero_ether_addr(bssid);
1855
1856 return 0;
1857}
1858
1859
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001860static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001861 char *cmd, char *buf,
1862 size_t buflen)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001863{
1864 u8 bssid[ETH_ALEN];
1865 struct wpa_blacklist *e;
1866 char *pos, *end;
1867 int ret;
1868
1869 /* cmd: "BLACKLIST [<BSSID>]" */
1870 if (*cmd == '\0') {
1871 pos = buf;
1872 end = buf + buflen;
1873 e = wpa_s->blacklist;
1874 while (e) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001875 ret = os_snprintf(pos, end - pos, MACSTR "\n",
1876 MAC2STR(e->bssid));
1877 if (ret < 0 || ret >= end - pos)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001878 return pos - buf;
1879 pos += ret;
1880 e = e->next;
1881 }
1882 return pos - buf;
1883 }
1884
1885 cmd++;
1886 if (os_strncmp(cmd, "clear", 5) == 0) {
1887 wpa_blacklist_clear(wpa_s);
1888 os_memcpy(buf, "OK\n", 3);
1889 return 3;
1890 }
1891
1892 wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
1893 if (hwaddr_aton(cmd, bssid)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001894 wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001895 return -1;
1896 }
1897
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001898 /*
1899 * Add the BSSID twice, so its count will be 2, causing it to be
1900 * skipped when processing scan results.
1901 */
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001902 ret = wpa_blacklist_add(wpa_s, bssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001903 if (ret != 0)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001904 return -1;
1905 ret = wpa_blacklist_add(wpa_s, bssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001906 if (ret != 0)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001907 return -1;
1908 os_memcpy(buf, "OK\n", 3);
1909 return 3;
1910}
1911
1912
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001913static const char * debug_level_str(int level)
1914{
1915 switch (level) {
1916 case MSG_EXCESSIVE:
1917 return "EXCESSIVE";
1918 case MSG_MSGDUMP:
1919 return "MSGDUMP";
1920 case MSG_DEBUG:
1921 return "DEBUG";
1922 case MSG_INFO:
1923 return "INFO";
1924 case MSG_WARNING:
1925 return "WARNING";
1926 case MSG_ERROR:
1927 return "ERROR";
1928 default:
1929 return "?";
1930 }
1931}
1932
1933
1934static int str_to_debug_level(const char *s)
1935{
1936 if (os_strcasecmp(s, "EXCESSIVE") == 0)
1937 return MSG_EXCESSIVE;
1938 if (os_strcasecmp(s, "MSGDUMP") == 0)
1939 return MSG_MSGDUMP;
1940 if (os_strcasecmp(s, "DEBUG") == 0)
1941 return MSG_DEBUG;
1942 if (os_strcasecmp(s, "INFO") == 0)
1943 return MSG_INFO;
1944 if (os_strcasecmp(s, "WARNING") == 0)
1945 return MSG_WARNING;
1946 if (os_strcasecmp(s, "ERROR") == 0)
1947 return MSG_ERROR;
1948 return -1;
1949}
1950
1951
1952static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
1953 char *cmd, char *buf,
1954 size_t buflen)
1955{
1956 char *pos, *end, *stamp;
1957 int ret;
1958
1959 if (cmd == NULL) {
1960 return -1;
1961 }
1962
1963 /* cmd: "LOG_LEVEL [<level>]" */
1964 if (*cmd == '\0') {
1965 pos = buf;
1966 end = buf + buflen;
1967 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
1968 "Timestamp: %d\n",
1969 debug_level_str(wpa_debug_level),
1970 wpa_debug_timestamp);
1971 if (ret < 0 || ret >= end - pos)
1972 ret = 0;
1973
1974 return ret;
1975 }
1976
1977 while (*cmd == ' ')
1978 cmd++;
1979
1980 stamp = os_strchr(cmd, ' ');
1981 if (stamp) {
1982 *stamp++ = '\0';
1983 while (*stamp == ' ') {
1984 stamp++;
1985 }
1986 }
1987
1988 if (cmd && os_strlen(cmd)) {
1989 int level = str_to_debug_level(cmd);
1990 if (level < 0)
1991 return -1;
1992 wpa_debug_level = level;
1993 }
1994
1995 if (stamp && os_strlen(stamp))
1996 wpa_debug_timestamp = atoi(stamp);
1997
1998 os_memcpy(buf, "OK\n", 3);
1999 return 3;
2000}
2001
2002
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002003static int wpa_supplicant_ctrl_iface_list_networks(
2004 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
2005{
2006 char *pos, *end;
2007 struct wpa_ssid *ssid;
2008 int ret;
2009
2010 pos = buf;
2011 end = buf + buflen;
2012 ret = os_snprintf(pos, end - pos,
2013 "network id / ssid / bssid / flags\n");
2014 if (ret < 0 || ret >= end - pos)
2015 return pos - buf;
2016 pos += ret;
2017
2018 ssid = wpa_s->conf->ssid;
2019 while (ssid) {
2020 ret = os_snprintf(pos, end - pos, "%d\t%s",
2021 ssid->id,
2022 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
2023 if (ret < 0 || ret >= end - pos)
2024 return pos - buf;
2025 pos += ret;
2026 if (ssid->bssid_set) {
2027 ret = os_snprintf(pos, end - pos, "\t" MACSTR,
2028 MAC2STR(ssid->bssid));
2029 } else {
2030 ret = os_snprintf(pos, end - pos, "\tany");
2031 }
2032 if (ret < 0 || ret >= end - pos)
2033 return pos - buf;
2034 pos += ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002035 ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002036 ssid == wpa_s->current_ssid ?
2037 "[CURRENT]" : "",
2038 ssid->disabled ? "[DISABLED]" : "",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002039 ssid->disabled_until.sec ?
2040 "[TEMP-DISABLED]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002041 ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
2042 "");
2043 if (ret < 0 || ret >= end - pos)
2044 return pos - buf;
2045 pos += ret;
2046 ret = os_snprintf(pos, end - pos, "\n");
2047 if (ret < 0 || ret >= end - pos)
2048 return pos - buf;
2049 pos += ret;
2050
2051 ssid = ssid->next;
2052 }
2053
2054 return pos - buf;
2055}
2056
2057
2058static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
2059{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002060 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002061 ret = os_snprintf(pos, end - pos, "-");
2062 if (ret < 0 || ret >= end - pos)
2063 return pos;
2064 pos += ret;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002065 ret = wpa_write_ciphers(pos, end, cipher, "+");
2066 if (ret < 0)
2067 return pos;
2068 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002069 return pos;
2070}
2071
2072
2073static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
2074 const u8 *ie, size_t ie_len)
2075{
2076 struct wpa_ie_data data;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002077 char *start;
2078 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002079
2080 ret = os_snprintf(pos, end - pos, "[%s-", proto);
2081 if (ret < 0 || ret >= end - pos)
2082 return pos;
2083 pos += ret;
2084
2085 if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
2086 ret = os_snprintf(pos, end - pos, "?]");
2087 if (ret < 0 || ret >= end - pos)
2088 return pos;
2089 pos += ret;
2090 return pos;
2091 }
2092
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002093 start = pos;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002094 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002095 ret = os_snprintf(pos, end - pos, "%sEAP",
2096 pos == start ? "" : "+");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002097 if (ret < 0 || ret >= end - pos)
2098 return pos;
2099 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002100 }
2101 if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002102 ret = os_snprintf(pos, end - pos, "%sPSK",
2103 pos == start ? "" : "+");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002104 if (ret < 0 || ret >= end - pos)
2105 return pos;
2106 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002107 }
2108 if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002109 ret = os_snprintf(pos, end - pos, "%sNone",
2110 pos == start ? "" : "+");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002111 if (ret < 0 || ret >= end - pos)
2112 return pos;
2113 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002114 }
2115#ifdef CONFIG_IEEE80211R
2116 if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
2117 ret = os_snprintf(pos, end - pos, "%sFT/EAP",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002118 pos == start ? "" : "+");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002119 if (ret < 0 || ret >= end - pos)
2120 return pos;
2121 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002122 }
2123 if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
2124 ret = os_snprintf(pos, end - pos, "%sFT/PSK",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002125 pos == start ? "" : "+");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002126 if (ret < 0 || ret >= end - pos)
2127 return pos;
2128 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002129 }
2130#endif /* CONFIG_IEEE80211R */
2131#ifdef CONFIG_IEEE80211W
2132 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
2133 ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002134 pos == start ? "" : "+");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002135 if (ret < 0 || ret >= end - pos)
2136 return pos;
2137 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002138 }
2139 if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
2140 ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002141 pos == start ? "" : "+");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002142 if (ret < 0 || ret >= end - pos)
2143 return pos;
2144 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002145 }
2146#endif /* CONFIG_IEEE80211W */
2147
2148 pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
2149
2150 if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
2151 ret = os_snprintf(pos, end - pos, "-preauth");
2152 if (ret < 0 || ret >= end - pos)
2153 return pos;
2154 pos += ret;
2155 }
2156
2157 ret = os_snprintf(pos, end - pos, "]");
2158 if (ret < 0 || ret >= end - pos)
2159 return pos;
2160 pos += ret;
2161
2162 return pos;
2163}
2164
2165
2166#ifdef CONFIG_WPS
2167static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
2168 char *pos, char *end,
2169 struct wpabuf *wps_ie)
2170{
2171 int ret;
2172 const char *txt;
2173
2174 if (wps_ie == NULL)
2175 return pos;
2176 if (wps_is_selected_pbc_registrar(wps_ie))
2177 txt = "[WPS-PBC]";
2178#ifdef CONFIG_WPS2
2179 else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
2180 txt = "[WPS-AUTH]";
2181#endif /* CONFIG_WPS2 */
2182 else if (wps_is_selected_pin_registrar(wps_ie))
2183 txt = "[WPS-PIN]";
2184 else
2185 txt = "[WPS]";
2186
2187 ret = os_snprintf(pos, end - pos, "%s", txt);
2188 if (ret >= 0 && ret < end - pos)
2189 pos += ret;
2190 wpabuf_free(wps_ie);
2191 return pos;
2192}
2193#endif /* CONFIG_WPS */
2194
2195
2196static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
2197 char *pos, char *end,
2198 const struct wpa_bss *bss)
2199{
2200#ifdef CONFIG_WPS
2201 struct wpabuf *wps_ie;
2202 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
2203 return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
2204#else /* CONFIG_WPS */
2205 return pos;
2206#endif /* CONFIG_WPS */
2207}
2208
2209
2210/* Format one result on one text line into a buffer. */
2211static int wpa_supplicant_ctrl_iface_scan_result(
2212 struct wpa_supplicant *wpa_s,
2213 const struct wpa_bss *bss, char *buf, size_t buflen)
2214{
2215 char *pos, *end;
2216 int ret;
2217 const u8 *ie, *ie2, *p2p;
2218
2219 p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -07002220 if (!p2p)
2221 p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002222 if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
2223 os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
2224 0)
2225 return 0; /* Do not show P2P listen discovery results here */
2226
2227 pos = buf;
2228 end = buf + buflen;
2229
2230 ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
2231 MAC2STR(bss->bssid), bss->freq, bss->level);
2232 if (ret < 0 || ret >= end - pos)
2233 return -1;
2234 pos += ret;
2235 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
2236 if (ie)
2237 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
2238 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
2239 if (ie2)
2240 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
2241 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
2242 if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
2243 ret = os_snprintf(pos, end - pos, "[WEP]");
2244 if (ret < 0 || ret >= end - pos)
2245 return -1;
2246 pos += ret;
2247 }
2248 if (bss->caps & IEEE80211_CAP_IBSS) {
2249 ret = os_snprintf(pos, end - pos, "[IBSS]");
2250 if (ret < 0 || ret >= end - pos)
2251 return -1;
2252 pos += ret;
2253 }
2254 if (bss->caps & IEEE80211_CAP_ESS) {
2255 ret = os_snprintf(pos, end - pos, "[ESS]");
2256 if (ret < 0 || ret >= end - pos)
2257 return -1;
2258 pos += ret;
2259 }
2260 if (p2p) {
2261 ret = os_snprintf(pos, end - pos, "[P2P]");
2262 if (ret < 0 || ret >= end - pos)
2263 return -1;
2264 pos += ret;
2265 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002266#ifdef CONFIG_HS20
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002267 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002268 ret = os_snprintf(pos, end - pos, "[HS20]");
2269 if (ret < 0 || ret >= end - pos)
2270 return -1;
2271 pos += ret;
2272 }
2273#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002274
2275 ret = os_snprintf(pos, end - pos, "\t%s",
2276 wpa_ssid_txt(bss->ssid, bss->ssid_len));
2277 if (ret < 0 || ret >= end - pos)
2278 return -1;
2279 pos += ret;
2280
2281 ret = os_snprintf(pos, end - pos, "\n");
2282 if (ret < 0 || ret >= end - pos)
2283 return -1;
2284 pos += ret;
2285
2286 return pos - buf;
2287}
2288
2289
2290static int wpa_supplicant_ctrl_iface_scan_results(
2291 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
2292{
2293 char *pos, *end;
2294 struct wpa_bss *bss;
2295 int ret;
2296
2297 pos = buf;
2298 end = buf + buflen;
2299 ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
2300 "flags / ssid\n");
2301 if (ret < 0 || ret >= end - pos)
2302 return pos - buf;
2303 pos += ret;
2304
2305 dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
2306 ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
2307 end - pos);
2308 if (ret < 0 || ret >= end - pos)
2309 return pos - buf;
2310 pos += ret;
2311 }
2312
2313 return pos - buf;
2314}
2315
2316
2317static int wpa_supplicant_ctrl_iface_select_network(
2318 struct wpa_supplicant *wpa_s, char *cmd)
2319{
2320 int id;
2321 struct wpa_ssid *ssid;
2322
2323 /* cmd: "<network id>" or "any" */
2324 if (os_strcmp(cmd, "any") == 0) {
2325 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
2326 ssid = NULL;
2327 } else {
2328 id = atoi(cmd);
2329 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
2330
2331 ssid = wpa_config_get_network(wpa_s->conf, id);
2332 if (ssid == NULL) {
2333 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2334 "network id=%d", id);
2335 return -1;
2336 }
2337 if (ssid->disabled == 2) {
2338 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2339 "SELECT_NETWORK with persistent P2P group");
2340 return -1;
2341 }
2342 }
2343
2344 wpa_supplicant_select_network(wpa_s, ssid);
2345
2346 return 0;
2347}
2348
2349
2350static int wpa_supplicant_ctrl_iface_enable_network(
2351 struct wpa_supplicant *wpa_s, char *cmd)
2352{
2353 int id;
2354 struct wpa_ssid *ssid;
2355
2356 /* cmd: "<network id>" or "all" */
2357 if (os_strcmp(cmd, "all") == 0) {
2358 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
2359 ssid = NULL;
2360 } else {
2361 id = atoi(cmd);
2362 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
2363
2364 ssid = wpa_config_get_network(wpa_s->conf, id);
2365 if (ssid == NULL) {
2366 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2367 "network id=%d", id);
2368 return -1;
2369 }
2370 if (ssid->disabled == 2) {
2371 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2372 "ENABLE_NETWORK with persistent P2P group");
2373 return -1;
2374 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002375
2376 if (os_strstr(cmd, " no-connect")) {
2377 ssid->disabled = 0;
2378 return 0;
2379 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002380 }
2381 wpa_supplicant_enable_network(wpa_s, ssid);
2382
2383 return 0;
2384}
2385
2386
2387static int wpa_supplicant_ctrl_iface_disable_network(
2388 struct wpa_supplicant *wpa_s, char *cmd)
2389{
2390 int id;
2391 struct wpa_ssid *ssid;
2392
2393 /* cmd: "<network id>" or "all" */
2394 if (os_strcmp(cmd, "all") == 0) {
2395 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
2396 ssid = NULL;
2397 } else {
2398 id = atoi(cmd);
2399 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
2400
2401 ssid = wpa_config_get_network(wpa_s->conf, id);
2402 if (ssid == NULL) {
2403 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2404 "network id=%d", id);
2405 return -1;
2406 }
2407 if (ssid->disabled == 2) {
2408 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2409 "DISABLE_NETWORK with persistent P2P "
2410 "group");
2411 return -1;
2412 }
2413 }
2414 wpa_supplicant_disable_network(wpa_s, ssid);
2415
2416 return 0;
2417}
2418
2419
2420static int wpa_supplicant_ctrl_iface_add_network(
2421 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
2422{
2423 struct wpa_ssid *ssid;
2424 int ret;
2425
2426 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
2427
2428 ssid = wpa_config_add_network(wpa_s->conf);
2429 if (ssid == NULL)
2430 return -1;
2431
2432 wpas_notify_network_added(wpa_s, ssid);
2433
2434 ssid->disabled = 1;
2435 wpa_config_set_network_defaults(ssid);
2436
2437 ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
2438 if (ret < 0 || (size_t) ret >= buflen)
2439 return -1;
2440 return ret;
2441}
2442
2443
2444static int wpa_supplicant_ctrl_iface_remove_network(
2445 struct wpa_supplicant *wpa_s, char *cmd)
2446{
2447 int id;
2448 struct wpa_ssid *ssid;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002449 int was_disabled;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002450
2451 /* cmd: "<network id>" or "all" */
2452 if (os_strcmp(cmd, "all") == 0) {
2453 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002454 if (wpa_s->sched_scanning)
2455 wpa_supplicant_cancel_sched_scan(wpa_s);
2456
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002457 eapol_sm_invalidate_cached_session(wpa_s->eapol);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002458 if (wpa_s->current_ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002459#ifdef CONFIG_SME
2460 wpa_s->sme.prev_bssid_set = 0;
2461#endif /* CONFIG_SME */
Jouni Malinen75ecf522011-06-27 15:19:46 -07002462 wpa_sm_set_config(wpa_s->wpa, NULL);
2463 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002464 wpa_supplicant_deauthenticate(
2465 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002466 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002467 ssid = wpa_s->conf->ssid;
2468 while (ssid) {
2469 struct wpa_ssid *remove_ssid = ssid;
2470 id = ssid->id;
2471 ssid = ssid->next;
2472 wpas_notify_network_removed(wpa_s, remove_ssid);
2473 wpa_config_remove_network(wpa_s->conf, id);
2474 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002475 return 0;
2476 }
2477
2478 id = atoi(cmd);
2479 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
2480
2481 ssid = wpa_config_get_network(wpa_s->conf, id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002482 if (ssid)
2483 wpas_notify_network_removed(wpa_s, ssid);
Deepthi Gowria831d782012-09-03 11:55:38 +03002484 if (ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002485 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2486 "id=%d", id);
2487 return -1;
2488 }
2489
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002490 if (ssid == wpa_s->current_ssid || wpa_s->current_ssid == NULL) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002491#ifdef CONFIG_SME
2492 wpa_s->sme.prev_bssid_set = 0;
2493#endif /* CONFIG_SME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002494 /*
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002495 * Invalidate the EAP session cache if the current or
2496 * previously used network is removed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002497 */
2498 eapol_sm_invalidate_cached_session(wpa_s->eapol);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002499 }
2500
2501 if (ssid == wpa_s->current_ssid) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07002502 wpa_sm_set_config(wpa_s->wpa, NULL);
2503 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002504
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002505 wpa_supplicant_deauthenticate(wpa_s,
2506 WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002507 }
2508
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002509 was_disabled = ssid->disabled;
2510
Deepthi Gowria831d782012-09-03 11:55:38 +03002511 if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
2512 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
2513 "network id=%d", id);
2514 return -1;
2515 }
2516
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002517 if (!was_disabled && wpa_s->sched_scanning) {
2518 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to remove "
2519 "network from filters");
2520 wpa_supplicant_cancel_sched_scan(wpa_s);
2521 wpa_supplicant_req_scan(wpa_s, 0, 0);
2522 }
2523
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002524 return 0;
2525}
2526
2527
2528static int wpa_supplicant_ctrl_iface_set_network(
2529 struct wpa_supplicant *wpa_s, char *cmd)
2530{
2531 int id;
2532 struct wpa_ssid *ssid;
2533 char *name, *value;
2534
2535 /* cmd: "<network id> <variable name> <value>" */
2536 name = os_strchr(cmd, ' ');
2537 if (name == NULL)
2538 return -1;
2539 *name++ = '\0';
2540
2541 value = os_strchr(name, ' ');
2542 if (value == NULL)
2543 return -1;
2544 *value++ = '\0';
2545
2546 id = atoi(cmd);
2547 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
2548 id, name);
2549 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2550 (u8 *) value, os_strlen(value));
2551
2552 ssid = wpa_config_get_network(wpa_s->conf, id);
2553 if (ssid == NULL) {
2554 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2555 "id=%d", id);
2556 return -1;
2557 }
2558
2559 if (wpa_config_set(ssid, name, value, 0) < 0) {
2560 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
2561 "variable '%s'", name);
2562 return -1;
2563 }
2564
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002565 if (os_strcmp(name, "bssid") != 0 &&
2566 os_strcmp(name, "priority") != 0)
2567 wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002568
2569 if (wpa_s->current_ssid == ssid || wpa_s->current_ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002570 /*
2571 * Invalidate the EAP session cache if anything in the current
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002572 * or previously used configuration changes.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002573 */
2574 eapol_sm_invalidate_cached_session(wpa_s->eapol);
2575 }
2576
2577 if ((os_strcmp(name, "psk") == 0 &&
2578 value[0] == '"' && ssid->ssid_len) ||
2579 (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
2580 wpa_config_update_psk(ssid);
2581 else if (os_strcmp(name, "priority") == 0)
2582 wpa_config_update_prio_list(wpa_s->conf);
2583
2584 return 0;
2585}
2586
2587
2588static int wpa_supplicant_ctrl_iface_get_network(
2589 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
2590{
2591 int id;
2592 size_t res;
2593 struct wpa_ssid *ssid;
2594 char *name, *value;
2595
2596 /* cmd: "<network id> <variable name>" */
2597 name = os_strchr(cmd, ' ');
2598 if (name == NULL || buflen == 0)
2599 return -1;
2600 *name++ = '\0';
2601
2602 id = atoi(cmd);
2603 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
2604 id, name);
2605
2606 ssid = wpa_config_get_network(wpa_s->conf, id);
2607 if (ssid == NULL) {
2608 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2609 "id=%d", id);
2610 return -1;
2611 }
2612
2613 value = wpa_config_get_no_key(ssid, name);
2614 if (value == NULL) {
2615 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
2616 "variable '%s'", name);
2617 return -1;
2618 }
2619
2620 res = os_strlcpy(buf, value, buflen);
2621 if (res >= buflen) {
2622 os_free(value);
2623 return -1;
2624 }
2625
2626 os_free(value);
2627
2628 return res;
2629}
2630
2631
Dmitry Shmidt04949592012-07-19 12:16:46 -07002632static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
2633 char *buf, size_t buflen)
2634{
2635 char *pos, *end;
2636 struct wpa_cred *cred;
2637 int ret;
2638
2639 pos = buf;
2640 end = buf + buflen;
2641 ret = os_snprintf(pos, end - pos,
2642 "cred id / realm / username / domain / imsi\n");
2643 if (ret < 0 || ret >= end - pos)
2644 return pos - buf;
2645 pos += ret;
2646
2647 cred = wpa_s->conf->cred;
2648 while (cred) {
2649 ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
2650 cred->id, cred->realm ? cred->realm : "",
2651 cred->username ? cred->username : "",
Dmitry Shmidt051af732013-10-22 13:52:46 -07002652 cred->domain ? cred->domain[0] : "",
Dmitry Shmidt04949592012-07-19 12:16:46 -07002653 cred->imsi ? cred->imsi : "");
2654 if (ret < 0 || ret >= end - pos)
2655 return pos - buf;
2656 pos += ret;
2657
2658 cred = cred->next;
2659 }
2660
2661 return pos - buf;
2662}
2663
2664
2665static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
2666 char *buf, size_t buflen)
2667{
2668 struct wpa_cred *cred;
2669 int ret;
2670
2671 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
2672
2673 cred = wpa_config_add_cred(wpa_s->conf);
2674 if (cred == NULL)
2675 return -1;
2676
2677 ret = os_snprintf(buf, buflen, "%d\n", cred->id);
2678 if (ret < 0 || (size_t) ret >= buflen)
2679 return -1;
2680 return ret;
2681}
2682
2683
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002684static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
2685 struct wpa_cred *cred)
2686{
2687 struct wpa_ssid *ssid;
2688 char str[20];
2689
2690 if (cred == NULL || wpa_config_remove_cred(wpa_s->conf, cred->id) < 0) {
2691 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
2692 return -1;
2693 }
2694
2695 /* Remove any network entry created based on the removed credential */
2696 ssid = wpa_s->conf->ssid;
2697 while (ssid) {
2698 if (ssid->parent_cred == cred) {
2699 wpa_printf(MSG_DEBUG, "Remove network id %d since it "
2700 "used the removed credential", ssid->id);
2701 os_snprintf(str, sizeof(str), "%d", ssid->id);
2702 ssid = ssid->next;
2703 wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
2704 } else
2705 ssid = ssid->next;
2706 }
2707
2708 return 0;
2709}
2710
2711
Dmitry Shmidt04949592012-07-19 12:16:46 -07002712static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
2713 char *cmd)
2714{
2715 int id;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002716 struct wpa_cred *cred, *prev;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002717
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002718 /* cmd: "<cred id>", "all", "sp_fqdn=<FQDN>", or
2719 * "provisioning_sp=<FQDN> */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002720 if (os_strcmp(cmd, "all") == 0) {
2721 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
2722 cred = wpa_s->conf->cred;
2723 while (cred) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002724 prev = cred;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002725 cred = cred->next;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002726 wpas_ctrl_remove_cred(wpa_s, prev);
2727 }
2728 return 0;
2729 }
2730
2731 if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
2732 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
2733 cmd + 8);
2734 cred = wpa_s->conf->cred;
2735 while (cred) {
2736 prev = cred;
2737 cred = cred->next;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002738 if (prev->domain) {
2739 size_t i;
2740 for (i = 0; i < prev->num_domain; i++) {
2741 if (os_strcmp(prev->domain[i], cmd + 8)
2742 != 0)
2743 continue;
2744 wpas_ctrl_remove_cred(wpa_s, prev);
2745 break;
2746 }
2747 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002748 }
2749 return 0;
2750 }
2751
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002752 if (os_strncmp(cmd, "provisioning_sp=", 16) == 0) {
2753 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED provisioning SP FQDN '%s'",
2754 cmd + 16);
2755 cred = wpa_s->conf->cred;
2756 while (cred) {
2757 prev = cred;
2758 cred = cred->next;
2759 if (prev->provisioning_sp &&
2760 os_strcmp(prev->provisioning_sp, cmd + 16) == 0)
2761 wpas_ctrl_remove_cred(wpa_s, prev);
2762 }
2763 return 0;
2764 }
2765
Dmitry Shmidt04949592012-07-19 12:16:46 -07002766 id = atoi(cmd);
2767 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
2768
2769 cred = wpa_config_get_cred(wpa_s->conf, id);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002770 return wpas_ctrl_remove_cred(wpa_s, cred);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002771}
2772
2773
2774static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
2775 char *cmd)
2776{
2777 int id;
2778 struct wpa_cred *cred;
2779 char *name, *value;
2780
2781 /* cmd: "<cred id> <variable name> <value>" */
2782 name = os_strchr(cmd, ' ');
2783 if (name == NULL)
2784 return -1;
2785 *name++ = '\0';
2786
2787 value = os_strchr(name, ' ');
2788 if (value == NULL)
2789 return -1;
2790 *value++ = '\0';
2791
2792 id = atoi(cmd);
2793 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
2794 id, name);
2795 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2796 (u8 *) value, os_strlen(value));
2797
2798 cred = wpa_config_get_cred(wpa_s->conf, id);
2799 if (cred == NULL) {
2800 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
2801 id);
2802 return -1;
2803 }
2804
2805 if (wpa_config_set_cred(cred, name, value, 0) < 0) {
2806 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
2807 "variable '%s'", name);
2808 return -1;
2809 }
2810
2811 return 0;
2812}
2813
2814
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002815#ifndef CONFIG_NO_CONFIG_WRITE
2816static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
2817{
2818 int ret;
2819
2820 if (!wpa_s->conf->update_config) {
2821 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
2822 "to update configuration (update_config=0)");
2823 return -1;
2824 }
2825
2826 ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
2827 if (ret) {
2828 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
2829 "update configuration");
2830 } else {
2831 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
2832 " updated");
2833 }
2834
2835 return ret;
2836}
2837#endif /* CONFIG_NO_CONFIG_WRITE */
2838
2839
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002840struct cipher_info {
2841 unsigned int capa;
2842 const char *name;
2843 int group_only;
2844};
2845
2846static const struct cipher_info ciphers[] = {
2847 { WPA_DRIVER_CAPA_ENC_CCMP_256, "CCMP-256", 0 },
2848 { WPA_DRIVER_CAPA_ENC_GCMP_256, "GCMP-256", 0 },
2849 { WPA_DRIVER_CAPA_ENC_CCMP, "CCMP", 0 },
2850 { WPA_DRIVER_CAPA_ENC_GCMP, "GCMP", 0 },
2851 { WPA_DRIVER_CAPA_ENC_TKIP, "TKIP", 0 },
2852 { WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE, "NONE", 0 },
2853 { WPA_DRIVER_CAPA_ENC_WEP104, "WEP104", 1 },
2854 { WPA_DRIVER_CAPA_ENC_WEP40, "WEP40", 1 }
2855};
2856
2857
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002858static int ctrl_iface_get_capability_pairwise(int res, char *strict,
2859 struct wpa_driver_capa *capa,
2860 char *buf, size_t buflen)
2861{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002862 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002863 char *pos, *end;
2864 size_t len;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002865 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002866
2867 pos = buf;
2868 end = pos + buflen;
2869
2870 if (res < 0) {
2871 if (strict)
2872 return 0;
2873 len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
2874 if (len >= buflen)
2875 return -1;
2876 return len;
2877 }
2878
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002879 for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
2880 if (!ciphers[i].group_only && capa->enc & ciphers[i].capa) {
2881 ret = os_snprintf(pos, end - pos, "%s%s",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002882 pos == buf ? "" : " ",
2883 ciphers[i].name);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002884 if (ret < 0 || ret >= end - pos)
2885 return pos - buf;
2886 pos += ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002887 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002888 }
2889
2890 return pos - buf;
2891}
2892
2893
2894static int ctrl_iface_get_capability_group(int res, char *strict,
2895 struct wpa_driver_capa *capa,
2896 char *buf, size_t buflen)
2897{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002898 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002899 char *pos, *end;
2900 size_t len;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002901 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002902
2903 pos = buf;
2904 end = pos + buflen;
2905
2906 if (res < 0) {
2907 if (strict)
2908 return 0;
2909 len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
2910 if (len >= buflen)
2911 return -1;
2912 return len;
2913 }
2914
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002915 for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
2916 if (capa->enc & ciphers[i].capa) {
2917 ret = os_snprintf(pos, end - pos, "%s%s",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002918 pos == buf ? "" : " ",
2919 ciphers[i].name);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002920 if (ret < 0 || ret >= end - pos)
2921 return pos - buf;
2922 pos += ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002923 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002924 }
2925
2926 return pos - buf;
2927}
2928
2929
2930static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
2931 struct wpa_driver_capa *capa,
2932 char *buf, size_t buflen)
2933{
2934 int ret;
2935 char *pos, *end;
2936 size_t len;
2937
2938 pos = buf;
2939 end = pos + buflen;
2940
2941 if (res < 0) {
2942 if (strict)
2943 return 0;
2944 len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
2945 "NONE", buflen);
2946 if (len >= buflen)
2947 return -1;
2948 return len;
2949 }
2950
2951 ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
2952 if (ret < 0 || ret >= end - pos)
2953 return pos - buf;
2954 pos += ret;
2955
2956 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2957 WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
2958 ret = os_snprintf(pos, end - pos, " WPA-EAP");
2959 if (ret < 0 || ret >= end - pos)
2960 return pos - buf;
2961 pos += ret;
2962 }
2963
2964 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
2965 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
2966 ret = os_snprintf(pos, end - pos, " WPA-PSK");
2967 if (ret < 0 || ret >= end - pos)
2968 return pos - buf;
2969 pos += ret;
2970 }
2971
2972 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
2973 ret = os_snprintf(pos, end - pos, " WPA-NONE");
2974 if (ret < 0 || ret >= end - pos)
2975 return pos - buf;
2976 pos += ret;
2977 }
2978
2979 return pos - buf;
2980}
2981
2982
2983static int ctrl_iface_get_capability_proto(int res, char *strict,
2984 struct wpa_driver_capa *capa,
2985 char *buf, size_t buflen)
2986{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002987 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002988 char *pos, *end;
2989 size_t len;
2990
2991 pos = buf;
2992 end = pos + buflen;
2993
2994 if (res < 0) {
2995 if (strict)
2996 return 0;
2997 len = os_strlcpy(buf, "RSN WPA", buflen);
2998 if (len >= buflen)
2999 return -1;
3000 return len;
3001 }
3002
3003 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
3004 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003005 ret = os_snprintf(pos, end - pos, "%sRSN",
3006 pos == buf ? "" : " ");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003007 if (ret < 0 || ret >= end - pos)
3008 return pos - buf;
3009 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003010 }
3011
3012 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
3013 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003014 ret = os_snprintf(pos, end - pos, "%sWPA",
3015 pos == buf ? "" : " ");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003016 if (ret < 0 || ret >= end - pos)
3017 return pos - buf;
3018 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003019 }
3020
3021 return pos - buf;
3022}
3023
3024
3025static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
3026 struct wpa_driver_capa *capa,
3027 char *buf, size_t buflen)
3028{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003029 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003030 char *pos, *end;
3031 size_t len;
3032
3033 pos = buf;
3034 end = pos + buflen;
3035
3036 if (res < 0) {
3037 if (strict)
3038 return 0;
3039 len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
3040 if (len >= buflen)
3041 return -1;
3042 return len;
3043 }
3044
3045 if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003046 ret = os_snprintf(pos, end - pos, "%sOPEN",
3047 pos == buf ? "" : " ");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003048 if (ret < 0 || ret >= end - pos)
3049 return pos - buf;
3050 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003051 }
3052
3053 if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
3054 ret = os_snprintf(pos, end - pos, "%sSHARED",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003055 pos == buf ? "" : " ");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003056 if (ret < 0 || ret >= end - pos)
3057 return pos - buf;
3058 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003059 }
3060
3061 if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003062 ret = os_snprintf(pos, end - pos, "%sLEAP",
3063 pos == buf ? "" : " ");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003064 if (ret < 0 || ret >= end - pos)
3065 return pos - buf;
3066 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003067 }
3068
3069 return pos - buf;
3070}
3071
3072
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003073static int ctrl_iface_get_capability_modes(int res, char *strict,
3074 struct wpa_driver_capa *capa,
3075 char *buf, size_t buflen)
3076{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003077 int ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003078 char *pos, *end;
3079 size_t len;
3080
3081 pos = buf;
3082 end = pos + buflen;
3083
3084 if (res < 0) {
3085 if (strict)
3086 return 0;
3087 len = os_strlcpy(buf, "IBSS AP", buflen);
3088 if (len >= buflen)
3089 return -1;
3090 return len;
3091 }
3092
3093 if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003094 ret = os_snprintf(pos, end - pos, "%sIBSS",
3095 pos == buf ? "" : " ");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003096 if (ret < 0 || ret >= end - pos)
3097 return pos - buf;
3098 pos += ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003099 }
3100
3101 if (capa->flags & WPA_DRIVER_FLAGS_AP) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003102 ret = os_snprintf(pos, end - pos, "%sAP",
3103 pos == buf ? "" : " ");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003104 if (ret < 0 || ret >= end - pos)
3105 return pos - buf;
3106 pos += ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003107 }
3108
3109 return pos - buf;
3110}
3111
3112
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003113static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
3114 char *buf, size_t buflen)
3115{
3116 struct hostapd_channel_data *chnl;
3117 int ret, i, j;
3118 char *pos, *end, *hmode;
3119
3120 pos = buf;
3121 end = pos + buflen;
3122
3123 for (j = 0; j < wpa_s->hw.num_modes; j++) {
3124 switch (wpa_s->hw.modes[j].mode) {
3125 case HOSTAPD_MODE_IEEE80211B:
3126 hmode = "B";
3127 break;
3128 case HOSTAPD_MODE_IEEE80211G:
3129 hmode = "G";
3130 break;
3131 case HOSTAPD_MODE_IEEE80211A:
3132 hmode = "A";
3133 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003134 case HOSTAPD_MODE_IEEE80211AD:
3135 hmode = "AD";
3136 break;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003137 default:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003138 continue;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003139 }
3140 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
3141 if (ret < 0 || ret >= end - pos)
3142 return pos - buf;
3143 pos += ret;
3144 chnl = wpa_s->hw.modes[j].channels;
3145 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003146 if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
3147 continue;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003148 ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
3149 if (ret < 0 || ret >= end - pos)
3150 return pos - buf;
3151 pos += ret;
3152 }
3153 ret = os_snprintf(pos, end - pos, "\n");
3154 if (ret < 0 || ret >= end - pos)
3155 return pos - buf;
3156 pos += ret;
3157 }
3158
3159 return pos - buf;
3160}
3161
3162
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003163static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
3164 char *buf, size_t buflen)
3165{
3166 struct hostapd_channel_data *chnl;
3167 int ret, i, j;
3168 char *pos, *end, *hmode;
3169
3170 pos = buf;
3171 end = pos + buflen;
3172
3173 for (j = 0; j < wpa_s->hw.num_modes; j++) {
3174 switch (wpa_s->hw.modes[j].mode) {
3175 case HOSTAPD_MODE_IEEE80211B:
3176 hmode = "B";
3177 break;
3178 case HOSTAPD_MODE_IEEE80211G:
3179 hmode = "G";
3180 break;
3181 case HOSTAPD_MODE_IEEE80211A:
3182 hmode = "A";
3183 break;
3184 case HOSTAPD_MODE_IEEE80211AD:
3185 hmode = "AD";
3186 break;
3187 default:
3188 continue;
3189 }
3190 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
3191 hmode);
3192 if (ret < 0 || ret >= end - pos)
3193 return pos - buf;
3194 pos += ret;
3195 chnl = wpa_s->hw.modes[j].channels;
3196 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
3197 if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
3198 continue;
Dmitry Shmidt5da5e352014-02-03 13:30:46 -08003199 ret = os_snprintf(pos, end - pos, " %d = %d MHz%s%s\n",
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003200 chnl[i].chan, chnl[i].freq,
3201 chnl[i].flag & HOSTAPD_CHAN_NO_IBSS ?
Dmitry Shmidt5da5e352014-02-03 13:30:46 -08003202 " (NO_IBSS)" : "",
3203 chnl[i].flag & HOSTAPD_CHAN_RADAR ?
3204 " (DFS)" : "");
3205
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003206 if (ret < 0 || ret >= end - pos)
3207 return pos - buf;
3208 pos += ret;
3209 }
3210 ret = os_snprintf(pos, end - pos, "\n");
3211 if (ret < 0 || ret >= end - pos)
3212 return pos - buf;
3213 pos += ret;
3214 }
3215
3216 return pos - buf;
3217}
3218
3219
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003220static int wpa_supplicant_ctrl_iface_get_capability(
3221 struct wpa_supplicant *wpa_s, const char *_field, char *buf,
3222 size_t buflen)
3223{
3224 struct wpa_driver_capa capa;
3225 int res;
3226 char *strict;
3227 char field[30];
3228 size_t len;
3229
3230 /* Determine whether or not strict checking was requested */
3231 len = os_strlcpy(field, _field, sizeof(field));
3232 if (len >= sizeof(field))
3233 return -1;
3234 strict = os_strchr(field, ' ');
3235 if (strict != NULL) {
3236 *strict++ = '\0';
3237 if (os_strcmp(strict, "strict") != 0)
3238 return -1;
3239 }
3240
3241 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
3242 field, strict ? strict : "");
3243
3244 if (os_strcmp(field, "eap") == 0) {
3245 return eap_get_names(buf, buflen);
3246 }
3247
3248 res = wpa_drv_get_capa(wpa_s, &capa);
3249
3250 if (os_strcmp(field, "pairwise") == 0)
3251 return ctrl_iface_get_capability_pairwise(res, strict, &capa,
3252 buf, buflen);
3253
3254 if (os_strcmp(field, "group") == 0)
3255 return ctrl_iface_get_capability_group(res, strict, &capa,
3256 buf, buflen);
3257
3258 if (os_strcmp(field, "key_mgmt") == 0)
3259 return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
3260 buf, buflen);
3261
3262 if (os_strcmp(field, "proto") == 0)
3263 return ctrl_iface_get_capability_proto(res, strict, &capa,
3264 buf, buflen);
3265
3266 if (os_strcmp(field, "auth_alg") == 0)
3267 return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
3268 buf, buflen);
3269
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003270 if (os_strcmp(field, "modes") == 0)
3271 return ctrl_iface_get_capability_modes(res, strict, &capa,
3272 buf, buflen);
3273
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003274 if (os_strcmp(field, "channels") == 0)
3275 return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
3276
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003277 if (os_strcmp(field, "freq") == 0)
3278 return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
3279
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003280 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
3281 field);
3282
3283 return -1;
3284}
3285
3286
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003287#ifdef CONFIG_INTERWORKING
3288static char * anqp_add_hex(char *pos, char *end, const char *title,
3289 struct wpabuf *data)
3290{
3291 char *start = pos;
3292 size_t i;
3293 int ret;
3294 const u8 *d;
3295
3296 if (data == NULL)
3297 return start;
3298
3299 ret = os_snprintf(pos, end - pos, "%s=", title);
3300 if (ret < 0 || ret >= end - pos)
3301 return start;
3302 pos += ret;
3303
3304 d = wpabuf_head_u8(data);
3305 for (i = 0; i < wpabuf_len(data); i++) {
3306 ret = os_snprintf(pos, end - pos, "%02x", *d++);
3307 if (ret < 0 || ret >= end - pos)
3308 return start;
3309 pos += ret;
3310 }
3311
3312 ret = os_snprintf(pos, end - pos, "\n");
3313 if (ret < 0 || ret >= end - pos)
3314 return start;
3315 pos += ret;
3316
3317 return pos;
3318}
3319#endif /* CONFIG_INTERWORKING */
3320
3321
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003322static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
3323 unsigned long mask, char *buf, size_t buflen)
3324{
3325 size_t i;
3326 int ret;
3327 char *pos, *end;
3328 const u8 *ie, *ie2;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003329
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003330 pos = buf;
3331 end = buf + buflen;
3332
3333 if (mask & WPA_BSS_MASK_ID) {
3334 ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
3335 if (ret < 0 || ret >= end - pos)
3336 return 0;
3337 pos += ret;
3338 }
3339
3340 if (mask & WPA_BSS_MASK_BSSID) {
3341 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
3342 MAC2STR(bss->bssid));
3343 if (ret < 0 || ret >= end - pos)
3344 return 0;
3345 pos += ret;
3346 }
3347
3348 if (mask & WPA_BSS_MASK_FREQ) {
3349 ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
3350 if (ret < 0 || ret >= end - pos)
3351 return 0;
3352 pos += ret;
3353 }
3354
3355 if (mask & WPA_BSS_MASK_BEACON_INT) {
3356 ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
3357 bss->beacon_int);
3358 if (ret < 0 || ret >= end - pos)
3359 return 0;
3360 pos += ret;
3361 }
3362
3363 if (mask & WPA_BSS_MASK_CAPABILITIES) {
3364 ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
3365 bss->caps);
3366 if (ret < 0 || ret >= end - pos)
3367 return 0;
3368 pos += ret;
3369 }
3370
3371 if (mask & WPA_BSS_MASK_QUAL) {
3372 ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
3373 if (ret < 0 || ret >= end - pos)
3374 return 0;
3375 pos += ret;
3376 }
3377
3378 if (mask & WPA_BSS_MASK_NOISE) {
3379 ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
3380 if (ret < 0 || ret >= end - pos)
3381 return 0;
3382 pos += ret;
3383 }
3384
3385 if (mask & WPA_BSS_MASK_LEVEL) {
3386 ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
3387 if (ret < 0 || ret >= end - pos)
3388 return 0;
3389 pos += ret;
3390 }
3391
3392 if (mask & WPA_BSS_MASK_TSF) {
3393 ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
3394 (unsigned long long) bss->tsf);
3395 if (ret < 0 || ret >= end - pos)
3396 return 0;
3397 pos += ret;
3398 }
3399
3400 if (mask & WPA_BSS_MASK_AGE) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003401 struct os_reltime now;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003402
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003403 os_get_reltime(&now);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003404 ret = os_snprintf(pos, end - pos, "age=%d\n",
3405 (int) (now.sec - bss->last_update.sec));
3406 if (ret < 0 || ret >= end - pos)
3407 return 0;
3408 pos += ret;
3409 }
3410
3411 if (mask & WPA_BSS_MASK_IE) {
3412 ret = os_snprintf(pos, end - pos, "ie=");
3413 if (ret < 0 || ret >= end - pos)
3414 return 0;
3415 pos += ret;
3416
3417 ie = (const u8 *) (bss + 1);
3418 for (i = 0; i < bss->ie_len; i++) {
3419 ret = os_snprintf(pos, end - pos, "%02x", *ie++);
3420 if (ret < 0 || ret >= end - pos)
3421 return 0;
3422 pos += ret;
3423 }
3424
3425 ret = os_snprintf(pos, end - pos, "\n");
3426 if (ret < 0 || ret >= end - pos)
3427 return 0;
3428 pos += ret;
3429 }
3430
3431 if (mask & WPA_BSS_MASK_FLAGS) {
3432 ret = os_snprintf(pos, end - pos, "flags=");
3433 if (ret < 0 || ret >= end - pos)
3434 return 0;
3435 pos += ret;
3436
3437 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
3438 if (ie)
3439 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
3440 2 + ie[1]);
3441 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
3442 if (ie2)
3443 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
3444 2 + ie2[1]);
3445 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
3446 if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
3447 ret = os_snprintf(pos, end - pos, "[WEP]");
3448 if (ret < 0 || ret >= end - pos)
3449 return 0;
3450 pos += ret;
3451 }
3452 if (bss->caps & IEEE80211_CAP_IBSS) {
3453 ret = os_snprintf(pos, end - pos, "[IBSS]");
3454 if (ret < 0 || ret >= end - pos)
3455 return 0;
3456 pos += ret;
3457 }
3458 if (bss->caps & IEEE80211_CAP_ESS) {
3459 ret = os_snprintf(pos, end - pos, "[ESS]");
3460 if (ret < 0 || ret >= end - pos)
3461 return 0;
3462 pos += ret;
3463 }
Dmitry Shmidt96571392013-10-14 12:54:46 -07003464 if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
3465 wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003466 ret = os_snprintf(pos, end - pos, "[P2P]");
3467 if (ret < 0 || ret >= end - pos)
3468 return 0;
3469 pos += ret;
3470 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003471#ifdef CONFIG_HS20
3472 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
3473 ret = os_snprintf(pos, end - pos, "[HS20]");
3474 if (ret < 0 || ret >= end - pos)
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003475 return 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003476 pos += ret;
3477 }
3478#endif /* CONFIG_HS20 */
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003479
3480 ret = os_snprintf(pos, end - pos, "\n");
3481 if (ret < 0 || ret >= end - pos)
3482 return 0;
3483 pos += ret;
3484 }
3485
3486 if (mask & WPA_BSS_MASK_SSID) {
3487 ret = os_snprintf(pos, end - pos, "ssid=%s\n",
3488 wpa_ssid_txt(bss->ssid, bss->ssid_len));
3489 if (ret < 0 || ret >= end - pos)
3490 return 0;
3491 pos += ret;
3492 }
3493
3494#ifdef CONFIG_WPS
3495 if (mask & WPA_BSS_MASK_WPS_SCAN) {
3496 ie = (const u8 *) (bss + 1);
3497 ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
3498 if (ret < 0 || ret >= end - pos)
3499 return 0;
3500 pos += ret;
3501 }
3502#endif /* CONFIG_WPS */
3503
3504#ifdef CONFIG_P2P
3505 if (mask & WPA_BSS_MASK_P2P_SCAN) {
3506 ie = (const u8 *) (bss + 1);
3507 ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
3508 if (ret < 0 || ret >= end - pos)
3509 return 0;
3510 pos += ret;
3511 }
3512#endif /* CONFIG_P2P */
3513
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003514#ifdef CONFIG_WIFI_DISPLAY
3515 if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
3516 struct wpabuf *wfd;
3517 ie = (const u8 *) (bss + 1);
3518 wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
3519 WFD_IE_VENDOR_TYPE);
3520 if (wfd) {
3521 ret = os_snprintf(pos, end - pos, "wfd_subelems=");
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003522 if (ret < 0 || ret >= end - pos) {
3523 wpabuf_free(wfd);
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003524 return 0;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003525 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003526 pos += ret;
3527
3528 pos += wpa_snprintf_hex(pos, end - pos,
3529 wpabuf_head(wfd),
3530 wpabuf_len(wfd));
3531 wpabuf_free(wfd);
3532
3533 ret = os_snprintf(pos, end - pos, "\n");
3534 if (ret < 0 || ret >= end - pos)
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003535 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003536 pos += ret;
3537 }
3538 }
3539#endif /* CONFIG_WIFI_DISPLAY */
3540
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003541#ifdef CONFIG_INTERWORKING
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003542 if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
3543 struct wpa_bss_anqp *anqp = bss->anqp;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003544 pos = anqp_add_hex(pos, end, "anqp_venue_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003545 anqp->venue_name);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003546 pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003547 anqp->network_auth_type);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003548 pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003549 anqp->roaming_consortium);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003550 pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003551 anqp->ip_addr_type_availability);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003552 pos = anqp_add_hex(pos, end, "anqp_nai_realm",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003553 anqp->nai_realm);
3554 pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003555 pos = anqp_add_hex(pos, end, "anqp_domain_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003556 anqp->domain_name);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003557#ifdef CONFIG_HS20
3558 pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003559 anqp->hs20_operator_friendly_name);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003560 pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003561 anqp->hs20_wan_metrics);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003562 pos = anqp_add_hex(pos, end, "hs20_connection_capability",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003563 anqp->hs20_connection_capability);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003564 pos = anqp_add_hex(pos, end, "hs20_operating_class",
3565 anqp->hs20_operating_class);
3566 pos = anqp_add_hex(pos, end, "hs20_osu_providers_list",
3567 anqp->hs20_osu_providers_list);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003568#endif /* CONFIG_HS20 */
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003569 }
3570#endif /* CONFIG_INTERWORKING */
3571
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003572 if (mask & WPA_BSS_MASK_DELIM) {
3573 ret = os_snprintf(pos, end - pos, "====\n");
3574 if (ret < 0 || ret >= end - pos)
3575 return 0;
3576 pos += ret;
3577 }
Irfan Sheriffe2ea0082012-08-13 10:56:16 -07003578
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003579 return pos - buf;
3580}
3581
Dmitry Shmidt04949592012-07-19 12:16:46 -07003582
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003583static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
3584 const char *cmd, char *buf,
3585 size_t buflen)
3586{
3587 u8 bssid[ETH_ALEN];
3588 size_t i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003589 struct wpa_bss *bss;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003590 struct wpa_bss *bsslast = NULL;
3591 struct dl_list *next;
3592 int ret = 0;
3593 int len;
3594 char *ctmp;
3595 unsigned long mask = WPA_BSS_MASK_ALL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003596
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003597 if (os_strncmp(cmd, "RANGE=", 6) == 0) {
3598 if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
3599 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003600 list_id);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003601 bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
3602 list_id);
3603 } else { /* N1-N2 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003604 unsigned int id1, id2;
3605
3606 if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
3607 wpa_printf(MSG_INFO, "Wrong BSS range "
3608 "format");
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003609 return 0;
3610 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003611
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003612 if (*(cmd + 6) == '-')
3613 id1 = 0;
3614 else
3615 id1 = atoi(cmd + 6);
3616 ctmp++;
3617 if (*ctmp >= '0' && *ctmp <= '9')
3618 id2 = atoi(ctmp);
3619 else
3620 id2 = (unsigned int) -1;
3621 bss = wpa_bss_get_id_range(wpa_s, id1, id2);
3622 if (id2 == (unsigned int) -1)
Dmitry Shmidt04949592012-07-19 12:16:46 -07003623 bsslast = dl_list_last(&wpa_s->bss_id,
3624 struct wpa_bss,
3625 list_id);
3626 else {
3627 bsslast = wpa_bss_get_id(wpa_s, id2);
3628 if (bsslast == NULL && bss && id2 > id1) {
3629 struct wpa_bss *tmp = bss;
3630 for (;;) {
3631 next = tmp->list_id.next;
3632 if (next == &wpa_s->bss_id)
3633 break;
3634 tmp = dl_list_entry(
3635 next, struct wpa_bss,
3636 list_id);
3637 if (tmp->id > id2)
3638 break;
3639 bsslast = tmp;
3640 }
3641 }
3642 }
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003643 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003644 } else if (os_strncmp(cmd, "FIRST", 5) == 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -07003645 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003646 else if (os_strncmp(cmd, "LAST", 4) == 0)
3647 bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003648 else if (os_strncmp(cmd, "ID-", 3) == 0) {
3649 i = atoi(cmd + 3);
3650 bss = wpa_bss_get_id(wpa_s, i);
3651 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
3652 i = atoi(cmd + 5);
3653 bss = wpa_bss_get_id(wpa_s, i);
3654 if (bss) {
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003655 next = bss->list_id.next;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003656 if (next == &wpa_s->bss_id)
3657 bss = NULL;
3658 else
3659 bss = dl_list_entry(next, struct wpa_bss,
3660 list_id);
3661 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003662#ifdef CONFIG_P2P
3663 } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
3664 if (hwaddr_aton(cmd + 13, bssid) == 0)
3665 bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
3666 else
3667 bss = NULL;
3668#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003669 } else if (hwaddr_aton(cmd, bssid) == 0)
3670 bss = wpa_bss_get_bssid(wpa_s, bssid);
3671 else {
3672 struct wpa_bss *tmp;
3673 i = atoi(cmd);
3674 bss = NULL;
3675 dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
3676 {
3677 if (i-- == 0) {
3678 bss = tmp;
3679 break;
3680 }
3681 }
3682 }
3683
Dmitry Shmidt04949592012-07-19 12:16:46 -07003684 if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
3685 mask = strtoul(ctmp + 5, NULL, 0x10);
3686 if (mask == 0)
3687 mask = WPA_BSS_MASK_ALL;
3688 }
3689
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003690 if (bss == NULL)
3691 return 0;
3692
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003693 if (bsslast == NULL)
3694 bsslast = bss;
3695 do {
3696 len = print_bss_info(wpa_s, bss, mask, buf, buflen);
3697 ret += len;
3698 buf += len;
3699 buflen -= len;
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003700 if (bss == bsslast) {
3701 if ((mask & WPA_BSS_MASK_DELIM) && len &&
3702 (bss == dl_list_last(&wpa_s->bss_id,
3703 struct wpa_bss, list_id)))
3704 os_snprintf(buf - 5, 5, "####\n");
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003705 break;
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003706 }
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003707 next = bss->list_id.next;
3708 if (next == &wpa_s->bss_id)
3709 break;
3710 bss = dl_list_entry(next, struct wpa_bss, list_id);
3711 } while (bss && len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003712
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003713 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003714}
3715
3716
3717static int wpa_supplicant_ctrl_iface_ap_scan(
3718 struct wpa_supplicant *wpa_s, char *cmd)
3719{
3720 int ap_scan = atoi(cmd);
3721 return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
3722}
3723
3724
3725static int wpa_supplicant_ctrl_iface_scan_interval(
3726 struct wpa_supplicant *wpa_s, char *cmd)
3727{
3728 int scan_int = atoi(cmd);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003729 return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003730}
3731
3732
3733static int wpa_supplicant_ctrl_iface_bss_expire_age(
3734 struct wpa_supplicant *wpa_s, char *cmd)
3735{
3736 int expire_age = atoi(cmd);
3737 return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
3738}
3739
3740
3741static int wpa_supplicant_ctrl_iface_bss_expire_count(
3742 struct wpa_supplicant *wpa_s, char *cmd)
3743{
3744 int expire_count = atoi(cmd);
3745 return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
3746}
3747
3748
Dmitry Shmidtf48e4f92012-08-24 11:14:44 -07003749static int wpa_supplicant_ctrl_iface_bss_flush(
3750 struct wpa_supplicant *wpa_s, char *cmd)
3751{
3752 int flush_age = atoi(cmd);
3753
3754 if (flush_age == 0)
3755 wpa_bss_flush(wpa_s);
3756 else
3757 wpa_bss_flush_by_age(wpa_s, flush_age);
3758 return 0;
3759}
3760
3761
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003762static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
3763{
3764 wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
3765 /* MLME-DELETEKEYS.request */
3766 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
3767 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
3768 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
3769 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
3770#ifdef CONFIG_IEEE80211W
3771 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
3772 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
3773#endif /* CONFIG_IEEE80211W */
3774
3775 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
3776 0);
3777 /* MLME-SETPROTECTION.request(None) */
3778 wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
3779 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
3780 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
3781 wpa_sm_drop_sa(wpa_s->wpa);
3782}
3783
3784
3785static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
3786 char *addr)
3787{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003788#ifdef CONFIG_NO_SCAN_PROCESSING
3789 return -1;
3790#else /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003791 u8 bssid[ETH_ALEN];
3792 struct wpa_bss *bss;
3793 struct wpa_ssid *ssid = wpa_s->current_ssid;
3794
3795 if (hwaddr_aton(addr, bssid)) {
3796 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
3797 "address '%s'", addr);
3798 return -1;
3799 }
3800
3801 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
3802
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003803 if (!ssid) {
3804 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
3805 "configuration known for the target AP");
3806 return -1;
3807 }
3808
3809 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003810 if (!bss) {
3811 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
3812 "from BSS table");
3813 return -1;
3814 }
3815
3816 /*
3817 * TODO: Find best network configuration block from configuration to
3818 * allow roaming to other networks
3819 */
3820
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003821 wpa_s->reassociate = 1;
3822 wpa_supplicant_connect(wpa_s, bss, ssid);
3823
3824 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003825#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003826}
3827
3828
3829#ifdef CONFIG_P2P
3830static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
3831{
3832 unsigned int timeout = atoi(cmd);
3833 enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003834 u8 dev_id[ETH_ALEN], *_dev_id = NULL;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003835 u8 dev_type[WPS_DEV_TYPE_LEN], *_dev_type = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003836 char *pos;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003837 unsigned int search_delay;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003838
3839 if (os_strstr(cmd, "type=social"))
3840 type = P2P_FIND_ONLY_SOCIAL;
3841 else if (os_strstr(cmd, "type=progressive"))
3842 type = P2P_FIND_PROGRESSIVE;
3843
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003844 pos = os_strstr(cmd, "dev_id=");
3845 if (pos) {
3846 pos += 7;
3847 if (hwaddr_aton(pos, dev_id))
3848 return -1;
3849 _dev_id = dev_id;
3850 }
3851
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003852 pos = os_strstr(cmd, "dev_type=");
3853 if (pos) {
3854 pos += 9;
3855 if (wps_dev_type_str2bin(pos, dev_type) < 0)
3856 return -1;
3857 _dev_type = dev_type;
3858 }
3859
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003860 pos = os_strstr(cmd, "delay=");
3861 if (pos) {
3862 pos += 6;
3863 search_delay = atoi(pos);
3864 } else
3865 search_delay = wpas_p2p_search_delay(wpa_s);
3866
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003867 return wpas_p2p_find(wpa_s, timeout, type, _dev_type != NULL, _dev_type,
3868 _dev_id, search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003869}
3870
3871
3872static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
3873 char *buf, size_t buflen)
3874{
3875 u8 addr[ETH_ALEN];
3876 char *pos, *pos2;
3877 char *pin = NULL;
3878 enum p2p_wps_method wps_method;
3879 int new_pin;
3880 int ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003881 int persistent_group, persistent_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003882 int join;
3883 int auth;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003884 int automatic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003885 int go_intent = -1;
3886 int freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003887 int pd;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003888 int ht40, vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003889
Dmitry Shmidt04949592012-07-19 12:16:46 -07003890 /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad]
3891 * [persistent|persistent=<network id>]
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003892 * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003893 * [ht40] [vht] */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003894
3895 if (hwaddr_aton(cmd, addr))
3896 return -1;
3897
3898 pos = cmd + 17;
3899 if (*pos != ' ')
3900 return -1;
3901 pos++;
3902
3903 persistent_group = os_strstr(pos, " persistent") != NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003904 pos2 = os_strstr(pos, " persistent=");
3905 if (pos2) {
3906 struct wpa_ssid *ssid;
3907 persistent_id = atoi(pos2 + 12);
3908 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3909 if (ssid == NULL || ssid->disabled != 2 ||
3910 ssid->mode != WPAS_MODE_P2P_GO) {
3911 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3912 "SSID id=%d for persistent P2P group (GO)",
3913 persistent_id);
3914 return -1;
3915 }
3916 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003917 join = os_strstr(pos, " join") != NULL;
3918 auth = os_strstr(pos, " auth") != NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003919 automatic = os_strstr(pos, " auto") != NULL;
3920 pd = os_strstr(pos, " provdisc") != NULL;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003921 vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
3922 ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
3923 vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003924
3925 pos2 = os_strstr(pos, " go_intent=");
3926 if (pos2) {
3927 pos2 += 11;
3928 go_intent = atoi(pos2);
3929 if (go_intent < 0 || go_intent > 15)
3930 return -1;
3931 }
3932
3933 pos2 = os_strstr(pos, " freq=");
3934 if (pos2) {
3935 pos2 += 6;
3936 freq = atoi(pos2);
3937 if (freq <= 0)
3938 return -1;
3939 }
3940
3941 if (os_strncmp(pos, "pin", 3) == 0) {
3942 /* Request random PIN (to be displayed) and enable the PIN */
3943 wps_method = WPS_PIN_DISPLAY;
3944 } else if (os_strncmp(pos, "pbc", 3) == 0) {
3945 wps_method = WPS_PBC;
3946 } else {
3947 pin = pos;
3948 pos = os_strchr(pin, ' ');
3949 wps_method = WPS_PIN_KEYPAD;
3950 if (pos) {
3951 *pos++ = '\0';
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003952 if (os_strncmp(pos, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003953 wps_method = WPS_PIN_DISPLAY;
3954 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003955 if (!wps_pin_str_valid(pin)) {
3956 os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
3957 return 17;
3958 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003959 }
3960
3961 new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003962 persistent_group, automatic, join,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003963 auth, go_intent, freq, persistent_id, pd,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003964 ht40, vht);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003965 if (new_pin == -2) {
3966 os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
3967 return 25;
3968 }
3969 if (new_pin == -3) {
3970 os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
3971 return 25;
3972 }
3973 if (new_pin < 0)
3974 return -1;
3975 if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
3976 ret = os_snprintf(buf, buflen, "%08d", new_pin);
3977 if (ret < 0 || (size_t) ret >= buflen)
3978 return -1;
3979 return ret;
3980 }
3981
3982 os_memcpy(buf, "OK\n", 3);
3983 return 3;
3984}
3985
3986
3987static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
3988{
3989 unsigned int timeout = atoi(cmd);
3990 return wpas_p2p_listen(wpa_s, timeout);
3991}
3992
3993
3994static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
3995{
3996 u8 addr[ETH_ALEN];
3997 char *pos;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003998 enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003999
Dmitry Shmidt04949592012-07-19 12:16:46 -07004000 /* <addr> <config method> [join|auto] */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004001
4002 if (hwaddr_aton(cmd, addr))
4003 return -1;
4004
4005 pos = cmd + 17;
4006 if (*pos != ' ')
4007 return -1;
4008 pos++;
4009
Dmitry Shmidt04949592012-07-19 12:16:46 -07004010 if (os_strstr(pos, " join") != NULL)
4011 use = WPAS_P2P_PD_FOR_JOIN;
4012 else if (os_strstr(pos, " auto") != NULL)
4013 use = WPAS_P2P_PD_AUTO;
4014
4015 return wpas_p2p_prov_disc(wpa_s, addr, pos, use);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004016}
4017
4018
4019static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
4020 size_t buflen)
4021{
4022 struct wpa_ssid *ssid = wpa_s->current_ssid;
4023
4024 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
4025 ssid->passphrase == NULL)
4026 return -1;
4027
4028 os_strlcpy(buf, ssid->passphrase, buflen);
4029 return os_strlen(buf);
4030}
4031
4032
4033static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
4034 char *buf, size_t buflen)
4035{
4036 u64 ref;
4037 int res;
4038 u8 dst_buf[ETH_ALEN], *dst;
4039 struct wpabuf *tlvs;
4040 char *pos;
4041 size_t len;
4042
4043 if (hwaddr_aton(cmd, dst_buf))
4044 return -1;
4045 dst = dst_buf;
4046 if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
4047 dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
4048 dst = NULL;
4049 pos = cmd + 17;
4050 if (*pos != ' ')
4051 return -1;
4052 pos++;
4053
4054 if (os_strncmp(pos, "upnp ", 5) == 0) {
4055 u8 version;
4056 pos += 5;
4057 if (hexstr2bin(pos, &version, 1) < 0)
4058 return -1;
4059 pos += 2;
4060 if (*pos != ' ')
4061 return -1;
4062 pos++;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004063 ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004064#ifdef CONFIG_WIFI_DISPLAY
4065 } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
4066 ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
4067#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004068 } else {
4069 len = os_strlen(pos);
4070 if (len & 1)
4071 return -1;
4072 len /= 2;
4073 tlvs = wpabuf_alloc(len);
4074 if (tlvs == NULL)
4075 return -1;
4076 if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
4077 wpabuf_free(tlvs);
4078 return -1;
4079 }
4080
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004081 ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004082 wpabuf_free(tlvs);
4083 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004084 if (ref == 0)
4085 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004086 res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
4087 if (res < 0 || (unsigned) res >= buflen)
4088 return -1;
4089 return res;
4090}
4091
4092
4093static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
4094 char *cmd)
4095{
4096 long long unsigned val;
4097 u64 req;
4098 if (sscanf(cmd, "%llx", &val) != 1)
4099 return -1;
4100 req = val;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004101 return wpas_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004102}
4103
4104
4105static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
4106{
4107 int freq;
4108 u8 dst[ETH_ALEN];
4109 u8 dialog_token;
4110 struct wpabuf *resp_tlvs;
4111 char *pos, *pos2;
4112 size_t len;
4113
4114 pos = os_strchr(cmd, ' ');
4115 if (pos == NULL)
4116 return -1;
4117 *pos++ = '\0';
4118 freq = atoi(cmd);
4119 if (freq == 0)
4120 return -1;
4121
4122 if (hwaddr_aton(pos, dst))
4123 return -1;
4124 pos += 17;
4125 if (*pos != ' ')
4126 return -1;
4127 pos++;
4128
4129 pos2 = os_strchr(pos, ' ');
4130 if (pos2 == NULL)
4131 return -1;
4132 *pos2++ = '\0';
4133 dialog_token = atoi(pos);
4134
4135 len = os_strlen(pos2);
4136 if (len & 1)
4137 return -1;
4138 len /= 2;
4139 resp_tlvs = wpabuf_alloc(len);
4140 if (resp_tlvs == NULL)
4141 return -1;
4142 if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
4143 wpabuf_free(resp_tlvs);
4144 return -1;
4145 }
4146
4147 wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
4148 wpabuf_free(resp_tlvs);
4149 return 0;
4150}
4151
4152
4153static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
4154 char *cmd)
4155{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004156 if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
4157 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004158 wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
4159 return 0;
4160}
4161
4162
4163static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
4164 char *cmd)
4165{
4166 char *pos;
4167 size_t len;
4168 struct wpabuf *query, *resp;
4169
4170 pos = os_strchr(cmd, ' ');
4171 if (pos == NULL)
4172 return -1;
4173 *pos++ = '\0';
4174
4175 len = os_strlen(cmd);
4176 if (len & 1)
4177 return -1;
4178 len /= 2;
4179 query = wpabuf_alloc(len);
4180 if (query == NULL)
4181 return -1;
4182 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
4183 wpabuf_free(query);
4184 return -1;
4185 }
4186
4187 len = os_strlen(pos);
4188 if (len & 1) {
4189 wpabuf_free(query);
4190 return -1;
4191 }
4192 len /= 2;
4193 resp = wpabuf_alloc(len);
4194 if (resp == NULL) {
4195 wpabuf_free(query);
4196 return -1;
4197 }
4198 if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
4199 wpabuf_free(query);
4200 wpabuf_free(resp);
4201 return -1;
4202 }
4203
4204 if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
4205 wpabuf_free(query);
4206 wpabuf_free(resp);
4207 return -1;
4208 }
4209 return 0;
4210}
4211
4212
4213static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
4214{
4215 char *pos;
4216 u8 version;
4217
4218 pos = os_strchr(cmd, ' ');
4219 if (pos == NULL)
4220 return -1;
4221 *pos++ = '\0';
4222
4223 if (hexstr2bin(cmd, &version, 1) < 0)
4224 return -1;
4225
4226 return wpas_p2p_service_add_upnp(wpa_s, version, pos);
4227}
4228
4229
4230static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
4231{
4232 char *pos;
4233
4234 pos = os_strchr(cmd, ' ');
4235 if (pos == NULL)
4236 return -1;
4237 *pos++ = '\0';
4238
4239 if (os_strcmp(cmd, "bonjour") == 0)
4240 return p2p_ctrl_service_add_bonjour(wpa_s, pos);
4241 if (os_strcmp(cmd, "upnp") == 0)
4242 return p2p_ctrl_service_add_upnp(wpa_s, pos);
4243 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
4244 return -1;
4245}
4246
4247
4248static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
4249 char *cmd)
4250{
4251 size_t len;
4252 struct wpabuf *query;
4253 int ret;
4254
4255 len = os_strlen(cmd);
4256 if (len & 1)
4257 return -1;
4258 len /= 2;
4259 query = wpabuf_alloc(len);
4260 if (query == NULL)
4261 return -1;
4262 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
4263 wpabuf_free(query);
4264 return -1;
4265 }
4266
4267 ret = wpas_p2p_service_del_bonjour(wpa_s, query);
4268 wpabuf_free(query);
4269 return ret;
4270}
4271
4272
4273static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
4274{
4275 char *pos;
4276 u8 version;
4277
4278 pos = os_strchr(cmd, ' ');
4279 if (pos == NULL)
4280 return -1;
4281 *pos++ = '\0';
4282
4283 if (hexstr2bin(cmd, &version, 1) < 0)
4284 return -1;
4285
4286 return wpas_p2p_service_del_upnp(wpa_s, version, pos);
4287}
4288
4289
4290static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
4291{
4292 char *pos;
4293
4294 pos = os_strchr(cmd, ' ');
4295 if (pos == NULL)
4296 return -1;
4297 *pos++ = '\0';
4298
4299 if (os_strcmp(cmd, "bonjour") == 0)
4300 return p2p_ctrl_service_del_bonjour(wpa_s, pos);
4301 if (os_strcmp(cmd, "upnp") == 0)
4302 return p2p_ctrl_service_del_upnp(wpa_s, pos);
4303 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
4304 return -1;
4305}
4306
4307
4308static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
4309{
4310 u8 addr[ETH_ALEN];
4311
4312 /* <addr> */
4313
4314 if (hwaddr_aton(cmd, addr))
4315 return -1;
4316
4317 return wpas_p2p_reject(wpa_s, addr);
4318}
4319
4320
4321static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
4322{
4323 char *pos;
4324 int id;
4325 struct wpa_ssid *ssid;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004326 u8 *_peer = NULL, peer[ETH_ALEN];
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004327 int freq = 0, pref_freq = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004328 int ht40, vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004329
4330 id = atoi(cmd);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004331 pos = os_strstr(cmd, " peer=");
4332 if (pos) {
4333 pos += 6;
4334 if (hwaddr_aton(pos, peer))
4335 return -1;
4336 _peer = peer;
4337 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004338 ssid = wpa_config_get_network(wpa_s->conf, id);
4339 if (ssid == NULL || ssid->disabled != 2) {
4340 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
4341 "for persistent P2P group",
4342 id);
4343 return -1;
4344 }
4345
Jouni Malinen31be0a42012-08-31 21:20:51 +03004346 pos = os_strstr(cmd, " freq=");
4347 if (pos) {
4348 pos += 6;
4349 freq = atoi(pos);
4350 if (freq <= 0)
4351 return -1;
4352 }
4353
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004354 pos = os_strstr(cmd, " pref=");
4355 if (pos) {
4356 pos += 6;
4357 pref_freq = atoi(pos);
4358 if (pref_freq <= 0)
4359 return -1;
4360 }
4361
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004362 vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
4363 ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
4364 vht;
Jouni Malinen31be0a42012-08-31 21:20:51 +03004365
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004366 return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, ht40, vht,
4367 pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004368}
4369
4370
4371static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
4372{
4373 char *pos;
4374 u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
4375
4376 pos = os_strstr(cmd, " peer=");
4377 if (!pos)
4378 return -1;
4379
4380 *pos = '\0';
4381 pos += 6;
4382 if (hwaddr_aton(pos, peer)) {
4383 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
4384 return -1;
4385 }
4386
4387 pos = os_strstr(pos, " go_dev_addr=");
4388 if (pos) {
4389 pos += 13;
4390 if (hwaddr_aton(pos, go_dev_addr)) {
4391 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
4392 pos);
4393 return -1;
4394 }
4395 go_dev = go_dev_addr;
4396 }
4397
4398 return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
4399}
4400
4401
4402static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
4403{
4404 if (os_strncmp(cmd, "persistent=", 11) == 0)
4405 return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
4406 if (os_strncmp(cmd, "group=", 6) == 0)
4407 return p2p_ctrl_invite_group(wpa_s, cmd + 6);
4408
4409 return -1;
4410}
4411
4412
4413static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004414 char *cmd, int freq, int ht40,
4415 int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004416{
4417 int id;
4418 struct wpa_ssid *ssid;
4419
4420 id = atoi(cmd);
4421 ssid = wpa_config_get_network(wpa_s->conf, id);
4422 if (ssid == NULL || ssid->disabled != 2) {
4423 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
4424 "for persistent P2P group",
4425 id);
4426 return -1;
4427 }
4428
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004429 return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq, 0, ht40, vht,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004430 NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004431}
4432
4433
4434static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
4435{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004436 int freq = 0, ht40, vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004437 char *pos;
4438
4439 pos = os_strstr(cmd, "freq=");
4440 if (pos)
4441 freq = atoi(pos + 5);
4442
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004443 vht = (os_strstr(cmd, "vht") != NULL) || wpa_s->conf->p2p_go_vht;
4444 ht40 = (os_strstr(cmd, "ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
4445 vht;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004446
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004447 if (os_strncmp(cmd, "persistent=", 11) == 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004448 return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004449 ht40, vht);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004450 if (os_strcmp(cmd, "persistent") == 0 ||
4451 os_strncmp(cmd, "persistent ", 11) == 0)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004452 return wpas_p2p_group_add(wpa_s, 1, freq, ht40, vht);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004453 if (os_strncmp(cmd, "freq=", 5) == 0)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004454 return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004455 if (ht40)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004456 return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004457
4458 wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
4459 cmd);
4460 return -1;
4461}
4462
4463
4464static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
4465 char *buf, size_t buflen)
4466{
4467 u8 addr[ETH_ALEN], *addr_ptr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004468 int next, res;
4469 const struct p2p_peer_info *info;
4470 char *pos, *end;
4471 char devtype[WPS_DEV_TYPE_BUFSIZE];
4472 struct wpa_ssid *ssid;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004473 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004474
4475 if (!wpa_s->global->p2p)
4476 return -1;
4477
4478 if (os_strcmp(cmd, "FIRST") == 0) {
4479 addr_ptr = NULL;
4480 next = 0;
4481 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
4482 if (hwaddr_aton(cmd + 5, addr) < 0)
4483 return -1;
4484 addr_ptr = addr;
4485 next = 1;
4486 } else {
4487 if (hwaddr_aton(cmd, addr) < 0)
4488 return -1;
4489 addr_ptr = addr;
4490 next = 0;
4491 }
4492
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004493 info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
4494 if (info == NULL)
4495 return -1;
4496
4497 pos = buf;
4498 end = buf + buflen;
4499
4500 res = os_snprintf(pos, end - pos, MACSTR "\n"
4501 "pri_dev_type=%s\n"
4502 "device_name=%s\n"
4503 "manufacturer=%s\n"
4504 "model_name=%s\n"
4505 "model_number=%s\n"
4506 "serial_number=%s\n"
4507 "config_methods=0x%x\n"
4508 "dev_capab=0x%x\n"
4509 "group_capab=0x%x\n"
4510 "level=%d\n",
4511 MAC2STR(info->p2p_device_addr),
4512 wps_dev_type_bin2str(info->pri_dev_type,
4513 devtype, sizeof(devtype)),
4514 info->device_name,
4515 info->manufacturer,
4516 info->model_name,
4517 info->model_number,
4518 info->serial_number,
4519 info->config_methods,
4520 info->dev_capab,
4521 info->group_capab,
4522 info->level);
4523 if (res < 0 || res >= end - pos)
4524 return pos - buf;
4525 pos += res;
4526
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004527 for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
4528 {
4529 const u8 *t;
4530 t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
4531 res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
4532 wps_dev_type_bin2str(t, devtype,
4533 sizeof(devtype)));
4534 if (res < 0 || res >= end - pos)
4535 return pos - buf;
4536 pos += res;
4537 }
4538
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004539 ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004540 if (ssid) {
4541 res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
4542 if (res < 0 || res >= end - pos)
4543 return pos - buf;
4544 pos += res;
4545 }
4546
4547 res = p2p_get_peer_info_txt(info, pos, end - pos);
4548 if (res < 0)
4549 return pos - buf;
4550 pos += res;
4551
4552 return pos - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004553}
4554
4555
Dmitry Shmidt04949592012-07-19 12:16:46 -07004556static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
4557 const char *param)
4558{
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07004559 unsigned int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004560
4561 if (wpa_s->global->p2p == NULL)
4562 return -1;
4563
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07004564 if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
4565 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004566
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07004567 for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
4568 struct wpa_freq_range *freq;
4569 freq = &wpa_s->global->p2p_disallow_freq.range[i];
Dmitry Shmidt04949592012-07-19 12:16:46 -07004570 wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07004571 freq->min, freq->max);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004572 }
4573
Dmitry Shmidt04949592012-07-19 12:16:46 -07004574 wpas_p2p_update_channel_list(wpa_s);
4575 return 0;
4576}
4577
4578
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004579static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
4580{
4581 char *param;
4582
4583 if (wpa_s->global->p2p == NULL)
4584 return -1;
4585
4586 param = os_strchr(cmd, ' ');
4587 if (param == NULL)
4588 return -1;
4589 *param++ = '\0';
4590
4591 if (os_strcmp(cmd, "discoverability") == 0) {
4592 p2p_set_client_discoverability(wpa_s->global->p2p,
4593 atoi(param));
4594 return 0;
4595 }
4596
4597 if (os_strcmp(cmd, "managed") == 0) {
4598 p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
4599 return 0;
4600 }
4601
4602 if (os_strcmp(cmd, "listen_channel") == 0) {
4603 return p2p_set_listen_channel(wpa_s->global->p2p, 81,
4604 atoi(param));
4605 }
4606
4607 if (os_strcmp(cmd, "ssid_postfix") == 0) {
4608 return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
4609 os_strlen(param));
4610 }
4611
4612 if (os_strcmp(cmd, "noa") == 0) {
4613 char *pos;
4614 int count, start, duration;
4615 /* GO NoA parameters: count,start_offset(ms),duration(ms) */
4616 count = atoi(param);
4617 pos = os_strchr(param, ',');
4618 if (pos == NULL)
4619 return -1;
4620 pos++;
4621 start = atoi(pos);
4622 pos = os_strchr(pos, ',');
4623 if (pos == NULL)
4624 return -1;
4625 pos++;
4626 duration = atoi(pos);
4627 if (count < 0 || count > 255 || start < 0 || duration < 0)
4628 return -1;
4629 if (count == 0 && duration > 0)
4630 return -1;
4631 wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
4632 "start=%d duration=%d", count, start, duration);
4633 return wpas_p2p_set_noa(wpa_s, count, start, duration);
4634 }
4635
4636 if (os_strcmp(cmd, "ps") == 0)
4637 return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
4638
4639 if (os_strcmp(cmd, "oppps") == 0)
4640 return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
4641
4642 if (os_strcmp(cmd, "ctwindow") == 0)
4643 return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
4644
4645 if (os_strcmp(cmd, "disabled") == 0) {
4646 wpa_s->global->p2p_disabled = atoi(param);
4647 wpa_printf(MSG_DEBUG, "P2P functionality %s",
4648 wpa_s->global->p2p_disabled ?
4649 "disabled" : "enabled");
4650 if (wpa_s->global->p2p_disabled) {
4651 wpas_p2p_stop_find(wpa_s);
4652 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
4653 p2p_flush(wpa_s->global->p2p);
4654 }
4655 return 0;
4656 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004657
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07004658 if (os_strcmp(cmd, "conc_pref") == 0) {
4659 if (os_strcmp(param, "sta") == 0)
4660 wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
4661 else if (os_strcmp(param, "p2p") == 0)
4662 wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004663 else {
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07004664 wpa_printf(MSG_INFO, "Invalid conc_pref value");
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004665 return -1;
4666 }
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07004667 wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
Dmitry Shmidt04949592012-07-19 12:16:46 -07004668 "%s", param);
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004669 return 0;
4670 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004671
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004672 if (os_strcmp(cmd, "force_long_sd") == 0) {
4673 wpa_s->force_long_sd = atoi(param);
4674 return 0;
4675 }
4676
4677 if (os_strcmp(cmd, "peer_filter") == 0) {
4678 u8 addr[ETH_ALEN];
4679 if (hwaddr_aton(param, addr))
4680 return -1;
4681 p2p_set_peer_filter(wpa_s->global->p2p, addr);
4682 return 0;
4683 }
4684
4685 if (os_strcmp(cmd, "cross_connect") == 0)
4686 return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
4687
4688 if (os_strcmp(cmd, "go_apsd") == 0) {
4689 if (os_strcmp(param, "disable") == 0)
4690 wpa_s->set_ap_uapsd = 0;
4691 else {
4692 wpa_s->set_ap_uapsd = 1;
4693 wpa_s->ap_uapsd = atoi(param);
4694 }
4695 return 0;
4696 }
4697
4698 if (os_strcmp(cmd, "client_apsd") == 0) {
4699 if (os_strcmp(param, "disable") == 0)
4700 wpa_s->set_sta_uapsd = 0;
4701 else {
4702 int be, bk, vi, vo;
4703 char *pos;
4704 /* format: BE,BK,VI,VO;max SP Length */
4705 be = atoi(param);
4706 pos = os_strchr(param, ',');
4707 if (pos == NULL)
4708 return -1;
4709 pos++;
4710 bk = atoi(pos);
4711 pos = os_strchr(pos, ',');
4712 if (pos == NULL)
4713 return -1;
4714 pos++;
4715 vi = atoi(pos);
4716 pos = os_strchr(pos, ',');
4717 if (pos == NULL)
4718 return -1;
4719 pos++;
4720 vo = atoi(pos);
4721 /* ignore max SP Length for now */
4722
4723 wpa_s->set_sta_uapsd = 1;
4724 wpa_s->sta_uapsd = 0;
4725 if (be)
4726 wpa_s->sta_uapsd |= BIT(0);
4727 if (bk)
4728 wpa_s->sta_uapsd |= BIT(1);
4729 if (vi)
4730 wpa_s->sta_uapsd |= BIT(2);
4731 if (vo)
4732 wpa_s->sta_uapsd |= BIT(3);
4733 }
4734 return 0;
4735 }
4736
Dmitry Shmidt04949592012-07-19 12:16:46 -07004737 if (os_strcmp(cmd, "disallow_freq") == 0)
4738 return p2p_ctrl_disallow_freq(wpa_s, param);
4739
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004740 if (os_strcmp(cmd, "disc_int") == 0) {
4741 int min_disc_int, max_disc_int, max_disc_tu;
4742 char *pos;
4743
4744 pos = param;
4745
4746 min_disc_int = atoi(pos);
4747 pos = os_strchr(pos, ' ');
4748 if (pos == NULL)
4749 return -1;
4750 *pos++ = '\0';
4751
4752 max_disc_int = atoi(pos);
4753 pos = os_strchr(pos, ' ');
4754 if (pos == NULL)
4755 return -1;
4756 *pos++ = '\0';
4757
4758 max_disc_tu = atoi(pos);
4759
4760 return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
4761 max_disc_int, max_disc_tu);
4762 }
4763
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004764 if (os_strcmp(cmd, "per_sta_psk") == 0) {
4765 wpa_s->global->p2p_per_sta_psk = !!atoi(param);
4766 return 0;
4767 }
4768
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004769#ifdef CONFIG_WPS_NFC
4770 if (os_strcmp(cmd, "nfc_tag") == 0)
4771 return wpas_p2p_nfc_tag_enabled(wpa_s, !!atoi(param));
4772#endif /* CONFIG_WPS_NFC */
4773
4774 if (os_strcmp(cmd, "disable_ip_addr_req") == 0) {
4775 wpa_s->p2p_disable_ip_addr_req = !!atoi(param);
4776 return 0;
4777 }
4778
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004779 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
4780 cmd);
4781
4782 return -1;
4783}
4784
4785
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004786static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
4787{
4788 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
4789 wpa_s->force_long_sd = 0;
4790 if (wpa_s->global->p2p)
4791 p2p_flush(wpa_s->global->p2p);
4792}
4793
4794
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004795static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
4796{
4797 char *pos, *pos2;
4798 unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
4799
4800 if (cmd[0]) {
4801 pos = os_strchr(cmd, ' ');
4802 if (pos == NULL)
4803 return -1;
4804 *pos++ = '\0';
4805 dur1 = atoi(cmd);
4806
4807 pos2 = os_strchr(pos, ' ');
4808 if (pos2)
4809 *pos2++ = '\0';
4810 int1 = atoi(pos);
4811 } else
4812 pos2 = NULL;
4813
4814 if (pos2) {
4815 pos = os_strchr(pos2, ' ');
4816 if (pos == NULL)
4817 return -1;
4818 *pos++ = '\0';
4819 dur2 = atoi(pos2);
4820 int2 = atoi(pos);
4821 }
4822
4823 return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
4824}
4825
4826
4827static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
4828{
4829 char *pos;
4830 unsigned int period = 0, interval = 0;
4831
4832 if (cmd[0]) {
4833 pos = os_strchr(cmd, ' ');
4834 if (pos == NULL)
4835 return -1;
4836 *pos++ = '\0';
4837 period = atoi(cmd);
4838 interval = atoi(pos);
4839 }
4840
4841 return wpas_p2p_ext_listen(wpa_s, period, interval);
4842}
4843
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004844
4845static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
4846{
4847 const char *pos;
4848 u8 peer[ETH_ALEN];
4849 int iface_addr = 0;
4850
4851 pos = cmd;
4852 if (os_strncmp(pos, "iface=", 6) == 0) {
4853 iface_addr = 1;
4854 pos += 6;
4855 }
4856 if (hwaddr_aton(pos, peer))
4857 return -1;
4858
4859 wpas_p2p_remove_client(wpa_s, peer, iface_addr);
4860 return 0;
4861}
4862
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004863#endif /* CONFIG_P2P */
4864
4865
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004866static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s, char *val)
4867{
4868 struct wpa_freq_range_list ranges;
4869 int *freqs = NULL;
4870 struct hostapd_hw_modes *mode;
4871 u16 i;
4872
4873 if (wpa_s->hw.modes == NULL)
4874 return NULL;
4875
4876 os_memset(&ranges, 0, sizeof(ranges));
4877 if (freq_range_list_parse(&ranges, val) < 0)
4878 return NULL;
4879
4880 for (i = 0; i < wpa_s->hw.num_modes; i++) {
4881 int j;
4882
4883 mode = &wpa_s->hw.modes[i];
4884 for (j = 0; j < mode->num_channels; j++) {
4885 unsigned int freq;
4886
4887 if (mode->channels[j].flag & HOSTAPD_CHAN_DISABLED)
4888 continue;
4889
4890 freq = mode->channels[j].freq;
4891 if (!freq_range_list_includes(&ranges, freq))
4892 continue;
4893
4894 int_array_add_unique(&freqs, freq);
4895 }
4896 }
4897
4898 os_free(ranges.range);
4899 return freqs;
4900}
4901
4902
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004903#ifdef CONFIG_INTERWORKING
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004904
4905static int ctrl_interworking_select(struct wpa_supplicant *wpa_s, char *param)
4906{
4907 int auto_sel = 0;
4908 int *freqs = NULL;
4909
4910 if (param) {
4911 char *pos;
4912
4913 auto_sel = os_strstr(param, "auto") != NULL;
4914
4915 pos = os_strstr(param, "freq=");
4916 if (pos) {
4917 freqs = freq_range_to_channel_list(wpa_s, pos + 5);
4918 if (freqs == NULL)
4919 return -1;
4920 }
4921
4922 }
4923
4924 return interworking_select(wpa_s, auto_sel, freqs);
4925}
4926
4927
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004928static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst)
4929{
4930 u8 bssid[ETH_ALEN];
4931 struct wpa_bss *bss;
4932
4933 if (hwaddr_aton(dst, bssid)) {
4934 wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
4935 return -1;
4936 }
4937
4938 bss = wpa_bss_get_bssid(wpa_s, bssid);
4939 if (bss == NULL) {
4940 wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
4941 MAC2STR(bssid));
4942 return -1;
4943 }
4944
4945 return interworking_connect(wpa_s, bss);
4946}
4947
4948
4949static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
4950{
4951 u8 dst_addr[ETH_ALEN];
4952 int used;
4953 char *pos;
4954#define MAX_ANQP_INFO_ID 100
4955 u16 id[MAX_ANQP_INFO_ID];
4956 size_t num_id = 0;
4957
4958 used = hwaddr_aton2(dst, dst_addr);
4959 if (used < 0)
4960 return -1;
4961 pos = dst + used;
4962 while (num_id < MAX_ANQP_INFO_ID) {
4963 id[num_id] = atoi(pos);
4964 if (id[num_id])
4965 num_id++;
4966 pos = os_strchr(pos + 1, ',');
4967 if (pos == NULL)
4968 break;
4969 pos++;
4970 }
4971
4972 if (num_id == 0)
4973 return -1;
4974
4975 return anqp_send_req(wpa_s, dst_addr, id, num_id);
4976}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004977
4978
4979static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
4980{
4981 u8 dst_addr[ETH_ALEN];
4982 struct wpabuf *advproto, *query = NULL;
4983 int used, ret = -1;
4984 char *pos, *end;
4985 size_t len;
4986
4987 used = hwaddr_aton2(cmd, dst_addr);
4988 if (used < 0)
4989 return -1;
4990
4991 pos = cmd + used;
4992 while (*pos == ' ')
4993 pos++;
4994
4995 /* Advertisement Protocol ID */
4996 end = os_strchr(pos, ' ');
4997 if (end)
4998 len = end - pos;
4999 else
5000 len = os_strlen(pos);
5001 if (len & 0x01)
5002 return -1;
5003 len /= 2;
5004 if (len == 0)
5005 return -1;
5006 advproto = wpabuf_alloc(len);
5007 if (advproto == NULL)
5008 return -1;
5009 if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
5010 goto fail;
5011
5012 if (end) {
5013 /* Optional Query Request */
5014 pos = end + 1;
5015 while (*pos == ' ')
5016 pos++;
5017
5018 len = os_strlen(pos);
5019 if (len) {
5020 if (len & 0x01)
5021 goto fail;
5022 len /= 2;
5023 if (len == 0)
5024 goto fail;
5025 query = wpabuf_alloc(len);
5026 if (query == NULL)
5027 goto fail;
5028 if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
5029 goto fail;
5030 }
5031 }
5032
5033 ret = gas_send_request(wpa_s, dst_addr, advproto, query);
5034
5035fail:
5036 wpabuf_free(advproto);
5037 wpabuf_free(query);
5038
5039 return ret;
5040}
5041
5042
5043static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
5044 size_t buflen)
5045{
5046 u8 addr[ETH_ALEN];
5047 int dialog_token;
5048 int used;
5049 char *pos;
5050 size_t resp_len, start, requested_len;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005051 struct wpabuf *resp;
5052 int ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005053
5054 used = hwaddr_aton2(cmd, addr);
5055 if (used < 0)
5056 return -1;
5057
5058 pos = cmd + used;
5059 while (*pos == ' ')
5060 pos++;
5061 dialog_token = atoi(pos);
5062
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005063 if (wpa_s->last_gas_resp &&
5064 os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) == 0 &&
5065 dialog_token == wpa_s->last_gas_dialog_token)
5066 resp = wpa_s->last_gas_resp;
5067 else if (wpa_s->prev_gas_resp &&
5068 os_memcmp(addr, wpa_s->prev_gas_addr, ETH_ALEN) == 0 &&
5069 dialog_token == wpa_s->prev_gas_dialog_token)
5070 resp = wpa_s->prev_gas_resp;
5071 else
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005072 return -1;
5073
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005074 resp_len = wpabuf_len(resp);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005075 start = 0;
5076 requested_len = resp_len;
5077
5078 pos = os_strchr(pos, ' ');
5079 if (pos) {
5080 start = atoi(pos);
5081 if (start > resp_len)
5082 return os_snprintf(buf, buflen, "FAIL-Invalid range");
5083 pos = os_strchr(pos, ',');
5084 if (pos == NULL)
5085 return -1;
5086 pos++;
5087 requested_len = atoi(pos);
5088 if (start + requested_len > resp_len)
5089 return os_snprintf(buf, buflen, "FAIL-Invalid range");
5090 }
5091
5092 if (requested_len * 2 + 1 > buflen)
5093 return os_snprintf(buf, buflen, "FAIL-Too long response");
5094
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005095 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(resp) + start,
5096 requested_len);
5097
5098 if (start + requested_len == resp_len) {
5099 /*
5100 * Free memory by dropping the response after it has been
5101 * fetched.
5102 */
5103 if (resp == wpa_s->prev_gas_resp) {
5104 wpabuf_free(wpa_s->prev_gas_resp);
5105 wpa_s->prev_gas_resp = NULL;
5106 } else {
5107 wpabuf_free(wpa_s->last_gas_resp);
5108 wpa_s->last_gas_resp = NULL;
5109 }
5110 }
5111
5112 return ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005113}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005114#endif /* CONFIG_INTERWORKING */
5115
5116
Dmitry Shmidt04949592012-07-19 12:16:46 -07005117#ifdef CONFIG_HS20
5118
5119static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
5120{
5121 u8 dst_addr[ETH_ALEN];
5122 int used;
5123 char *pos;
5124 u32 subtypes = 0;
5125
5126 used = hwaddr_aton2(dst, dst_addr);
5127 if (used < 0)
5128 return -1;
5129 pos = dst + used;
5130 for (;;) {
5131 int num = atoi(pos);
5132 if (num <= 0 || num > 31)
5133 return -1;
5134 subtypes |= BIT(num);
5135 pos = os_strchr(pos + 1, ',');
5136 if (pos == NULL)
5137 break;
5138 pos++;
5139 }
5140
5141 if (subtypes == 0)
5142 return -1;
5143
5144 return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0);
5145}
5146
5147
5148static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
5149 const u8 *addr, const char *realm)
5150{
5151 u8 *buf;
5152 size_t rlen, len;
5153 int ret;
5154
5155 rlen = os_strlen(realm);
5156 len = 3 + rlen;
5157 buf = os_malloc(len);
5158 if (buf == NULL)
5159 return -1;
5160 buf[0] = 1; /* NAI Home Realm Count */
5161 buf[1] = 0; /* Formatted in accordance with RFC 4282 */
5162 buf[2] = rlen;
5163 os_memcpy(buf + 3, realm, rlen);
5164
5165 ret = hs20_anqp_send_req(wpa_s, addr,
5166 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
5167 buf, len);
5168
5169 os_free(buf);
5170
5171 return ret;
5172}
5173
5174
5175static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
5176 char *dst)
5177{
5178 struct wpa_cred *cred = wpa_s->conf->cred;
5179 u8 dst_addr[ETH_ALEN];
5180 int used;
5181 u8 *buf;
5182 size_t len;
5183 int ret;
5184
5185 used = hwaddr_aton2(dst, dst_addr);
5186 if (used < 0)
5187 return -1;
5188
5189 while (dst[used] == ' ')
5190 used++;
5191 if (os_strncmp(dst + used, "realm=", 6) == 0)
5192 return hs20_nai_home_realm_list(wpa_s, dst_addr,
5193 dst + used + 6);
5194
5195 len = os_strlen(dst + used);
5196
5197 if (len == 0 && cred && cred->realm)
5198 return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
5199
5200 if (len % 1)
5201 return -1;
5202 len /= 2;
5203 buf = os_malloc(len);
5204 if (buf == NULL)
5205 return -1;
5206 if (hexstr2bin(dst + used, buf, len) < 0) {
5207 os_free(buf);
5208 return -1;
5209 }
5210
5211 ret = hs20_anqp_send_req(wpa_s, dst_addr,
5212 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
5213 buf, len);
5214 os_free(buf);
5215
5216 return ret;
5217}
5218
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08005219
5220static int hs20_icon_request(struct wpa_supplicant *wpa_s, char *cmd)
5221{
5222 u8 dst_addr[ETH_ALEN];
5223 int used;
5224 char *icon;
5225
5226 used = hwaddr_aton2(cmd, dst_addr);
5227 if (used < 0)
5228 return -1;
5229
5230 while (cmd[used] == ' ')
5231 used++;
5232 icon = &cmd[used];
5233
5234 wpa_s->fetch_osu_icon_in_progress = 0;
5235 return hs20_anqp_send_req(wpa_s, dst_addr, BIT(HS20_STYPE_ICON_REQUEST),
5236 (u8 *) icon, os_strlen(icon));
5237}
5238
Dmitry Shmidt04949592012-07-19 12:16:46 -07005239#endif /* CONFIG_HS20 */
5240
5241
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005242static int wpa_supplicant_ctrl_iface_sta_autoconnect(
5243 struct wpa_supplicant *wpa_s, char *cmd)
5244{
5245 wpa_s->auto_reconnect_disabled = atoi(cmd) == 0 ? 1 : 0;
5246 return 0;
5247}
5248
5249
Dmitry Shmidt04949592012-07-19 12:16:46 -07005250#ifdef CONFIG_AUTOSCAN
5251
5252static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
5253 char *cmd)
5254{
5255 enum wpa_states state = wpa_s->wpa_state;
5256 char *new_params = NULL;
5257
5258 if (os_strlen(cmd) > 0) {
5259 new_params = os_strdup(cmd);
5260 if (new_params == NULL)
5261 return -1;
5262 }
5263
5264 os_free(wpa_s->conf->autoscan);
5265 wpa_s->conf->autoscan = new_params;
5266
5267 if (wpa_s->conf->autoscan == NULL)
5268 autoscan_deinit(wpa_s);
5269 else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
5270 autoscan_init(wpa_s, 1);
5271 else if (state == WPA_SCANNING)
5272 wpa_supplicant_reinit_autoscan(wpa_s);
5273
5274 return 0;
5275}
5276
5277#endif /* CONFIG_AUTOSCAN */
5278
5279
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005280#ifdef CONFIG_WNM
5281
5282static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
5283{
5284 int enter;
5285 int intval = 0;
5286 char *pos;
5287 int ret;
5288 struct wpabuf *tfs_req = NULL;
5289
5290 if (os_strncmp(cmd, "enter", 5) == 0)
5291 enter = 1;
5292 else if (os_strncmp(cmd, "exit", 4) == 0)
5293 enter = 0;
5294 else
5295 return -1;
5296
5297 pos = os_strstr(cmd, " interval=");
5298 if (pos)
5299 intval = atoi(pos + 10);
5300
5301 pos = os_strstr(cmd, " tfs_req=");
5302 if (pos) {
5303 char *end;
5304 size_t len;
5305 pos += 9;
5306 end = os_strchr(pos, ' ');
5307 if (end)
5308 len = end - pos;
5309 else
5310 len = os_strlen(pos);
5311 if (len & 1)
5312 return -1;
5313 len /= 2;
5314 tfs_req = wpabuf_alloc(len);
5315 if (tfs_req == NULL)
5316 return -1;
5317 if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
5318 wpabuf_free(tfs_req);
5319 return -1;
5320 }
5321 }
5322
5323 ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
5324 WNM_SLEEP_MODE_EXIT, intval,
5325 tfs_req);
5326 wpabuf_free(tfs_req);
5327
5328 return ret;
5329}
5330
Dmitry Shmidt44c95782013-05-17 09:51:35 -07005331
5332static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
5333{
5334 int query_reason;
5335
5336 query_reason = atoi(cmd);
5337
5338 wpa_printf(MSG_DEBUG, "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d",
5339 query_reason);
5340
5341 return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason);
5342}
5343
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005344#endif /* CONFIG_WNM */
5345
5346
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005347/* Get string representation of channel width */
5348static const char * channel_width_name(enum chan_width width)
5349{
5350 switch (width) {
5351 case CHAN_WIDTH_20_NOHT:
5352 return "20 MHz (no HT)";
5353 case CHAN_WIDTH_20:
5354 return "20 MHz";
5355 case CHAN_WIDTH_40:
5356 return "40 MHz";
5357 case CHAN_WIDTH_80:
5358 return "80 MHz";
5359 case CHAN_WIDTH_80P80:
5360 return "80+80 MHz";
5361 case CHAN_WIDTH_160:
5362 return "160 MHz";
5363 default:
5364 return "unknown";
5365 }
5366}
5367
5368
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005369static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
5370 size_t buflen)
5371{
5372 struct wpa_signal_info si;
5373 int ret;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005374 char *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005375
5376 ret = wpa_drv_signal_poll(wpa_s, &si);
5377 if (ret)
5378 return -1;
5379
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005380 pos = buf;
5381 end = buf + buflen;
5382
5383 ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005384 "NOISE=%d\nFREQUENCY=%u\n",
5385 si.current_signal, si.current_txrate / 1000,
5386 si.current_noise, si.frequency);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005387 if (ret < 0 || ret > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005388 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005389 pos += ret;
5390
5391 if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
5392 ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
5393 channel_width_name(si.chanwidth));
5394 if (ret < 0 || ret > end - pos)
5395 return -1;
5396 pos += ret;
5397 }
5398
5399 if (si.center_frq1 > 0 && si.center_frq2 > 0) {
5400 ret = os_snprintf(pos, end - pos,
5401 "CENTER_FRQ1=%d\nCENTER_FRQ2=%d\n",
5402 si.center_frq1, si.center_frq2);
5403 if (ret < 0 || ret > end - pos)
5404 return -1;
5405 pos += ret;
5406 }
5407
5408 if (si.avg_signal) {
5409 ret = os_snprintf(pos, end - pos,
5410 "AVG_RSSI=%d\n", si.avg_signal);
5411 if (ret < 0 || ret >= end - pos)
5412 return -1;
5413 pos += ret;
5414 }
5415
5416 return pos - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005417}
5418
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03005419
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07005420static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
5421 size_t buflen)
5422{
5423 struct hostap_sta_driver_data sta;
5424 int ret;
5425
5426 ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
5427 if (ret)
5428 return -1;
5429
5430 ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03005431 sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
5432 if (ret < 0 || (size_t) ret > buflen)
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07005433 return -1;
5434 return ret;
5435}
5436
5437
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005438#ifdef ANDROID
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07005439static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
5440 char *buf, size_t buflen)
5441{
5442 int ret;
5443
5444 ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
Dmitry Shmidt9432e122013-09-12 12:39:30 -07005445 if (ret == 0) {
5446 if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
5447 struct p2p_data *p2p = wpa_s->global->p2p;
5448 if (p2p) {
5449 char country[3];
5450 country[0] = cmd[8];
5451 country[1] = cmd[9];
5452 country[2] = 0x04;
5453 p2p_set_country(p2p, country);
5454 }
5455 }
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08005456 ret = os_snprintf(buf, buflen, "%s\n", "OK");
Dmitry Shmidt9432e122013-09-12 12:39:30 -07005457 }
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07005458 return ret;
5459}
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08005460#endif /* ANDROID */
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07005461
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07005462
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005463static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
5464{
5465 wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
5466
5467#ifdef CONFIG_P2P
5468 wpas_p2p_stop_find(wpa_s);
5469 p2p_ctrl_flush(wpa_s);
5470 wpas_p2p_group_remove(wpa_s, "*");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005471 wpas_p2p_service_flush(wpa_s);
5472 wpa_s->global->p2p_disabled = 0;
5473 wpa_s->global->p2p_per_sta_psk = 0;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005474 wpa_s->conf->num_sec_device_types = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005475 wpa_s->p2p_disable_ip_addr_req = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005476#endif /* CONFIG_P2P */
5477
5478#ifdef CONFIG_WPS_TESTING
5479 wps_version_number = 0x20;
5480 wps_testing_dummy_cred = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005481 wps_corrupt_pkhash = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005482#endif /* CONFIG_WPS_TESTING */
5483#ifdef CONFIG_WPS
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005484 wpa_s->wps_fragment_size = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005485 wpas_wps_cancel(wpa_s);
5486#endif /* CONFIG_WPS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07005487 wpa_s->after_wps = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005488 wpa_s->known_wps_freq = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005489
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005490#ifdef CONFIG_TDLS
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005491#ifdef CONFIG_TDLS_TESTING
5492 extern unsigned int tdls_testing;
5493 tdls_testing = 0;
5494#endif /* CONFIG_TDLS_TESTING */
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005495 wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
5496 wpa_tdls_enable(wpa_s->wpa, 1);
5497#endif /* CONFIG_TDLS */
5498
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005499 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
5500 wpa_supplicant_stop_countermeasures(wpa_s, NULL);
5501
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005502 wpa_s->no_keep_alive = 0;
5503
5504 os_free(wpa_s->disallow_aps_bssid);
5505 wpa_s->disallow_aps_bssid = NULL;
5506 wpa_s->disallow_aps_bssid_count = 0;
5507 os_free(wpa_s->disallow_aps_ssid);
5508 wpa_s->disallow_aps_ssid = NULL;
5509 wpa_s->disallow_aps_ssid_count = 0;
5510
5511 wpa_s->set_sta_uapsd = 0;
5512 wpa_s->sta_uapsd = 0;
5513
5514 wpa_drv_radio_disable(wpa_s, 0);
5515
5516 wpa_bss_flush(wpa_s);
5517 wpa_blacklist_clear(wpa_s);
Dmitry Shmidt4b060592013-04-29 16:42:49 -07005518 wpa_s->extra_blacklist_count = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005519 wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
5520 wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005521 wpa_config_flush_blobs(wpa_s->conf);
Dmitry Shmidt18463232014-01-24 12:29:41 -08005522 wpa_s->conf->auto_interworking = 0;
5523 wpa_s->conf->okc = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005524
5525 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME, 43200);
5526 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD, 70);
5527 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, 60);
5528 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
5529
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08005530 radio_remove_works(wpa_s, NULL, 1);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08005531
5532 wpa_s->next_ssid = NULL;
5533
5534#ifdef CONFIG_INTERWORKING
5535 hs20_cancel_fetch_osu(wpa_s);
5536#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005537}
5538
5539
5540static int wpas_ctrl_radio_work_show(struct wpa_supplicant *wpa_s,
5541 char *buf, size_t buflen)
5542{
5543 struct wpa_radio_work *work;
5544 char *pos, *end;
5545 struct os_reltime now, diff;
5546
5547 pos = buf;
5548 end = buf + buflen;
5549
5550 os_get_reltime(&now);
5551
5552 dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
5553 {
5554 int ret;
5555
5556 os_reltime_sub(&now, &work->time, &diff);
5557 ret = os_snprintf(pos, end - pos, "%s@%s:%u:%u:%ld.%06ld\n",
5558 work->type, work->wpa_s->ifname, work->freq,
5559 work->started, diff.sec, diff.usec);
5560 if (ret < 0 || ret >= end - pos)
5561 break;
5562 pos += ret;
5563 }
5564
5565 return pos - buf;
5566}
5567
5568
5569static void wpas_ctrl_radio_work_timeout(void *eloop_ctx, void *timeout_ctx)
5570{
5571 struct wpa_radio_work *work = eloop_ctx;
5572 struct wpa_external_work *ework = work->ctx;
5573
5574 wpa_dbg(work->wpa_s, MSG_DEBUG,
5575 "Timing out external radio work %u (%s)",
5576 ework->id, work->type);
5577 wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_TIMEOUT "%u", ework->id);
5578 os_free(ework);
5579 radio_work_done(work);
5580}
5581
5582
5583static void wpas_ctrl_radio_work_cb(struct wpa_radio_work *work, int deinit)
5584{
5585 struct wpa_external_work *ework = work->ctx;
5586
5587 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08005588 if (work->started)
5589 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
5590 work, NULL);
5591
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005592 os_free(ework);
5593 return;
5594 }
5595
5596 wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting external radio work %u (%s)",
5597 ework->id, ework->type);
5598 wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_START "%u", ework->id);
5599 if (!ework->timeout)
5600 ework->timeout = 10;
5601 eloop_register_timeout(ework->timeout, 0, wpas_ctrl_radio_work_timeout,
5602 work, NULL);
5603}
5604
5605
5606static int wpas_ctrl_radio_work_add(struct wpa_supplicant *wpa_s, char *cmd,
5607 char *buf, size_t buflen)
5608{
5609 struct wpa_external_work *ework;
5610 char *pos, *pos2;
5611 size_t type_len;
5612 int ret;
5613 unsigned int freq = 0;
5614
5615 /* format: <name> [freq=<MHz>] [timeout=<seconds>] */
5616
5617 ework = os_zalloc(sizeof(*ework));
5618 if (ework == NULL)
5619 return -1;
5620
5621 pos = os_strchr(cmd, ' ');
5622 if (pos) {
5623 type_len = pos - cmd;
5624 pos++;
5625
5626 pos2 = os_strstr(pos, "freq=");
5627 if (pos2)
5628 freq = atoi(pos2 + 5);
5629
5630 pos2 = os_strstr(pos, "timeout=");
5631 if (pos2)
5632 ework->timeout = atoi(pos2 + 8);
5633 } else {
5634 type_len = os_strlen(cmd);
5635 }
5636 if (4 + type_len >= sizeof(ework->type))
5637 type_len = sizeof(ework->type) - 4 - 1;
5638 os_strlcpy(ework->type, "ext:", sizeof(ework->type));
5639 os_memcpy(ework->type + 4, cmd, type_len);
5640 ework->type[4 + type_len] = '\0';
5641
5642 wpa_s->ext_work_id++;
5643 if (wpa_s->ext_work_id == 0)
5644 wpa_s->ext_work_id++;
5645 ework->id = wpa_s->ext_work_id;
5646
5647 if (radio_add_work(wpa_s, freq, ework->type, 0, wpas_ctrl_radio_work_cb,
5648 ework) < 0) {
5649 os_free(ework);
5650 return -1;
5651 }
5652
5653 ret = os_snprintf(buf, buflen, "%u", ework->id);
5654 if (ret < 0 || (size_t) ret >= buflen)
5655 return -1;
5656 return ret;
5657}
5658
5659
5660static int wpas_ctrl_radio_work_done(struct wpa_supplicant *wpa_s, char *cmd)
5661{
5662 struct wpa_radio_work *work;
5663 unsigned int id = atoi(cmd);
5664
5665 dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
5666 {
5667 struct wpa_external_work *ework;
5668
5669 if (os_strncmp(work->type, "ext:", 4) != 0)
5670 continue;
5671 ework = work->ctx;
5672 if (id && ework->id != id)
5673 continue;
5674 wpa_dbg(wpa_s, MSG_DEBUG,
5675 "Completed external radio work %u (%s)",
5676 ework->id, ework->type);
5677 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout, work, NULL);
5678 os_free(ework);
5679 radio_work_done(work);
5680 return 3; /* "OK\n" */
5681 }
5682
5683 return -1;
5684}
5685
5686
5687static int wpas_ctrl_radio_work(struct wpa_supplicant *wpa_s, char *cmd,
5688 char *buf, size_t buflen)
5689{
5690 if (os_strcmp(cmd, "show") == 0)
5691 return wpas_ctrl_radio_work_show(wpa_s, buf, buflen);
5692 if (os_strncmp(cmd, "add ", 4) == 0)
5693 return wpas_ctrl_radio_work_add(wpa_s, cmd + 4, buf, buflen);
5694 if (os_strncmp(cmd, "done ", 5) == 0)
5695 return wpas_ctrl_radio_work_done(wpa_s, cmd + 4);
5696 return -1;
5697}
5698
5699
5700void wpas_ctrl_radio_work_flush(struct wpa_supplicant *wpa_s)
5701{
5702 struct wpa_radio_work *work, *tmp;
5703
Dmitry Shmidt18463232014-01-24 12:29:41 -08005704 if (!wpa_s || !wpa_s->radio)
5705 return;
5706
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005707 dl_list_for_each_safe(work, tmp, &wpa_s->radio->work,
5708 struct wpa_radio_work, list) {
5709 struct wpa_external_work *ework;
5710
5711 if (os_strncmp(work->type, "ext:", 4) != 0)
5712 continue;
5713 ework = work->ctx;
5714 wpa_dbg(wpa_s, MSG_DEBUG,
5715 "Flushing %sexternal radio work %u (%s)",
5716 work->started ? " started" : "", ework->id,
5717 ework->type);
5718 if (work->started)
5719 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
5720 work, NULL);
5721 os_free(ework);
5722 radio_work_done(work);
5723 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005724}
5725
5726
Dmitry Shmidt051af732013-10-22 13:52:46 -07005727static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
5728{
5729 struct wpa_supplicant *wpa_s = eloop_ctx;
5730 eapol_sm_notify_ctrl_response(wpa_s->eapol);
5731}
5732
5733
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005734static int set_scan_freqs(struct wpa_supplicant *wpa_s, char *val)
5735{
5736 int *freqs = NULL;
5737
5738 freqs = freq_range_to_channel_list(wpa_s, val);
5739 if (freqs == NULL)
5740 return -1;
5741
5742 os_free(wpa_s->manual_scan_freqs);
5743 wpa_s->manual_scan_freqs = freqs;
5744
5745 return 0;
5746}
5747
5748
5749static void wpas_ctrl_scan(struct wpa_supplicant *wpa_s, char *params,
5750 char *reply, int reply_size, int *reply_len)
5751{
5752 char *pos;
5753
5754 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
5755 *reply_len = -1;
5756 return;
5757 }
5758
5759 wpa_s->manual_scan_passive = 0;
5760 wpa_s->manual_scan_use_id = 0;
5761 wpa_s->manual_scan_only_new = 0;
5762
5763 if (params) {
5764 if (os_strncasecmp(params, "TYPE=ONLY", 9) == 0)
5765 wpa_s->scan_res_handler = scan_only_handler;
5766
5767 pos = os_strstr(params, "freq=");
5768 if (pos && set_scan_freqs(wpa_s, pos + 5) < 0) {
5769 *reply_len = -1;
5770 return;
5771 }
5772
5773 pos = os_strstr(params, "passive=");
5774 if (pos)
5775 wpa_s->manual_scan_passive = !!atoi(pos + 8);
5776
5777 pos = os_strstr(params, "use_id=");
5778 if (pos)
5779 wpa_s->manual_scan_use_id = atoi(pos + 7);
5780
5781 pos = os_strstr(params, "only_new=1");
5782 if (pos)
5783 wpa_s->manual_scan_only_new = 1;
5784 } else {
5785 os_free(wpa_s->manual_scan_freqs);
5786 wpa_s->manual_scan_freqs = NULL;
5787 if (wpa_s->scan_res_handler == scan_only_handler)
5788 wpa_s->scan_res_handler = NULL;
5789 }
5790
5791 if (!wpa_s->sched_scanning && !wpa_s->scanning &&
5792 ((wpa_s->wpa_state <= WPA_SCANNING) ||
5793 (wpa_s->wpa_state == WPA_COMPLETED))) {
5794 wpa_s->normal_scans = 0;
5795 wpa_s->scan_req = MANUAL_SCAN_REQ;
5796 wpa_s->after_wps = 0;
5797 wpa_s->known_wps_freq = 0;
5798 wpa_supplicant_req_scan(wpa_s, 0, 0);
5799 if (wpa_s->manual_scan_use_id) {
5800 wpa_s->manual_scan_id++;
5801 wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
5802 wpa_s->manual_scan_id);
5803 *reply_len = os_snprintf(reply, reply_size, "%u\n",
5804 wpa_s->manual_scan_id);
5805 }
5806 } else if (wpa_s->sched_scanning) {
5807 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to allow requested full scan to proceed");
5808 wpa_supplicant_cancel_sched_scan(wpa_s);
5809 wpa_s->scan_req = MANUAL_SCAN_REQ;
5810 wpa_supplicant_req_scan(wpa_s, 0, 0);
5811 if (wpa_s->manual_scan_use_id) {
5812 wpa_s->manual_scan_id++;
5813 *reply_len = os_snprintf(reply, reply_size, "%u\n",
5814 wpa_s->manual_scan_id);
5815 wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
5816 wpa_s->manual_scan_id);
5817 }
5818 } else {
5819 wpa_printf(MSG_DEBUG, "Ongoing scan action - reject new request");
5820 *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
5821 }
5822}
5823
5824
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005825char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
5826 char *buf, size_t *resp_len)
5827{
5828 char *reply;
5829 const int reply_size = 4096;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005830 int reply_len;
5831
5832 if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005833 os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
5834 if (wpa_debug_show_keys)
5835 wpa_dbg(wpa_s, MSG_DEBUG,
5836 "Control interface command '%s'", buf);
5837 else
5838 wpa_dbg(wpa_s, MSG_DEBUG,
5839 "Control interface command '%s [REMOVED]'",
5840 os_strncmp(buf, WPA_CTRL_RSP,
5841 os_strlen(WPA_CTRL_RSP)) == 0 ?
5842 WPA_CTRL_RSP : "SET_NETWORK");
5843 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
5844 os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0 ||
5845 os_strncmp(buf, "NFC_RX_HANDOVER_SEL", 19) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005846 wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
5847 (const u8 *) buf, os_strlen(buf));
5848 } else {
5849 int level = MSG_DEBUG;
5850 if (os_strcmp(buf, "PING") == 0)
5851 level = MSG_EXCESSIVE;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005852 wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005853 }
5854
5855 reply = os_malloc(reply_size);
5856 if (reply == NULL) {
5857 *resp_len = 1;
5858 return NULL;
5859 }
5860
5861 os_memcpy(reply, "OK\n", 3);
5862 reply_len = 3;
5863
5864 if (os_strcmp(buf, "PING") == 0) {
5865 os_memcpy(reply, "PONG\n", 5);
5866 reply_len = 5;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005867 } else if (os_strcmp(buf, "IFNAME") == 0) {
5868 reply_len = os_strlen(wpa_s->ifname);
5869 os_memcpy(reply, wpa_s->ifname, reply_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005870 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
5871 if (wpa_debug_reopen_file() < 0)
5872 reply_len = -1;
5873 } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
5874 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
5875 } else if (os_strcmp(buf, "MIB") == 0) {
5876 reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
5877 if (reply_len >= 0) {
5878 int res;
5879 res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
5880 reply_size - reply_len);
5881 if (res < 0)
5882 reply_len = -1;
5883 else
5884 reply_len += res;
5885 }
5886 } else if (os_strncmp(buf, "STATUS", 6) == 0) {
5887 reply_len = wpa_supplicant_ctrl_iface_status(
5888 wpa_s, buf + 6, reply, reply_size);
5889 } else if (os_strcmp(buf, "PMKSA") == 0) {
5890 reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
5891 reply_size);
5892 } else if (os_strncmp(buf, "SET ", 4) == 0) {
5893 if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
5894 reply_len = -1;
5895 } else if (os_strncmp(buf, "GET ", 4) == 0) {
5896 reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
5897 reply, reply_size);
5898 } else if (os_strcmp(buf, "LOGON") == 0) {
5899 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
5900 } else if (os_strcmp(buf, "LOGOFF") == 0) {
5901 eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
5902 } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
5903 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
5904 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005905 else
5906 wpas_request_connection(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005907 } else if (os_strcmp(buf, "RECONNECT") == 0) {
5908 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
5909 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005910 else if (wpa_s->disconnected)
5911 wpas_request_connection(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005912#ifdef IEEE8021X_EAPOL
5913 } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
5914 if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
5915 reply_len = -1;
5916#endif /* IEEE8021X_EAPOL */
5917#ifdef CONFIG_PEERKEY
5918 } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
5919 if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
5920 reply_len = -1;
5921#endif /* CONFIG_PEERKEY */
5922#ifdef CONFIG_IEEE80211R
5923 } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
5924 if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
5925 reply_len = -1;
5926#endif /* CONFIG_IEEE80211R */
5927#ifdef CONFIG_WPS
5928 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
5929 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
5930 if (res == -2) {
5931 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
5932 reply_len = 17;
5933 } else if (res)
5934 reply_len = -1;
5935 } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
5936 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
5937 if (res == -2) {
5938 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
5939 reply_len = 17;
5940 } else if (res)
5941 reply_len = -1;
5942 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
5943 reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
5944 reply,
5945 reply_size);
5946 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
5947 reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
5948 wpa_s, buf + 14, reply, reply_size);
5949 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
5950 if (wpas_wps_cancel(wpa_s))
5951 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005952#ifdef CONFIG_WPS_NFC
5953 } else if (os_strcmp(buf, "WPS_NFC") == 0) {
5954 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
5955 reply_len = -1;
5956 } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
5957 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
5958 reply_len = -1;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005959 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
5960 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
5961 wpa_s, buf + 21, reply, reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005962 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
5963 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
5964 wpa_s, buf + 14, reply, reply_size);
5965 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
5966 if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
5967 buf + 17))
5968 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005969 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
5970 reply_len = wpas_ctrl_nfc_get_handover_req(
5971 wpa_s, buf + 21, reply, reply_size);
5972 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
5973 reply_len = wpas_ctrl_nfc_get_handover_sel(
5974 wpa_s, buf + 21, reply, reply_size);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005975 } else if (os_strncmp(buf, "NFC_RX_HANDOVER_SEL ", 20) == 0) {
5976 if (wpas_ctrl_nfc_rx_handover_sel(wpa_s, buf + 20))
5977 reply_len = -1;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005978 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
5979 if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
5980 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005981#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005982 } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
5983 if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
5984 reply_len = -1;
5985#ifdef CONFIG_AP
5986 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
5987 reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
5988 wpa_s, buf + 11, reply, reply_size);
5989#endif /* CONFIG_AP */
5990#ifdef CONFIG_WPS_ER
5991 } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
5992 if (wpas_wps_er_start(wpa_s, NULL))
5993 reply_len = -1;
5994 } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
5995 if (wpas_wps_er_start(wpa_s, buf + 13))
5996 reply_len = -1;
5997 } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
5998 if (wpas_wps_er_stop(wpa_s))
5999 reply_len = -1;
6000 } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
6001 if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
6002 reply_len = -1;
6003 } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
6004 int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
6005 if (ret == -2) {
6006 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
6007 reply_len = 17;
6008 } else if (ret == -3) {
6009 os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
6010 reply_len = 18;
6011 } else if (ret == -4) {
6012 os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
6013 reply_len = 20;
6014 } else if (ret)
6015 reply_len = -1;
6016 } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
6017 if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
6018 reply_len = -1;
6019 } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
6020 if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
6021 buf + 18))
6022 reply_len = -1;
6023 } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
6024 if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
6025 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006026#ifdef CONFIG_WPS_NFC
6027 } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
6028 reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
6029 wpa_s, buf + 24, reply, reply_size);
6030#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006031#endif /* CONFIG_WPS_ER */
6032#endif /* CONFIG_WPS */
6033#ifdef CONFIG_IBSS_RSN
6034 } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
6035 if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
6036 reply_len = -1;
6037#endif /* CONFIG_IBSS_RSN */
6038#ifdef CONFIG_P2P
6039 } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
6040 if (p2p_ctrl_find(wpa_s, buf + 9))
6041 reply_len = -1;
6042 } else if (os_strcmp(buf, "P2P_FIND") == 0) {
6043 if (p2p_ctrl_find(wpa_s, ""))
6044 reply_len = -1;
6045 } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
6046 wpas_p2p_stop_find(wpa_s);
6047 } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
6048 reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
6049 reply_size);
6050 } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
6051 if (p2p_ctrl_listen(wpa_s, buf + 11))
6052 reply_len = -1;
6053 } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
6054 if (p2p_ctrl_listen(wpa_s, ""))
6055 reply_len = -1;
6056 } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
6057 if (wpas_p2p_group_remove(wpa_s, buf + 17))
6058 reply_len = -1;
6059 } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006060 if (wpas_p2p_group_add(wpa_s, 0, 0, 0, 0))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006061 reply_len = -1;
6062 } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
6063 if (p2p_ctrl_group_add(wpa_s, buf + 14))
6064 reply_len = -1;
6065 } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
6066 if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
6067 reply_len = -1;
6068 } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
6069 reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
6070 } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
6071 reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
6072 reply_size);
6073 } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
6074 if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
6075 reply_len = -1;
6076 } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
6077 if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
6078 reply_len = -1;
6079 } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
6080 wpas_p2p_sd_service_update(wpa_s);
6081 } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
6082 if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
6083 reply_len = -1;
6084 } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
6085 wpas_p2p_service_flush(wpa_s);
6086 } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
6087 if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
6088 reply_len = -1;
6089 } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
6090 if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
6091 reply_len = -1;
6092 } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
6093 if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
6094 reply_len = -1;
6095 } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
6096 if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
6097 reply_len = -1;
6098 } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
6099 reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
6100 reply_size);
6101 } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
6102 if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
6103 reply_len = -1;
6104 } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006105 p2p_ctrl_flush(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006106 } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
6107 if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
6108 reply_len = -1;
6109 } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
6110 if (wpas_p2p_cancel(wpa_s))
6111 reply_len = -1;
6112 } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
6113 if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
6114 reply_len = -1;
6115 } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
6116 if (p2p_ctrl_presence_req(wpa_s, "") < 0)
6117 reply_len = -1;
6118 } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
6119 if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
6120 reply_len = -1;
6121 } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
6122 if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
6123 reply_len = -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006124 } else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
6125 if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
6126 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006127#endif /* CONFIG_P2P */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006128#ifdef CONFIG_WIFI_DISPLAY
6129 } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
6130 if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
6131 reply_len = -1;
6132 } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
6133 reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
6134 reply, reply_size);
6135#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006136#ifdef CONFIG_INTERWORKING
6137 } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
6138 if (interworking_fetch_anqp(wpa_s) < 0)
6139 reply_len = -1;
6140 } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
6141 interworking_stop_fetch_anqp(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006142 } else if (os_strcmp(buf, "INTERWORKING_SELECT") == 0) {
6143 if (ctrl_interworking_select(wpa_s, NULL) < 0)
6144 reply_len = -1;
6145 } else if (os_strncmp(buf, "INTERWORKING_SELECT ", 20) == 0) {
6146 if (ctrl_interworking_select(wpa_s, buf + 20) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006147 reply_len = -1;
6148 } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
6149 if (ctrl_interworking_connect(wpa_s, buf + 21) < 0)
6150 reply_len = -1;
6151 } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
6152 if (get_anqp(wpa_s, buf + 9) < 0)
6153 reply_len = -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006154 } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
6155 if (gas_request(wpa_s, buf + 12) < 0)
6156 reply_len = -1;
6157 } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
6158 reply_len = gas_response_get(wpa_s, buf + 17, reply,
6159 reply_size);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006160#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt04949592012-07-19 12:16:46 -07006161#ifdef CONFIG_HS20
6162 } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
6163 if (get_hs20_anqp(wpa_s, buf + 14) < 0)
6164 reply_len = -1;
6165 } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
6166 if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
6167 reply_len = -1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006168 } else if (os_strncmp(buf, "HS20_ICON_REQUEST ", 18) == 0) {
6169 if (hs20_icon_request(wpa_s, buf + 18) < 0)
6170 reply_len = -1;
6171 } else if (os_strcmp(buf, "FETCH_OSU") == 0) {
6172 if (hs20_fetch_osu(wpa_s) < 0)
6173 reply_len = -1;
6174 } else if (os_strcmp(buf, "CANCEL_FETCH_OSU") == 0) {
6175 hs20_cancel_fetch_osu(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006176#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006177 } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
6178 {
6179 if (wpa_supplicant_ctrl_iface_ctrl_rsp(
6180 wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
6181 reply_len = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006182 else {
6183 /*
6184 * Notify response from timeout to allow the control
6185 * interface response to be sent first.
6186 */
6187 eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
6188 wpa_s, NULL);
6189 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006190 } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
6191 if (wpa_supplicant_reload_configuration(wpa_s))
6192 reply_len = -1;
6193 } else if (os_strcmp(buf, "TERMINATE") == 0) {
6194 wpa_supplicant_terminate_proc(wpa_s->global);
6195 } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
6196 if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
6197 reply_len = -1;
Dmitry Shmidte19501d2011-03-16 14:32:18 -07006198 } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006199 reply_len = wpa_supplicant_ctrl_iface_blacklist(
6200 wpa_s, buf + 9, reply, reply_size);
6201 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
6202 reply_len = wpa_supplicant_ctrl_iface_log_level(
6203 wpa_s, buf + 9, reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006204 } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
6205 reply_len = wpa_supplicant_ctrl_iface_list_networks(
6206 wpa_s, reply, reply_size);
6207 } else if (os_strcmp(buf, "DISCONNECT") == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006208#ifdef CONFIG_SME
6209 wpa_s->sme.prev_bssid_set = 0;
6210#endif /* CONFIG_SME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006211 wpa_s->reassociate = 0;
6212 wpa_s->disconnected = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006213 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006214 wpa_supplicant_cancel_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006215 wpa_supplicant_deauthenticate(wpa_s,
6216 WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006217 } else if (os_strcmp(buf, "SCAN") == 0) {
6218 wpas_ctrl_scan(wpa_s, NULL, reply, reply_size, &reply_len);
6219 } else if (os_strncmp(buf, "SCAN ", 5) == 0) {
6220 wpas_ctrl_scan(wpa_s, buf + 5, reply, reply_size, &reply_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006221 } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
6222 reply_len = wpa_supplicant_ctrl_iface_scan_results(
6223 wpa_s, reply, reply_size);
6224 } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
6225 if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
6226 reply_len = -1;
6227 } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
6228 if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
6229 reply_len = -1;
6230 } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
6231 if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
6232 reply_len = -1;
6233 } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
6234 reply_len = wpa_supplicant_ctrl_iface_add_network(
6235 wpa_s, reply, reply_size);
6236 } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
6237 if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
6238 reply_len = -1;
6239 } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
6240 if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
6241 reply_len = -1;
6242 } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
6243 reply_len = wpa_supplicant_ctrl_iface_get_network(
6244 wpa_s, buf + 12, reply, reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006245 } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
6246 reply_len = wpa_supplicant_ctrl_iface_list_creds(
6247 wpa_s, reply, reply_size);
6248 } else if (os_strcmp(buf, "ADD_CRED") == 0) {
6249 reply_len = wpa_supplicant_ctrl_iface_add_cred(
6250 wpa_s, reply, reply_size);
6251 } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
6252 if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
6253 reply_len = -1;
6254 } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
6255 if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
6256 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006257#ifndef CONFIG_NO_CONFIG_WRITE
6258 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
6259 if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
6260 reply_len = -1;
6261#endif /* CONFIG_NO_CONFIG_WRITE */
6262 } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
6263 reply_len = wpa_supplicant_ctrl_iface_get_capability(
6264 wpa_s, buf + 15, reply, reply_size);
6265 } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
6266 if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
6267 reply_len = -1;
6268 } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
6269 if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
6270 reply_len = -1;
6271 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
6272 reply_len = wpa_supplicant_global_iface_list(
6273 wpa_s->global, reply, reply_size);
6274 } else if (os_strcmp(buf, "INTERFACES") == 0) {
6275 reply_len = wpa_supplicant_global_iface_interfaces(
6276 wpa_s->global, reply, reply_size);
6277 } else if (os_strncmp(buf, "BSS ", 4) == 0) {
6278 reply_len = wpa_supplicant_ctrl_iface_bss(
6279 wpa_s, buf + 4, reply, reply_size);
6280#ifdef CONFIG_AP
6281 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
6282 reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
6283 } else if (os_strncmp(buf, "STA ", 4) == 0) {
6284 reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
6285 reply_size);
6286 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
6287 reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
6288 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006289 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
6290 if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
6291 reply_len = -1;
6292 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
6293 if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
6294 reply_len = -1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006295 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
6296 if (ap_ctrl_iface_chanswitch(wpa_s, buf + 12))
6297 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006298#endif /* CONFIG_AP */
6299 } else if (os_strcmp(buf, "SUSPEND") == 0) {
6300 wpas_notify_suspend(wpa_s->global);
6301 } else if (os_strcmp(buf, "RESUME") == 0) {
6302 wpas_notify_resume(wpa_s->global);
6303 } else if (os_strcmp(buf, "DROP_SA") == 0) {
6304 wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
6305 } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
6306 if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
6307 reply_len = -1;
6308 } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
6309 if (wpa_supplicant_ctrl_iface_sta_autoconnect(wpa_s, buf + 16))
6310 reply_len = -1;
6311 } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
6312 if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
6313 reply_len = -1;
6314 } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
6315 if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
6316 buf + 17))
6317 reply_len = -1;
Dmitry Shmidtf48e4f92012-08-24 11:14:44 -07006318 } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
6319 if (wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10))
6320 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006321#ifdef CONFIG_TDLS
6322 } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
6323 if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
6324 reply_len = -1;
6325 } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
6326 if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
6327 reply_len = -1;
6328 } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
6329 if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
6330 reply_len = -1;
6331#endif /* CONFIG_TDLS */
6332 } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
6333 reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
6334 reply_size);
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07006335 } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
6336 reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
6337 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006338#ifdef CONFIG_AUTOSCAN
6339 } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
6340 if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
6341 reply_len = -1;
6342#endif /* CONFIG_AUTOSCAN */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006343#ifdef ANDROID
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07006344 } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
6345 reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
6346 reply_size);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08006347#endif /* ANDROID */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006348 } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006349 pmksa_cache_clear_current(wpa_s->wpa);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006350 eapol_sm_request_reauth(wpa_s->eapol);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006351#ifdef CONFIG_WNM
6352 } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
6353 if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
6354 reply_len = -1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07006355 } else if (os_strncmp(buf, "WNM_BSS_QUERY ", 10) == 0) {
6356 if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 10))
6357 reply_len = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006358#endif /* CONFIG_WNM */
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006359 } else if (os_strcmp(buf, "FLUSH") == 0) {
6360 wpa_supplicant_ctrl_iface_flush(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006361 } else if (os_strncmp(buf, "RADIO_WORK ", 11) == 0) {
6362 reply_len = wpas_ctrl_radio_work(wpa_s, buf + 11, reply,
6363 reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006364 } else {
6365 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
6366 reply_len = 16;
6367 }
6368
6369 if (reply_len < 0) {
6370 os_memcpy(reply, "FAIL\n", 5);
6371 reply_len = 5;
6372 }
6373
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006374 *resp_len = reply_len;
6375 return reply;
6376}
6377
6378
6379static int wpa_supplicant_global_iface_add(struct wpa_global *global,
6380 char *cmd)
6381{
6382 struct wpa_interface iface;
6383 char *pos;
6384
6385 /*
6386 * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
6387 * TAB<bridge_ifname>
6388 */
6389 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
6390
6391 os_memset(&iface, 0, sizeof(iface));
6392
6393 do {
6394 iface.ifname = pos = cmd;
6395 pos = os_strchr(pos, '\t');
6396 if (pos)
6397 *pos++ = '\0';
6398 if (iface.ifname[0] == '\0')
6399 return -1;
6400 if (pos == NULL)
6401 break;
6402
6403 iface.confname = pos;
6404 pos = os_strchr(pos, '\t');
6405 if (pos)
6406 *pos++ = '\0';
6407 if (iface.confname[0] == '\0')
6408 iface.confname = NULL;
6409 if (pos == NULL)
6410 break;
6411
6412 iface.driver = pos;
6413 pos = os_strchr(pos, '\t');
6414 if (pos)
6415 *pos++ = '\0';
6416 if (iface.driver[0] == '\0')
6417 iface.driver = NULL;
6418 if (pos == NULL)
6419 break;
6420
6421 iface.ctrl_interface = pos;
6422 pos = os_strchr(pos, '\t');
6423 if (pos)
6424 *pos++ = '\0';
6425 if (iface.ctrl_interface[0] == '\0')
6426 iface.ctrl_interface = NULL;
6427 if (pos == NULL)
6428 break;
6429
6430 iface.driver_param = pos;
6431 pos = os_strchr(pos, '\t');
6432 if (pos)
6433 *pos++ = '\0';
6434 if (iface.driver_param[0] == '\0')
6435 iface.driver_param = NULL;
6436 if (pos == NULL)
6437 break;
6438
6439 iface.bridge_ifname = pos;
6440 pos = os_strchr(pos, '\t');
6441 if (pos)
6442 *pos++ = '\0';
6443 if (iface.bridge_ifname[0] == '\0')
6444 iface.bridge_ifname = NULL;
6445 if (pos == NULL)
6446 break;
6447 } while (0);
6448
6449 if (wpa_supplicant_get_iface(global, iface.ifname))
6450 return -1;
6451
6452 return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
6453}
6454
6455
6456static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
6457 char *cmd)
6458{
6459 struct wpa_supplicant *wpa_s;
6460
6461 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
6462
6463 wpa_s = wpa_supplicant_get_iface(global, cmd);
6464 if (wpa_s == NULL)
6465 return -1;
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07006466 return wpa_supplicant_remove_iface(global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006467}
6468
6469
6470static void wpa_free_iface_info(struct wpa_interface_info *iface)
6471{
6472 struct wpa_interface_info *prev;
6473
6474 while (iface) {
6475 prev = iface;
6476 iface = iface->next;
6477
6478 os_free(prev->ifname);
6479 os_free(prev->desc);
6480 os_free(prev);
6481 }
6482}
6483
6484
6485static int wpa_supplicant_global_iface_list(struct wpa_global *global,
6486 char *buf, int len)
6487{
6488 int i, res;
6489 struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
6490 char *pos, *end;
6491
6492 for (i = 0; wpa_drivers[i]; i++) {
6493 struct wpa_driver_ops *drv = wpa_drivers[i];
6494 if (drv->get_interfaces == NULL)
6495 continue;
6496 tmp = drv->get_interfaces(global->drv_priv[i]);
6497 if (tmp == NULL)
6498 continue;
6499
6500 if (last == NULL)
6501 iface = last = tmp;
6502 else
6503 last->next = tmp;
6504 while (last->next)
6505 last = last->next;
6506 }
6507
6508 pos = buf;
6509 end = buf + len;
6510 for (tmp = iface; tmp; tmp = tmp->next) {
6511 res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
6512 tmp->drv_name, tmp->ifname,
6513 tmp->desc ? tmp->desc : "");
6514 if (res < 0 || res >= end - pos) {
6515 *pos = '\0';
6516 break;
6517 }
6518 pos += res;
6519 }
6520
6521 wpa_free_iface_info(iface);
6522
6523 return pos - buf;
6524}
6525
6526
6527static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
6528 char *buf, int len)
6529{
6530 int res;
6531 char *pos, *end;
6532 struct wpa_supplicant *wpa_s;
6533
6534 wpa_s = global->ifaces;
6535 pos = buf;
6536 end = buf + len;
6537
6538 while (wpa_s) {
6539 res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
6540 if (res < 0 || res >= end - pos) {
6541 *pos = '\0';
6542 break;
6543 }
6544 pos += res;
6545 wpa_s = wpa_s->next;
6546 }
6547 return pos - buf;
6548}
6549
6550
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006551static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
6552 const char *ifname,
6553 char *cmd, size_t *resp_len)
6554{
6555 struct wpa_supplicant *wpa_s;
6556
6557 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6558 if (os_strcmp(ifname, wpa_s->ifname) == 0)
6559 break;
6560 }
6561
6562 if (wpa_s == NULL) {
6563 char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
6564 if (resp)
6565 *resp_len = os_strlen(resp);
6566 else
6567 *resp_len = 1;
6568 return resp;
6569 }
6570
6571 return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
6572}
6573
6574
6575static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
6576 char *buf, size_t *resp_len)
6577{
6578#ifdef CONFIG_P2P
6579 static const char * cmd[] = {
Dmitry Shmidt0c18dcd2013-08-16 15:29:47 -07006580 "LIST_NETWORKS",
6581 "SAVE_CONFIG",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006582 "P2P_FIND",
6583 "P2P_STOP_FIND",
6584 "P2P_LISTEN",
6585 "P2P_GROUP_ADD",
6586 "P2P_GET_PASSPHRASE",
6587 "P2P_SERVICE_UPDATE",
6588 "P2P_SERVICE_FLUSH",
6589 "P2P_FLUSH",
6590 "P2P_CANCEL",
6591 "P2P_PRESENCE_REQ",
6592 "P2P_EXT_LISTEN",
6593 NULL
6594 };
6595 static const char * prefix[] = {
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08006596#ifdef ANDROID
Dmitry Shmidt0c18dcd2013-08-16 15:29:47 -07006597 "DRIVER ",
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08006598#endif /* ANDROID */
Dmitry Shmidt0c18dcd2013-08-16 15:29:47 -07006599 "GET_NETWORK ",
6600 "REMOVE_NETWORK ",
6601 "SET ",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006602 "P2P_FIND ",
6603 "P2P_CONNECT ",
6604 "P2P_LISTEN ",
6605 "P2P_GROUP_REMOVE ",
6606 "P2P_GROUP_ADD ",
6607 "P2P_PROV_DISC ",
6608 "P2P_SERV_DISC_REQ ",
6609 "P2P_SERV_DISC_CANCEL_REQ ",
6610 "P2P_SERV_DISC_RESP ",
6611 "P2P_SERV_DISC_EXTERNAL ",
6612 "P2P_SERVICE_ADD ",
6613 "P2P_SERVICE_DEL ",
6614 "P2P_REJECT ",
6615 "P2P_INVITE ",
6616 "P2P_PEER ",
6617 "P2P_SET ",
6618 "P2P_UNAUTHORIZE ",
6619 "P2P_PRESENCE_REQ ",
6620 "P2P_EXT_LISTEN ",
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006621 "P2P_REMOVE_CLIENT ",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006622 NULL
6623 };
6624 int found = 0;
6625 int i;
6626
6627 if (global->p2p_init_wpa_s == NULL)
6628 return NULL;
6629
6630 for (i = 0; !found && cmd[i]; i++) {
6631 if (os_strcmp(buf, cmd[i]) == 0)
6632 found = 1;
6633 }
6634
6635 for (i = 0; !found && prefix[i]; i++) {
6636 if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
6637 found = 1;
6638 }
6639
6640 if (found)
6641 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
6642 buf, resp_len);
6643#endif /* CONFIG_P2P */
6644 return NULL;
6645}
6646
6647
6648static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
6649 char *buf, size_t *resp_len)
6650{
6651#ifdef CONFIG_WIFI_DISPLAY
6652 if (global->p2p_init_wpa_s == NULL)
6653 return NULL;
6654 if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
6655 os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
6656 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
6657 buf, resp_len);
6658#endif /* CONFIG_WIFI_DISPLAY */
6659 return NULL;
6660}
6661
6662
6663static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
6664 char *buf, size_t *resp_len)
6665{
6666 char *ret;
6667
6668 ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
6669 if (ret)
6670 return ret;
6671
6672 ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
6673 if (ret)
6674 return ret;
6675
6676 return NULL;
6677}
6678
6679
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006680static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
6681{
6682 char *value;
6683
6684 value = os_strchr(cmd, ' ');
6685 if (value == NULL)
6686 return -1;
6687 *value++ = '\0';
6688
6689 wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
6690
6691#ifdef CONFIG_WIFI_DISPLAY
6692 if (os_strcasecmp(cmd, "wifi_display") == 0) {
6693 wifi_display_enable(global, !!atoi(value));
6694 return 0;
6695 }
6696#endif /* CONFIG_WIFI_DISPLAY */
6697
6698 return -1;
6699}
6700
6701
6702#ifndef CONFIG_NO_CONFIG_WRITE
6703static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
6704{
6705 int ret = 0;
6706 struct wpa_supplicant *wpa_s;
6707
6708 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6709 if (!wpa_s->conf->update_config) {
6710 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
6711 continue;
6712 }
6713
6714 if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
6715 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
6716 ret = 1;
6717 } else {
6718 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
6719 }
6720 }
6721
6722 return ret;
6723}
6724#endif /* CONFIG_NO_CONFIG_WRITE */
6725
6726
6727static int wpas_global_ctrl_iface_status(struct wpa_global *global,
6728 char *buf, size_t buflen)
6729{
6730 char *pos, *end;
6731 int ret;
6732 struct wpa_supplicant *wpa_s;
6733
6734 pos = buf;
6735 end = buf + buflen;
6736
6737#ifdef CONFIG_P2P
6738 if (global->p2p && !global->p2p_disabled) {
6739 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
6740 "\n"
6741 "p2p_state=%s\n",
6742 MAC2STR(global->p2p_dev_addr),
6743 p2p_get_state_txt(global->p2p));
6744 if (ret < 0 || ret >= end - pos)
6745 return pos - buf;
6746 pos += ret;
6747 } else if (global->p2p) {
6748 ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
6749 if (ret < 0 || ret >= end - pos)
6750 return pos - buf;
6751 pos += ret;
6752 }
6753#endif /* CONFIG_P2P */
6754
6755#ifdef CONFIG_WIFI_DISPLAY
6756 ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
6757 !!global->wifi_display);
6758 if (ret < 0 || ret >= end - pos)
6759 return pos - buf;
6760 pos += ret;
6761#endif /* CONFIG_WIFI_DISPLAY */
6762
6763 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6764 ret = os_snprintf(pos, end - pos, "ifname=%s\n"
6765 "address=" MACSTR "\n",
6766 wpa_s->ifname, MAC2STR(wpa_s->own_addr));
6767 if (ret < 0 || ret >= end - pos)
6768 return pos - buf;
6769 pos += ret;
6770 }
6771
6772 return pos - buf;
6773}
6774
6775
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006776char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
6777 char *buf, size_t *resp_len)
6778{
6779 char *reply;
6780 const int reply_size = 2048;
6781 int reply_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006782 int level = MSG_DEBUG;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006783
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006784 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
6785 char *pos = os_strchr(buf + 7, ' ');
6786 if (pos) {
6787 *pos++ = '\0';
6788 return wpas_global_ctrl_iface_ifname(global,
6789 buf + 7, pos,
6790 resp_len);
6791 }
6792 }
6793
6794 reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
6795 if (reply)
6796 return reply;
6797
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006798 if (os_strcmp(buf, "PING") == 0)
6799 level = MSG_EXCESSIVE;
6800 wpa_hexdump_ascii(level, "RX global ctrl_iface",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006801 (const u8 *) buf, os_strlen(buf));
6802
6803 reply = os_malloc(reply_size);
6804 if (reply == NULL) {
6805 *resp_len = 1;
6806 return NULL;
6807 }
6808
6809 os_memcpy(reply, "OK\n", 3);
6810 reply_len = 3;
6811
6812 if (os_strcmp(buf, "PING") == 0) {
6813 os_memcpy(reply, "PONG\n", 5);
6814 reply_len = 5;
6815 } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
6816 if (wpa_supplicant_global_iface_add(global, buf + 14))
6817 reply_len = -1;
6818 } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
6819 if (wpa_supplicant_global_iface_remove(global, buf + 17))
6820 reply_len = -1;
6821 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
6822 reply_len = wpa_supplicant_global_iface_list(
6823 global, reply, reply_size);
6824 } else if (os_strcmp(buf, "INTERFACES") == 0) {
6825 reply_len = wpa_supplicant_global_iface_interfaces(
6826 global, reply, reply_size);
6827 } else if (os_strcmp(buf, "TERMINATE") == 0) {
6828 wpa_supplicant_terminate_proc(global);
6829 } else if (os_strcmp(buf, "SUSPEND") == 0) {
6830 wpas_notify_suspend(global);
6831 } else if (os_strcmp(buf, "RESUME") == 0) {
6832 wpas_notify_resume(global);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006833 } else if (os_strncmp(buf, "SET ", 4) == 0) {
6834 if (wpas_global_ctrl_iface_set(global, buf + 4))
6835 reply_len = -1;
6836#ifndef CONFIG_NO_CONFIG_WRITE
6837 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
6838 if (wpas_global_ctrl_iface_save_config(global))
6839 reply_len = -1;
6840#endif /* CONFIG_NO_CONFIG_WRITE */
6841 } else if (os_strcmp(buf, "STATUS") == 0) {
6842 reply_len = wpas_global_ctrl_iface_status(global, reply,
6843 reply_size);
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08006844#ifdef CONFIG_MODULE_TESTS
6845 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
6846 int wpas_module_tests(void);
6847 if (wpas_module_tests() < 0)
6848 reply_len = -1;
6849#endif /* CONFIG_MODULE_TESTS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006850 } else {
6851 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
6852 reply_len = 16;
6853 }
6854
6855 if (reply_len < 0) {
6856 os_memcpy(reply, "FAIL\n", 5);
6857 reply_len = 5;
6858 }
6859
6860 *resp_len = reply_len;
6861 return reply;
6862}