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