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.
16 #include "utils/includes.h"
18 #include "utils/common.h"
19 #include "common/ieee802_11_defs.h"
20 #include "ap/hostapd.h"
21 #include "ap/ap_config.h"
23 #include "ap/ieee802_11.h"
24 #endif /* NEED_AP_MLME */
25 #include "ap/beacon.h"
26 #include "ap/ieee802_1x.h"
27 #include "ap/wps_hostapd.h"
28 #include "ap/ctrl_iface_ap.h"
29 #include "eap_common/eap_defs.h"
30 #include "eap_server/eap_methods.h"
31 #include "eap_common/eap_wsc_common.h"
33 #include "common/ieee802_11_defs.h"
34 #include "config_ssid.h"
36 #include "wpa_supplicant_i.h"
38 #include "p2p_supplicant.h"
40 #include "ap/sta_info.h"
43 static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
44 struct wpa_ssid *ssid,
45 struct hostapd_config *conf)
47 struct hostapd_bss_config *bss = &conf->bss[0];
50 conf->driver = wpa_s->driver;
52 os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
54 if (ssid->frequency == 0) {
55 /* default channel 11 */
56 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
58 } else if (ssid->frequency >= 2412 && ssid->frequency <= 2472) {
59 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
60 conf->channel = (ssid->frequency - 2407) / 5;
61 } else if ((ssid->frequency >= 5180 && ssid->frequency <= 5240) ||
62 (ssid->frequency >= 5745 && ssid->frequency <= 5825)) {
63 conf->hw_mode = HOSTAPD_MODE_IEEE80211A;
64 conf->channel = (ssid->frequency - 5000) / 5;
66 wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
71 /* TODO: enable HT if driver supports it;
72 * drop to 11b if driver does not support 11g */
75 if (conf->hw_mode == HOSTAPD_MODE_IEEE80211G) {
76 /* Remove 802.11b rates from supported and basic rate sets */
77 int *list = os_malloc(4 * sizeof(int));
84 conf->basic_rates = list;
86 list = os_malloc(9 * sizeof(int));
98 conf->supported_rates = list;
100 #endif /* CONFIG_P2P */
102 if (ssid->ssid_len == 0) {
103 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
106 os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
107 bss->ssid.ssid[ssid->ssid_len] = '\0';
108 bss->ssid.ssid_len = ssid->ssid_len;
109 bss->ssid.ssid_set = 1;
111 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
112 bss->wpa = ssid->proto;
113 bss->wpa_key_mgmt = ssid->key_mgmt;
114 bss->wpa_pairwise = ssid->pairwise_cipher;
115 if (ssid->passphrase) {
116 bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
117 } else if (ssid->psk_set) {
118 os_free(bss->ssid.wpa_psk);
119 bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
120 if (bss->ssid.wpa_psk == NULL)
122 os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
123 bss->ssid.wpa_psk->group = 1;
126 /* Select group cipher based on the enabled pairwise cipher suites */
129 pairwise |= bss->wpa_pairwise;
131 if (bss->rsn_pairwise == 0)
132 bss->rsn_pairwise = bss->wpa_pairwise;
133 pairwise |= bss->rsn_pairwise;
135 if (pairwise & WPA_CIPHER_TKIP)
136 bss->wpa_group = WPA_CIPHER_TKIP;
138 bss->wpa_group = WPA_CIPHER_CCMP;
140 if (bss->wpa && bss->ieee802_1x)
141 bss->ssid.security_policy = SECURITY_WPA;
143 bss->ssid.security_policy = SECURITY_WPA_PSK;
144 else if (bss->ieee802_1x) {
145 bss->ssid.security_policy = SECURITY_IEEE_802_1X;
146 bss->ssid.wep.default_len = bss->default_wep_key_len;
147 } else if (bss->ssid.wep.keys_set)
148 bss->ssid.security_policy = SECURITY_STATIC_WEP;
150 bss->ssid.security_policy = SECURITY_PLAINTEXT;
154 * Enable WPS by default, but require user interaction to actually use
155 * it. Only the internal Registrar is supported.
159 bss->ap_setup_locked = 1;
160 if (wpa_s->conf->config_methods)
161 bss->config_methods = os_strdup(wpa_s->conf->config_methods);
162 if (wpa_s->conf->device_type)
163 bss->device_type = os_strdup(wpa_s->conf->device_type);
164 if (wpa_s->conf->device_name) {
165 bss->device_name = os_strdup(wpa_s->conf->device_name);
166 bss->friendly_name = os_strdup(wpa_s->conf->device_name);
168 if (wpa_s->conf->manufacturer)
169 bss->manufacturer = os_strdup(wpa_s->conf->manufacturer);
170 if (wpa_s->conf->model_name)
171 bss->model_name = os_strdup(wpa_s->conf->model_name);
172 if (wpa_s->conf->model_number)
173 bss->model_number = os_strdup(wpa_s->conf->model_number);
174 if (wpa_s->conf->serial_number)
175 bss->serial_number = os_strdup(wpa_s->conf->serial_number);
176 os_memcpy(bss->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
177 os_memcpy(bss->os_version, wpa_s->conf->os_version, 4);
178 #endif /* CONFIG_WPS */
184 static void ap_public_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
187 struct wpa_supplicant *wpa_s = ctx;
188 const struct ieee80211_mgmt *mgmt;
191 mgmt = (const struct ieee80211_mgmt *) buf;
192 hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
195 wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
196 mgmt->u.action.category,
197 &mgmt->u.action.u.vs_public_action.action,
198 len - hdr_len, freq);
199 #endif /* CONFIG_P2P */
203 static int ap_vendor_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
206 struct wpa_supplicant *wpa_s = ctx;
207 const struct ieee80211_mgmt *mgmt;
210 mgmt = (const struct ieee80211_mgmt *) buf;
211 hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
214 wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
215 mgmt->u.action.category,
216 &mgmt->u.action.u.vs_public_action.action,
217 len - hdr_len, freq);
218 #endif /* CONFIG_P2P */
223 static int ap_probe_req_rx(void *ctx, const u8 *addr, const u8 *ie,
227 struct wpa_supplicant *wpa_s = ctx;
228 return wpas_p2p_probe_req_rx(wpa_s, addr, ie, ie_len);
229 #else /* CONFIG_P2P */
231 #endif /* CONFIG_P2P */
235 static void ap_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
239 struct wpa_supplicant *wpa_s = ctx;
240 wpas_p2p_wps_success(wpa_s, mac_addr, 1);
241 #endif /* CONFIG_P2P */
245 int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
246 struct wpa_ssid *ssid)
248 struct wpa_driver_associate_params params;
249 struct hostapd_iface *hapd_iface;
250 struct hostapd_config *conf;
253 if (ssid->ssid == NULL || ssid->ssid_len == 0) {
254 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
258 wpa_supplicant_ap_deinit(wpa_s);
260 wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
261 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
263 os_memset(¶ms, 0, sizeof(params));
264 params.ssid = ssid->ssid;
265 params.ssid_len = ssid->ssid_len;
266 switch (ssid->mode) {
267 case WPAS_MODE_INFRA:
268 params.mode = IEEE80211_MODE_INFRA;
271 params.mode = IEEE80211_MODE_IBSS;
274 case WPAS_MODE_P2P_GO:
275 case WPAS_MODE_P2P_GROUP_FORMATION:
276 params.mode = IEEE80211_MODE_AP;
279 params.freq = ssid->frequency;
281 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK)
282 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
284 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
285 params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
287 if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
288 wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
289 else if (ssid->pairwise_cipher & WPA_CIPHER_TKIP)
290 wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
291 else if (ssid->pairwise_cipher & WPA_CIPHER_NONE)
292 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
294 wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
298 params.pairwise_suite = cipher_suite2driver(wpa_s->pairwise_cipher);
299 params.group_suite = params.pairwise_suite;
302 if (ssid->mode == WPAS_MODE_P2P_GO ||
303 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
305 wpa_drv_set_intra_bss(wpa_s, wpa_s->conf->p2p_intra_bss);
306 #endif /* CONFIG_P2P */
308 if (wpa_s->parent->set_ap_uapsd)
309 params.uapsd = wpa_s->parent->ap_uapsd;
313 if (wpa_drv_associate(wpa_s, ¶ms) < 0) {
314 wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
318 wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
319 if (hapd_iface == NULL)
321 hapd_iface->owner = wpa_s;
323 wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
325 wpa_supplicant_ap_deinit(wpa_s);
329 if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
330 wpa_printf(MSG_ERROR, "Failed to create AP configuration");
331 wpa_supplicant_ap_deinit(wpa_s);
336 if (ssid->mode == WPAS_MODE_P2P_GO)
337 conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER;
338 else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
339 conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER |
341 #endif /* CONFIG_P2P */
343 hapd_iface->num_bss = conf->num_bss;
344 hapd_iface->bss = os_zalloc(conf->num_bss *
345 sizeof(struct hostapd_data *));
346 if (hapd_iface->bss == NULL) {
347 wpa_supplicant_ap_deinit(wpa_s);
351 for (i = 0; i < conf->num_bss; i++) {
353 hostapd_alloc_bss_data(hapd_iface, conf,
355 if (hapd_iface->bss[i] == NULL) {
356 wpa_supplicant_ap_deinit(wpa_s);
360 hapd_iface->bss[i]->msg_ctx = wpa_s;
361 hapd_iface->bss[i]->public_action_cb = ap_public_action_rx;
362 hapd_iface->bss[i]->public_action_cb_ctx = wpa_s;
363 hapd_iface->bss[i]->vendor_action_cb = ap_vendor_action_rx;
364 hapd_iface->bss[i]->vendor_action_cb_ctx = wpa_s;
365 hostapd_register_probereq_cb(hapd_iface->bss[i],
366 ap_probe_req_rx, wpa_s);
367 hapd_iface->bss[i]->wps_reg_success_cb = ap_wps_reg_success_cb;
368 hapd_iface->bss[i]->wps_reg_success_cb_ctx = wpa_s;
370 hapd_iface->bss[i]->p2p = wpa_s->global->p2p;
371 hapd_iface->bss[i]->p2p_group = wpas_p2p_group_init(
372 wpa_s, ssid->p2p_persistent_group,
373 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION);
374 #endif /* CONFIG_P2P */
377 os_memcpy(hapd_iface->bss[0]->own_addr, wpa_s->own_addr, ETH_ALEN);
378 hapd_iface->bss[0]->driver = wpa_s->driver;
379 hapd_iface->bss[0]->drv_priv = wpa_s->drv_priv;
381 if (hostapd_setup_interface(wpa_s->ap_iface)) {
382 wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
383 wpa_supplicant_ap_deinit(wpa_s);
387 wpa_s->current_ssid = ssid;
388 os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
389 wpa_s->assoc_freq = ssid->frequency;
390 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
392 if (wpa_s->ap_configured_cb)
393 wpa_s->ap_configured_cb(wpa_s->ap_configured_cb_ctx,
394 wpa_s->ap_configured_cb_data);
400 void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
402 if (wpa_s->ap_iface == NULL)
405 wpa_s->current_ssid = NULL;
407 if (wpa_s->ap_iface->bss)
408 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
409 wpas_p2p_group_deinit(wpa_s);
410 #endif /* CONFIG_P2P */
411 hostapd_interface_deinit(wpa_s->ap_iface);
412 hostapd_interface_free(wpa_s->ap_iface);
413 wpa_s->ap_iface = NULL;
414 wpa_drv_deinit_ap(wpa_s);
418 void ap_tx_status(void *ctx, const u8 *addr,
419 const u8 *buf, size_t len, int ack)
422 struct wpa_supplicant *wpa_s = ctx;
423 hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
424 #endif /* NEED_AP_MLME */
428 void ap_rx_from_unknown_sta(void *ctx, const u8 *frame, size_t len)
431 struct wpa_supplicant *wpa_s = ctx;
432 const struct ieee80211_hdr *hdr =
433 (const struct ieee80211_hdr *) frame;
434 u16 fc = le_to_host16(hdr->frame_control);
435 ieee802_11_rx_from_unknown(wpa_s->ap_iface->bss[0], hdr->addr2,
436 (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
437 (WLAN_FC_TODS | WLAN_FC_FROMDS));
438 #endif /* NEED_AP_MLME */
442 void ap_mgmt_rx(void *ctx, struct rx_mgmt *rx_mgmt)
445 struct wpa_supplicant *wpa_s = ctx;
446 struct hostapd_frame_info fi;
447 os_memset(&fi, 0, sizeof(fi));
448 fi.datarate = rx_mgmt->datarate;
449 fi.ssi_signal = rx_mgmt->ssi_signal;
450 ieee802_11_mgmt(wpa_s->ap_iface->bss[0], rx_mgmt->frame,
451 rx_mgmt->frame_len, &fi);
452 #endif /* NEED_AP_MLME */
456 void ap_mgmt_tx_cb(void *ctx, const u8 *buf, size_t len, u16 stype, int ok)
459 struct wpa_supplicant *wpa_s = ctx;
460 ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
461 #endif /* NEED_AP_MLME */
465 void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
466 const u8 *src_addr, const u8 *buf, size_t len)
468 ieee802_1x_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
474 int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
476 if (!wpa_s->ap_iface)
478 return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0]);
482 static int wpa_supplicant_ap_wps_sta_cancel(struct hostapd_data *hapd,
483 struct sta_info *sta, void *ctx)
485 if (sta && (sta->flags & WLAN_STA_WPS)) {
486 ap_sta_deauthenticate(hapd, sta,
487 WLAN_REASON_PREV_AUTH_NOT_VALID);
488 wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
489 __func__, MAC2STR(sta->addr));
497 int wpa_supplicant_ap_wps_cancel(struct wpa_supplicant *wpa_s)
499 struct wps_registrar *reg;
500 int reg_sel = 0, wps_sta = 0;
502 if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0]->wps)
505 reg = wpa_s->ap_iface->bss[0]->wps->registrar;
506 reg_sel = wps_registrar_wps_cancel(reg);
507 wps_sta = ap_for_each_sta(wpa_s->ap_iface->bss[0],
508 wpa_supplicant_ap_wps_sta_cancel, NULL);
510 if (!reg_sel && !wps_sta) {
511 wpa_printf(MSG_DEBUG, "No WPS operation in progress at this "
517 * There are 2 cases to return wps cancel as success:
518 * 1. When wps cancel was initiated but no connection has been
519 * established with client yet.
520 * 2. Client is in the middle of exchanging WPS messages.
527 int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
528 const char *pin, char *buf, size_t buflen)
530 int ret, ret_len = 0;
532 if (!wpa_s->ap_iface)
536 unsigned int rpin = wps_generate_pin();
537 ret_len = os_snprintf(buf, buflen, "%d", rpin);
540 ret_len = os_snprintf(buf, buflen, "%s", pin);
542 ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], bssid, "any", pin,
549 #endif /* CONFIG_WPS */
552 #ifdef CONFIG_CTRL_IFACE
554 int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
555 char *buf, size_t buflen)
557 if (wpa_s->ap_iface == NULL)
559 return hostapd_ctrl_iface_sta_first(wpa_s->ap_iface->bss[0],
564 int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
565 char *buf, size_t buflen)
567 if (wpa_s->ap_iface == NULL)
569 return hostapd_ctrl_iface_sta(wpa_s->ap_iface->bss[0], txtaddr,
574 int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
575 char *buf, size_t buflen)
577 if (wpa_s->ap_iface == NULL)
579 return hostapd_ctrl_iface_sta_next(wpa_s->ap_iface->bss[0], txtaddr,
584 int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
585 size_t buflen, int verbose)
587 char *pos = buf, *end = buf + buflen;
589 struct hostapd_bss_config *conf;
591 if (wpa_s->ap_iface == NULL)
594 conf = wpa_s->ap_iface->bss[0]->conf;
598 ret = os_snprintf(pos, end - pos,
599 "pairwise_cipher=%s\n"
602 wpa_cipher_txt(conf->rsn_pairwise),
603 wpa_cipher_txt(conf->wpa_group),
604 wpa_key_mgmt_txt(conf->wpa_key_mgmt,
606 if (ret < 0 || ret >= end - pos)
612 #endif /* CONFIG_CTRL_IFACE */
615 int wpa_supplicant_ap_update_beacon(struct wpa_supplicant *wpa_s)
617 struct hostapd_iface *iface = wpa_s->ap_iface;
618 struct wpa_ssid *ssid = wpa_s->current_ssid;
619 struct hostapd_data *hapd;
621 if (ssid == NULL || wpa_s->ap_iface == NULL)
625 if (ssid->mode == WPAS_MODE_P2P_GO)
626 iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER;
627 else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
628 iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER |
630 #endif /* CONFIG_P2P */
632 ieee802_11_set_beacons(iface);
633 hapd = iface->bss[0];
634 hapd->drv.set_ap_wps_ie(hapd);
640 int wpa_supplicant_ap_mac_addr_filter(struct wpa_supplicant *wpa_s,
643 struct hostapd_data *hapd;
644 struct hostapd_bss_config *conf;
646 if (!wpa_s->ap_iface)
650 wpa_printf(MSG_DEBUG, "AP: Set MAC address filter: " MACSTR,
653 wpa_printf(MSG_DEBUG, "AP: Clear MAC address filter");
655 hapd = wpa_s->ap_iface->bss[0];
658 os_free(conf->accept_mac);
659 conf->accept_mac = NULL;
660 conf->num_accept_mac = 0;
661 os_free(conf->deny_mac);
662 conf->deny_mac = NULL;
663 conf->num_deny_mac = 0;
666 conf->macaddr_acl = ACCEPT_UNLESS_DENIED;
670 conf->macaddr_acl = DENY_UNLESS_ACCEPTED;
671 conf->accept_mac = os_zalloc(sizeof(struct mac_acl_entry));
672 if (conf->accept_mac == NULL)
674 os_memcpy(conf->accept_mac[0].addr, addr, ETH_ALEN);
675 conf->num_accept_mac = 1;