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 "ap/hostapd.h"
20 #include "ap/config.h"
22 #include "ap/ieee802_11.h"
23 #endif /* NEED_AP_MLME */
24 #include "ap/wps_hostapd.h"
25 #include "ap/ctrl_iface_ap.h"
26 #include "eap_common/eap_defs.h"
27 #include "eap_server/eap_methods.h"
28 #include "eap_common/eap_wsc_common.h"
30 #include "config_ssid.h"
32 #include "wpa_supplicant_i.h"
37 struct hapd_interfaces {
39 struct hostapd_iface **iface;
43 int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
44 int (*cb)(struct hostapd_iface *iface,
45 void *ctx), void *ctx)
52 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
58 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
63 static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
64 struct wpa_ssid *ssid,
65 struct hostapd_config *conf)
67 struct hostapd_bss_config *bss = &conf->bss[0];
70 conf->driver = wpa_s->driver;
72 os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
74 if (ssid->frequency == 0) {
75 /* default channel 11 */
76 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
78 } else if (ssid->frequency >= 2412 && ssid->frequency <= 2472) {
79 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
80 conf->channel = (ssid->frequency - 2407) / 5;
81 } else if ((ssid->frequency >= 5180 && ssid->frequency <= 5240) ||
82 (ssid->frequency >= 5745 && ssid->frequency <= 5825)) {
83 conf->hw_mode = HOSTAPD_MODE_IEEE80211A;
84 conf->channel = (ssid->frequency - 5000) / 5;
86 wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
91 /* TODO: enable HT if driver supports it;
92 * drop to 11b if driver does not support 11g */
94 if (ssid->ssid_len == 0) {
95 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
98 os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
99 bss->ssid.ssid[ssid->ssid_len] = '\0';
100 bss->ssid.ssid_len = ssid->ssid_len;
101 bss->ssid.ssid_set = 1;
103 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
104 bss->wpa = ssid->proto;
105 bss->wpa_key_mgmt = ssid->key_mgmt;
106 bss->wpa_pairwise = ssid->pairwise_cipher;
107 if (ssid->passphrase) {
108 bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
109 } else if (ssid->psk_set) {
110 os_free(bss->ssid.wpa_psk);
111 bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
112 if (bss->ssid.wpa_psk == NULL)
114 os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
115 bss->ssid.wpa_psk->group = 1;
118 /* Select group cipher based on the enabled pairwise cipher suites */
121 pairwise |= bss->wpa_pairwise;
123 if (bss->rsn_pairwise == 0)
124 bss->rsn_pairwise = bss->wpa_pairwise;
125 pairwise |= bss->rsn_pairwise;
127 if (pairwise & WPA_CIPHER_TKIP)
128 bss->wpa_group = WPA_CIPHER_TKIP;
130 bss->wpa_group = WPA_CIPHER_CCMP;
132 if (bss->wpa && bss->ieee802_1x)
133 bss->ssid.security_policy = SECURITY_WPA;
135 bss->ssid.security_policy = SECURITY_WPA_PSK;
136 else if (bss->ieee802_1x) {
137 bss->ssid.security_policy = SECURITY_IEEE_802_1X;
138 bss->ssid.wep.default_len = bss->default_wep_key_len;
139 } else if (bss->ssid.wep.keys_set)
140 bss->ssid.security_policy = SECURITY_STATIC_WEP;
142 bss->ssid.security_policy = SECURITY_PLAINTEXT;
146 * Enable WPS by default, but require user interaction to actually use
147 * it. Only the internal Registrar is supported.
151 bss->ap_setup_locked = 1;
152 if (wpa_s->conf->config_methods)
153 bss->config_methods = os_strdup(wpa_s->conf->config_methods);
154 if (wpa_s->conf->device_type)
155 bss->device_type = os_strdup(wpa_s->conf->device_type);
156 #endif /* CONFIG_WPS */
162 int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
163 struct wpa_ssid *ssid)
165 struct wpa_driver_associate_params params;
166 struct hostapd_iface *hapd_iface;
167 struct hostapd_config *conf;
170 if (ssid->ssid == NULL || ssid->ssid_len == 0) {
171 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
175 wpa_supplicant_ap_deinit(wpa_s);
177 wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
178 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
180 os_memset(¶ms, 0, sizeof(params));
181 params.ssid = ssid->ssid;
182 params.ssid_len = ssid->ssid_len;
183 params.mode = ssid->mode;
184 params.freq = ssid->frequency;
186 if (wpa_drv_associate(wpa_s, ¶ms) < 0) {
187 wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
191 wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
192 if (hapd_iface == NULL)
194 hapd_iface->owner = wpa_s;
196 wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
198 wpa_supplicant_ap_deinit(wpa_s);
202 if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
203 wpa_printf(MSG_ERROR, "Failed to create AP configuration");
204 wpa_supplicant_ap_deinit(wpa_s);
208 hapd_iface->num_bss = conf->num_bss;
209 hapd_iface->bss = os_zalloc(conf->num_bss *
210 sizeof(struct hostapd_data *));
211 if (hapd_iface->bss == NULL) {
212 wpa_supplicant_ap_deinit(wpa_s);
216 for (i = 0; i < conf->num_bss; i++) {
218 hostapd_alloc_bss_data(hapd_iface, conf,
220 if (hapd_iface->bss[i] == NULL) {
221 wpa_supplicant_ap_deinit(wpa_s);
225 hapd_iface->bss[i]->msg_ctx = wpa_s;
228 os_memcpy(hapd_iface->bss[0]->own_addr, wpa_s->own_addr, ETH_ALEN);
229 hapd_iface->bss[0]->driver = wpa_s->driver;
230 hapd_iface->bss[0]->drv_priv = wpa_s->drv_priv;
232 if (hostapd_setup_interface(wpa_s->ap_iface)) {
233 wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
234 wpa_supplicant_ap_deinit(wpa_s);
238 wpa_s->current_ssid = ssid;
239 os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
240 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
246 void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
248 if (wpa_s->ap_iface == NULL)
251 hostapd_interface_deinit(wpa_s->ap_iface);
252 wpa_s->ap_iface = NULL;
256 void ap_tx_status(void *ctx, const u8 *addr,
257 const u8 *buf, size_t len, int ack)
260 struct wpa_supplicant *wpa_s = ctx;
261 hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
262 #endif /* NEED_AP_MLME */
266 void ap_rx_from_unknown_sta(void *ctx, const struct ieee80211_hdr *hdr,
270 struct wpa_supplicant *wpa_s = ctx;
271 u16 fc = le_to_host16(hdr->frame_control);
272 ieee802_11_rx_from_unknown(wpa_s->ap_iface->bss[0], hdr->addr2,
273 (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
274 (WLAN_FC_TODS | WLAN_FC_FROMDS));
275 #endif /* NEED_AP_MLME */
279 void ap_mgmt_rx(void *ctx, const u8 *buf, size_t len,
280 struct hostapd_frame_info *fi)
283 struct wpa_supplicant *wpa_s = ctx;
284 ieee802_11_mgmt(wpa_s->ap_iface->bss[0], buf, len, fi);
285 #endif /* NEED_AP_MLME */
289 void ap_mgmt_tx_cb(void *ctx, const u8 *buf, size_t len, u16 stype, int ok)
292 struct wpa_supplicant *wpa_s = ctx;
293 ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
294 #endif /* NEED_AP_MLME */
298 void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
299 const u8 *src_addr, const u8 *buf, size_t len)
301 hostapd_eapol_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
307 int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
309 return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0]);
313 int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
314 const char *pin, char *buf, size_t buflen)
316 int ret, ret_len = 0;
319 unsigned int rpin = wps_generate_pin();
320 ret_len = os_snprintf(buf, buflen, "%d", rpin);
324 ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], "any", pin, 0);
330 #endif /* CONFIG_WPS */
333 #ifdef CONFIG_CTRL_IFACE
335 int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
336 char *buf, size_t buflen)
338 if (wpa_s->ap_iface == NULL)
340 return hostapd_ctrl_iface_sta_first(wpa_s->ap_iface->bss[0],
345 int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
346 char *buf, size_t buflen)
348 if (wpa_s->ap_iface == NULL)
350 return hostapd_ctrl_iface_sta(wpa_s->ap_iface->bss[0], txtaddr,
355 int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
356 char *buf, size_t buflen)
358 if (wpa_s->ap_iface == NULL)
360 return hostapd_ctrl_iface_sta_next(wpa_s->ap_iface->bss[0], txtaddr,
365 int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
366 size_t buflen, int verbose)
368 char *pos = buf, *end = buf + buflen;
370 struct hostapd_bss_config *conf;
372 if (wpa_s->ap_iface == NULL)
375 conf = wpa_s->ap_iface->bss[0]->conf;
379 ret = os_snprintf(pos, end - pos,
380 "pairwise_cipher=%s\n"
383 wpa_cipher_txt(conf->rsn_pairwise),
384 wpa_cipher_txt(conf->wpa_group),
385 wpa_key_mgmt_txt(conf->wpa_key_mgmt,
387 if (ret < 0 || ret >= end - pos)
393 #endif /* CONFIG_CTRL_IFACE */