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/wps_hostapd.h"
26 #include "ap/ctrl_iface_ap.h"
27 #include "eap_common/eap_defs.h"
28 #include "eap_server/eap_methods.h"
29 #include "eap_common/eap_wsc_common.h"
31 #include "config_ssid.h"
33 #include "wpa_supplicant_i.h"
38 static int wpa_supplicant_conf_ap(struct wpa_supplicant
*wpa_s
,
39 struct wpa_ssid
*ssid
,
40 struct hostapd_config
*conf
)
42 struct hostapd_bss_config
*bss
= &conf
->bss
[0];
45 conf
->driver
= wpa_s
->driver
;
47 os_strlcpy(bss
->iface
, wpa_s
->ifname
, sizeof(bss
->iface
));
49 if (ssid
->frequency
== 0) {
50 /* default channel 11 */
51 conf
->hw_mode
= HOSTAPD_MODE_IEEE80211G
;
53 } else if (ssid
->frequency
>= 2412 && ssid
->frequency
<= 2472) {
54 conf
->hw_mode
= HOSTAPD_MODE_IEEE80211G
;
55 conf
->channel
= (ssid
->frequency
- 2407) / 5;
56 } else if ((ssid
->frequency
>= 5180 && ssid
->frequency
<= 5240) ||
57 (ssid
->frequency
>= 5745 && ssid
->frequency
<= 5825)) {
58 conf
->hw_mode
= HOSTAPD_MODE_IEEE80211A
;
59 conf
->channel
= (ssid
->frequency
- 5000) / 5;
61 wpa_printf(MSG_ERROR
, "Unsupported AP mode frequency: %d MHz",
66 /* TODO: enable HT if driver supports it;
67 * drop to 11b if driver does not support 11g */
69 if (ssid
->ssid_len
== 0) {
70 wpa_printf(MSG_ERROR
, "No SSID configured for AP mode");
73 os_memcpy(bss
->ssid
.ssid
, ssid
->ssid
, ssid
->ssid_len
);
74 bss
->ssid
.ssid
[ssid
->ssid_len
] = '\0';
75 bss
->ssid
.ssid_len
= ssid
->ssid_len
;
76 bss
->ssid
.ssid_set
= 1;
78 if (wpa_key_mgmt_wpa_psk(ssid
->key_mgmt
))
79 bss
->wpa
= ssid
->proto
;
80 bss
->wpa_key_mgmt
= ssid
->key_mgmt
;
81 bss
->wpa_pairwise
= ssid
->pairwise_cipher
;
82 if (ssid
->passphrase
) {
83 bss
->ssid
.wpa_passphrase
= os_strdup(ssid
->passphrase
);
84 } else if (ssid
->psk_set
) {
85 os_free(bss
->ssid
.wpa_psk
);
86 bss
->ssid
.wpa_psk
= os_zalloc(sizeof(struct hostapd_wpa_psk
));
87 if (bss
->ssid
.wpa_psk
== NULL
)
89 os_memcpy(bss
->ssid
.wpa_psk
->psk
, ssid
->psk
, PMK_LEN
);
90 bss
->ssid
.wpa_psk
->group
= 1;
93 /* Select group cipher based on the enabled pairwise cipher suites */
96 pairwise
|= bss
->wpa_pairwise
;
98 if (bss
->rsn_pairwise
== 0)
99 bss
->rsn_pairwise
= bss
->wpa_pairwise
;
100 pairwise
|= bss
->rsn_pairwise
;
102 if (pairwise
& WPA_CIPHER_TKIP
)
103 bss
->wpa_group
= WPA_CIPHER_TKIP
;
105 bss
->wpa_group
= WPA_CIPHER_CCMP
;
107 if (bss
->wpa
&& bss
->ieee802_1x
)
108 bss
->ssid
.security_policy
= SECURITY_WPA
;
110 bss
->ssid
.security_policy
= SECURITY_WPA_PSK
;
111 else if (bss
->ieee802_1x
) {
112 bss
->ssid
.security_policy
= SECURITY_IEEE_802_1X
;
113 bss
->ssid
.wep
.default_len
= bss
->default_wep_key_len
;
114 } else if (bss
->ssid
.wep
.keys_set
)
115 bss
->ssid
.security_policy
= SECURITY_STATIC_WEP
;
117 bss
->ssid
.security_policy
= SECURITY_PLAINTEXT
;
121 * Enable WPS by default, but require user interaction to actually use
122 * it. Only the internal Registrar is supported.
126 bss
->ap_setup_locked
= 1;
127 if (wpa_s
->conf
->config_methods
)
128 bss
->config_methods
= os_strdup(wpa_s
->conf
->config_methods
);
129 if (wpa_s
->conf
->device_type
)
130 bss
->device_type
= os_strdup(wpa_s
->conf
->device_type
);
131 #endif /* CONFIG_WPS */
137 int wpa_supplicant_create_ap(struct wpa_supplicant
*wpa_s
,
138 struct wpa_ssid
*ssid
)
140 struct wpa_driver_associate_params params
;
141 struct hostapd_iface
*hapd_iface
;
142 struct hostapd_config
*conf
;
145 if (ssid
->ssid
== NULL
|| ssid
->ssid_len
== 0) {
146 wpa_printf(MSG_ERROR
, "No SSID configured for AP mode");
150 wpa_supplicant_ap_deinit(wpa_s
);
152 wpa_printf(MSG_DEBUG
, "Setting up AP (SSID='%s')",
153 wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
));
155 os_memset(¶ms
, 0, sizeof(params
));
156 params
.ssid
= ssid
->ssid
;
157 params
.ssid_len
= ssid
->ssid_len
;
158 switch (ssid
->mode
) {
159 case WPAS_MODE_INFRA
:
160 params
.mode
= IEEE80211_MODE_INFRA
;
163 params
.mode
= IEEE80211_MODE_IBSS
;
166 params
.mode
= IEEE80211_MODE_AP
;
169 params
.freq
= ssid
->frequency
;
171 if (wpa_drv_associate(wpa_s
, ¶ms
) < 0) {
172 wpa_msg(wpa_s
, MSG_INFO
, "Failed to start AP functionality");
176 wpa_s
->ap_iface
= hapd_iface
= os_zalloc(sizeof(*wpa_s
->ap_iface
));
177 if (hapd_iface
== NULL
)
179 hapd_iface
->owner
= wpa_s
;
181 wpa_s
->ap_iface
->conf
= conf
= hostapd_config_defaults();
183 wpa_supplicant_ap_deinit(wpa_s
);
187 if (wpa_supplicant_conf_ap(wpa_s
, ssid
, conf
)) {
188 wpa_printf(MSG_ERROR
, "Failed to create AP configuration");
189 wpa_supplicant_ap_deinit(wpa_s
);
193 hapd_iface
->num_bss
= conf
->num_bss
;
194 hapd_iface
->bss
= os_zalloc(conf
->num_bss
*
195 sizeof(struct hostapd_data
*));
196 if (hapd_iface
->bss
== NULL
) {
197 wpa_supplicant_ap_deinit(wpa_s
);
201 for (i
= 0; i
< conf
->num_bss
; i
++) {
203 hostapd_alloc_bss_data(hapd_iface
, conf
,
205 if (hapd_iface
->bss
[i
] == NULL
) {
206 wpa_supplicant_ap_deinit(wpa_s
);
210 hapd_iface
->bss
[i
]->msg_ctx
= wpa_s
;
213 os_memcpy(hapd_iface
->bss
[0]->own_addr
, wpa_s
->own_addr
, ETH_ALEN
);
214 hapd_iface
->bss
[0]->driver
= wpa_s
->driver
;
215 hapd_iface
->bss
[0]->drv_priv
= wpa_s
->drv_priv
;
217 if (hostapd_setup_interface(wpa_s
->ap_iface
)) {
218 wpa_printf(MSG_ERROR
, "Failed to initialize AP interface");
219 wpa_supplicant_ap_deinit(wpa_s
);
223 wpa_s
->current_ssid
= ssid
;
224 os_memcpy(wpa_s
->bssid
, wpa_s
->own_addr
, ETH_ALEN
);
225 wpa_supplicant_set_state(wpa_s
, WPA_COMPLETED
);
231 void wpa_supplicant_ap_deinit(struct wpa_supplicant
*wpa_s
)
233 if (wpa_s
->ap_iface
== NULL
)
236 hostapd_interface_deinit(wpa_s
->ap_iface
);
237 hostapd_interface_free(wpa_s
->ap_iface
);
238 wpa_s
->ap_iface
= NULL
;
242 void ap_tx_status(void *ctx
, const u8
*addr
,
243 const u8
*buf
, size_t len
, int ack
)
246 struct wpa_supplicant
*wpa_s
= ctx
;
247 hostapd_tx_status(wpa_s
->ap_iface
->bss
[0], addr
, buf
, len
, ack
);
248 #endif /* NEED_AP_MLME */
252 void ap_rx_from_unknown_sta(void *ctx
, const struct ieee80211_hdr
*hdr
,
256 struct wpa_supplicant
*wpa_s
= ctx
;
257 u16 fc
= le_to_host16(hdr
->frame_control
);
258 ieee802_11_rx_from_unknown(wpa_s
->ap_iface
->bss
[0], hdr
->addr2
,
259 (fc
& (WLAN_FC_TODS
| WLAN_FC_FROMDS
)) ==
260 (WLAN_FC_TODS
| WLAN_FC_FROMDS
));
261 #endif /* NEED_AP_MLME */
265 void ap_mgmt_rx(void *ctx
, const u8
*buf
, size_t len
,
266 struct hostapd_frame_info
*fi
)
269 struct wpa_supplicant
*wpa_s
= ctx
;
270 ieee802_11_mgmt(wpa_s
->ap_iface
->bss
[0], buf
, len
, fi
);
271 #endif /* NEED_AP_MLME */
275 void ap_mgmt_tx_cb(void *ctx
, const u8
*buf
, size_t len
, u16 stype
, int ok
)
278 struct wpa_supplicant
*wpa_s
= ctx
;
279 ieee802_11_mgmt_cb(wpa_s
->ap_iface
->bss
[0], buf
, len
, stype
, ok
);
280 #endif /* NEED_AP_MLME */
284 void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant
*wpa_s
,
285 const u8
*src_addr
, const u8
*buf
, size_t len
)
287 hostapd_eapol_receive(wpa_s
->ap_iface
->bss
[0], src_addr
, buf
, len
);
293 int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant
*wpa_s
, const u8
*bssid
)
295 return hostapd_wps_button_pushed(wpa_s
->ap_iface
->bss
[0]);
299 int wpa_supplicant_ap_wps_pin(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
300 const char *pin
, char *buf
, size_t buflen
)
302 int ret
, ret_len
= 0;
305 unsigned int rpin
= wps_generate_pin();
306 ret_len
= os_snprintf(buf
, buflen
, "%d", rpin
);
310 ret
= hostapd_wps_add_pin(wpa_s
->ap_iface
->bss
[0], "any", pin
, 0);
316 #endif /* CONFIG_WPS */
319 #ifdef CONFIG_CTRL_IFACE
321 int ap_ctrl_iface_sta_first(struct wpa_supplicant
*wpa_s
,
322 char *buf
, size_t buflen
)
324 if (wpa_s
->ap_iface
== NULL
)
326 return hostapd_ctrl_iface_sta_first(wpa_s
->ap_iface
->bss
[0],
331 int ap_ctrl_iface_sta(struct wpa_supplicant
*wpa_s
, const char *txtaddr
,
332 char *buf
, size_t buflen
)
334 if (wpa_s
->ap_iface
== NULL
)
336 return hostapd_ctrl_iface_sta(wpa_s
->ap_iface
->bss
[0], txtaddr
,
341 int ap_ctrl_iface_sta_next(struct wpa_supplicant
*wpa_s
, const char *txtaddr
,
342 char *buf
, size_t buflen
)
344 if (wpa_s
->ap_iface
== NULL
)
346 return hostapd_ctrl_iface_sta_next(wpa_s
->ap_iface
->bss
[0], txtaddr
,
351 int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant
*wpa_s
, char *buf
,
352 size_t buflen
, int verbose
)
354 char *pos
= buf
, *end
= buf
+ buflen
;
356 struct hostapd_bss_config
*conf
;
358 if (wpa_s
->ap_iface
== NULL
)
361 conf
= wpa_s
->ap_iface
->bss
[0]->conf
;
365 ret
= os_snprintf(pos
, end
- pos
,
366 "pairwise_cipher=%s\n"
369 wpa_cipher_txt(conf
->rsn_pairwise
),
370 wpa_cipher_txt(conf
->wpa_group
),
371 wpa_key_mgmt_txt(conf
->wpa_key_mgmt
,
373 if (ret
< 0 || ret
>= end
- pos
)
379 #endif /* CONFIG_CTRL_IFACE */