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"
16 #include "ieee802_11.h"
18 #include "ap_config.h"
19 #include "p2p_hostapd.h"
20 #include "ap_drv_ops.h"
23 u32 hostapd_sta_flags_to_drv(u32 flags)
26 if (flags & WLAN_STA_AUTHORIZED)
27 res |= WPA_STA_AUTHORIZED;
28 if (flags & WLAN_STA_WMM)
30 if (flags & WLAN_STA_SHORT_PREAMBLE)
31 res |= WPA_STA_SHORT_PREAMBLE;
32 if (flags & WLAN_STA_MFP)
38 int hostapd_build_ap_extra_ies(struct hostapd_data *hapd,
39 struct wpabuf **beacon_ret,
40 struct wpabuf **proberesp_ret,
41 struct wpabuf **assocresp_ret)
43 struct wpabuf *beacon = NULL, *proberesp = NULL, *assocresp = NULL;
46 *beacon_ret = *proberesp_ret = *assocresp_ret = NULL;
49 pos = hostapd_eid_time_adv(hapd, pos);
51 if (wpabuf_resize(&beacon, pos - buf) != 0)
53 wpabuf_put_data(beacon, buf, pos - buf);
55 pos = hostapd_eid_time_zone(hapd, pos);
57 if (wpabuf_resize(&proberesp, pos - buf) != 0)
59 wpabuf_put_data(proberesp, buf, pos - buf);
63 pos = hostapd_eid_ext_capab(hapd, pos);
65 if (wpabuf_resize(&assocresp, pos - buf) != 0)
67 wpabuf_put_data(assocresp, buf, pos - buf);
69 pos = hostapd_eid_interworking(hapd, pos);
70 pos = hostapd_eid_adv_proto(hapd, pos);
71 pos = hostapd_eid_roaming_consortium(hapd, pos);
73 if (wpabuf_resize(&beacon, pos - buf) != 0)
75 wpabuf_put_data(beacon, buf, pos - buf);
77 if (wpabuf_resize(&proberesp, pos - buf) != 0)
79 wpabuf_put_data(proberesp, buf, pos - buf);
82 if (hapd->wps_beacon_ie) {
83 if (wpabuf_resize(&beacon, wpabuf_len(hapd->wps_beacon_ie)) <
86 wpabuf_put_buf(beacon, hapd->wps_beacon_ie);
89 if (hapd->wps_probe_resp_ie) {
90 if (wpabuf_resize(&proberesp,
91 wpabuf_len(hapd->wps_probe_resp_ie)) < 0)
93 wpabuf_put_buf(proberesp, hapd->wps_probe_resp_ie);
97 if (hapd->p2p_beacon_ie) {
98 if (wpabuf_resize(&beacon, wpabuf_len(hapd->p2p_beacon_ie)) <
101 wpabuf_put_buf(beacon, hapd->p2p_beacon_ie);
104 if (hapd->p2p_probe_resp_ie) {
105 if (wpabuf_resize(&proberesp,
106 wpabuf_len(hapd->p2p_probe_resp_ie)) < 0)
108 wpabuf_put_buf(proberesp, hapd->p2p_probe_resp_ie);
110 #endif /* CONFIG_P2P */
112 #ifdef CONFIG_P2P_MANAGER
113 if (hapd->conf->p2p & P2P_MANAGE) {
114 if (wpabuf_resize(&beacon, 100) == 0) {
116 start = wpabuf_put(beacon, 0);
117 p = hostapd_eid_p2p_manage(hapd, start);
118 wpabuf_put(beacon, p - start);
121 if (wpabuf_resize(&proberesp, 100) == 0) {
123 start = wpabuf_put(proberesp, 0);
124 p = hostapd_eid_p2p_manage(hapd, start);
125 wpabuf_put(proberesp, p - start);
128 #endif /* CONFIG_P2P_MANAGER */
131 if (hapd->conf->wps_state) {
132 struct wpabuf *a = wps_build_assoc_resp_ie();
133 if (a && wpabuf_resize(&assocresp, wpabuf_len(a)) == 0)
134 wpabuf_put_buf(assocresp, a);
137 #endif /* CONFIG_WPS2 */
139 #ifdef CONFIG_P2P_MANAGER
140 if (hapd->conf->p2p & P2P_MANAGE) {
141 if (wpabuf_resize(&assocresp, 100) == 0) {
143 start = wpabuf_put(assocresp, 0);
144 p = hostapd_eid_p2p_manage(hapd, start);
145 wpabuf_put(assocresp, p - start);
148 #endif /* CONFIG_P2P_MANAGER */
150 *beacon_ret = beacon;
151 *proberesp_ret = proberesp;
152 *assocresp_ret = assocresp;
158 wpabuf_free(proberesp);
159 wpabuf_free(assocresp);
164 void hostapd_free_ap_extra_ies(struct hostapd_data *hapd,
165 struct wpabuf *beacon,
166 struct wpabuf *proberesp,
167 struct wpabuf *assocresp)
170 wpabuf_free(proberesp);
171 wpabuf_free(assocresp);
175 int hostapd_set_ap_wps_ie(struct hostapd_data *hapd)
177 struct wpabuf *beacon, *proberesp, *assocresp;
180 if (hapd->driver == NULL || hapd->driver->set_ap_wps_ie == NULL)
183 if (hostapd_build_ap_extra_ies(hapd, &beacon, &proberesp, &assocresp) <
187 ret = hapd->driver->set_ap_wps_ie(hapd->drv_priv, beacon, proberesp,
190 hostapd_free_ap_extra_ies(hapd, beacon, proberesp, assocresp);
196 int hostapd_set_authorized(struct hostapd_data *hapd,
197 struct sta_info *sta, int authorized)
200 return hostapd_sta_set_flags(hapd, sta->addr,
201 hostapd_sta_flags_to_drv(
203 WPA_STA_AUTHORIZED, ~0);
206 return hostapd_sta_set_flags(hapd, sta->addr,
207 hostapd_sta_flags_to_drv(sta->flags),
208 0, ~WPA_STA_AUTHORIZED);
212 int hostapd_set_sta_flags(struct hostapd_data *hapd, struct sta_info *sta)
214 int set_flags, total_flags, flags_and, flags_or;
215 total_flags = hostapd_sta_flags_to_drv(sta->flags);
216 set_flags = WPA_STA_SHORT_PREAMBLE | WPA_STA_WMM | WPA_STA_MFP;
217 if (((!hapd->conf->ieee802_1x && !hapd->conf->wpa) ||
218 sta->auth_alg == WLAN_AUTH_FT) &&
219 sta->flags & WLAN_STA_AUTHORIZED)
220 set_flags |= WPA_STA_AUTHORIZED;
221 flags_or = total_flags & set_flags;
222 flags_and = total_flags | ~set_flags;
223 return hostapd_sta_set_flags(hapd, sta->addr, total_flags,
224 flags_or, flags_and);
228 int hostapd_set_drv_ieee8021x(struct hostapd_data *hapd, const char *ifname,
231 struct wpa_bss_params params;
232 os_memset(¶ms, 0, sizeof(params));
233 params.ifname = ifname;
234 params.enabled = enabled;
236 params.wpa = hapd->conf->wpa;
237 params.ieee802_1x = hapd->conf->ieee802_1x;
238 params.wpa_group = hapd->conf->wpa_group;
239 params.wpa_pairwise = hapd->conf->wpa_pairwise;
240 params.wpa_key_mgmt = hapd->conf->wpa_key_mgmt;
241 params.rsn_preauth = hapd->conf->rsn_preauth;
242 #ifdef CONFIG_IEEE80211W
243 params.ieee80211w = hapd->conf->ieee80211w;
244 #endif /* CONFIG_IEEE80211W */
246 return hostapd_set_ieee8021x(hapd, ¶ms);
250 int hostapd_vlan_if_add(struct hostapd_data *hapd, const char *ifname)
252 char force_ifname[IFNAMSIZ];
253 u8 if_addr[ETH_ALEN];
254 return hostapd_if_add(hapd, WPA_IF_AP_VLAN, ifname, hapd->own_addr,
255 NULL, NULL, force_ifname, if_addr, NULL);
259 int hostapd_vlan_if_remove(struct hostapd_data *hapd, const char *ifname)
261 return hostapd_if_remove(hapd, WPA_IF_AP_VLAN, ifname);
265 int hostapd_set_wds_sta(struct hostapd_data *hapd, const u8 *addr, int aid,
268 const char *bridge = NULL;
270 if (hapd->driver == NULL || hapd->driver->set_wds_sta == NULL)
272 if (hapd->conf->wds_bridge[0])
273 bridge = hapd->conf->wds_bridge;
274 else if (hapd->conf->bridge[0])
275 bridge = hapd->conf->bridge;
276 return hapd->driver->set_wds_sta(hapd->drv_priv, addr, aid, val,
281 int hostapd_add_sta_node(struct hostapd_data *hapd, const u8 *addr,
284 if (hapd->driver == NULL || hapd->driver->add_sta_node == NULL)
286 return hapd->driver->add_sta_node(hapd->drv_priv, addr, auth_alg);
290 int hostapd_sta_auth(struct hostapd_data *hapd, const u8 *addr,
291 u16 seq, u16 status, const u8 *ie, size_t len)
293 if (hapd->driver == NULL || hapd->driver->sta_auth == NULL)
295 return hapd->driver->sta_auth(hapd->drv_priv, hapd->own_addr, addr,
296 seq, status, ie, len);
300 int hostapd_sta_assoc(struct hostapd_data *hapd, const u8 *addr,
301 int reassoc, u16 status, const u8 *ie, size_t len)
303 if (hapd->driver == NULL || hapd->driver->sta_assoc == NULL)
305 return hapd->driver->sta_assoc(hapd->drv_priv, hapd->own_addr, addr,
306 reassoc, status, ie, len);
310 int hostapd_sta_add(struct hostapd_data *hapd,
311 const u8 *addr, u16 aid, u16 capability,
312 const u8 *supp_rates, size_t supp_rates_len,
314 const struct ieee80211_ht_capabilities *ht_capab,
315 u32 flags, u8 qosinfo)
317 struct hostapd_sta_add_params params;
319 if (hapd->driver == NULL)
321 if (hapd->driver->sta_add == NULL)
324 os_memset(¶ms, 0, sizeof(params));
327 params.capability = capability;
328 params.supp_rates = supp_rates;
329 params.supp_rates_len = supp_rates_len;
330 params.listen_interval = listen_interval;
331 params.ht_capabilities = ht_capab;
332 params.flags = hostapd_sta_flags_to_drv(flags);
333 params.qosinfo = qosinfo;
334 return hapd->driver->sta_add(hapd->drv_priv, ¶ms);
338 int hostapd_add_tspec(struct hostapd_data *hapd, const u8 *addr,
339 u8 *tspec_ie, size_t tspec_ielen)
341 if (hapd->driver == NULL || hapd->driver->add_tspec == NULL)
343 return hapd->driver->add_tspec(hapd->drv_priv, addr, tspec_ie,
348 int hostapd_set_privacy(struct hostapd_data *hapd, int enabled)
350 if (hapd->driver == NULL || hapd->driver->set_privacy == NULL)
352 return hapd->driver->set_privacy(hapd->drv_priv, enabled);
356 int hostapd_set_generic_elem(struct hostapd_data *hapd, const u8 *elem,
359 if (hapd->driver == NULL || hapd->driver->set_generic_elem == NULL)
361 return hapd->driver->set_generic_elem(hapd->drv_priv, elem, elem_len);
365 int hostapd_get_ssid(struct hostapd_data *hapd, u8 *buf, size_t len)
367 if (hapd->driver == NULL || hapd->driver->hapd_get_ssid == NULL)
369 return hapd->driver->hapd_get_ssid(hapd->drv_priv, buf, len);
373 int hostapd_set_ssid(struct hostapd_data *hapd, const u8 *buf, size_t len)
375 if (hapd->driver == NULL || hapd->driver->hapd_set_ssid == NULL)
377 return hapd->driver->hapd_set_ssid(hapd->drv_priv, buf, len);
381 int hostapd_if_add(struct hostapd_data *hapd, enum wpa_driver_if_type type,
382 const char *ifname, const u8 *addr, void *bss_ctx,
383 void **drv_priv, char *force_ifname, u8 *if_addr,
386 if (hapd->driver == NULL || hapd->driver->if_add == NULL)
388 return hapd->driver->if_add(hapd->drv_priv, type, ifname, addr,
389 bss_ctx, drv_priv, force_ifname, if_addr,
394 int hostapd_if_remove(struct hostapd_data *hapd, enum wpa_driver_if_type type,
397 if (hapd->driver == NULL || hapd->driver->if_remove == NULL)
399 return hapd->driver->if_remove(hapd->drv_priv, type, ifname);
403 int hostapd_set_ieee8021x(struct hostapd_data *hapd,
404 struct wpa_bss_params *params)
406 if (hapd->driver == NULL || hapd->driver->set_ieee8021x == NULL)
408 return hapd->driver->set_ieee8021x(hapd->drv_priv, params);
412 int hostapd_get_seqnum(const char *ifname, struct hostapd_data *hapd,
413 const u8 *addr, int idx, u8 *seq)
415 if (hapd->driver == NULL || hapd->driver->get_seqnum == NULL)
417 return hapd->driver->get_seqnum(ifname, hapd->drv_priv, addr, idx,
422 int hostapd_flush(struct hostapd_data *hapd)
424 if (hapd->driver == NULL || hapd->driver->flush == NULL)
426 return hapd->driver->flush(hapd->drv_priv);
430 int hostapd_set_freq(struct hostapd_data *hapd, int mode, int freq,
431 int channel, int ht_enabled, int sec_channel_offset)
433 struct hostapd_freq_params data;
434 if (hapd->driver == NULL)
436 if (hapd->driver->set_freq == NULL)
438 os_memset(&data, 0, sizeof(data));
441 data.channel = channel;
442 data.ht_enabled = ht_enabled;
443 data.sec_channel_offset = sec_channel_offset;
444 return hapd->driver->set_freq(hapd->drv_priv, &data);
447 int hostapd_set_rts(struct hostapd_data *hapd, int rts)
449 if (hapd->driver == NULL || hapd->driver->set_rts == NULL)
451 return hapd->driver->set_rts(hapd->drv_priv, rts);
455 int hostapd_set_frag(struct hostapd_data *hapd, int frag)
457 if (hapd->driver == NULL || hapd->driver->set_frag == NULL)
459 return hapd->driver->set_frag(hapd->drv_priv, frag);
463 int hostapd_sta_set_flags(struct hostapd_data *hapd, u8 *addr,
464 int total_flags, int flags_or, int flags_and)
466 if (hapd->driver == NULL || hapd->driver->sta_set_flags == NULL)
468 return hapd->driver->sta_set_flags(hapd->drv_priv, addr, total_flags,
469 flags_or, flags_and);
473 int hostapd_set_country(struct hostapd_data *hapd, const char *country)
475 if (hapd->driver == NULL ||
476 hapd->driver->set_country == NULL)
478 return hapd->driver->set_country(hapd->drv_priv, country);
482 int hostapd_set_tx_queue_params(struct hostapd_data *hapd, int queue, int aifs,
483 int cw_min, int cw_max, int burst_time)
485 if (hapd->driver == NULL || hapd->driver->set_tx_queue_params == NULL)
487 return hapd->driver->set_tx_queue_params(hapd->drv_priv, queue, aifs,
488 cw_min, cw_max, burst_time);
492 struct hostapd_hw_modes *
493 hostapd_get_hw_feature_data(struct hostapd_data *hapd, u16 *num_modes,
496 if (hapd->driver == NULL ||
497 hapd->driver->get_hw_feature_data == NULL)
499 return hapd->driver->get_hw_feature_data(hapd->drv_priv, num_modes,
504 int hostapd_driver_commit(struct hostapd_data *hapd)
506 if (hapd->driver == NULL || hapd->driver->commit == NULL)
508 return hapd->driver->commit(hapd->drv_priv);
512 int hostapd_drv_none(struct hostapd_data *hapd)
514 return hapd->driver && os_strcmp(hapd->driver->name, "none") == 0;
518 int hostapd_driver_scan(struct hostapd_data *hapd,
519 struct wpa_driver_scan_params *params)
521 if (hapd->driver && hapd->driver->scan2)
522 return hapd->driver->scan2(hapd->drv_priv, params);
527 struct wpa_scan_results * hostapd_driver_get_scan_results(
528 struct hostapd_data *hapd)
530 if (hapd->driver && hapd->driver->get_scan_results2)
531 return hapd->driver->get_scan_results2(hapd->drv_priv);
536 int hostapd_driver_set_noa(struct hostapd_data *hapd, u8 count, int start,
539 if (hapd->driver && hapd->driver->set_noa)
540 return hapd->driver->set_noa(hapd->drv_priv, count, start,
546 int hostapd_drv_set_key(const char *ifname, struct hostapd_data *hapd,
547 enum wpa_alg alg, const u8 *addr,
548 int key_idx, int set_tx,
549 const u8 *seq, size_t seq_len,
550 const u8 *key, size_t key_len)
552 if (hapd->driver == NULL || hapd->driver->set_key == NULL)
554 return hapd->driver->set_key(ifname, hapd->drv_priv, alg, addr,
555 key_idx, set_tx, seq, seq_len, key,
560 int hostapd_drv_send_mlme(struct hostapd_data *hapd,
561 const void *msg, size_t len, int noack)
563 if (hapd->driver == NULL || hapd->driver->send_mlme == NULL)
565 return hapd->driver->send_mlme(hapd->drv_priv, msg, len, noack);
569 int hostapd_drv_sta_deauth(struct hostapd_data *hapd,
570 const u8 *addr, int reason)
572 if (hapd->driver == NULL || hapd->driver->sta_deauth == NULL)
574 return hapd->driver->sta_deauth(hapd->drv_priv, hapd->own_addr, addr,
579 int hostapd_drv_sta_disassoc(struct hostapd_data *hapd,
580 const u8 *addr, int reason)
582 if (hapd->driver == NULL || hapd->driver->sta_disassoc == NULL)
584 return hapd->driver->sta_disassoc(hapd->drv_priv, hapd->own_addr, addr,