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"
21 #include "ieee802_11.h"
23 #include "ap_config.h"
24 #include "ap_drv_ops.h"
27 static int hostapd_sta_flags_to_drv(int flags)
30 if (flags & WLAN_STA_AUTHORIZED)
31 res |= WPA_STA_AUTHORIZED;
32 if (flags & WLAN_STA_WMM)
34 if (flags & WLAN_STA_SHORT_PREAMBLE)
35 res |= WPA_STA_SHORT_PREAMBLE;
36 if (flags & WLAN_STA_MFP)
42 static int hostapd_set_ap_wps_ie(struct hostapd_data *hapd,
43 const struct wpabuf *beacon,
44 const struct wpabuf *proberesp)
46 if (hapd->driver == NULL || hapd->driver->set_ap_wps_ie == NULL)
48 return hapd->driver->set_ap_wps_ie(hapd->drv_priv, beacon, proberesp);
52 static int hostapd_send_mgmt_frame(struct hostapd_data *hapd, const void *msg,
55 if (hapd->driver == NULL || hapd->driver->send_mlme == NULL)
57 return hapd->driver->send_mlme(hapd->drv_priv, msg, len);
61 static int hostapd_send_eapol(struct hostapd_data *hapd, const u8 *addr,
62 const u8 *data, size_t data_len, int encrypt)
64 if (hapd->driver == NULL || hapd->driver->hapd_send_eapol == NULL)
66 return hapd->driver->hapd_send_eapol(hapd->drv_priv, addr, data,
72 static int hostapd_set_authorized(struct hostapd_data *hapd,
73 struct sta_info *sta, int authorized)
76 return hostapd_sta_set_flags(hapd, sta->addr,
77 hostapd_sta_flags_to_drv(
79 WPA_STA_AUTHORIZED, ~0);
82 return hostapd_sta_set_flags(hapd, sta->addr,
83 hostapd_sta_flags_to_drv(sta->flags),
84 0, ~WPA_STA_AUTHORIZED);
88 static int hostapd_set_key(const char *ifname, struct hostapd_data *hapd,
89 enum wpa_alg alg, const u8 *addr, int key_idx,
90 int set_tx, const u8 *seq, size_t seq_len,
91 const u8 *key, size_t key_len)
93 if (hapd->driver == NULL || hapd->driver->set_key == NULL)
95 return hapd->driver->set_key(ifname, hapd->drv_priv, alg, addr,
96 key_idx, set_tx, seq, seq_len, key,
101 static int hostapd_read_sta_data(struct hostapd_data *hapd,
102 struct hostap_sta_driver_data *data,
105 if (hapd->driver == NULL || hapd->driver->read_sta_data == NULL)
107 return hapd->driver->read_sta_data(hapd->drv_priv, data, addr);
111 static int hostapd_sta_clear_stats(struct hostapd_data *hapd, const u8 *addr)
113 if (hapd->driver == NULL || hapd->driver->sta_clear_stats == NULL)
115 return hapd->driver->sta_clear_stats(hapd->drv_priv, addr);
119 static int hostapd_set_sta_flags(struct hostapd_data *hapd,
120 struct sta_info *sta)
122 int set_flags, total_flags, flags_and, flags_or;
123 total_flags = hostapd_sta_flags_to_drv(sta->flags);
124 set_flags = WPA_STA_SHORT_PREAMBLE | WPA_STA_WMM | WPA_STA_MFP;
125 if (((!hapd->conf->ieee802_1x && !hapd->conf->wpa) ||
126 sta->auth_alg == WLAN_AUTH_FT) &&
127 sta->flags & WLAN_STA_AUTHORIZED)
128 set_flags |= WPA_STA_AUTHORIZED;
129 flags_or = total_flags & set_flags;
130 flags_and = total_flags | ~set_flags;
131 return hostapd_sta_set_flags(hapd, sta->addr, total_flags,
132 flags_or, flags_and);
136 static int hostapd_set_drv_ieee8021x(struct hostapd_data *hapd,
137 const char *ifname, int enabled)
139 struct wpa_bss_params params;
140 os_memset(¶ms, 0, sizeof(params));
141 params.ifname = ifname;
142 params.enabled = enabled;
144 params.wpa = hapd->conf->wpa;
145 params.ieee802_1x = hapd->conf->ieee802_1x;
146 params.wpa_group = hapd->conf->wpa_group;
147 params.wpa_pairwise = hapd->conf->wpa_pairwise;
148 params.wpa_key_mgmt = hapd->conf->wpa_key_mgmt;
149 params.rsn_preauth = hapd->conf->rsn_preauth;
151 return hostapd_set_ieee8021x(hapd, ¶ms);
155 static int hostapd_set_radius_acl_auth(struct hostapd_data *hapd,
156 const u8 *mac, int accepted,
159 if (hapd->driver == NULL || hapd->driver->set_radius_acl_auth == NULL)
161 return hapd->driver->set_radius_acl_auth(hapd->drv_priv, mac, accepted,
166 static int hostapd_set_radius_acl_expire(struct hostapd_data *hapd,
169 if (hapd->driver == NULL ||
170 hapd->driver->set_radius_acl_expire == NULL)
172 return hapd->driver->set_radius_acl_expire(hapd->drv_priv, mac);
176 static int hostapd_set_bss_params(struct hostapd_data *hapd,
181 #ifdef CONFIG_IEEE80211N
182 u8 buf[60], *ht_capab, *ht_oper, *pos;
186 pos = hostapd_eid_ht_capabilities(hapd, pos);
188 pos = hostapd_eid_ht_operation(hapd, pos);
189 if (pos > ht_oper && ht_oper > ht_capab &&
190 hostapd_set_ht_params(hapd, ht_capab + 2, ht_capab[1],
191 ht_oper + 2, ht_oper[1])) {
192 wpa_printf(MSG_ERROR, "Could not set HT capabilities "
193 "for kernel driver");
197 #endif /* CONFIG_IEEE80211N */
199 if (hostapd_set_cts_protect(hapd, use_protection)) {
200 wpa_printf(MSG_ERROR, "Failed to set CTS protect in kernel "
205 if (hapd->iface->current_mode &&
206 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
207 hostapd_set_short_slot_time(hapd,
208 hapd->iface->num_sta_no_short_slot_time
210 wpa_printf(MSG_ERROR, "Failed to set Short Slot Time option "
215 if (hapd->iface->num_sta_no_short_preamble == 0 &&
216 hapd->iconf->preamble == SHORT_PREAMBLE)
217 preamble = SHORT_PREAMBLE;
219 preamble = LONG_PREAMBLE;
220 if (hostapd_set_preamble(hapd, preamble)) {
221 wpa_printf(MSG_ERROR, "Could not set preamble for kernel "
230 static int hostapd_set_beacon(struct hostapd_data *hapd,
231 const u8 *head, size_t head_len,
232 const u8 *tail, size_t tail_len, int dtim_period,
235 if (hapd->driver == NULL || hapd->driver->set_beacon == NULL)
237 return hapd->driver->set_beacon(hapd->drv_priv,
238 head, head_len, tail, tail_len,
239 dtim_period, beacon_int);
243 static int hostapd_vlan_if_add(struct hostapd_data *hapd, const char *ifname)
245 char force_ifname[IFNAMSIZ];
246 u8 if_addr[ETH_ALEN];
247 return hostapd_if_add(hapd, WPA_IF_AP_VLAN, ifname, NULL, NULL, NULL,
248 force_ifname, if_addr);
251 static int hostapd_vlan_if_remove(struct hostapd_data *hapd,
254 return hostapd_if_remove(hapd, WPA_IF_AP_VLAN, ifname);
258 static int hostapd_set_wds_sta(struct hostapd_data *hapd, const u8 *addr,
261 if (hapd->driver == NULL || hapd->driver->set_wds_sta == NULL)
263 return hapd->driver->set_wds_sta(hapd->drv_priv, addr, aid, val);
267 static int hostapd_set_sta_vlan(const char *ifname, struct hostapd_data *hapd,
268 const u8 *addr, int vlan_id)
270 if (hapd->driver == NULL || hapd->driver->set_sta_vlan == NULL)
272 return hapd->driver->set_sta_vlan(hapd->drv_priv, addr, ifname,
277 static int hostapd_get_inact_sec(struct hostapd_data *hapd, const u8 *addr)
279 if (hapd->driver == NULL || hapd->driver->get_inact_sec == NULL)
281 return hapd->driver->get_inact_sec(hapd->drv_priv, addr);
285 static int hostapd_sta_deauth(struct hostapd_data *hapd, const u8 *addr,
288 if (hapd->driver == NULL || hapd->driver->sta_deauth == NULL)
290 return hapd->driver->sta_deauth(hapd->drv_priv, hapd->own_addr, addr,
295 static int hostapd_sta_disassoc(struct hostapd_data *hapd, const u8 *addr,
298 if (hapd->driver == NULL || hapd->driver->sta_disassoc == NULL)
300 return hapd->driver->sta_disassoc(hapd->drv_priv, hapd->own_addr, addr,
305 static int hostapd_sta_add(struct hostapd_data *hapd,
306 const u8 *addr, u16 aid, u16 capability,
307 const u8 *supp_rates, size_t supp_rates_len,
309 const struct ieee80211_ht_capabilities *ht_capab)
311 struct hostapd_sta_add_params params;
313 if (hapd->driver == NULL)
315 if (hapd->driver->sta_add == NULL)
318 os_memset(¶ms, 0, sizeof(params));
321 params.capability = capability;
322 params.supp_rates = supp_rates;
323 params.supp_rates_len = supp_rates_len;
324 params.listen_interval = listen_interval;
325 params.ht_capabilities = ht_capab;
326 return hapd->driver->sta_add(hapd->drv_priv, ¶ms);
330 static int hostapd_sta_remove(struct hostapd_data *hapd, const u8 *addr)
332 if (hapd->driver == NULL || hapd->driver->sta_remove == NULL)
334 return hapd->driver->sta_remove(hapd->drv_priv, addr);
338 static int hostapd_set_countermeasures(struct hostapd_data *hapd, int enabled)
340 if (hapd->driver == NULL ||
341 hapd->driver->hapd_set_countermeasures == NULL)
343 return hapd->driver->hapd_set_countermeasures(hapd->drv_priv, enabled);
347 void hostapd_set_driver_ops(struct hostapd_driver_ops *ops)
349 ops->set_ap_wps_ie = hostapd_set_ap_wps_ie;
350 ops->send_mgmt_frame = hostapd_send_mgmt_frame;
351 ops->send_eapol = hostapd_send_eapol;
352 ops->set_authorized = hostapd_set_authorized;
353 ops->set_key = hostapd_set_key;
354 ops->read_sta_data = hostapd_read_sta_data;
355 ops->sta_clear_stats = hostapd_sta_clear_stats;
356 ops->set_sta_flags = hostapd_set_sta_flags;
357 ops->set_drv_ieee8021x = hostapd_set_drv_ieee8021x;
358 ops->set_radius_acl_auth = hostapd_set_radius_acl_auth;
359 ops->set_radius_acl_expire = hostapd_set_radius_acl_expire;
360 ops->set_bss_params = hostapd_set_bss_params;
361 ops->set_beacon = hostapd_set_beacon;
362 ops->vlan_if_add = hostapd_vlan_if_add;
363 ops->vlan_if_remove = hostapd_vlan_if_remove;
364 ops->set_wds_sta = hostapd_set_wds_sta;
365 ops->set_sta_vlan = hostapd_set_sta_vlan;
366 ops->get_inact_sec = hostapd_get_inact_sec;
367 ops->sta_deauth = hostapd_sta_deauth;
368 ops->sta_disassoc = hostapd_sta_disassoc;
369 ops->sta_add = hostapd_sta_add;
370 ops->sta_remove = hostapd_sta_remove;
371 ops->set_countermeasures = hostapd_set_countermeasures;
375 int hostapd_set_privacy(struct hostapd_data *hapd, int enabled)
377 if (hapd->driver == NULL || hapd->driver->set_privacy == NULL)
379 return hapd->driver->set_privacy(hapd->drv_priv, enabled);
383 int hostapd_set_generic_elem(struct hostapd_data *hapd, const u8 *elem,
386 if (hapd->driver == NULL || hapd->driver->set_generic_elem == NULL)
388 return hapd->driver->set_generic_elem(hapd->drv_priv, elem, elem_len);
392 int hostapd_get_ssid(struct hostapd_data *hapd, u8 *buf, size_t len)
394 if (hapd->driver == NULL || hapd->driver->hapd_get_ssid == NULL)
396 return hapd->driver->hapd_get_ssid(hapd->drv_priv, buf, len);
400 int hostapd_set_ssid(struct hostapd_data *hapd, const u8 *buf, size_t len)
402 if (hapd->driver == NULL || hapd->driver->hapd_set_ssid == NULL)
404 return hapd->driver->hapd_set_ssid(hapd->drv_priv, buf, len);
408 int hostapd_if_add(struct hostapd_data *hapd, enum wpa_driver_if_type type,
409 const char *ifname, const u8 *addr, void *bss_ctx,
410 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);
419 int hostapd_if_remove(struct hostapd_data *hapd, enum wpa_driver_if_type type,
422 if (hapd->driver == NULL || hapd->driver->if_remove == NULL)
424 return hapd->driver->if_remove(hapd->drv_priv, type, ifname);
428 int hostapd_set_ieee8021x(struct hostapd_data *hapd,
429 struct wpa_bss_params *params)
431 if (hapd->driver == NULL || hapd->driver->set_ieee8021x == NULL)
433 return hapd->driver->set_ieee8021x(hapd->drv_priv, params);
437 int hostapd_get_seqnum(const char *ifname, struct hostapd_data *hapd,
438 const u8 *addr, int idx, u8 *seq)
440 if (hapd->driver == NULL || hapd->driver->get_seqnum == NULL)
442 return hapd->driver->get_seqnum(ifname, hapd->drv_priv, addr, idx,
447 int hostapd_flush(struct hostapd_data *hapd)
449 if (hapd->driver == NULL || hapd->driver->flush == NULL)
451 return hapd->driver->flush(hapd->drv_priv);
455 int hostapd_set_freq(struct hostapd_data *hapd, int mode, int freq,
456 int channel, int ht_enabled, int sec_channel_offset)
458 struct hostapd_freq_params data;
459 if (hapd->driver == NULL)
461 if (hapd->driver->set_freq == NULL)
463 os_memset(&data, 0, sizeof(data));
466 data.channel = channel;
467 data.ht_enabled = ht_enabled;
468 data.sec_channel_offset = sec_channel_offset;
469 return hapd->driver->set_freq(hapd->drv_priv, &data);
472 int hostapd_set_rts(struct hostapd_data *hapd, int rts)
474 if (hapd->driver == NULL || hapd->driver->set_rts == NULL)
476 return hapd->driver->set_rts(hapd->drv_priv, rts);
480 int hostapd_set_frag(struct hostapd_data *hapd, int frag)
482 if (hapd->driver == NULL || hapd->driver->set_frag == NULL)
484 return hapd->driver->set_frag(hapd->drv_priv, frag);
488 int hostapd_sta_set_flags(struct hostapd_data *hapd, u8 *addr,
489 int total_flags, int flags_or, int flags_and)
491 if (hapd->driver == NULL || hapd->driver->sta_set_flags == NULL)
493 return hapd->driver->sta_set_flags(hapd->drv_priv, addr, total_flags,
494 flags_or, flags_and);
498 int hostapd_set_rate_sets(struct hostapd_data *hapd, int *supp_rates,
499 int *basic_rates, int mode)
501 if (hapd->driver == NULL || hapd->driver->set_rate_sets == NULL)
503 return hapd->driver->set_rate_sets(hapd->drv_priv, supp_rates,
508 int hostapd_set_country(struct hostapd_data *hapd, const char *country)
510 if (hapd->driver == NULL ||
511 hapd->driver->set_country == NULL)
513 return hapd->driver->set_country(hapd->drv_priv, country);
517 int hostapd_set_cts_protect(struct hostapd_data *hapd, int value)
519 if (hapd->driver == NULL || hapd->driver->set_cts_protect == NULL)
521 return hapd->driver->set_cts_protect(hapd->drv_priv, value);
525 int hostapd_set_preamble(struct hostapd_data *hapd, int value)
527 if (hapd->driver == NULL || hapd->driver->set_preamble == NULL)
529 return hapd->driver->set_preamble(hapd->drv_priv, value);
533 int hostapd_set_short_slot_time(struct hostapd_data *hapd, int value)
535 if (hapd->driver == NULL || hapd->driver->set_short_slot_time == NULL)
537 return hapd->driver->set_short_slot_time(hapd->drv_priv, value);
541 int hostapd_set_tx_queue_params(struct hostapd_data *hapd, int queue, int aifs,
542 int cw_min, int cw_max, int burst_time)
544 if (hapd->driver == NULL || hapd->driver->set_tx_queue_params == NULL)
546 return hapd->driver->set_tx_queue_params(hapd->drv_priv, queue, aifs,
547 cw_min, cw_max, burst_time);
551 int hostapd_valid_bss_mask(struct hostapd_data *hapd, const u8 *addr,
554 if (hapd->driver == NULL || hapd->driver->valid_bss_mask == NULL)
556 return hapd->driver->valid_bss_mask(hapd->drv_priv, addr, mask);
560 struct hostapd_hw_modes *
561 hostapd_get_hw_feature_data(struct hostapd_data *hapd, u16 *num_modes,
564 if (hapd->driver == NULL ||
565 hapd->driver->get_hw_feature_data == NULL)
567 return hapd->driver->get_hw_feature_data(hapd->drv_priv, num_modes,
572 int hostapd_driver_commit(struct hostapd_data *hapd)
574 if (hapd->driver == NULL || hapd->driver->commit == NULL)
576 return hapd->driver->commit(hapd->drv_priv);
580 int hostapd_set_ht_params(struct hostapd_data *hapd,
581 const u8 *ht_capab, size_t ht_capab_len,
582 const u8 *ht_oper, size_t ht_oper_len)
584 if (hapd->driver == NULL || hapd->driver->set_ht_params == NULL ||
585 ht_capab == NULL || ht_oper == NULL)
587 return hapd->driver->set_ht_params(hapd->drv_priv,
588 ht_capab, ht_capab_len,
589 ht_oper, ht_oper_len);
593 int hostapd_drv_none(struct hostapd_data *hapd)
595 return hapd->driver && os_strcmp(hapd->driver->name, "none") == 0;
599 int hostapd_driver_scan(struct hostapd_data *hapd,
600 struct wpa_driver_scan_params *params)
602 if (hapd->driver && hapd->driver->scan2)
603 return hapd->driver->scan2(hapd->drv_priv, params);
608 struct wpa_scan_results * hostapd_driver_get_scan_results(
609 struct hostapd_data *hapd)
611 if (hapd->driver && hapd->driver->get_scan_results2)
612 return hapd->driver->get_scan_results2(hapd->drv_priv);