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 static int hostapd_sta_flags_to_drv(int 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_set_ap_wps_ie(struct hostapd_data *hapd)
46 struct wpabuf *beacon, *proberesp, *assocresp = NULL;
49 if (hapd->driver == NULL || hapd->driver->set_ap_wps_ie == NULL)
52 beacon = hapd->wps_beacon_ie;
53 proberesp = hapd->wps_probe_resp_ie;
56 if (hapd->wps_beacon_ie == NULL && hapd->p2p_beacon_ie == NULL)
59 beacon = wpabuf_alloc((hapd->wps_beacon_ie ?
60 wpabuf_len(hapd->wps_beacon_ie) : 0) +
61 (hapd->p2p_beacon_ie ?
62 wpabuf_len(hapd->p2p_beacon_ie) : 0));
65 if (hapd->wps_beacon_ie)
66 wpabuf_put_buf(beacon, hapd->wps_beacon_ie);
67 if (hapd->p2p_beacon_ie)
68 wpabuf_put_buf(beacon, hapd->p2p_beacon_ie);
71 if (hapd->wps_probe_resp_ie == NULL && hapd->p2p_probe_resp_ie == NULL)
74 proberesp = wpabuf_alloc(
75 (hapd->wps_probe_resp_ie ?
76 wpabuf_len(hapd->wps_probe_resp_ie) : 0) +
77 (hapd->p2p_probe_resp_ie ?
78 wpabuf_len(hapd->p2p_probe_resp_ie) : 0));
79 if (proberesp == NULL) {
83 if (hapd->wps_probe_resp_ie)
84 wpabuf_put_buf(proberesp, hapd->wps_probe_resp_ie);
85 if (hapd->p2p_probe_resp_ie)
86 wpabuf_put_buf(proberesp, hapd->p2p_probe_resp_ie);
88 #endif /* CONFIG_P2P */
90 #ifdef CONFIG_P2P_MANAGER
91 if (hapd->conf->p2p & P2P_MANAGE) {
94 a = wpabuf_alloc(100 + (beacon ? wpabuf_len(beacon) : 0));
98 wpabuf_put_buf(a, beacon);
99 if (beacon != hapd->wps_beacon_ie)
101 start = wpabuf_put(a, 0);
102 p = hostapd_eid_p2p_manage(hapd, start);
103 wpabuf_put(a, p - start);
107 a = wpabuf_alloc(100 + (proberesp ? wpabuf_len(proberesp) :
112 wpabuf_put_buf(a, proberesp);
113 if (proberesp != hapd->wps_probe_resp_ie)
114 wpabuf_free(proberesp);
115 start = wpabuf_put(a, 0);
116 p = hostapd_eid_p2p_manage(hapd, start);
117 wpabuf_put(a, p - start);
121 #endif /* CONFIG_P2P_MANAGER */
124 if (hapd->conf->wps_state)
125 assocresp = wps_build_assoc_resp_ie();
126 #endif /* CONFIG_WPS2 */
128 #ifdef CONFIG_P2P_MANAGER
129 if (hapd->conf->p2p & P2P_MANAGE) {
131 a = wpabuf_alloc(100 + (assocresp ? wpabuf_len(assocresp) :
135 start = wpabuf_put(a, 0);
136 p = hostapd_eid_p2p_manage(hapd, start);
137 wpabuf_put(a, p - start);
139 wpabuf_put_buf(a, assocresp);
140 wpabuf_free(assocresp);
145 #endif /* CONFIG_P2P_MANAGER */
147 ret = hapd->driver->set_ap_wps_ie(hapd->drv_priv, beacon, proberesp,
150 if (beacon != hapd->wps_beacon_ie)
152 if (proberesp != hapd->wps_probe_resp_ie)
153 wpabuf_free(proberesp);
154 wpabuf_free(assocresp);
160 int hostapd_set_authorized(struct hostapd_data *hapd,
161 struct sta_info *sta, int authorized)
164 return hostapd_sta_set_flags(hapd, sta->addr,
165 hostapd_sta_flags_to_drv(
167 WPA_STA_AUTHORIZED, ~0);
170 return hostapd_sta_set_flags(hapd, sta->addr,
171 hostapd_sta_flags_to_drv(sta->flags),
172 0, ~WPA_STA_AUTHORIZED);
176 int hostapd_set_sta_flags(struct hostapd_data *hapd, struct sta_info *sta)
178 int set_flags, total_flags, flags_and, flags_or;
179 total_flags = hostapd_sta_flags_to_drv(sta->flags);
180 set_flags = WPA_STA_SHORT_PREAMBLE | WPA_STA_WMM | WPA_STA_MFP;
181 if (((!hapd->conf->ieee802_1x && !hapd->conf->wpa) ||
182 sta->auth_alg == WLAN_AUTH_FT) &&
183 sta->flags & WLAN_STA_AUTHORIZED)
184 set_flags |= WPA_STA_AUTHORIZED;
185 flags_or = total_flags & set_flags;
186 flags_and = total_flags | ~set_flags;
187 return hostapd_sta_set_flags(hapd, sta->addr, total_flags,
188 flags_or, flags_and);
192 int hostapd_set_drv_ieee8021x(struct hostapd_data *hapd, const char *ifname,
195 struct wpa_bss_params params;
196 os_memset(¶ms, 0, sizeof(params));
197 params.ifname = ifname;
198 params.enabled = enabled;
200 params.wpa = hapd->conf->wpa;
201 params.ieee802_1x = hapd->conf->ieee802_1x;
202 params.wpa_group = hapd->conf->wpa_group;
203 params.wpa_pairwise = hapd->conf->wpa_pairwise;
204 params.wpa_key_mgmt = hapd->conf->wpa_key_mgmt;
205 params.rsn_preauth = hapd->conf->rsn_preauth;
207 return hostapd_set_ieee8021x(hapd, ¶ms);
211 static int hostapd_set_ap_isolate(struct hostapd_data *hapd, int value)
213 if (hapd->driver == NULL || hapd->driver->set_intra_bss == NULL)
215 return hapd->driver->set_intra_bss(hapd->drv_priv, !value);
219 int hostapd_set_bss_params(struct hostapd_data *hapd, int use_protection)
223 #ifdef CONFIG_IEEE80211N
224 u8 buf[60], *ht_capab, *ht_oper, *pos;
228 pos = hostapd_eid_ht_capabilities(hapd, pos);
230 pos = hostapd_eid_ht_operation(hapd, pos);
231 if (pos > ht_oper && ht_oper > ht_capab &&
232 hostapd_set_ht_params(hapd, ht_capab + 2, ht_capab[1],
233 ht_oper + 2, ht_oper[1])) {
234 wpa_printf(MSG_ERROR, "Could not set HT capabilities "
235 "for kernel driver");
239 #endif /* CONFIG_IEEE80211N */
241 if (hostapd_set_cts_protect(hapd, use_protection)) {
242 wpa_printf(MSG_ERROR, "Failed to set CTS protect in kernel "
247 if (hapd->iface->current_mode &&
248 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
249 hostapd_set_short_slot_time(hapd,
250 hapd->iface->num_sta_no_short_slot_time
252 wpa_printf(MSG_ERROR, "Failed to set Short Slot Time option "
257 if (hapd->iface->num_sta_no_short_preamble == 0 &&
258 hapd->iconf->preamble == SHORT_PREAMBLE)
259 preamble = SHORT_PREAMBLE;
261 preamble = LONG_PREAMBLE;
262 if (hostapd_set_preamble(hapd, preamble)) {
263 wpa_printf(MSG_ERROR, "Could not set preamble for kernel "
268 if (hostapd_set_ap_isolate(hapd, hapd->conf->isolate) &&
269 hapd->conf->isolate) {
270 wpa_printf(MSG_ERROR, "Could not enable AP isolation in "
279 int hostapd_vlan_if_add(struct hostapd_data *hapd, const char *ifname)
281 char force_ifname[IFNAMSIZ];
282 u8 if_addr[ETH_ALEN];
283 return hostapd_if_add(hapd, WPA_IF_AP_VLAN, ifname, hapd->own_addr,
284 NULL, NULL, force_ifname, if_addr);
288 int hostapd_vlan_if_remove(struct hostapd_data *hapd, const char *ifname)
290 return hostapd_if_remove(hapd, WPA_IF_AP_VLAN, ifname);
294 int hostapd_set_wds_sta(struct hostapd_data *hapd, const u8 *addr, int aid,
297 const char *bridge = NULL;
299 if (hapd->driver == NULL || hapd->driver->set_wds_sta == NULL)
301 if (hapd->conf->wds_bridge[0])
302 bridge = hapd->conf->wds_bridge;
303 else if (hapd->conf->bridge[0])
304 bridge = hapd->conf->bridge;
305 return hapd->driver->set_wds_sta(hapd->drv_priv, addr, aid, val,
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)
316 struct hostapd_sta_add_params params;
318 if (hapd->driver == NULL)
320 if (hapd->driver->sta_add == NULL)
323 os_memset(¶ms, 0, sizeof(params));
326 params.capability = capability;
327 params.supp_rates = supp_rates;
328 params.supp_rates_len = supp_rates_len;
329 params.listen_interval = listen_interval;
330 params.ht_capabilities = ht_capab;
331 return hapd->driver->sta_add(hapd->drv_priv, ¶ms);
335 int hostapd_set_privacy(struct hostapd_data *hapd, int enabled)
337 if (hapd->driver == NULL || hapd->driver->set_privacy == NULL)
339 return hapd->driver->set_privacy(hapd->drv_priv, enabled);
343 int hostapd_set_generic_elem(struct hostapd_data *hapd, const u8 *elem,
346 if (hapd->driver == NULL || hapd->driver->set_generic_elem == NULL)
348 return hapd->driver->set_generic_elem(hapd->drv_priv, elem, elem_len);
352 int hostapd_get_ssid(struct hostapd_data *hapd, u8 *buf, size_t len)
354 if (hapd->driver == NULL || hapd->driver->hapd_get_ssid == NULL)
356 return hapd->driver->hapd_get_ssid(hapd->drv_priv, buf, len);
360 int hostapd_set_ssid(struct hostapd_data *hapd, const u8 *buf, size_t len)
362 if (hapd->driver == NULL || hapd->driver->hapd_set_ssid == NULL)
364 return hapd->driver->hapd_set_ssid(hapd->drv_priv, buf, len);
368 int hostapd_if_add(struct hostapd_data *hapd, enum wpa_driver_if_type type,
369 const char *ifname, const u8 *addr, void *bss_ctx,
370 void **drv_priv, char *force_ifname, u8 *if_addr)
372 if (hapd->driver == NULL || hapd->driver->if_add == NULL)
374 return hapd->driver->if_add(hapd->drv_priv, type, ifname, addr,
375 bss_ctx, drv_priv, force_ifname, if_addr);
379 int hostapd_if_remove(struct hostapd_data *hapd, enum wpa_driver_if_type type,
382 if (hapd->driver == NULL || hapd->driver->if_remove == NULL)
384 return hapd->driver->if_remove(hapd->drv_priv, type, ifname);
388 int hostapd_set_ieee8021x(struct hostapd_data *hapd,
389 struct wpa_bss_params *params)
391 if (hapd->driver == NULL || hapd->driver->set_ieee8021x == NULL)
393 return hapd->driver->set_ieee8021x(hapd->drv_priv, params);
397 int hostapd_get_seqnum(const char *ifname, struct hostapd_data *hapd,
398 const u8 *addr, int idx, u8 *seq)
400 if (hapd->driver == NULL || hapd->driver->get_seqnum == NULL)
402 return hapd->driver->get_seqnum(ifname, hapd->drv_priv, addr, idx,
407 int hostapd_flush(struct hostapd_data *hapd)
409 if (hapd->driver == NULL || hapd->driver->flush == NULL)
411 return hapd->driver->flush(hapd->drv_priv);
415 int hostapd_set_freq(struct hostapd_data *hapd, int mode, int freq,
416 int channel, int ht_enabled, int sec_channel_offset)
418 struct hostapd_freq_params data;
419 if (hapd->driver == NULL)
421 if (hapd->driver->set_freq == NULL)
423 os_memset(&data, 0, sizeof(data));
426 data.channel = channel;
427 data.ht_enabled = ht_enabled;
428 data.sec_channel_offset = sec_channel_offset;
429 return hapd->driver->set_freq(hapd->drv_priv, &data);
432 int hostapd_set_rts(struct hostapd_data *hapd, int rts)
434 if (hapd->driver == NULL || hapd->driver->set_rts == NULL)
436 return hapd->driver->set_rts(hapd->drv_priv, rts);
440 int hostapd_set_frag(struct hostapd_data *hapd, int frag)
442 if (hapd->driver == NULL || hapd->driver->set_frag == NULL)
444 return hapd->driver->set_frag(hapd->drv_priv, frag);
448 int hostapd_sta_set_flags(struct hostapd_data *hapd, u8 *addr,
449 int total_flags, int flags_or, int flags_and)
451 if (hapd->driver == NULL || hapd->driver->sta_set_flags == NULL)
453 return hapd->driver->sta_set_flags(hapd->drv_priv, addr, total_flags,
454 flags_or, flags_and);
458 int hostapd_set_rate_sets(struct hostapd_data *hapd, int *supp_rates,
459 int *basic_rates, int mode)
461 if (hapd->driver == NULL || hapd->driver->set_rate_sets == NULL)
463 return hapd->driver->set_rate_sets(hapd->drv_priv, supp_rates,
468 int hostapd_set_country(struct hostapd_data *hapd, const char *country)
470 if (hapd->driver == NULL ||
471 hapd->driver->set_country == NULL)
473 return hapd->driver->set_country(hapd->drv_priv, country);
477 int hostapd_set_cts_protect(struct hostapd_data *hapd, int value)
479 if (hapd->driver == NULL || hapd->driver->set_cts_protect == NULL)
481 return hapd->driver->set_cts_protect(hapd->drv_priv, value);
485 int hostapd_set_preamble(struct hostapd_data *hapd, int value)
487 if (hapd->driver == NULL || hapd->driver->set_preamble == NULL)
489 return hapd->driver->set_preamble(hapd->drv_priv, value);
493 int hostapd_set_short_slot_time(struct hostapd_data *hapd, int value)
495 if (hapd->driver == NULL || hapd->driver->set_short_slot_time == NULL)
497 return hapd->driver->set_short_slot_time(hapd->drv_priv, value);
501 int hostapd_set_tx_queue_params(struct hostapd_data *hapd, int queue, int aifs,
502 int cw_min, int cw_max, int burst_time)
504 if (hapd->driver == NULL || hapd->driver->set_tx_queue_params == NULL)
506 return hapd->driver->set_tx_queue_params(hapd->drv_priv, queue, aifs,
507 cw_min, cw_max, burst_time);
511 int hostapd_valid_bss_mask(struct hostapd_data *hapd, const u8 *addr,
514 if (hapd->driver == NULL || hapd->driver->valid_bss_mask == NULL)
516 return hapd->driver->valid_bss_mask(hapd->drv_priv, addr, mask);
520 struct hostapd_hw_modes *
521 hostapd_get_hw_feature_data(struct hostapd_data *hapd, u16 *num_modes,
524 if (hapd->driver == NULL ||
525 hapd->driver->get_hw_feature_data == NULL)
527 return hapd->driver->get_hw_feature_data(hapd->drv_priv, num_modes,
532 int hostapd_driver_commit(struct hostapd_data *hapd)
534 if (hapd->driver == NULL || hapd->driver->commit == NULL)
536 return hapd->driver->commit(hapd->drv_priv);
540 int hostapd_set_ht_params(struct hostapd_data *hapd,
541 const u8 *ht_capab, size_t ht_capab_len,
542 const u8 *ht_oper, size_t ht_oper_len)
544 if (hapd->driver == NULL || hapd->driver->set_ht_params == NULL ||
545 ht_capab == NULL || ht_oper == NULL)
547 return hapd->driver->set_ht_params(hapd->drv_priv,
548 ht_capab, ht_capab_len,
549 ht_oper, ht_oper_len);
553 int hostapd_drv_none(struct hostapd_data *hapd)
555 return hapd->driver && os_strcmp(hapd->driver->name, "none") == 0;
559 int hostapd_driver_scan(struct hostapd_data *hapd,
560 struct wpa_driver_scan_params *params)
562 if (hapd->driver && hapd->driver->scan2)
563 return hapd->driver->scan2(hapd->drv_priv, params);
568 struct wpa_scan_results * hostapd_driver_get_scan_results(
569 struct hostapd_data *hapd)
571 if (hapd->driver && hapd->driver->get_scan_results2)
572 return hapd->driver->get_scan_results2(hapd->drv_priv);
577 int hostapd_driver_set_noa(struct hostapd_data *hapd, u8 count, int start,
580 if (hapd->driver && hapd->driver->set_noa)
581 return hapd->driver->set_noa(hapd->drv_priv, count, start,
587 int hostapd_drv_set_key(const char *ifname, struct hostapd_data *hapd,
588 enum wpa_alg alg, const u8 *addr,
589 int key_idx, int set_tx,
590 const u8 *seq, size_t seq_len,
591 const u8 *key, size_t key_len)
593 if (hapd->driver == NULL || hapd->driver->set_key == NULL)
595 return hapd->driver->set_key(ifname, hapd->drv_priv, alg, addr,
596 key_idx, set_tx, seq, seq_len, key,
601 int hostapd_drv_send_mlme(struct hostapd_data *hapd,
602 const void *msg, size_t len)
604 if (hapd->driver == NULL || hapd->driver->send_mlme == NULL)
606 return hapd->driver->send_mlme(hapd->drv_priv, msg, len);
610 int hostapd_drv_sta_deauth(struct hostapd_data *hapd,
611 const u8 *addr, int reason)
613 if (hapd->driver == NULL || hapd->driver->sta_deauth == NULL)
615 return hapd->driver->sta_deauth(hapd->drv_priv, hapd->own_addr, addr,
620 int hostapd_drv_sta_disassoc(struct hostapd_data *hapd,
621 const u8 *addr, int reason)
623 if (hapd->driver == NULL || hapd->driver->sta_disassoc == NULL)
625 return hapd->driver->sta_disassoc(hapd->drv_priv, hapd->own_addr, addr,