2 * hostapd - Driver operations
3 * Copyright (c) 2009-2010, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "drivers/driver.h"
13 #include "common/ieee802_11_defs.h"
17 #include "ieee802_11.h"
19 #include "ap_config.h"
20 #include "p2p_hostapd.h"
22 #include "ap_drv_ops.h"
25 u32 hostapd_sta_flags_to_drv(u32 flags)
28 if (flags & WLAN_STA_AUTHORIZED)
29 res |= WPA_STA_AUTHORIZED;
30 if (flags & WLAN_STA_WMM)
32 if (flags & WLAN_STA_SHORT_PREAMBLE)
33 res |= WPA_STA_SHORT_PREAMBLE;
34 if (flags & WLAN_STA_MFP)
40 int hostapd_build_ap_extra_ies(struct hostapd_data *hapd,
41 struct wpabuf **beacon_ret,
42 struct wpabuf **proberesp_ret,
43 struct wpabuf **assocresp_ret)
45 struct wpabuf *beacon = NULL, *proberesp = NULL, *assocresp = NULL;
48 *beacon_ret = *proberesp_ret = *assocresp_ret = NULL;
51 pos = hostapd_eid_time_adv(hapd, pos);
53 if (wpabuf_resize(&beacon, pos - buf) != 0)
55 wpabuf_put_data(beacon, buf, pos - buf);
57 pos = hostapd_eid_time_zone(hapd, pos);
59 if (wpabuf_resize(&proberesp, pos - buf) != 0)
61 wpabuf_put_data(proberesp, buf, pos - buf);
65 pos = hostapd_eid_ext_capab(hapd, pos);
67 if (wpabuf_resize(&assocresp, pos - buf) != 0)
69 wpabuf_put_data(assocresp, buf, pos - buf);
71 pos = hostapd_eid_interworking(hapd, pos);
72 pos = hostapd_eid_adv_proto(hapd, pos);
73 pos = hostapd_eid_roaming_consortium(hapd, pos);
75 if (wpabuf_resize(&beacon, pos - buf) != 0)
77 wpabuf_put_data(beacon, buf, pos - buf);
79 if (wpabuf_resize(&proberesp, pos - buf) != 0)
81 wpabuf_put_data(proberesp, buf, pos - buf);
84 if (hapd->wps_beacon_ie) {
85 if (wpabuf_resize(&beacon, wpabuf_len(hapd->wps_beacon_ie)) <
88 wpabuf_put_buf(beacon, hapd->wps_beacon_ie);
91 if (hapd->wps_probe_resp_ie) {
92 if (wpabuf_resize(&proberesp,
93 wpabuf_len(hapd->wps_probe_resp_ie)) < 0)
95 wpabuf_put_buf(proberesp, hapd->wps_probe_resp_ie);
99 if (hapd->p2p_beacon_ie) {
100 if (wpabuf_resize(&beacon, wpabuf_len(hapd->p2p_beacon_ie)) <
103 wpabuf_put_buf(beacon, hapd->p2p_beacon_ie);
106 if (hapd->p2p_probe_resp_ie) {
107 if (wpabuf_resize(&proberesp,
108 wpabuf_len(hapd->p2p_probe_resp_ie)) < 0)
110 wpabuf_put_buf(proberesp, hapd->p2p_probe_resp_ie);
112 #endif /* CONFIG_P2P */
114 #ifdef CONFIG_P2P_MANAGER
115 if (hapd->conf->p2p & P2P_MANAGE) {
116 if (wpabuf_resize(&beacon, 100) == 0) {
118 start = wpabuf_put(beacon, 0);
119 p = hostapd_eid_p2p_manage(hapd, start);
120 wpabuf_put(beacon, p - start);
123 if (wpabuf_resize(&proberesp, 100) == 0) {
125 start = wpabuf_put(proberesp, 0);
126 p = hostapd_eid_p2p_manage(hapd, start);
127 wpabuf_put(proberesp, p - start);
130 #endif /* CONFIG_P2P_MANAGER */
133 if (hapd->conf->wps_state) {
134 struct wpabuf *a = wps_build_assoc_resp_ie();
135 if (a && wpabuf_resize(&assocresp, wpabuf_len(a)) == 0)
136 wpabuf_put_buf(assocresp, a);
139 #endif /* CONFIG_WPS2 */
141 #ifdef CONFIG_P2P_MANAGER
142 if (hapd->conf->p2p & P2P_MANAGE) {
143 if (wpabuf_resize(&assocresp, 100) == 0) {
145 start = wpabuf_put(assocresp, 0);
146 p = hostapd_eid_p2p_manage(hapd, start);
147 wpabuf_put(assocresp, p - start);
150 #endif /* CONFIG_P2P_MANAGER */
152 #ifdef CONFIG_WIFI_DISPLAY
153 if (hapd->p2p_group) {
155 a = p2p_group_assoc_resp_ie(hapd->p2p_group, P2P_SC_SUCCESS);
156 if (a && wpabuf_resize(&assocresp, wpabuf_len(a)) == 0)
157 wpabuf_put_buf(assocresp, a);
160 #endif /* CONFIG_WIFI_DISPLAY */
164 pos = hostapd_eid_hs20_indication(hapd, pos);
166 if (wpabuf_resize(&beacon, pos - buf) != 0)
168 wpabuf_put_data(beacon, buf, pos - buf);
170 if (wpabuf_resize(&proberesp, pos - buf) != 0)
172 wpabuf_put_data(proberesp, buf, pos - buf);
174 #endif /* CONFIG_HS20 */
176 *beacon_ret = beacon;
177 *proberesp_ret = proberesp;
178 *assocresp_ret = assocresp;
184 wpabuf_free(proberesp);
185 wpabuf_free(assocresp);
190 void hostapd_free_ap_extra_ies(struct hostapd_data *hapd,
191 struct wpabuf *beacon,
192 struct wpabuf *proberesp,
193 struct wpabuf *assocresp)
196 wpabuf_free(proberesp);
197 wpabuf_free(assocresp);
201 int hostapd_set_ap_wps_ie(struct hostapd_data *hapd)
203 struct wpabuf *beacon, *proberesp, *assocresp;
206 if (hapd->driver == NULL || hapd->driver->set_ap_wps_ie == NULL)
209 if (hostapd_build_ap_extra_ies(hapd, &beacon, &proberesp, &assocresp) <
213 ret = hapd->driver->set_ap_wps_ie(hapd->drv_priv, beacon, proberesp,
216 hostapd_free_ap_extra_ies(hapd, beacon, proberesp, assocresp);
222 int hostapd_set_authorized(struct hostapd_data *hapd,
223 struct sta_info *sta, int authorized)
226 return hostapd_sta_set_flags(hapd, sta->addr,
227 hostapd_sta_flags_to_drv(
229 WPA_STA_AUTHORIZED, ~0);
232 return hostapd_sta_set_flags(hapd, sta->addr,
233 hostapd_sta_flags_to_drv(sta->flags),
234 0, ~WPA_STA_AUTHORIZED);
238 int hostapd_set_sta_flags(struct hostapd_data *hapd, struct sta_info *sta)
240 int set_flags, total_flags, flags_and, flags_or;
241 total_flags = hostapd_sta_flags_to_drv(sta->flags);
242 set_flags = WPA_STA_SHORT_PREAMBLE | WPA_STA_WMM | WPA_STA_MFP;
243 if (((!hapd->conf->ieee802_1x && !hapd->conf->wpa) ||
244 sta->auth_alg == WLAN_AUTH_FT) &&
245 sta->flags & WLAN_STA_AUTHORIZED)
246 set_flags |= WPA_STA_AUTHORIZED;
247 flags_or = total_flags & set_flags;
248 flags_and = total_flags | ~set_flags;
249 return hostapd_sta_set_flags(hapd, sta->addr, total_flags,
250 flags_or, flags_and);
254 int hostapd_set_drv_ieee8021x(struct hostapd_data *hapd, const char *ifname,
257 struct wpa_bss_params params;
258 os_memset(¶ms, 0, sizeof(params));
259 params.ifname = ifname;
260 params.enabled = enabled;
262 params.wpa = hapd->conf->wpa;
263 params.ieee802_1x = hapd->conf->ieee802_1x;
264 params.wpa_group = hapd->conf->wpa_group;
265 params.wpa_pairwise = hapd->conf->wpa_pairwise;
266 params.wpa_key_mgmt = hapd->conf->wpa_key_mgmt;
267 params.rsn_preauth = hapd->conf->rsn_preauth;
268 #ifdef CONFIG_IEEE80211W
269 params.ieee80211w = hapd->conf->ieee80211w;
270 #endif /* CONFIG_IEEE80211W */
272 return hostapd_set_ieee8021x(hapd, ¶ms);
276 int hostapd_vlan_if_add(struct hostapd_data *hapd, const char *ifname)
278 char force_ifname[IFNAMSIZ];
279 u8 if_addr[ETH_ALEN];
280 return hostapd_if_add(hapd, WPA_IF_AP_VLAN, ifname, hapd->own_addr,
281 NULL, NULL, force_ifname, if_addr, NULL);
285 int hostapd_vlan_if_remove(struct hostapd_data *hapd, const char *ifname)
287 return hostapd_if_remove(hapd, WPA_IF_AP_VLAN, ifname);
291 int hostapd_set_wds_sta(struct hostapd_data *hapd, const u8 *addr, int aid,
294 const char *bridge = NULL;
296 if (hapd->driver == NULL || hapd->driver->set_wds_sta == NULL)
298 if (hapd->conf->wds_bridge[0])
299 bridge = hapd->conf->wds_bridge;
300 else if (hapd->conf->bridge[0])
301 bridge = hapd->conf->bridge;
302 return hapd->driver->set_wds_sta(hapd->drv_priv, addr, aid, val,
307 int hostapd_add_sta_node(struct hostapd_data *hapd, const u8 *addr,
310 if (hapd->driver == NULL || hapd->driver->add_sta_node == NULL)
312 return hapd->driver->add_sta_node(hapd->drv_priv, addr, auth_alg);
316 int hostapd_sta_auth(struct hostapd_data *hapd, const u8 *addr,
317 u16 seq, u16 status, const u8 *ie, size_t len)
319 if (hapd->driver == NULL || hapd->driver->sta_auth == NULL)
321 return hapd->driver->sta_auth(hapd->drv_priv, hapd->own_addr, addr,
322 seq, status, ie, len);
326 int hostapd_sta_assoc(struct hostapd_data *hapd, const u8 *addr,
327 int reassoc, u16 status, const u8 *ie, size_t len)
329 if (hapd->driver == NULL || hapd->driver->sta_assoc == NULL)
331 return hapd->driver->sta_assoc(hapd->drv_priv, hapd->own_addr, addr,
332 reassoc, status, ie, len);
336 int hostapd_sta_add(struct hostapd_data *hapd,
337 const u8 *addr, u16 aid, u16 capability,
338 const u8 *supp_rates, size_t supp_rates_len,
340 const struct ieee80211_ht_capabilities *ht_capab,
341 u32 flags, u8 qosinfo)
343 struct hostapd_sta_add_params params;
345 if (hapd->driver == NULL)
347 if (hapd->driver->sta_add == NULL)
350 os_memset(¶ms, 0, sizeof(params));
353 params.capability = capability;
354 params.supp_rates = supp_rates;
355 params.supp_rates_len = supp_rates_len;
356 params.listen_interval = listen_interval;
357 params.ht_capabilities = ht_capab;
358 params.flags = hostapd_sta_flags_to_drv(flags);
359 params.qosinfo = qosinfo;
360 return hapd->driver->sta_add(hapd->drv_priv, ¶ms);
364 int hostapd_add_tspec(struct hostapd_data *hapd, const u8 *addr,
365 u8 *tspec_ie, size_t tspec_ielen)
367 if (hapd->driver == NULL || hapd->driver->add_tspec == NULL)
369 return hapd->driver->add_tspec(hapd->drv_priv, addr, tspec_ie,
374 int hostapd_set_privacy(struct hostapd_data *hapd, int enabled)
376 if (hapd->driver == NULL || hapd->driver->set_privacy == NULL)
378 return hapd->driver->set_privacy(hapd->drv_priv, enabled);
382 int hostapd_set_generic_elem(struct hostapd_data *hapd, const u8 *elem,
385 if (hapd->driver == NULL || hapd->driver->set_generic_elem == NULL)
387 return hapd->driver->set_generic_elem(hapd->drv_priv, elem, elem_len);
391 int hostapd_get_ssid(struct hostapd_data *hapd, u8 *buf, size_t len)
393 if (hapd->driver == NULL || hapd->driver->hapd_get_ssid == NULL)
395 return hapd->driver->hapd_get_ssid(hapd->drv_priv, buf, len);
399 int hostapd_set_ssid(struct hostapd_data *hapd, const u8 *buf, size_t len)
401 if (hapd->driver == NULL || hapd->driver->hapd_set_ssid == NULL)
403 return hapd->driver->hapd_set_ssid(hapd->drv_priv, buf, len);
407 int hostapd_if_add(struct hostapd_data *hapd, enum wpa_driver_if_type type,
408 const char *ifname, const u8 *addr, void *bss_ctx,
409 void **drv_priv, char *force_ifname, u8 *if_addr,
412 if (hapd->driver == NULL || hapd->driver->if_add == NULL)
414 return hapd->driver->if_add(hapd->drv_priv, type, ifname, addr,
415 bss_ctx, drv_priv, force_ifname, if_addr,
420 int hostapd_if_remove(struct hostapd_data *hapd, enum wpa_driver_if_type type,
423 if (hapd->driver == NULL || hapd->driver->if_remove == NULL)
425 return hapd->driver->if_remove(hapd->drv_priv, type, ifname);
429 int hostapd_set_ieee8021x(struct hostapd_data *hapd,
430 struct wpa_bss_params *params)
432 if (hapd->driver == NULL || hapd->driver->set_ieee8021x == NULL)
434 return hapd->driver->set_ieee8021x(hapd->drv_priv, params);
438 int hostapd_get_seqnum(const char *ifname, struct hostapd_data *hapd,
439 const u8 *addr, int idx, u8 *seq)
441 if (hapd->driver == NULL || hapd->driver->get_seqnum == NULL)
443 return hapd->driver->get_seqnum(ifname, hapd->drv_priv, addr, idx,
448 int hostapd_flush(struct hostapd_data *hapd)
450 if (hapd->driver == NULL || hapd->driver->flush == NULL)
452 return hapd->driver->flush(hapd->drv_priv);
456 int hostapd_set_freq(struct hostapd_data *hapd, int mode, int freq,
457 int channel, int ht_enabled, int sec_channel_offset)
459 struct hostapd_freq_params data;
460 if (hapd->driver == NULL)
462 if (hapd->driver->set_freq == NULL)
464 os_memset(&data, 0, sizeof(data));
467 data.channel = channel;
468 data.ht_enabled = ht_enabled;
469 data.sec_channel_offset = sec_channel_offset;
470 return hapd->driver->set_freq(hapd->drv_priv, &data);
473 int hostapd_set_rts(struct hostapd_data *hapd, int rts)
475 if (hapd->driver == NULL || hapd->driver->set_rts == NULL)
477 return hapd->driver->set_rts(hapd->drv_priv, rts);
481 int hostapd_set_frag(struct hostapd_data *hapd, int frag)
483 if (hapd->driver == NULL || hapd->driver->set_frag == NULL)
485 return hapd->driver->set_frag(hapd->drv_priv, frag);
489 int hostapd_sta_set_flags(struct hostapd_data *hapd, u8 *addr,
490 int total_flags, int flags_or, int flags_and)
492 if (hapd->driver == NULL || hapd->driver->sta_set_flags == NULL)
494 return hapd->driver->sta_set_flags(hapd->drv_priv, addr, total_flags,
495 flags_or, flags_and);
499 int hostapd_set_country(struct hostapd_data *hapd, const char *country)
501 if (hapd->driver == NULL ||
502 hapd->driver->set_country == NULL)
504 return hapd->driver->set_country(hapd->drv_priv, country);
508 int hostapd_set_tx_queue_params(struct hostapd_data *hapd, int queue, int aifs,
509 int cw_min, int cw_max, int burst_time)
511 if (hapd->driver == NULL || hapd->driver->set_tx_queue_params == NULL)
513 return hapd->driver->set_tx_queue_params(hapd->drv_priv, queue, aifs,
514 cw_min, cw_max, burst_time);
518 struct hostapd_hw_modes *
519 hostapd_get_hw_feature_data(struct hostapd_data *hapd, u16 *num_modes,
522 if (hapd->driver == NULL ||
523 hapd->driver->get_hw_feature_data == NULL)
525 return hapd->driver->get_hw_feature_data(hapd->drv_priv, num_modes,
530 int hostapd_driver_commit(struct hostapd_data *hapd)
532 if (hapd->driver == NULL || hapd->driver->commit == NULL)
534 return hapd->driver->commit(hapd->drv_priv);
538 int hostapd_drv_none(struct hostapd_data *hapd)
540 return hapd->driver && os_strcmp(hapd->driver->name, "none") == 0;
544 int hostapd_driver_scan(struct hostapd_data *hapd,
545 struct wpa_driver_scan_params *params)
547 if (hapd->driver && hapd->driver->scan2)
548 return hapd->driver->scan2(hapd->drv_priv, params);
553 struct wpa_scan_results * hostapd_driver_get_scan_results(
554 struct hostapd_data *hapd)
556 if (hapd->driver && hapd->driver->get_scan_results2)
557 return hapd->driver->get_scan_results2(hapd->drv_priv);
562 int hostapd_driver_set_noa(struct hostapd_data *hapd, u8 count, int start,
565 if (hapd->driver && hapd->driver->set_noa)
566 return hapd->driver->set_noa(hapd->drv_priv, count, start,
572 int hostapd_drv_set_key(const char *ifname, struct hostapd_data *hapd,
573 enum wpa_alg alg, const u8 *addr,
574 int key_idx, int set_tx,
575 const u8 *seq, size_t seq_len,
576 const u8 *key, size_t key_len)
578 if (hapd->driver == NULL || hapd->driver->set_key == NULL)
580 return hapd->driver->set_key(ifname, hapd->drv_priv, alg, addr,
581 key_idx, set_tx, seq, seq_len, key,
586 int hostapd_drv_send_mlme(struct hostapd_data *hapd,
587 const void *msg, size_t len, int noack)
589 if (hapd->driver == NULL || hapd->driver->send_mlme == NULL)
591 return hapd->driver->send_mlme(hapd->drv_priv, msg, len, noack);
595 int hostapd_drv_sta_deauth(struct hostapd_data *hapd,
596 const u8 *addr, int reason)
598 if (hapd->driver == NULL || hapd->driver->sta_deauth == NULL)
600 return hapd->driver->sta_deauth(hapd->drv_priv, hapd->own_addr, addr,
605 int hostapd_drv_sta_disassoc(struct hostapd_data *hapd,
606 const u8 *addr, int reason)
608 if (hapd->driver == NULL || hapd->driver->sta_disassoc == NULL)
610 return hapd->driver->sta_disassoc(hapd->drv_priv, hapd->own_addr, addr,
615 int hostapd_drv_wnm_oper(struct hostapd_data *hapd, enum wnm_oper oper,
616 const u8 *peer, u8 *buf, u16 *buf_len)
618 if (hapd->driver == NULL || hapd->driver->wnm_oper == NULL)
620 return hapd->driver->wnm_oper(hapd->drv_priv, oper, peer, buf,
625 int hostapd_drv_send_action(struct hostapd_data *hapd, unsigned int freq,
626 unsigned int wait, const u8 *dst, const u8 *data,
629 if (hapd->driver == NULL || hapd->driver->send_action == NULL)
631 return hapd->driver->send_action(hapd->drv_priv, freq, wait, dst,
632 hapd->own_addr, hapd->own_addr, data,