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"
22 #include "../hostapd/ieee802_11.h"
23 #endif /* NEED_MLME */
24 #include "eap_common/eap_defs.h"
25 #include "eap_server/eap_methods.h"
26 #include "eap_common/eap_wsc_common.h"
27 #include "config_ssid.h"
28 #include "wpa_supplicant_i.h"
33 int hostapd_for_each_interface(int (*cb
)(struct hostapd_iface
*iface
,
34 void *ctx
), void *ctx
)
41 int hostapd_ctrl_iface_init(struct hostapd_data
*hapd
)
47 void hostapd_ctrl_iface_deinit(struct hostapd_data
*hapd
)
52 struct ap_driver_data
{
53 struct hostapd_data
*hapd
;
57 static void * ap_driver_init(struct hostapd_data
*hapd
,
58 struct wpa_init_params
*params
)
60 struct ap_driver_data
*drv
;
61 struct wpa_supplicant
*wpa_s
= hapd
->iface
->owner
;
63 drv
= os_zalloc(sizeof(struct ap_driver_data
));
65 wpa_printf(MSG_ERROR
, "Could not allocate memory for AP "
70 os_memcpy(hapd
->own_addr
, wpa_s
->own_addr
, ETH_ALEN
);
76 static void ap_driver_deinit(void *priv
)
78 struct ap_driver_data
*drv
= priv
;
84 static int ap_driver_send_ether(void *priv
, const u8
*dst
, const u8
*src
,
85 u16 proto
, const u8
*data
, size_t data_len
)
87 wpa_printf(MSG_DEBUG
, "AP TODO: %s", __func__
);
92 static int ap_driver_set_key(const char *iface
, void *priv
, wpa_alg alg
,
93 const u8
*addr
, int key_idx
, int set_tx
,
94 const u8
*seq
, size_t seq_len
, const u8
*key
,
97 struct ap_driver_data
*drv
= priv
;
98 struct wpa_supplicant
*wpa_s
= drv
->hapd
->iface
->owner
;
99 return wpa_drv_set_key(wpa_s
, alg
, addr
, key_idx
, set_tx
, seq
, seq_len
,
104 static int ap_driver_get_seqnum(const char *iface
, void *priv
, const u8
*addr
,
107 wpa_printf(MSG_DEBUG
, "AP TODO: %s", __func__
);
112 static int ap_driver_flush(void *priv
)
114 wpa_printf(MSG_DEBUG
, "AP TODO: %s", __func__
);
119 static int ap_driver_read_sta_data(void *priv
,
120 struct hostap_sta_driver_data
*data
,
123 wpa_printf(MSG_DEBUG
, "AP TODO: %s", __func__
);
128 static int ap_driver_sta_set_flags(void *priv
, const u8
*addr
, int total_flags
,
129 int flags_or
, int flags_and
)
131 wpa_printf(MSG_DEBUG
, "AP TODO: %s", __func__
);
136 static int ap_driver_sta_deauth(void *priv
, const u8
*own_addr
, const u8
*addr
,
139 wpa_printf(MSG_DEBUG
, "AP TODO: %s", __func__
);
144 static int ap_driver_sta_disassoc(void *priv
, const u8
*own_addr
,
145 const u8
*addr
, int reason
)
147 wpa_printf(MSG_DEBUG
, "AP TODO: %s", __func__
);
152 static int ap_driver_sta_remove(void *priv
, const u8
*addr
)
154 struct ap_driver_data
*drv
= priv
;
155 struct wpa_supplicant
*wpa_s
= drv
->hapd
->iface
->owner
;
156 return wpa_drv_sta_remove(wpa_s
, addr
);
160 static int ap_driver_send_mlme(void *priv
, const u8
*data
, size_t len
)
162 struct ap_driver_data
*drv
= priv
;
163 struct wpa_supplicant
*wpa_s
= drv
->hapd
->iface
->owner
;
164 return wpa_drv_send_mlme(wpa_s
, data
, len
);
168 static int ap_driver_sta_add(const char *ifname
, void *priv
,
169 struct hostapd_sta_add_params
*params
)
171 struct ap_driver_data
*drv
= priv
;
172 struct wpa_supplicant
*wpa_s
= drv
->hapd
->iface
->owner
;
173 return wpa_drv_sta_add(wpa_s
, params
);
177 static int ap_driver_get_inact_sec(void *priv
, const u8
*addr
)
179 wpa_printf(MSG_DEBUG
, "AP TODO: %s", __func__
);
184 static int ap_driver_set_freq(void *priv
, struct hostapd_freq_params
*freq
)
186 wpa_printf(MSG_DEBUG
, "AP TODO: %s", __func__
);
191 static int ap_driver_set_beacon(const char *iface
, void *priv
,
192 const u8
*head
, size_t head_len
,
193 const u8
*tail
, size_t tail_len
,
196 struct ap_driver_data
*drv
= priv
;
197 struct wpa_supplicant
*wpa_s
= drv
->hapd
->iface
->owner
;
198 return wpa_drv_set_beacon(wpa_s
, head
, head_len
, tail
, tail_len
,
203 static int ap_driver_set_beacon_int(void *priv
, int value
)
205 struct ap_driver_data
*drv
= priv
;
206 struct wpa_supplicant
*wpa_s
= drv
->hapd
->iface
->owner
;
207 return wpa_drv_set_beacon_int(wpa_s
, value
);
211 static int ap_driver_set_cts_protect(void *priv
, int value
)
213 wpa_printf(MSG_DEBUG
, "AP TODO: %s", __func__
);
218 static int ap_driver_set_preamble(void *priv
, int value
)
220 wpa_printf(MSG_DEBUG
, "AP TODO: %s", __func__
);
225 static int ap_driver_set_short_slot_time(void *priv
, int value
)
227 wpa_printf(MSG_DEBUG
, "AP TODO: %s", __func__
);
232 static int ap_driver_set_tx_queue_params(void *priv
, int queue
, int aifs
,
233 int cw_min
, int cw_max
,
236 wpa_printf(MSG_DEBUG
, "AP TODO: %s", __func__
);
241 static struct hostapd_hw_modes
*ap_driver_get_hw_feature_data(void *priv
,
245 struct ap_driver_data
*drv
= priv
;
246 struct wpa_supplicant
*wpa_s
= drv
->hapd
->iface
->owner
;
247 return wpa_drv_get_hw_feature_data(wpa_s
, num_modes
, flags
);
251 struct wpa_driver_ops ap_driver_ops
=
253 .name
= "wpa_supplicant",
254 .hapd_init
= ap_driver_init
,
255 .hapd_deinit
= ap_driver_deinit
,
256 .send_ether
= ap_driver_send_ether
,
257 .hapd_set_key
= ap_driver_set_key
,
258 .get_seqnum
= ap_driver_get_seqnum
,
259 .flush
= ap_driver_flush
,
260 .read_sta_data
= ap_driver_read_sta_data
,
261 .sta_set_flags
= ap_driver_sta_set_flags
,
262 .sta_deauth
= ap_driver_sta_deauth
,
263 .sta_disassoc
= ap_driver_sta_disassoc
,
264 .sta_remove
= ap_driver_sta_remove
,
265 .send_mlme
= ap_driver_send_mlme
,
266 .sta_add
= ap_driver_sta_add
,
267 .get_inact_sec
= ap_driver_get_inact_sec
,
268 .set_freq
= ap_driver_set_freq
,
269 .hapd_set_beacon
= ap_driver_set_beacon
,
270 .set_beacon_int
= ap_driver_set_beacon_int
,
271 .set_cts_protect
= ap_driver_set_cts_protect
,
272 .set_preamble
= ap_driver_set_preamble
,
273 .set_short_slot_time
= ap_driver_set_short_slot_time
,
274 .set_tx_queue_params
= ap_driver_set_tx_queue_params
,
275 .get_hw_feature_data
= ap_driver_get_hw_feature_data
,
279 extern struct wpa_driver_ops
*wpa_drivers
[];
281 static int wpa_supplicant_conf_ap(struct wpa_supplicant
*wpa_s
,
282 struct wpa_ssid
*ssid
,
283 struct hostapd_config
*conf
)
285 struct hostapd_bss_config
*bss
= &conf
->bss
[0];
288 for (j
= 0; wpa_drivers
[j
]; j
++) {
289 if (os_strcmp("wpa_supplicant", wpa_drivers
[j
]->name
) == 0) {
290 conf
->driver
= wpa_drivers
[j
];
294 if (conf
->driver
== NULL
) {
295 wpa_printf(MSG_ERROR
, "No AP driver ops found");
299 os_strlcpy(bss
->iface
, wpa_s
->ifname
, sizeof(bss
->iface
));
301 if (ssid
->frequency
== 0) {
302 /* default channel 11 */
303 conf
->hw_mode
= HOSTAPD_MODE_IEEE80211G
;
305 } else if (ssid
->frequency
>= 2412 && ssid
->frequency
<= 2472) {
306 conf
->hw_mode
= HOSTAPD_MODE_IEEE80211G
;
307 conf
->channel
= (ssid
->frequency
- 2407) / 5;
308 } else if ((ssid
->frequency
>= 5180 && ssid
->frequency
<= 5240) ||
309 (ssid
->frequency
>= 5745 && ssid
->frequency
<= 5825)) {
310 conf
->hw_mode
= HOSTAPD_MODE_IEEE80211A
;
311 conf
->channel
= (ssid
->frequency
- 5000) / 5;
313 wpa_printf(MSG_ERROR
, "Unsupported AP mode frequency: %d MHz",
318 /* TODO: enable HT if driver supports it;
319 * drop to 11b if driver does not support 11g */
321 if (ssid
->ssid_len
== 0) {
322 wpa_printf(MSG_ERROR
, "No SSID configured for AP mode");
325 os_memcpy(bss
->ssid
.ssid
, ssid
->ssid
, ssid
->ssid_len
);
326 bss
->ssid
.ssid
[ssid
->ssid_len
] = '\0';
327 bss
->ssid
.ssid_len
= ssid
->ssid_len
;
328 bss
->ssid
.ssid_set
= 1;
330 if (wpa_key_mgmt_wpa_psk(ssid
->key_mgmt
))
331 bss
->wpa
= ssid
->proto
;
332 bss
->wpa_key_mgmt
= ssid
->key_mgmt
;
333 bss
->wpa_pairwise
= ssid
->pairwise_cipher
;
334 if (ssid
->passphrase
) {
335 bss
->ssid
.wpa_passphrase
= os_strdup(ssid
->passphrase
);
336 if (hostapd_setup_wpa_psk(bss
))
338 } else if (ssid
->psk_set
) {
339 os_free(bss
->ssid
.wpa_psk
);
340 bss
->ssid
.wpa_psk
= os_zalloc(sizeof(struct hostapd_wpa_psk
));
341 if (bss
->ssid
.wpa_psk
== NULL
)
343 os_memcpy(bss
->ssid
.wpa_psk
->psk
, ssid
->psk
, PMK_LEN
);
344 bss
->ssid
.wpa_psk
->group
= 1;
351 int wpa_supplicant_create_ap(struct wpa_supplicant
*wpa_s
,
352 struct wpa_ssid
*ssid
)
354 struct wpa_driver_associate_params params
;
355 struct hostapd_iface
*hapd_iface
;
356 struct hostapd_config
*conf
;
359 if (ssid
->ssid
== NULL
|| ssid
->ssid_len
== 0) {
360 wpa_printf(MSG_ERROR
, "No SSID configured for AP mode");
364 wpa_supplicant_ap_deinit(wpa_s
);
366 wpa_printf(MSG_DEBUG
, "Setting up AP (SSID='%s')",
367 wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
));
369 os_memset(¶ms
, 0, sizeof(params
));
370 params
.ssid
= ssid
->ssid
;
371 params
.ssid_len
= ssid
->ssid_len
;
372 params
.mode
= ssid
->mode
;
373 params
.freq
= ssid
->frequency
;
375 if (wpa_drv_associate(wpa_s
, ¶ms
) < 0) {
376 wpa_msg(wpa_s
, MSG_INFO
, "Failed to start AP functionality");
380 wpa_s
->ap_iface
= hapd_iface
= os_zalloc(sizeof(*wpa_s
->ap_iface
));
381 if (hapd_iface
== NULL
)
383 hapd_iface
->owner
= wpa_s
;
385 wpa_s
->ap_iface
->conf
= conf
= hostapd_config_defaults();
387 wpa_supplicant_ap_deinit(wpa_s
);
391 if (wpa_supplicant_conf_ap(wpa_s
, ssid
, conf
)) {
392 wpa_printf(MSG_ERROR
, "Failed to create AP configuration");
393 wpa_supplicant_ap_deinit(wpa_s
);
397 hapd_iface
->num_bss
= conf
->num_bss
;
398 hapd_iface
->bss
= os_zalloc(conf
->num_bss
*
399 sizeof(struct hostapd_data
*));
400 if (hapd_iface
->bss
== NULL
) {
401 wpa_supplicant_ap_deinit(wpa_s
);
405 for (i
= 0; i
< conf
->num_bss
; i
++) {
407 hostapd_alloc_bss_data(hapd_iface
, conf
,
409 if (hapd_iface
->bss
[i
] == NULL
) {
410 wpa_supplicant_ap_deinit(wpa_s
);
415 if (hostapd_setup_interface(wpa_s
->ap_iface
)) {
416 wpa_printf(MSG_ERROR
, "Failed to initialize AP interface");
417 wpa_supplicant_ap_deinit(wpa_s
);
425 void wpa_supplicant_ap_deinit(struct wpa_supplicant
*wpa_s
)
427 if (wpa_s
->ap_iface
== NULL
)
430 hostapd_interface_deinit(wpa_s
->ap_iface
);
431 wpa_s
->ap_iface
= NULL
;
435 void ap_tx_status(void *ctx
, const u8
*addr
,
436 const u8
*buf
, size_t len
, int ack
)
438 struct wpa_supplicant
*wpa_s
= ctx
;
439 hostapd_tx_status(wpa_s
->ap_iface
->bss
[0], addr
, buf
, len
, ack
);
443 void ap_rx_from_unknown_sta(void *ctx
, const u8
*addr
)
445 struct wpa_supplicant
*wpa_s
= ctx
;
446 ap_rx_from_unknown_sta(wpa_s
->ap_iface
->bss
[0], addr
);
451 void ap_mgmt_rx(void *ctx
, u8
*buf
, size_t len
, u16 stype
,
452 struct hostapd_frame_info
*fi
)
454 struct wpa_supplicant
*wpa_s
= ctx
;
455 ieee802_11_mgmt(wpa_s
->ap_iface
->bss
[0], buf
, len
, stype
, fi
);
459 void ap_mgmt_tx_cb(void *ctx
, u8
*buf
, size_t len
, u16 stype
, int ok
)
461 struct wpa_supplicant
*wpa_s
= ctx
;
462 ieee802_11_mgmt_cb(wpa_s
->ap_iface
->bss
[0], buf
, len
, stype
, ok
);
464 #endif /* NEED_MLME */