]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/ap.c
Clear current_bss pointer on disassociation/deauthentication
[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"
58c26600 19#include "common/ieee802_11_defs.h"
1057d78e 20#include "ap/hostapd.h"
6226e38d 21#include "ap/ap_config.h"
fe6bdb77 22#ifdef NEED_AP_MLME
1057d78e 23#include "ap/ieee802_11.h"
fe6bdb77 24#endif /* NEED_AP_MLME */
a8e0505b 25#include "ap/ieee802_1x.h"
363b9e60 26#include "ap/wps_hostapd.h"
0e2d35c6 27#include "ap/ctrl_iface_ap.h"
f1a48710
JM
28#include "eap_common/eap_defs.h"
29#include "eap_server/eap_methods.h"
30#include "eap_common/eap_wsc_common.h"
3ec97afe 31#include "wps/wps.h"
1f1b62a0 32#include "config_ssid.h"
094393b1 33#include "config.h"
1f1b62a0 34#include "wpa_supplicant_i.h"
2d5b792d
JM
35#include "driver_i.h"
36#include "ap.h"
f1a48710
JM
37
38
07f117ed
JM
39static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
40 struct wpa_ssid *ssid,
41 struct hostapd_config *conf)
42{
43 struct hostapd_bss_config *bss = &conf->bss[0];
a911a6e6 44 int pairwise;
c5121837 45
a911a6e6 46 conf->driver = wpa_s->driver;
07f117ed
JM
47
48 os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
49
50 if (ssid->frequency == 0) {
51 /* default channel 11 */
52 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
53 conf->channel = 11;
54 } else if (ssid->frequency >= 2412 && ssid->frequency <= 2472) {
55 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
56 conf->channel = (ssid->frequency - 2407) / 5;
57 } else if ((ssid->frequency >= 5180 && ssid->frequency <= 5240) ||
58 (ssid->frequency >= 5745 && ssid->frequency <= 5825)) {
b615a25e 59 conf->hw_mode = HOSTAPD_MODE_IEEE80211A;
07f117ed
JM
60 conf->channel = (ssid->frequency - 5000) / 5;
61 } else {
62 wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
63 ssid->frequency);
64 return -1;
65 }
66
67 /* TODO: enable HT if driver supports it;
68 * drop to 11b if driver does not support 11g */
69
70 if (ssid->ssid_len == 0) {
71 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
72 return -1;
73 }
74 os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
75 bss->ssid.ssid[ssid->ssid_len] = '\0';
76 bss->ssid.ssid_len = ssid->ssid_len;
77 bss->ssid.ssid_set = 1;
78
79 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
80 bss->wpa = ssid->proto;
81 bss->wpa_key_mgmt = ssid->key_mgmt;
82 bss->wpa_pairwise = ssid->pairwise_cipher;
83 if (ssid->passphrase) {
84 bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
07f117ed
JM
85 } else if (ssid->psk_set) {
86 os_free(bss->ssid.wpa_psk);
87 bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
88 if (bss->ssid.wpa_psk == NULL)
89 return -1;
90 os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
91 bss->ssid.wpa_psk->group = 1;
92 }
93
07d9a552
JM
94 /* Select group cipher based on the enabled pairwise cipher suites */
95 pairwise = 0;
96 if (bss->wpa & 1)
97 pairwise |= bss->wpa_pairwise;
98 if (bss->wpa & 2) {
99 if (bss->rsn_pairwise == 0)
100 bss->rsn_pairwise = bss->wpa_pairwise;
101 pairwise |= bss->rsn_pairwise;
102 }
103 if (pairwise & WPA_CIPHER_TKIP)
104 bss->wpa_group = WPA_CIPHER_TKIP;
105 else
106 bss->wpa_group = WPA_CIPHER_CCMP;
107
108 if (bss->wpa && bss->ieee802_1x)
109 bss->ssid.security_policy = SECURITY_WPA;
110 else if (bss->wpa)
111 bss->ssid.security_policy = SECURITY_WPA_PSK;
112 else if (bss->ieee802_1x) {
113 bss->ssid.security_policy = SECURITY_IEEE_802_1X;
114 bss->ssid.wep.default_len = bss->default_wep_key_len;
115 } else if (bss->ssid.wep.keys_set)
116 bss->ssid.security_policy = SECURITY_STATIC_WEP;
117 else
118 bss->ssid.security_policy = SECURITY_PLAINTEXT;
119
3ec97afe
JM
120#ifdef CONFIG_WPS
121 /*
122 * Enable WPS by default, but require user interaction to actually use
123 * it. Only the internal Registrar is supported.
124 */
125 bss->eap_server = 1;
126 bss->wps_state = 2;
127 bss->ap_setup_locked = 1;
094393b1
JM
128 if (wpa_s->conf->config_methods)
129 bss->config_methods = os_strdup(wpa_s->conf->config_methods);
130 if (wpa_s->conf->device_type)
131 bss->device_type = os_strdup(wpa_s->conf->device_type);
3ec97afe
JM
132#endif /* CONFIG_WPS */
133
07f117ed
JM
134 return 0;
135}
136
137
2d5b792d
JM
138int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
139 struct wpa_ssid *ssid)
140{
141 struct wpa_driver_associate_params params;
142 struct hostapd_iface *hapd_iface;
143 struct hostapd_config *conf;
144 size_t i;
f1a48710 145
2d5b792d
JM
146 if (ssid->ssid == NULL || ssid->ssid_len == 0) {
147 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
148 return -1;
f1a48710
JM
149 }
150
2d5b792d 151 wpa_supplicant_ap_deinit(wpa_s);
d2440ba0
JM
152
153 wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
154 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
155
156 os_memset(&params, 0, sizeof(params));
157 params.ssid = ssid->ssid;
158 params.ssid_len = ssid->ssid_len;
d7dcba70
JM
159 switch (ssid->mode) {
160 case WPAS_MODE_INFRA:
161 params.mode = IEEE80211_MODE_INFRA;
162 break;
163 case WPAS_MODE_IBSS:
164 params.mode = IEEE80211_MODE_IBSS;
165 break;
166 case WPAS_MODE_AP:
167 params.mode = IEEE80211_MODE_AP;
168 break;
169 }
d2440ba0
JM
170 params.freq = ssid->frequency;
171
172 if (wpa_drv_associate(wpa_s, &params) < 0) {
173 wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
174 return -1;
175 }
176
2d5b792d
JM
177 wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
178 if (hapd_iface == NULL)
179 return -1;
0f2b2c19 180 hapd_iface->owner = wpa_s;
1f1b62a0 181
2d5b792d
JM
182 wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
183 if (conf == NULL) {
184 wpa_supplicant_ap_deinit(wpa_s);
185 return -1;
186 }
1f1b62a0 187
07f117ed
JM
188 if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
189 wpa_printf(MSG_ERROR, "Failed to create AP configuration");
190 wpa_supplicant_ap_deinit(wpa_s);
191 return -1;
192 }
193
2d5b792d
JM
194 hapd_iface->num_bss = conf->num_bss;
195 hapd_iface->bss = os_zalloc(conf->num_bss *
196 sizeof(struct hostapd_data *));
197 if (hapd_iface->bss == NULL) {
198 wpa_supplicant_ap_deinit(wpa_s);
199 return -1;
200 }
1f1b62a0 201
2d5b792d
JM
202 for (i = 0; i < conf->num_bss; i++) {
203 hapd_iface->bss[i] =
204 hostapd_alloc_bss_data(hapd_iface, conf,
205 &conf->bss[i]);
206 if (hapd_iface->bss[i] == NULL) {
207 wpa_supplicant_ap_deinit(wpa_s);
208 return -1;
209 }
4f760fcc
JM
210
211 hapd_iface->bss[i]->msg_ctx = wpa_s;
2d5b792d
JM
212 }
213
a911a6e6
JM
214 os_memcpy(hapd_iface->bss[0]->own_addr, wpa_s->own_addr, ETH_ALEN);
215 hapd_iface->bss[0]->driver = wpa_s->driver;
216 hapd_iface->bss[0]->drv_priv = wpa_s->drv_priv;
217
218 if (hostapd_setup_interface(wpa_s->ap_iface)) {
2d5b792d
JM
219 wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
220 wpa_supplicant_ap_deinit(wpa_s);
221 return -1;
1f1b62a0
JM
222 }
223
b2cc8056 224 wpa_s->current_ssid = ssid;
43fb5297 225 os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
137fb724
WS
226 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
227
2d5b792d
JM
228 return 0;
229}
230
231
232void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
233{
234 if (wpa_s->ap_iface == NULL)
235 return;
236
237 hostapd_interface_deinit(wpa_s->ap_iface);
f7c47833 238 hostapd_interface_free(wpa_s->ap_iface);
2d5b792d 239 wpa_s->ap_iface = NULL;
1f1b62a0 240}
0915d02c
JM
241
242
243void ap_tx_status(void *ctx, const u8 *addr,
244 const u8 *buf, size_t len, int ack)
245{
f8b1f695 246#ifdef NEED_AP_MLME
0915d02c
JM
247 struct wpa_supplicant *wpa_s = ctx;
248 hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
f8b1f695 249#endif /* NEED_AP_MLME */
0915d02c
JM
250}
251
252
fdbe50ed 253void ap_rx_from_unknown_sta(void *ctx, const u8 *frame, size_t len)
0915d02c 254{
f8b1f695 255#ifdef NEED_AP_MLME
0915d02c 256 struct wpa_supplicant *wpa_s = ctx;
fdbe50ed
JM
257 const struct ieee80211_hdr *hdr =
258 (const struct ieee80211_hdr *) frame;
fbbfcbac
FF
259 u16 fc = le_to_host16(hdr->frame_control);
260 ieee802_11_rx_from_unknown(wpa_s->ap_iface->bss[0], hdr->addr2,
261 (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
262 (WLAN_FC_TODS | WLAN_FC_FROMDS));
f8b1f695 263#endif /* NEED_AP_MLME */
0915d02c
JM
264}
265
266
2a8b7416 267void ap_mgmt_rx(void *ctx, struct rx_mgmt *rx_mgmt)
0915d02c 268{
f8b1f695 269#ifdef NEED_AP_MLME
0915d02c 270 struct wpa_supplicant *wpa_s = ctx;
2a8b7416
JM
271 struct hostapd_frame_info fi;
272 os_memset(&fi, 0, sizeof(fi));
273 fi.datarate = rx_mgmt->datarate;
274 fi.ssi_signal = rx_mgmt->ssi_signal;
275 ieee802_11_mgmt(wpa_s->ap_iface->bss[0], rx_mgmt->frame,
276 rx_mgmt->frame_len, &fi);
f8b1f695 277#endif /* NEED_AP_MLME */
0915d02c
JM
278}
279
280
f8b1f695 281void ap_mgmt_tx_cb(void *ctx, const u8 *buf, size_t len, u16 stype, int ok)
0915d02c 282{
f8b1f695 283#ifdef NEED_AP_MLME
0915d02c
JM
284 struct wpa_supplicant *wpa_s = ctx;
285 ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
fe6bdb77 286#endif /* NEED_AP_MLME */
f8b1f695 287}
db149ac9
JM
288
289
290void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
291 const u8 *src_addr, const u8 *buf, size_t len)
292{
a8e0505b 293 ieee802_1x_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
db149ac9 294}
3ec97afe
JM
295
296
297#ifdef CONFIG_WPS
298
299int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
300{
301 return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0]);
302}
303
304
305int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
306 const char *pin, char *buf, size_t buflen)
307{
308 int ret, ret_len = 0;
309
310 if (pin == NULL) {
311 unsigned int rpin = wps_generate_pin();
312 ret_len = os_snprintf(buf, buflen, "%d", rpin);
313 pin = buf;
314 }
315
077a781f 316 ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], "any", pin, 0);
3ec97afe
JM
317 if (ret)
318 return -1;
319 return ret_len;
320}
321
322#endif /* CONFIG_WPS */
e653b622
JM
323
324
35deb646
JM
325#ifdef CONFIG_CTRL_IFACE
326
e653b622
JM
327int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
328 char *buf, size_t buflen)
329{
330 if (wpa_s->ap_iface == NULL)
331 return -1;
332 return hostapd_ctrl_iface_sta_first(wpa_s->ap_iface->bss[0],
333 buf, buflen);
334}
335
336
337int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
338 char *buf, size_t buflen)
339{
340 if (wpa_s->ap_iface == NULL)
341 return -1;
342 return hostapd_ctrl_iface_sta(wpa_s->ap_iface->bss[0], txtaddr,
343 buf, buflen);
344}
345
346
347int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
348 char *buf, size_t buflen)
349{
350 if (wpa_s->ap_iface == NULL)
351 return -1;
352 return hostapd_ctrl_iface_sta_next(wpa_s->ap_iface->bss[0], txtaddr,
353 buf, buflen);
354}
35deb646 355
43fb5297
JM
356
357int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
358 size_t buflen, int verbose)
359{
360 char *pos = buf, *end = buf + buflen;
361 int ret;
362 struct hostapd_bss_config *conf;
363
364 if (wpa_s->ap_iface == NULL)
365 return -1;
366
367 conf = wpa_s->ap_iface->bss[0]->conf;
368 if (conf->wpa == 0)
369 return 0;
370
371 ret = os_snprintf(pos, end - pos,
372 "pairwise_cipher=%s\n"
373 "group_cipher=%s\n"
374 "key_mgmt=%s\n",
375 wpa_cipher_txt(conf->rsn_pairwise),
376 wpa_cipher_txt(conf->wpa_group),
377 wpa_key_mgmt_txt(conf->wpa_key_mgmt,
378 conf->wpa));
379 if (ret < 0 || ret >= end - pos)
380 return pos - buf;
381 pos += ret;
382 return pos - buf;
383}
384
35deb646 385#endif /* CONFIG_CTRL_IFACE */