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