ap_drv_ops.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. /*
  2. * hostapd - Driver operations
  3. * Copyright (c) 2009-2010, 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 "utils/includes.h"
  15. #include "utils/common.h"
  16. #include "drivers/driver.h"
  17. #include "common/ieee802_11_defs.h"
  18. #include "wps/wps.h"
  19. #include "hostapd.h"
  20. #include "ieee802_11.h"
  21. #include "sta_info.h"
  22. #include "ap_config.h"
  23. #include "p2p_hostapd.h"
  24. #include "ap_drv_ops.h"
  25. u32 hostapd_sta_flags_to_drv(u32 flags)
  26. {
  27. int res = 0;
  28. if (flags & WLAN_STA_AUTHORIZED)
  29. res |= WPA_STA_AUTHORIZED;
  30. if (flags & WLAN_STA_WMM)
  31. res |= WPA_STA_WMM;
  32. if (flags & WLAN_STA_SHORT_PREAMBLE)
  33. res |= WPA_STA_SHORT_PREAMBLE;
  34. if (flags & WLAN_STA_MFP)
  35. res |= WPA_STA_MFP;
  36. return res;
  37. }
  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)
  42. {
  43. struct wpabuf *beacon = NULL, *proberesp = NULL, *assocresp = NULL;
  44. u8 buf[100], *pos;
  45. *beacon_ret = *proberesp_ret = *assocresp_ret = NULL;
  46. pos = buf;
  47. pos = hostapd_eid_ext_capab(hapd, pos);
  48. if (pos != buf) {
  49. if (wpabuf_resize(&assocresp, pos - buf) != 0)
  50. goto fail;
  51. wpabuf_put_data(assocresp, buf, pos - buf);
  52. }
  53. pos = hostapd_eid_interworking(hapd, pos);
  54. pos = hostapd_eid_adv_proto(hapd, pos);
  55. if (pos != buf) {
  56. if (wpabuf_resize(&beacon, pos - buf) != 0)
  57. goto fail;
  58. wpabuf_put_data(beacon, buf, pos - buf);
  59. if (wpabuf_resize(&proberesp, pos - buf) != 0)
  60. goto fail;
  61. wpabuf_put_data(proberesp, buf, pos - buf);
  62. }
  63. if (hapd->wps_beacon_ie) {
  64. if (wpabuf_resize(&beacon, wpabuf_len(hapd->wps_beacon_ie)) <
  65. 0)
  66. goto fail;
  67. wpabuf_put_buf(beacon, hapd->wps_beacon_ie);
  68. }
  69. if (hapd->wps_probe_resp_ie) {
  70. if (wpabuf_resize(&proberesp,
  71. wpabuf_len(hapd->wps_probe_resp_ie)) < 0)
  72. goto fail;
  73. wpabuf_put_buf(proberesp, hapd->wps_probe_resp_ie);
  74. }
  75. #ifdef CONFIG_P2P
  76. if (hapd->p2p_beacon_ie) {
  77. if (wpabuf_resize(&beacon, wpabuf_len(hapd->p2p_beacon_ie)) <
  78. 0)
  79. goto fail;
  80. wpabuf_put_buf(beacon, hapd->p2p_beacon_ie);
  81. }
  82. if (hapd->p2p_probe_resp_ie) {
  83. if (wpabuf_resize(&beacon, wpabuf_len(hapd->p2p_probe_resp_ie))
  84. < 0)
  85. goto fail;
  86. wpabuf_put_buf(beacon, hapd->p2p_probe_resp_ie);
  87. }
  88. #endif /* CONFIG_P2P */
  89. #ifdef CONFIG_P2P_MANAGER
  90. if (hapd->conf->p2p & P2P_MANAGE) {
  91. if (wpabuf_resize(&beacon, 100) == 0) {
  92. u8 *start, *p;
  93. start = wpabuf_put(beacon, 0);
  94. p = hostapd_eid_p2p_manage(hapd, start);
  95. wpabuf_put(beacon, p - start);
  96. }
  97. if (wpabuf_resize(&proberesp, 100) == 0) {
  98. u8 *start, *p;
  99. start = wpabuf_put(proberesp, 0);
  100. p = hostapd_eid_p2p_manage(hapd, start);
  101. wpabuf_put(proberesp, p - start);
  102. }
  103. }
  104. #endif /* CONFIG_P2P_MANAGER */
  105. #ifdef CONFIG_WPS2
  106. if (hapd->conf->wps_state) {
  107. struct wpabuf *a = wps_build_assoc_resp_ie();
  108. if (a && wpabuf_resize(&assocresp, wpabuf_len(a)) == 0)
  109. wpabuf_put_buf(assocresp, a);
  110. wpabuf_free(a);
  111. }
  112. #endif /* CONFIG_WPS2 */
  113. #ifdef CONFIG_P2P_MANAGER
  114. if (hapd->conf->p2p & P2P_MANAGE) {
  115. if (wpabuf_resize(&assocresp, 100) == 0) {
  116. u8 *start, *p;
  117. start = wpabuf_put(assocresp, 0);
  118. p = hostapd_eid_p2p_manage(hapd, start);
  119. wpabuf_put(assocresp, p - start);
  120. }
  121. }
  122. #endif /* CONFIG_P2P_MANAGER */
  123. *beacon_ret = beacon;
  124. *proberesp_ret = proberesp;
  125. *assocresp_ret = assocresp;
  126. return 0;
  127. fail:
  128. wpabuf_free(beacon);
  129. wpabuf_free(proberesp);
  130. wpabuf_free(assocresp);
  131. return -1;
  132. }
  133. void hostapd_free_ap_extra_ies(struct hostapd_data *hapd,
  134. struct wpabuf *beacon,
  135. struct wpabuf *proberesp,
  136. struct wpabuf *assocresp)
  137. {
  138. wpabuf_free(beacon);
  139. wpabuf_free(proberesp);
  140. wpabuf_free(assocresp);
  141. }
  142. int hostapd_set_ap_wps_ie(struct hostapd_data *hapd)
  143. {
  144. struct wpabuf *beacon, *proberesp, *assocresp;
  145. int ret;
  146. if (hapd->driver == NULL || hapd->driver->set_ap_wps_ie == NULL)
  147. return 0;
  148. if (hostapd_build_ap_extra_ies(hapd, &beacon, &proberesp, &assocresp) <
  149. 0)
  150. return -1;
  151. ret = hapd->driver->set_ap_wps_ie(hapd->drv_priv, beacon, proberesp,
  152. assocresp);
  153. hostapd_free_ap_extra_ies(hapd, beacon, proberesp, assocresp);
  154. return ret;
  155. }
  156. int hostapd_set_authorized(struct hostapd_data *hapd,
  157. struct sta_info *sta, int authorized)
  158. {
  159. if (authorized) {
  160. return hostapd_sta_set_flags(hapd, sta->addr,
  161. hostapd_sta_flags_to_drv(
  162. sta->flags),
  163. WPA_STA_AUTHORIZED, ~0);
  164. }
  165. return hostapd_sta_set_flags(hapd, sta->addr,
  166. hostapd_sta_flags_to_drv(sta->flags),
  167. 0, ~WPA_STA_AUTHORIZED);
  168. }
  169. int hostapd_set_sta_flags(struct hostapd_data *hapd, struct sta_info *sta)
  170. {
  171. int set_flags, total_flags, flags_and, flags_or;
  172. total_flags = hostapd_sta_flags_to_drv(sta->flags);
  173. set_flags = WPA_STA_SHORT_PREAMBLE | WPA_STA_WMM | WPA_STA_MFP;
  174. if (((!hapd->conf->ieee802_1x && !hapd->conf->wpa) ||
  175. sta->auth_alg == WLAN_AUTH_FT) &&
  176. sta->flags & WLAN_STA_AUTHORIZED)
  177. set_flags |= WPA_STA_AUTHORIZED;
  178. flags_or = total_flags & set_flags;
  179. flags_and = total_flags | ~set_flags;
  180. return hostapd_sta_set_flags(hapd, sta->addr, total_flags,
  181. flags_or, flags_and);
  182. }
  183. int hostapd_set_drv_ieee8021x(struct hostapd_data *hapd, const char *ifname,
  184. int enabled)
  185. {
  186. struct wpa_bss_params params;
  187. os_memset(&params, 0, sizeof(params));
  188. params.ifname = ifname;
  189. params.enabled = enabled;
  190. if (enabled) {
  191. params.wpa = hapd->conf->wpa;
  192. params.ieee802_1x = hapd->conf->ieee802_1x;
  193. params.wpa_group = hapd->conf->wpa_group;
  194. params.wpa_pairwise = hapd->conf->wpa_pairwise;
  195. params.wpa_key_mgmt = hapd->conf->wpa_key_mgmt;
  196. params.rsn_preauth = hapd->conf->rsn_preauth;
  197. #ifdef CONFIG_IEEE80211W
  198. params.ieee80211w = hapd->conf->ieee80211w;
  199. #endif /* CONFIG_IEEE80211W */
  200. }
  201. return hostapd_set_ieee8021x(hapd, &params);
  202. }
  203. int hostapd_vlan_if_add(struct hostapd_data *hapd, const char *ifname)
  204. {
  205. char force_ifname[IFNAMSIZ];
  206. u8 if_addr[ETH_ALEN];
  207. return hostapd_if_add(hapd, WPA_IF_AP_VLAN, ifname, hapd->own_addr,
  208. NULL, NULL, force_ifname, if_addr, NULL);
  209. }
  210. int hostapd_vlan_if_remove(struct hostapd_data *hapd, const char *ifname)
  211. {
  212. return hostapd_if_remove(hapd, WPA_IF_AP_VLAN, ifname);
  213. }
  214. int hostapd_set_wds_sta(struct hostapd_data *hapd, const u8 *addr, int aid,
  215. int val)
  216. {
  217. const char *bridge = NULL;
  218. if (hapd->driver == NULL || hapd->driver->set_wds_sta == NULL)
  219. return 0;
  220. if (hapd->conf->wds_bridge[0])
  221. bridge = hapd->conf->wds_bridge;
  222. else if (hapd->conf->bridge[0])
  223. bridge = hapd->conf->bridge;
  224. return hapd->driver->set_wds_sta(hapd->drv_priv, addr, aid, val,
  225. bridge);
  226. }
  227. int hostapd_add_sta_node(struct hostapd_data *hapd, const u8 *addr,
  228. u16 auth_alg)
  229. {
  230. if (hapd->driver == NULL || hapd->driver->add_sta_node == NULL)
  231. return 0;
  232. return hapd->driver->add_sta_node(hapd->drv_priv, addr, auth_alg);
  233. }
  234. int hostapd_sta_auth(struct hostapd_data *hapd, const u8 *addr,
  235. u16 seq, u16 status, const u8 *ie, size_t len)
  236. {
  237. if (hapd->driver == NULL || hapd->driver->sta_auth == NULL)
  238. return 0;
  239. return hapd->driver->sta_auth(hapd->drv_priv, hapd->own_addr, addr,
  240. seq, status, ie, len);
  241. }
  242. int hostapd_sta_assoc(struct hostapd_data *hapd, const u8 *addr,
  243. int reassoc, u16 status, const u8 *ie, size_t len)
  244. {
  245. if (hapd->driver == NULL || hapd->driver->sta_assoc == NULL)
  246. return 0;
  247. return hapd->driver->sta_assoc(hapd->drv_priv, hapd->own_addr, addr,
  248. reassoc, status, ie, len);
  249. }
  250. int hostapd_sta_add(struct hostapd_data *hapd,
  251. const u8 *addr, u16 aid, u16 capability,
  252. const u8 *supp_rates, size_t supp_rates_len,
  253. u16 listen_interval,
  254. const struct ieee80211_ht_capabilities *ht_capab,
  255. u32 flags)
  256. {
  257. struct hostapd_sta_add_params params;
  258. if (hapd->driver == NULL)
  259. return 0;
  260. if (hapd->driver->sta_add == NULL)
  261. return 0;
  262. os_memset(&params, 0, sizeof(params));
  263. params.addr = addr;
  264. params.aid = aid;
  265. params.capability = capability;
  266. params.supp_rates = supp_rates;
  267. params.supp_rates_len = supp_rates_len;
  268. params.listen_interval = listen_interval;
  269. params.ht_capabilities = ht_capab;
  270. params.flags = hostapd_sta_flags_to_drv(flags);
  271. return hapd->driver->sta_add(hapd->drv_priv, &params);
  272. }
  273. int hostapd_add_tspec(struct hostapd_data *hapd, const u8 *addr,
  274. u8 *tspec_ie, size_t tspec_ielen)
  275. {
  276. if (hapd->driver == NULL || hapd->driver->add_tspec == NULL)
  277. return 0;
  278. return hapd->driver->add_tspec(hapd->drv_priv, addr, tspec_ie,
  279. tspec_ielen);
  280. }
  281. int hostapd_set_privacy(struct hostapd_data *hapd, int enabled)
  282. {
  283. if (hapd->driver == NULL || hapd->driver->set_privacy == NULL)
  284. return 0;
  285. return hapd->driver->set_privacy(hapd->drv_priv, enabled);
  286. }
  287. int hostapd_set_generic_elem(struct hostapd_data *hapd, const u8 *elem,
  288. size_t elem_len)
  289. {
  290. if (hapd->driver == NULL || hapd->driver->set_generic_elem == NULL)
  291. return 0;
  292. return hapd->driver->set_generic_elem(hapd->drv_priv, elem, elem_len);
  293. }
  294. int hostapd_get_ssid(struct hostapd_data *hapd, u8 *buf, size_t len)
  295. {
  296. if (hapd->driver == NULL || hapd->driver->hapd_get_ssid == NULL)
  297. return 0;
  298. return hapd->driver->hapd_get_ssid(hapd->drv_priv, buf, len);
  299. }
  300. int hostapd_set_ssid(struct hostapd_data *hapd, const u8 *buf, size_t len)
  301. {
  302. if (hapd->driver == NULL || hapd->driver->hapd_set_ssid == NULL)
  303. return 0;
  304. return hapd->driver->hapd_set_ssid(hapd->drv_priv, buf, len);
  305. }
  306. int hostapd_if_add(struct hostapd_data *hapd, enum wpa_driver_if_type type,
  307. const char *ifname, const u8 *addr, void *bss_ctx,
  308. void **drv_priv, char *force_ifname, u8 *if_addr,
  309. const char *bridge)
  310. {
  311. if (hapd->driver == NULL || hapd->driver->if_add == NULL)
  312. return -1;
  313. return hapd->driver->if_add(hapd->drv_priv, type, ifname, addr,
  314. bss_ctx, drv_priv, force_ifname, if_addr,
  315. bridge);
  316. }
  317. int hostapd_if_remove(struct hostapd_data *hapd, enum wpa_driver_if_type type,
  318. const char *ifname)
  319. {
  320. if (hapd->driver == NULL || hapd->driver->if_remove == NULL)
  321. return -1;
  322. return hapd->driver->if_remove(hapd->drv_priv, type, ifname);
  323. }
  324. int hostapd_set_ieee8021x(struct hostapd_data *hapd,
  325. struct wpa_bss_params *params)
  326. {
  327. if (hapd->driver == NULL || hapd->driver->set_ieee8021x == NULL)
  328. return 0;
  329. return hapd->driver->set_ieee8021x(hapd->drv_priv, params);
  330. }
  331. int hostapd_get_seqnum(const char *ifname, struct hostapd_data *hapd,
  332. const u8 *addr, int idx, u8 *seq)
  333. {
  334. if (hapd->driver == NULL || hapd->driver->get_seqnum == NULL)
  335. return 0;
  336. return hapd->driver->get_seqnum(ifname, hapd->drv_priv, addr, idx,
  337. seq);
  338. }
  339. int hostapd_flush(struct hostapd_data *hapd)
  340. {
  341. if (hapd->driver == NULL || hapd->driver->flush == NULL)
  342. return 0;
  343. return hapd->driver->flush(hapd->drv_priv);
  344. }
  345. int hostapd_set_freq(struct hostapd_data *hapd, int mode, int freq,
  346. int channel, int ht_enabled, int sec_channel_offset)
  347. {
  348. struct hostapd_freq_params data;
  349. if (hapd->driver == NULL)
  350. return 0;
  351. if (hapd->driver->set_freq == NULL)
  352. return 0;
  353. os_memset(&data, 0, sizeof(data));
  354. data.mode = mode;
  355. data.freq = freq;
  356. data.channel = channel;
  357. data.ht_enabled = ht_enabled;
  358. data.sec_channel_offset = sec_channel_offset;
  359. return hapd->driver->set_freq(hapd->drv_priv, &data);
  360. }
  361. int hostapd_set_rts(struct hostapd_data *hapd, int rts)
  362. {
  363. if (hapd->driver == NULL || hapd->driver->set_rts == NULL)
  364. return 0;
  365. return hapd->driver->set_rts(hapd->drv_priv, rts);
  366. }
  367. int hostapd_set_frag(struct hostapd_data *hapd, int frag)
  368. {
  369. if (hapd->driver == NULL || hapd->driver->set_frag == NULL)
  370. return 0;
  371. return hapd->driver->set_frag(hapd->drv_priv, frag);
  372. }
  373. int hostapd_sta_set_flags(struct hostapd_data *hapd, u8 *addr,
  374. int total_flags, int flags_or, int flags_and)
  375. {
  376. if (hapd->driver == NULL || hapd->driver->sta_set_flags == NULL)
  377. return 0;
  378. return hapd->driver->sta_set_flags(hapd->drv_priv, addr, total_flags,
  379. flags_or, flags_and);
  380. }
  381. int hostapd_set_rate_sets(struct hostapd_data *hapd, int *supp_rates,
  382. int *basic_rates, int mode)
  383. {
  384. if (hapd->driver == NULL || hapd->driver->set_rate_sets == NULL)
  385. return 0;
  386. return hapd->driver->set_rate_sets(hapd->drv_priv, supp_rates,
  387. basic_rates, mode);
  388. }
  389. int hostapd_set_country(struct hostapd_data *hapd, const char *country)
  390. {
  391. if (hapd->driver == NULL ||
  392. hapd->driver->set_country == NULL)
  393. return 0;
  394. return hapd->driver->set_country(hapd->drv_priv, country);
  395. }
  396. int hostapd_set_tx_queue_params(struct hostapd_data *hapd, int queue, int aifs,
  397. int cw_min, int cw_max, int burst_time)
  398. {
  399. if (hapd->driver == NULL || hapd->driver->set_tx_queue_params == NULL)
  400. return 0;
  401. return hapd->driver->set_tx_queue_params(hapd->drv_priv, queue, aifs,
  402. cw_min, cw_max, burst_time);
  403. }
  404. int hostapd_valid_bss_mask(struct hostapd_data *hapd, const u8 *addr,
  405. const u8 *mask)
  406. {
  407. if (hapd->driver == NULL || hapd->driver->valid_bss_mask == NULL)
  408. return 1;
  409. return hapd->driver->valid_bss_mask(hapd->drv_priv, addr, mask);
  410. }
  411. struct hostapd_hw_modes *
  412. hostapd_get_hw_feature_data(struct hostapd_data *hapd, u16 *num_modes,
  413. u16 *flags)
  414. {
  415. if (hapd->driver == NULL ||
  416. hapd->driver->get_hw_feature_data == NULL)
  417. return NULL;
  418. return hapd->driver->get_hw_feature_data(hapd->drv_priv, num_modes,
  419. flags);
  420. }
  421. int hostapd_driver_commit(struct hostapd_data *hapd)
  422. {
  423. if (hapd->driver == NULL || hapd->driver->commit == NULL)
  424. return 0;
  425. return hapd->driver->commit(hapd->drv_priv);
  426. }
  427. int hostapd_drv_none(struct hostapd_data *hapd)
  428. {
  429. return hapd->driver && os_strcmp(hapd->driver->name, "none") == 0;
  430. }
  431. int hostapd_driver_scan(struct hostapd_data *hapd,
  432. struct wpa_driver_scan_params *params)
  433. {
  434. if (hapd->driver && hapd->driver->scan2)
  435. return hapd->driver->scan2(hapd->drv_priv, params);
  436. return -1;
  437. }
  438. struct wpa_scan_results * hostapd_driver_get_scan_results(
  439. struct hostapd_data *hapd)
  440. {
  441. if (hapd->driver && hapd->driver->get_scan_results2)
  442. return hapd->driver->get_scan_results2(hapd->drv_priv);
  443. return NULL;
  444. }
  445. int hostapd_driver_set_noa(struct hostapd_data *hapd, u8 count, int start,
  446. int duration)
  447. {
  448. if (hapd->driver && hapd->driver->set_noa)
  449. return hapd->driver->set_noa(hapd->drv_priv, count, start,
  450. duration);
  451. return -1;
  452. }
  453. int hostapd_drv_set_key(const char *ifname, struct hostapd_data *hapd,
  454. enum wpa_alg alg, const u8 *addr,
  455. int key_idx, int set_tx,
  456. const u8 *seq, size_t seq_len,
  457. const u8 *key, size_t key_len)
  458. {
  459. if (hapd->driver == NULL || hapd->driver->set_key == NULL)
  460. return 0;
  461. return hapd->driver->set_key(ifname, hapd->drv_priv, alg, addr,
  462. key_idx, set_tx, seq, seq_len, key,
  463. key_len);
  464. }
  465. int hostapd_drv_send_mlme(struct hostapd_data *hapd,
  466. const void *msg, size_t len)
  467. {
  468. if (hapd->driver == NULL || hapd->driver->send_mlme == NULL)
  469. return 0;
  470. return hapd->driver->send_mlme(hapd->drv_priv, msg, len);
  471. }
  472. int hostapd_drv_sta_deauth(struct hostapd_data *hapd,
  473. const u8 *addr, int reason)
  474. {
  475. if (hapd->driver == NULL || hapd->driver->sta_deauth == NULL)
  476. return 0;
  477. return hapd->driver->sta_deauth(hapd->drv_priv, hapd->own_addr, addr,
  478. reason);
  479. }
  480. int hostapd_drv_sta_disassoc(struct hostapd_data *hapd,
  481. const u8 *addr, int reason)
  482. {
  483. if (hapd->driver == NULL || hapd->driver->sta_disassoc == NULL)
  484. return 0;
  485. return hapd->driver->sta_disassoc(hapd->drv_priv, hapd->own_addr, addr,
  486. reason);
  487. }