2 * WPA Supplicant - Basic AP mode support routines
3 * Copyright (c) 2003-2009, Jouni Malinen <j@w1.fi>
4 * Copyright (c) 2009, Atheros Communications
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.
10 * Alternatively, this software may be distributed under the terms of BSD
13 * See README and COPYING for more details.
19 #include "../hostapd/hostapd.h"
20 #include "../hostapd/config.h"
21 #include "../hostapd/driver.h"
22 #include "eap_common/eap_defs.h"
23 #include "eap_server/eap_methods.h"
24 #include "eap_common/eap_wsc_common.h"
25 #include "config_ssid.h"
26 #include "wpa_supplicant_i.h"
31 int hostapd_for_each_interface(int (*cb)(struct hostapd_iface *iface,
32 void *ctx), void *ctx)
39 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
45 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
50 struct ap_driver_data {
51 struct hostapd_data *hapd;
55 static void * ap_driver_init(struct hostapd_data *hapd)
57 struct ap_driver_data *drv;
59 drv = os_zalloc(sizeof(struct ap_driver_data));
61 wpa_printf(MSG_ERROR, "Could not allocate memory for AP "
71 static void ap_driver_deinit(void *priv)
73 struct ap_driver_data *drv = priv;
79 static int ap_driver_send_ether(void *priv, const u8 *dst, const u8 *src,
80 u16 proto, const u8 *data, size_t data_len)
82 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
87 static int ap_driver_set_key(const char *iface, void *priv, wpa_alg alg,
88 const u8 *addr, int key_idx, int set_tx,
89 const u8 *seq, size_t seq_len, const u8 *key,
92 struct ap_driver_data *drv = priv;
93 struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
94 return wpa_drv_set_key(wpa_s, alg, addr, key_idx, set_tx, seq, seq_len,
99 static int ap_driver_get_seqnum(const char *iface, void *priv, const u8 *addr,
102 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
107 static int ap_driver_flush(void *priv)
109 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
114 static int ap_driver_read_sta_data(void *priv,
115 struct hostap_sta_driver_data *data,
118 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
123 static int ap_driver_sta_set_flags(void *priv, const u8 *addr, int total_flags,
124 int flags_or, int flags_and)
126 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
131 static int ap_driver_sta_deauth(void *priv, const u8 *addr, int reason)
133 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
138 static int ap_driver_sta_disassoc(void *priv, const u8 *addr, int reason)
140 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
145 static int ap_driver_sta_remove(void *priv, const u8 *addr)
147 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
152 static int ap_driver_send_mgmt_frame(void *priv, const void *data, size_t len,
155 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
160 static int ap_driver_sta_add(const char *ifname, void *priv,
161 struct hostapd_sta_add_params *params)
163 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
168 static int ap_driver_get_inact_sec(void *priv, const u8 *addr)
170 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
175 static int ap_driver_set_freq(void *priv, struct hostapd_freq_params *freq)
177 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
182 static int ap_driver_set_beacon(const char *iface, void *priv,
183 u8 *head, size_t head_len,
184 u8 *tail, size_t tail_len)
186 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
191 static int ap_driver_set_beacon_int(void *priv, int value)
193 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
198 static int ap_driver_set_dtim_period(const char *iface, void *priv, int value)
200 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
205 static int ap_driver_set_cts_protect(void *priv, int value)
207 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
212 static int ap_driver_set_preamble(void *priv, int value)
214 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
219 static int ap_driver_set_short_slot_time(void *priv, int value)
221 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
226 static int ap_driver_set_tx_queue_params(void *priv, int queue, int aifs,
227 int cw_min, int cw_max,
230 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
235 static struct hostapd_hw_modes *ap_driver_get_hw_feature_data(void *priv,
239 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
244 static struct hapd_driver_ops ap_driver_ops =
246 .name = "wpa_supplicant",
247 .init = ap_driver_init,
248 .deinit = ap_driver_deinit,
249 .send_ether = ap_driver_send_ether,
250 .set_key = ap_driver_set_key,
251 .get_seqnum = ap_driver_get_seqnum,
252 .flush = ap_driver_flush,
253 .read_sta_data = ap_driver_read_sta_data,
254 .sta_set_flags = ap_driver_sta_set_flags,
255 .sta_deauth = ap_driver_sta_deauth,
256 .sta_disassoc = ap_driver_sta_disassoc,
257 .sta_remove = ap_driver_sta_remove,
258 .send_mgmt_frame = ap_driver_send_mgmt_frame,
259 .sta_add = ap_driver_sta_add,
260 .get_inact_sec = ap_driver_get_inact_sec,
261 .set_freq = ap_driver_set_freq,
262 .set_beacon = ap_driver_set_beacon,
263 .set_beacon_int = ap_driver_set_beacon_int,
264 .set_dtim_period = ap_driver_set_dtim_period,
265 .set_cts_protect = ap_driver_set_cts_protect,
266 .set_preamble = ap_driver_set_preamble,
267 .set_short_slot_time = ap_driver_set_short_slot_time,
268 .set_tx_queue_params = ap_driver_set_tx_queue_params,
269 .get_hw_feature_data = ap_driver_get_hw_feature_data,
272 struct hapd_driver_ops *hostapd_drivers[] =
279 static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
280 struct wpa_ssid *ssid,
281 struct hostapd_config *conf)
283 struct hostapd_bss_config *bss = &conf->bss[0];
285 os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
287 if (ssid->frequency == 0) {
288 /* default channel 11 */
289 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
291 } else if (ssid->frequency >= 2412 && ssid->frequency <= 2472) {
292 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
293 conf->channel = (ssid->frequency - 2407) / 5;
294 } else if ((ssid->frequency >= 5180 && ssid->frequency <= 5240) ||
295 (ssid->frequency >= 5745 && ssid->frequency <= 5825)) {
296 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
297 conf->channel = (ssid->frequency - 5000) / 5;
299 wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
304 /* TODO: enable HT if driver supports it;
305 * drop to 11b if driver does not support 11g */
307 if (ssid->ssid_len == 0) {
308 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
311 os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
312 bss->ssid.ssid[ssid->ssid_len] = '\0';
313 bss->ssid.ssid_len = ssid->ssid_len;
314 bss->ssid.ssid_set = 1;
316 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
317 bss->wpa = ssid->proto;
318 bss->wpa_key_mgmt = ssid->key_mgmt;
319 bss->wpa_pairwise = ssid->pairwise_cipher;
320 if (ssid->passphrase) {
321 bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
322 if (hostapd_setup_wpa_psk(bss))
324 } else if (ssid->psk_set) {
325 os_free(bss->ssid.wpa_psk);
326 bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
327 if (bss->ssid.wpa_psk == NULL)
329 os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
330 bss->ssid.wpa_psk->group = 1;
337 int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
338 struct wpa_ssid *ssid)
340 struct wpa_driver_associate_params params;
341 struct hostapd_iface *hapd_iface;
342 struct hostapd_config *conf;
345 if (ssid->ssid == NULL || ssid->ssid_len == 0) {
346 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
350 wpa_supplicant_ap_deinit(wpa_s);
351 wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
352 if (hapd_iface == NULL)
354 hapd_iface->owner = wpa_s;
356 wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
358 wpa_supplicant_ap_deinit(wpa_s);
362 if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
363 wpa_printf(MSG_ERROR, "Failed to create AP configuration");
364 wpa_supplicant_ap_deinit(wpa_s);
368 hapd_iface->num_bss = conf->num_bss;
369 hapd_iface->bss = os_zalloc(conf->num_bss *
370 sizeof(struct hostapd_data *));
371 if (hapd_iface->bss == NULL) {
372 wpa_supplicant_ap_deinit(wpa_s);
376 for (i = 0; i < conf->num_bss; i++) {
378 hostapd_alloc_bss_data(hapd_iface, conf,
380 if (hapd_iface->bss[i] == NULL) {
381 wpa_supplicant_ap_deinit(wpa_s);
386 if (hostapd_setup_interface(wpa_s->ap_iface)) {
387 wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
388 wpa_supplicant_ap_deinit(wpa_s);
392 wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
393 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
395 os_memset(¶ms, 0, sizeof(params));
396 params.ssid = ssid->ssid;
397 params.ssid_len = ssid->ssid_len;
398 params.mode = ssid->mode;
400 if (wpa_drv_associate(wpa_s, ¶ms) < 0) {
401 wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
409 void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
411 if (wpa_s->ap_iface == NULL)
414 hostapd_interface_deinit(wpa_s->ap_iface);
415 wpa_s->ap_iface = NULL;