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