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 "common/wpa_ctrl.h"
21 #include "ap/hostapd.h"
22 #include "ap/ap_config.h"
24 #include "ap/ieee802_11.h"
25 #endif /* NEED_AP_MLME */
26 #include "ap/beacon.h"
27 #include "ap/ieee802_1x.h"
28 #include "ap/wps_hostapd.h"
29 #include "ap/ctrl_iface_ap.h"
30 #include "eap_common/eap_defs.h"
31 #include "eap_server/eap_methods.h"
32 #include "eap_common/eap_wsc_common.h"
34 #include "common/ieee802_11_defs.h"
35 #include "config_ssid.h"
37 #include "wpa_supplicant_i.h"
39 #include "p2p_supplicant.h"
41 #include "ap/sta_info.h"
44 static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
45 struct wpa_ssid *ssid,
46 struct hostapd_config *conf)
48 struct hostapd_bss_config *bss = &conf->bss[0];
51 conf->driver = wpa_s->driver;
53 os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
55 if (ssid->frequency == 0) {
56 /* default channel 11 */
57 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
59 } else if (ssid->frequency >= 2412 && ssid->frequency <= 2472) {
60 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
61 conf->channel = (ssid->frequency - 2407) / 5;
62 } else if ((ssid->frequency >= 5180 && ssid->frequency <= 5240) ||
63 (ssid->frequency >= 5745 && ssid->frequency <= 5825)) {
64 conf->hw_mode = HOSTAPD_MODE_IEEE80211A;
65 conf->channel = (ssid->frequency - 5000) / 5;
67 wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
72 /* TODO: enable HT if driver supports it;
73 * drop to 11b if driver does not support 11g */
76 if (conf->hw_mode == HOSTAPD_MODE_IEEE80211G) {
77 /* Remove 802.11b rates from supported and basic rate sets */
78 int *list = os_malloc(4 * sizeof(int));
85 conf->basic_rates = list;
87 list = os_malloc(9 * sizeof(int));
99 conf->supported_rates = list;
101 #endif /* CONFIG_P2P */
103 if (ssid->ssid_len == 0) {
104 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
107 os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
108 bss->ssid.ssid[ssid->ssid_len] = '\0';
109 bss->ssid.ssid_len = ssid->ssid_len;
110 bss->ssid.ssid_set = 1;
112 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
113 bss->wpa = ssid->proto;
114 bss->wpa_key_mgmt = ssid->key_mgmt;
115 bss->wpa_pairwise = ssid->pairwise_cipher;
116 if (ssid->passphrase) {
117 bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
118 } else if (ssid->psk_set) {
119 os_free(bss->ssid.wpa_psk);
120 bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
121 if (bss->ssid.wpa_psk == NULL)
123 os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
124 bss->ssid.wpa_psk->group = 1;
127 /* Select group cipher based on the enabled pairwise cipher suites */
130 pairwise |= bss->wpa_pairwise;
132 if (bss->rsn_pairwise == 0)
133 bss->rsn_pairwise = bss->wpa_pairwise;
134 pairwise |= bss->rsn_pairwise;
136 if (pairwise & WPA_CIPHER_TKIP)
137 bss->wpa_group = WPA_CIPHER_TKIP;
139 bss->wpa_group = WPA_CIPHER_CCMP;
141 if (bss->wpa && bss->ieee802_1x)
142 bss->ssid.security_policy = SECURITY_WPA;
144 bss->ssid.security_policy = SECURITY_WPA_PSK;
145 else if (bss->ieee802_1x) {
146 bss->ssid.security_policy = SECURITY_IEEE_802_1X;
147 bss->ssid.wep.default_len = bss->default_wep_key_len;
148 } else if (bss->ssid.wep.keys_set)
149 bss->ssid.security_policy = SECURITY_STATIC_WEP;
151 bss->ssid.security_policy = SECURITY_PLAINTEXT;
155 * Enable WPS by default, but require user interaction to actually use
156 * it. Only the internal Registrar is supported.
160 bss->ap_setup_locked = 1;
161 if (wpa_s->conf->config_methods)
162 bss->config_methods = os_strdup(wpa_s->conf->config_methods);
163 if (wpa_s->conf->device_type)
164 bss->device_type = os_strdup(wpa_s->conf->device_type);
165 if (wpa_s->conf->device_name) {
166 bss->device_name = os_strdup(wpa_s->conf->device_name);
167 bss->friendly_name = os_strdup(wpa_s->conf->device_name);
169 if (wpa_s->conf->manufacturer)
170 bss->manufacturer = os_strdup(wpa_s->conf->manufacturer);
171 if (wpa_s->conf->model_name)
172 bss->model_name = os_strdup(wpa_s->conf->model_name);
173 if (wpa_s->conf->model_number)
174 bss->model_number = os_strdup(wpa_s->conf->model_number);
175 if (wpa_s->conf->serial_number)
176 bss->serial_number = os_strdup(wpa_s->conf->serial_number);
177 os_memcpy(bss->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
178 os_memcpy(bss->os_version, wpa_s->conf->os_version, 4);
179 #endif /* CONFIG_WPS */
185 static void ap_public_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
188 struct wpa_supplicant *wpa_s = ctx;
189 const struct ieee80211_mgmt *mgmt;
192 mgmt = (const struct ieee80211_mgmt *) buf;
193 hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
196 wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
197 mgmt->u.action.category,
198 &mgmt->u.action.u.vs_public_action.action,
199 len - hdr_len, freq);
200 #endif /* CONFIG_P2P */
204 static void ap_wps_event_cb(void *ctx, enum wps_event event,
205 union wps_event_data *data)
207 struct wpa_supplicant *wpa_s = ctx;
209 if (event == WPS_EV_FAIL && wpa_s->parent && wpa_s->parent != wpa_s) {
210 struct wps_event_fail *fail = &data->fail;
213 * src/ap/wps_hostapd.c has already sent this on the main
214 * interface, so only send on the parent interface here if
217 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
218 "msg=%d config_error=%d",
219 fail->msg, fail->config_error);
224 static int ap_vendor_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
227 struct wpa_supplicant *wpa_s = ctx;
228 const struct ieee80211_mgmt *mgmt;
231 mgmt = (const struct ieee80211_mgmt *) buf;
232 hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
235 wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
236 mgmt->u.action.category,
237 &mgmt->u.action.u.vs_public_action.action,
238 len - hdr_len, freq);
239 #endif /* CONFIG_P2P */
244 static int ap_probe_req_rx(void *ctx, const u8 *addr, const u8 *ie,
248 struct wpa_supplicant *wpa_s = ctx;
249 return wpas_p2p_probe_req_rx(wpa_s, addr, ie, ie_len);
250 #else /* CONFIG_P2P */
252 #endif /* CONFIG_P2P */
256 static void ap_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
260 struct wpa_supplicant *wpa_s = ctx;
261 wpas_p2p_wps_success(wpa_s, mac_addr, 1);
262 #endif /* CONFIG_P2P */
266 int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
267 struct wpa_ssid *ssid)
269 struct wpa_driver_associate_params params;
270 struct hostapd_iface *hapd_iface;
271 struct hostapd_config *conf;
274 if (ssid->ssid == NULL || ssid->ssid_len == 0) {
275 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
279 wpa_supplicant_ap_deinit(wpa_s);
281 wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
282 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
284 os_memset(¶ms, 0, sizeof(params));
285 params.ssid = ssid->ssid;
286 params.ssid_len = ssid->ssid_len;
287 switch (ssid->mode) {
288 case WPAS_MODE_INFRA:
289 params.mode = IEEE80211_MODE_INFRA;
292 params.mode = IEEE80211_MODE_IBSS;
295 case WPAS_MODE_P2P_GO:
296 case WPAS_MODE_P2P_GROUP_FORMATION:
297 params.mode = IEEE80211_MODE_AP;
300 params.freq = ssid->frequency;
302 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK)
303 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
305 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
306 params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
308 if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
309 wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
310 else if (ssid->pairwise_cipher & WPA_CIPHER_TKIP)
311 wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
312 else if (ssid->pairwise_cipher & WPA_CIPHER_NONE)
313 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
315 wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
319 params.pairwise_suite = cipher_suite2driver(wpa_s->pairwise_cipher);
320 params.group_suite = params.pairwise_suite;
323 if (ssid->mode == WPAS_MODE_P2P_GO ||
324 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
326 wpa_drv_set_intra_bss(wpa_s, wpa_s->conf->p2p_intra_bss);
327 #endif /* CONFIG_P2P */
329 if (wpa_s->parent->set_ap_uapsd)
330 params.uapsd = wpa_s->parent->ap_uapsd;
334 if (wpa_drv_associate(wpa_s, ¶ms) < 0) {
335 wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
339 wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
340 if (hapd_iface == NULL)
342 hapd_iface->owner = wpa_s;
344 wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
346 wpa_supplicant_ap_deinit(wpa_s);
350 if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
351 wpa_printf(MSG_ERROR, "Failed to create AP configuration");
352 wpa_supplicant_ap_deinit(wpa_s);
357 if (ssid->mode == WPAS_MODE_P2P_GO)
358 conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER;
359 else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
360 conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER |
362 #endif /* CONFIG_P2P */
364 hapd_iface->num_bss = conf->num_bss;
365 hapd_iface->bss = os_zalloc(conf->num_bss *
366 sizeof(struct hostapd_data *));
367 if (hapd_iface->bss == NULL) {
368 wpa_supplicant_ap_deinit(wpa_s);
372 for (i = 0; i < conf->num_bss; i++) {
374 hostapd_alloc_bss_data(hapd_iface, conf,
376 if (hapd_iface->bss[i] == NULL) {
377 wpa_supplicant_ap_deinit(wpa_s);
381 hapd_iface->bss[i]->msg_ctx = wpa_s;
382 hapd_iface->bss[i]->public_action_cb = ap_public_action_rx;
383 hapd_iface->bss[i]->public_action_cb_ctx = wpa_s;
384 hapd_iface->bss[i]->vendor_action_cb = ap_vendor_action_rx;
385 hapd_iface->bss[i]->vendor_action_cb_ctx = wpa_s;
386 hostapd_register_probereq_cb(hapd_iface->bss[i],
387 ap_probe_req_rx, wpa_s);
388 hapd_iface->bss[i]->wps_reg_success_cb = ap_wps_reg_success_cb;
389 hapd_iface->bss[i]->wps_reg_success_cb_ctx = wpa_s;
390 hapd_iface->bss[i]->wps_event_cb = ap_wps_event_cb;
391 hapd_iface->bss[i]->wps_event_cb_ctx = wpa_s;
393 hapd_iface->bss[i]->p2p = wpa_s->global->p2p;
394 hapd_iface->bss[i]->p2p_group = wpas_p2p_group_init(
395 wpa_s, ssid->p2p_persistent_group,
396 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION);
397 #endif /* CONFIG_P2P */
400 os_memcpy(hapd_iface->bss[0]->own_addr, wpa_s->own_addr, ETH_ALEN);
401 hapd_iface->bss[0]->driver = wpa_s->driver;
402 hapd_iface->bss[0]->drv_priv = wpa_s->drv_priv;
404 if (hostapd_setup_interface(wpa_s->ap_iface)) {
405 wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
406 wpa_supplicant_ap_deinit(wpa_s);
410 wpa_s->current_ssid = ssid;
411 os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
412 wpa_s->assoc_freq = ssid->frequency;
413 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
415 if (wpa_s->ap_configured_cb)
416 wpa_s->ap_configured_cb(wpa_s->ap_configured_cb_ctx,
417 wpa_s->ap_configured_cb_data);
423 void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
425 if (wpa_s->ap_iface == NULL)
428 wpa_s->current_ssid = NULL;
430 if (wpa_s->ap_iface->bss)
431 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
432 wpas_p2p_group_deinit(wpa_s);
433 #endif /* CONFIG_P2P */
434 hostapd_interface_deinit(wpa_s->ap_iface);
435 hostapd_interface_free(wpa_s->ap_iface);
436 wpa_s->ap_iface = NULL;
437 wpa_drv_deinit_ap(wpa_s);
441 void ap_tx_status(void *ctx, const u8 *addr,
442 const u8 *buf, size_t len, int ack)
445 struct wpa_supplicant *wpa_s = ctx;
446 hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
447 #endif /* NEED_AP_MLME */
451 void ap_rx_from_unknown_sta(void *ctx, const u8 *frame, size_t len)
454 struct wpa_supplicant *wpa_s = ctx;
455 const struct ieee80211_hdr *hdr =
456 (const struct ieee80211_hdr *) frame;
457 u16 fc = le_to_host16(hdr->frame_control);
458 ieee802_11_rx_from_unknown(wpa_s->ap_iface->bss[0], hdr->addr2,
459 (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
460 (WLAN_FC_TODS | WLAN_FC_FROMDS));
461 #endif /* NEED_AP_MLME */
465 void ap_mgmt_rx(void *ctx, struct rx_mgmt *rx_mgmt)
468 struct wpa_supplicant *wpa_s = ctx;
469 struct hostapd_frame_info fi;
470 os_memset(&fi, 0, sizeof(fi));
471 fi.datarate = rx_mgmt->datarate;
472 fi.ssi_signal = rx_mgmt->ssi_signal;
473 ieee802_11_mgmt(wpa_s->ap_iface->bss[0], rx_mgmt->frame,
474 rx_mgmt->frame_len, &fi);
475 #endif /* NEED_AP_MLME */
479 void ap_mgmt_tx_cb(void *ctx, const u8 *buf, size_t len, u16 stype, int ok)
482 struct wpa_supplicant *wpa_s = ctx;
483 ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
484 #endif /* NEED_AP_MLME */
488 void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
489 const u8 *src_addr, const u8 *buf, size_t len)
491 ieee802_1x_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
497 int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
499 if (!wpa_s->ap_iface)
501 return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0]);
505 static int wpa_supplicant_ap_wps_sta_cancel(struct hostapd_data *hapd,
506 struct sta_info *sta, void *ctx)
508 if (sta && (sta->flags & WLAN_STA_WPS)) {
509 ap_sta_deauthenticate(hapd, sta,
510 WLAN_REASON_PREV_AUTH_NOT_VALID);
511 wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
512 __func__, MAC2STR(sta->addr));
520 int wpa_supplicant_ap_wps_cancel(struct wpa_supplicant *wpa_s)
522 struct wps_registrar *reg;
523 int reg_sel = 0, wps_sta = 0;
525 if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0]->wps)
528 reg = wpa_s->ap_iface->bss[0]->wps->registrar;
529 reg_sel = wps_registrar_wps_cancel(reg);
530 wps_sta = ap_for_each_sta(wpa_s->ap_iface->bss[0],
531 wpa_supplicant_ap_wps_sta_cancel, NULL);
533 if (!reg_sel && !wps_sta) {
534 wpa_printf(MSG_DEBUG, "No WPS operation in progress at this "
540 * There are 2 cases to return wps cancel as success:
541 * 1. When wps cancel was initiated but no connection has been
542 * established with client yet.
543 * 2. Client is in the middle of exchanging WPS messages.
550 int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
551 const char *pin, char *buf, size_t buflen)
553 int ret, ret_len = 0;
555 if (!wpa_s->ap_iface)
559 unsigned int rpin = wps_generate_pin();
560 ret_len = os_snprintf(buf, buflen, "%d", rpin);
563 ret_len = os_snprintf(buf, buflen, "%s", pin);
565 ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], bssid, "any", pin,
572 #endif /* CONFIG_WPS */
575 #ifdef CONFIG_CTRL_IFACE
577 int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
578 char *buf, size_t buflen)
580 if (wpa_s->ap_iface == NULL)
582 return hostapd_ctrl_iface_sta_first(wpa_s->ap_iface->bss[0],
587 int ap_ctrl_iface_sta(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(wpa_s->ap_iface->bss[0], txtaddr,
597 int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
598 char *buf, size_t buflen)
600 if (wpa_s->ap_iface == NULL)
602 return hostapd_ctrl_iface_sta_next(wpa_s->ap_iface->bss[0], txtaddr,
607 int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
608 size_t buflen, int verbose)
610 char *pos = buf, *end = buf + buflen;
612 struct hostapd_bss_config *conf;
614 if (wpa_s->ap_iface == NULL)
617 conf = wpa_s->ap_iface->bss[0]->conf;
621 ret = os_snprintf(pos, end - pos,
622 "pairwise_cipher=%s\n"
625 wpa_cipher_txt(conf->rsn_pairwise),
626 wpa_cipher_txt(conf->wpa_group),
627 wpa_key_mgmt_txt(conf->wpa_key_mgmt,
629 if (ret < 0 || ret >= end - pos)
635 #endif /* CONFIG_CTRL_IFACE */
638 int wpa_supplicant_ap_update_beacon(struct wpa_supplicant *wpa_s)
640 struct hostapd_iface *iface = wpa_s->ap_iface;
641 struct wpa_ssid *ssid = wpa_s->current_ssid;
642 struct hostapd_data *hapd;
644 if (ssid == NULL || wpa_s->ap_iface == NULL)
648 if (ssid->mode == WPAS_MODE_P2P_GO)
649 iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER;
650 else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
651 iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER |
653 #endif /* CONFIG_P2P */
655 ieee802_11_set_beacons(iface);
656 hapd = iface->bss[0];
657 hapd->drv.set_ap_wps_ie(hapd);
663 int wpa_supplicant_ap_mac_addr_filter(struct wpa_supplicant *wpa_s,
666 struct hostapd_data *hapd;
667 struct hostapd_bss_config *conf;
669 if (!wpa_s->ap_iface)
673 wpa_printf(MSG_DEBUG, "AP: Set MAC address filter: " MACSTR,
676 wpa_printf(MSG_DEBUG, "AP: Clear MAC address filter");
678 hapd = wpa_s->ap_iface->bss[0];
681 os_free(conf->accept_mac);
682 conf->accept_mac = NULL;
683 conf->num_accept_mac = 0;
684 os_free(conf->deny_mac);
685 conf->deny_mac = NULL;
686 conf->num_deny_mac = 0;
689 conf->macaddr_acl = ACCEPT_UNLESS_DENIED;
693 conf->macaddr_acl = DENY_UNLESS_ACCEPTED;
694 conf->accept_mac = os_zalloc(sizeof(struct mac_acl_entry));
695 if (conf->accept_mac == NULL)
697 os_memcpy(conf->accept_mac[0].addr, addr, ETH_ALEN);
698 conf->num_accept_mac = 1;