]> git.ipfire.org Git - thirdparty/hostap.git/blob - wpa_supplicant/ap.c
P2P: Indicate WPS events from AP mode only during group formation
[thirdparty/hostap.git] / wpa_supplicant / ap.c
1 /*
2 * WPA Supplicant - Basic AP mode support routines
3 * Copyright (c) 2003-2009, Jouni Malinen <j@w1.fi>
4 * Copyright (c) 2009, Atheros Communications
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * Alternatively, this software may be distributed under the terms of BSD
11 * license.
12 *
13 * See README and COPYING for more details.
14 */
15
16 #include "utils/includes.h"
17
18 #include "utils/common.h"
19 #include "utils/eloop.h"
20 #include "common/ieee802_11_defs.h"
21 #include "common/wpa_ctrl.h"
22 #include "ap/hostapd.h"
23 #include "ap/ap_config.h"
24 #ifdef NEED_AP_MLME
25 #include "ap/ieee802_11.h"
26 #endif /* NEED_AP_MLME */
27 #include "ap/beacon.h"
28 #include "ap/ieee802_1x.h"
29 #include "ap/wps_hostapd.h"
30 #include "ap/ctrl_iface_ap.h"
31 #include "eap_common/eap_defs.h"
32 #include "eap_server/eap_methods.h"
33 #include "eap_common/eap_wsc_common.h"
34 #include "wps/wps.h"
35 #include "common/ieee802_11_defs.h"
36 #include "config_ssid.h"
37 #include "config.h"
38 #include "wpa_supplicant_i.h"
39 #include "driver_i.h"
40 #include "p2p_supplicant.h"
41 #include "ap.h"
42 #include "ap/sta_info.h"
43
44
45 static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx);
46
47
48 static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
49 struct wpa_ssid *ssid,
50 struct hostapd_config *conf)
51 {
52 struct hostapd_bss_config *bss = &conf->bss[0];
53 int pairwise;
54
55 conf->driver = wpa_s->driver;
56
57 os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
58
59 if (ssid->frequency == 0) {
60 /* default channel 11 */
61 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
62 conf->channel = 11;
63 } else if (ssid->frequency >= 2412 && ssid->frequency <= 2472) {
64 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
65 conf->channel = (ssid->frequency - 2407) / 5;
66 } else if ((ssid->frequency >= 5180 && ssid->frequency <= 5240) ||
67 (ssid->frequency >= 5745 && ssid->frequency <= 5825)) {
68 conf->hw_mode = HOSTAPD_MODE_IEEE80211A;
69 conf->channel = (ssid->frequency - 5000) / 5;
70 } else {
71 wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
72 ssid->frequency);
73 return -1;
74 }
75
76 /* TODO: enable HT if driver supports it;
77 * drop to 11b if driver does not support 11g */
78
79 #ifdef CONFIG_P2P
80 if (conf->hw_mode == HOSTAPD_MODE_IEEE80211G) {
81 /* Remove 802.11b rates from supported and basic rate sets */
82 int *list = os_malloc(4 * sizeof(int));
83 if (list) {
84 list[0] = 60;
85 list[1] = 120;
86 list[2] = 240;
87 list[3] = -1;
88 }
89 conf->basic_rates = list;
90
91 list = os_malloc(9 * sizeof(int));
92 if (list) {
93 list[0] = 60;
94 list[1] = 90;
95 list[2] = 120;
96 list[3] = 180;
97 list[4] = 240;
98 list[5] = 360;
99 list[6] = 480;
100 list[7] = 540;
101 list[8] = -1;
102 }
103 conf->supported_rates = list;
104 }
105 #endif /* CONFIG_P2P */
106
107 if (ssid->ssid_len == 0) {
108 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
109 return -1;
110 }
111 os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
112 bss->ssid.ssid[ssid->ssid_len] = '\0';
113 bss->ssid.ssid_len = ssid->ssid_len;
114 bss->ssid.ssid_set = 1;
115
116 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
117 bss->wpa = ssid->proto;
118 bss->wpa_key_mgmt = ssid->key_mgmt;
119 bss->wpa_pairwise = ssid->pairwise_cipher;
120 if (ssid->passphrase) {
121 bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
122 } else if (ssid->psk_set) {
123 os_free(bss->ssid.wpa_psk);
124 bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
125 if (bss->ssid.wpa_psk == NULL)
126 return -1;
127 os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
128 bss->ssid.wpa_psk->group = 1;
129 }
130
131 /* Select group cipher based on the enabled pairwise cipher suites */
132 pairwise = 0;
133 if (bss->wpa & 1)
134 pairwise |= bss->wpa_pairwise;
135 if (bss->wpa & 2) {
136 if (bss->rsn_pairwise == 0)
137 bss->rsn_pairwise = bss->wpa_pairwise;
138 pairwise |= bss->rsn_pairwise;
139 }
140 if (pairwise & WPA_CIPHER_TKIP)
141 bss->wpa_group = WPA_CIPHER_TKIP;
142 else
143 bss->wpa_group = WPA_CIPHER_CCMP;
144
145 if (bss->wpa && bss->ieee802_1x)
146 bss->ssid.security_policy = SECURITY_WPA;
147 else if (bss->wpa)
148 bss->ssid.security_policy = SECURITY_WPA_PSK;
149 else if (bss->ieee802_1x) {
150 bss->ssid.security_policy = SECURITY_IEEE_802_1X;
151 bss->ssid.wep.default_len = bss->default_wep_key_len;
152 } else if (bss->ssid.wep.keys_set)
153 bss->ssid.security_policy = SECURITY_STATIC_WEP;
154 else
155 bss->ssid.security_policy = SECURITY_PLAINTEXT;
156
157 #ifdef CONFIG_WPS
158 /*
159 * Enable WPS by default, but require user interaction to actually use
160 * it. Only the internal Registrar is supported.
161 */
162 bss->eap_server = 1;
163 bss->wps_state = 2;
164 bss->ap_setup_locked = 2;
165 if (wpa_s->conf->config_methods)
166 bss->config_methods = os_strdup(wpa_s->conf->config_methods);
167 if (wpa_s->conf->device_type)
168 bss->device_type = os_strdup(wpa_s->conf->device_type);
169 if (wpa_s->conf->device_name) {
170 bss->device_name = os_strdup(wpa_s->conf->device_name);
171 bss->friendly_name = os_strdup(wpa_s->conf->device_name);
172 }
173 if (wpa_s->conf->manufacturer)
174 bss->manufacturer = os_strdup(wpa_s->conf->manufacturer);
175 if (wpa_s->conf->model_name)
176 bss->model_name = os_strdup(wpa_s->conf->model_name);
177 if (wpa_s->conf->model_number)
178 bss->model_number = os_strdup(wpa_s->conf->model_number);
179 if (wpa_s->conf->serial_number)
180 bss->serial_number = os_strdup(wpa_s->conf->serial_number);
181 os_memcpy(bss->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
182 os_memcpy(bss->os_version, wpa_s->conf->os_version, 4);
183 #endif /* CONFIG_WPS */
184
185 if (wpa_s->max_stations &&
186 wpa_s->max_stations < wpa_s->conf->max_num_sta)
187 bss->max_num_sta = wpa_s->max_stations;
188 else
189 bss->max_num_sta = wpa_s->conf->max_num_sta;
190
191 return 0;
192 }
193
194
195 static void ap_public_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
196 {
197 #ifdef CONFIG_P2P
198 struct wpa_supplicant *wpa_s = ctx;
199 const struct ieee80211_mgmt *mgmt;
200 size_t hdr_len;
201
202 mgmt = (const struct ieee80211_mgmt *) buf;
203 hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
204 if (hdr_len > len)
205 return;
206 wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
207 mgmt->u.action.category,
208 &mgmt->u.action.u.vs_public_action.action,
209 len - hdr_len, freq);
210 #endif /* CONFIG_P2P */
211 }
212
213
214 static void ap_wps_event_cb(void *ctx, enum wps_event event,
215 union wps_event_data *data)
216 {
217 #ifdef CONFIG_P2P
218 struct wpa_supplicant *wpa_s = ctx;
219
220 if (event == WPS_EV_FAIL && wpa_s->parent && wpa_s->parent != wpa_s &&
221 wpa_s == wpa_s->global->p2p_group_formation) {
222 struct wps_event_fail *fail = &data->fail;
223
224 /*
225 * src/ap/wps_hostapd.c has already sent this on the main
226 * interface, so only send on the parent interface here if
227 * needed.
228 */
229 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
230 "msg=%d config_error=%d",
231 fail->msg, fail->config_error);
232 }
233 #endif /* CONFIG_P2P */
234 }
235
236
237 static int ap_vendor_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
238 {
239 #ifdef CONFIG_P2P
240 struct wpa_supplicant *wpa_s = ctx;
241 const struct ieee80211_mgmt *mgmt;
242 size_t hdr_len;
243
244 mgmt = (const struct ieee80211_mgmt *) buf;
245 hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
246 if (hdr_len > len)
247 return -1;
248 wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
249 mgmt->u.action.category,
250 &mgmt->u.action.u.vs_public_action.action,
251 len - hdr_len, freq);
252 #endif /* CONFIG_P2P */
253 return 0;
254 }
255
256
257 static int ap_probe_req_rx(void *ctx, const u8 *addr, const u8 *ie,
258 size_t ie_len)
259 {
260 #ifdef CONFIG_P2P
261 struct wpa_supplicant *wpa_s = ctx;
262 return wpas_p2p_probe_req_rx(wpa_s, addr, ie, ie_len);
263 #else /* CONFIG_P2P */
264 return 0;
265 #endif /* CONFIG_P2P */
266 }
267
268
269 static void ap_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
270 const u8 *uuid_e)
271 {
272 #ifdef CONFIG_P2P
273 struct wpa_supplicant *wpa_s = ctx;
274 wpas_p2p_wps_success(wpa_s, mac_addr, 1);
275 #endif /* CONFIG_P2P */
276 }
277
278
279 int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
280 struct wpa_ssid *ssid)
281 {
282 struct wpa_driver_associate_params params;
283 struct hostapd_iface *hapd_iface;
284 struct hostapd_config *conf;
285 size_t i;
286
287 if (ssid->ssid == NULL || ssid->ssid_len == 0) {
288 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
289 return -1;
290 }
291
292 wpa_supplicant_ap_deinit(wpa_s);
293
294 wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
295 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
296
297 os_memset(&params, 0, sizeof(params));
298 params.ssid = ssid->ssid;
299 params.ssid_len = ssid->ssid_len;
300 switch (ssid->mode) {
301 case WPAS_MODE_INFRA:
302 params.mode = IEEE80211_MODE_INFRA;
303 break;
304 case WPAS_MODE_IBSS:
305 params.mode = IEEE80211_MODE_IBSS;
306 break;
307 case WPAS_MODE_AP:
308 case WPAS_MODE_P2P_GO:
309 case WPAS_MODE_P2P_GROUP_FORMATION:
310 params.mode = IEEE80211_MODE_AP;
311 break;
312 }
313 params.freq = ssid->frequency;
314
315 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK)
316 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
317 else
318 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
319 params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
320
321 if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
322 wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
323 else if (ssid->pairwise_cipher & WPA_CIPHER_TKIP)
324 wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
325 else if (ssid->pairwise_cipher & WPA_CIPHER_NONE)
326 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
327 else {
328 wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
329 "cipher.");
330 return -1;
331 }
332 params.pairwise_suite = cipher_suite2driver(wpa_s->pairwise_cipher);
333 params.group_suite = params.pairwise_suite;
334
335 #ifdef CONFIG_P2P
336 if (ssid->mode == WPAS_MODE_P2P_GO ||
337 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
338 params.p2p = 1;
339 wpa_drv_set_intra_bss(wpa_s, wpa_s->conf->p2p_intra_bss);
340 #endif /* CONFIG_P2P */
341
342 if (wpa_s->parent->set_ap_uapsd)
343 params.uapsd = wpa_s->parent->ap_uapsd;
344 else
345 params.uapsd = -1;
346
347 if (wpa_drv_associate(wpa_s, &params) < 0) {
348 wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
349 return -1;
350 }
351
352 wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
353 if (hapd_iface == NULL)
354 return -1;
355 hapd_iface->owner = wpa_s;
356
357 wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
358 if (conf == NULL) {
359 wpa_supplicant_ap_deinit(wpa_s);
360 return -1;
361 }
362
363 if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
364 wpa_printf(MSG_ERROR, "Failed to create AP configuration");
365 wpa_supplicant_ap_deinit(wpa_s);
366 return -1;
367 }
368
369 #ifdef CONFIG_P2P
370 if (ssid->mode == WPAS_MODE_P2P_GO)
371 conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER;
372 else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
373 conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER |
374 P2P_GROUP_FORMATION;
375 #endif /* CONFIG_P2P */
376
377 hapd_iface->num_bss = conf->num_bss;
378 hapd_iface->bss = os_zalloc(conf->num_bss *
379 sizeof(struct hostapd_data *));
380 if (hapd_iface->bss == NULL) {
381 wpa_supplicant_ap_deinit(wpa_s);
382 return -1;
383 }
384
385 for (i = 0; i < conf->num_bss; i++) {
386 hapd_iface->bss[i] =
387 hostapd_alloc_bss_data(hapd_iface, conf,
388 &conf->bss[i]);
389 if (hapd_iface->bss[i] == NULL) {
390 wpa_supplicant_ap_deinit(wpa_s);
391 return -1;
392 }
393
394 hapd_iface->bss[i]->msg_ctx = wpa_s;
395 hapd_iface->bss[i]->public_action_cb = ap_public_action_rx;
396 hapd_iface->bss[i]->public_action_cb_ctx = wpa_s;
397 hapd_iface->bss[i]->vendor_action_cb = ap_vendor_action_rx;
398 hapd_iface->bss[i]->vendor_action_cb_ctx = wpa_s;
399 hostapd_register_probereq_cb(hapd_iface->bss[i],
400 ap_probe_req_rx, wpa_s);
401 hapd_iface->bss[i]->wps_reg_success_cb = ap_wps_reg_success_cb;
402 hapd_iface->bss[i]->wps_reg_success_cb_ctx = wpa_s;
403 hapd_iface->bss[i]->wps_event_cb = ap_wps_event_cb;
404 hapd_iface->bss[i]->wps_event_cb_ctx = wpa_s;
405 #ifdef CONFIG_P2P
406 hapd_iface->bss[i]->p2p = wpa_s->global->p2p;
407 hapd_iface->bss[i]->p2p_group = wpas_p2p_group_init(
408 wpa_s, ssid->p2p_persistent_group,
409 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION);
410 #endif /* CONFIG_P2P */
411 }
412
413 os_memcpy(hapd_iface->bss[0]->own_addr, wpa_s->own_addr, ETH_ALEN);
414 hapd_iface->bss[0]->driver = wpa_s->driver;
415 hapd_iface->bss[0]->drv_priv = wpa_s->drv_priv;
416
417 if (hostapd_setup_interface(wpa_s->ap_iface)) {
418 wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
419 wpa_supplicant_ap_deinit(wpa_s);
420 return -1;
421 }
422
423 wpa_s->current_ssid = ssid;
424 os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
425 wpa_s->assoc_freq = ssid->frequency;
426 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
427
428 if (wpa_s->ap_configured_cb)
429 wpa_s->ap_configured_cb(wpa_s->ap_configured_cb_ctx,
430 wpa_s->ap_configured_cb_data);
431
432 return 0;
433 }
434
435
436 void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
437 {
438 eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
439
440 if (wpa_s->ap_iface == NULL)
441 return;
442
443 wpa_s->current_ssid = NULL;
444 #ifdef CONFIG_P2P
445 if (wpa_s->ap_iface->bss)
446 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
447 wpas_p2p_group_deinit(wpa_s);
448 #endif /* CONFIG_P2P */
449 hostapd_interface_deinit(wpa_s->ap_iface);
450 hostapd_interface_free(wpa_s->ap_iface);
451 wpa_s->ap_iface = NULL;
452 wpa_drv_deinit_ap(wpa_s);
453 }
454
455
456 void ap_tx_status(void *ctx, const u8 *addr,
457 const u8 *buf, size_t len, int ack)
458 {
459 #ifdef NEED_AP_MLME
460 struct wpa_supplicant *wpa_s = ctx;
461 hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
462 #endif /* NEED_AP_MLME */
463 }
464
465
466 void ap_rx_from_unknown_sta(void *ctx, const u8 *frame, size_t len)
467 {
468 #ifdef NEED_AP_MLME
469 struct wpa_supplicant *wpa_s = ctx;
470 const struct ieee80211_hdr *hdr =
471 (const struct ieee80211_hdr *) frame;
472 u16 fc = le_to_host16(hdr->frame_control);
473 ieee802_11_rx_from_unknown(wpa_s->ap_iface->bss[0], hdr->addr2,
474 (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
475 (WLAN_FC_TODS | WLAN_FC_FROMDS));
476 #endif /* NEED_AP_MLME */
477 }
478
479
480 void ap_mgmt_rx(void *ctx, struct rx_mgmt *rx_mgmt)
481 {
482 #ifdef NEED_AP_MLME
483 struct wpa_supplicant *wpa_s = ctx;
484 struct hostapd_frame_info fi;
485 os_memset(&fi, 0, sizeof(fi));
486 fi.datarate = rx_mgmt->datarate;
487 fi.ssi_signal = rx_mgmt->ssi_signal;
488 ieee802_11_mgmt(wpa_s->ap_iface->bss[0], rx_mgmt->frame,
489 rx_mgmt->frame_len, &fi);
490 #endif /* NEED_AP_MLME */
491 }
492
493
494 void ap_mgmt_tx_cb(void *ctx, const u8 *buf, size_t len, u16 stype, int ok)
495 {
496 #ifdef NEED_AP_MLME
497 struct wpa_supplicant *wpa_s = ctx;
498 ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
499 #endif /* NEED_AP_MLME */
500 }
501
502
503 void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
504 const u8 *src_addr, const u8 *buf, size_t len)
505 {
506 ieee802_1x_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
507 }
508
509
510 #ifdef CONFIG_WPS
511
512 int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
513 {
514 if (!wpa_s->ap_iface)
515 return -1;
516 return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0]);
517 }
518
519
520 static int wpa_supplicant_ap_wps_sta_cancel(struct hostapd_data *hapd,
521 struct sta_info *sta, void *ctx)
522 {
523 if (sta && (sta->flags & WLAN_STA_WPS)) {
524 ap_sta_deauthenticate(hapd, sta,
525 WLAN_REASON_PREV_AUTH_NOT_VALID);
526 wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
527 __func__, MAC2STR(sta->addr));
528 return 1;
529 }
530
531 return 0;
532 }
533
534
535 int wpa_supplicant_ap_wps_cancel(struct wpa_supplicant *wpa_s)
536 {
537 struct wps_registrar *reg;
538 int reg_sel = 0, wps_sta = 0;
539
540 if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0]->wps)
541 return -1;
542
543 reg = wpa_s->ap_iface->bss[0]->wps->registrar;
544 reg_sel = wps_registrar_wps_cancel(reg);
545 wps_sta = ap_for_each_sta(wpa_s->ap_iface->bss[0],
546 wpa_supplicant_ap_wps_sta_cancel, NULL);
547
548 if (!reg_sel && !wps_sta) {
549 wpa_printf(MSG_DEBUG, "No WPS operation in progress at this "
550 "time");
551 return -1;
552 }
553
554 /*
555 * There are 2 cases to return wps cancel as success:
556 * 1. When wps cancel was initiated but no connection has been
557 * established with client yet.
558 * 2. Client is in the middle of exchanging WPS messages.
559 */
560
561 return 0;
562 }
563
564
565 int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
566 const char *pin, char *buf, size_t buflen)
567 {
568 int ret, ret_len = 0;
569
570 if (!wpa_s->ap_iface)
571 return -1;
572
573 if (pin == NULL) {
574 unsigned int rpin = wps_generate_pin();
575 ret_len = os_snprintf(buf, buflen, "%d", rpin);
576 pin = buf;
577 } else
578 ret_len = os_snprintf(buf, buflen, "%s", pin);
579
580 ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], bssid, "any", pin,
581 0);
582 if (ret)
583 return -1;
584 return ret_len;
585 }
586
587
588 static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
589 {
590 struct wpa_supplicant *wpa_s = eloop_data;
591 wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
592 wpas_wps_ap_pin_disable(wpa_s);
593 }
594
595
596 static void wpas_wps_ap_pin_enable(struct wpa_supplicant *wpa_s, int timeout)
597 {
598 struct hostapd_data *hapd;
599
600 if (wpa_s->ap_iface == NULL)
601 return;
602 hapd = wpa_s->ap_iface->bss[0];
603 wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
604 hapd->ap_pin_failures = 0;
605 eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
606 if (timeout > 0)
607 eloop_register_timeout(timeout, 0,
608 wpas_wps_ap_pin_timeout, wpa_s, NULL);
609 }
610
611
612 void wpas_wps_ap_pin_disable(struct wpa_supplicant *wpa_s)
613 {
614 struct hostapd_data *hapd;
615
616 if (wpa_s->ap_iface == NULL)
617 return;
618 wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
619 hapd = wpa_s->ap_iface->bss[0];
620 os_free(hapd->conf->ap_pin);
621 hapd->conf->ap_pin = NULL;
622 eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
623 }
624
625
626 const char * wpas_wps_ap_pin_random(struct wpa_supplicant *wpa_s, int timeout)
627 {
628 struct hostapd_data *hapd;
629 unsigned int pin;
630 char pin_txt[9];
631
632 if (wpa_s->ap_iface == NULL)
633 return NULL;
634 hapd = wpa_s->ap_iface->bss[0];
635 pin = wps_generate_pin();
636 os_snprintf(pin_txt, sizeof(pin_txt), "%u", pin);
637 os_free(hapd->conf->ap_pin);
638 hapd->conf->ap_pin = os_strdup(pin_txt);
639 if (hapd->conf->ap_pin == NULL)
640 return NULL;
641 wpas_wps_ap_pin_enable(wpa_s, timeout);
642
643 return hapd->conf->ap_pin;
644 }
645
646
647 const char * wpas_wps_ap_pin_get(struct wpa_supplicant *wpa_s)
648 {
649 struct hostapd_data *hapd;
650 if (wpa_s->ap_iface == NULL)
651 return NULL;
652 hapd = wpa_s->ap_iface->bss[0];
653 return hapd->conf->ap_pin;
654 }
655
656
657 int wpas_wps_ap_pin_set(struct wpa_supplicant *wpa_s, const char *pin,
658 int timeout)
659 {
660 struct hostapd_data *hapd;
661 char pin_txt[9];
662 int ret;
663
664 if (wpa_s->ap_iface == NULL)
665 return -1;
666 hapd = wpa_s->ap_iface->bss[0];
667 ret = os_snprintf(pin_txt, sizeof(pin_txt), "%s", pin);
668 if (ret < 0 || ret >= (int) sizeof(pin_txt))
669 return -1;
670 os_free(hapd->conf->ap_pin);
671 hapd->conf->ap_pin = os_strdup(pin_txt);
672 if (hapd->conf->ap_pin == NULL)
673 return -1;
674 wpas_wps_ap_pin_enable(wpa_s, timeout);
675
676 return 0;
677 }
678
679
680 void wpa_supplicant_ap_pwd_auth_fail(struct wpa_supplicant *wpa_s)
681 {
682 struct hostapd_data *hapd;
683
684 if (wpa_s->ap_iface == NULL)
685 return;
686 hapd = wpa_s->ap_iface->bss[0];
687
688 /*
689 * Registrar failed to prove its knowledge of the AP PIN. Disable AP
690 * PIN if this happens multiple times to slow down brute force attacks.
691 */
692 hapd->ap_pin_failures++;
693 wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u",
694 hapd->ap_pin_failures);
695 if (hapd->ap_pin_failures < 3)
696 return;
697
698 wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN");
699 hapd->ap_pin_failures = 0;
700 os_free(hapd->conf->ap_pin);
701 hapd->conf->ap_pin = NULL;
702 }
703
704 #endif /* CONFIG_WPS */
705
706
707 #ifdef CONFIG_CTRL_IFACE
708
709 int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
710 char *buf, size_t buflen)
711 {
712 if (wpa_s->ap_iface == NULL)
713 return -1;
714 return hostapd_ctrl_iface_sta_first(wpa_s->ap_iface->bss[0],
715 buf, buflen);
716 }
717
718
719 int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
720 char *buf, size_t buflen)
721 {
722 if (wpa_s->ap_iface == NULL)
723 return -1;
724 return hostapd_ctrl_iface_sta(wpa_s->ap_iface->bss[0], txtaddr,
725 buf, buflen);
726 }
727
728
729 int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
730 char *buf, size_t buflen)
731 {
732 if (wpa_s->ap_iface == NULL)
733 return -1;
734 return hostapd_ctrl_iface_sta_next(wpa_s->ap_iface->bss[0], txtaddr,
735 buf, buflen);
736 }
737
738
739 int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
740 size_t buflen, int verbose)
741 {
742 char *pos = buf, *end = buf + buflen;
743 int ret;
744 struct hostapd_bss_config *conf;
745
746 if (wpa_s->ap_iface == NULL)
747 return -1;
748
749 conf = wpa_s->ap_iface->bss[0]->conf;
750 if (conf->wpa == 0)
751 return 0;
752
753 ret = os_snprintf(pos, end - pos,
754 "pairwise_cipher=%s\n"
755 "group_cipher=%s\n"
756 "key_mgmt=%s\n",
757 wpa_cipher_txt(conf->rsn_pairwise),
758 wpa_cipher_txt(conf->wpa_group),
759 wpa_key_mgmt_txt(conf->wpa_key_mgmt,
760 conf->wpa));
761 if (ret < 0 || ret >= end - pos)
762 return pos - buf;
763 pos += ret;
764 return pos - buf;
765 }
766
767 #endif /* CONFIG_CTRL_IFACE */
768
769
770 int wpa_supplicant_ap_update_beacon(struct wpa_supplicant *wpa_s)
771 {
772 struct hostapd_iface *iface = wpa_s->ap_iface;
773 struct wpa_ssid *ssid = wpa_s->current_ssid;
774 struct hostapd_data *hapd;
775
776 if (ssid == NULL || wpa_s->ap_iface == NULL)
777 return -1;
778
779 #ifdef CONFIG_P2P
780 if (ssid->mode == WPAS_MODE_P2P_GO)
781 iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER;
782 else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
783 iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER |
784 P2P_GROUP_FORMATION;
785 #endif /* CONFIG_P2P */
786
787 ieee802_11_set_beacons(iface);
788 hapd = iface->bss[0];
789 hapd->drv.set_ap_wps_ie(hapd);
790
791 return 0;
792 }
793
794
795 int wpa_supplicant_ap_mac_addr_filter(struct wpa_supplicant *wpa_s,
796 const u8 *addr)
797 {
798 struct hostapd_data *hapd;
799 struct hostapd_bss_config *conf;
800
801 if (!wpa_s->ap_iface)
802 return -1;
803
804 if (addr)
805 wpa_printf(MSG_DEBUG, "AP: Set MAC address filter: " MACSTR,
806 MAC2STR(addr));
807 else
808 wpa_printf(MSG_DEBUG, "AP: Clear MAC address filter");
809
810 hapd = wpa_s->ap_iface->bss[0];
811 conf = hapd->conf;
812
813 os_free(conf->accept_mac);
814 conf->accept_mac = NULL;
815 conf->num_accept_mac = 0;
816 os_free(conf->deny_mac);
817 conf->deny_mac = NULL;
818 conf->num_deny_mac = 0;
819
820 if (addr == NULL) {
821 conf->macaddr_acl = ACCEPT_UNLESS_DENIED;
822 return 0;
823 }
824
825 conf->macaddr_acl = DENY_UNLESS_ACCEPTED;
826 conf->accept_mac = os_zalloc(sizeof(struct mac_acl_entry));
827 if (conf->accept_mac == NULL)
828 return -1;
829 os_memcpy(conf->accept_mac[0].addr, addr, ETH_ALEN);
830 conf->num_accept_mac = 1;
831
832 return 0;
833 }