Move rest of the generic AP mode functionality into src/ap
[hostap-gosc2009.git] / wpa_supplicant / ap.c
bloba393b75292ddae62125958541ce431cf96434ac9
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
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
11 * license.
13 * See README and COPYING for more details.
16 #include "includes.h"
18 #include "common.h"
19 #include "ap/hostapd.h"
20 #include "ap/config.h"
21 #ifdef NEED_AP_MLME
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"
29 #include "wps/wps.h"
30 #include "config_ssid.h"
31 #include "config.h"
32 #include "wpa_supplicant_i.h"
33 #include "driver_i.h"
34 #include "ap.h"
37 struct hapd_interfaces {
38 size_t count;
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)
47 /* TODO */
48 return 0;
52 static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
53 struct wpa_ssid *ssid,
54 struct hostapd_config *conf)
56 struct hostapd_bss_config *bss = &conf->bss[0];
57 int pairwise;
59 conf->driver = wpa_s->driver;
61 os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
63 if (ssid->frequency == 0) {
64 /* default channel 11 */
65 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
66 conf->channel = 11;
67 } else if (ssid->frequency >= 2412 && ssid->frequency <= 2472) {
68 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
69 conf->channel = (ssid->frequency - 2407) / 5;
70 } else if ((ssid->frequency >= 5180 && ssid->frequency <= 5240) ||
71 (ssid->frequency >= 5745 && ssid->frequency <= 5825)) {
72 conf->hw_mode = HOSTAPD_MODE_IEEE80211A;
73 conf->channel = (ssid->frequency - 5000) / 5;
74 } else {
75 wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
76 ssid->frequency);
77 return -1;
80 /* TODO: enable HT if driver supports it;
81 * drop to 11b if driver does not support 11g */
83 if (ssid->ssid_len == 0) {
84 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
85 return -1;
87 os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
88 bss->ssid.ssid[ssid->ssid_len] = '\0';
89 bss->ssid.ssid_len = ssid->ssid_len;
90 bss->ssid.ssid_set = 1;
92 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
93 bss->wpa = ssid->proto;
94 bss->wpa_key_mgmt = ssid->key_mgmt;
95 bss->wpa_pairwise = ssid->pairwise_cipher;
96 if (ssid->passphrase) {
97 bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
98 } else if (ssid->psk_set) {
99 os_free(bss->ssid.wpa_psk);
100 bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
101 if (bss->ssid.wpa_psk == NULL)
102 return -1;
103 os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
104 bss->ssid.wpa_psk->group = 1;
107 /* Select group cipher based on the enabled pairwise cipher suites */
108 pairwise = 0;
109 if (bss->wpa & 1)
110 pairwise |= bss->wpa_pairwise;
111 if (bss->wpa & 2) {
112 if (bss->rsn_pairwise == 0)
113 bss->rsn_pairwise = bss->wpa_pairwise;
114 pairwise |= bss->rsn_pairwise;
116 if (pairwise & WPA_CIPHER_TKIP)
117 bss->wpa_group = WPA_CIPHER_TKIP;
118 else
119 bss->wpa_group = WPA_CIPHER_CCMP;
121 if (bss->wpa && bss->ieee802_1x)
122 bss->ssid.security_policy = SECURITY_WPA;
123 else if (bss->wpa)
124 bss->ssid.security_policy = SECURITY_WPA_PSK;
125 else if (bss->ieee802_1x) {
126 bss->ssid.security_policy = SECURITY_IEEE_802_1X;
127 bss->ssid.wep.default_len = bss->default_wep_key_len;
128 } else if (bss->ssid.wep.keys_set)
129 bss->ssid.security_policy = SECURITY_STATIC_WEP;
130 else
131 bss->ssid.security_policy = SECURITY_PLAINTEXT;
133 #ifdef CONFIG_WPS
135 * Enable WPS by default, but require user interaction to actually use
136 * it. Only the internal Registrar is supported.
138 bss->eap_server = 1;
139 bss->wps_state = 2;
140 bss->ap_setup_locked = 1;
141 if (wpa_s->conf->config_methods)
142 bss->config_methods = os_strdup(wpa_s->conf->config_methods);
143 if (wpa_s->conf->device_type)
144 bss->device_type = os_strdup(wpa_s->conf->device_type);
145 #endif /* CONFIG_WPS */
147 return 0;
151 int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
152 struct wpa_ssid *ssid)
154 struct wpa_driver_associate_params params;
155 struct hostapd_iface *hapd_iface;
156 struct hostapd_config *conf;
157 size_t i;
159 if (ssid->ssid == NULL || ssid->ssid_len == 0) {
160 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
161 return -1;
164 wpa_supplicant_ap_deinit(wpa_s);
166 wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
167 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
169 os_memset(&params, 0, sizeof(params));
170 params.ssid = ssid->ssid;
171 params.ssid_len = ssid->ssid_len;
172 params.mode = ssid->mode;
173 params.freq = ssid->frequency;
175 if (wpa_drv_associate(wpa_s, &params) < 0) {
176 wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
177 return -1;
180 wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
181 if (hapd_iface == NULL)
182 return -1;
183 hapd_iface->owner = wpa_s;
185 wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
186 if (conf == NULL) {
187 wpa_supplicant_ap_deinit(wpa_s);
188 return -1;
191 if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
192 wpa_printf(MSG_ERROR, "Failed to create AP configuration");
193 wpa_supplicant_ap_deinit(wpa_s);
194 return -1;
197 hapd_iface->num_bss = conf->num_bss;
198 hapd_iface->bss = os_zalloc(conf->num_bss *
199 sizeof(struct hostapd_data *));
200 if (hapd_iface->bss == NULL) {
201 wpa_supplicant_ap_deinit(wpa_s);
202 return -1;
205 for (i = 0; i < conf->num_bss; i++) {
206 hapd_iface->bss[i] =
207 hostapd_alloc_bss_data(hapd_iface, conf,
208 &conf->bss[i]);
209 if (hapd_iface->bss[i] == NULL) {
210 wpa_supplicant_ap_deinit(wpa_s);
211 return -1;
214 hapd_iface->bss[i]->msg_ctx = wpa_s;
217 os_memcpy(hapd_iface->bss[0]->own_addr, wpa_s->own_addr, ETH_ALEN);
218 hapd_iface->bss[0]->driver = wpa_s->driver;
219 hapd_iface->bss[0]->drv_priv = wpa_s->drv_priv;
221 if (hostapd_setup_interface(wpa_s->ap_iface)) {
222 wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
223 wpa_supplicant_ap_deinit(wpa_s);
224 return -1;
227 wpa_s->current_ssid = ssid;
228 os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
229 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
231 return 0;
235 void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
237 if (wpa_s->ap_iface == NULL)
238 return;
240 hostapd_interface_deinit(wpa_s->ap_iface);
241 wpa_s->ap_iface = NULL;
245 void ap_tx_status(void *ctx, const u8 *addr,
246 const u8 *buf, size_t len, int ack)
248 #ifdef NEED_AP_MLME
249 struct wpa_supplicant *wpa_s = ctx;
250 hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
251 #endif /* NEED_AP_MLME */
255 void ap_rx_from_unknown_sta(void *ctx, const struct ieee80211_hdr *hdr,
256 size_t len)
258 #ifdef NEED_AP_MLME
259 struct wpa_supplicant *wpa_s = ctx;
260 u16 fc = le_to_host16(hdr->frame_control);
261 ieee802_11_rx_from_unknown(wpa_s->ap_iface->bss[0], hdr->addr2,
262 (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
263 (WLAN_FC_TODS | WLAN_FC_FROMDS));
264 #endif /* NEED_AP_MLME */
268 void ap_mgmt_rx(void *ctx, const u8 *buf, size_t len,
269 struct hostapd_frame_info *fi)
271 #ifdef NEED_AP_MLME
272 struct wpa_supplicant *wpa_s = ctx;
273 ieee802_11_mgmt(wpa_s->ap_iface->bss[0], buf, len, fi);
274 #endif /* NEED_AP_MLME */
278 void ap_mgmt_tx_cb(void *ctx, const u8 *buf, size_t len, u16 stype, int ok)
280 #ifdef NEED_AP_MLME
281 struct wpa_supplicant *wpa_s = ctx;
282 ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
283 #endif /* NEED_AP_MLME */
287 void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
288 const u8 *src_addr, const u8 *buf, size_t len)
290 hostapd_eapol_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
294 #ifdef CONFIG_WPS
296 int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
298 return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0]);
302 int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
303 const char *pin, char *buf, size_t buflen)
305 int ret, ret_len = 0;
307 if (pin == NULL) {
308 unsigned int rpin = wps_generate_pin();
309 ret_len = os_snprintf(buf, buflen, "%d", rpin);
310 pin = buf;
313 ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], "any", pin, 0);
314 if (ret)
315 return -1;
316 return ret_len;
319 #endif /* CONFIG_WPS */
322 #ifdef CONFIG_CTRL_IFACE
324 int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
325 char *buf, size_t buflen)
327 if (wpa_s->ap_iface == NULL)
328 return -1;
329 return hostapd_ctrl_iface_sta_first(wpa_s->ap_iface->bss[0],
330 buf, buflen);
334 int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
335 char *buf, size_t buflen)
337 if (wpa_s->ap_iface == NULL)
338 return -1;
339 return hostapd_ctrl_iface_sta(wpa_s->ap_iface->bss[0], txtaddr,
340 buf, buflen);
344 int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
345 char *buf, size_t buflen)
347 if (wpa_s->ap_iface == NULL)
348 return -1;
349 return hostapd_ctrl_iface_sta_next(wpa_s->ap_iface->bss[0], txtaddr,
350 buf, buflen);
354 int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
355 size_t buflen, int verbose)
357 char *pos = buf, *end = buf + buflen;
358 int ret;
359 struct hostapd_bss_config *conf;
361 if (wpa_s->ap_iface == NULL)
362 return -1;
364 conf = wpa_s->ap_iface->bss[0]->conf;
365 if (conf->wpa == 0)
366 return 0;
368 ret = os_snprintf(pos, end - pos,
369 "pairwise_cipher=%s\n"
370 "group_cipher=%s\n"
371 "key_mgmt=%s\n",
372 wpa_cipher_txt(conf->rsn_pairwise),
373 wpa_cipher_txt(conf->wpa_group),
374 wpa_key_mgmt_txt(conf->wpa_key_mgmt,
375 conf->wpa));
376 if (ret < 0 || ret >= end - pos)
377 return pos - buf;
378 pos += ret;
379 return pos - buf;
382 #endif /* CONFIG_CTRL_IFACE */