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 struct hapd_interfaces {
38 struct hostapd_iface **iface;
42 int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
43 int (*cb)(struct hostapd_iface *iface,
44 void *ctx), void *ctx)
51 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
57 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
62 struct ap_driver_data {
63 struct hostapd_data *hapd;
67 static void * ap_driver_init(struct hostapd_data *hapd,
68 struct wpa_init_params *params)
70 struct ap_driver_data *drv;
71 struct wpa_supplicant *wpa_s = hapd->iface->owner;
73 drv = os_zalloc(sizeof(struct ap_driver_data));
75 wpa_printf(MSG_ERROR, "Could not allocate memory for AP "
80 os_memcpy(hapd->own_addr, wpa_s->own_addr, ETH_ALEN);
86 static void ap_driver_deinit(void *priv)
88 struct ap_driver_data *drv = priv;
94 static int ap_driver_send_ether(void *priv, const u8 *dst, const u8 *src,
95 u16 proto, const u8 *data, size_t data_len)
97 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
102 static int ap_driver_set_key(const char *iface, void *priv, wpa_alg alg,
103 const u8 *addr, int key_idx, int set_tx,
104 const u8 *seq, size_t seq_len, const u8 *key,
107 struct ap_driver_data *drv = priv;
108 struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
109 return wpa_drv_set_key(wpa_s, alg, addr, key_idx, set_tx, seq, seq_len,
114 static int ap_driver_get_seqnum(const char *iface, void *priv, const u8 *addr,
117 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
122 static int ap_driver_flush(void *priv)
124 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
129 static int ap_driver_read_sta_data(void *priv,
130 struct hostap_sta_driver_data *data,
133 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
138 static int ap_driver_sta_set_flags(void *priv, const u8 *addr, int total_flags,
139 int flags_or, int flags_and)
141 struct ap_driver_data *drv = priv;
142 struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
143 return wpa_drv_sta_set_flags(wpa_s, addr, total_flags, flags_or,
148 static int ap_driver_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
151 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
156 static int ap_driver_sta_disassoc(void *priv, const u8 *own_addr,
157 const u8 *addr, int reason)
159 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
164 static int ap_driver_sta_remove(void *priv, const u8 *addr)
166 struct ap_driver_data *drv = priv;
167 struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
168 return wpa_drv_sta_remove(wpa_s, addr);
172 static int ap_driver_send_mlme(void *priv, const u8 *data, size_t len)
174 struct ap_driver_data *drv = priv;
175 struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
176 return wpa_drv_send_mlme(wpa_s, data, len);
180 static int ap_driver_sta_add(const char *ifname, void *priv,
181 struct hostapd_sta_add_params *params)
183 struct ap_driver_data *drv = priv;
184 struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
185 return wpa_drv_sta_add(wpa_s, params);
189 static int ap_driver_get_inact_sec(void *priv, const u8 *addr)
191 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
196 static int ap_driver_set_freq(void *priv, struct hostapd_freq_params *freq)
198 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
203 static int ap_driver_set_beacon(const char *iface, void *priv,
204 const u8 *head, size_t head_len,
205 const u8 *tail, size_t tail_len,
206 int dtim_period, int beacon_int)
208 struct ap_driver_data *drv = priv;
209 struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
210 return wpa_drv_set_beacon(wpa_s, head, head_len, tail, tail_len,
211 dtim_period, beacon_int);
215 static int ap_driver_set_cts_protect(void *priv, int value)
217 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
222 static int ap_driver_set_preamble(void *priv, int value)
224 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
229 static int ap_driver_set_short_slot_time(void *priv, int value)
231 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
236 static int ap_driver_set_tx_queue_params(void *priv, int queue, int aifs,
237 int cw_min, int cw_max,
240 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
245 static struct hostapd_hw_modes *ap_driver_get_hw_feature_data(void *priv,
249 struct ap_driver_data *drv = priv;
250 struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
251 return wpa_drv_get_hw_feature_data(wpa_s, num_modes, flags);
255 static int ap_driver_hapd_send_eapol(void *priv, const u8 *addr,
256 const u8 *data, size_t data_len,
257 int encrypt, const u8 *own_addr)
259 struct ap_driver_data *drv = priv;
260 struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
261 return wpa_drv_hapd_send_eapol(wpa_s, addr, data, data_len, encrypt,
266 struct wpa_driver_ops ap_driver_ops =
268 .name = "wpa_supplicant",
269 .hapd_init = ap_driver_init,
270 .hapd_deinit = ap_driver_deinit,
271 .send_ether = ap_driver_send_ether,
272 .set_key = ap_driver_set_key,
273 .get_seqnum = ap_driver_get_seqnum,
274 .flush = ap_driver_flush,
275 .read_sta_data = ap_driver_read_sta_data,
276 .sta_set_flags = ap_driver_sta_set_flags,
277 .sta_deauth = ap_driver_sta_deauth,
278 .sta_disassoc = ap_driver_sta_disassoc,
279 .sta_remove = ap_driver_sta_remove,
280 .send_mlme = ap_driver_send_mlme,
281 .sta_add = ap_driver_sta_add,
282 .get_inact_sec = ap_driver_get_inact_sec,
283 .set_freq = ap_driver_set_freq,
284 .set_beacon = ap_driver_set_beacon,
285 .set_cts_protect = ap_driver_set_cts_protect,
286 .set_preamble = ap_driver_set_preamble,
287 .set_short_slot_time = ap_driver_set_short_slot_time,
288 .set_tx_queue_params = ap_driver_set_tx_queue_params,
289 .get_hw_feature_data = ap_driver_get_hw_feature_data,
290 .hapd_send_eapol = ap_driver_hapd_send_eapol,
294 extern struct wpa_driver_ops *wpa_drivers[];
296 static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
297 struct wpa_ssid *ssid,
298 struct hostapd_config *conf)
300 struct hostapd_bss_config *bss = &conf->bss[0];
303 for (j = 0; wpa_drivers[j]; j++) {
304 if (os_strcmp("wpa_supplicant", wpa_drivers[j]->name) == 0) {
305 conf->driver = wpa_drivers[j];
309 if (conf->driver == NULL) {
310 wpa_printf(MSG_ERROR, "No AP driver ops found");
314 os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
316 if (ssid->frequency == 0) {
317 /* default channel 11 */
318 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
320 } else if (ssid->frequency >= 2412 && ssid->frequency <= 2472) {
321 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
322 conf->channel = (ssid->frequency - 2407) / 5;
323 } else if ((ssid->frequency >= 5180 && ssid->frequency <= 5240) ||
324 (ssid->frequency >= 5745 && ssid->frequency <= 5825)) {
325 conf->hw_mode = HOSTAPD_MODE_IEEE80211A;
326 conf->channel = (ssid->frequency - 5000) / 5;
328 wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
333 /* TODO: enable HT if driver supports it;
334 * drop to 11b if driver does not support 11g */
336 if (ssid->ssid_len == 0) {
337 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
340 os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
341 bss->ssid.ssid[ssid->ssid_len] = '\0';
342 bss->ssid.ssid_len = ssid->ssid_len;
343 bss->ssid.ssid_set = 1;
345 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
346 bss->wpa = ssid->proto;
347 bss->wpa_key_mgmt = ssid->key_mgmt;
348 bss->wpa_pairwise = ssid->pairwise_cipher;
349 if (ssid->passphrase) {
350 bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
351 } else if (ssid->psk_set) {
352 os_free(bss->ssid.wpa_psk);
353 bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
354 if (bss->ssid.wpa_psk == NULL)
356 os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
357 bss->ssid.wpa_psk->group = 1;
360 /* Select group cipher based on the enabled pairwise cipher suites */
363 pairwise |= bss->wpa_pairwise;
365 if (bss->rsn_pairwise == 0)
366 bss->rsn_pairwise = bss->wpa_pairwise;
367 pairwise |= bss->rsn_pairwise;
369 if (pairwise & WPA_CIPHER_TKIP)
370 bss->wpa_group = WPA_CIPHER_TKIP;
372 bss->wpa_group = WPA_CIPHER_CCMP;
374 if (bss->wpa && bss->ieee802_1x)
375 bss->ssid.security_policy = SECURITY_WPA;
377 bss->ssid.security_policy = SECURITY_WPA_PSK;
378 else if (bss->ieee802_1x) {
379 bss->ssid.security_policy = SECURITY_IEEE_802_1X;
380 bss->ssid.wep.default_len = bss->default_wep_key_len;
381 } else if (bss->ssid.wep.keys_set)
382 bss->ssid.security_policy = SECURITY_STATIC_WEP;
384 bss->ssid.security_policy = SECURITY_PLAINTEXT;
388 * Enable WPS by default, but require user interaction to actually use
389 * it. Only the internal Registrar is supported.
393 bss->ap_setup_locked = 1;
394 bss->config_methods = os_strdup("display push_button keypad");
395 #endif /* CONFIG_WPS */
401 int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
402 struct wpa_ssid *ssid)
404 struct wpa_driver_associate_params params;
405 struct hostapd_iface *hapd_iface;
406 struct hostapd_config *conf;
409 if (ssid->ssid == NULL || ssid->ssid_len == 0) {
410 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
414 wpa_supplicant_ap_deinit(wpa_s);
416 wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
417 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
419 os_memset(¶ms, 0, sizeof(params));
420 params.ssid = ssid->ssid;
421 params.ssid_len = ssid->ssid_len;
422 params.mode = ssid->mode;
423 params.freq = ssid->frequency;
425 if (wpa_drv_associate(wpa_s, ¶ms) < 0) {
426 wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
430 wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
431 if (hapd_iface == NULL)
433 hapd_iface->owner = wpa_s;
435 wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
437 wpa_supplicant_ap_deinit(wpa_s);
441 if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
442 wpa_printf(MSG_ERROR, "Failed to create AP configuration");
443 wpa_supplicant_ap_deinit(wpa_s);
447 hapd_iface->num_bss = conf->num_bss;
448 hapd_iface->bss = os_zalloc(conf->num_bss *
449 sizeof(struct hostapd_data *));
450 if (hapd_iface->bss == NULL) {
451 wpa_supplicant_ap_deinit(wpa_s);
455 for (i = 0; i < conf->num_bss; i++) {
457 hostapd_alloc_bss_data(hapd_iface, conf,
459 if (hapd_iface->bss[i] == NULL) {
460 wpa_supplicant_ap_deinit(wpa_s);
464 hapd_iface->bss[i]->msg_ctx = wpa_s;
467 if (hostapd_setup_interface(wpa_s->ap_iface)) {
468 wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
469 wpa_supplicant_ap_deinit(wpa_s);
473 wpa_s->current_ssid = ssid;
474 os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
475 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
481 void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
483 if (wpa_s->ap_iface == NULL)
486 hostapd_interface_deinit(wpa_s->ap_iface);
487 wpa_s->ap_iface = NULL;
491 void ap_tx_status(void *ctx, const u8 *addr,
492 const u8 *buf, size_t len, int ack)
495 struct wpa_supplicant *wpa_s = ctx;
496 hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
497 #endif /* NEED_AP_MLME */
501 void ap_rx_from_unknown_sta(void *ctx, const struct ieee80211_hdr *hdr,
505 struct wpa_supplicant *wpa_s = ctx;
506 ieee802_11_rx_from_unknown(wpa_s->ap_iface->bss[0], hdr->addr2);
507 #endif /* NEED_AP_MLME */
511 void ap_mgmt_rx(void *ctx, const u8 *buf, size_t len,
512 struct hostapd_frame_info *fi)
515 struct wpa_supplicant *wpa_s = ctx;
516 ieee802_11_mgmt(wpa_s->ap_iface->bss[0], buf, len, fi);
517 #endif /* NEED_AP_MLME */
521 void ap_mgmt_tx_cb(void *ctx, const u8 *buf, size_t len, u16 stype, int ok)
524 struct wpa_supplicant *wpa_s = ctx;
525 ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
526 #endif /* NEED_AP_MLME */
530 void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
531 const u8 *src_addr, const u8 *buf, size_t len)
533 hostapd_eapol_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
539 int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
541 return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0]);
545 int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
546 const char *pin, char *buf, size_t buflen)
548 int ret, ret_len = 0;
551 unsigned int rpin = wps_generate_pin();
552 ret_len = os_snprintf(buf, buflen, "%d", rpin);
556 ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], "any", pin, 0);
562 #endif /* CONFIG_WPS */
565 #ifdef CONFIG_CTRL_IFACE
567 int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
568 char *buf, size_t buflen)
570 if (wpa_s->ap_iface == NULL)
572 return hostapd_ctrl_iface_sta_first(wpa_s->ap_iface->bss[0],
577 int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
578 char *buf, size_t buflen)
580 if (wpa_s->ap_iface == NULL)
582 return hostapd_ctrl_iface_sta(wpa_s->ap_iface->bss[0], txtaddr,
587 int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
588 char *buf, size_t buflen)
590 if (wpa_s->ap_iface == NULL)
592 return hostapd_ctrl_iface_sta_next(wpa_s->ap_iface->bss[0], txtaddr,
597 int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
598 size_t buflen, int verbose)
600 char *pos = buf, *end = buf + buflen;
602 struct hostapd_bss_config *conf;
604 if (wpa_s->ap_iface == NULL)
607 conf = wpa_s->ap_iface->bss[0]->conf;
611 ret = os_snprintf(pos, end - pos,
612 "pairwise_cipher=%s\n"
615 wpa_cipher_txt(conf->rsn_pairwise),
616 wpa_cipher_txt(conf->wpa_group),
617 wpa_key_mgmt_txt(conf->wpa_key_mgmt,
619 if (ret < 0 || ret >= end - pos)
625 #endif /* CONFIG_CTRL_IFACE */