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