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