]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/ap.c
DBus/P2P: Return Primary and Secondary device types as bytes
[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;
163 }
164
07d9a552
JM
165 /* Select group cipher based on the enabled pairwise cipher suites */
166 pairwise = 0;
167 if (bss->wpa & 1)
168 pairwise |= bss->wpa_pairwise;
169 if (bss->wpa & 2) {
170 if (bss->rsn_pairwise == 0)
171 bss->rsn_pairwise = bss->wpa_pairwise;
172 pairwise |= bss->rsn_pairwise;
173 }
174 if (pairwise & WPA_CIPHER_TKIP)
175 bss->wpa_group = WPA_CIPHER_TKIP;
176 else
177 bss->wpa_group = WPA_CIPHER_CCMP;
178
179 if (bss->wpa && bss->ieee802_1x)
180 bss->ssid.security_policy = SECURITY_WPA;
181 else if (bss->wpa)
182 bss->ssid.security_policy = SECURITY_WPA_PSK;
183 else if (bss->ieee802_1x) {
184 bss->ssid.security_policy = SECURITY_IEEE_802_1X;
185 bss->ssid.wep.default_len = bss->default_wep_key_len;
186 } else if (bss->ssid.wep.keys_set)
187 bss->ssid.security_policy = SECURITY_STATIC_WEP;
188 else
189 bss->ssid.security_policy = SECURITY_PLAINTEXT;
190
3ec97afe
JM
191#ifdef CONFIG_WPS
192 /*
193 * Enable WPS by default, but require user interaction to actually use
194 * it. Only the internal Registrar is supported.
195 */
196 bss->eap_server = 1;
197 bss->wps_state = 2;
9dd7d6b0 198 bss->ap_setup_locked = 2;
094393b1
JM
199 if (wpa_s->conf->config_methods)
200 bss->config_methods = os_strdup(wpa_s->conf->config_methods);
2f646b6e
JB
201 os_memcpy(bss->device_type, wpa_s->conf->device_type,
202 WPS_DEV_TYPE_LEN);
e44f8bf2
JM
203 if (wpa_s->conf->device_name) {
204 bss->device_name = os_strdup(wpa_s->conf->device_name);
205 bss->friendly_name = os_strdup(wpa_s->conf->device_name);
206 }
56815b2b
JM
207 if (wpa_s->conf->manufacturer)
208 bss->manufacturer = os_strdup(wpa_s->conf->manufacturer);
209 if (wpa_s->conf->model_name)
210 bss->model_name = os_strdup(wpa_s->conf->model_name);
211 if (wpa_s->conf->model_number)
212 bss->model_number = os_strdup(wpa_s->conf->model_number);
213 if (wpa_s->conf->serial_number)
214 bss->serial_number = os_strdup(wpa_s->conf->serial_number);
ab45223b
JM
215 if (is_nil_uuid(wpa_s->conf->uuid))
216 os_memcpy(bss->uuid, wpa_s->wps->uuid, WPS_UUID_LEN);
217 else
218 os_memcpy(bss->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
56815b2b
JM
219 os_memcpy(bss->os_version, wpa_s->conf->os_version, 4);
220#endif /* CONFIG_WPS */
e44f8bf2 221
de979d8f
JM
222 if (wpa_s->max_stations &&
223 wpa_s->max_stations < wpa_s->conf->max_num_sta)
224 bss->max_num_sta = wpa_s->max_stations;
225 else
226 bss->max_num_sta = wpa_s->conf->max_num_sta;
dae608d5 227
0d7e5a3a
JB
228 bss->disassoc_low_ack = wpa_s->conf->disassoc_low_ack;
229
07f117ed
JM
230 return 0;
231}
232
233
c706d5aa
JM
234static void ap_public_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
235{
e44f8bf2
JM
236#ifdef CONFIG_P2P
237 struct wpa_supplicant *wpa_s = ctx;
238 const struct ieee80211_mgmt *mgmt;
239 size_t hdr_len;
240
241 mgmt = (const struct ieee80211_mgmt *) buf;
242 hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
243 if (hdr_len > len)
244 return;
245 wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
246 mgmt->u.action.category,
247 &mgmt->u.action.u.vs_public_action.action,
248 len - hdr_len, freq);
249#endif /* CONFIG_P2P */
250}
251
252
a0dee797
AGS
253static void ap_wps_event_cb(void *ctx, enum wps_event event,
254 union wps_event_data *data)
255{
ca806fb5 256#ifdef CONFIG_P2P
a0dee797 257 struct wpa_supplicant *wpa_s = ctx;
8be6450c
JMB
258
259 if (event == WPS_EV_FAIL) {
260 struct wps_event_fail *fail = &data->fail;
261
262 if (wpa_s->parent && wpa_s->parent != wpa_s &&
263 wpa_s == wpa_s->global->p2p_group_formation) {
264 /*
265 * src/ap/wps_hostapd.c has already sent this on the
266 * main interface, so only send on the parent interface
267 * here if needed.
268 */
269 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
270 "msg=%d config_error=%d",
271 fail->msg, fail->config_error);
272 }
273 wpas_p2p_wps_failed(wpa_s, fail);
a0dee797 274 }
ca806fb5 275#endif /* CONFIG_P2P */
a0dee797
AGS
276}
277
278
d8a43924
JB
279static void ap_sta_authorized_cb(void *ctx, const u8 *mac_addr,
280 int authorized)
281{
282 wpas_notify_sta_authorized(ctx, mac_addr, authorized);
283}
284
285
e44f8bf2
JM
286static int ap_vendor_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
287{
288#ifdef CONFIG_P2P
289 struct wpa_supplicant *wpa_s = ctx;
290 const struct ieee80211_mgmt *mgmt;
291 size_t hdr_len;
292
293 mgmt = (const struct ieee80211_mgmt *) buf;
294 hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
295 if (hdr_len > len)
296 return -1;
297 wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
298 mgmt->u.action.category,
299 &mgmt->u.action.u.vs_public_action.action,
300 len - hdr_len, freq);
301#endif /* CONFIG_P2P */
302 return 0;
c706d5aa
JM
303}
304
305
04a85e44
JM
306static int ap_probe_req_rx(void *ctx, const u8 *sa, const u8 *da,
307 const u8 *bssid, const u8 *ie, size_t ie_len)
c706d5aa 308{
e44f8bf2
JM
309#ifdef CONFIG_P2P
310 struct wpa_supplicant *wpa_s = ctx;
04a85e44 311 return wpas_p2p_probe_req_rx(wpa_s, sa, da, bssid, ie, ie_len);
e44f8bf2 312#else /* CONFIG_P2P */
c706d5aa 313 return 0;
e44f8bf2 314#endif /* CONFIG_P2P */
c706d5aa
JM
315}
316
317
318static void ap_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
319 const u8 *uuid_e)
320{
e44f8bf2
JM
321#ifdef CONFIG_P2P
322 struct wpa_supplicant *wpa_s = ctx;
323 wpas_p2p_wps_success(wpa_s, mac_addr, 1);
324#endif /* CONFIG_P2P */
c706d5aa
JM
325}
326
327
c76e5d7f
JB
328static void wpas_ap_configured_cb(void *ctx)
329{
330 struct wpa_supplicant *wpa_s = ctx;
331
332 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
333
334 if (wpa_s->ap_configured_cb)
335 wpa_s->ap_configured_cb(wpa_s->ap_configured_cb_ctx,
336 wpa_s->ap_configured_cb_data);
337}
338
339
2d5b792d
JM
340int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
341 struct wpa_ssid *ssid)
342{
343 struct wpa_driver_associate_params params;
344 struct hostapd_iface *hapd_iface;
345 struct hostapd_config *conf;
346 size_t i;
f1a48710 347
2d5b792d
JM
348 if (ssid->ssid == NULL || ssid->ssid_len == 0) {
349 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
350 return -1;
f1a48710
JM
351 }
352
2d5b792d 353 wpa_supplicant_ap_deinit(wpa_s);
d2440ba0
JM
354
355 wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
356 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
357
358 os_memset(&params, 0, sizeof(params));
359 params.ssid = ssid->ssid;
360 params.ssid_len = ssid->ssid_len;
d7dcba70
JM
361 switch (ssid->mode) {
362 case WPAS_MODE_INFRA:
363 params.mode = IEEE80211_MODE_INFRA;
364 break;
365 case WPAS_MODE_IBSS:
366 params.mode = IEEE80211_MODE_IBSS;
367 break;
368 case WPAS_MODE_AP:
2c5d725c
JM
369 case WPAS_MODE_P2P_GO:
370 case WPAS_MODE_P2P_GROUP_FORMATION:
d7dcba70
JM
371 params.mode = IEEE80211_MODE_AP;
372 break;
373 }
d2440ba0
JM
374 params.freq = ssid->frequency;
375
508545f3
JM
376 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK)
377 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
378 else
379 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
380 params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
381
382 if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
383 wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
384 else if (ssid->pairwise_cipher & WPA_CIPHER_TKIP)
385 wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
386 else if (ssid->pairwise_cipher & WPA_CIPHER_NONE)
387 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
388 else {
389 wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
390 "cipher.");
391 return -1;
392 }
393 params.pairwise_suite = cipher_suite2driver(wpa_s->pairwise_cipher);
394 params.group_suite = params.pairwise_suite;
395
6e3f4b89
JM
396#ifdef CONFIG_P2P
397 if (ssid->mode == WPAS_MODE_P2P_GO ||
398 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
399 params.p2p = 1;
0f66abd2 400 wpa_drv_set_intra_bss(wpa_s, wpa_s->conf->p2p_intra_bss);
6e3f4b89
JM
401#endif /* CONFIG_P2P */
402
eea2fd9e
JM
403 if (wpa_s->parent->set_ap_uapsd)
404 params.uapsd = wpa_s->parent->ap_uapsd;
405 else
406 params.uapsd = -1;
407
d2440ba0
JM
408 if (wpa_drv_associate(wpa_s, &params) < 0) {
409 wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
410 return -1;
411 }
412
2d5b792d
JM
413 wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
414 if (hapd_iface == NULL)
415 return -1;
0f2b2c19 416 hapd_iface->owner = wpa_s;
1f1b62a0 417
2d5b792d
JM
418 wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
419 if (conf == NULL) {
420 wpa_supplicant_ap_deinit(wpa_s);
421 return -1;
422 }
1f1b62a0 423
07f117ed
JM
424 if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
425 wpa_printf(MSG_ERROR, "Failed to create AP configuration");
426 wpa_supplicant_ap_deinit(wpa_s);
427 return -1;
428 }
429
e44f8bf2
JM
430#ifdef CONFIG_P2P
431 if (ssid->mode == WPAS_MODE_P2P_GO)
432 conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER;
433 else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
434 conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER |
435 P2P_GROUP_FORMATION;
436#endif /* CONFIG_P2P */
437
2d5b792d
JM
438 hapd_iface->num_bss = conf->num_bss;
439 hapd_iface->bss = os_zalloc(conf->num_bss *
440 sizeof(struct hostapd_data *));
441 if (hapd_iface->bss == NULL) {
442 wpa_supplicant_ap_deinit(wpa_s);
443 return -1;
444 }
1f1b62a0 445
2d5b792d
JM
446 for (i = 0; i < conf->num_bss; i++) {
447 hapd_iface->bss[i] =
448 hostapd_alloc_bss_data(hapd_iface, conf,
449 &conf->bss[i]);
450 if (hapd_iface->bss[i] == NULL) {
451 wpa_supplicant_ap_deinit(wpa_s);
452 return -1;
453 }
4f760fcc
JM
454
455 hapd_iface->bss[i]->msg_ctx = wpa_s;
c706d5aa
JM
456 hapd_iface->bss[i]->public_action_cb = ap_public_action_rx;
457 hapd_iface->bss[i]->public_action_cb_ctx = wpa_s;
e44f8bf2
JM
458 hapd_iface->bss[i]->vendor_action_cb = ap_vendor_action_rx;
459 hapd_iface->bss[i]->vendor_action_cb_ctx = wpa_s;
c706d5aa
JM
460 hostapd_register_probereq_cb(hapd_iface->bss[i],
461 ap_probe_req_rx, wpa_s);
462 hapd_iface->bss[i]->wps_reg_success_cb = ap_wps_reg_success_cb;
463 hapd_iface->bss[i]->wps_reg_success_cb_ctx = wpa_s;
a0dee797
AGS
464 hapd_iface->bss[i]->wps_event_cb = ap_wps_event_cb;
465 hapd_iface->bss[i]->wps_event_cb_ctx = wpa_s;
d8a43924
JB
466 hapd_iface->bss[i]->sta_authorized_cb = ap_sta_authorized_cb;
467 hapd_iface->bss[i]->sta_authorized_cb_ctx = wpa_s;
e44f8bf2
JM
468#ifdef CONFIG_P2P
469 hapd_iface->bss[i]->p2p = wpa_s->global->p2p;
470 hapd_iface->bss[i]->p2p_group = wpas_p2p_group_init(
471 wpa_s, ssid->p2p_persistent_group,
472 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION);
473#endif /* CONFIG_P2P */
c76e5d7f
JB
474 hapd_iface->bss[i]->setup_complete_cb = wpas_ap_configured_cb;
475 hapd_iface->bss[i]->setup_complete_cb_ctx = wpa_s;
2d5b792d
JM
476 }
477
a911a6e6
JM
478 os_memcpy(hapd_iface->bss[0]->own_addr, wpa_s->own_addr, ETH_ALEN);
479 hapd_iface->bss[0]->driver = wpa_s->driver;
480 hapd_iface->bss[0]->drv_priv = wpa_s->drv_priv;
481
e73edcaa
JM
482 wpa_s->current_ssid = ssid;
483 os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
484 wpa_s->assoc_freq = ssid->frequency;
485
a911a6e6 486 if (hostapd_setup_interface(wpa_s->ap_iface)) {
2d5b792d
JM
487 wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
488 wpa_supplicant_ap_deinit(wpa_s);
489 return -1;
1f1b62a0
JM
490 }
491
2d5b792d
JM
492 return 0;
493}
494
495
496void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
497{
0fe620de 498#ifdef CONFIG_WPS
70d84f11 499 eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
0fe620de 500#endif /* CONFIG_WPS */
70d84f11 501
2d5b792d
JM
502 if (wpa_s->ap_iface == NULL)
503 return;
504
7a649c7d 505 wpa_s->current_ssid = NULL;
3c85f144 506 wpa_s->assoc_freq = 0;
ca62e114 507 wpa_s->reassociated_connection = 0;
e44f8bf2 508#ifdef CONFIG_P2P
0e14267a
JM
509 if (wpa_s->ap_iface->bss)
510 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
e44f8bf2
JM
511 wpas_p2p_group_deinit(wpa_s);
512#endif /* CONFIG_P2P */
2d5b792d 513 hostapd_interface_deinit(wpa_s->ap_iface);
f7c47833 514 hostapd_interface_free(wpa_s->ap_iface);
2d5b792d 515 wpa_s->ap_iface = NULL;
7a649c7d 516 wpa_drv_deinit_ap(wpa_s);
1f1b62a0 517}
0915d02c
JM
518
519
520void ap_tx_status(void *ctx, const u8 *addr,
521 const u8 *buf, size_t len, int ack)
522{
f8b1f695 523#ifdef NEED_AP_MLME
0915d02c
JM
524 struct wpa_supplicant *wpa_s = ctx;
525 hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
f8b1f695 526#endif /* NEED_AP_MLME */
0915d02c
JM
527}
528
529
fdbe50ed 530void ap_rx_from_unknown_sta(void *ctx, const u8 *frame, size_t len)
0915d02c 531{
f8b1f695 532#ifdef NEED_AP_MLME
0915d02c 533 struct wpa_supplicant *wpa_s = ctx;
fdbe50ed
JM
534 const struct ieee80211_hdr *hdr =
535 (const struct ieee80211_hdr *) frame;
fbbfcbac
FF
536 u16 fc = le_to_host16(hdr->frame_control);
537 ieee802_11_rx_from_unknown(wpa_s->ap_iface->bss[0], hdr->addr2,
538 (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
539 (WLAN_FC_TODS | WLAN_FC_FROMDS));
f8b1f695 540#endif /* NEED_AP_MLME */
0915d02c
JM
541}
542
543
2a8b7416 544void ap_mgmt_rx(void *ctx, struct rx_mgmt *rx_mgmt)
0915d02c 545{
f8b1f695 546#ifdef NEED_AP_MLME
0915d02c 547 struct wpa_supplicant *wpa_s = ctx;
2a8b7416
JM
548 struct hostapd_frame_info fi;
549 os_memset(&fi, 0, sizeof(fi));
550 fi.datarate = rx_mgmt->datarate;
551 fi.ssi_signal = rx_mgmt->ssi_signal;
552 ieee802_11_mgmt(wpa_s->ap_iface->bss[0], rx_mgmt->frame,
553 rx_mgmt->frame_len, &fi);
f8b1f695 554#endif /* NEED_AP_MLME */
0915d02c
JM
555}
556
557
f8b1f695 558void ap_mgmt_tx_cb(void *ctx, const u8 *buf, size_t len, u16 stype, int ok)
0915d02c 559{
f8b1f695 560#ifdef NEED_AP_MLME
0915d02c
JM
561 struct wpa_supplicant *wpa_s = ctx;
562 ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
fe6bdb77 563#endif /* NEED_AP_MLME */
f8b1f695 564}
db149ac9
JM
565
566
567void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
568 const u8 *src_addr, const u8 *buf, size_t len)
569{
a8e0505b 570 ieee802_1x_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
db149ac9 571}
3ec97afe
JM
572
573
574#ifdef CONFIG_WPS
575
d601247c
JM
576int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
577 const u8 *p2p_dev_addr)
3ec97afe 578{
48b357a9
JM
579 if (!wpa_s->ap_iface)
580 return -1;
d601247c
JM
581 return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0],
582 p2p_dev_addr);
3ec97afe
JM
583}
584
585
2f9929ff
AC
586static int wpa_supplicant_ap_wps_sta_cancel(struct hostapd_data *hapd,
587 struct sta_info *sta, void *ctx)
588{
589 if (sta && (sta->flags & WLAN_STA_WPS)) {
590 ap_sta_deauthenticate(hapd, sta,
591 WLAN_REASON_PREV_AUTH_NOT_VALID);
592 wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
593 __func__, MAC2STR(sta->addr));
594 return 1;
595 }
596
597 return 0;
598}
599
600
601int wpa_supplicant_ap_wps_cancel(struct wpa_supplicant *wpa_s)
602{
603 struct wps_registrar *reg;
604 int reg_sel = 0, wps_sta = 0;
605
606 if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0]->wps)
607 return -1;
608
609 reg = wpa_s->ap_iface->bss[0]->wps->registrar;
610 reg_sel = wps_registrar_wps_cancel(reg);
611 wps_sta = ap_for_each_sta(wpa_s->ap_iface->bss[0],
612 wpa_supplicant_ap_wps_sta_cancel, NULL);
613
614 if (!reg_sel && !wps_sta) {
615 wpa_printf(MSG_DEBUG, "No WPS operation in progress at this "
616 "time");
617 return -1;
618 }
619
620 /*
621 * There are 2 cases to return wps cancel as success:
622 * 1. When wps cancel was initiated but no connection has been
623 * established with client yet.
624 * 2. Client is in the middle of exchanging WPS messages.
625 */
626
627 return 0;
628}
629
630
3ec97afe
JM
631int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
632 const char *pin, char *buf, size_t buflen)
633{
634 int ret, ret_len = 0;
635
48b357a9
JM
636 if (!wpa_s->ap_iface)
637 return -1;
638
3ec97afe
JM
639 if (pin == NULL) {
640 unsigned int rpin = wps_generate_pin();
641 ret_len = os_snprintf(buf, buflen, "%d", rpin);
642 pin = buf;
f80a2237
JM
643 } else
644 ret_len = os_snprintf(buf, buflen, "%s", pin);
3ec97afe 645
31fcea93
JM
646 ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], bssid, "any", pin,
647 0);
3ec97afe
JM
648 if (ret)
649 return -1;
650 return ret_len;
651}
652
70d84f11
JM
653
654static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
655{
656 struct wpa_supplicant *wpa_s = eloop_data;
657 wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
658 wpas_wps_ap_pin_disable(wpa_s);
659}
660
661
662static void wpas_wps_ap_pin_enable(struct wpa_supplicant *wpa_s, int timeout)
663{
664 struct hostapd_data *hapd;
665
666 if (wpa_s->ap_iface == NULL)
667 return;
668 hapd = wpa_s->ap_iface->bss[0];
669 wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
670 hapd->ap_pin_failures = 0;
671 eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
672 if (timeout > 0)
673 eloop_register_timeout(timeout, 0,
674 wpas_wps_ap_pin_timeout, wpa_s, NULL);
675}
676
677
678void wpas_wps_ap_pin_disable(struct wpa_supplicant *wpa_s)
679{
680 struct hostapd_data *hapd;
681
682 if (wpa_s->ap_iface == NULL)
683 return;
684 wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
685 hapd = wpa_s->ap_iface->bss[0];
686 os_free(hapd->conf->ap_pin);
687 hapd->conf->ap_pin = NULL;
688 eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
689}
690
691
692const char * wpas_wps_ap_pin_random(struct wpa_supplicant *wpa_s, int timeout)
693{
694 struct hostapd_data *hapd;
695 unsigned int pin;
696 char pin_txt[9];
697
698 if (wpa_s->ap_iface == NULL)
699 return NULL;
700 hapd = wpa_s->ap_iface->bss[0];
701 pin = wps_generate_pin();
702 os_snprintf(pin_txt, sizeof(pin_txt), "%u", pin);
703 os_free(hapd->conf->ap_pin);
704 hapd->conf->ap_pin = os_strdup(pin_txt);
705 if (hapd->conf->ap_pin == NULL)
706 return NULL;
707 wpas_wps_ap_pin_enable(wpa_s, timeout);
708
709 return hapd->conf->ap_pin;
710}
711
712
713const char * wpas_wps_ap_pin_get(struct wpa_supplicant *wpa_s)
714{
715 struct hostapd_data *hapd;
716 if (wpa_s->ap_iface == NULL)
717 return NULL;
718 hapd = wpa_s->ap_iface->bss[0];
719 return hapd->conf->ap_pin;
720}
721
722
723int wpas_wps_ap_pin_set(struct wpa_supplicant *wpa_s, const char *pin,
724 int timeout)
725{
726 struct hostapd_data *hapd;
727 char pin_txt[9];
728 int ret;
729
730 if (wpa_s->ap_iface == NULL)
731 return -1;
732 hapd = wpa_s->ap_iface->bss[0];
733 ret = os_snprintf(pin_txt, sizeof(pin_txt), "%s", pin);
734 if (ret < 0 || ret >= (int) sizeof(pin_txt))
735 return -1;
736 os_free(hapd->conf->ap_pin);
737 hapd->conf->ap_pin = os_strdup(pin_txt);
738 if (hapd->conf->ap_pin == NULL)
739 return -1;
740 wpas_wps_ap_pin_enable(wpa_s, timeout);
741
742 return 0;
743}
744
745
746void wpa_supplicant_ap_pwd_auth_fail(struct wpa_supplicant *wpa_s)
747{
748 struct hostapd_data *hapd;
749
750 if (wpa_s->ap_iface == NULL)
751 return;
752 hapd = wpa_s->ap_iface->bss[0];
753
754 /*
755 * Registrar failed to prove its knowledge of the AP PIN. Disable AP
756 * PIN if this happens multiple times to slow down brute force attacks.
757 */
758 hapd->ap_pin_failures++;
759 wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u",
760 hapd->ap_pin_failures);
761 if (hapd->ap_pin_failures < 3)
762 return;
763
764 wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN");
765 hapd->ap_pin_failures = 0;
766 os_free(hapd->conf->ap_pin);
767 hapd->conf->ap_pin = NULL;
768}
769
3ec97afe 770#endif /* CONFIG_WPS */
e653b622
JM
771
772
35deb646
JM
773#ifdef CONFIG_CTRL_IFACE
774
e653b622
JM
775int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
776 char *buf, size_t buflen)
777{
778 if (wpa_s->ap_iface == NULL)
779 return -1;
780 return hostapd_ctrl_iface_sta_first(wpa_s->ap_iface->bss[0],
781 buf, buflen);
782}
783
784
785int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
786 char *buf, size_t buflen)
787{
788 if (wpa_s->ap_iface == NULL)
789 return -1;
790 return hostapd_ctrl_iface_sta(wpa_s->ap_iface->bss[0], txtaddr,
791 buf, buflen);
792}
793
794
795int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
796 char *buf, size_t buflen)
797{
798 if (wpa_s->ap_iface == NULL)
799 return -1;
800 return hostapd_ctrl_iface_sta_next(wpa_s->ap_iface->bss[0], txtaddr,
801 buf, buflen);
802}
35deb646 803
43fb5297
JM
804
805int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
806 size_t buflen, int verbose)
807{
808 char *pos = buf, *end = buf + buflen;
809 int ret;
810 struct hostapd_bss_config *conf;
811
812 if (wpa_s->ap_iface == NULL)
813 return -1;
814
815 conf = wpa_s->ap_iface->bss[0]->conf;
816 if (conf->wpa == 0)
817 return 0;
818
819 ret = os_snprintf(pos, end - pos,
820 "pairwise_cipher=%s\n"
821 "group_cipher=%s\n"
822 "key_mgmt=%s\n",
823 wpa_cipher_txt(conf->rsn_pairwise),
824 wpa_cipher_txt(conf->wpa_group),
825 wpa_key_mgmt_txt(conf->wpa_key_mgmt,
826 conf->wpa));
827 if (ret < 0 || ret >= end - pos)
828 return pos - buf;
829 pos += ret;
830 return pos - buf;
831}
832
35deb646 833#endif /* CONFIG_CTRL_IFACE */
f90ceeaa
JM
834
835
b22128ef
JM
836int wpa_supplicant_ap_update_beacon(struct wpa_supplicant *wpa_s)
837{
838 struct hostapd_iface *iface = wpa_s->ap_iface;
839 struct wpa_ssid *ssid = wpa_s->current_ssid;
840 struct hostapd_data *hapd;
841
842 if (ssid == NULL || wpa_s->ap_iface == NULL)
843 return -1;
844
e44f8bf2
JM
845#ifdef CONFIG_P2P
846 if (ssid->mode == WPAS_MODE_P2P_GO)
847 iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER;
848 else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
849 iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER |
850 P2P_GROUP_FORMATION;
851#endif /* CONFIG_P2P */
852
b22128ef
JM
853 ieee802_11_set_beacons(iface);
854 hapd = iface->bss[0];
fe99fb74 855 hostapd_set_ap_wps_ie(hapd);
b22128ef
JM
856
857 return 0;
858}
859
860
f90ceeaa
JM
861int wpa_supplicant_ap_mac_addr_filter(struct wpa_supplicant *wpa_s,
862 const u8 *addr)
863{
864 struct hostapd_data *hapd;
865 struct hostapd_bss_config *conf;
866
867 if (!wpa_s->ap_iface)
868 return -1;
869
870 if (addr)
871 wpa_printf(MSG_DEBUG, "AP: Set MAC address filter: " MACSTR,
872 MAC2STR(addr));
873 else
874 wpa_printf(MSG_DEBUG, "AP: Clear MAC address filter");
875
876 hapd = wpa_s->ap_iface->bss[0];
877 conf = hapd->conf;
878
879 os_free(conf->accept_mac);
880 conf->accept_mac = NULL;
881 conf->num_accept_mac = 0;
882 os_free(conf->deny_mac);
883 conf->deny_mac = NULL;
884 conf->num_deny_mac = 0;
885
886 if (addr == NULL) {
887 conf->macaddr_acl = ACCEPT_UNLESS_DENIED;
888 return 0;
889 }
890
891 conf->macaddr_acl = DENY_UNLESS_ACCEPTED;
892 conf->accept_mac = os_zalloc(sizeof(struct mac_acl_entry));
893 if (conf->accept_mac == NULL)
894 return -1;
895 os_memcpy(conf->accept_mac[0].addr, addr, ETH_ALEN);
896 conf->num_accept_mac = 1;
897
898 return 0;
899}