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