ap_drv_ops.c 10 KB

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