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
,
199 int dtim_period
, int beacon_int
)
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
,
204 dtim_period
, beacon_int
);
208 static int ap_driver_set_cts_protect(void *priv
, int value
)
210 wpa_printf(MSG_DEBUG
, "AP TODO: %s", __func__
);
215 static int ap_driver_set_preamble(void *priv
, int value
)
217 wpa_printf(MSG_DEBUG
, "AP TODO: %s", __func__
);
222 static int ap_driver_set_short_slot_time(void *priv
, int value
)
224 wpa_printf(MSG_DEBUG
, "AP TODO: %s", __func__
);
229 static int ap_driver_set_tx_queue_params(void *priv
, int queue
, int aifs
,
230 int cw_min
, int cw_max
,
233 wpa_printf(MSG_DEBUG
, "AP TODO: %s", __func__
);
238 static struct hostapd_hw_modes
*ap_driver_get_hw_feature_data(void *priv
,
242 struct ap_driver_data
*drv
= priv
;
243 struct wpa_supplicant
*wpa_s
= drv
->hapd
->iface
->owner
;
244 return wpa_drv_get_hw_feature_data(wpa_s
, num_modes
, flags
);
248 static int ap_driver_hapd_send_eapol(void *priv
, const u8
*addr
,
249 const u8
*data
, size_t data_len
,
250 int encrypt
, const u8
*own_addr
)
252 struct ap_driver_data
*drv
= priv
;
253 struct wpa_supplicant
*wpa_s
= drv
->hapd
->iface
->owner
;
254 return wpa_drv_hapd_send_eapol(wpa_s
, addr
, data
, data_len
, encrypt
,
259 struct wpa_driver_ops ap_driver_ops
=
261 .name
= "wpa_supplicant",
262 .hapd_init
= ap_driver_init
,
263 .hapd_deinit
= ap_driver_deinit
,
264 .send_ether
= ap_driver_send_ether
,
265 .set_key
= ap_driver_set_key
,
266 .get_seqnum
= ap_driver_get_seqnum
,
267 .flush
= ap_driver_flush
,
268 .read_sta_data
= ap_driver_read_sta_data
,
269 .sta_set_flags
= ap_driver_sta_set_flags
,
270 .sta_deauth
= ap_driver_sta_deauth
,
271 .sta_disassoc
= ap_driver_sta_disassoc
,
272 .sta_remove
= ap_driver_sta_remove
,
273 .send_mlme
= ap_driver_send_mlme
,
274 .sta_add
= ap_driver_sta_add
,
275 .get_inact_sec
= ap_driver_get_inact_sec
,
276 .set_freq
= ap_driver_set_freq
,
277 .set_beacon
= ap_driver_set_beacon
,
278 .set_cts_protect
= ap_driver_set_cts_protect
,
279 .set_preamble
= ap_driver_set_preamble
,
280 .set_short_slot_time
= ap_driver_set_short_slot_time
,
281 .set_tx_queue_params
= ap_driver_set_tx_queue_params
,
282 .get_hw_feature_data
= ap_driver_get_hw_feature_data
,
283 .hapd_send_eapol
= ap_driver_hapd_send_eapol
,
287 extern struct wpa_driver_ops
*wpa_drivers
[];
289 static int wpa_supplicant_conf_ap(struct wpa_supplicant
*wpa_s
,
290 struct wpa_ssid
*ssid
,
291 struct hostapd_config
*conf
)
293 struct hostapd_bss_config
*bss
= &conf
->bss
[0];
296 for (j
= 0; wpa_drivers
[j
]; j
++) {
297 if (os_strcmp("wpa_supplicant", wpa_drivers
[j
]->name
) == 0) {
298 conf
->driver
= wpa_drivers
[j
];
302 if (conf
->driver
== NULL
) {
303 wpa_printf(MSG_ERROR
, "No AP driver ops found");
307 os_strlcpy(bss
->iface
, wpa_s
->ifname
, sizeof(bss
->iface
));
309 if (ssid
->frequency
== 0) {
310 /* default channel 11 */
311 conf
->hw_mode
= HOSTAPD_MODE_IEEE80211G
;
313 } else if (ssid
->frequency
>= 2412 && ssid
->frequency
<= 2472) {
314 conf
->hw_mode
= HOSTAPD_MODE_IEEE80211G
;
315 conf
->channel
= (ssid
->frequency
- 2407) / 5;
316 } else if ((ssid
->frequency
>= 5180 && ssid
->frequency
<= 5240) ||
317 (ssid
->frequency
>= 5745 && ssid
->frequency
<= 5825)) {
318 conf
->hw_mode
= HOSTAPD_MODE_IEEE80211A
;
319 conf
->channel
= (ssid
->frequency
- 5000) / 5;
321 wpa_printf(MSG_ERROR
, "Unsupported AP mode frequency: %d MHz",
326 /* TODO: enable HT if driver supports it;
327 * drop to 11b if driver does not support 11g */
329 if (ssid
->ssid_len
== 0) {
330 wpa_printf(MSG_ERROR
, "No SSID configured for AP mode");
333 os_memcpy(bss
->ssid
.ssid
, ssid
->ssid
, ssid
->ssid_len
);
334 bss
->ssid
.ssid
[ssid
->ssid_len
] = '\0';
335 bss
->ssid
.ssid_len
= ssid
->ssid_len
;
336 bss
->ssid
.ssid_set
= 1;
338 if (wpa_key_mgmt_wpa_psk(ssid
->key_mgmt
))
339 bss
->wpa
= ssid
->proto
;
340 bss
->wpa_key_mgmt
= ssid
->key_mgmt
;
341 bss
->wpa_pairwise
= ssid
->pairwise_cipher
;
342 if (ssid
->passphrase
) {
343 bss
->ssid
.wpa_passphrase
= os_strdup(ssid
->passphrase
);
344 } else if (ssid
->psk_set
) {
345 os_free(bss
->ssid
.wpa_psk
);
346 bss
->ssid
.wpa_psk
= os_zalloc(sizeof(struct hostapd_wpa_psk
));
347 if (bss
->ssid
.wpa_psk
== NULL
)
349 os_memcpy(bss
->ssid
.wpa_psk
->psk
, ssid
->psk
, PMK_LEN
);
350 bss
->ssid
.wpa_psk
->group
= 1;
353 /* Select group cipher based on the enabled pairwise cipher suites */
356 pairwise
|= bss
->wpa_pairwise
;
358 if (bss
->rsn_pairwise
== 0)
359 bss
->rsn_pairwise
= bss
->wpa_pairwise
;
360 pairwise
|= bss
->rsn_pairwise
;
362 if (pairwise
& WPA_CIPHER_TKIP
)
363 bss
->wpa_group
= WPA_CIPHER_TKIP
;
365 bss
->wpa_group
= WPA_CIPHER_CCMP
;
367 if (bss
->wpa
&& bss
->ieee802_1x
)
368 bss
->ssid
.security_policy
= SECURITY_WPA
;
370 bss
->ssid
.security_policy
= SECURITY_WPA_PSK
;
371 else if (bss
->ieee802_1x
) {
372 bss
->ssid
.security_policy
= SECURITY_IEEE_802_1X
;
373 bss
->ssid
.wep
.default_len
= bss
->default_wep_key_len
;
374 } else if (bss
->ssid
.wep
.keys_set
)
375 bss
->ssid
.security_policy
= SECURITY_STATIC_WEP
;
377 bss
->ssid
.security_policy
= SECURITY_PLAINTEXT
;
381 * Enable WPS by default, but require user interaction to actually use
382 * it. Only the internal Registrar is supported.
386 bss
->ap_setup_locked
= 1;
387 bss
->config_methods
= os_strdup("display push_button");
388 #endif /* CONFIG_WPS */
394 int wpa_supplicant_create_ap(struct wpa_supplicant
*wpa_s
,
395 struct wpa_ssid
*ssid
)
397 struct wpa_driver_associate_params params
;
398 struct hostapd_iface
*hapd_iface
;
399 struct hostapd_config
*conf
;
402 if (ssid
->ssid
== NULL
|| ssid
->ssid_len
== 0) {
403 wpa_printf(MSG_ERROR
, "No SSID configured for AP mode");
407 wpa_supplicant_ap_deinit(wpa_s
);
409 wpa_printf(MSG_DEBUG
, "Setting up AP (SSID='%s')",
410 wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
));
412 os_memset(¶ms
, 0, sizeof(params
));
413 params
.ssid
= ssid
->ssid
;
414 params
.ssid_len
= ssid
->ssid_len
;
415 params
.mode
= ssid
->mode
;
416 params
.freq
= ssid
->frequency
;
418 if (wpa_drv_associate(wpa_s
, ¶ms
) < 0) {
419 wpa_msg(wpa_s
, MSG_INFO
, "Failed to start AP functionality");
423 wpa_s
->ap_iface
= hapd_iface
= os_zalloc(sizeof(*wpa_s
->ap_iface
));
424 if (hapd_iface
== NULL
)
426 hapd_iface
->owner
= wpa_s
;
428 wpa_s
->ap_iface
->conf
= conf
= hostapd_config_defaults();
430 wpa_supplicant_ap_deinit(wpa_s
);
434 if (wpa_supplicant_conf_ap(wpa_s
, ssid
, conf
)) {
435 wpa_printf(MSG_ERROR
, "Failed to create AP configuration");
436 wpa_supplicant_ap_deinit(wpa_s
);
440 hapd_iface
->num_bss
= conf
->num_bss
;
441 hapd_iface
->bss
= os_zalloc(conf
->num_bss
*
442 sizeof(struct hostapd_data
*));
443 if (hapd_iface
->bss
== NULL
) {
444 wpa_supplicant_ap_deinit(wpa_s
);
448 for (i
= 0; i
< conf
->num_bss
; i
++) {
450 hostapd_alloc_bss_data(hapd_iface
, conf
,
452 if (hapd_iface
->bss
[i
] == NULL
) {
453 wpa_supplicant_ap_deinit(wpa_s
);
457 hapd_iface
->bss
[i
]->msg_ctx
= wpa_s
;
460 if (hostapd_setup_interface(wpa_s
->ap_iface
)) {
461 wpa_printf(MSG_ERROR
, "Failed to initialize AP interface");
462 wpa_supplicant_ap_deinit(wpa_s
);
466 wpa_s
->current_ssid
= ssid
;
467 os_memcpy(wpa_s
->bssid
, wpa_s
->own_addr
, ETH_ALEN
);
468 wpa_supplicant_set_state(wpa_s
, WPA_COMPLETED
);
474 void wpa_supplicant_ap_deinit(struct wpa_supplicant
*wpa_s
)
476 if (wpa_s
->ap_iface
== NULL
)
479 hostapd_interface_deinit(wpa_s
->ap_iface
);
480 wpa_s
->ap_iface
= NULL
;
484 void ap_tx_status(void *ctx
, const u8
*addr
,
485 const u8
*buf
, size_t len
, int ack
)
488 struct wpa_supplicant
*wpa_s
= ctx
;
489 hostapd_tx_status(wpa_s
->ap_iface
->bss
[0], addr
, buf
, len
, ack
);
490 #endif /* NEED_AP_MLME */
494 void ap_rx_from_unknown_sta(void *ctx
, const struct ieee80211_hdr
*hdr
,
498 struct wpa_supplicant
*wpa_s
= ctx
;
499 ieee802_11_rx_from_unknown(wpa_s
->ap_iface
->bss
[0], hdr
->addr2
);
500 #endif /* NEED_AP_MLME */
504 void ap_mgmt_rx(void *ctx
, u8
*buf
, size_t len
,
505 struct hostapd_frame_info
*fi
)
508 struct wpa_supplicant
*wpa_s
= ctx
;
509 ieee802_11_mgmt(wpa_s
->ap_iface
->bss
[0], buf
, len
, fi
);
510 #endif /* NEED_AP_MLME */
514 void ap_mgmt_tx_cb(void *ctx
, const u8
*buf
, size_t len
, u16 stype
, int ok
)
517 struct wpa_supplicant
*wpa_s
= ctx
;
518 ieee802_11_mgmt_cb(wpa_s
->ap_iface
->bss
[0], buf
, len
, stype
, ok
);
519 #endif /* NEED_AP_MLME */
523 void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant
*wpa_s
,
524 const u8
*src_addr
, const u8
*buf
, size_t len
)
526 hostapd_eapol_receive(wpa_s
->ap_iface
->bss
[0], src_addr
, buf
, len
);
532 int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant
*wpa_s
, const u8
*bssid
)
534 return hostapd_wps_button_pushed(wpa_s
->ap_iface
->bss
[0]);
538 int wpa_supplicant_ap_wps_pin(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
539 const char *pin
, char *buf
, size_t buflen
)
541 int ret
, ret_len
= 0;
544 unsigned int rpin
= wps_generate_pin();
545 ret_len
= os_snprintf(buf
, buflen
, "%d", rpin
);
549 ret
= hostapd_wps_add_pin(wpa_s
->ap_iface
->bss
[0], "any", pin
, 0);
555 #endif /* CONFIG_WPS */
558 #ifdef CONFIG_CTRL_IFACE
560 int ap_ctrl_iface_sta_first(struct wpa_supplicant
*wpa_s
,
561 char *buf
, size_t buflen
)
563 if (wpa_s
->ap_iface
== NULL
)
565 return hostapd_ctrl_iface_sta_first(wpa_s
->ap_iface
->bss
[0],
570 int ap_ctrl_iface_sta(struct wpa_supplicant
*wpa_s
, const char *txtaddr
,
571 char *buf
, size_t buflen
)
573 if (wpa_s
->ap_iface
== NULL
)
575 return hostapd_ctrl_iface_sta(wpa_s
->ap_iface
->bss
[0], txtaddr
,
580 int ap_ctrl_iface_sta_next(struct wpa_supplicant
*wpa_s
, const char *txtaddr
,
581 char *buf
, size_t buflen
)
583 if (wpa_s
->ap_iface
== NULL
)
585 return hostapd_ctrl_iface_sta_next(wpa_s
->ap_iface
->bss
[0], txtaddr
,
590 int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant
*wpa_s
, char *buf
,
591 size_t buflen
, int verbose
)
593 char *pos
= buf
, *end
= buf
+ buflen
;
595 struct hostapd_bss_config
*conf
;
597 if (wpa_s
->ap_iface
== NULL
)
600 conf
= wpa_s
->ap_iface
->bss
[0]->conf
;
604 ret
= os_snprintf(pos
, end
- pos
,
605 "pairwise_cipher=%s\n"
608 wpa_cipher_txt(conf
->rsn_pairwise
),
609 wpa_cipher_txt(conf
->wpa_group
),
610 wpa_key_mgmt_txt(conf
->wpa_key_mgmt
,
612 if (ret
< 0 || ret
>= end
- pos
)
618 #endif /* CONFIG_CTRL_IFACE */