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)
86 static struct hapd_driver_ops ap_driver_ops =
88 .name = "wpa_supplicant",
89 .init = ap_driver_init,
90 .deinit = ap_driver_deinit,
91 .send_ether = ap_driver_send_ether,
94 struct hapd_driver_ops *hostapd_drivers[] =
101 static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
102 struct wpa_ssid *ssid,
103 struct hostapd_config *conf)
105 struct hostapd_bss_config *bss = &conf->bss[0];
107 os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
109 if (ssid->frequency == 0) {
110 /* default channel 11 */
111 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
113 } else if (ssid->frequency >= 2412 && ssid->frequency <= 2472) {
114 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
115 conf->channel = (ssid->frequency - 2407) / 5;
116 } else if ((ssid->frequency >= 5180 && ssid->frequency <= 5240) ||
117 (ssid->frequency >= 5745 && ssid->frequency <= 5825)) {
118 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
119 conf->channel = (ssid->frequency - 5000) / 5;
121 wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
126 /* TODO: enable HT if driver supports it;
127 * drop to 11b if driver does not support 11g */
129 if (ssid->ssid_len == 0) {
130 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
133 os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
134 bss->ssid.ssid[ssid->ssid_len] = '\0';
135 bss->ssid.ssid_len = ssid->ssid_len;
136 bss->ssid.ssid_set = 1;
138 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
139 bss->wpa = ssid->proto;
140 bss->wpa_key_mgmt = ssid->key_mgmt;
141 bss->wpa_pairwise = ssid->pairwise_cipher;
142 if (ssid->passphrase) {
143 bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
144 if (hostapd_setup_wpa_psk(bss))
146 } else if (ssid->psk_set) {
147 os_free(bss->ssid.wpa_psk);
148 bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
149 if (bss->ssid.wpa_psk == NULL)
151 os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
152 bss->ssid.wpa_psk->group = 1;
159 int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
160 struct wpa_ssid *ssid)
162 struct wpa_driver_associate_params params;
163 struct hostapd_iface *hapd_iface;
164 struct hostapd_config *conf;
167 if (ssid->ssid == NULL || ssid->ssid_len == 0) {
168 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
172 wpa_supplicant_ap_deinit(wpa_s);
173 wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
174 if (hapd_iface == NULL)
177 wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
179 wpa_supplicant_ap_deinit(wpa_s);
183 if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
184 wpa_printf(MSG_ERROR, "Failed to create AP configuration");
185 wpa_supplicant_ap_deinit(wpa_s);
189 hapd_iface->num_bss = conf->num_bss;
190 hapd_iface->bss = os_zalloc(conf->num_bss *
191 sizeof(struct hostapd_data *));
192 if (hapd_iface->bss == NULL) {
193 wpa_supplicant_ap_deinit(wpa_s);
197 for (i = 0; i < conf->num_bss; i++) {
199 hostapd_alloc_bss_data(hapd_iface, conf,
201 if (hapd_iface->bss[i] == NULL) {
202 wpa_supplicant_ap_deinit(wpa_s);
207 if (hostapd_setup_interface(wpa_s->ap_iface)) {
208 wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
209 wpa_supplicant_ap_deinit(wpa_s);
213 wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
214 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
216 os_memset(¶ms, 0, sizeof(params));
217 params.ssid = ssid->ssid;
218 params.ssid_len = ssid->ssid_len;
219 params.mode = ssid->mode;
221 if (wpa_drv_associate(wpa_s, ¶ms) < 0) {
222 wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
230 void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
232 if (wpa_s->ap_iface == NULL)
235 hostapd_interface_deinit(wpa_s->ap_iface);
236 wpa_s->ap_iface = NULL;