ap_drv_ops.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * hostapd - Driver operations
  3. * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
  4. *
  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.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "ap/hostapd.h"
  17. #include "ap/ieee802_11.h"
  18. #include "ap/sta_info.h"
  19. #include "driver_i.h"
  20. #include "ap_drv_ops.h"
  21. static int hostapd_sta_flags_to_drv(int flags)
  22. {
  23. int res = 0;
  24. if (flags & WLAN_STA_AUTHORIZED)
  25. res |= WPA_STA_AUTHORIZED;
  26. if (flags & WLAN_STA_WMM)
  27. res |= WPA_STA_WMM;
  28. if (flags & WLAN_STA_SHORT_PREAMBLE)
  29. res |= WPA_STA_SHORT_PREAMBLE;
  30. if (flags & WLAN_STA_MFP)
  31. res |= WPA_STA_MFP;
  32. return res;
  33. }
  34. static int hostapd_set_ap_wps_ie(struct hostapd_data *hapd,
  35. const struct wpabuf *beacon,
  36. const struct wpabuf *proberesp)
  37. {
  38. if (hapd->driver == NULL || hapd->driver->set_ap_wps_ie == NULL)
  39. return 0;
  40. return hapd->driver->set_ap_wps_ie(hapd->conf->iface, hapd->drv_priv,
  41. beacon, proberesp);
  42. }
  43. static int hostapd_send_mgmt_frame(struct hostapd_data *hapd, const void *msg,
  44. size_t len)
  45. {
  46. if (hapd->driver == NULL || hapd->driver->send_mlme == NULL)
  47. return 0;
  48. return hapd->driver->send_mlme(hapd->drv_priv, msg, len);
  49. }
  50. static int hostapd_send_eapol(struct hostapd_data *hapd, const u8 *addr,
  51. const u8 *data, size_t data_len, int encrypt)
  52. {
  53. if (hapd->driver == NULL || hapd->driver->hapd_send_eapol == NULL)
  54. return 0;
  55. return hapd->driver->hapd_send_eapol(hapd->drv_priv, addr, data,
  56. data_len, encrypt,
  57. hapd->own_addr);
  58. }
  59. static int hostapd_set_authorized(struct hostapd_data *hapd,
  60. struct sta_info *sta, int authorized)
  61. {
  62. if (authorized) {
  63. return hostapd_sta_set_flags(hapd, sta->addr,
  64. hostapd_sta_flags_to_drv(
  65. sta->flags),
  66. WPA_STA_AUTHORIZED, ~0);
  67. }
  68. return hostapd_sta_set_flags(hapd, sta->addr,
  69. hostapd_sta_flags_to_drv(sta->flags),
  70. 0, ~WPA_STA_AUTHORIZED);
  71. }
  72. static int hostapd_set_key(const char *ifname, struct hostapd_data *hapd,
  73. wpa_alg alg, const u8 *addr, int key_idx,
  74. int set_tx, const u8 *seq, size_t seq_len,
  75. const u8 *key, size_t key_len)
  76. {
  77. if (hapd->driver == NULL || hapd->driver->set_key == NULL)
  78. return 0;
  79. return hapd->driver->set_key(ifname, hapd->drv_priv, alg, addr,
  80. key_idx, set_tx, seq, seq_len, key,
  81. key_len);
  82. }
  83. static int hostapd_read_sta_data(struct hostapd_data *hapd,
  84. struct hostap_sta_driver_data *data,
  85. const u8 *addr)
  86. {
  87. if (hapd->driver == NULL || hapd->driver->read_sta_data == NULL)
  88. return -1;
  89. return hapd->driver->read_sta_data(hapd->drv_priv, data, addr);
  90. }
  91. static int hostapd_sta_clear_stats(struct hostapd_data *hapd, const u8 *addr)
  92. {
  93. if (hapd->driver == NULL || hapd->driver->sta_clear_stats == NULL)
  94. return 0;
  95. return hapd->driver->sta_clear_stats(hapd->drv_priv, addr);
  96. }
  97. static int hostapd_set_sta_flags(struct hostapd_data *hapd,
  98. struct sta_info *sta)
  99. {
  100. int set_flags, total_flags, flags_and, flags_or;
  101. total_flags = hostapd_sta_flags_to_drv(sta->flags);
  102. set_flags = WPA_STA_SHORT_PREAMBLE | WPA_STA_WMM | WPA_STA_MFP;
  103. if (!hapd->conf->ieee802_1x && !hapd->conf->wpa &&
  104. sta->flags & WLAN_STA_AUTHORIZED)
  105. set_flags |= WPA_STA_AUTHORIZED;
  106. flags_or = total_flags & set_flags;
  107. flags_and = total_flags | ~set_flags;
  108. return hostapd_sta_set_flags(hapd, sta->addr, total_flags,
  109. flags_or, flags_and);
  110. }
  111. static int hostapd_set_drv_ieee8021x(struct hostapd_data *hapd,
  112. const char *ifname, int enabled)
  113. {
  114. struct wpa_bss_params params;
  115. os_memset(&params, 0, sizeof(params));
  116. params.ifname = ifname;
  117. params.enabled = enabled;
  118. if (enabled) {
  119. params.wpa = hapd->conf->wpa;
  120. params.ieee802_1x = hapd->conf->ieee802_1x;
  121. params.wpa_group = hapd->conf->wpa_group;
  122. params.wpa_pairwise = hapd->conf->wpa_pairwise;
  123. params.wpa_key_mgmt = hapd->conf->wpa_key_mgmt;
  124. params.rsn_preauth = hapd->conf->rsn_preauth;
  125. }
  126. return hostapd_set_ieee8021x(hapd, &params);
  127. }
  128. static int hostapd_set_radius_acl_auth(struct hostapd_data *hapd,
  129. const u8 *mac, int accepted,
  130. u32 session_timeout)
  131. {
  132. if (hapd->driver == NULL || hapd->driver->set_radius_acl_auth == NULL)
  133. return 0;
  134. return hapd->driver->set_radius_acl_auth(hapd->drv_priv, mac, accepted,
  135. session_timeout);
  136. }
  137. static int hostapd_set_radius_acl_expire(struct hostapd_data *hapd,
  138. const u8 *mac)
  139. {
  140. if (hapd->driver == NULL ||
  141. hapd->driver->set_radius_acl_expire == NULL)
  142. return 0;
  143. return hapd->driver->set_radius_acl_expire(hapd->drv_priv, mac);
  144. }
  145. static int hostapd_set_bss_params(struct hostapd_data *hapd,
  146. int use_protection)
  147. {
  148. int ret = 0;
  149. int preamble;
  150. #ifdef CONFIG_IEEE80211N
  151. u8 buf[60], *ht_capab, *ht_oper, *pos;
  152. pos = buf;
  153. ht_capab = pos;
  154. pos = hostapd_eid_ht_capabilities(hapd, pos);
  155. ht_oper = pos;
  156. pos = hostapd_eid_ht_operation(hapd, pos);
  157. if (pos > ht_oper && ht_oper > ht_capab &&
  158. hostapd_set_ht_params(hapd->conf->iface, hapd,
  159. ht_capab + 2, ht_capab[1],
  160. ht_oper + 2, ht_oper[1])) {
  161. wpa_printf(MSG_ERROR, "Could not set HT capabilities "
  162. "for kernel driver");
  163. ret = -1;
  164. }
  165. #endif /* CONFIG_IEEE80211N */
  166. if (hostapd_set_cts_protect(hapd, use_protection)) {
  167. wpa_printf(MSG_ERROR, "Failed to set CTS protect in kernel "
  168. "driver");
  169. ret = -1;
  170. }
  171. if (hapd->iface->current_mode &&
  172. hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
  173. hostapd_set_short_slot_time(hapd,
  174. hapd->iface->num_sta_no_short_slot_time
  175. > 0 ? 0 : 1)) {
  176. wpa_printf(MSG_ERROR, "Failed to set Short Slot Time option "
  177. "in kernel driver");
  178. ret = -1;
  179. }
  180. if (hapd->iface->num_sta_no_short_preamble == 0 &&
  181. hapd->iconf->preamble == SHORT_PREAMBLE)
  182. preamble = SHORT_PREAMBLE;
  183. else
  184. preamble = LONG_PREAMBLE;
  185. if (hostapd_set_preamble(hapd, preamble)) {
  186. wpa_printf(MSG_ERROR, "Could not set preamble for kernel "
  187. "driver");
  188. ret = -1;
  189. }
  190. return ret;
  191. }
  192. static int hostapd_set_beacon(const char *ifname, struct hostapd_data *hapd,
  193. const u8 *head, size_t head_len,
  194. const u8 *tail, size_t tail_len, int dtim_period,
  195. int beacon_int)
  196. {
  197. if (hapd->driver == NULL || hapd->driver->set_beacon == NULL)
  198. return 0;
  199. return hapd->driver->set_beacon(ifname, hapd->drv_priv,
  200. head, head_len, tail, tail_len,
  201. dtim_period, beacon_int);
  202. }
  203. static int hostapd_vlan_if_add(struct hostapd_data *hapd, const char *ifname)
  204. {
  205. return hostapd_if_add(hapd, WPA_IF_AP_VLAN, ifname, NULL, NULL);
  206. }
  207. static int hostapd_vlan_if_remove(struct hostapd_data *hapd,
  208. const char *ifname)
  209. {
  210. return hostapd_if_remove(hapd, WPA_IF_AP_VLAN, ifname);
  211. }
  212. static int hostapd_set_wds_sta(struct hostapd_data *hapd, const u8 *addr,
  213. int aid, int val)
  214. {
  215. if (hapd->driver == NULL || hapd->driver->set_wds_sta == NULL)
  216. return 0;
  217. return hapd->driver->set_wds_sta(hapd->drv_priv, addr, aid, val);
  218. }
  219. static int hostapd_set_sta_vlan(const char *ifname, struct hostapd_data *hapd,
  220. const u8 *addr, int vlan_id)
  221. {
  222. if (hapd->driver == NULL || hapd->driver->set_sta_vlan == NULL)
  223. return 0;
  224. return hapd->driver->set_sta_vlan(hapd->drv_priv, addr, ifname,
  225. vlan_id);
  226. }
  227. static int hostapd_get_inact_sec(struct hostapd_data *hapd, const u8 *addr)
  228. {
  229. if (hapd->driver == NULL || hapd->driver->get_inact_sec == NULL)
  230. return 0;
  231. return hapd->driver->get_inact_sec(hapd->drv_priv, addr);
  232. }
  233. static int hostapd_sta_deauth(struct hostapd_data *hapd, const u8 *addr,
  234. int reason)
  235. {
  236. if (hapd->driver == NULL || hapd->driver->sta_deauth == NULL)
  237. return 0;
  238. return hapd->driver->sta_deauth(hapd->drv_priv, hapd->own_addr, addr,
  239. reason);
  240. }
  241. static int hostapd_sta_disassoc(struct hostapd_data *hapd, const u8 *addr,
  242. int reason)
  243. {
  244. if (hapd->driver == NULL || hapd->driver->sta_disassoc == NULL)
  245. return 0;
  246. return hapd->driver->sta_disassoc(hapd->drv_priv, hapd->own_addr, addr,
  247. reason);
  248. }
  249. static int hostapd_sta_add(const char *ifname, struct hostapd_data *hapd,
  250. const u8 *addr, u16 aid, u16 capability,
  251. const u8 *supp_rates, size_t supp_rates_len,
  252. u16 listen_interval,
  253. const struct ieee80211_ht_capabilities *ht_capab)
  254. {
  255. struct hostapd_sta_add_params params;
  256. if (hapd->driver == NULL)
  257. return 0;
  258. if (hapd->driver->sta_add == NULL)
  259. return 0;
  260. os_memset(&params, 0, sizeof(params));
  261. params.addr = addr;
  262. params.aid = aid;
  263. params.capability = capability;
  264. params.supp_rates = supp_rates;
  265. params.supp_rates_len = supp_rates_len;
  266. params.listen_interval = listen_interval;
  267. params.ht_capabilities = ht_capab;
  268. return hapd->driver->sta_add(ifname, hapd->drv_priv, &params);
  269. }
  270. static int hostapd_sta_remove(struct hostapd_data *hapd, const u8 *addr)
  271. {
  272. if (hapd->driver == NULL || hapd->driver->sta_remove == NULL)
  273. return 0;
  274. return hapd->driver->sta_remove(hapd->drv_priv, addr);
  275. }
  276. static int hostapd_set_countermeasures(struct hostapd_data *hapd, int enabled)
  277. {
  278. if (hapd->driver == NULL ||
  279. hapd->driver->hapd_set_countermeasures == NULL)
  280. return 0;
  281. return hapd->driver->hapd_set_countermeasures(hapd->drv_priv, enabled);
  282. }
  283. void hostapd_set_driver_ops(struct hostapd_driver_ops *ops)
  284. {
  285. ops->set_ap_wps_ie = hostapd_set_ap_wps_ie;
  286. ops->send_mgmt_frame = hostapd_send_mgmt_frame;
  287. ops->send_eapol = hostapd_send_eapol;
  288. ops->set_authorized = hostapd_set_authorized;
  289. ops->set_key = hostapd_set_key;
  290. ops->read_sta_data = hostapd_read_sta_data;
  291. ops->sta_clear_stats = hostapd_sta_clear_stats;
  292. ops->set_sta_flags = hostapd_set_sta_flags;
  293. ops->set_drv_ieee8021x = hostapd_set_drv_ieee8021x;
  294. ops->set_radius_acl_auth = hostapd_set_radius_acl_auth;
  295. ops->set_radius_acl_expire = hostapd_set_radius_acl_expire;
  296. ops->set_bss_params = hostapd_set_bss_params;
  297. ops->set_beacon = hostapd_set_beacon;
  298. ops->vlan_if_add = hostapd_vlan_if_add;
  299. ops->vlan_if_remove = hostapd_vlan_if_remove;
  300. ops->set_wds_sta = hostapd_set_wds_sta;
  301. ops->set_sta_vlan = hostapd_set_sta_vlan;
  302. ops->get_inact_sec = hostapd_get_inact_sec;
  303. ops->sta_deauth = hostapd_sta_deauth;
  304. ops->sta_disassoc = hostapd_sta_disassoc;
  305. ops->sta_add = hostapd_sta_add;
  306. ops->sta_remove = hostapd_sta_remove;
  307. ops->set_countermeasures = hostapd_set_countermeasures;
  308. }