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_AP_MLME */
24 #include "../hostapd/wps_hostapd.h"
25 #include "../hostapd/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"
31 #include "wpa_supplicant_i.h"
36 int hostapd_for_each_interface(int (*cb)(struct hostapd_iface *iface,
37 void *ctx), void *ctx)
44 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
50 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
55 struct ap_driver_data {
56 struct hostapd_data *hapd;
60 static void * ap_driver_init(struct hostapd_data *hapd,
61 struct wpa_init_params *params)
63 struct ap_driver_data *drv;
64 struct wpa_supplicant *wpa_s = hapd->iface->owner;
66 drv = os_zalloc(sizeof(struct ap_driver_data));
68 wpa_printf(MSG_ERROR, "Could not allocate memory for AP "
73 os_memcpy(hapd->own_addr, wpa_s->own_addr, ETH_ALEN);
79 static void ap_driver_deinit(void *priv)
81 struct ap_driver_data *drv = priv;
87 static int ap_driver_send_ether(void *priv, const u8 *dst, const u8 *src,
88 u16 proto, const u8 *data, size_t data_len)
90 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
95 static int ap_driver_set_key(const char *iface, void *priv, wpa_alg alg,
96 const u8 *addr, int key_idx, int set_tx,
97 const u8 *seq, size_t seq_len, const u8 *key,
100 struct ap_driver_data *drv = priv;
101 struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
102 return wpa_drv_set_key(wpa_s, alg, addr, key_idx, set_tx, seq, seq_len,
107 static int ap_driver_get_seqnum(const char *iface, void *priv, const u8 *addr,
110 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
115 static int ap_driver_flush(void *priv)
117 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
122 static int ap_driver_read_sta_data(void *priv,
123 struct hostap_sta_driver_data *data,
126 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
131 static int ap_driver_sta_set_flags(void *priv, const u8 *addr, int total_flags,
132 int flags_or, int flags_and)
134 struct ap_driver_data *drv = priv;
135 struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
136 return wpa_drv_sta_set_flags(wpa_s, addr, total_flags, flags_or,
141 static int ap_driver_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
144 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
149 static int ap_driver_sta_disassoc(void *priv, const u8 *own_addr,
150 const u8 *addr, int reason)
152 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
157 static int ap_driver_sta_remove(void *priv, const u8 *addr)
159 struct ap_driver_data *drv = priv;
160 struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
161 return wpa_drv_sta_remove(wpa_s, addr);
165 static int ap_driver_send_mlme(void *priv, const u8 *data, size_t len)
167 struct ap_driver_data *drv = priv;
168 struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
169 return wpa_drv_send_mlme(wpa_s, data, len);
173 static int ap_driver_sta_add(const char *ifname, void *priv,
174 struct hostapd_sta_add_params *params)
176 struct ap_driver_data *drv = priv;
177 struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
178 return wpa_drv_sta_add(wpa_s, params);
182 static int ap_driver_get_inact_sec(void *priv, const u8 *addr)
184 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
189 static int ap_driver_set_freq(void *priv, struct hostapd_freq_params *freq)
191 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
196 static int ap_driver_set_beacon(const char *iface, void *priv,
197 const u8 *head, size_t head_len,
198 const u8 *tail, size_t tail_len,
201 struct ap_driver_data *drv = priv;
202 struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
203 return wpa_drv_set_beacon(wpa_s, head, head_len, tail, tail_len,
208 static int ap_driver_set_beacon_int(void *priv, int value)
210 struct ap_driver_data *drv = priv;
211 struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
212 return wpa_drv_set_beacon_int(wpa_s, value);
216 static int ap_driver_set_cts_protect(void *priv, int value)
218 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
223 static int ap_driver_set_preamble(void *priv, int value)
225 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
230 static int ap_driver_set_short_slot_time(void *priv, int value)
232 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
237 static int ap_driver_set_tx_queue_params(void *priv, int queue, int aifs,
238 int cw_min, int cw_max,
241 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
246 static struct hostapd_hw_modes *ap_driver_get_hw_feature_data(void *priv,
250 struct ap_driver_data *drv = priv;
251 struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
252 return wpa_drv_get_hw_feature_data(wpa_s, num_modes, flags);
256 static int ap_driver_hapd_send_eapol(void *priv, const u8 *addr,
257 const u8 *data, size_t data_len,
258 int encrypt, const u8 *own_addr)
260 struct ap_driver_data *drv = priv;
261 struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
262 return wpa_drv_hapd_send_eapol(wpa_s, addr, data, data_len, encrypt,
267 struct wpa_driver_ops ap_driver_ops =
269 .name = "wpa_supplicant",
270 .hapd_init = ap_driver_init,
271 .hapd_deinit = ap_driver_deinit,
272 .send_ether = ap_driver_send_ether,
273 .hapd_set_key = ap_driver_set_key,
274 .get_seqnum = ap_driver_get_seqnum,
275 .flush = ap_driver_flush,
276 .read_sta_data = ap_driver_read_sta_data,
277 .sta_set_flags = ap_driver_sta_set_flags,
278 .sta_deauth = ap_driver_sta_deauth,
279 .sta_disassoc = ap_driver_sta_disassoc,
280 .sta_remove = ap_driver_sta_remove,
281 .send_mlme = ap_driver_send_mlme,
282 .sta_add = ap_driver_sta_add,
283 .get_inact_sec = ap_driver_get_inact_sec,
284 .set_freq = ap_driver_set_freq,
285 .hapd_set_beacon = ap_driver_set_beacon,
286 .set_beacon_int = ap_driver_set_beacon_int,
287 .set_cts_protect = ap_driver_set_cts_protect,
288 .set_preamble = ap_driver_set_preamble,
289 .set_short_slot_time = ap_driver_set_short_slot_time,
290 .set_tx_queue_params = ap_driver_set_tx_queue_params,
291 .get_hw_feature_data = ap_driver_get_hw_feature_data,
292 .hapd_send_eapol = ap_driver_hapd_send_eapol,
296 extern struct wpa_driver_ops *wpa_drivers[];
298 static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
299 struct wpa_ssid *ssid,
300 struct hostapd_config *conf)
302 struct hostapd_bss_config *bss = &conf->bss[0];
305 for (j = 0; wpa_drivers[j]; j++) {
306 if (os_strcmp("wpa_supplicant", wpa_drivers[j]->name) == 0) {
307 conf->driver = wpa_drivers[j];
311 if (conf->driver == NULL) {
312 wpa_printf(MSG_ERROR, "No AP driver ops found");
316 os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
318 if (ssid->frequency == 0) {
319 /* default channel 11 */
320 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
322 } else if (ssid->frequency >= 2412 && ssid->frequency <= 2472) {
323 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
324 conf->channel = (ssid->frequency - 2407) / 5;
325 } else if ((ssid->frequency >= 5180 && ssid->frequency <= 5240) ||
326 (ssid->frequency >= 5745 && ssid->frequency <= 5825)) {
327 conf->hw_mode = HOSTAPD_MODE_IEEE80211A;
328 conf->channel = (ssid->frequency - 5000) / 5;
330 wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
335 /* TODO: enable HT if driver supports it;
336 * drop to 11b if driver does not support 11g */
338 if (ssid->ssid_len == 0) {
339 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
342 os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
343 bss->ssid.ssid[ssid->ssid_len] = '\0';
344 bss->ssid.ssid_len = ssid->ssid_len;
345 bss->ssid.ssid_set = 1;
347 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
348 bss->wpa = ssid->proto;
349 bss->wpa_key_mgmt = ssid->key_mgmt;
350 bss->wpa_pairwise = ssid->pairwise_cipher;
351 if (ssid->passphrase) {
352 bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
353 } else if (ssid->psk_set) {
354 os_free(bss->ssid.wpa_psk);
355 bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
356 if (bss->ssid.wpa_psk == NULL)
358 os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
359 bss->ssid.wpa_psk->group = 1;
362 /* Select group cipher based on the enabled pairwise cipher suites */
365 pairwise |= bss->wpa_pairwise;
367 if (bss->rsn_pairwise == 0)
368 bss->rsn_pairwise = bss->wpa_pairwise;
369 pairwise |= bss->rsn_pairwise;
371 if (pairwise & WPA_CIPHER_TKIP)
372 bss->wpa_group = WPA_CIPHER_TKIP;
374 bss->wpa_group = WPA_CIPHER_CCMP;
376 if (bss->wpa && bss->ieee802_1x)
377 bss->ssid.security_policy = SECURITY_WPA;
379 bss->ssid.security_policy = SECURITY_WPA_PSK;
380 else if (bss->ieee802_1x) {
381 bss->ssid.security_policy = SECURITY_IEEE_802_1X;
382 bss->ssid.wep.default_len = bss->default_wep_key_len;
383 } else if (bss->ssid.wep.keys_set)
384 bss->ssid.security_policy = SECURITY_STATIC_WEP;
386 bss->ssid.security_policy = SECURITY_PLAINTEXT;
390 * Enable WPS by default, but require user interaction to actually use
391 * it. Only the internal Registrar is supported.
395 bss->ap_setup_locked = 1;
396 bss->config_methods = os_strdup("display push_button");
397 #endif /* CONFIG_WPS */
403 int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
404 struct wpa_ssid *ssid)
406 struct wpa_driver_associate_params params;
407 struct hostapd_iface *hapd_iface;
408 struct hostapd_config *conf;
411 if (ssid->ssid == NULL || ssid->ssid_len == 0) {
412 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
416 wpa_supplicant_ap_deinit(wpa_s);
418 wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
419 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
421 os_memset(¶ms, 0, sizeof(params));
422 params.ssid = ssid->ssid;
423 params.ssid_len = ssid->ssid_len;
424 params.mode = ssid->mode;
425 params.freq = ssid->frequency;
427 if (wpa_drv_associate(wpa_s, ¶ms) < 0) {
428 wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
432 wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
433 if (hapd_iface == NULL)
435 hapd_iface->owner = wpa_s;
437 wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
439 wpa_supplicant_ap_deinit(wpa_s);
443 if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
444 wpa_printf(MSG_ERROR, "Failed to create AP configuration");
445 wpa_supplicant_ap_deinit(wpa_s);
449 hapd_iface->num_bss = conf->num_bss;
450 hapd_iface->bss = os_zalloc(conf->num_bss *
451 sizeof(struct hostapd_data *));
452 if (hapd_iface->bss == NULL) {
453 wpa_supplicant_ap_deinit(wpa_s);
457 for (i = 0; i < conf->num_bss; i++) {
459 hostapd_alloc_bss_data(hapd_iface, conf,
461 if (hapd_iface->bss[i] == NULL) {
462 wpa_supplicant_ap_deinit(wpa_s);
466 hapd_iface->bss[i]->msg_ctx = wpa_s;
469 if (hostapd_setup_interface(wpa_s->ap_iface)) {
470 wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
471 wpa_supplicant_ap_deinit(wpa_s);
475 wpa_s->current_ssid = ssid;
476 os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
477 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
483 void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
485 if (wpa_s->ap_iface == NULL)
488 hostapd_interface_deinit(wpa_s->ap_iface);
489 wpa_s->ap_iface = NULL;
493 void ap_tx_status(void *ctx, const u8 *addr,
494 const u8 *buf, size_t len, int ack)
496 struct wpa_supplicant *wpa_s = ctx;
497 hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
501 void ap_rx_from_unknown_sta(void *ctx, struct ieee80211_hdr *hdr, size_t len)
503 struct wpa_supplicant *wpa_s = ctx;
504 hostapd_rx_from_unknown_sta(wpa_s->ap_iface->bss[0], hdr, len);
509 void ap_mgmt_rx(void *ctx, u8 *buf, size_t len, u16 stype,
510 struct hostapd_frame_info *fi)
512 struct wpa_supplicant *wpa_s = ctx;
513 ieee802_11_mgmt(wpa_s->ap_iface->bss[0], buf, len, stype, fi);
517 void ap_mgmt_tx_cb(void *ctx, u8 *buf, size_t len, u16 stype, int ok)
519 struct wpa_supplicant *wpa_s = ctx;
520 ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
522 #endif /* NEED_AP_MLME */
525 void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
526 const u8 *src_addr, const u8 *buf, size_t len)
528 hostapd_eapol_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
534 int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
536 return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0]);
540 int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
541 const char *pin, char *buf, size_t buflen)
543 int ret, ret_len = 0;
546 unsigned int rpin = wps_generate_pin();
547 ret_len = os_snprintf(buf, buflen, "%d", rpin);
551 ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], "any", pin, 0);
557 #endif /* CONFIG_WPS */
560 #ifdef CONFIG_CTRL_IFACE
562 int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
563 char *buf, size_t buflen)
565 if (wpa_s->ap_iface == NULL)
567 return hostapd_ctrl_iface_sta_first(wpa_s->ap_iface->bss[0],
572 int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
573 char *buf, size_t buflen)
575 if (wpa_s->ap_iface == NULL)
577 return hostapd_ctrl_iface_sta(wpa_s->ap_iface->bss[0], txtaddr,
582 int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
583 char *buf, size_t buflen)
585 if (wpa_s->ap_iface == NULL)
587 return hostapd_ctrl_iface_sta_next(wpa_s->ap_iface->bss[0], txtaddr,
592 int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
593 size_t buflen, int verbose)
595 char *pos = buf, *end = buf + buflen;
597 struct hostapd_bss_config *conf;
599 if (wpa_s->ap_iface == NULL)
602 conf = wpa_s->ap_iface->bss[0]->conf;
606 ret = os_snprintf(pos, end - pos,
607 "pairwise_cipher=%s\n"
610 wpa_cipher_txt(conf->rsn_pairwise),
611 wpa_cipher_txt(conf->wpa_group),
612 wpa_key_mgmt_txt(conf->wpa_key_mgmt,
614 if (ret < 0 || ret >= end - pos)
620 #endif /* CONFIG_CTRL_IFACE */