blob: f7ee6e3a27ad5ebb1d18d6b02db7541a5c820212 [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
57 if (wpa_s->pno)
58 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) {
67 wpa_supplicant_cancel_sched_scan(wpa_s);
68 wpa_supplicant_cancel_scan(wpa_s);
69 }
70
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080071 os_memset(&params, 0, sizeof(params));
72
73 num_ssid = 0;
74 ssid = wpa_s->conf->ssid;
75 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -070076 if (!wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080077 num_ssid++;
78 ssid = ssid->next;
79 }
80 if (num_ssid > WPAS_MAX_SCAN_SSIDS) {
81 wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
82 "%u", WPAS_MAX_SCAN_SSIDS, (unsigned int) num_ssid);
83 num_ssid = WPAS_MAX_SCAN_SSIDS;
84 }
85
86 if (num_ssid == 0) {
87 wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
88 return -1;
89 }
90
91 params.filter_ssids = os_malloc(sizeof(struct wpa_driver_scan_filter) *
92 num_ssid);
93 if (params.filter_ssids == NULL)
94 return -1;
95 i = 0;
96 ssid = wpa_s->conf->ssid;
97 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -070098 if (!wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080099 params.ssids[i].ssid = ssid->ssid;
100 params.ssids[i].ssid_len = ssid->ssid_len;
101 params.num_ssids++;
102 os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
103 ssid->ssid_len);
104 params.filter_ssids[i].ssid_len = ssid->ssid_len;
105 params.num_filter_ssids++;
106 i++;
107 if (i == num_ssid)
108 break;
109 }
110 ssid = ssid->next;
111 }
112
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700113 if (wpa_s->conf->filter_rssi)
114 params.filter_rssi = wpa_s->conf->filter_rssi;
115
Dmitry Shmidt56052862013-10-04 10:23:25 -0700116 interval = wpa_s->conf->sched_scan_interval ?
117 wpa_s->conf->sched_scan_interval : 10;
118
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800119 ret = wpa_supplicant_start_sched_scan(wpa_s, &params, interval);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800120 os_free(params.filter_ssids);
121 if (ret == 0)
122 wpa_s->pno = 1;
123 return ret;
124}
125
126
127static int pno_stop(struct wpa_supplicant *wpa_s)
128{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800129 int ret = 0;
130
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800131 if (wpa_s->pno) {
132 wpa_s->pno = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800133 ret = wpa_supplicant_stop_sched_scan(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800134 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800135
136 if (wpa_s->wpa_state == WPA_SCANNING)
137 wpa_supplicant_req_scan(wpa_s, 0, 0);
138
139 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800140}
141
142
Dmitry Shmidt04949592012-07-19 12:16:46 -0700143static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
144{
145 char *pos;
146 u8 addr[ETH_ALEN], *filter = NULL, *n;
147 size_t count = 0;
148
149 pos = val;
150 while (pos) {
151 if (*pos == '\0')
152 break;
153 if (hwaddr_aton(pos, addr)) {
154 os_free(filter);
155 return -1;
156 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700157 n = os_realloc_array(filter, count + 1, ETH_ALEN);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700158 if (n == NULL) {
159 os_free(filter);
160 return -1;
161 }
162 filter = n;
163 os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
164 count++;
165
166 pos = os_strchr(pos, ' ');
167 if (pos)
168 pos++;
169 }
170
171 wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
172 os_free(wpa_s->bssid_filter);
173 wpa_s->bssid_filter = filter;
174 wpa_s->bssid_filter_count = count;
175
176 return 0;
177}
178
179
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800180static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
181{
182 char *pos;
183 u8 addr[ETH_ALEN], *bssid = NULL, *n;
184 struct wpa_ssid_value *ssid = NULL, *ns;
185 size_t count = 0, ssid_count = 0;
186 struct wpa_ssid *c;
187
188 /*
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800189 * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | ""
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800190 * SSID_SPEC ::= ssid <SSID_HEX>
191 * BSSID_SPEC ::= bssid <BSSID_HEX>
192 */
193
194 pos = val;
195 while (pos) {
196 if (*pos == '\0')
197 break;
198 if (os_strncmp(pos, "bssid ", 6) == 0) {
199 int res;
200 pos += 6;
201 res = hwaddr_aton2(pos, addr);
202 if (res < 0) {
203 os_free(ssid);
204 os_free(bssid);
205 wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
206 "BSSID value '%s'", pos);
207 return -1;
208 }
209 pos += res;
210 n = os_realloc_array(bssid, count + 1, ETH_ALEN);
211 if (n == NULL) {
212 os_free(ssid);
213 os_free(bssid);
214 return -1;
215 }
216 bssid = n;
217 os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
218 count++;
219 } else if (os_strncmp(pos, "ssid ", 5) == 0) {
220 char *end;
221 pos += 5;
222
223 end = pos;
224 while (*end) {
225 if (*end == '\0' || *end == ' ')
226 break;
227 end++;
228 }
229
230 ns = os_realloc_array(ssid, ssid_count + 1,
231 sizeof(struct wpa_ssid_value));
232 if (ns == NULL) {
233 os_free(ssid);
234 os_free(bssid);
235 return -1;
236 }
237 ssid = ns;
238
239 if ((end - pos) & 0x01 || end - pos > 2 * 32 ||
240 hexstr2bin(pos, ssid[ssid_count].ssid,
241 (end - pos) / 2) < 0) {
242 os_free(ssid);
243 os_free(bssid);
244 wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
245 "SSID value '%s'", pos);
246 return -1;
247 }
248 ssid[ssid_count].ssid_len = (end - pos) / 2;
249 wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
250 ssid[ssid_count].ssid,
251 ssid[ssid_count].ssid_len);
252 ssid_count++;
253 pos = end;
254 } else {
255 wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
256 "'%s'", pos);
257 os_free(ssid);
258 os_free(bssid);
259 return -1;
260 }
261
262 pos = os_strchr(pos, ' ');
263 if (pos)
264 pos++;
265 }
266
267 wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
268 os_free(wpa_s->disallow_aps_bssid);
269 wpa_s->disallow_aps_bssid = bssid;
270 wpa_s->disallow_aps_bssid_count = count;
271
272 wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
273 os_free(wpa_s->disallow_aps_ssid);
274 wpa_s->disallow_aps_ssid = ssid;
275 wpa_s->disallow_aps_ssid_count = ssid_count;
276
277 if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
278 return 0;
279
280 c = wpa_s->current_ssid;
281 if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
282 return 0;
283
284 if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
285 !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
286 return 0;
287
288 wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
289 "because current AP was marked disallowed");
290
291#ifdef CONFIG_SME
292 wpa_s->sme.prev_bssid_set = 0;
293#endif /* CONFIG_SME */
294 wpa_s->reassociate = 1;
295 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
296 wpa_supplicant_req_scan(wpa_s, 0, 0);
297
298 return 0;
299}
300
301
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700302static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
303 char *cmd)
304{
305 char *value;
306 int ret = 0;
307
308 value = os_strchr(cmd, ' ');
309 if (value == NULL)
310 return -1;
311 *value++ = '\0';
312
313 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
314 if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
315 eapol_sm_configure(wpa_s->eapol,
316 atoi(value), -1, -1, -1);
317 } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
318 eapol_sm_configure(wpa_s->eapol,
319 -1, atoi(value), -1, -1);
320 } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
321 eapol_sm_configure(wpa_s->eapol,
322 -1, -1, atoi(value), -1);
323 } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
324 eapol_sm_configure(wpa_s->eapol,
325 -1, -1, -1, atoi(value));
326 } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
327 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
328 atoi(value)))
329 ret = -1;
330 } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
331 0) {
332 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
333 atoi(value)))
334 ret = -1;
335 } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
336 if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
337 ret = -1;
338 } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
339 wpa_s->wps_fragment_size = atoi(value);
340#ifdef CONFIG_WPS_TESTING
341 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
342 long int val;
343 val = strtol(value, NULL, 0);
344 if (val < 0 || val > 0xff) {
345 ret = -1;
346 wpa_printf(MSG_DEBUG, "WPS: Invalid "
347 "wps_version_number %ld", val);
348 } else {
349 wps_version_number = val;
350 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
351 "version %u.%u",
352 (wps_version_number & 0xf0) >> 4,
353 wps_version_number & 0x0f);
354 }
355 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
356 wps_testing_dummy_cred = atoi(value);
357 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
358 wps_testing_dummy_cred);
359#endif /* CONFIG_WPS_TESTING */
360 } else if (os_strcasecmp(cmd, "ampdu") == 0) {
361 if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
362 ret = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800363#ifdef CONFIG_TDLS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700364#ifdef CONFIG_TDLS_TESTING
365 } else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
366 extern unsigned int tdls_testing;
367 tdls_testing = strtol(value, NULL, 0);
368 wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
369#endif /* CONFIG_TDLS_TESTING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700370 } else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
371 int disabled = atoi(value);
372 wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
373 if (disabled) {
374 if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
375 ret = -1;
376 } else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
377 ret = -1;
378 wpa_tdls_enable(wpa_s->wpa, !disabled);
379#endif /* CONFIG_TDLS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800380 } else if (os_strcasecmp(cmd, "pno") == 0) {
381 if (atoi(value))
382 ret = pno_start(wpa_s);
383 else
384 ret = pno_stop(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700385 } else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
386 int disabled = atoi(value);
387 if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
388 ret = -1;
389 else if (disabled)
390 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
391 } else if (os_strcasecmp(cmd, "uapsd") == 0) {
392 if (os_strcmp(value, "disable") == 0)
393 wpa_s->set_sta_uapsd = 0;
394 else {
395 int be, bk, vi, vo;
396 char *pos;
397 /* format: BE,BK,VI,VO;max SP Length */
398 be = atoi(value);
399 pos = os_strchr(value, ',');
400 if (pos == NULL)
401 return -1;
402 pos++;
403 bk = atoi(pos);
404 pos = os_strchr(pos, ',');
405 if (pos == NULL)
406 return -1;
407 pos++;
408 vi = atoi(pos);
409 pos = os_strchr(pos, ',');
410 if (pos == NULL)
411 return -1;
412 pos++;
413 vo = atoi(pos);
414 /* ignore max SP Length for now */
415
416 wpa_s->set_sta_uapsd = 1;
417 wpa_s->sta_uapsd = 0;
418 if (be)
419 wpa_s->sta_uapsd |= BIT(0);
420 if (bk)
421 wpa_s->sta_uapsd |= BIT(1);
422 if (vi)
423 wpa_s->sta_uapsd |= BIT(2);
424 if (vo)
425 wpa_s->sta_uapsd |= BIT(3);
426 }
Jouni Malinen21d6bc82012-04-10 16:17:59 -0700427 } else if (os_strcasecmp(cmd, "ps") == 0) {
428 ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700429#ifdef CONFIG_WIFI_DISPLAY
430 } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
431 wifi_display_enable(wpa_s->global, !!atoi(value));
432#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700433 } else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
434 ret = set_bssid_filter(wpa_s, value);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800435 } else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
436 ret = set_disallow_aps(wpa_s, value);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800437 } else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
438 wpa_s->no_keep_alive = !!atoi(value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700439 } else {
440 value[-1] = '=';
441 ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
442 if (ret == 0)
443 wpa_supplicant_update_config(wpa_s);
444 }
445
446 return ret;
447}
448
449
450static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
451 char *cmd, char *buf, size_t buflen)
452{
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700453 int res = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700454
455 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
456
457 if (os_strcmp(cmd, "version") == 0) {
458 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700459 } else if (os_strcasecmp(cmd, "country") == 0) {
460 if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
461 res = os_snprintf(buf, buflen, "%c%c",
462 wpa_s->conf->country[0],
463 wpa_s->conf->country[1]);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700464#ifdef CONFIG_WIFI_DISPLAY
465 } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
466 res = os_snprintf(buf, buflen, "%d",
467 wpa_s->global->wifi_display);
468 if (res < 0 || (unsigned int) res >= buflen)
469 return -1;
470 return res;
471#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700472#ifdef CONFIG_TESTING_GET_GTK
473 } else if (os_strcmp(cmd, "gtk") == 0) {
474 if (wpa_s->last_gtk_len == 0)
475 return -1;
476 res = wpa_snprintf_hex(buf, buflen, wpa_s->last_gtk,
477 wpa_s->last_gtk_len);
478 return res;
479#endif /* CONFIG_TESTING_GET_GTK */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700480 }
481
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700482 if (res < 0 || (unsigned int) res >= buflen)
483 return -1;
484 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700485}
486
487
488#ifdef IEEE8021X_EAPOL
489static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
490 char *addr)
491{
492 u8 bssid[ETH_ALEN];
493 struct wpa_ssid *ssid = wpa_s->current_ssid;
494
495 if (hwaddr_aton(addr, bssid)) {
496 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
497 "'%s'", addr);
498 return -1;
499 }
500
501 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
502 rsn_preauth_deinit(wpa_s->wpa);
503 if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
504 return -1;
505
506 return 0;
507}
508#endif /* IEEE8021X_EAPOL */
509
510
511#ifdef CONFIG_PEERKEY
512/* MLME-STKSTART.request(peer) */
513static int wpa_supplicant_ctrl_iface_stkstart(
514 struct wpa_supplicant *wpa_s, char *addr)
515{
516 u8 peer[ETH_ALEN];
517
518 if (hwaddr_aton(addr, peer)) {
519 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
520 "address '%s'", addr);
521 return -1;
522 }
523
524 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
525 MAC2STR(peer));
526
527 return wpa_sm_stkstart(wpa_s->wpa, peer);
528}
529#endif /* CONFIG_PEERKEY */
530
531
532#ifdef CONFIG_TDLS
533
534static int wpa_supplicant_ctrl_iface_tdls_discover(
535 struct wpa_supplicant *wpa_s, char *addr)
536{
537 u8 peer[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800538 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700539
540 if (hwaddr_aton(addr, peer)) {
541 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
542 "address '%s'", addr);
543 return -1;
544 }
545
546 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
547 MAC2STR(peer));
548
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800549 if (wpa_tdls_is_external_setup(wpa_s->wpa))
550 ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
551 else
552 ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
553
554 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700555}
556
557
558static int wpa_supplicant_ctrl_iface_tdls_setup(
559 struct wpa_supplicant *wpa_s, char *addr)
560{
561 u8 peer[ETH_ALEN];
562 int ret;
563
564 if (hwaddr_aton(addr, peer)) {
565 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
566 "address '%s'", addr);
567 return -1;
568 }
569
570 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
571 MAC2STR(peer));
572
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800573 if ((wpa_s->conf->tdls_external_control) &&
574 wpa_tdls_is_external_setup(wpa_s->wpa))
575 return wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
576
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800577 wpa_tdls_remove(wpa_s->wpa, peer);
578
579 if (wpa_tdls_is_external_setup(wpa_s->wpa))
580 ret = wpa_tdls_start(wpa_s->wpa, peer);
581 else
582 ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800583
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700584 return ret;
585}
586
587
588static int wpa_supplicant_ctrl_iface_tdls_teardown(
589 struct wpa_supplicant *wpa_s, char *addr)
590{
591 u8 peer[ETH_ALEN];
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700592 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700593
594 if (hwaddr_aton(addr, peer)) {
595 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
596 "address '%s'", addr);
597 return -1;
598 }
599
600 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
601 MAC2STR(peer));
602
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800603 if ((wpa_s->conf->tdls_external_control) &&
604 wpa_tdls_is_external_setup(wpa_s->wpa))
605 return wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
606
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700607 if (wpa_tdls_is_external_setup(wpa_s->wpa))
608 ret = wpa_tdls_teardown_link(
609 wpa_s->wpa, peer,
610 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
611 else
612 ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
613
614 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700615}
616
617#endif /* CONFIG_TDLS */
618
619
620#ifdef CONFIG_IEEE80211R
621static int wpa_supplicant_ctrl_iface_ft_ds(
622 struct wpa_supplicant *wpa_s, char *addr)
623{
624 u8 target_ap[ETH_ALEN];
625 struct wpa_bss *bss;
626 const u8 *mdie;
627
628 if (hwaddr_aton(addr, target_ap)) {
629 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
630 "address '%s'", addr);
631 return -1;
632 }
633
634 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
635
636 bss = wpa_bss_get_bssid(wpa_s, target_ap);
637 if (bss)
638 mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
639 else
640 mdie = NULL;
641
642 return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
643}
644#endif /* CONFIG_IEEE80211R */
645
646
647#ifdef CONFIG_WPS
648static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
649 char *cmd)
650{
651 u8 bssid[ETH_ALEN], *_bssid = bssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700652#ifdef CONFIG_P2P
653 u8 p2p_dev_addr[ETH_ALEN];
654#endif /* CONFIG_P2P */
655#ifdef CONFIG_AP
656 u8 *_p2p_dev_addr = NULL;
657#endif /* CONFIG_AP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800658
Dmitry Shmidtc0a8db02013-11-08 11:18:33 -0800659 if (cmd == NULL || os_strcmp(cmd, "any") == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700660 _bssid = NULL;
661#ifdef CONFIG_P2P
662 } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
663 if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
664 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
665 "P2P Device Address '%s'",
666 cmd + 13);
667 return -1;
668 }
669 _p2p_dev_addr = p2p_dev_addr;
670#endif /* CONFIG_P2P */
671 } else if (hwaddr_aton(cmd, bssid)) {
672 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
673 cmd);
674 return -1;
675 }
676
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800677#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700678 if (wpa_s->ap_iface)
679 return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
680#endif /* CONFIG_AP */
681
682 return wpas_wps_start_pbc(wpa_s, _bssid, 0);
683}
684
685
686static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
687 char *cmd, char *buf,
688 size_t buflen)
689{
690 u8 bssid[ETH_ALEN], *_bssid = bssid;
691 char *pin;
692 int ret;
693
694 pin = os_strchr(cmd, ' ');
695 if (pin)
696 *pin++ = '\0';
697
698 if (os_strcmp(cmd, "any") == 0)
699 _bssid = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800700 else if (os_strcmp(cmd, "get") == 0) {
701 ret = wps_generate_pin();
702 goto done;
703 } else if (hwaddr_aton(cmd, bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700704 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
705 cmd);
706 return -1;
707 }
708
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800709#ifdef CONFIG_AP
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800710 if (wpa_s->ap_iface) {
711 int timeout = 0;
712 char *pos;
713
714 if (pin) {
715 pos = os_strchr(pin, ' ');
716 if (pos) {
717 *pos++ = '\0';
718 timeout = atoi(pos);
719 }
720 }
721
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700722 return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800723 buf, buflen, timeout);
724 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700725#endif /* CONFIG_AP */
726
727 if (pin) {
728 ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
729 DEV_PW_DEFAULT);
730 if (ret < 0)
731 return -1;
732 ret = os_snprintf(buf, buflen, "%s", pin);
733 if (ret < 0 || (size_t) ret >= buflen)
734 return -1;
735 return ret;
736 }
737
738 ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
739 if (ret < 0)
740 return -1;
741
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800742done:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700743 /* Return the generated PIN */
744 ret = os_snprintf(buf, buflen, "%08d", ret);
745 if (ret < 0 || (size_t) ret >= buflen)
746 return -1;
747 return ret;
748}
749
750
751static int wpa_supplicant_ctrl_iface_wps_check_pin(
752 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
753{
754 char pin[9];
755 size_t len;
756 char *pos;
757 int ret;
758
759 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
760 (u8 *) cmd, os_strlen(cmd));
761 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
762 if (*pos < '0' || *pos > '9')
763 continue;
764 pin[len++] = *pos;
765 if (len == 9) {
766 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
767 return -1;
768 }
769 }
770 if (len != 4 && len != 8) {
771 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
772 return -1;
773 }
774 pin[len] = '\0';
775
776 if (len == 8) {
777 unsigned int pin_val;
778 pin_val = atoi(pin);
779 if (!wps_pin_valid(pin_val)) {
780 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
781 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
782 if (ret < 0 || (size_t) ret >= buflen)
783 return -1;
784 return ret;
785 }
786 }
787
788 ret = os_snprintf(buf, buflen, "%s", pin);
789 if (ret < 0 || (size_t) ret >= buflen)
790 return -1;
791
792 return ret;
793}
794
795
Dmitry Shmidt04949592012-07-19 12:16:46 -0700796#ifdef CONFIG_WPS_NFC
797
798static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
799 char *cmd)
800{
801 u8 bssid[ETH_ALEN], *_bssid = bssid;
802
803 if (cmd == NULL || cmd[0] == '\0')
804 _bssid = NULL;
805 else if (hwaddr_aton(cmd, bssid))
806 return -1;
807
808 return wpas_wps_start_nfc(wpa_s, _bssid);
809}
810
811
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800812static int wpa_supplicant_ctrl_iface_wps_nfc_config_token(
813 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
814{
815 int ndef;
816 struct wpabuf *buf;
817 int res;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700818 char *pos;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800819
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700820 pos = os_strchr(cmd, ' ');
821 if (pos)
822 *pos++ = '\0';
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800823 if (os_strcmp(cmd, "WPS") == 0)
824 ndef = 0;
825 else if (os_strcmp(cmd, "NDEF") == 0)
826 ndef = 1;
827 else
828 return -1;
829
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700830 buf = wpas_wps_nfc_config_token(wpa_s, ndef, pos);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800831 if (buf == NULL)
832 return -1;
833
834 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
835 wpabuf_len(buf));
836 reply[res++] = '\n';
837 reply[res] = '\0';
838
839 wpabuf_free(buf);
840
841 return res;
842}
843
844
Dmitry Shmidt04949592012-07-19 12:16:46 -0700845static int wpa_supplicant_ctrl_iface_wps_nfc_token(
846 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
847{
848 int ndef;
849 struct wpabuf *buf;
850 int res;
851
852 if (os_strcmp(cmd, "WPS") == 0)
853 ndef = 0;
854 else if (os_strcmp(cmd, "NDEF") == 0)
855 ndef = 1;
856 else
857 return -1;
858
859 buf = wpas_wps_nfc_token(wpa_s, ndef);
860 if (buf == NULL)
861 return -1;
862
863 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
864 wpabuf_len(buf));
865 reply[res++] = '\n';
866 reply[res] = '\0';
867
868 wpabuf_free(buf);
869
870 return res;
871}
872
873
874static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
875 struct wpa_supplicant *wpa_s, char *pos)
876{
877 size_t len;
878 struct wpabuf *buf;
879 int ret;
880
881 len = os_strlen(pos);
882 if (len & 0x01)
883 return -1;
884 len /= 2;
885
886 buf = wpabuf_alloc(len);
887 if (buf == NULL)
888 return -1;
889 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
890 wpabuf_free(buf);
891 return -1;
892 }
893
894 ret = wpas_wps_nfc_tag_read(wpa_s, buf);
895 wpabuf_free(buf);
896
897 return ret;
898}
899
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800900
901static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800902 char *reply, size_t max_len,
903 int cr)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800904{
905 struct wpabuf *buf;
906 int res;
907
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800908 buf = wpas_wps_nfc_handover_req(wpa_s, cr);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800909 if (buf == NULL)
910 return -1;
911
912 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
913 wpabuf_len(buf));
914 reply[res++] = '\n';
915 reply[res] = '\0';
916
917 wpabuf_free(buf);
918
919 return res;
920}
921
922
923static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
924 char *cmd, char *reply,
925 size_t max_len)
926{
927 char *pos;
928
929 pos = os_strchr(cmd, ' ');
930 if (pos == NULL)
931 return -1;
932 *pos++ = '\0';
933
934 if (os_strcmp(cmd, "NDEF") != 0)
935 return -1;
936
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800937 if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
938 return wpas_ctrl_nfc_get_handover_req_wps(
939 wpa_s, reply, max_len, os_strcmp(pos, "WPS-CR") == 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800940 }
941
942 return -1;
943}
944
945
946static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800947 char *reply, size_t max_len,
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -0800948 int ndef, int cr, char *uuid)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800949{
950 struct wpabuf *buf;
951 int res;
952
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -0800953 buf = wpas_wps_nfc_handover_sel(wpa_s, ndef, cr, uuid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800954 if (buf == NULL)
955 return -1;
956
957 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
958 wpabuf_len(buf));
959 reply[res++] = '\n';
960 reply[res] = '\0';
961
962 wpabuf_free(buf);
963
964 return res;
965}
966
967
968static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
969 char *cmd, char *reply,
970 size_t max_len)
971{
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -0800972 char *pos, *pos2;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800973 int ndef;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800974
975 pos = os_strchr(cmd, ' ');
976 if (pos == NULL)
977 return -1;
978 *pos++ = '\0';
979
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800980 if (os_strcmp(cmd, "WPS") == 0)
981 ndef = 0;
982 else if (os_strcmp(cmd, "NDEF") == 0)
983 ndef = 1;
984 else
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800985 return -1;
986
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -0800987 pos2 = os_strchr(pos, ' ');
988 if (pos2)
989 *pos2++ = '\0';
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800990 if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
991 return wpas_ctrl_nfc_get_handover_sel_wps(
992 wpa_s, reply, max_len, ndef,
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -0800993 os_strcmp(pos, "WPS-CR") == 0, pos2);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800994 }
995
996 return -1;
997}
998
999
1000static int wpas_ctrl_nfc_rx_handover_req(struct wpa_supplicant *wpa_s,
1001 char *cmd, char *reply,
1002 size_t max_len)
1003{
1004 size_t len;
1005 struct wpabuf *buf;
1006 int ret;
1007
1008 len = os_strlen(cmd);
1009 if (len & 0x01)
1010 return -1;
1011 len /= 2;
1012
1013 buf = wpabuf_alloc(len);
1014 if (buf == NULL)
1015 return -1;
1016 if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
1017 wpabuf_free(buf);
1018 return -1;
1019 }
1020
1021 ret = wpas_wps_nfc_rx_handover_req(wpa_s, buf);
1022 wpabuf_free(buf);
1023
1024 return ret;
1025}
1026
1027
1028static int wpas_ctrl_nfc_rx_handover_sel(struct wpa_supplicant *wpa_s,
1029 char *cmd)
1030{
1031 size_t len;
1032 struct wpabuf *buf;
1033 int ret;
1034
1035 len = os_strlen(cmd);
1036 if (len & 0x01)
1037 return -1;
1038 len /= 2;
1039
1040 buf = wpabuf_alloc(len);
1041 if (buf == NULL)
1042 return -1;
1043 if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
1044 wpabuf_free(buf);
1045 return -1;
1046 }
1047
1048 ret = wpas_wps_nfc_rx_handover_sel(wpa_s, buf);
1049 wpabuf_free(buf);
1050
1051 return ret;
1052}
1053
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001054
1055static int wpas_ctrl_nfc_report_handover(struct wpa_supplicant *wpa_s,
1056 char *cmd)
1057{
1058 size_t len;
1059 struct wpabuf *req, *sel;
1060 int ret;
1061 char *pos, *role, *type, *pos2;
1062
1063 role = cmd;
1064 pos = os_strchr(role, ' ');
1065 if (pos == NULL)
1066 return -1;
1067 *pos++ = '\0';
1068
1069 type = pos;
1070 pos = os_strchr(type, ' ');
1071 if (pos == NULL)
1072 return -1;
1073 *pos++ = '\0';
1074
1075 pos2 = os_strchr(pos, ' ');
1076 if (pos2 == NULL)
1077 return -1;
1078 *pos2++ = '\0';
1079
1080 len = os_strlen(pos);
1081 if (len & 0x01)
1082 return -1;
1083 len /= 2;
1084
1085 req = wpabuf_alloc(len);
1086 if (req == NULL)
1087 return -1;
1088 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
1089 wpabuf_free(req);
1090 return -1;
1091 }
1092
1093 len = os_strlen(pos2);
1094 if (len & 0x01) {
1095 wpabuf_free(req);
1096 return -1;
1097 }
1098 len /= 2;
1099
1100 sel = wpabuf_alloc(len);
1101 if (sel == NULL) {
1102 wpabuf_free(req);
1103 return -1;
1104 }
1105 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
1106 wpabuf_free(req);
1107 wpabuf_free(sel);
1108 return -1;
1109 }
1110
1111 if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "WPS") == 0) {
1112 ret = wpas_wps_nfc_report_handover(wpa_s, req, sel);
1113 } else {
1114 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
1115 "reported: role=%s type=%s", role, type);
1116 ret = -1;
1117 }
1118 wpabuf_free(req);
1119 wpabuf_free(sel);
1120
1121 return ret;
1122}
1123
Dmitry Shmidt04949592012-07-19 12:16:46 -07001124#endif /* CONFIG_WPS_NFC */
1125
1126
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001127static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
1128 char *cmd)
1129{
1130 u8 bssid[ETH_ALEN];
1131 char *pin;
1132 char *new_ssid;
1133 char *new_auth;
1134 char *new_encr;
1135 char *new_key;
1136 struct wps_new_ap_settings ap;
1137
1138 pin = os_strchr(cmd, ' ');
1139 if (pin == NULL)
1140 return -1;
1141 *pin++ = '\0';
1142
1143 if (hwaddr_aton(cmd, bssid)) {
1144 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
1145 cmd);
1146 return -1;
1147 }
1148
1149 new_ssid = os_strchr(pin, ' ');
1150 if (new_ssid == NULL)
1151 return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
1152 *new_ssid++ = '\0';
1153
1154 new_auth = os_strchr(new_ssid, ' ');
1155 if (new_auth == NULL)
1156 return -1;
1157 *new_auth++ = '\0';
1158
1159 new_encr = os_strchr(new_auth, ' ');
1160 if (new_encr == NULL)
1161 return -1;
1162 *new_encr++ = '\0';
1163
1164 new_key = os_strchr(new_encr, ' ');
1165 if (new_key == NULL)
1166 return -1;
1167 *new_key++ = '\0';
1168
1169 os_memset(&ap, 0, sizeof(ap));
1170 ap.ssid_hex = new_ssid;
1171 ap.auth = new_auth;
1172 ap.encr = new_encr;
1173 ap.key_hex = new_key;
1174 return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
1175}
1176
1177
1178#ifdef CONFIG_AP
1179static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
1180 char *cmd, char *buf,
1181 size_t buflen)
1182{
1183 int timeout = 300;
1184 char *pos;
1185 const char *pin_txt;
1186
1187 if (!wpa_s->ap_iface)
1188 return -1;
1189
1190 pos = os_strchr(cmd, ' ');
1191 if (pos)
1192 *pos++ = '\0';
1193
1194 if (os_strcmp(cmd, "disable") == 0) {
1195 wpas_wps_ap_pin_disable(wpa_s);
1196 return os_snprintf(buf, buflen, "OK\n");
1197 }
1198
1199 if (os_strcmp(cmd, "random") == 0) {
1200 if (pos)
1201 timeout = atoi(pos);
1202 pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
1203 if (pin_txt == NULL)
1204 return -1;
1205 return os_snprintf(buf, buflen, "%s", pin_txt);
1206 }
1207
1208 if (os_strcmp(cmd, "get") == 0) {
1209 pin_txt = wpas_wps_ap_pin_get(wpa_s);
1210 if (pin_txt == NULL)
1211 return -1;
1212 return os_snprintf(buf, buflen, "%s", pin_txt);
1213 }
1214
1215 if (os_strcmp(cmd, "set") == 0) {
1216 char *pin;
1217 if (pos == NULL)
1218 return -1;
1219 pin = pos;
1220 pos = os_strchr(pos, ' ');
1221 if (pos) {
1222 *pos++ = '\0';
1223 timeout = atoi(pos);
1224 }
1225 if (os_strlen(pin) > buflen)
1226 return -1;
1227 if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
1228 return -1;
1229 return os_snprintf(buf, buflen, "%s", pin);
1230 }
1231
1232 return -1;
1233}
1234#endif /* CONFIG_AP */
1235
1236
1237#ifdef CONFIG_WPS_ER
1238static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
1239 char *cmd)
1240{
1241 char *uuid = cmd, *pin, *pos;
1242 u8 addr_buf[ETH_ALEN], *addr = NULL;
1243 pin = os_strchr(uuid, ' ');
1244 if (pin == NULL)
1245 return -1;
1246 *pin++ = '\0';
1247 pos = os_strchr(pin, ' ');
1248 if (pos) {
1249 *pos++ = '\0';
1250 if (hwaddr_aton(pos, addr_buf) == 0)
1251 addr = addr_buf;
1252 }
1253 return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
1254}
1255
1256
1257static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
1258 char *cmd)
1259{
1260 char *uuid = cmd, *pin;
1261 pin = os_strchr(uuid, ' ');
1262 if (pin == NULL)
1263 return -1;
1264 *pin++ = '\0';
1265 return wpas_wps_er_learn(wpa_s, uuid, pin);
1266}
1267
1268
1269static int wpa_supplicant_ctrl_iface_wps_er_set_config(
1270 struct wpa_supplicant *wpa_s, char *cmd)
1271{
1272 char *uuid = cmd, *id;
1273 id = os_strchr(uuid, ' ');
1274 if (id == NULL)
1275 return -1;
1276 *id++ = '\0';
1277 return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
1278}
1279
1280
1281static int wpa_supplicant_ctrl_iface_wps_er_config(
1282 struct wpa_supplicant *wpa_s, char *cmd)
1283{
1284 char *pin;
1285 char *new_ssid;
1286 char *new_auth;
1287 char *new_encr;
1288 char *new_key;
1289 struct wps_new_ap_settings ap;
1290
1291 pin = os_strchr(cmd, ' ');
1292 if (pin == NULL)
1293 return -1;
1294 *pin++ = '\0';
1295
1296 new_ssid = os_strchr(pin, ' ');
1297 if (new_ssid == NULL)
1298 return -1;
1299 *new_ssid++ = '\0';
1300
1301 new_auth = os_strchr(new_ssid, ' ');
1302 if (new_auth == NULL)
1303 return -1;
1304 *new_auth++ = '\0';
1305
1306 new_encr = os_strchr(new_auth, ' ');
1307 if (new_encr == NULL)
1308 return -1;
1309 *new_encr++ = '\0';
1310
1311 new_key = os_strchr(new_encr, ' ');
1312 if (new_key == NULL)
1313 return -1;
1314 *new_key++ = '\0';
1315
1316 os_memset(&ap, 0, sizeof(ap));
1317 ap.ssid_hex = new_ssid;
1318 ap.auth = new_auth;
1319 ap.encr = new_encr;
1320 ap.key_hex = new_key;
1321 return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
1322}
Dmitry Shmidt04949592012-07-19 12:16:46 -07001323
1324
1325#ifdef CONFIG_WPS_NFC
1326static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
1327 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1328{
1329 int ndef;
1330 struct wpabuf *buf;
1331 int res;
1332 char *uuid;
1333
1334 uuid = os_strchr(cmd, ' ');
1335 if (uuid == NULL)
1336 return -1;
1337 *uuid++ = '\0';
1338
1339 if (os_strcmp(cmd, "WPS") == 0)
1340 ndef = 0;
1341 else if (os_strcmp(cmd, "NDEF") == 0)
1342 ndef = 1;
1343 else
1344 return -1;
1345
1346 buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
1347 if (buf == NULL)
1348 return -1;
1349
1350 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1351 wpabuf_len(buf));
1352 reply[res++] = '\n';
1353 reply[res] = '\0';
1354
1355 wpabuf_free(buf);
1356
1357 return res;
1358}
1359#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001360#endif /* CONFIG_WPS_ER */
1361
1362#endif /* CONFIG_WPS */
1363
1364
1365#ifdef CONFIG_IBSS_RSN
1366static int wpa_supplicant_ctrl_iface_ibss_rsn(
1367 struct wpa_supplicant *wpa_s, char *addr)
1368{
1369 u8 peer[ETH_ALEN];
1370
1371 if (hwaddr_aton(addr, peer)) {
1372 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
1373 "address '%s'", addr);
1374 return -1;
1375 }
1376
1377 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
1378 MAC2STR(peer));
1379
1380 return ibss_rsn_start(wpa_s->ibss_rsn, peer);
1381}
1382#endif /* CONFIG_IBSS_RSN */
1383
1384
1385static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
1386 char *rsp)
1387{
1388#ifdef IEEE8021X_EAPOL
1389 char *pos, *id_pos;
1390 int id;
1391 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001392
1393 pos = os_strchr(rsp, '-');
1394 if (pos == NULL)
1395 return -1;
1396 *pos++ = '\0';
1397 id_pos = pos;
1398 pos = os_strchr(pos, ':');
1399 if (pos == NULL)
1400 return -1;
1401 *pos++ = '\0';
1402 id = atoi(id_pos);
1403 wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
1404 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
1405 (u8 *) pos, os_strlen(pos));
1406
1407 ssid = wpa_config_get_network(wpa_s->conf, id);
1408 if (ssid == NULL) {
1409 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
1410 "to update", id);
1411 return -1;
1412 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001413
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001414 return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
1415 pos);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001416#else /* IEEE8021X_EAPOL */
1417 wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
1418 return -1;
1419#endif /* IEEE8021X_EAPOL */
1420}
1421
1422
1423static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
1424 const char *params,
1425 char *buf, size_t buflen)
1426{
1427 char *pos, *end, tmp[30];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001428 int res, verbose, wps, ret;
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001429
Dmitry Shmidt56052862013-10-04 10:23:25 -07001430 if (os_strcmp(params, "-DRIVER") == 0)
1431 return wpa_drv_status(wpa_s, buf, buflen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001432 verbose = os_strcmp(params, "-VERBOSE") == 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001433 wps = os_strcmp(params, "-WPS") == 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001434 pos = buf;
1435 end = buf + buflen;
1436 if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1437 struct wpa_ssid *ssid = wpa_s->current_ssid;
1438 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
1439 MAC2STR(wpa_s->bssid));
1440 if (ret < 0 || ret >= end - pos)
1441 return pos - buf;
1442 pos += ret;
1443 if (ssid) {
1444 u8 *_ssid = ssid->ssid;
1445 size_t ssid_len = ssid->ssid_len;
1446 u8 ssid_buf[MAX_SSID_LEN];
1447 if (ssid_len == 0) {
1448 int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
1449 if (_res < 0)
1450 ssid_len = 0;
1451 else
1452 ssid_len = _res;
1453 _ssid = ssid_buf;
1454 }
1455 ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
1456 wpa_ssid_txt(_ssid, ssid_len),
1457 ssid->id);
1458 if (ret < 0 || ret >= end - pos)
1459 return pos - buf;
1460 pos += ret;
1461
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001462 if (wps && ssid->passphrase &&
1463 wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
1464 (ssid->mode == WPAS_MODE_AP ||
1465 ssid->mode == WPAS_MODE_P2P_GO)) {
1466 ret = os_snprintf(pos, end - pos,
1467 "passphrase=%s\n",
1468 ssid->passphrase);
1469 if (ret < 0 || ret >= end - pos)
1470 return pos - buf;
1471 pos += ret;
1472 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001473 if (ssid->id_str) {
1474 ret = os_snprintf(pos, end - pos,
1475 "id_str=%s\n",
1476 ssid->id_str);
1477 if (ret < 0 || ret >= end - pos)
1478 return pos - buf;
1479 pos += ret;
1480 }
1481
1482 switch (ssid->mode) {
1483 case WPAS_MODE_INFRA:
1484 ret = os_snprintf(pos, end - pos,
1485 "mode=station\n");
1486 break;
1487 case WPAS_MODE_IBSS:
1488 ret = os_snprintf(pos, end - pos,
1489 "mode=IBSS\n");
1490 break;
1491 case WPAS_MODE_AP:
1492 ret = os_snprintf(pos, end - pos,
1493 "mode=AP\n");
1494 break;
1495 case WPAS_MODE_P2P_GO:
1496 ret = os_snprintf(pos, end - pos,
1497 "mode=P2P GO\n");
1498 break;
1499 case WPAS_MODE_P2P_GROUP_FORMATION:
1500 ret = os_snprintf(pos, end - pos,
1501 "mode=P2P GO - group "
1502 "formation\n");
1503 break;
1504 default:
1505 ret = 0;
1506 break;
1507 }
1508 if (ret < 0 || ret >= end - pos)
1509 return pos - buf;
1510 pos += ret;
1511 }
1512
1513#ifdef CONFIG_AP
1514 if (wpa_s->ap_iface) {
1515 pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
1516 end - pos,
1517 verbose);
1518 } else
1519#endif /* CONFIG_AP */
1520 pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
1521 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001522#ifdef CONFIG_SAE
1523 if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001524#ifdef CONFIG_AP
1525 !wpa_s->ap_iface &&
1526#endif /* CONFIG_AP */
1527 wpa_s->sme.sae.state == SAE_ACCEPTED) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001528 ret = os_snprintf(pos, end - pos, "sae_group=%d\n",
1529 wpa_s->sme.sae.group);
1530 if (ret < 0 || ret >= end - pos)
1531 return pos - buf;
1532 pos += ret;
1533 }
1534#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001535 ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
1536 wpa_supplicant_state_txt(wpa_s->wpa_state));
1537 if (ret < 0 || ret >= end - pos)
1538 return pos - buf;
1539 pos += ret;
1540
1541 if (wpa_s->l2 &&
1542 l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
1543 ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
1544 if (ret < 0 || ret >= end - pos)
1545 return pos - buf;
1546 pos += ret;
1547 }
1548
1549#ifdef CONFIG_P2P
1550 if (wpa_s->global->p2p) {
1551 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
1552 "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
1553 if (ret < 0 || ret >= end - pos)
1554 return pos - buf;
1555 pos += ret;
1556 }
1557#endif /* CONFIG_P2P */
1558
1559 ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
1560 MAC2STR(wpa_s->own_addr));
1561 if (ret < 0 || ret >= end - pos)
1562 return pos - buf;
1563 pos += ret;
1564
Dmitry Shmidt04949592012-07-19 12:16:46 -07001565#ifdef CONFIG_HS20
1566 if (wpa_s->current_bss &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001567 wpa_bss_get_vendor_ie(wpa_s->current_bss, HS20_IE_VENDOR_TYPE) &&
1568 wpa_s->wpa_proto == WPA_PROTO_RSN &&
1569 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001570 ret = os_snprintf(pos, end - pos, "hs20=1\n");
1571 if (ret < 0 || ret >= end - pos)
1572 return pos - buf;
1573 pos += ret;
1574 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001575
1576 if (wpa_s->current_ssid) {
1577 struct wpa_cred *cred;
1578 char *type;
1579
1580 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001581 size_t i;
1582
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001583 if (wpa_s->current_ssid->parent_cred != cred)
1584 continue;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001585
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001586 for (i = 0; cred->domain && i < cred->num_domain; i++) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001587 ret = os_snprintf(pos, end - pos,
1588 "home_sp=%s\n",
1589 cred->domain[i]);
1590 if (ret < 0 || ret >= end - pos)
1591 return pos - buf;
1592 pos += ret;
1593 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001594
1595 if (wpa_s->current_bss == NULL ||
1596 wpa_s->current_bss->anqp == NULL)
1597 res = -1;
1598 else
1599 res = interworking_home_sp_cred(
1600 wpa_s, cred,
1601 wpa_s->current_bss->anqp->domain_name);
1602 if (res > 0)
1603 type = "home";
1604 else if (res == 0)
1605 type = "roaming";
1606 else
1607 type = "unknown";
1608
1609 ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
1610 if (ret < 0 || ret >= end - pos)
1611 return pos - buf;
1612 pos += ret;
1613
1614 break;
1615 }
1616 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001617#endif /* CONFIG_HS20 */
1618
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001619 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
1620 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1621 res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
1622 verbose);
1623 if (res >= 0)
1624 pos += res;
1625 }
1626
1627 res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
1628 if (res >= 0)
1629 pos += res;
1630
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001631#ifdef CONFIG_WPS
1632 {
1633 char uuid_str[100];
1634 uuid_bin2str(wpa_s->wps->uuid, uuid_str, sizeof(uuid_str));
1635 ret = os_snprintf(pos, end - pos, "uuid=%s\n", uuid_str);
1636 if (ret < 0 || ret >= end - pos)
1637 return pos - buf;
1638 pos += ret;
1639 }
1640#endif /* CONFIG_WPS */
1641
Dmitry Shmidt2fd7fa62011-12-19 11:19:09 -08001642#ifdef ANDROID
1643 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
Irfan Sherifff20a4432012-04-16 16:48:34 -07001644 "id=%d state=%d BSSID=" MACSTR " SSID=%s",
Dmitry Shmidt2fd7fa62011-12-19 11:19:09 -08001645 wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
Irfan Sherifff20a4432012-04-16 16:48:34 -07001646 wpa_s->wpa_state,
Irfan Sheriffe78e7672012-08-01 11:10:15 -07001647 MAC2STR(wpa_s->bssid),
andy2_kuo5b5fb022012-05-22 11:53:07 -07001648 wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
1649 wpa_ssid_txt(wpa_s->current_ssid->ssid,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001650 wpa_s->current_ssid->ssid_len) : "");
Irfan Sheriffbf5edf42012-01-11 16:54:57 -08001651 if (wpa_s->wpa_state == WPA_COMPLETED) {
1652 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001653 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED
1654 "- connection to " MACSTR
1655 " completed %s [id=%d id_str=%s]",
1656 MAC2STR(wpa_s->bssid), "(auth)",
1657 ssid ? ssid->id : -1,
1658 ssid && ssid->id_str ? ssid->id_str : "");
Irfan Sheriffbf5edf42012-01-11 16:54:57 -08001659 }
Dmitry Shmidt2fd7fa62011-12-19 11:19:09 -08001660#endif /* ANDROID */
1661
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001662 return pos - buf;
1663}
1664
1665
1666static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
1667 char *cmd)
1668{
1669 char *pos;
1670 int id;
1671 struct wpa_ssid *ssid;
1672 u8 bssid[ETH_ALEN];
1673
1674 /* cmd: "<network id> <BSSID>" */
1675 pos = os_strchr(cmd, ' ');
1676 if (pos == NULL)
1677 return -1;
1678 *pos++ = '\0';
1679 id = atoi(cmd);
1680 wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
1681 if (hwaddr_aton(pos, bssid)) {
1682 wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
1683 return -1;
1684 }
1685
1686 ssid = wpa_config_get_network(wpa_s->conf, id);
1687 if (ssid == NULL) {
1688 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
1689 "to update", id);
1690 return -1;
1691 }
1692
1693 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
1694 ssid->bssid_set = !is_zero_ether_addr(bssid);
1695
1696 return 0;
1697}
1698
1699
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001700static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001701 char *cmd, char *buf,
1702 size_t buflen)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001703{
1704 u8 bssid[ETH_ALEN];
1705 struct wpa_blacklist *e;
1706 char *pos, *end;
1707 int ret;
1708
1709 /* cmd: "BLACKLIST [<BSSID>]" */
1710 if (*cmd == '\0') {
1711 pos = buf;
1712 end = buf + buflen;
1713 e = wpa_s->blacklist;
1714 while (e) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001715 ret = os_snprintf(pos, end - pos, MACSTR "\n",
1716 MAC2STR(e->bssid));
1717 if (ret < 0 || ret >= end - pos)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001718 return pos - buf;
1719 pos += ret;
1720 e = e->next;
1721 }
1722 return pos - buf;
1723 }
1724
1725 cmd++;
1726 if (os_strncmp(cmd, "clear", 5) == 0) {
1727 wpa_blacklist_clear(wpa_s);
1728 os_memcpy(buf, "OK\n", 3);
1729 return 3;
1730 }
1731
1732 wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
1733 if (hwaddr_aton(cmd, bssid)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001734 wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001735 return -1;
1736 }
1737
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001738 /*
1739 * Add the BSSID twice, so its count will be 2, causing it to be
1740 * skipped when processing scan results.
1741 */
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001742 ret = wpa_blacklist_add(wpa_s, bssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001743 if (ret != 0)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001744 return -1;
1745 ret = wpa_blacklist_add(wpa_s, bssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001746 if (ret != 0)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001747 return -1;
1748 os_memcpy(buf, "OK\n", 3);
1749 return 3;
1750}
1751
1752
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001753static const char * debug_level_str(int level)
1754{
1755 switch (level) {
1756 case MSG_EXCESSIVE:
1757 return "EXCESSIVE";
1758 case MSG_MSGDUMP:
1759 return "MSGDUMP";
1760 case MSG_DEBUG:
1761 return "DEBUG";
1762 case MSG_INFO:
1763 return "INFO";
1764 case MSG_WARNING:
1765 return "WARNING";
1766 case MSG_ERROR:
1767 return "ERROR";
1768 default:
1769 return "?";
1770 }
1771}
1772
1773
1774static int str_to_debug_level(const char *s)
1775{
1776 if (os_strcasecmp(s, "EXCESSIVE") == 0)
1777 return MSG_EXCESSIVE;
1778 if (os_strcasecmp(s, "MSGDUMP") == 0)
1779 return MSG_MSGDUMP;
1780 if (os_strcasecmp(s, "DEBUG") == 0)
1781 return MSG_DEBUG;
1782 if (os_strcasecmp(s, "INFO") == 0)
1783 return MSG_INFO;
1784 if (os_strcasecmp(s, "WARNING") == 0)
1785 return MSG_WARNING;
1786 if (os_strcasecmp(s, "ERROR") == 0)
1787 return MSG_ERROR;
1788 return -1;
1789}
1790
1791
1792static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
1793 char *cmd, char *buf,
1794 size_t buflen)
1795{
1796 char *pos, *end, *stamp;
1797 int ret;
1798
1799 if (cmd == NULL) {
1800 return -1;
1801 }
1802
1803 /* cmd: "LOG_LEVEL [<level>]" */
1804 if (*cmd == '\0') {
1805 pos = buf;
1806 end = buf + buflen;
1807 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
1808 "Timestamp: %d\n",
1809 debug_level_str(wpa_debug_level),
1810 wpa_debug_timestamp);
1811 if (ret < 0 || ret >= end - pos)
1812 ret = 0;
1813
1814 return ret;
1815 }
1816
1817 while (*cmd == ' ')
1818 cmd++;
1819
1820 stamp = os_strchr(cmd, ' ');
1821 if (stamp) {
1822 *stamp++ = '\0';
1823 while (*stamp == ' ') {
1824 stamp++;
1825 }
1826 }
1827
1828 if (cmd && os_strlen(cmd)) {
1829 int level = str_to_debug_level(cmd);
1830 if (level < 0)
1831 return -1;
1832 wpa_debug_level = level;
1833 }
1834
1835 if (stamp && os_strlen(stamp))
1836 wpa_debug_timestamp = atoi(stamp);
1837
1838 os_memcpy(buf, "OK\n", 3);
1839 return 3;
1840}
1841
1842
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001843static int wpa_supplicant_ctrl_iface_list_networks(
1844 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
1845{
1846 char *pos, *end;
1847 struct wpa_ssid *ssid;
1848 int ret;
1849
1850 pos = buf;
1851 end = buf + buflen;
1852 ret = os_snprintf(pos, end - pos,
1853 "network id / ssid / bssid / flags\n");
1854 if (ret < 0 || ret >= end - pos)
1855 return pos - buf;
1856 pos += ret;
1857
1858 ssid = wpa_s->conf->ssid;
1859 while (ssid) {
1860 ret = os_snprintf(pos, end - pos, "%d\t%s",
1861 ssid->id,
1862 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1863 if (ret < 0 || ret >= end - pos)
1864 return pos - buf;
1865 pos += ret;
1866 if (ssid->bssid_set) {
1867 ret = os_snprintf(pos, end - pos, "\t" MACSTR,
1868 MAC2STR(ssid->bssid));
1869 } else {
1870 ret = os_snprintf(pos, end - pos, "\tany");
1871 }
1872 if (ret < 0 || ret >= end - pos)
1873 return pos - buf;
1874 pos += ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001875 ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001876 ssid == wpa_s->current_ssid ?
1877 "[CURRENT]" : "",
1878 ssid->disabled ? "[DISABLED]" : "",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001879 ssid->disabled_until.sec ?
1880 "[TEMP-DISABLED]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001881 ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
1882 "");
1883 if (ret < 0 || ret >= end - pos)
1884 return pos - buf;
1885 pos += ret;
1886 ret = os_snprintf(pos, end - pos, "\n");
1887 if (ret < 0 || ret >= end - pos)
1888 return pos - buf;
1889 pos += ret;
1890
1891 ssid = ssid->next;
1892 }
1893
1894 return pos - buf;
1895}
1896
1897
1898static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
1899{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001900 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001901 ret = os_snprintf(pos, end - pos, "-");
1902 if (ret < 0 || ret >= end - pos)
1903 return pos;
1904 pos += ret;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001905 ret = wpa_write_ciphers(pos, end, cipher, "+");
1906 if (ret < 0)
1907 return pos;
1908 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001909 return pos;
1910}
1911
1912
1913static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
1914 const u8 *ie, size_t ie_len)
1915{
1916 struct wpa_ie_data data;
1917 int first, ret;
1918
1919 ret = os_snprintf(pos, end - pos, "[%s-", proto);
1920 if (ret < 0 || ret >= end - pos)
1921 return pos;
1922 pos += ret;
1923
1924 if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
1925 ret = os_snprintf(pos, end - pos, "?]");
1926 if (ret < 0 || ret >= end - pos)
1927 return pos;
1928 pos += ret;
1929 return pos;
1930 }
1931
1932 first = 1;
1933 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
1934 ret = os_snprintf(pos, end - pos, "%sEAP", first ? "" : "+");
1935 if (ret < 0 || ret >= end - pos)
1936 return pos;
1937 pos += ret;
1938 first = 0;
1939 }
1940 if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
1941 ret = os_snprintf(pos, end - pos, "%sPSK", first ? "" : "+");
1942 if (ret < 0 || ret >= end - pos)
1943 return pos;
1944 pos += ret;
1945 first = 0;
1946 }
1947 if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
1948 ret = os_snprintf(pos, end - pos, "%sNone", first ? "" : "+");
1949 if (ret < 0 || ret >= end - pos)
1950 return pos;
1951 pos += ret;
1952 first = 0;
1953 }
1954#ifdef CONFIG_IEEE80211R
1955 if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
1956 ret = os_snprintf(pos, end - pos, "%sFT/EAP",
1957 first ? "" : "+");
1958 if (ret < 0 || ret >= end - pos)
1959 return pos;
1960 pos += ret;
1961 first = 0;
1962 }
1963 if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
1964 ret = os_snprintf(pos, end - pos, "%sFT/PSK",
1965 first ? "" : "+");
1966 if (ret < 0 || ret >= end - pos)
1967 return pos;
1968 pos += ret;
1969 first = 0;
1970 }
1971#endif /* CONFIG_IEEE80211R */
1972#ifdef CONFIG_IEEE80211W
1973 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1974 ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
1975 first ? "" : "+");
1976 if (ret < 0 || ret >= end - pos)
1977 return pos;
1978 pos += ret;
1979 first = 0;
1980 }
1981 if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
1982 ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
1983 first ? "" : "+");
1984 if (ret < 0 || ret >= end - pos)
1985 return pos;
1986 pos += ret;
1987 first = 0;
1988 }
1989#endif /* CONFIG_IEEE80211W */
1990
1991 pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
1992
1993 if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
1994 ret = os_snprintf(pos, end - pos, "-preauth");
1995 if (ret < 0 || ret >= end - pos)
1996 return pos;
1997 pos += ret;
1998 }
1999
2000 ret = os_snprintf(pos, end - pos, "]");
2001 if (ret < 0 || ret >= end - pos)
2002 return pos;
2003 pos += ret;
2004
2005 return pos;
2006}
2007
2008
2009#ifdef CONFIG_WPS
2010static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
2011 char *pos, char *end,
2012 struct wpabuf *wps_ie)
2013{
2014 int ret;
2015 const char *txt;
2016
2017 if (wps_ie == NULL)
2018 return pos;
2019 if (wps_is_selected_pbc_registrar(wps_ie))
2020 txt = "[WPS-PBC]";
2021#ifdef CONFIG_WPS2
2022 else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
2023 txt = "[WPS-AUTH]";
2024#endif /* CONFIG_WPS2 */
2025 else if (wps_is_selected_pin_registrar(wps_ie))
2026 txt = "[WPS-PIN]";
2027 else
2028 txt = "[WPS]";
2029
2030 ret = os_snprintf(pos, end - pos, "%s", txt);
2031 if (ret >= 0 && ret < end - pos)
2032 pos += ret;
2033 wpabuf_free(wps_ie);
2034 return pos;
2035}
2036#endif /* CONFIG_WPS */
2037
2038
2039static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
2040 char *pos, char *end,
2041 const struct wpa_bss *bss)
2042{
2043#ifdef CONFIG_WPS
2044 struct wpabuf *wps_ie;
2045 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
2046 return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
2047#else /* CONFIG_WPS */
2048 return pos;
2049#endif /* CONFIG_WPS */
2050}
2051
2052
2053/* Format one result on one text line into a buffer. */
2054static int wpa_supplicant_ctrl_iface_scan_result(
2055 struct wpa_supplicant *wpa_s,
2056 const struct wpa_bss *bss, char *buf, size_t buflen)
2057{
2058 char *pos, *end;
2059 int ret;
2060 const u8 *ie, *ie2, *p2p;
2061
2062 p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -07002063 if (!p2p)
2064 p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002065 if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
2066 os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
2067 0)
2068 return 0; /* Do not show P2P listen discovery results here */
2069
2070 pos = buf;
2071 end = buf + buflen;
2072
2073 ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
2074 MAC2STR(bss->bssid), bss->freq, bss->level);
2075 if (ret < 0 || ret >= end - pos)
2076 return -1;
2077 pos += ret;
2078 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
2079 if (ie)
2080 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
2081 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
2082 if (ie2)
2083 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
2084 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
2085 if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
2086 ret = os_snprintf(pos, end - pos, "[WEP]");
2087 if (ret < 0 || ret >= end - pos)
2088 return -1;
2089 pos += ret;
2090 }
2091 if (bss->caps & IEEE80211_CAP_IBSS) {
2092 ret = os_snprintf(pos, end - pos, "[IBSS]");
2093 if (ret < 0 || ret >= end - pos)
2094 return -1;
2095 pos += ret;
2096 }
2097 if (bss->caps & IEEE80211_CAP_ESS) {
2098 ret = os_snprintf(pos, end - pos, "[ESS]");
2099 if (ret < 0 || ret >= end - pos)
2100 return -1;
2101 pos += ret;
2102 }
2103 if (p2p) {
2104 ret = os_snprintf(pos, end - pos, "[P2P]");
2105 if (ret < 0 || ret >= end - pos)
2106 return -1;
2107 pos += ret;
2108 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002109#ifdef CONFIG_HS20
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002110 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002111 ret = os_snprintf(pos, end - pos, "[HS20]");
2112 if (ret < 0 || ret >= end - pos)
2113 return -1;
2114 pos += ret;
2115 }
2116#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002117
2118 ret = os_snprintf(pos, end - pos, "\t%s",
2119 wpa_ssid_txt(bss->ssid, bss->ssid_len));
2120 if (ret < 0 || ret >= end - pos)
2121 return -1;
2122 pos += ret;
2123
2124 ret = os_snprintf(pos, end - pos, "\n");
2125 if (ret < 0 || ret >= end - pos)
2126 return -1;
2127 pos += ret;
2128
2129 return pos - buf;
2130}
2131
2132
2133static int wpa_supplicant_ctrl_iface_scan_results(
2134 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
2135{
2136 char *pos, *end;
2137 struct wpa_bss *bss;
2138 int ret;
2139
2140 pos = buf;
2141 end = buf + buflen;
2142 ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
2143 "flags / ssid\n");
2144 if (ret < 0 || ret >= end - pos)
2145 return pos - buf;
2146 pos += ret;
2147
2148 dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
2149 ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
2150 end - pos);
2151 if (ret < 0 || ret >= end - pos)
2152 return pos - buf;
2153 pos += ret;
2154 }
2155
2156 return pos - buf;
2157}
2158
2159
2160static int wpa_supplicant_ctrl_iface_select_network(
2161 struct wpa_supplicant *wpa_s, char *cmd)
2162{
2163 int id;
2164 struct wpa_ssid *ssid;
2165
2166 /* cmd: "<network id>" or "any" */
2167 if (os_strcmp(cmd, "any") == 0) {
2168 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
2169 ssid = NULL;
2170 } else {
2171 id = atoi(cmd);
2172 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
2173
2174 ssid = wpa_config_get_network(wpa_s->conf, id);
2175 if (ssid == NULL) {
2176 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2177 "network id=%d", id);
2178 return -1;
2179 }
2180 if (ssid->disabled == 2) {
2181 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2182 "SELECT_NETWORK with persistent P2P group");
2183 return -1;
2184 }
2185 }
2186
2187 wpa_supplicant_select_network(wpa_s, ssid);
2188
2189 return 0;
2190}
2191
2192
2193static int wpa_supplicant_ctrl_iface_enable_network(
2194 struct wpa_supplicant *wpa_s, char *cmd)
2195{
2196 int id;
2197 struct wpa_ssid *ssid;
2198
2199 /* cmd: "<network id>" or "all" */
2200 if (os_strcmp(cmd, "all") == 0) {
2201 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
2202 ssid = NULL;
2203 } else {
2204 id = atoi(cmd);
2205 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
2206
2207 ssid = wpa_config_get_network(wpa_s->conf, id);
2208 if (ssid == NULL) {
2209 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2210 "network id=%d", id);
2211 return -1;
2212 }
2213 if (ssid->disabled == 2) {
2214 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2215 "ENABLE_NETWORK with persistent P2P group");
2216 return -1;
2217 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002218
2219 if (os_strstr(cmd, " no-connect")) {
2220 ssid->disabled = 0;
2221 return 0;
2222 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002223 }
2224 wpa_supplicant_enable_network(wpa_s, ssid);
2225
2226 return 0;
2227}
2228
2229
2230static int wpa_supplicant_ctrl_iface_disable_network(
2231 struct wpa_supplicant *wpa_s, char *cmd)
2232{
2233 int id;
2234 struct wpa_ssid *ssid;
2235
2236 /* cmd: "<network id>" or "all" */
2237 if (os_strcmp(cmd, "all") == 0) {
2238 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
2239 ssid = NULL;
2240 } else {
2241 id = atoi(cmd);
2242 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
2243
2244 ssid = wpa_config_get_network(wpa_s->conf, id);
2245 if (ssid == NULL) {
2246 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2247 "network id=%d", id);
2248 return -1;
2249 }
2250 if (ssid->disabled == 2) {
2251 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2252 "DISABLE_NETWORK with persistent P2P "
2253 "group");
2254 return -1;
2255 }
2256 }
2257 wpa_supplicant_disable_network(wpa_s, ssid);
2258
2259 return 0;
2260}
2261
2262
2263static int wpa_supplicant_ctrl_iface_add_network(
2264 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
2265{
2266 struct wpa_ssid *ssid;
2267 int ret;
2268
2269 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
2270
2271 ssid = wpa_config_add_network(wpa_s->conf);
2272 if (ssid == NULL)
2273 return -1;
2274
2275 wpas_notify_network_added(wpa_s, ssid);
2276
2277 ssid->disabled = 1;
2278 wpa_config_set_network_defaults(ssid);
2279
2280 ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
2281 if (ret < 0 || (size_t) ret >= buflen)
2282 return -1;
2283 return ret;
2284}
2285
2286
2287static int wpa_supplicant_ctrl_iface_remove_network(
2288 struct wpa_supplicant *wpa_s, char *cmd)
2289{
2290 int id;
2291 struct wpa_ssid *ssid;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002292 int was_disabled;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002293
2294 /* cmd: "<network id>" or "all" */
2295 if (os_strcmp(cmd, "all") == 0) {
2296 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002297 if (wpa_s->sched_scanning)
2298 wpa_supplicant_cancel_sched_scan(wpa_s);
2299
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002300 eapol_sm_invalidate_cached_session(wpa_s->eapol);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002301 if (wpa_s->current_ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002302#ifdef CONFIG_SME
2303 wpa_s->sme.prev_bssid_set = 0;
2304#endif /* CONFIG_SME */
Jouni Malinen75ecf522011-06-27 15:19:46 -07002305 wpa_sm_set_config(wpa_s->wpa, NULL);
2306 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002307 wpa_supplicant_deauthenticate(
2308 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002309 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002310 ssid = wpa_s->conf->ssid;
2311 while (ssid) {
2312 struct wpa_ssid *remove_ssid = ssid;
2313 id = ssid->id;
2314 ssid = ssid->next;
2315 wpas_notify_network_removed(wpa_s, remove_ssid);
2316 wpa_config_remove_network(wpa_s->conf, id);
2317 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002318 return 0;
2319 }
2320
2321 id = atoi(cmd);
2322 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
2323
2324 ssid = wpa_config_get_network(wpa_s->conf, id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002325 if (ssid)
2326 wpas_notify_network_removed(wpa_s, ssid);
Deepthi Gowria831d782012-09-03 11:55:38 +03002327 if (ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002328 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2329 "id=%d", id);
2330 return -1;
2331 }
2332
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002333 if (ssid == wpa_s->current_ssid || wpa_s->current_ssid == NULL) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002334#ifdef CONFIG_SME
2335 wpa_s->sme.prev_bssid_set = 0;
2336#endif /* CONFIG_SME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002337 /*
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002338 * Invalidate the EAP session cache if the current or
2339 * previously used network is removed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002340 */
2341 eapol_sm_invalidate_cached_session(wpa_s->eapol);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002342 }
2343
2344 if (ssid == wpa_s->current_ssid) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07002345 wpa_sm_set_config(wpa_s->wpa, NULL);
2346 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002347
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002348 wpa_supplicant_deauthenticate(wpa_s,
2349 WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002350 }
2351
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002352 was_disabled = ssid->disabled;
2353
Deepthi Gowria831d782012-09-03 11:55:38 +03002354 if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
2355 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
2356 "network id=%d", id);
2357 return -1;
2358 }
2359
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002360 if (!was_disabled && wpa_s->sched_scanning) {
2361 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to remove "
2362 "network from filters");
2363 wpa_supplicant_cancel_sched_scan(wpa_s);
2364 wpa_supplicant_req_scan(wpa_s, 0, 0);
2365 }
2366
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002367 return 0;
2368}
2369
2370
2371static int wpa_supplicant_ctrl_iface_set_network(
2372 struct wpa_supplicant *wpa_s, char *cmd)
2373{
2374 int id;
2375 struct wpa_ssid *ssid;
2376 char *name, *value;
2377
2378 /* cmd: "<network id> <variable name> <value>" */
2379 name = os_strchr(cmd, ' ');
2380 if (name == NULL)
2381 return -1;
2382 *name++ = '\0';
2383
2384 value = os_strchr(name, ' ');
2385 if (value == NULL)
2386 return -1;
2387 *value++ = '\0';
2388
2389 id = atoi(cmd);
2390 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
2391 id, name);
2392 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2393 (u8 *) value, os_strlen(value));
2394
2395 ssid = wpa_config_get_network(wpa_s->conf, id);
2396 if (ssid == NULL) {
2397 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2398 "id=%d", id);
2399 return -1;
2400 }
2401
2402 if (wpa_config_set(ssid, name, value, 0) < 0) {
2403 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
2404 "variable '%s'", name);
2405 return -1;
2406 }
2407
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002408 if (os_strcmp(name, "bssid") != 0 &&
2409 os_strcmp(name, "priority") != 0)
2410 wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002411
2412 if (wpa_s->current_ssid == ssid || wpa_s->current_ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002413 /*
2414 * Invalidate the EAP session cache if anything in the current
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002415 * or previously used configuration changes.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002416 */
2417 eapol_sm_invalidate_cached_session(wpa_s->eapol);
2418 }
2419
2420 if ((os_strcmp(name, "psk") == 0 &&
2421 value[0] == '"' && ssid->ssid_len) ||
2422 (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
2423 wpa_config_update_psk(ssid);
2424 else if (os_strcmp(name, "priority") == 0)
2425 wpa_config_update_prio_list(wpa_s->conf);
2426
2427 return 0;
2428}
2429
2430
2431static int wpa_supplicant_ctrl_iface_get_network(
2432 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
2433{
2434 int id;
2435 size_t res;
2436 struct wpa_ssid *ssid;
2437 char *name, *value;
2438
2439 /* cmd: "<network id> <variable name>" */
2440 name = os_strchr(cmd, ' ');
2441 if (name == NULL || buflen == 0)
2442 return -1;
2443 *name++ = '\0';
2444
2445 id = atoi(cmd);
2446 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
2447 id, name);
2448
2449 ssid = wpa_config_get_network(wpa_s->conf, id);
2450 if (ssid == NULL) {
2451 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2452 "id=%d", id);
2453 return -1;
2454 }
2455
2456 value = wpa_config_get_no_key(ssid, name);
2457 if (value == NULL) {
2458 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
2459 "variable '%s'", name);
2460 return -1;
2461 }
2462
2463 res = os_strlcpy(buf, value, buflen);
2464 if (res >= buflen) {
2465 os_free(value);
2466 return -1;
2467 }
2468
2469 os_free(value);
2470
2471 return res;
2472}
2473
2474
Dmitry Shmidt04949592012-07-19 12:16:46 -07002475static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
2476 char *buf, size_t buflen)
2477{
2478 char *pos, *end;
2479 struct wpa_cred *cred;
2480 int ret;
2481
2482 pos = buf;
2483 end = buf + buflen;
2484 ret = os_snprintf(pos, end - pos,
2485 "cred id / realm / username / domain / imsi\n");
2486 if (ret < 0 || ret >= end - pos)
2487 return pos - buf;
2488 pos += ret;
2489
2490 cred = wpa_s->conf->cred;
2491 while (cred) {
2492 ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
2493 cred->id, cred->realm ? cred->realm : "",
2494 cred->username ? cred->username : "",
Dmitry Shmidt051af732013-10-22 13:52:46 -07002495 cred->domain ? cred->domain[0] : "",
Dmitry Shmidt04949592012-07-19 12:16:46 -07002496 cred->imsi ? cred->imsi : "");
2497 if (ret < 0 || ret >= end - pos)
2498 return pos - buf;
2499 pos += ret;
2500
2501 cred = cred->next;
2502 }
2503
2504 return pos - buf;
2505}
2506
2507
2508static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
2509 char *buf, size_t buflen)
2510{
2511 struct wpa_cred *cred;
2512 int ret;
2513
2514 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
2515
2516 cred = wpa_config_add_cred(wpa_s->conf);
2517 if (cred == NULL)
2518 return -1;
2519
2520 ret = os_snprintf(buf, buflen, "%d\n", cred->id);
2521 if (ret < 0 || (size_t) ret >= buflen)
2522 return -1;
2523 return ret;
2524}
2525
2526
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002527static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
2528 struct wpa_cred *cred)
2529{
2530 struct wpa_ssid *ssid;
2531 char str[20];
2532
2533 if (cred == NULL || wpa_config_remove_cred(wpa_s->conf, cred->id) < 0) {
2534 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
2535 return -1;
2536 }
2537
2538 /* Remove any network entry created based on the removed credential */
2539 ssid = wpa_s->conf->ssid;
2540 while (ssid) {
2541 if (ssid->parent_cred == cred) {
2542 wpa_printf(MSG_DEBUG, "Remove network id %d since it "
2543 "used the removed credential", ssid->id);
2544 os_snprintf(str, sizeof(str), "%d", ssid->id);
2545 ssid = ssid->next;
2546 wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
2547 } else
2548 ssid = ssid->next;
2549 }
2550
2551 return 0;
2552}
2553
2554
Dmitry Shmidt04949592012-07-19 12:16:46 -07002555static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
2556 char *cmd)
2557{
2558 int id;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002559 struct wpa_cred *cred, *prev;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002560
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002561 /* cmd: "<cred id>", "all", or "sp_fqdn=<FQDN>" */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002562 if (os_strcmp(cmd, "all") == 0) {
2563 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
2564 cred = wpa_s->conf->cred;
2565 while (cred) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002566 prev = cred;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002567 cred = cred->next;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002568 wpas_ctrl_remove_cred(wpa_s, prev);
2569 }
2570 return 0;
2571 }
2572
2573 if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
2574 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
2575 cmd + 8);
2576 cred = wpa_s->conf->cred;
2577 while (cred) {
2578 prev = cred;
2579 cred = cred->next;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002580 if (prev->domain) {
2581 size_t i;
2582 for (i = 0; i < prev->num_domain; i++) {
2583 if (os_strcmp(prev->domain[i], cmd + 8)
2584 != 0)
2585 continue;
2586 wpas_ctrl_remove_cred(wpa_s, prev);
2587 break;
2588 }
2589 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002590 }
2591 return 0;
2592 }
2593
2594 id = atoi(cmd);
2595 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
2596
2597 cred = wpa_config_get_cred(wpa_s->conf, id);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002598 return wpas_ctrl_remove_cred(wpa_s, cred);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002599}
2600
2601
2602static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
2603 char *cmd)
2604{
2605 int id;
2606 struct wpa_cred *cred;
2607 char *name, *value;
2608
2609 /* cmd: "<cred id> <variable name> <value>" */
2610 name = os_strchr(cmd, ' ');
2611 if (name == NULL)
2612 return -1;
2613 *name++ = '\0';
2614
2615 value = os_strchr(name, ' ');
2616 if (value == NULL)
2617 return -1;
2618 *value++ = '\0';
2619
2620 id = atoi(cmd);
2621 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
2622 id, name);
2623 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2624 (u8 *) value, os_strlen(value));
2625
2626 cred = wpa_config_get_cred(wpa_s->conf, id);
2627 if (cred == NULL) {
2628 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
2629 id);
2630 return -1;
2631 }
2632
2633 if (wpa_config_set_cred(cred, name, value, 0) < 0) {
2634 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
2635 "variable '%s'", name);
2636 return -1;
2637 }
2638
2639 return 0;
2640}
2641
2642
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002643#ifndef CONFIG_NO_CONFIG_WRITE
2644static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
2645{
2646 int ret;
2647
2648 if (!wpa_s->conf->update_config) {
2649 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
2650 "to update configuration (update_config=0)");
2651 return -1;
2652 }
2653
2654 ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
2655 if (ret) {
2656 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
2657 "update configuration");
2658 } else {
2659 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
2660 " updated");
2661 }
2662
2663 return ret;
2664}
2665#endif /* CONFIG_NO_CONFIG_WRITE */
2666
2667
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002668struct cipher_info {
2669 unsigned int capa;
2670 const char *name;
2671 int group_only;
2672};
2673
2674static const struct cipher_info ciphers[] = {
2675 { WPA_DRIVER_CAPA_ENC_CCMP_256, "CCMP-256", 0 },
2676 { WPA_DRIVER_CAPA_ENC_GCMP_256, "GCMP-256", 0 },
2677 { WPA_DRIVER_CAPA_ENC_CCMP, "CCMP", 0 },
2678 { WPA_DRIVER_CAPA_ENC_GCMP, "GCMP", 0 },
2679 { WPA_DRIVER_CAPA_ENC_TKIP, "TKIP", 0 },
2680 { WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE, "NONE", 0 },
2681 { WPA_DRIVER_CAPA_ENC_WEP104, "WEP104", 1 },
2682 { WPA_DRIVER_CAPA_ENC_WEP40, "WEP40", 1 }
2683};
2684
2685
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002686static int ctrl_iface_get_capability_pairwise(int res, char *strict,
2687 struct wpa_driver_capa *capa,
2688 char *buf, size_t buflen)
2689{
2690 int ret, first = 1;
2691 char *pos, *end;
2692 size_t len;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002693 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002694
2695 pos = buf;
2696 end = pos + buflen;
2697
2698 if (res < 0) {
2699 if (strict)
2700 return 0;
2701 len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
2702 if (len >= buflen)
2703 return -1;
2704 return len;
2705 }
2706
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002707 for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
2708 if (!ciphers[i].group_only && capa->enc & ciphers[i].capa) {
2709 ret = os_snprintf(pos, end - pos, "%s%s",
2710 first ? "" : " ", ciphers[i].name);
2711 if (ret < 0 || ret >= end - pos)
2712 return pos - buf;
2713 pos += ret;
2714 first = 0;
2715 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002716 }
2717
2718 return pos - buf;
2719}
2720
2721
2722static int ctrl_iface_get_capability_group(int res, char *strict,
2723 struct wpa_driver_capa *capa,
2724 char *buf, size_t buflen)
2725{
2726 int ret, first = 1;
2727 char *pos, *end;
2728 size_t len;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002729 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002730
2731 pos = buf;
2732 end = pos + buflen;
2733
2734 if (res < 0) {
2735 if (strict)
2736 return 0;
2737 len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
2738 if (len >= buflen)
2739 return -1;
2740 return len;
2741 }
2742
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002743 for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
2744 if (capa->enc & ciphers[i].capa) {
2745 ret = os_snprintf(pos, end - pos, "%s%s",
2746 first ? "" : " ", ciphers[i].name);
2747 if (ret < 0 || ret >= end - pos)
2748 return pos - buf;
2749 pos += ret;
2750 first = 0;
2751 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002752 }
2753
2754 return pos - buf;
2755}
2756
2757
2758static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
2759 struct wpa_driver_capa *capa,
2760 char *buf, size_t buflen)
2761{
2762 int ret;
2763 char *pos, *end;
2764 size_t len;
2765
2766 pos = buf;
2767 end = pos + buflen;
2768
2769 if (res < 0) {
2770 if (strict)
2771 return 0;
2772 len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
2773 "NONE", buflen);
2774 if (len >= buflen)
2775 return -1;
2776 return len;
2777 }
2778
2779 ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
2780 if (ret < 0 || ret >= end - pos)
2781 return pos - buf;
2782 pos += ret;
2783
2784 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2785 WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
2786 ret = os_snprintf(pos, end - pos, " WPA-EAP");
2787 if (ret < 0 || ret >= end - pos)
2788 return pos - buf;
2789 pos += ret;
2790 }
2791
2792 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
2793 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
2794 ret = os_snprintf(pos, end - pos, " WPA-PSK");
2795 if (ret < 0 || ret >= end - pos)
2796 return pos - buf;
2797 pos += ret;
2798 }
2799
2800 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
2801 ret = os_snprintf(pos, end - pos, " WPA-NONE");
2802 if (ret < 0 || ret >= end - pos)
2803 return pos - buf;
2804 pos += ret;
2805 }
2806
2807 return pos - buf;
2808}
2809
2810
2811static int ctrl_iface_get_capability_proto(int res, char *strict,
2812 struct wpa_driver_capa *capa,
2813 char *buf, size_t buflen)
2814{
2815 int ret, first = 1;
2816 char *pos, *end;
2817 size_t len;
2818
2819 pos = buf;
2820 end = pos + buflen;
2821
2822 if (res < 0) {
2823 if (strict)
2824 return 0;
2825 len = os_strlcpy(buf, "RSN WPA", buflen);
2826 if (len >= buflen)
2827 return -1;
2828 return len;
2829 }
2830
2831 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
2832 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
2833 ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
2834 if (ret < 0 || ret >= end - pos)
2835 return pos - buf;
2836 pos += ret;
2837 first = 0;
2838 }
2839
2840 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2841 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
2842 ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
2843 if (ret < 0 || ret >= end - pos)
2844 return pos - buf;
2845 pos += ret;
2846 first = 0;
2847 }
2848
2849 return pos - buf;
2850}
2851
2852
2853static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
2854 struct wpa_driver_capa *capa,
2855 char *buf, size_t buflen)
2856{
2857 int ret, first = 1;
2858 char *pos, *end;
2859 size_t len;
2860
2861 pos = buf;
2862 end = pos + buflen;
2863
2864 if (res < 0) {
2865 if (strict)
2866 return 0;
2867 len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
2868 if (len >= buflen)
2869 return -1;
2870 return len;
2871 }
2872
2873 if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
2874 ret = os_snprintf(pos, end - pos, "%sOPEN", first ? "" : " ");
2875 if (ret < 0 || ret >= end - pos)
2876 return pos - buf;
2877 pos += ret;
2878 first = 0;
2879 }
2880
2881 if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
2882 ret = os_snprintf(pos, end - pos, "%sSHARED",
2883 first ? "" : " ");
2884 if (ret < 0 || ret >= end - pos)
2885 return pos - buf;
2886 pos += ret;
2887 first = 0;
2888 }
2889
2890 if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
2891 ret = os_snprintf(pos, end - pos, "%sLEAP", first ? "" : " ");
2892 if (ret < 0 || ret >= end - pos)
2893 return pos - buf;
2894 pos += ret;
2895 first = 0;
2896 }
2897
2898 return pos - buf;
2899}
2900
2901
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002902static int ctrl_iface_get_capability_modes(int res, char *strict,
2903 struct wpa_driver_capa *capa,
2904 char *buf, size_t buflen)
2905{
2906 int ret, first = 1;
2907 char *pos, *end;
2908 size_t len;
2909
2910 pos = buf;
2911 end = pos + buflen;
2912
2913 if (res < 0) {
2914 if (strict)
2915 return 0;
2916 len = os_strlcpy(buf, "IBSS AP", buflen);
2917 if (len >= buflen)
2918 return -1;
2919 return len;
2920 }
2921
2922 if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
2923 ret = os_snprintf(pos, end - pos, "%sIBSS", first ? "" : " ");
2924 if (ret < 0 || ret >= end - pos)
2925 return pos - buf;
2926 pos += ret;
2927 first = 0;
2928 }
2929
2930 if (capa->flags & WPA_DRIVER_FLAGS_AP) {
2931 ret = os_snprintf(pos, end - pos, "%sAP", first ? "" : " ");
2932 if (ret < 0 || ret >= end - pos)
2933 return pos - buf;
2934 pos += ret;
2935 first = 0;
2936 }
2937
2938 return pos - buf;
2939}
2940
2941
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07002942static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
2943 char *buf, size_t buflen)
2944{
2945 struct hostapd_channel_data *chnl;
2946 int ret, i, j;
2947 char *pos, *end, *hmode;
2948
2949 pos = buf;
2950 end = pos + buflen;
2951
2952 for (j = 0; j < wpa_s->hw.num_modes; j++) {
2953 switch (wpa_s->hw.modes[j].mode) {
2954 case HOSTAPD_MODE_IEEE80211B:
2955 hmode = "B";
2956 break;
2957 case HOSTAPD_MODE_IEEE80211G:
2958 hmode = "G";
2959 break;
2960 case HOSTAPD_MODE_IEEE80211A:
2961 hmode = "A";
2962 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002963 case HOSTAPD_MODE_IEEE80211AD:
2964 hmode = "AD";
2965 break;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07002966 default:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002967 continue;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07002968 }
2969 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
2970 if (ret < 0 || ret >= end - pos)
2971 return pos - buf;
2972 pos += ret;
2973 chnl = wpa_s->hw.modes[j].channels;
2974 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002975 if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
2976 continue;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07002977 ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
2978 if (ret < 0 || ret >= end - pos)
2979 return pos - buf;
2980 pos += ret;
2981 }
2982 ret = os_snprintf(pos, end - pos, "\n");
2983 if (ret < 0 || ret >= end - pos)
2984 return pos - buf;
2985 pos += ret;
2986 }
2987
2988 return pos - buf;
2989}
2990
2991
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002992static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
2993 char *buf, size_t buflen)
2994{
2995 struct hostapd_channel_data *chnl;
2996 int ret, i, j;
2997 char *pos, *end, *hmode;
2998
2999 pos = buf;
3000 end = pos + buflen;
3001
3002 for (j = 0; j < wpa_s->hw.num_modes; j++) {
3003 switch (wpa_s->hw.modes[j].mode) {
3004 case HOSTAPD_MODE_IEEE80211B:
3005 hmode = "B";
3006 break;
3007 case HOSTAPD_MODE_IEEE80211G:
3008 hmode = "G";
3009 break;
3010 case HOSTAPD_MODE_IEEE80211A:
3011 hmode = "A";
3012 break;
3013 case HOSTAPD_MODE_IEEE80211AD:
3014 hmode = "AD";
3015 break;
3016 default:
3017 continue;
3018 }
3019 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
3020 hmode);
3021 if (ret < 0 || ret >= end - pos)
3022 return pos - buf;
3023 pos += ret;
3024 chnl = wpa_s->hw.modes[j].channels;
3025 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
3026 if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
3027 continue;
3028 ret = os_snprintf(pos, end - pos, " %d = %d MHz%s\n",
3029 chnl[i].chan, chnl[i].freq,
3030 chnl[i].flag & HOSTAPD_CHAN_NO_IBSS ?
3031 " (NO_IBSS)" : "");
3032 if (ret < 0 || ret >= end - pos)
3033 return pos - buf;
3034 pos += ret;
3035 }
3036 ret = os_snprintf(pos, end - pos, "\n");
3037 if (ret < 0 || ret >= end - pos)
3038 return pos - buf;
3039 pos += ret;
3040 }
3041
3042 return pos - buf;
3043}
3044
3045
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003046static int wpa_supplicant_ctrl_iface_get_capability(
3047 struct wpa_supplicant *wpa_s, const char *_field, char *buf,
3048 size_t buflen)
3049{
3050 struct wpa_driver_capa capa;
3051 int res;
3052 char *strict;
3053 char field[30];
3054 size_t len;
3055
3056 /* Determine whether or not strict checking was requested */
3057 len = os_strlcpy(field, _field, sizeof(field));
3058 if (len >= sizeof(field))
3059 return -1;
3060 strict = os_strchr(field, ' ');
3061 if (strict != NULL) {
3062 *strict++ = '\0';
3063 if (os_strcmp(strict, "strict") != 0)
3064 return -1;
3065 }
3066
3067 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
3068 field, strict ? strict : "");
3069
3070 if (os_strcmp(field, "eap") == 0) {
3071 return eap_get_names(buf, buflen);
3072 }
3073
3074 res = wpa_drv_get_capa(wpa_s, &capa);
3075
3076 if (os_strcmp(field, "pairwise") == 0)
3077 return ctrl_iface_get_capability_pairwise(res, strict, &capa,
3078 buf, buflen);
3079
3080 if (os_strcmp(field, "group") == 0)
3081 return ctrl_iface_get_capability_group(res, strict, &capa,
3082 buf, buflen);
3083
3084 if (os_strcmp(field, "key_mgmt") == 0)
3085 return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
3086 buf, buflen);
3087
3088 if (os_strcmp(field, "proto") == 0)
3089 return ctrl_iface_get_capability_proto(res, strict, &capa,
3090 buf, buflen);
3091
3092 if (os_strcmp(field, "auth_alg") == 0)
3093 return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
3094 buf, buflen);
3095
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003096 if (os_strcmp(field, "modes") == 0)
3097 return ctrl_iface_get_capability_modes(res, strict, &capa,
3098 buf, buflen);
3099
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003100 if (os_strcmp(field, "channels") == 0)
3101 return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
3102
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003103 if (os_strcmp(field, "freq") == 0)
3104 return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
3105
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003106 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
3107 field);
3108
3109 return -1;
3110}
3111
3112
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003113#ifdef CONFIG_INTERWORKING
3114static char * anqp_add_hex(char *pos, char *end, const char *title,
3115 struct wpabuf *data)
3116{
3117 char *start = pos;
3118 size_t i;
3119 int ret;
3120 const u8 *d;
3121
3122 if (data == NULL)
3123 return start;
3124
3125 ret = os_snprintf(pos, end - pos, "%s=", title);
3126 if (ret < 0 || ret >= end - pos)
3127 return start;
3128 pos += ret;
3129
3130 d = wpabuf_head_u8(data);
3131 for (i = 0; i < wpabuf_len(data); i++) {
3132 ret = os_snprintf(pos, end - pos, "%02x", *d++);
3133 if (ret < 0 || ret >= end - pos)
3134 return start;
3135 pos += ret;
3136 }
3137
3138 ret = os_snprintf(pos, end - pos, "\n");
3139 if (ret < 0 || ret >= end - pos)
3140 return start;
3141 pos += ret;
3142
3143 return pos;
3144}
3145#endif /* CONFIG_INTERWORKING */
3146
3147
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003148static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
3149 unsigned long mask, char *buf, size_t buflen)
3150{
3151 size_t i;
3152 int ret;
3153 char *pos, *end;
3154 const u8 *ie, *ie2;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003155
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003156 pos = buf;
3157 end = buf + buflen;
3158
3159 if (mask & WPA_BSS_MASK_ID) {
3160 ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
3161 if (ret < 0 || ret >= end - pos)
3162 return 0;
3163 pos += ret;
3164 }
3165
3166 if (mask & WPA_BSS_MASK_BSSID) {
3167 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
3168 MAC2STR(bss->bssid));
3169 if (ret < 0 || ret >= end - pos)
3170 return 0;
3171 pos += ret;
3172 }
3173
3174 if (mask & WPA_BSS_MASK_FREQ) {
3175 ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
3176 if (ret < 0 || ret >= end - pos)
3177 return 0;
3178 pos += ret;
3179 }
3180
3181 if (mask & WPA_BSS_MASK_BEACON_INT) {
3182 ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
3183 bss->beacon_int);
3184 if (ret < 0 || ret >= end - pos)
3185 return 0;
3186 pos += ret;
3187 }
3188
3189 if (mask & WPA_BSS_MASK_CAPABILITIES) {
3190 ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
3191 bss->caps);
3192 if (ret < 0 || ret >= end - pos)
3193 return 0;
3194 pos += ret;
3195 }
3196
3197 if (mask & WPA_BSS_MASK_QUAL) {
3198 ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
3199 if (ret < 0 || ret >= end - pos)
3200 return 0;
3201 pos += ret;
3202 }
3203
3204 if (mask & WPA_BSS_MASK_NOISE) {
3205 ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
3206 if (ret < 0 || ret >= end - pos)
3207 return 0;
3208 pos += ret;
3209 }
3210
3211 if (mask & WPA_BSS_MASK_LEVEL) {
3212 ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
3213 if (ret < 0 || ret >= end - pos)
3214 return 0;
3215 pos += ret;
3216 }
3217
3218 if (mask & WPA_BSS_MASK_TSF) {
3219 ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
3220 (unsigned long long) bss->tsf);
3221 if (ret < 0 || ret >= end - pos)
3222 return 0;
3223 pos += ret;
3224 }
3225
3226 if (mask & WPA_BSS_MASK_AGE) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003227 struct os_reltime now;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003228
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003229 os_get_reltime(&now);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003230 ret = os_snprintf(pos, end - pos, "age=%d\n",
3231 (int) (now.sec - bss->last_update.sec));
3232 if (ret < 0 || ret >= end - pos)
3233 return 0;
3234 pos += ret;
3235 }
3236
3237 if (mask & WPA_BSS_MASK_IE) {
3238 ret = os_snprintf(pos, end - pos, "ie=");
3239 if (ret < 0 || ret >= end - pos)
3240 return 0;
3241 pos += ret;
3242
3243 ie = (const u8 *) (bss + 1);
3244 for (i = 0; i < bss->ie_len; i++) {
3245 ret = os_snprintf(pos, end - pos, "%02x", *ie++);
3246 if (ret < 0 || ret >= end - pos)
3247 return 0;
3248 pos += ret;
3249 }
3250
3251 ret = os_snprintf(pos, end - pos, "\n");
3252 if (ret < 0 || ret >= end - pos)
3253 return 0;
3254 pos += ret;
3255 }
3256
3257 if (mask & WPA_BSS_MASK_FLAGS) {
3258 ret = os_snprintf(pos, end - pos, "flags=");
3259 if (ret < 0 || ret >= end - pos)
3260 return 0;
3261 pos += ret;
3262
3263 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
3264 if (ie)
3265 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
3266 2 + ie[1]);
3267 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
3268 if (ie2)
3269 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
3270 2 + ie2[1]);
3271 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
3272 if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
3273 ret = os_snprintf(pos, end - pos, "[WEP]");
3274 if (ret < 0 || ret >= end - pos)
3275 return 0;
3276 pos += ret;
3277 }
3278 if (bss->caps & IEEE80211_CAP_IBSS) {
3279 ret = os_snprintf(pos, end - pos, "[IBSS]");
3280 if (ret < 0 || ret >= end - pos)
3281 return 0;
3282 pos += ret;
3283 }
3284 if (bss->caps & IEEE80211_CAP_ESS) {
3285 ret = os_snprintf(pos, end - pos, "[ESS]");
3286 if (ret < 0 || ret >= end - pos)
3287 return 0;
3288 pos += ret;
3289 }
Dmitry Shmidt96571392013-10-14 12:54:46 -07003290 if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
3291 wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003292 ret = os_snprintf(pos, end - pos, "[P2P]");
3293 if (ret < 0 || ret >= end - pos)
3294 return 0;
3295 pos += ret;
3296 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003297#ifdef CONFIG_HS20
3298 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
3299 ret = os_snprintf(pos, end - pos, "[HS20]");
3300 if (ret < 0 || ret >= end - pos)
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003301 return 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003302 pos += ret;
3303 }
3304#endif /* CONFIG_HS20 */
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003305
3306 ret = os_snprintf(pos, end - pos, "\n");
3307 if (ret < 0 || ret >= end - pos)
3308 return 0;
3309 pos += ret;
3310 }
3311
3312 if (mask & WPA_BSS_MASK_SSID) {
3313 ret = os_snprintf(pos, end - pos, "ssid=%s\n",
3314 wpa_ssid_txt(bss->ssid, bss->ssid_len));
3315 if (ret < 0 || ret >= end - pos)
3316 return 0;
3317 pos += ret;
3318 }
3319
3320#ifdef CONFIG_WPS
3321 if (mask & WPA_BSS_MASK_WPS_SCAN) {
3322 ie = (const u8 *) (bss + 1);
3323 ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
3324 if (ret < 0 || ret >= end - pos)
3325 return 0;
3326 pos += ret;
3327 }
3328#endif /* CONFIG_WPS */
3329
3330#ifdef CONFIG_P2P
3331 if (mask & WPA_BSS_MASK_P2P_SCAN) {
3332 ie = (const u8 *) (bss + 1);
3333 ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
3334 if (ret < 0 || ret >= end - pos)
3335 return 0;
3336 pos += ret;
3337 }
3338#endif /* CONFIG_P2P */
3339
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003340#ifdef CONFIG_WIFI_DISPLAY
3341 if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
3342 struct wpabuf *wfd;
3343 ie = (const u8 *) (bss + 1);
3344 wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
3345 WFD_IE_VENDOR_TYPE);
3346 if (wfd) {
3347 ret = os_snprintf(pos, end - pos, "wfd_subelems=");
3348 if (ret < 0 || ret >= end - pos)
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003349 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003350 pos += ret;
3351
3352 pos += wpa_snprintf_hex(pos, end - pos,
3353 wpabuf_head(wfd),
3354 wpabuf_len(wfd));
3355 wpabuf_free(wfd);
3356
3357 ret = os_snprintf(pos, end - pos, "\n");
3358 if (ret < 0 || ret >= end - pos)
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003359 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003360 pos += ret;
3361 }
3362 }
3363#endif /* CONFIG_WIFI_DISPLAY */
3364
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003365#ifdef CONFIG_INTERWORKING
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003366 if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
3367 struct wpa_bss_anqp *anqp = bss->anqp;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003368 pos = anqp_add_hex(pos, end, "anqp_venue_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003369 anqp->venue_name);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003370 pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003371 anqp->network_auth_type);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003372 pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003373 anqp->roaming_consortium);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003374 pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003375 anqp->ip_addr_type_availability);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003376 pos = anqp_add_hex(pos, end, "anqp_nai_realm",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003377 anqp->nai_realm);
3378 pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003379 pos = anqp_add_hex(pos, end, "anqp_domain_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003380 anqp->domain_name);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003381#ifdef CONFIG_HS20
3382 pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003383 anqp->hs20_operator_friendly_name);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003384 pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003385 anqp->hs20_wan_metrics);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003386 pos = anqp_add_hex(pos, end, "hs20_connection_capability",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003387 anqp->hs20_connection_capability);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003388#endif /* CONFIG_HS20 */
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003389 }
3390#endif /* CONFIG_INTERWORKING */
3391
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003392 if (mask & WPA_BSS_MASK_DELIM) {
3393 ret = os_snprintf(pos, end - pos, "====\n");
3394 if (ret < 0 || ret >= end - pos)
3395 return 0;
3396 pos += ret;
3397 }
Irfan Sheriffe2ea0082012-08-13 10:56:16 -07003398
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003399 return pos - buf;
3400}
3401
Dmitry Shmidt04949592012-07-19 12:16:46 -07003402
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003403static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
3404 const char *cmd, char *buf,
3405 size_t buflen)
3406{
3407 u8 bssid[ETH_ALEN];
3408 size_t i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003409 struct wpa_bss *bss;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003410 struct wpa_bss *bsslast = NULL;
3411 struct dl_list *next;
3412 int ret = 0;
3413 int len;
3414 char *ctmp;
3415 unsigned long mask = WPA_BSS_MASK_ALL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003416
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003417 if (os_strncmp(cmd, "RANGE=", 6) == 0) {
3418 if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
3419 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003420 list_id);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003421 bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
3422 list_id);
3423 } else { /* N1-N2 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003424 unsigned int id1, id2;
3425
3426 if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
3427 wpa_printf(MSG_INFO, "Wrong BSS range "
3428 "format");
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003429 return 0;
3430 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003431
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003432 if (*(cmd + 6) == '-')
3433 id1 = 0;
3434 else
3435 id1 = atoi(cmd + 6);
3436 ctmp++;
3437 if (*ctmp >= '0' && *ctmp <= '9')
3438 id2 = atoi(ctmp);
3439 else
3440 id2 = (unsigned int) -1;
3441 bss = wpa_bss_get_id_range(wpa_s, id1, id2);
3442 if (id2 == (unsigned int) -1)
Dmitry Shmidt04949592012-07-19 12:16:46 -07003443 bsslast = dl_list_last(&wpa_s->bss_id,
3444 struct wpa_bss,
3445 list_id);
3446 else {
3447 bsslast = wpa_bss_get_id(wpa_s, id2);
3448 if (bsslast == NULL && bss && id2 > id1) {
3449 struct wpa_bss *tmp = bss;
3450 for (;;) {
3451 next = tmp->list_id.next;
3452 if (next == &wpa_s->bss_id)
3453 break;
3454 tmp = dl_list_entry(
3455 next, struct wpa_bss,
3456 list_id);
3457 if (tmp->id > id2)
3458 break;
3459 bsslast = tmp;
3460 }
3461 }
3462 }
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003463 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003464 } else if (os_strncmp(cmd, "FIRST", 5) == 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -07003465 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003466 else if (os_strncmp(cmd, "LAST", 4) == 0)
3467 bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003468 else if (os_strncmp(cmd, "ID-", 3) == 0) {
3469 i = atoi(cmd + 3);
3470 bss = wpa_bss_get_id(wpa_s, i);
3471 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
3472 i = atoi(cmd + 5);
3473 bss = wpa_bss_get_id(wpa_s, i);
3474 if (bss) {
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003475 next = bss->list_id.next;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003476 if (next == &wpa_s->bss_id)
3477 bss = NULL;
3478 else
3479 bss = dl_list_entry(next, struct wpa_bss,
3480 list_id);
3481 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003482#ifdef CONFIG_P2P
3483 } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
3484 if (hwaddr_aton(cmd + 13, bssid) == 0)
3485 bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
3486 else
3487 bss = NULL;
3488#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003489 } else if (hwaddr_aton(cmd, bssid) == 0)
3490 bss = wpa_bss_get_bssid(wpa_s, bssid);
3491 else {
3492 struct wpa_bss *tmp;
3493 i = atoi(cmd);
3494 bss = NULL;
3495 dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
3496 {
3497 if (i-- == 0) {
3498 bss = tmp;
3499 break;
3500 }
3501 }
3502 }
3503
Dmitry Shmidt04949592012-07-19 12:16:46 -07003504 if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
3505 mask = strtoul(ctmp + 5, NULL, 0x10);
3506 if (mask == 0)
3507 mask = WPA_BSS_MASK_ALL;
3508 }
3509
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003510 if (bss == NULL)
3511 return 0;
3512
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003513 if (bsslast == NULL)
3514 bsslast = bss;
3515 do {
3516 len = print_bss_info(wpa_s, bss, mask, buf, buflen);
3517 ret += len;
3518 buf += len;
3519 buflen -= len;
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003520 if (bss == bsslast) {
3521 if ((mask & WPA_BSS_MASK_DELIM) && len &&
3522 (bss == dl_list_last(&wpa_s->bss_id,
3523 struct wpa_bss, list_id)))
3524 os_snprintf(buf - 5, 5, "####\n");
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003525 break;
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003526 }
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003527 next = bss->list_id.next;
3528 if (next == &wpa_s->bss_id)
3529 break;
3530 bss = dl_list_entry(next, struct wpa_bss, list_id);
3531 } while (bss && len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003532
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003533 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003534}
3535
3536
3537static int wpa_supplicant_ctrl_iface_ap_scan(
3538 struct wpa_supplicant *wpa_s, char *cmd)
3539{
3540 int ap_scan = atoi(cmd);
3541 return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
3542}
3543
3544
3545static int wpa_supplicant_ctrl_iface_scan_interval(
3546 struct wpa_supplicant *wpa_s, char *cmd)
3547{
3548 int scan_int = atoi(cmd);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003549 return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003550}
3551
3552
3553static int wpa_supplicant_ctrl_iface_bss_expire_age(
3554 struct wpa_supplicant *wpa_s, char *cmd)
3555{
3556 int expire_age = atoi(cmd);
3557 return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
3558}
3559
3560
3561static int wpa_supplicant_ctrl_iface_bss_expire_count(
3562 struct wpa_supplicant *wpa_s, char *cmd)
3563{
3564 int expire_count = atoi(cmd);
3565 return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
3566}
3567
3568
Dmitry Shmidtf48e4f92012-08-24 11:14:44 -07003569static int wpa_supplicant_ctrl_iface_bss_flush(
3570 struct wpa_supplicant *wpa_s, char *cmd)
3571{
3572 int flush_age = atoi(cmd);
3573
3574 if (flush_age == 0)
3575 wpa_bss_flush(wpa_s);
3576 else
3577 wpa_bss_flush_by_age(wpa_s, flush_age);
3578 return 0;
3579}
3580
3581
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003582static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
3583{
3584 wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
3585 /* MLME-DELETEKEYS.request */
3586 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
3587 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
3588 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
3589 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
3590#ifdef CONFIG_IEEE80211W
3591 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
3592 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
3593#endif /* CONFIG_IEEE80211W */
3594
3595 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
3596 0);
3597 /* MLME-SETPROTECTION.request(None) */
3598 wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
3599 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
3600 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
3601 wpa_sm_drop_sa(wpa_s->wpa);
3602}
3603
3604
3605static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
3606 char *addr)
3607{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003608#ifdef CONFIG_NO_SCAN_PROCESSING
3609 return -1;
3610#else /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003611 u8 bssid[ETH_ALEN];
3612 struct wpa_bss *bss;
3613 struct wpa_ssid *ssid = wpa_s->current_ssid;
3614
3615 if (hwaddr_aton(addr, bssid)) {
3616 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
3617 "address '%s'", addr);
3618 return -1;
3619 }
3620
3621 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
3622
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003623 if (!ssid) {
3624 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
3625 "configuration known for the target AP");
3626 return -1;
3627 }
3628
3629 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003630 if (!bss) {
3631 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
3632 "from BSS table");
3633 return -1;
3634 }
3635
3636 /*
3637 * TODO: Find best network configuration block from configuration to
3638 * allow roaming to other networks
3639 */
3640
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003641 wpa_s->reassociate = 1;
3642 wpa_supplicant_connect(wpa_s, bss, ssid);
3643
3644 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003645#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003646}
3647
3648
3649#ifdef CONFIG_P2P
3650static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
3651{
3652 unsigned int timeout = atoi(cmd);
3653 enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003654 u8 dev_id[ETH_ALEN], *_dev_id = NULL;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003655 u8 dev_type[WPS_DEV_TYPE_LEN], *_dev_type = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003656 char *pos;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003657 unsigned int search_delay;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003658
3659 if (os_strstr(cmd, "type=social"))
3660 type = P2P_FIND_ONLY_SOCIAL;
3661 else if (os_strstr(cmd, "type=progressive"))
3662 type = P2P_FIND_PROGRESSIVE;
3663
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003664 pos = os_strstr(cmd, "dev_id=");
3665 if (pos) {
3666 pos += 7;
3667 if (hwaddr_aton(pos, dev_id))
3668 return -1;
3669 _dev_id = dev_id;
3670 }
3671
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003672 pos = os_strstr(cmd, "dev_type=");
3673 if (pos) {
3674 pos += 9;
3675 if (wps_dev_type_str2bin(pos, dev_type) < 0)
3676 return -1;
3677 _dev_type = dev_type;
3678 }
3679
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003680 pos = os_strstr(cmd, "delay=");
3681 if (pos) {
3682 pos += 6;
3683 search_delay = atoi(pos);
3684 } else
3685 search_delay = wpas_p2p_search_delay(wpa_s);
3686
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003687 return wpas_p2p_find(wpa_s, timeout, type, _dev_type != NULL, _dev_type,
3688 _dev_id, search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003689}
3690
3691
3692static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
3693 char *buf, size_t buflen)
3694{
3695 u8 addr[ETH_ALEN];
3696 char *pos, *pos2;
3697 char *pin = NULL;
3698 enum p2p_wps_method wps_method;
3699 int new_pin;
3700 int ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003701 int persistent_group, persistent_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003702 int join;
3703 int auth;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003704 int automatic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003705 int go_intent = -1;
3706 int freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003707 int pd;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003708 int ht40, vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003709
Dmitry Shmidt04949592012-07-19 12:16:46 -07003710 /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad]
3711 * [persistent|persistent=<network id>]
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003712 * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003713 * [ht40] [vht] */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003714
3715 if (hwaddr_aton(cmd, addr))
3716 return -1;
3717
3718 pos = cmd + 17;
3719 if (*pos != ' ')
3720 return -1;
3721 pos++;
3722
3723 persistent_group = os_strstr(pos, " persistent") != NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003724 pos2 = os_strstr(pos, " persistent=");
3725 if (pos2) {
3726 struct wpa_ssid *ssid;
3727 persistent_id = atoi(pos2 + 12);
3728 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3729 if (ssid == NULL || ssid->disabled != 2 ||
3730 ssid->mode != WPAS_MODE_P2P_GO) {
3731 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3732 "SSID id=%d for persistent P2P group (GO)",
3733 persistent_id);
3734 return -1;
3735 }
3736 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003737 join = os_strstr(pos, " join") != NULL;
3738 auth = os_strstr(pos, " auth") != NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003739 automatic = os_strstr(pos, " auto") != NULL;
3740 pd = os_strstr(pos, " provdisc") != NULL;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003741 vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
3742 ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
3743 vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003744
3745 pos2 = os_strstr(pos, " go_intent=");
3746 if (pos2) {
3747 pos2 += 11;
3748 go_intent = atoi(pos2);
3749 if (go_intent < 0 || go_intent > 15)
3750 return -1;
3751 }
3752
3753 pos2 = os_strstr(pos, " freq=");
3754 if (pos2) {
3755 pos2 += 6;
3756 freq = atoi(pos2);
3757 if (freq <= 0)
3758 return -1;
3759 }
3760
3761 if (os_strncmp(pos, "pin", 3) == 0) {
3762 /* Request random PIN (to be displayed) and enable the PIN */
3763 wps_method = WPS_PIN_DISPLAY;
3764 } else if (os_strncmp(pos, "pbc", 3) == 0) {
3765 wps_method = WPS_PBC;
3766 } else {
3767 pin = pos;
3768 pos = os_strchr(pin, ' ');
3769 wps_method = WPS_PIN_KEYPAD;
3770 if (pos) {
3771 *pos++ = '\0';
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003772 if (os_strncmp(pos, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003773 wps_method = WPS_PIN_DISPLAY;
3774 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003775 if (!wps_pin_str_valid(pin)) {
3776 os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
3777 return 17;
3778 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003779 }
3780
3781 new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003782 persistent_group, automatic, join,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003783 auth, go_intent, freq, persistent_id, pd,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003784 ht40, vht);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003785 if (new_pin == -2) {
3786 os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
3787 return 25;
3788 }
3789 if (new_pin == -3) {
3790 os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
3791 return 25;
3792 }
3793 if (new_pin < 0)
3794 return -1;
3795 if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
3796 ret = os_snprintf(buf, buflen, "%08d", new_pin);
3797 if (ret < 0 || (size_t) ret >= buflen)
3798 return -1;
3799 return ret;
3800 }
3801
3802 os_memcpy(buf, "OK\n", 3);
3803 return 3;
3804}
3805
3806
3807static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
3808{
3809 unsigned int timeout = atoi(cmd);
3810 return wpas_p2p_listen(wpa_s, timeout);
3811}
3812
3813
3814static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
3815{
3816 u8 addr[ETH_ALEN];
3817 char *pos;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003818 enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003819
Dmitry Shmidt04949592012-07-19 12:16:46 -07003820 /* <addr> <config method> [join|auto] */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003821
3822 if (hwaddr_aton(cmd, addr))
3823 return -1;
3824
3825 pos = cmd + 17;
3826 if (*pos != ' ')
3827 return -1;
3828 pos++;
3829
Dmitry Shmidt04949592012-07-19 12:16:46 -07003830 if (os_strstr(pos, " join") != NULL)
3831 use = WPAS_P2P_PD_FOR_JOIN;
3832 else if (os_strstr(pos, " auto") != NULL)
3833 use = WPAS_P2P_PD_AUTO;
3834
3835 return wpas_p2p_prov_disc(wpa_s, addr, pos, use);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003836}
3837
3838
3839static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
3840 size_t buflen)
3841{
3842 struct wpa_ssid *ssid = wpa_s->current_ssid;
3843
3844 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3845 ssid->passphrase == NULL)
3846 return -1;
3847
3848 os_strlcpy(buf, ssid->passphrase, buflen);
3849 return os_strlen(buf);
3850}
3851
3852
3853static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
3854 char *buf, size_t buflen)
3855{
3856 u64 ref;
3857 int res;
3858 u8 dst_buf[ETH_ALEN], *dst;
3859 struct wpabuf *tlvs;
3860 char *pos;
3861 size_t len;
3862
3863 if (hwaddr_aton(cmd, dst_buf))
3864 return -1;
3865 dst = dst_buf;
3866 if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
3867 dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
3868 dst = NULL;
3869 pos = cmd + 17;
3870 if (*pos != ' ')
3871 return -1;
3872 pos++;
3873
3874 if (os_strncmp(pos, "upnp ", 5) == 0) {
3875 u8 version;
3876 pos += 5;
3877 if (hexstr2bin(pos, &version, 1) < 0)
3878 return -1;
3879 pos += 2;
3880 if (*pos != ' ')
3881 return -1;
3882 pos++;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003883 ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003884#ifdef CONFIG_WIFI_DISPLAY
3885 } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
3886 ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
3887#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003888 } else {
3889 len = os_strlen(pos);
3890 if (len & 1)
3891 return -1;
3892 len /= 2;
3893 tlvs = wpabuf_alloc(len);
3894 if (tlvs == NULL)
3895 return -1;
3896 if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
3897 wpabuf_free(tlvs);
3898 return -1;
3899 }
3900
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003901 ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003902 wpabuf_free(tlvs);
3903 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003904 if (ref == 0)
3905 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003906 res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
3907 if (res < 0 || (unsigned) res >= buflen)
3908 return -1;
3909 return res;
3910}
3911
3912
3913static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
3914 char *cmd)
3915{
3916 long long unsigned val;
3917 u64 req;
3918 if (sscanf(cmd, "%llx", &val) != 1)
3919 return -1;
3920 req = val;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003921 return wpas_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003922}
3923
3924
3925static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
3926{
3927 int freq;
3928 u8 dst[ETH_ALEN];
3929 u8 dialog_token;
3930 struct wpabuf *resp_tlvs;
3931 char *pos, *pos2;
3932 size_t len;
3933
3934 pos = os_strchr(cmd, ' ');
3935 if (pos == NULL)
3936 return -1;
3937 *pos++ = '\0';
3938 freq = atoi(cmd);
3939 if (freq == 0)
3940 return -1;
3941
3942 if (hwaddr_aton(pos, dst))
3943 return -1;
3944 pos += 17;
3945 if (*pos != ' ')
3946 return -1;
3947 pos++;
3948
3949 pos2 = os_strchr(pos, ' ');
3950 if (pos2 == NULL)
3951 return -1;
3952 *pos2++ = '\0';
3953 dialog_token = atoi(pos);
3954
3955 len = os_strlen(pos2);
3956 if (len & 1)
3957 return -1;
3958 len /= 2;
3959 resp_tlvs = wpabuf_alloc(len);
3960 if (resp_tlvs == NULL)
3961 return -1;
3962 if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
3963 wpabuf_free(resp_tlvs);
3964 return -1;
3965 }
3966
3967 wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
3968 wpabuf_free(resp_tlvs);
3969 return 0;
3970}
3971
3972
3973static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
3974 char *cmd)
3975{
Dmitry Shmidt04949592012-07-19 12:16:46 -07003976 if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
3977 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003978 wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
3979 return 0;
3980}
3981
3982
3983static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
3984 char *cmd)
3985{
3986 char *pos;
3987 size_t len;
3988 struct wpabuf *query, *resp;
3989
3990 pos = os_strchr(cmd, ' ');
3991 if (pos == NULL)
3992 return -1;
3993 *pos++ = '\0';
3994
3995 len = os_strlen(cmd);
3996 if (len & 1)
3997 return -1;
3998 len /= 2;
3999 query = wpabuf_alloc(len);
4000 if (query == NULL)
4001 return -1;
4002 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
4003 wpabuf_free(query);
4004 return -1;
4005 }
4006
4007 len = os_strlen(pos);
4008 if (len & 1) {
4009 wpabuf_free(query);
4010 return -1;
4011 }
4012 len /= 2;
4013 resp = wpabuf_alloc(len);
4014 if (resp == NULL) {
4015 wpabuf_free(query);
4016 return -1;
4017 }
4018 if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
4019 wpabuf_free(query);
4020 wpabuf_free(resp);
4021 return -1;
4022 }
4023
4024 if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
4025 wpabuf_free(query);
4026 wpabuf_free(resp);
4027 return -1;
4028 }
4029 return 0;
4030}
4031
4032
4033static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
4034{
4035 char *pos;
4036 u8 version;
4037
4038 pos = os_strchr(cmd, ' ');
4039 if (pos == NULL)
4040 return -1;
4041 *pos++ = '\0';
4042
4043 if (hexstr2bin(cmd, &version, 1) < 0)
4044 return -1;
4045
4046 return wpas_p2p_service_add_upnp(wpa_s, version, pos);
4047}
4048
4049
4050static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
4051{
4052 char *pos;
4053
4054 pos = os_strchr(cmd, ' ');
4055 if (pos == NULL)
4056 return -1;
4057 *pos++ = '\0';
4058
4059 if (os_strcmp(cmd, "bonjour") == 0)
4060 return p2p_ctrl_service_add_bonjour(wpa_s, pos);
4061 if (os_strcmp(cmd, "upnp") == 0)
4062 return p2p_ctrl_service_add_upnp(wpa_s, pos);
4063 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
4064 return -1;
4065}
4066
4067
4068static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
4069 char *cmd)
4070{
4071 size_t len;
4072 struct wpabuf *query;
4073 int ret;
4074
4075 len = os_strlen(cmd);
4076 if (len & 1)
4077 return -1;
4078 len /= 2;
4079 query = wpabuf_alloc(len);
4080 if (query == NULL)
4081 return -1;
4082 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
4083 wpabuf_free(query);
4084 return -1;
4085 }
4086
4087 ret = wpas_p2p_service_del_bonjour(wpa_s, query);
4088 wpabuf_free(query);
4089 return ret;
4090}
4091
4092
4093static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
4094{
4095 char *pos;
4096 u8 version;
4097
4098 pos = os_strchr(cmd, ' ');
4099 if (pos == NULL)
4100 return -1;
4101 *pos++ = '\0';
4102
4103 if (hexstr2bin(cmd, &version, 1) < 0)
4104 return -1;
4105
4106 return wpas_p2p_service_del_upnp(wpa_s, version, pos);
4107}
4108
4109
4110static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
4111{
4112 char *pos;
4113
4114 pos = os_strchr(cmd, ' ');
4115 if (pos == NULL)
4116 return -1;
4117 *pos++ = '\0';
4118
4119 if (os_strcmp(cmd, "bonjour") == 0)
4120 return p2p_ctrl_service_del_bonjour(wpa_s, pos);
4121 if (os_strcmp(cmd, "upnp") == 0)
4122 return p2p_ctrl_service_del_upnp(wpa_s, pos);
4123 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
4124 return -1;
4125}
4126
4127
4128static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
4129{
4130 u8 addr[ETH_ALEN];
4131
4132 /* <addr> */
4133
4134 if (hwaddr_aton(cmd, addr))
4135 return -1;
4136
4137 return wpas_p2p_reject(wpa_s, addr);
4138}
4139
4140
4141static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
4142{
4143 char *pos;
4144 int id;
4145 struct wpa_ssid *ssid;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004146 u8 *_peer = NULL, peer[ETH_ALEN];
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004147 int freq = 0, pref_freq = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004148 int ht40, vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004149
4150 id = atoi(cmd);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004151 pos = os_strstr(cmd, " peer=");
4152 if (pos) {
4153 pos += 6;
4154 if (hwaddr_aton(pos, peer))
4155 return -1;
4156 _peer = peer;
4157 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004158 ssid = wpa_config_get_network(wpa_s->conf, id);
4159 if (ssid == NULL || ssid->disabled != 2) {
4160 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
4161 "for persistent P2P group",
4162 id);
4163 return -1;
4164 }
4165
Jouni Malinen31be0a42012-08-31 21:20:51 +03004166 pos = os_strstr(cmd, " freq=");
4167 if (pos) {
4168 pos += 6;
4169 freq = atoi(pos);
4170 if (freq <= 0)
4171 return -1;
4172 }
4173
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004174 pos = os_strstr(cmd, " pref=");
4175 if (pos) {
4176 pos += 6;
4177 pref_freq = atoi(pos);
4178 if (pref_freq <= 0)
4179 return -1;
4180 }
4181
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004182 vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
4183 ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
4184 vht;
Jouni Malinen31be0a42012-08-31 21:20:51 +03004185
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004186 return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, ht40, vht,
4187 pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004188}
4189
4190
4191static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
4192{
4193 char *pos;
4194 u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
4195
4196 pos = os_strstr(cmd, " peer=");
4197 if (!pos)
4198 return -1;
4199
4200 *pos = '\0';
4201 pos += 6;
4202 if (hwaddr_aton(pos, peer)) {
4203 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
4204 return -1;
4205 }
4206
4207 pos = os_strstr(pos, " go_dev_addr=");
4208 if (pos) {
4209 pos += 13;
4210 if (hwaddr_aton(pos, go_dev_addr)) {
4211 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
4212 pos);
4213 return -1;
4214 }
4215 go_dev = go_dev_addr;
4216 }
4217
4218 return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
4219}
4220
4221
4222static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
4223{
4224 if (os_strncmp(cmd, "persistent=", 11) == 0)
4225 return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
4226 if (os_strncmp(cmd, "group=", 6) == 0)
4227 return p2p_ctrl_invite_group(wpa_s, cmd + 6);
4228
4229 return -1;
4230}
4231
4232
4233static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004234 char *cmd, int freq, int ht40,
4235 int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004236{
4237 int id;
4238 struct wpa_ssid *ssid;
4239
4240 id = atoi(cmd);
4241 ssid = wpa_config_get_network(wpa_s->conf, id);
4242 if (ssid == NULL || ssid->disabled != 2) {
4243 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
4244 "for persistent P2P group",
4245 id);
4246 return -1;
4247 }
4248
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004249 return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq, ht40, vht,
4250 NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004251}
4252
4253
4254static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
4255{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004256 int freq = 0, ht40, vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004257 char *pos;
4258
4259 pos = os_strstr(cmd, "freq=");
4260 if (pos)
4261 freq = atoi(pos + 5);
4262
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004263 vht = (os_strstr(cmd, "vht") != NULL) || wpa_s->conf->p2p_go_vht;
4264 ht40 = (os_strstr(cmd, "ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
4265 vht;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004266
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004267 if (os_strncmp(cmd, "persistent=", 11) == 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004268 return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004269 ht40, vht);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004270 if (os_strcmp(cmd, "persistent") == 0 ||
4271 os_strncmp(cmd, "persistent ", 11) == 0)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004272 return wpas_p2p_group_add(wpa_s, 1, freq, ht40, vht);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004273 if (os_strncmp(cmd, "freq=", 5) == 0)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004274 return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004275 if (ht40)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004276 return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004277
4278 wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
4279 cmd);
4280 return -1;
4281}
4282
4283
4284static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
4285 char *buf, size_t buflen)
4286{
4287 u8 addr[ETH_ALEN], *addr_ptr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004288 int next, res;
4289 const struct p2p_peer_info *info;
4290 char *pos, *end;
4291 char devtype[WPS_DEV_TYPE_BUFSIZE];
4292 struct wpa_ssid *ssid;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004293 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004294
4295 if (!wpa_s->global->p2p)
4296 return -1;
4297
4298 if (os_strcmp(cmd, "FIRST") == 0) {
4299 addr_ptr = NULL;
4300 next = 0;
4301 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
4302 if (hwaddr_aton(cmd + 5, addr) < 0)
4303 return -1;
4304 addr_ptr = addr;
4305 next = 1;
4306 } else {
4307 if (hwaddr_aton(cmd, addr) < 0)
4308 return -1;
4309 addr_ptr = addr;
4310 next = 0;
4311 }
4312
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004313 info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
4314 if (info == NULL)
4315 return -1;
4316
4317 pos = buf;
4318 end = buf + buflen;
4319
4320 res = os_snprintf(pos, end - pos, MACSTR "\n"
4321 "pri_dev_type=%s\n"
4322 "device_name=%s\n"
4323 "manufacturer=%s\n"
4324 "model_name=%s\n"
4325 "model_number=%s\n"
4326 "serial_number=%s\n"
4327 "config_methods=0x%x\n"
4328 "dev_capab=0x%x\n"
4329 "group_capab=0x%x\n"
4330 "level=%d\n",
4331 MAC2STR(info->p2p_device_addr),
4332 wps_dev_type_bin2str(info->pri_dev_type,
4333 devtype, sizeof(devtype)),
4334 info->device_name,
4335 info->manufacturer,
4336 info->model_name,
4337 info->model_number,
4338 info->serial_number,
4339 info->config_methods,
4340 info->dev_capab,
4341 info->group_capab,
4342 info->level);
4343 if (res < 0 || res >= end - pos)
4344 return pos - buf;
4345 pos += res;
4346
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004347 for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
4348 {
4349 const u8 *t;
4350 t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
4351 res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
4352 wps_dev_type_bin2str(t, devtype,
4353 sizeof(devtype)));
4354 if (res < 0 || res >= end - pos)
4355 return pos - buf;
4356 pos += res;
4357 }
4358
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004359 ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004360 if (ssid) {
4361 res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
4362 if (res < 0 || res >= end - pos)
4363 return pos - buf;
4364 pos += res;
4365 }
4366
4367 res = p2p_get_peer_info_txt(info, pos, end - pos);
4368 if (res < 0)
4369 return pos - buf;
4370 pos += res;
4371
4372 return pos - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004373}
4374
4375
Dmitry Shmidt04949592012-07-19 12:16:46 -07004376static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
4377 const char *param)
4378{
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07004379 unsigned int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004380
4381 if (wpa_s->global->p2p == NULL)
4382 return -1;
4383
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07004384 if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
4385 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004386
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07004387 for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
4388 struct wpa_freq_range *freq;
4389 freq = &wpa_s->global->p2p_disallow_freq.range[i];
Dmitry Shmidt04949592012-07-19 12:16:46 -07004390 wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07004391 freq->min, freq->max);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004392 }
4393
Dmitry Shmidt04949592012-07-19 12:16:46 -07004394 wpas_p2p_update_channel_list(wpa_s);
4395 return 0;
4396}
4397
4398
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004399static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
4400{
4401 char *param;
4402
4403 if (wpa_s->global->p2p == NULL)
4404 return -1;
4405
4406 param = os_strchr(cmd, ' ');
4407 if (param == NULL)
4408 return -1;
4409 *param++ = '\0';
4410
4411 if (os_strcmp(cmd, "discoverability") == 0) {
4412 p2p_set_client_discoverability(wpa_s->global->p2p,
4413 atoi(param));
4414 return 0;
4415 }
4416
4417 if (os_strcmp(cmd, "managed") == 0) {
4418 p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
4419 return 0;
4420 }
4421
4422 if (os_strcmp(cmd, "listen_channel") == 0) {
4423 return p2p_set_listen_channel(wpa_s->global->p2p, 81,
4424 atoi(param));
4425 }
4426
4427 if (os_strcmp(cmd, "ssid_postfix") == 0) {
4428 return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
4429 os_strlen(param));
4430 }
4431
4432 if (os_strcmp(cmd, "noa") == 0) {
4433 char *pos;
4434 int count, start, duration;
4435 /* GO NoA parameters: count,start_offset(ms),duration(ms) */
4436 count = atoi(param);
4437 pos = os_strchr(param, ',');
4438 if (pos == NULL)
4439 return -1;
4440 pos++;
4441 start = atoi(pos);
4442 pos = os_strchr(pos, ',');
4443 if (pos == NULL)
4444 return -1;
4445 pos++;
4446 duration = atoi(pos);
4447 if (count < 0 || count > 255 || start < 0 || duration < 0)
4448 return -1;
4449 if (count == 0 && duration > 0)
4450 return -1;
4451 wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
4452 "start=%d duration=%d", count, start, duration);
4453 return wpas_p2p_set_noa(wpa_s, count, start, duration);
4454 }
4455
4456 if (os_strcmp(cmd, "ps") == 0)
4457 return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
4458
4459 if (os_strcmp(cmd, "oppps") == 0)
4460 return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
4461
4462 if (os_strcmp(cmd, "ctwindow") == 0)
4463 return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
4464
4465 if (os_strcmp(cmd, "disabled") == 0) {
4466 wpa_s->global->p2p_disabled = atoi(param);
4467 wpa_printf(MSG_DEBUG, "P2P functionality %s",
4468 wpa_s->global->p2p_disabled ?
4469 "disabled" : "enabled");
4470 if (wpa_s->global->p2p_disabled) {
4471 wpas_p2p_stop_find(wpa_s);
4472 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
4473 p2p_flush(wpa_s->global->p2p);
4474 }
4475 return 0;
4476 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004477
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07004478 if (os_strcmp(cmd, "conc_pref") == 0) {
4479 if (os_strcmp(param, "sta") == 0)
4480 wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
4481 else if (os_strcmp(param, "p2p") == 0)
4482 wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004483 else {
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07004484 wpa_printf(MSG_INFO, "Invalid conc_pref value");
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004485 return -1;
4486 }
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07004487 wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
Dmitry Shmidt04949592012-07-19 12:16:46 -07004488 "%s", param);
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004489 return 0;
4490 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004491
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004492 if (os_strcmp(cmd, "force_long_sd") == 0) {
4493 wpa_s->force_long_sd = atoi(param);
4494 return 0;
4495 }
4496
4497 if (os_strcmp(cmd, "peer_filter") == 0) {
4498 u8 addr[ETH_ALEN];
4499 if (hwaddr_aton(param, addr))
4500 return -1;
4501 p2p_set_peer_filter(wpa_s->global->p2p, addr);
4502 return 0;
4503 }
4504
4505 if (os_strcmp(cmd, "cross_connect") == 0)
4506 return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
4507
4508 if (os_strcmp(cmd, "go_apsd") == 0) {
4509 if (os_strcmp(param, "disable") == 0)
4510 wpa_s->set_ap_uapsd = 0;
4511 else {
4512 wpa_s->set_ap_uapsd = 1;
4513 wpa_s->ap_uapsd = atoi(param);
4514 }
4515 return 0;
4516 }
4517
4518 if (os_strcmp(cmd, "client_apsd") == 0) {
4519 if (os_strcmp(param, "disable") == 0)
4520 wpa_s->set_sta_uapsd = 0;
4521 else {
4522 int be, bk, vi, vo;
4523 char *pos;
4524 /* format: BE,BK,VI,VO;max SP Length */
4525 be = atoi(param);
4526 pos = os_strchr(param, ',');
4527 if (pos == NULL)
4528 return -1;
4529 pos++;
4530 bk = atoi(pos);
4531 pos = os_strchr(pos, ',');
4532 if (pos == NULL)
4533 return -1;
4534 pos++;
4535 vi = atoi(pos);
4536 pos = os_strchr(pos, ',');
4537 if (pos == NULL)
4538 return -1;
4539 pos++;
4540 vo = atoi(pos);
4541 /* ignore max SP Length for now */
4542
4543 wpa_s->set_sta_uapsd = 1;
4544 wpa_s->sta_uapsd = 0;
4545 if (be)
4546 wpa_s->sta_uapsd |= BIT(0);
4547 if (bk)
4548 wpa_s->sta_uapsd |= BIT(1);
4549 if (vi)
4550 wpa_s->sta_uapsd |= BIT(2);
4551 if (vo)
4552 wpa_s->sta_uapsd |= BIT(3);
4553 }
4554 return 0;
4555 }
4556
Dmitry Shmidt04949592012-07-19 12:16:46 -07004557 if (os_strcmp(cmd, "disallow_freq") == 0)
4558 return p2p_ctrl_disallow_freq(wpa_s, param);
4559
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004560 if (os_strcmp(cmd, "disc_int") == 0) {
4561 int min_disc_int, max_disc_int, max_disc_tu;
4562 char *pos;
4563
4564 pos = param;
4565
4566 min_disc_int = atoi(pos);
4567 pos = os_strchr(pos, ' ');
4568 if (pos == NULL)
4569 return -1;
4570 *pos++ = '\0';
4571
4572 max_disc_int = atoi(pos);
4573 pos = os_strchr(pos, ' ');
4574 if (pos == NULL)
4575 return -1;
4576 *pos++ = '\0';
4577
4578 max_disc_tu = atoi(pos);
4579
4580 return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
4581 max_disc_int, max_disc_tu);
4582 }
4583
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004584 if (os_strcmp(cmd, "per_sta_psk") == 0) {
4585 wpa_s->global->p2p_per_sta_psk = !!atoi(param);
4586 return 0;
4587 }
4588
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004589 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
4590 cmd);
4591
4592 return -1;
4593}
4594
4595
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004596static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
4597{
4598 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
4599 wpa_s->force_long_sd = 0;
4600 if (wpa_s->global->p2p)
4601 p2p_flush(wpa_s->global->p2p);
4602}
4603
4604
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004605static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
4606{
4607 char *pos, *pos2;
4608 unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
4609
4610 if (cmd[0]) {
4611 pos = os_strchr(cmd, ' ');
4612 if (pos == NULL)
4613 return -1;
4614 *pos++ = '\0';
4615 dur1 = atoi(cmd);
4616
4617 pos2 = os_strchr(pos, ' ');
4618 if (pos2)
4619 *pos2++ = '\0';
4620 int1 = atoi(pos);
4621 } else
4622 pos2 = NULL;
4623
4624 if (pos2) {
4625 pos = os_strchr(pos2, ' ');
4626 if (pos == NULL)
4627 return -1;
4628 *pos++ = '\0';
4629 dur2 = atoi(pos2);
4630 int2 = atoi(pos);
4631 }
4632
4633 return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
4634}
4635
4636
4637static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
4638{
4639 char *pos;
4640 unsigned int period = 0, interval = 0;
4641
4642 if (cmd[0]) {
4643 pos = os_strchr(cmd, ' ');
4644 if (pos == NULL)
4645 return -1;
4646 *pos++ = '\0';
4647 period = atoi(cmd);
4648 interval = atoi(pos);
4649 }
4650
4651 return wpas_p2p_ext_listen(wpa_s, period, interval);
4652}
4653
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004654
4655static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
4656{
4657 const char *pos;
4658 u8 peer[ETH_ALEN];
4659 int iface_addr = 0;
4660
4661 pos = cmd;
4662 if (os_strncmp(pos, "iface=", 6) == 0) {
4663 iface_addr = 1;
4664 pos += 6;
4665 }
4666 if (hwaddr_aton(pos, peer))
4667 return -1;
4668
4669 wpas_p2p_remove_client(wpa_s, peer, iface_addr);
4670 return 0;
4671}
4672
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004673#endif /* CONFIG_P2P */
4674
4675
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004676static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s, char *val)
4677{
4678 struct wpa_freq_range_list ranges;
4679 int *freqs = NULL;
4680 struct hostapd_hw_modes *mode;
4681 u16 i;
4682
4683 if (wpa_s->hw.modes == NULL)
4684 return NULL;
4685
4686 os_memset(&ranges, 0, sizeof(ranges));
4687 if (freq_range_list_parse(&ranges, val) < 0)
4688 return NULL;
4689
4690 for (i = 0; i < wpa_s->hw.num_modes; i++) {
4691 int j;
4692
4693 mode = &wpa_s->hw.modes[i];
4694 for (j = 0; j < mode->num_channels; j++) {
4695 unsigned int freq;
4696
4697 if (mode->channels[j].flag & HOSTAPD_CHAN_DISABLED)
4698 continue;
4699
4700 freq = mode->channels[j].freq;
4701 if (!freq_range_list_includes(&ranges, freq))
4702 continue;
4703
4704 int_array_add_unique(&freqs, freq);
4705 }
4706 }
4707
4708 os_free(ranges.range);
4709 return freqs;
4710}
4711
4712
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004713#ifdef CONFIG_INTERWORKING
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004714
4715static int ctrl_interworking_select(struct wpa_supplicant *wpa_s, char *param)
4716{
4717 int auto_sel = 0;
4718 int *freqs = NULL;
4719
4720 if (param) {
4721 char *pos;
4722
4723 auto_sel = os_strstr(param, "auto") != NULL;
4724
4725 pos = os_strstr(param, "freq=");
4726 if (pos) {
4727 freqs = freq_range_to_channel_list(wpa_s, pos + 5);
4728 if (freqs == NULL)
4729 return -1;
4730 }
4731
4732 }
4733
4734 return interworking_select(wpa_s, auto_sel, freqs);
4735}
4736
4737
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004738static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst)
4739{
4740 u8 bssid[ETH_ALEN];
4741 struct wpa_bss *bss;
4742
4743 if (hwaddr_aton(dst, bssid)) {
4744 wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
4745 return -1;
4746 }
4747
4748 bss = wpa_bss_get_bssid(wpa_s, bssid);
4749 if (bss == NULL) {
4750 wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
4751 MAC2STR(bssid));
4752 return -1;
4753 }
4754
4755 return interworking_connect(wpa_s, bss);
4756}
4757
4758
4759static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
4760{
4761 u8 dst_addr[ETH_ALEN];
4762 int used;
4763 char *pos;
4764#define MAX_ANQP_INFO_ID 100
4765 u16 id[MAX_ANQP_INFO_ID];
4766 size_t num_id = 0;
4767
4768 used = hwaddr_aton2(dst, dst_addr);
4769 if (used < 0)
4770 return -1;
4771 pos = dst + used;
4772 while (num_id < MAX_ANQP_INFO_ID) {
4773 id[num_id] = atoi(pos);
4774 if (id[num_id])
4775 num_id++;
4776 pos = os_strchr(pos + 1, ',');
4777 if (pos == NULL)
4778 break;
4779 pos++;
4780 }
4781
4782 if (num_id == 0)
4783 return -1;
4784
4785 return anqp_send_req(wpa_s, dst_addr, id, num_id);
4786}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004787
4788
4789static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
4790{
4791 u8 dst_addr[ETH_ALEN];
4792 struct wpabuf *advproto, *query = NULL;
4793 int used, ret = -1;
4794 char *pos, *end;
4795 size_t len;
4796
4797 used = hwaddr_aton2(cmd, dst_addr);
4798 if (used < 0)
4799 return -1;
4800
4801 pos = cmd + used;
4802 while (*pos == ' ')
4803 pos++;
4804
4805 /* Advertisement Protocol ID */
4806 end = os_strchr(pos, ' ');
4807 if (end)
4808 len = end - pos;
4809 else
4810 len = os_strlen(pos);
4811 if (len & 0x01)
4812 return -1;
4813 len /= 2;
4814 if (len == 0)
4815 return -1;
4816 advproto = wpabuf_alloc(len);
4817 if (advproto == NULL)
4818 return -1;
4819 if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
4820 goto fail;
4821
4822 if (end) {
4823 /* Optional Query Request */
4824 pos = end + 1;
4825 while (*pos == ' ')
4826 pos++;
4827
4828 len = os_strlen(pos);
4829 if (len) {
4830 if (len & 0x01)
4831 goto fail;
4832 len /= 2;
4833 if (len == 0)
4834 goto fail;
4835 query = wpabuf_alloc(len);
4836 if (query == NULL)
4837 goto fail;
4838 if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
4839 goto fail;
4840 }
4841 }
4842
4843 ret = gas_send_request(wpa_s, dst_addr, advproto, query);
4844
4845fail:
4846 wpabuf_free(advproto);
4847 wpabuf_free(query);
4848
4849 return ret;
4850}
4851
4852
4853static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
4854 size_t buflen)
4855{
4856 u8 addr[ETH_ALEN];
4857 int dialog_token;
4858 int used;
4859 char *pos;
4860 size_t resp_len, start, requested_len;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004861 struct wpabuf *resp;
4862 int ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004863
4864 used = hwaddr_aton2(cmd, addr);
4865 if (used < 0)
4866 return -1;
4867
4868 pos = cmd + used;
4869 while (*pos == ' ')
4870 pos++;
4871 dialog_token = atoi(pos);
4872
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004873 if (wpa_s->last_gas_resp &&
4874 os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) == 0 &&
4875 dialog_token == wpa_s->last_gas_dialog_token)
4876 resp = wpa_s->last_gas_resp;
4877 else if (wpa_s->prev_gas_resp &&
4878 os_memcmp(addr, wpa_s->prev_gas_addr, ETH_ALEN) == 0 &&
4879 dialog_token == wpa_s->prev_gas_dialog_token)
4880 resp = wpa_s->prev_gas_resp;
4881 else
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004882 return -1;
4883
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004884 resp_len = wpabuf_len(resp);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004885 start = 0;
4886 requested_len = resp_len;
4887
4888 pos = os_strchr(pos, ' ');
4889 if (pos) {
4890 start = atoi(pos);
4891 if (start > resp_len)
4892 return os_snprintf(buf, buflen, "FAIL-Invalid range");
4893 pos = os_strchr(pos, ',');
4894 if (pos == NULL)
4895 return -1;
4896 pos++;
4897 requested_len = atoi(pos);
4898 if (start + requested_len > resp_len)
4899 return os_snprintf(buf, buflen, "FAIL-Invalid range");
4900 }
4901
4902 if (requested_len * 2 + 1 > buflen)
4903 return os_snprintf(buf, buflen, "FAIL-Too long response");
4904
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004905 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(resp) + start,
4906 requested_len);
4907
4908 if (start + requested_len == resp_len) {
4909 /*
4910 * Free memory by dropping the response after it has been
4911 * fetched.
4912 */
4913 if (resp == wpa_s->prev_gas_resp) {
4914 wpabuf_free(wpa_s->prev_gas_resp);
4915 wpa_s->prev_gas_resp = NULL;
4916 } else {
4917 wpabuf_free(wpa_s->last_gas_resp);
4918 wpa_s->last_gas_resp = NULL;
4919 }
4920 }
4921
4922 return ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004923}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004924#endif /* CONFIG_INTERWORKING */
4925
4926
Dmitry Shmidt04949592012-07-19 12:16:46 -07004927#ifdef CONFIG_HS20
4928
4929static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
4930{
4931 u8 dst_addr[ETH_ALEN];
4932 int used;
4933 char *pos;
4934 u32 subtypes = 0;
4935
4936 used = hwaddr_aton2(dst, dst_addr);
4937 if (used < 0)
4938 return -1;
4939 pos = dst + used;
4940 for (;;) {
4941 int num = atoi(pos);
4942 if (num <= 0 || num > 31)
4943 return -1;
4944 subtypes |= BIT(num);
4945 pos = os_strchr(pos + 1, ',');
4946 if (pos == NULL)
4947 break;
4948 pos++;
4949 }
4950
4951 if (subtypes == 0)
4952 return -1;
4953
4954 return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0);
4955}
4956
4957
4958static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
4959 const u8 *addr, const char *realm)
4960{
4961 u8 *buf;
4962 size_t rlen, len;
4963 int ret;
4964
4965 rlen = os_strlen(realm);
4966 len = 3 + rlen;
4967 buf = os_malloc(len);
4968 if (buf == NULL)
4969 return -1;
4970 buf[0] = 1; /* NAI Home Realm Count */
4971 buf[1] = 0; /* Formatted in accordance with RFC 4282 */
4972 buf[2] = rlen;
4973 os_memcpy(buf + 3, realm, rlen);
4974
4975 ret = hs20_anqp_send_req(wpa_s, addr,
4976 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
4977 buf, len);
4978
4979 os_free(buf);
4980
4981 return ret;
4982}
4983
4984
4985static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
4986 char *dst)
4987{
4988 struct wpa_cred *cred = wpa_s->conf->cred;
4989 u8 dst_addr[ETH_ALEN];
4990 int used;
4991 u8 *buf;
4992 size_t len;
4993 int ret;
4994
4995 used = hwaddr_aton2(dst, dst_addr);
4996 if (used < 0)
4997 return -1;
4998
4999 while (dst[used] == ' ')
5000 used++;
5001 if (os_strncmp(dst + used, "realm=", 6) == 0)
5002 return hs20_nai_home_realm_list(wpa_s, dst_addr,
5003 dst + used + 6);
5004
5005 len = os_strlen(dst + used);
5006
5007 if (len == 0 && cred && cred->realm)
5008 return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
5009
5010 if (len % 1)
5011 return -1;
5012 len /= 2;
5013 buf = os_malloc(len);
5014 if (buf == NULL)
5015 return -1;
5016 if (hexstr2bin(dst + used, buf, len) < 0) {
5017 os_free(buf);
5018 return -1;
5019 }
5020
5021 ret = hs20_anqp_send_req(wpa_s, dst_addr,
5022 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
5023 buf, len);
5024 os_free(buf);
5025
5026 return ret;
5027}
5028
5029#endif /* CONFIG_HS20 */
5030
5031
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005032static int wpa_supplicant_ctrl_iface_sta_autoconnect(
5033 struct wpa_supplicant *wpa_s, char *cmd)
5034{
5035 wpa_s->auto_reconnect_disabled = atoi(cmd) == 0 ? 1 : 0;
5036 return 0;
5037}
5038
5039
Dmitry Shmidt04949592012-07-19 12:16:46 -07005040#ifdef CONFIG_AUTOSCAN
5041
5042static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
5043 char *cmd)
5044{
5045 enum wpa_states state = wpa_s->wpa_state;
5046 char *new_params = NULL;
5047
5048 if (os_strlen(cmd) > 0) {
5049 new_params = os_strdup(cmd);
5050 if (new_params == NULL)
5051 return -1;
5052 }
5053
5054 os_free(wpa_s->conf->autoscan);
5055 wpa_s->conf->autoscan = new_params;
5056
5057 if (wpa_s->conf->autoscan == NULL)
5058 autoscan_deinit(wpa_s);
5059 else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
5060 autoscan_init(wpa_s, 1);
5061 else if (state == WPA_SCANNING)
5062 wpa_supplicant_reinit_autoscan(wpa_s);
5063
5064 return 0;
5065}
5066
5067#endif /* CONFIG_AUTOSCAN */
5068
5069
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005070#ifdef CONFIG_WNM
5071
5072static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
5073{
5074 int enter;
5075 int intval = 0;
5076 char *pos;
5077 int ret;
5078 struct wpabuf *tfs_req = NULL;
5079
5080 if (os_strncmp(cmd, "enter", 5) == 0)
5081 enter = 1;
5082 else if (os_strncmp(cmd, "exit", 4) == 0)
5083 enter = 0;
5084 else
5085 return -1;
5086
5087 pos = os_strstr(cmd, " interval=");
5088 if (pos)
5089 intval = atoi(pos + 10);
5090
5091 pos = os_strstr(cmd, " tfs_req=");
5092 if (pos) {
5093 char *end;
5094 size_t len;
5095 pos += 9;
5096 end = os_strchr(pos, ' ');
5097 if (end)
5098 len = end - pos;
5099 else
5100 len = os_strlen(pos);
5101 if (len & 1)
5102 return -1;
5103 len /= 2;
5104 tfs_req = wpabuf_alloc(len);
5105 if (tfs_req == NULL)
5106 return -1;
5107 if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
5108 wpabuf_free(tfs_req);
5109 return -1;
5110 }
5111 }
5112
5113 ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
5114 WNM_SLEEP_MODE_EXIT, intval,
5115 tfs_req);
5116 wpabuf_free(tfs_req);
5117
5118 return ret;
5119}
5120
Dmitry Shmidt44c95782013-05-17 09:51:35 -07005121
5122static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
5123{
5124 int query_reason;
5125
5126 query_reason = atoi(cmd);
5127
5128 wpa_printf(MSG_DEBUG, "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d",
5129 query_reason);
5130
5131 return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason);
5132}
5133
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005134#endif /* CONFIG_WNM */
5135
5136
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005137/* Get string representation of channel width */
5138static const char * channel_width_name(enum chan_width width)
5139{
5140 switch (width) {
5141 case CHAN_WIDTH_20_NOHT:
5142 return "20 MHz (no HT)";
5143 case CHAN_WIDTH_20:
5144 return "20 MHz";
5145 case CHAN_WIDTH_40:
5146 return "40 MHz";
5147 case CHAN_WIDTH_80:
5148 return "80 MHz";
5149 case CHAN_WIDTH_80P80:
5150 return "80+80 MHz";
5151 case CHAN_WIDTH_160:
5152 return "160 MHz";
5153 default:
5154 return "unknown";
5155 }
5156}
5157
5158
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005159static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
5160 size_t buflen)
5161{
5162 struct wpa_signal_info si;
5163 int ret;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005164 char *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005165
5166 ret = wpa_drv_signal_poll(wpa_s, &si);
5167 if (ret)
5168 return -1;
5169
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005170 pos = buf;
5171 end = buf + buflen;
5172
5173 ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005174 "NOISE=%d\nFREQUENCY=%u\n",
5175 si.current_signal, si.current_txrate / 1000,
5176 si.current_noise, si.frequency);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005177 if (ret < 0 || ret > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005178 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005179 pos += ret;
5180
5181 if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
5182 ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
5183 channel_width_name(si.chanwidth));
5184 if (ret < 0 || ret > end - pos)
5185 return -1;
5186 pos += ret;
5187 }
5188
5189 if (si.center_frq1 > 0 && si.center_frq2 > 0) {
5190 ret = os_snprintf(pos, end - pos,
5191 "CENTER_FRQ1=%d\nCENTER_FRQ2=%d\n",
5192 si.center_frq1, si.center_frq2);
5193 if (ret < 0 || ret > end - pos)
5194 return -1;
5195 pos += ret;
5196 }
5197
5198 if (si.avg_signal) {
5199 ret = os_snprintf(pos, end - pos,
5200 "AVG_RSSI=%d\n", si.avg_signal);
5201 if (ret < 0 || ret >= end - pos)
5202 return -1;
5203 pos += ret;
5204 }
5205
5206 return pos - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005207}
5208
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03005209
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07005210static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
5211 size_t buflen)
5212{
5213 struct hostap_sta_driver_data sta;
5214 int ret;
5215
5216 ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
5217 if (ret)
5218 return -1;
5219
5220 ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03005221 sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
5222 if (ret < 0 || (size_t) ret > buflen)
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07005223 return -1;
5224 return ret;
5225}
5226
5227
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005228#ifdef ANDROID
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07005229static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
5230 char *buf, size_t buflen)
5231{
5232 int ret;
5233
5234 ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
Dmitry Shmidt9432e122013-09-12 12:39:30 -07005235 if (ret == 0) {
5236 if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
5237 struct p2p_data *p2p = wpa_s->global->p2p;
5238 if (p2p) {
5239 char country[3];
5240 country[0] = cmd[8];
5241 country[1] = cmd[9];
5242 country[2] = 0x04;
5243 p2p_set_country(p2p, country);
5244 }
5245 }
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08005246 ret = os_snprintf(buf, buflen, "%s\n", "OK");
Dmitry Shmidt9432e122013-09-12 12:39:30 -07005247 }
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07005248 return ret;
5249}
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08005250#endif /* ANDROID */
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07005251
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07005252
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005253static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
5254{
5255 wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
5256
5257#ifdef CONFIG_P2P
5258 wpas_p2p_stop_find(wpa_s);
5259 p2p_ctrl_flush(wpa_s);
5260 wpas_p2p_group_remove(wpa_s, "*");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005261 wpas_p2p_service_flush(wpa_s);
5262 wpa_s->global->p2p_disabled = 0;
5263 wpa_s->global->p2p_per_sta_psk = 0;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005264 wpa_s->conf->num_sec_device_types = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005265#endif /* CONFIG_P2P */
5266
5267#ifdef CONFIG_WPS_TESTING
5268 wps_version_number = 0x20;
5269 wps_testing_dummy_cred = 0;
5270#endif /* CONFIG_WPS_TESTING */
5271#ifdef CONFIG_WPS
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005272 wpa_s->wps_fragment_size = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005273 wpas_wps_cancel(wpa_s);
5274#endif /* CONFIG_WPS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07005275 wpa_s->after_wps = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005276 wpa_s->known_wps_freq = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005277
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005278#ifdef CONFIG_TDLS
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005279#ifdef CONFIG_TDLS_TESTING
5280 extern unsigned int tdls_testing;
5281 tdls_testing = 0;
5282#endif /* CONFIG_TDLS_TESTING */
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005283 wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
5284 wpa_tdls_enable(wpa_s->wpa, 1);
5285#endif /* CONFIG_TDLS */
5286
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005287 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
5288 wpa_supplicant_stop_countermeasures(wpa_s, NULL);
5289
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005290 wpa_s->no_keep_alive = 0;
5291
5292 os_free(wpa_s->disallow_aps_bssid);
5293 wpa_s->disallow_aps_bssid = NULL;
5294 wpa_s->disallow_aps_bssid_count = 0;
5295 os_free(wpa_s->disallow_aps_ssid);
5296 wpa_s->disallow_aps_ssid = NULL;
5297 wpa_s->disallow_aps_ssid_count = 0;
5298
5299 wpa_s->set_sta_uapsd = 0;
5300 wpa_s->sta_uapsd = 0;
5301
5302 wpa_drv_radio_disable(wpa_s, 0);
5303
5304 wpa_bss_flush(wpa_s);
5305 wpa_blacklist_clear(wpa_s);
Dmitry Shmidt4b060592013-04-29 16:42:49 -07005306 wpa_s->extra_blacklist_count = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005307 wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
5308 wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005309 wpa_config_flush_blobs(wpa_s->conf);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005310
5311 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME, 43200);
5312 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD, 70);
5313 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, 60);
5314 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
5315
5316 radio_remove_unstarted_work(wpa_s, NULL);
5317}
5318
5319
5320static int wpas_ctrl_radio_work_show(struct wpa_supplicant *wpa_s,
5321 char *buf, size_t buflen)
5322{
5323 struct wpa_radio_work *work;
5324 char *pos, *end;
5325 struct os_reltime now, diff;
5326
5327 pos = buf;
5328 end = buf + buflen;
5329
5330 os_get_reltime(&now);
5331
5332 dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
5333 {
5334 int ret;
5335
5336 os_reltime_sub(&now, &work->time, &diff);
5337 ret = os_snprintf(pos, end - pos, "%s@%s:%u:%u:%ld.%06ld\n",
5338 work->type, work->wpa_s->ifname, work->freq,
5339 work->started, diff.sec, diff.usec);
5340 if (ret < 0 || ret >= end - pos)
5341 break;
5342 pos += ret;
5343 }
5344
5345 return pos - buf;
5346}
5347
5348
5349static void wpas_ctrl_radio_work_timeout(void *eloop_ctx, void *timeout_ctx)
5350{
5351 struct wpa_radio_work *work = eloop_ctx;
5352 struct wpa_external_work *ework = work->ctx;
5353
5354 wpa_dbg(work->wpa_s, MSG_DEBUG,
5355 "Timing out external radio work %u (%s)",
5356 ework->id, work->type);
5357 wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_TIMEOUT "%u", ework->id);
5358 os_free(ework);
5359 radio_work_done(work);
5360}
5361
5362
5363static void wpas_ctrl_radio_work_cb(struct wpa_radio_work *work, int deinit)
5364{
5365 struct wpa_external_work *ework = work->ctx;
5366
5367 if (deinit) {
5368 os_free(ework);
5369 return;
5370 }
5371
5372 wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting external radio work %u (%s)",
5373 ework->id, ework->type);
5374 wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_START "%u", ework->id);
5375 if (!ework->timeout)
5376 ework->timeout = 10;
5377 eloop_register_timeout(ework->timeout, 0, wpas_ctrl_radio_work_timeout,
5378 work, NULL);
5379}
5380
5381
5382static int wpas_ctrl_radio_work_add(struct wpa_supplicant *wpa_s, char *cmd,
5383 char *buf, size_t buflen)
5384{
5385 struct wpa_external_work *ework;
5386 char *pos, *pos2;
5387 size_t type_len;
5388 int ret;
5389 unsigned int freq = 0;
5390
5391 /* format: <name> [freq=<MHz>] [timeout=<seconds>] */
5392
5393 ework = os_zalloc(sizeof(*ework));
5394 if (ework == NULL)
5395 return -1;
5396
5397 pos = os_strchr(cmd, ' ');
5398 if (pos) {
5399 type_len = pos - cmd;
5400 pos++;
5401
5402 pos2 = os_strstr(pos, "freq=");
5403 if (pos2)
5404 freq = atoi(pos2 + 5);
5405
5406 pos2 = os_strstr(pos, "timeout=");
5407 if (pos2)
5408 ework->timeout = atoi(pos2 + 8);
5409 } else {
5410 type_len = os_strlen(cmd);
5411 }
5412 if (4 + type_len >= sizeof(ework->type))
5413 type_len = sizeof(ework->type) - 4 - 1;
5414 os_strlcpy(ework->type, "ext:", sizeof(ework->type));
5415 os_memcpy(ework->type + 4, cmd, type_len);
5416 ework->type[4 + type_len] = '\0';
5417
5418 wpa_s->ext_work_id++;
5419 if (wpa_s->ext_work_id == 0)
5420 wpa_s->ext_work_id++;
5421 ework->id = wpa_s->ext_work_id;
5422
5423 if (radio_add_work(wpa_s, freq, ework->type, 0, wpas_ctrl_radio_work_cb,
5424 ework) < 0) {
5425 os_free(ework);
5426 return -1;
5427 }
5428
5429 ret = os_snprintf(buf, buflen, "%u", ework->id);
5430 if (ret < 0 || (size_t) ret >= buflen)
5431 return -1;
5432 return ret;
5433}
5434
5435
5436static int wpas_ctrl_radio_work_done(struct wpa_supplicant *wpa_s, char *cmd)
5437{
5438 struct wpa_radio_work *work;
5439 unsigned int id = atoi(cmd);
5440
5441 dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
5442 {
5443 struct wpa_external_work *ework;
5444
5445 if (os_strncmp(work->type, "ext:", 4) != 0)
5446 continue;
5447 ework = work->ctx;
5448 if (id && ework->id != id)
5449 continue;
5450 wpa_dbg(wpa_s, MSG_DEBUG,
5451 "Completed external radio work %u (%s)",
5452 ework->id, ework->type);
5453 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout, work, NULL);
5454 os_free(ework);
5455 radio_work_done(work);
5456 return 3; /* "OK\n" */
5457 }
5458
5459 return -1;
5460}
5461
5462
5463static int wpas_ctrl_radio_work(struct wpa_supplicant *wpa_s, char *cmd,
5464 char *buf, size_t buflen)
5465{
5466 if (os_strcmp(cmd, "show") == 0)
5467 return wpas_ctrl_radio_work_show(wpa_s, buf, buflen);
5468 if (os_strncmp(cmd, "add ", 4) == 0)
5469 return wpas_ctrl_radio_work_add(wpa_s, cmd + 4, buf, buflen);
5470 if (os_strncmp(cmd, "done ", 5) == 0)
5471 return wpas_ctrl_radio_work_done(wpa_s, cmd + 4);
5472 return -1;
5473}
5474
5475
5476void wpas_ctrl_radio_work_flush(struct wpa_supplicant *wpa_s)
5477{
5478 struct wpa_radio_work *work, *tmp;
5479
5480 dl_list_for_each_safe(work, tmp, &wpa_s->radio->work,
5481 struct wpa_radio_work, list) {
5482 struct wpa_external_work *ework;
5483
5484 if (os_strncmp(work->type, "ext:", 4) != 0)
5485 continue;
5486 ework = work->ctx;
5487 wpa_dbg(wpa_s, MSG_DEBUG,
5488 "Flushing %sexternal radio work %u (%s)",
5489 work->started ? " started" : "", ework->id,
5490 ework->type);
5491 if (work->started)
5492 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
5493 work, NULL);
5494 os_free(ework);
5495 radio_work_done(work);
5496 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005497}
5498
5499
Dmitry Shmidt051af732013-10-22 13:52:46 -07005500static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
5501{
5502 struct wpa_supplicant *wpa_s = eloop_ctx;
5503 eapol_sm_notify_ctrl_response(wpa_s->eapol);
5504}
5505
5506
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005507static int set_scan_freqs(struct wpa_supplicant *wpa_s, char *val)
5508{
5509 int *freqs = NULL;
5510
5511 freqs = freq_range_to_channel_list(wpa_s, val);
5512 if (freqs == NULL)
5513 return -1;
5514
5515 os_free(wpa_s->manual_scan_freqs);
5516 wpa_s->manual_scan_freqs = freqs;
5517
5518 return 0;
5519}
5520
5521
5522static void wpas_ctrl_scan(struct wpa_supplicant *wpa_s, char *params,
5523 char *reply, int reply_size, int *reply_len)
5524{
5525 char *pos;
5526
5527 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
5528 *reply_len = -1;
5529 return;
5530 }
5531
5532 wpa_s->manual_scan_passive = 0;
5533 wpa_s->manual_scan_use_id = 0;
5534 wpa_s->manual_scan_only_new = 0;
5535
5536 if (params) {
5537 if (os_strncasecmp(params, "TYPE=ONLY", 9) == 0)
5538 wpa_s->scan_res_handler = scan_only_handler;
5539
5540 pos = os_strstr(params, "freq=");
5541 if (pos && set_scan_freqs(wpa_s, pos + 5) < 0) {
5542 *reply_len = -1;
5543 return;
5544 }
5545
5546 pos = os_strstr(params, "passive=");
5547 if (pos)
5548 wpa_s->manual_scan_passive = !!atoi(pos + 8);
5549
5550 pos = os_strstr(params, "use_id=");
5551 if (pos)
5552 wpa_s->manual_scan_use_id = atoi(pos + 7);
5553
5554 pos = os_strstr(params, "only_new=1");
5555 if (pos)
5556 wpa_s->manual_scan_only_new = 1;
5557 } else {
5558 os_free(wpa_s->manual_scan_freqs);
5559 wpa_s->manual_scan_freqs = NULL;
5560 if (wpa_s->scan_res_handler == scan_only_handler)
5561 wpa_s->scan_res_handler = NULL;
5562 }
5563
5564 if (!wpa_s->sched_scanning && !wpa_s->scanning &&
5565 ((wpa_s->wpa_state <= WPA_SCANNING) ||
5566 (wpa_s->wpa_state == WPA_COMPLETED))) {
5567 wpa_s->normal_scans = 0;
5568 wpa_s->scan_req = MANUAL_SCAN_REQ;
5569 wpa_s->after_wps = 0;
5570 wpa_s->known_wps_freq = 0;
5571 wpa_supplicant_req_scan(wpa_s, 0, 0);
5572 if (wpa_s->manual_scan_use_id) {
5573 wpa_s->manual_scan_id++;
5574 wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
5575 wpa_s->manual_scan_id);
5576 *reply_len = os_snprintf(reply, reply_size, "%u\n",
5577 wpa_s->manual_scan_id);
5578 }
5579 } else if (wpa_s->sched_scanning) {
5580 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to allow requested full scan to proceed");
5581 wpa_supplicant_cancel_sched_scan(wpa_s);
5582 wpa_s->scan_req = MANUAL_SCAN_REQ;
5583 wpa_supplicant_req_scan(wpa_s, 0, 0);
5584 if (wpa_s->manual_scan_use_id) {
5585 wpa_s->manual_scan_id++;
5586 *reply_len = os_snprintf(reply, reply_size, "%u\n",
5587 wpa_s->manual_scan_id);
5588 wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
5589 wpa_s->manual_scan_id);
5590 }
5591 } else {
5592 wpa_printf(MSG_DEBUG, "Ongoing scan action - reject new request");
5593 *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
5594 }
5595}
5596
5597
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005598char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
5599 char *buf, size_t *resp_len)
5600{
5601 char *reply;
5602 const int reply_size = 4096;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005603 int reply_len;
5604
5605 if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005606 os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
5607 if (wpa_debug_show_keys)
5608 wpa_dbg(wpa_s, MSG_DEBUG,
5609 "Control interface command '%s'", buf);
5610 else
5611 wpa_dbg(wpa_s, MSG_DEBUG,
5612 "Control interface command '%s [REMOVED]'",
5613 os_strncmp(buf, WPA_CTRL_RSP,
5614 os_strlen(WPA_CTRL_RSP)) == 0 ?
5615 WPA_CTRL_RSP : "SET_NETWORK");
5616 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
5617 os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0 ||
5618 os_strncmp(buf, "NFC_RX_HANDOVER_SEL", 19) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005619 wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
5620 (const u8 *) buf, os_strlen(buf));
5621 } else {
5622 int level = MSG_DEBUG;
5623 if (os_strcmp(buf, "PING") == 0)
5624 level = MSG_EXCESSIVE;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005625 wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005626 }
5627
5628 reply = os_malloc(reply_size);
5629 if (reply == NULL) {
5630 *resp_len = 1;
5631 return NULL;
5632 }
5633
5634 os_memcpy(reply, "OK\n", 3);
5635 reply_len = 3;
5636
5637 if (os_strcmp(buf, "PING") == 0) {
5638 os_memcpy(reply, "PONG\n", 5);
5639 reply_len = 5;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005640 } else if (os_strcmp(buf, "IFNAME") == 0) {
5641 reply_len = os_strlen(wpa_s->ifname);
5642 os_memcpy(reply, wpa_s->ifname, reply_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005643 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
5644 if (wpa_debug_reopen_file() < 0)
5645 reply_len = -1;
5646 } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
5647 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
5648 } else if (os_strcmp(buf, "MIB") == 0) {
5649 reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
5650 if (reply_len >= 0) {
5651 int res;
5652 res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
5653 reply_size - reply_len);
5654 if (res < 0)
5655 reply_len = -1;
5656 else
5657 reply_len += res;
5658 }
5659 } else if (os_strncmp(buf, "STATUS", 6) == 0) {
5660 reply_len = wpa_supplicant_ctrl_iface_status(
5661 wpa_s, buf + 6, reply, reply_size);
5662 } else if (os_strcmp(buf, "PMKSA") == 0) {
5663 reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
5664 reply_size);
5665 } else if (os_strncmp(buf, "SET ", 4) == 0) {
5666 if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
5667 reply_len = -1;
5668 } else if (os_strncmp(buf, "GET ", 4) == 0) {
5669 reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
5670 reply, reply_size);
5671 } else if (os_strcmp(buf, "LOGON") == 0) {
5672 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
5673 } else if (os_strcmp(buf, "LOGOFF") == 0) {
5674 eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
5675 } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
5676 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
5677 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005678 else
5679 wpas_request_connection(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005680 } else if (os_strcmp(buf, "RECONNECT") == 0) {
5681 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
5682 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005683 else if (wpa_s->disconnected)
5684 wpas_request_connection(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005685#ifdef IEEE8021X_EAPOL
5686 } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
5687 if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
5688 reply_len = -1;
5689#endif /* IEEE8021X_EAPOL */
5690#ifdef CONFIG_PEERKEY
5691 } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
5692 if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
5693 reply_len = -1;
5694#endif /* CONFIG_PEERKEY */
5695#ifdef CONFIG_IEEE80211R
5696 } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
5697 if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
5698 reply_len = -1;
5699#endif /* CONFIG_IEEE80211R */
5700#ifdef CONFIG_WPS
5701 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
5702 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
5703 if (res == -2) {
5704 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
5705 reply_len = 17;
5706 } else if (res)
5707 reply_len = -1;
5708 } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
5709 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
5710 if (res == -2) {
5711 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
5712 reply_len = 17;
5713 } else if (res)
5714 reply_len = -1;
5715 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
5716 reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
5717 reply,
5718 reply_size);
5719 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
5720 reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
5721 wpa_s, buf + 14, reply, reply_size);
5722 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
5723 if (wpas_wps_cancel(wpa_s))
5724 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005725#ifdef CONFIG_WPS_NFC
5726 } else if (os_strcmp(buf, "WPS_NFC") == 0) {
5727 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
5728 reply_len = -1;
5729 } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
5730 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
5731 reply_len = -1;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005732 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
5733 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
5734 wpa_s, buf + 21, reply, reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005735 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
5736 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
5737 wpa_s, buf + 14, reply, reply_size);
5738 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
5739 if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
5740 buf + 17))
5741 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005742 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
5743 reply_len = wpas_ctrl_nfc_get_handover_req(
5744 wpa_s, buf + 21, reply, reply_size);
5745 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
5746 reply_len = wpas_ctrl_nfc_get_handover_sel(
5747 wpa_s, buf + 21, reply, reply_size);
5748 } else if (os_strncmp(buf, "NFC_RX_HANDOVER_REQ ", 20) == 0) {
5749 reply_len = wpas_ctrl_nfc_rx_handover_req(
5750 wpa_s, buf + 20, reply, reply_size);
5751 } else if (os_strncmp(buf, "NFC_RX_HANDOVER_SEL ", 20) == 0) {
5752 if (wpas_ctrl_nfc_rx_handover_sel(wpa_s, buf + 20))
5753 reply_len = -1;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005754 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
5755 if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
5756 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005757#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005758 } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
5759 if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
5760 reply_len = -1;
5761#ifdef CONFIG_AP
5762 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
5763 reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
5764 wpa_s, buf + 11, reply, reply_size);
5765#endif /* CONFIG_AP */
5766#ifdef CONFIG_WPS_ER
5767 } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
5768 if (wpas_wps_er_start(wpa_s, NULL))
5769 reply_len = -1;
5770 } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
5771 if (wpas_wps_er_start(wpa_s, buf + 13))
5772 reply_len = -1;
5773 } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
5774 if (wpas_wps_er_stop(wpa_s))
5775 reply_len = -1;
5776 } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
5777 if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
5778 reply_len = -1;
5779 } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
5780 int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
5781 if (ret == -2) {
5782 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
5783 reply_len = 17;
5784 } else if (ret == -3) {
5785 os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
5786 reply_len = 18;
5787 } else if (ret == -4) {
5788 os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
5789 reply_len = 20;
5790 } else if (ret)
5791 reply_len = -1;
5792 } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
5793 if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
5794 reply_len = -1;
5795 } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
5796 if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
5797 buf + 18))
5798 reply_len = -1;
5799 } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
5800 if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
5801 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005802#ifdef CONFIG_WPS_NFC
5803 } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
5804 reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
5805 wpa_s, buf + 24, reply, reply_size);
5806#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005807#endif /* CONFIG_WPS_ER */
5808#endif /* CONFIG_WPS */
5809#ifdef CONFIG_IBSS_RSN
5810 } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
5811 if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
5812 reply_len = -1;
5813#endif /* CONFIG_IBSS_RSN */
5814#ifdef CONFIG_P2P
5815 } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
5816 if (p2p_ctrl_find(wpa_s, buf + 9))
5817 reply_len = -1;
5818 } else if (os_strcmp(buf, "P2P_FIND") == 0) {
5819 if (p2p_ctrl_find(wpa_s, ""))
5820 reply_len = -1;
5821 } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
5822 wpas_p2p_stop_find(wpa_s);
5823 } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
5824 reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
5825 reply_size);
5826 } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
5827 if (p2p_ctrl_listen(wpa_s, buf + 11))
5828 reply_len = -1;
5829 } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
5830 if (p2p_ctrl_listen(wpa_s, ""))
5831 reply_len = -1;
5832 } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
5833 if (wpas_p2p_group_remove(wpa_s, buf + 17))
5834 reply_len = -1;
5835 } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005836 if (wpas_p2p_group_add(wpa_s, 0, 0, 0, 0))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005837 reply_len = -1;
5838 } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
5839 if (p2p_ctrl_group_add(wpa_s, buf + 14))
5840 reply_len = -1;
5841 } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
5842 if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
5843 reply_len = -1;
5844 } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
5845 reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
5846 } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
5847 reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
5848 reply_size);
5849 } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
5850 if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
5851 reply_len = -1;
5852 } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
5853 if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
5854 reply_len = -1;
5855 } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
5856 wpas_p2p_sd_service_update(wpa_s);
5857 } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
5858 if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
5859 reply_len = -1;
5860 } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
5861 wpas_p2p_service_flush(wpa_s);
5862 } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
5863 if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
5864 reply_len = -1;
5865 } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
5866 if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
5867 reply_len = -1;
5868 } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
5869 if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
5870 reply_len = -1;
5871 } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
5872 if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
5873 reply_len = -1;
5874 } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
5875 reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
5876 reply_size);
5877 } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
5878 if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
5879 reply_len = -1;
5880 } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005881 p2p_ctrl_flush(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005882 } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
5883 if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
5884 reply_len = -1;
5885 } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
5886 if (wpas_p2p_cancel(wpa_s))
5887 reply_len = -1;
5888 } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
5889 if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
5890 reply_len = -1;
5891 } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
5892 if (p2p_ctrl_presence_req(wpa_s, "") < 0)
5893 reply_len = -1;
5894 } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
5895 if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
5896 reply_len = -1;
5897 } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
5898 if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
5899 reply_len = -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005900 } else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
5901 if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
5902 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005903#endif /* CONFIG_P2P */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005904#ifdef CONFIG_WIFI_DISPLAY
5905 } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
5906 if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
5907 reply_len = -1;
5908 } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
5909 reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
5910 reply, reply_size);
5911#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005912#ifdef CONFIG_INTERWORKING
5913 } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
5914 if (interworking_fetch_anqp(wpa_s) < 0)
5915 reply_len = -1;
5916 } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
5917 interworking_stop_fetch_anqp(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005918 } else if (os_strcmp(buf, "INTERWORKING_SELECT") == 0) {
5919 if (ctrl_interworking_select(wpa_s, NULL) < 0)
5920 reply_len = -1;
5921 } else if (os_strncmp(buf, "INTERWORKING_SELECT ", 20) == 0) {
5922 if (ctrl_interworking_select(wpa_s, buf + 20) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005923 reply_len = -1;
5924 } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
5925 if (ctrl_interworking_connect(wpa_s, buf + 21) < 0)
5926 reply_len = -1;
5927 } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
5928 if (get_anqp(wpa_s, buf + 9) < 0)
5929 reply_len = -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005930 } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
5931 if (gas_request(wpa_s, buf + 12) < 0)
5932 reply_len = -1;
5933 } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
5934 reply_len = gas_response_get(wpa_s, buf + 17, reply,
5935 reply_size);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005936#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt04949592012-07-19 12:16:46 -07005937#ifdef CONFIG_HS20
5938 } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
5939 if (get_hs20_anqp(wpa_s, buf + 14) < 0)
5940 reply_len = -1;
5941 } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
5942 if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
5943 reply_len = -1;
5944#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005945 } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
5946 {
5947 if (wpa_supplicant_ctrl_iface_ctrl_rsp(
5948 wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
5949 reply_len = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005950 else {
5951 /*
5952 * Notify response from timeout to allow the control
5953 * interface response to be sent first.
5954 */
5955 eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
5956 wpa_s, NULL);
5957 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005958 } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
5959 if (wpa_supplicant_reload_configuration(wpa_s))
5960 reply_len = -1;
5961 } else if (os_strcmp(buf, "TERMINATE") == 0) {
5962 wpa_supplicant_terminate_proc(wpa_s->global);
5963 } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
5964 if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
5965 reply_len = -1;
Dmitry Shmidte19501d2011-03-16 14:32:18 -07005966 } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005967 reply_len = wpa_supplicant_ctrl_iface_blacklist(
5968 wpa_s, buf + 9, reply, reply_size);
5969 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
5970 reply_len = wpa_supplicant_ctrl_iface_log_level(
5971 wpa_s, buf + 9, reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005972 } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
5973 reply_len = wpa_supplicant_ctrl_iface_list_networks(
5974 wpa_s, reply, reply_size);
5975 } else if (os_strcmp(buf, "DISCONNECT") == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005976#ifdef CONFIG_SME
5977 wpa_s->sme.prev_bssid_set = 0;
5978#endif /* CONFIG_SME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005979 wpa_s->reassociate = 0;
5980 wpa_s->disconnected = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005981 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005982 wpa_supplicant_cancel_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005983 wpa_supplicant_deauthenticate(wpa_s,
5984 WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005985 } else if (os_strcmp(buf, "SCAN") == 0) {
5986 wpas_ctrl_scan(wpa_s, NULL, reply, reply_size, &reply_len);
5987 } else if (os_strncmp(buf, "SCAN ", 5) == 0) {
5988 wpas_ctrl_scan(wpa_s, buf + 5, reply, reply_size, &reply_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005989 } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
5990 reply_len = wpa_supplicant_ctrl_iface_scan_results(
5991 wpa_s, reply, reply_size);
5992 } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
5993 if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
5994 reply_len = -1;
5995 } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
5996 if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
5997 reply_len = -1;
5998 } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
5999 if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
6000 reply_len = -1;
6001 } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
6002 reply_len = wpa_supplicant_ctrl_iface_add_network(
6003 wpa_s, reply, reply_size);
6004 } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
6005 if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
6006 reply_len = -1;
6007 } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
6008 if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
6009 reply_len = -1;
6010 } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
6011 reply_len = wpa_supplicant_ctrl_iface_get_network(
6012 wpa_s, buf + 12, reply, reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006013 } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
6014 reply_len = wpa_supplicant_ctrl_iface_list_creds(
6015 wpa_s, reply, reply_size);
6016 } else if (os_strcmp(buf, "ADD_CRED") == 0) {
6017 reply_len = wpa_supplicant_ctrl_iface_add_cred(
6018 wpa_s, reply, reply_size);
6019 } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
6020 if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
6021 reply_len = -1;
6022 } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
6023 if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
6024 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006025#ifndef CONFIG_NO_CONFIG_WRITE
6026 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
6027 if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
6028 reply_len = -1;
6029#endif /* CONFIG_NO_CONFIG_WRITE */
6030 } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
6031 reply_len = wpa_supplicant_ctrl_iface_get_capability(
6032 wpa_s, buf + 15, reply, reply_size);
6033 } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
6034 if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
6035 reply_len = -1;
6036 } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
6037 if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
6038 reply_len = -1;
6039 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
6040 reply_len = wpa_supplicant_global_iface_list(
6041 wpa_s->global, reply, reply_size);
6042 } else if (os_strcmp(buf, "INTERFACES") == 0) {
6043 reply_len = wpa_supplicant_global_iface_interfaces(
6044 wpa_s->global, reply, reply_size);
6045 } else if (os_strncmp(buf, "BSS ", 4) == 0) {
6046 reply_len = wpa_supplicant_ctrl_iface_bss(
6047 wpa_s, buf + 4, reply, reply_size);
6048#ifdef CONFIG_AP
6049 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
6050 reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
6051 } else if (os_strncmp(buf, "STA ", 4) == 0) {
6052 reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
6053 reply_size);
6054 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
6055 reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
6056 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006057 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
6058 if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
6059 reply_len = -1;
6060 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
6061 if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
6062 reply_len = -1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006063 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
6064 if (ap_ctrl_iface_chanswitch(wpa_s, buf + 12))
6065 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006066#endif /* CONFIG_AP */
6067 } else if (os_strcmp(buf, "SUSPEND") == 0) {
6068 wpas_notify_suspend(wpa_s->global);
6069 } else if (os_strcmp(buf, "RESUME") == 0) {
6070 wpas_notify_resume(wpa_s->global);
6071 } else if (os_strcmp(buf, "DROP_SA") == 0) {
6072 wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
6073 } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
6074 if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
6075 reply_len = -1;
6076 } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
6077 if (wpa_supplicant_ctrl_iface_sta_autoconnect(wpa_s, buf + 16))
6078 reply_len = -1;
6079 } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
6080 if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
6081 reply_len = -1;
6082 } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
6083 if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
6084 buf + 17))
6085 reply_len = -1;
Dmitry Shmidtf48e4f92012-08-24 11:14:44 -07006086 } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
6087 if (wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10))
6088 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006089#ifdef CONFIG_TDLS
6090 } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
6091 if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
6092 reply_len = -1;
6093 } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
6094 if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
6095 reply_len = -1;
6096 } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
6097 if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
6098 reply_len = -1;
6099#endif /* CONFIG_TDLS */
6100 } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
6101 reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
6102 reply_size);
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07006103 } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
6104 reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
6105 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006106#ifdef CONFIG_AUTOSCAN
6107 } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
6108 if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
6109 reply_len = -1;
6110#endif /* CONFIG_AUTOSCAN */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006111#ifdef ANDROID
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07006112 } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
6113 reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
6114 reply_size);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08006115#endif /* ANDROID */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006116 } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006117 pmksa_cache_clear_current(wpa_s->wpa);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006118 eapol_sm_request_reauth(wpa_s->eapol);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006119#ifdef CONFIG_WNM
6120 } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
6121 if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
6122 reply_len = -1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07006123 } else if (os_strncmp(buf, "WNM_BSS_QUERY ", 10) == 0) {
6124 if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 10))
6125 reply_len = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006126#endif /* CONFIG_WNM */
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006127 } else if (os_strcmp(buf, "FLUSH") == 0) {
6128 wpa_supplicant_ctrl_iface_flush(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006129 } else if (os_strncmp(buf, "RADIO_WORK ", 11) == 0) {
6130 reply_len = wpas_ctrl_radio_work(wpa_s, buf + 11, reply,
6131 reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006132 } else {
6133 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
6134 reply_len = 16;
6135 }
6136
6137 if (reply_len < 0) {
6138 os_memcpy(reply, "FAIL\n", 5);
6139 reply_len = 5;
6140 }
6141
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006142 *resp_len = reply_len;
6143 return reply;
6144}
6145
6146
6147static int wpa_supplicant_global_iface_add(struct wpa_global *global,
6148 char *cmd)
6149{
6150 struct wpa_interface iface;
6151 char *pos;
6152
6153 /*
6154 * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
6155 * TAB<bridge_ifname>
6156 */
6157 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
6158
6159 os_memset(&iface, 0, sizeof(iface));
6160
6161 do {
6162 iface.ifname = pos = cmd;
6163 pos = os_strchr(pos, '\t');
6164 if (pos)
6165 *pos++ = '\0';
6166 if (iface.ifname[0] == '\0')
6167 return -1;
6168 if (pos == NULL)
6169 break;
6170
6171 iface.confname = pos;
6172 pos = os_strchr(pos, '\t');
6173 if (pos)
6174 *pos++ = '\0';
6175 if (iface.confname[0] == '\0')
6176 iface.confname = NULL;
6177 if (pos == NULL)
6178 break;
6179
6180 iface.driver = pos;
6181 pos = os_strchr(pos, '\t');
6182 if (pos)
6183 *pos++ = '\0';
6184 if (iface.driver[0] == '\0')
6185 iface.driver = NULL;
6186 if (pos == NULL)
6187 break;
6188
6189 iface.ctrl_interface = pos;
6190 pos = os_strchr(pos, '\t');
6191 if (pos)
6192 *pos++ = '\0';
6193 if (iface.ctrl_interface[0] == '\0')
6194 iface.ctrl_interface = NULL;
6195 if (pos == NULL)
6196 break;
6197
6198 iface.driver_param = pos;
6199 pos = os_strchr(pos, '\t');
6200 if (pos)
6201 *pos++ = '\0';
6202 if (iface.driver_param[0] == '\0')
6203 iface.driver_param = NULL;
6204 if (pos == NULL)
6205 break;
6206
6207 iface.bridge_ifname = pos;
6208 pos = os_strchr(pos, '\t');
6209 if (pos)
6210 *pos++ = '\0';
6211 if (iface.bridge_ifname[0] == '\0')
6212 iface.bridge_ifname = NULL;
6213 if (pos == NULL)
6214 break;
6215 } while (0);
6216
6217 if (wpa_supplicant_get_iface(global, iface.ifname))
6218 return -1;
6219
6220 return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
6221}
6222
6223
6224static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
6225 char *cmd)
6226{
6227 struct wpa_supplicant *wpa_s;
6228
6229 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
6230
6231 wpa_s = wpa_supplicant_get_iface(global, cmd);
6232 if (wpa_s == NULL)
6233 return -1;
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07006234 return wpa_supplicant_remove_iface(global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006235}
6236
6237
6238static void wpa_free_iface_info(struct wpa_interface_info *iface)
6239{
6240 struct wpa_interface_info *prev;
6241
6242 while (iface) {
6243 prev = iface;
6244 iface = iface->next;
6245
6246 os_free(prev->ifname);
6247 os_free(prev->desc);
6248 os_free(prev);
6249 }
6250}
6251
6252
6253static int wpa_supplicant_global_iface_list(struct wpa_global *global,
6254 char *buf, int len)
6255{
6256 int i, res;
6257 struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
6258 char *pos, *end;
6259
6260 for (i = 0; wpa_drivers[i]; i++) {
6261 struct wpa_driver_ops *drv = wpa_drivers[i];
6262 if (drv->get_interfaces == NULL)
6263 continue;
6264 tmp = drv->get_interfaces(global->drv_priv[i]);
6265 if (tmp == NULL)
6266 continue;
6267
6268 if (last == NULL)
6269 iface = last = tmp;
6270 else
6271 last->next = tmp;
6272 while (last->next)
6273 last = last->next;
6274 }
6275
6276 pos = buf;
6277 end = buf + len;
6278 for (tmp = iface; tmp; tmp = tmp->next) {
6279 res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
6280 tmp->drv_name, tmp->ifname,
6281 tmp->desc ? tmp->desc : "");
6282 if (res < 0 || res >= end - pos) {
6283 *pos = '\0';
6284 break;
6285 }
6286 pos += res;
6287 }
6288
6289 wpa_free_iface_info(iface);
6290
6291 return pos - buf;
6292}
6293
6294
6295static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
6296 char *buf, int len)
6297{
6298 int res;
6299 char *pos, *end;
6300 struct wpa_supplicant *wpa_s;
6301
6302 wpa_s = global->ifaces;
6303 pos = buf;
6304 end = buf + len;
6305
6306 while (wpa_s) {
6307 res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
6308 if (res < 0 || res >= end - pos) {
6309 *pos = '\0';
6310 break;
6311 }
6312 pos += res;
6313 wpa_s = wpa_s->next;
6314 }
6315 return pos - buf;
6316}
6317
6318
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006319static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
6320 const char *ifname,
6321 char *cmd, size_t *resp_len)
6322{
6323 struct wpa_supplicant *wpa_s;
6324
6325 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6326 if (os_strcmp(ifname, wpa_s->ifname) == 0)
6327 break;
6328 }
6329
6330 if (wpa_s == NULL) {
6331 char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
6332 if (resp)
6333 *resp_len = os_strlen(resp);
6334 else
6335 *resp_len = 1;
6336 return resp;
6337 }
6338
6339 return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
6340}
6341
6342
6343static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
6344 char *buf, size_t *resp_len)
6345{
6346#ifdef CONFIG_P2P
6347 static const char * cmd[] = {
Dmitry Shmidt0c18dcd2013-08-16 15:29:47 -07006348 "LIST_NETWORKS",
6349 "SAVE_CONFIG",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006350 "P2P_FIND",
6351 "P2P_STOP_FIND",
6352 "P2P_LISTEN",
6353 "P2P_GROUP_ADD",
6354 "P2P_GET_PASSPHRASE",
6355 "P2P_SERVICE_UPDATE",
6356 "P2P_SERVICE_FLUSH",
6357 "P2P_FLUSH",
6358 "P2P_CANCEL",
6359 "P2P_PRESENCE_REQ",
6360 "P2P_EXT_LISTEN",
6361 NULL
6362 };
6363 static const char * prefix[] = {
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08006364#ifdef ANDROID
Dmitry Shmidt0c18dcd2013-08-16 15:29:47 -07006365 "DRIVER ",
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08006366#endif /* ANDROID */
Dmitry Shmidt0c18dcd2013-08-16 15:29:47 -07006367 "GET_NETWORK ",
6368 "REMOVE_NETWORK ",
6369 "SET ",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006370 "P2P_FIND ",
6371 "P2P_CONNECT ",
6372 "P2P_LISTEN ",
6373 "P2P_GROUP_REMOVE ",
6374 "P2P_GROUP_ADD ",
6375 "P2P_PROV_DISC ",
6376 "P2P_SERV_DISC_REQ ",
6377 "P2P_SERV_DISC_CANCEL_REQ ",
6378 "P2P_SERV_DISC_RESP ",
6379 "P2P_SERV_DISC_EXTERNAL ",
6380 "P2P_SERVICE_ADD ",
6381 "P2P_SERVICE_DEL ",
6382 "P2P_REJECT ",
6383 "P2P_INVITE ",
6384 "P2P_PEER ",
6385 "P2P_SET ",
6386 "P2P_UNAUTHORIZE ",
6387 "P2P_PRESENCE_REQ ",
6388 "P2P_EXT_LISTEN ",
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006389 "P2P_REMOVE_CLIENT ",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006390 NULL
6391 };
6392 int found = 0;
6393 int i;
6394
6395 if (global->p2p_init_wpa_s == NULL)
6396 return NULL;
6397
6398 for (i = 0; !found && cmd[i]; i++) {
6399 if (os_strcmp(buf, cmd[i]) == 0)
6400 found = 1;
6401 }
6402
6403 for (i = 0; !found && prefix[i]; i++) {
6404 if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
6405 found = 1;
6406 }
6407
6408 if (found)
6409 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
6410 buf, resp_len);
6411#endif /* CONFIG_P2P */
6412 return NULL;
6413}
6414
6415
6416static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
6417 char *buf, size_t *resp_len)
6418{
6419#ifdef CONFIG_WIFI_DISPLAY
6420 if (global->p2p_init_wpa_s == NULL)
6421 return NULL;
6422 if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
6423 os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
6424 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
6425 buf, resp_len);
6426#endif /* CONFIG_WIFI_DISPLAY */
6427 return NULL;
6428}
6429
6430
6431static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
6432 char *buf, size_t *resp_len)
6433{
6434 char *ret;
6435
6436 ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
6437 if (ret)
6438 return ret;
6439
6440 ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
6441 if (ret)
6442 return ret;
6443
6444 return NULL;
6445}
6446
6447
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006448static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
6449{
6450 char *value;
6451
6452 value = os_strchr(cmd, ' ');
6453 if (value == NULL)
6454 return -1;
6455 *value++ = '\0';
6456
6457 wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
6458
6459#ifdef CONFIG_WIFI_DISPLAY
6460 if (os_strcasecmp(cmd, "wifi_display") == 0) {
6461 wifi_display_enable(global, !!atoi(value));
6462 return 0;
6463 }
6464#endif /* CONFIG_WIFI_DISPLAY */
6465
6466 return -1;
6467}
6468
6469
6470#ifndef CONFIG_NO_CONFIG_WRITE
6471static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
6472{
6473 int ret = 0;
6474 struct wpa_supplicant *wpa_s;
6475
6476 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6477 if (!wpa_s->conf->update_config) {
6478 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
6479 continue;
6480 }
6481
6482 if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
6483 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
6484 ret = 1;
6485 } else {
6486 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
6487 }
6488 }
6489
6490 return ret;
6491}
6492#endif /* CONFIG_NO_CONFIG_WRITE */
6493
6494
6495static int wpas_global_ctrl_iface_status(struct wpa_global *global,
6496 char *buf, size_t buflen)
6497{
6498 char *pos, *end;
6499 int ret;
6500 struct wpa_supplicant *wpa_s;
6501
6502 pos = buf;
6503 end = buf + buflen;
6504
6505#ifdef CONFIG_P2P
6506 if (global->p2p && !global->p2p_disabled) {
6507 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
6508 "\n"
6509 "p2p_state=%s\n",
6510 MAC2STR(global->p2p_dev_addr),
6511 p2p_get_state_txt(global->p2p));
6512 if (ret < 0 || ret >= end - pos)
6513 return pos - buf;
6514 pos += ret;
6515 } else if (global->p2p) {
6516 ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
6517 if (ret < 0 || ret >= end - pos)
6518 return pos - buf;
6519 pos += ret;
6520 }
6521#endif /* CONFIG_P2P */
6522
6523#ifdef CONFIG_WIFI_DISPLAY
6524 ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
6525 !!global->wifi_display);
6526 if (ret < 0 || ret >= end - pos)
6527 return pos - buf;
6528 pos += ret;
6529#endif /* CONFIG_WIFI_DISPLAY */
6530
6531 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6532 ret = os_snprintf(pos, end - pos, "ifname=%s\n"
6533 "address=" MACSTR "\n",
6534 wpa_s->ifname, MAC2STR(wpa_s->own_addr));
6535 if (ret < 0 || ret >= end - pos)
6536 return pos - buf;
6537 pos += ret;
6538 }
6539
6540 return pos - buf;
6541}
6542
6543
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006544char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
6545 char *buf, size_t *resp_len)
6546{
6547 char *reply;
6548 const int reply_size = 2048;
6549 int reply_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006550 int level = MSG_DEBUG;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006551
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006552 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
6553 char *pos = os_strchr(buf + 7, ' ');
6554 if (pos) {
6555 *pos++ = '\0';
6556 return wpas_global_ctrl_iface_ifname(global,
6557 buf + 7, pos,
6558 resp_len);
6559 }
6560 }
6561
6562 reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
6563 if (reply)
6564 return reply;
6565
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006566 if (os_strcmp(buf, "PING") == 0)
6567 level = MSG_EXCESSIVE;
6568 wpa_hexdump_ascii(level, "RX global ctrl_iface",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006569 (const u8 *) buf, os_strlen(buf));
6570
6571 reply = os_malloc(reply_size);
6572 if (reply == NULL) {
6573 *resp_len = 1;
6574 return NULL;
6575 }
6576
6577 os_memcpy(reply, "OK\n", 3);
6578 reply_len = 3;
6579
6580 if (os_strcmp(buf, "PING") == 0) {
6581 os_memcpy(reply, "PONG\n", 5);
6582 reply_len = 5;
6583 } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
6584 if (wpa_supplicant_global_iface_add(global, buf + 14))
6585 reply_len = -1;
6586 } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
6587 if (wpa_supplicant_global_iface_remove(global, buf + 17))
6588 reply_len = -1;
6589 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
6590 reply_len = wpa_supplicant_global_iface_list(
6591 global, reply, reply_size);
6592 } else if (os_strcmp(buf, "INTERFACES") == 0) {
6593 reply_len = wpa_supplicant_global_iface_interfaces(
6594 global, reply, reply_size);
6595 } else if (os_strcmp(buf, "TERMINATE") == 0) {
6596 wpa_supplicant_terminate_proc(global);
6597 } else if (os_strcmp(buf, "SUSPEND") == 0) {
6598 wpas_notify_suspend(global);
6599 } else if (os_strcmp(buf, "RESUME") == 0) {
6600 wpas_notify_resume(global);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006601 } else if (os_strncmp(buf, "SET ", 4) == 0) {
6602 if (wpas_global_ctrl_iface_set(global, buf + 4))
6603 reply_len = -1;
6604#ifndef CONFIG_NO_CONFIG_WRITE
6605 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
6606 if (wpas_global_ctrl_iface_save_config(global))
6607 reply_len = -1;
6608#endif /* CONFIG_NO_CONFIG_WRITE */
6609 } else if (os_strcmp(buf, "STATUS") == 0) {
6610 reply_len = wpas_global_ctrl_iface_status(global, reply,
6611 reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006612 } else {
6613 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
6614 reply_len = 16;
6615 }
6616
6617 if (reply_len < 0) {
6618 os_memcpy(reply, "FAIL\n", 5);
6619 reply_len = 5;
6620 }
6621
6622 *resp_len = reply_len;
6623 return reply;
6624}