2 * hostapd - Driver operations
3 * Copyright (c) 2009-2010, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
15 #include "utils/includes.h"
17 #include "utils/common.h"
18 #include "drivers/driver.h"
19 #include "common/ieee802_11_defs.h"
22 #include "ieee802_11.h"
24 #include "ap_config.h"
25 #include "p2p_hostapd.h"
26 #include "ap_drv_ops.h"
29 u32 hostapd_sta_flags_to_drv(u32 flags)
32 if (flags & WLAN_STA_AUTHORIZED)
33 res |= WPA_STA_AUTHORIZED;
34 if (flags & WLAN_STA_WMM)
36 if (flags & WLAN_STA_SHORT_PREAMBLE)
37 res |= WPA_STA_SHORT_PREAMBLE;
38 if (flags & WLAN_STA_MFP)
44 int hostapd_build_ap_extra_ies(struct hostapd_data *hapd,
45 struct wpabuf **beacon_ret,
46 struct wpabuf **proberesp_ret,
47 struct wpabuf **assocresp_ret)
49 struct wpabuf *beacon = NULL, *proberesp = NULL, *assocresp = NULL;
52 *beacon_ret = *proberesp_ret = *assocresp_ret = NULL;
55 pos = hostapd_eid_ext_capab(hapd, pos);
57 if (wpabuf_resize(&assocresp, pos - buf) != 0)
59 wpabuf_put_data(assocresp, buf, pos - buf);
61 pos = hostapd_eid_interworking(hapd, pos);
62 pos = hostapd_eid_adv_proto(hapd, pos);
63 pos = hostapd_eid_roaming_consortium(hapd, pos);
65 if (wpabuf_resize(&beacon, pos - buf) != 0)
67 wpabuf_put_data(beacon, buf, pos - buf);
69 if (wpabuf_resize(&proberesp, pos - buf) != 0)
71 wpabuf_put_data(proberesp, buf, pos - buf);
74 if (hapd->wps_beacon_ie) {
75 if (wpabuf_resize(&beacon, wpabuf_len(hapd->wps_beacon_ie)) <
78 wpabuf_put_buf(beacon, hapd->wps_beacon_ie);
81 if (hapd->wps_probe_resp_ie) {
82 if (wpabuf_resize(&proberesp,
83 wpabuf_len(hapd->wps_probe_resp_ie)) < 0)
85 wpabuf_put_buf(proberesp, hapd->wps_probe_resp_ie);
89 if (hapd->p2p_beacon_ie) {
90 if (wpabuf_resize(&beacon, wpabuf_len(hapd->p2p_beacon_ie)) <
93 wpabuf_put_buf(beacon, hapd->p2p_beacon_ie);
96 if (hapd->p2p_probe_resp_ie) {
97 if (wpabuf_resize(&beacon, wpabuf_len(hapd->p2p_probe_resp_ie))
100 wpabuf_put_buf(beacon, hapd->p2p_probe_resp_ie);
102 #endif /* CONFIG_P2P */
104 #ifdef CONFIG_P2P_MANAGER
105 if (hapd->conf->p2p & P2P_MANAGE) {
106 if (wpabuf_resize(&beacon, 100) == 0) {
108 start = wpabuf_put(beacon, 0);
109 p = hostapd_eid_p2p_manage(hapd, start);
110 wpabuf_put(beacon, p - start);
113 if (wpabuf_resize(&proberesp, 100) == 0) {
115 start = wpabuf_put(proberesp, 0);
116 p = hostapd_eid_p2p_manage(hapd, start);
117 wpabuf_put(proberesp, p - start);
120 #endif /* CONFIG_P2P_MANAGER */
123 if (hapd->conf->wps_state) {
124 struct wpabuf *a = wps_build_assoc_resp_ie();
125 if (a && wpabuf_resize(&assocresp, wpabuf_len(a)) == 0)
126 wpabuf_put_buf(assocresp, a);
129 #endif /* CONFIG_WPS2 */
131 #ifdef CONFIG_P2P_MANAGER
132 if (hapd->conf->p2p & P2P_MANAGE) {
133 if (wpabuf_resize(&assocresp, 100) == 0) {
135 start = wpabuf_put(assocresp, 0);
136 p = hostapd_eid_p2p_manage(hapd, start);
137 wpabuf_put(assocresp, p - start);
140 #endif /* CONFIG_P2P_MANAGER */
142 *beacon_ret = beacon;
143 *proberesp_ret = proberesp;
144 *assocresp_ret = assocresp;
150 wpabuf_free(proberesp);
151 wpabuf_free(assocresp);
156 void hostapd_free_ap_extra_ies(struct hostapd_data *hapd,
157 struct wpabuf *beacon,
158 struct wpabuf *proberesp,
159 struct wpabuf *assocresp)
162 wpabuf_free(proberesp);
163 wpabuf_free(assocresp);
167 int hostapd_set_ap_wps_ie(struct hostapd_data *hapd)
169 struct wpabuf *beacon, *proberesp, *assocresp;
172 if (hapd->driver == NULL || hapd->driver->set_ap_wps_ie == NULL)
175 if (hostapd_build_ap_extra_ies(hapd, &beacon, &proberesp, &assocresp) <
179 ret = hapd->driver->set_ap_wps_ie(hapd->drv_priv, beacon, proberesp,
182 hostapd_free_ap_extra_ies(hapd, beacon, proberesp, assocresp);
188 int hostapd_set_authorized(struct hostapd_data *hapd,
189 struct sta_info *sta, int authorized)
192 return hostapd_sta_set_flags(hapd, sta->addr,
193 hostapd_sta_flags_to_drv(
195 WPA_STA_AUTHORIZED, ~0);
198 return hostapd_sta_set_flags(hapd, sta->addr,
199 hostapd_sta_flags_to_drv(sta->flags),
200 0, ~WPA_STA_AUTHORIZED);
204 int hostapd_set_sta_flags(struct hostapd_data *hapd, struct sta_info *sta)
206 int set_flags, total_flags, flags_and, flags_or;
207 total_flags = hostapd_sta_flags_to_drv(sta->flags);
208 set_flags = WPA_STA_SHORT_PREAMBLE | WPA_STA_WMM | WPA_STA_MFP;
209 if (((!hapd->conf->ieee802_1x && !hapd->conf->wpa) ||
210 sta->auth_alg == WLAN_AUTH_FT) &&
211 sta->flags & WLAN_STA_AUTHORIZED)
212 set_flags |= WPA_STA_AUTHORIZED;
213 flags_or = total_flags & set_flags;
214 flags_and = total_flags | ~set_flags;
215 return hostapd_sta_set_flags(hapd, sta->addr, total_flags,
216 flags_or, flags_and);
220 int hostapd_set_drv_ieee8021x(struct hostapd_data *hapd, const char *ifname,
223 struct wpa_bss_params params;
224 os_memset(¶ms, 0, sizeof(params));
225 params.ifname = ifname;
226 params.enabled = enabled;
228 params.wpa = hapd->conf->wpa;
229 params.ieee802_1x = hapd->conf->ieee802_1x;
230 params.wpa_group = hapd->conf->wpa_group;
231 params.wpa_pairwise = hapd->conf->wpa_pairwise;
232 params.wpa_key_mgmt = hapd->conf->wpa_key_mgmt;
233 params.rsn_preauth = hapd->conf->rsn_preauth;
234 #ifdef CONFIG_IEEE80211W
235 params.ieee80211w = hapd->conf->ieee80211w;
236 #endif /* CONFIG_IEEE80211W */
238 return hostapd_set_ieee8021x(hapd, ¶ms);
242 int hostapd_vlan_if_add(struct hostapd_data *hapd, const char *ifname)
244 char force_ifname[IFNAMSIZ];
245 u8 if_addr[ETH_ALEN];
246 return hostapd_if_add(hapd, WPA_IF_AP_VLAN, ifname, hapd->own_addr,
247 NULL, NULL, force_ifname, if_addr, NULL);
251 int hostapd_vlan_if_remove(struct hostapd_data *hapd, const char *ifname)
253 return hostapd_if_remove(hapd, WPA_IF_AP_VLAN, ifname);
257 int hostapd_set_wds_sta(struct hostapd_data *hapd, const u8 *addr, int aid,
260 const char *bridge = NULL;
262 if (hapd->driver == NULL || hapd->driver->set_wds_sta == NULL)
264 if (hapd->conf->wds_bridge[0])
265 bridge = hapd->conf->wds_bridge;
266 else if (hapd->conf->bridge[0])
267 bridge = hapd->conf->bridge;
268 return hapd->driver->set_wds_sta(hapd->drv_priv, addr, aid, val,
273 int hostapd_add_sta_node(struct hostapd_data *hapd, const u8 *addr,
276 if (hapd->driver == NULL || hapd->driver->add_sta_node == NULL)
278 return hapd->driver->add_sta_node(hapd->drv_priv, addr, auth_alg);
282 int hostapd_sta_auth(struct hostapd_data *hapd, const u8 *addr,
283 u16 seq, u16 status, const u8 *ie, size_t len)
285 if (hapd->driver == NULL || hapd->driver->sta_auth == NULL)
287 return hapd->driver->sta_auth(hapd->drv_priv, hapd->own_addr, addr,
288 seq, status, ie, len);
292 int hostapd_sta_assoc(struct hostapd_data *hapd, const u8 *addr,
293 int reassoc, u16 status, const u8 *ie, size_t len)
295 if (hapd->driver == NULL || hapd->driver->sta_assoc == NULL)
297 return hapd->driver->sta_assoc(hapd->drv_priv, hapd->own_addr, addr,
298 reassoc, status, ie, len);
302 int hostapd_sta_add(struct hostapd_data *hapd,
303 const u8 *addr, u16 aid, u16 capability,
304 const u8 *supp_rates, size_t supp_rates_len,
306 const struct ieee80211_ht_capabilities *ht_capab,
309 struct hostapd_sta_add_params params;
311 if (hapd->driver == NULL)
313 if (hapd->driver->sta_add == NULL)
316 os_memset(¶ms, 0, sizeof(params));
319 params.capability = capability;
320 params.supp_rates = supp_rates;
321 params.supp_rates_len = supp_rates_len;
322 params.listen_interval = listen_interval;
323 params.ht_capabilities = ht_capab;
324 params.flags = hostapd_sta_flags_to_drv(flags);
325 return hapd->driver->sta_add(hapd->drv_priv, ¶ms);
329 int hostapd_add_tspec(struct hostapd_data *hapd, const u8 *addr,
330 u8 *tspec_ie, size_t tspec_ielen)
332 if (hapd->driver == NULL || hapd->driver->add_tspec == NULL)
334 return hapd->driver->add_tspec(hapd->drv_priv, addr, tspec_ie,
339 int hostapd_set_privacy(struct hostapd_data *hapd, int enabled)
341 if (hapd->driver == NULL || hapd->driver->set_privacy == NULL)
343 return hapd->driver->set_privacy(hapd->drv_priv, enabled);
347 int hostapd_set_generic_elem(struct hostapd_data *hapd, const u8 *elem,
350 if (hapd->driver == NULL || hapd->driver->set_generic_elem == NULL)
352 return hapd->driver->set_generic_elem(hapd->drv_priv, elem, elem_len);
356 int hostapd_get_ssid(struct hostapd_data *hapd, u8 *buf, size_t len)
358 if (hapd->driver == NULL || hapd->driver->hapd_get_ssid == NULL)
360 return hapd->driver->hapd_get_ssid(hapd->drv_priv, buf, len);
364 int hostapd_set_ssid(struct hostapd_data *hapd, const u8 *buf, size_t len)
366 if (hapd->driver == NULL || hapd->driver->hapd_set_ssid == NULL)
368 return hapd->driver->hapd_set_ssid(hapd->drv_priv, buf, len);
372 int hostapd_if_add(struct hostapd_data *hapd, enum wpa_driver_if_type type,
373 const char *ifname, const u8 *addr, void *bss_ctx,
374 void **drv_priv, char *force_ifname, u8 *if_addr,
377 if (hapd->driver == NULL || hapd->driver->if_add == NULL)
379 return hapd->driver->if_add(hapd->drv_priv, type, ifname, addr,
380 bss_ctx, drv_priv, force_ifname, if_addr,
385 int hostapd_if_remove(struct hostapd_data *hapd, enum wpa_driver_if_type type,
388 if (hapd->driver == NULL || hapd->driver->if_remove == NULL)
390 return hapd->driver->if_remove(hapd->drv_priv, type, ifname);
394 int hostapd_set_ieee8021x(struct hostapd_data *hapd,
395 struct wpa_bss_params *params)
397 if (hapd->driver == NULL || hapd->driver->set_ieee8021x == NULL)
399 return hapd->driver->set_ieee8021x(hapd->drv_priv, params);
403 int hostapd_get_seqnum(const char *ifname, struct hostapd_data *hapd,
404 const u8 *addr, int idx, u8 *seq)
406 if (hapd->driver == NULL || hapd->driver->get_seqnum == NULL)
408 return hapd->driver->get_seqnum(ifname, hapd->drv_priv, addr, idx,
413 int hostapd_flush(struct hostapd_data *hapd)
415 if (hapd->driver == NULL || hapd->driver->flush == NULL)
417 return hapd->driver->flush(hapd->drv_priv);
421 int hostapd_set_freq(struct hostapd_data *hapd, int mode, int freq,
422 int channel, int ht_enabled, int sec_channel_offset)
424 struct hostapd_freq_params data;
425 if (hapd->driver == NULL)
427 if (hapd->driver->set_freq == NULL)
429 os_memset(&data, 0, sizeof(data));
432 data.channel = channel;
433 data.ht_enabled = ht_enabled;
434 data.sec_channel_offset = sec_channel_offset;
435 return hapd->driver->set_freq(hapd->drv_priv, &data);
438 int hostapd_set_rts(struct hostapd_data *hapd, int rts)
440 if (hapd->driver == NULL || hapd->driver->set_rts == NULL)
442 return hapd->driver->set_rts(hapd->drv_priv, rts);
446 int hostapd_set_frag(struct hostapd_data *hapd, int frag)
448 if (hapd->driver == NULL || hapd->driver->set_frag == NULL)
450 return hapd->driver->set_frag(hapd->drv_priv, frag);
454 int hostapd_sta_set_flags(struct hostapd_data *hapd, u8 *addr,
455 int total_flags, int flags_or, int flags_and)
457 if (hapd->driver == NULL || hapd->driver->sta_set_flags == NULL)
459 return hapd->driver->sta_set_flags(hapd->drv_priv, addr, total_flags,
460 flags_or, flags_and);
464 int hostapd_set_rate_sets(struct hostapd_data *hapd, int *supp_rates,
465 int *basic_rates, int mode)
467 if (hapd->driver == NULL || hapd->driver->set_rate_sets == NULL)
469 return hapd->driver->set_rate_sets(hapd->drv_priv, supp_rates,
474 int hostapd_set_country(struct hostapd_data *hapd, const char *country)
476 if (hapd->driver == NULL ||
477 hapd->driver->set_country == NULL)
479 return hapd->driver->set_country(hapd->drv_priv, country);
483 int hostapd_set_tx_queue_params(struct hostapd_data *hapd, int queue, int aifs,
484 int cw_min, int cw_max, int burst_time)
486 if (hapd->driver == NULL || hapd->driver->set_tx_queue_params == NULL)
488 return hapd->driver->set_tx_queue_params(hapd->drv_priv, queue, aifs,
489 cw_min, cw_max, burst_time);
493 int hostapd_valid_bss_mask(struct hostapd_data *hapd, const u8 *addr,
496 if (hapd->driver == NULL || hapd->driver->valid_bss_mask == NULL)
498 return hapd->driver->valid_bss_mask(hapd->drv_priv, addr, mask);
502 struct hostapd_hw_modes *
503 hostapd_get_hw_feature_data(struct hostapd_data *hapd, u16 *num_modes,
506 if (hapd->driver == NULL ||
507 hapd->driver->get_hw_feature_data == NULL)
509 return hapd->driver->get_hw_feature_data(hapd->drv_priv, num_modes,
514 int hostapd_driver_commit(struct hostapd_data *hapd)
516 if (hapd->driver == NULL || hapd->driver->commit == NULL)
518 return hapd->driver->commit(hapd->drv_priv);
522 int hostapd_drv_none(struct hostapd_data *hapd)
524 return hapd->driver && os_strcmp(hapd->driver->name, "none") == 0;
528 int hostapd_driver_scan(struct hostapd_data *hapd,
529 struct wpa_driver_scan_params *params)
531 if (hapd->driver && hapd->driver->scan2)
532 return hapd->driver->scan2(hapd->drv_priv, params);
537 struct wpa_scan_results * hostapd_driver_get_scan_results(
538 struct hostapd_data *hapd)
540 if (hapd->driver && hapd->driver->get_scan_results2)
541 return hapd->driver->get_scan_results2(hapd->drv_priv);
546 int hostapd_driver_set_noa(struct hostapd_data *hapd, u8 count, int start,
549 if (hapd->driver && hapd->driver->set_noa)
550 return hapd->driver->set_noa(hapd->drv_priv, count, start,
556 int hostapd_drv_set_key(const char *ifname, struct hostapd_data *hapd,
557 enum wpa_alg alg, const u8 *addr,
558 int key_idx, int set_tx,
559 const u8 *seq, size_t seq_len,
560 const u8 *key, size_t key_len)
562 if (hapd->driver == NULL || hapd->driver->set_key == NULL)
564 return hapd->driver->set_key(ifname, hapd->drv_priv, alg, addr,
565 key_idx, set_tx, seq, seq_len, key,
570 int hostapd_drv_send_mlme(struct hostapd_data *hapd,
571 const void *msg, size_t len)
573 if (hapd->driver == NULL || hapd->driver->send_mlme == NULL)
575 return hapd->driver->send_mlme(hapd->drv_priv, msg, len);
579 int hostapd_drv_sta_deauth(struct hostapd_data *hapd,
580 const u8 *addr, int reason)
582 if (hapd->driver == NULL || hapd->driver->sta_deauth == NULL)
584 return hapd->driver->sta_deauth(hapd->drv_priv, hapd->own_addr, addr,
589 int hostapd_drv_sta_disassoc(struct hostapd_data *hapd,
590 const u8 *addr, int reason)
592 if (hapd->driver == NULL || hapd->driver->sta_disassoc == NULL)
594 return hapd->driver->sta_disassoc(hapd->drv_priv, hapd->own_addr, addr,