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