drv_callbacks.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*
  2. * hostapd / Callback functions for driver wrappers
  3. * Copyright (c) 2002-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 "hostapd.h"
  17. #include "driver_i.h"
  18. #include "ieee802_11.h"
  19. #include "radius/radius.h"
  20. #include "sta_flags.h"
  21. #include "sta_info.h"
  22. #include "accounting.h"
  23. #include "tkip_countermeasures.h"
  24. #include "ieee802_1x.h"
  25. #include "wpa.h"
  26. #include "iapp.h"
  27. #include "wme.h"
  28. #include "wps_hostapd.h"
  29. struct prune_data {
  30. struct hostapd_data *hapd;
  31. const u8 *addr;
  32. };
  33. static int prune_associations(struct hostapd_iface *iface, void *ctx)
  34. {
  35. struct prune_data *data = ctx;
  36. struct sta_info *osta;
  37. struct hostapd_data *ohapd;
  38. size_t j;
  39. for (j = 0; j < iface->num_bss; j++) {
  40. ohapd = iface->bss[j];
  41. if (ohapd == data->hapd)
  42. continue;
  43. osta = ap_get_sta(ohapd, data->addr);
  44. if (!osta)
  45. continue;
  46. ap_sta_disassociate(ohapd, osta, WLAN_REASON_UNSPECIFIED);
  47. }
  48. return 0;
  49. }
  50. /**
  51. * hostapd_prune_associations - Remove extraneous associations
  52. * @hapd: Pointer to BSS data for the most recent association
  53. * @sta: Pointer to the associated STA data
  54. *
  55. * This function looks through all radios and BSS's for previous
  56. * (stale) associations of STA. If any are found they are removed.
  57. */
  58. static void hostapd_prune_associations(struct hostapd_data *hapd,
  59. struct sta_info *sta)
  60. {
  61. struct prune_data data;
  62. data.hapd = hapd;
  63. data.addr = sta->addr;
  64. hostapd_for_each_interface(prune_associations, &data);
  65. }
  66. /**
  67. * hostapd_new_assoc_sta - Notify that a new station associated with the AP
  68. * @hapd: Pointer to BSS data
  69. * @sta: Pointer to the associated STA data
  70. * @reassoc: 1 to indicate this was a re-association; 0 = first association
  71. *
  72. * This function will be called whenever a station associates with the AP. It
  73. * can be called from ieee802_11.c for drivers that export MLME to hostapd and
  74. * from driver_*.c for drivers that take care of management frames (IEEE 802.11
  75. * authentication and association) internally.
  76. */
  77. void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
  78. int reassoc)
  79. {
  80. if (hapd->tkip_countermeasures) {
  81. hostapd_sta_deauth(hapd, sta->addr,
  82. WLAN_REASON_MICHAEL_MIC_FAILURE);
  83. return;
  84. }
  85. hostapd_prune_associations(hapd, sta);
  86. /* IEEE 802.11F (IAPP) */
  87. if (hapd->conf->ieee802_11f)
  88. iapp_new_station(hapd->iapp, sta);
  89. /* Start accounting here, if IEEE 802.1X and WPA are not used.
  90. * IEEE 802.1X/WPA code will start accounting after the station has
  91. * been authorized. */
  92. if (!hapd->conf->ieee802_1x && !hapd->conf->wpa)
  93. accounting_sta_start(hapd, sta);
  94. /* Start IEEE 802.1X authentication process for new stations */
  95. ieee802_1x_new_station(hapd, sta);
  96. if (reassoc) {
  97. if (sta->auth_alg != WLAN_AUTH_FT &&
  98. !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
  99. wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
  100. } else
  101. wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
  102. }
  103. void hostapd_tx_status(struct hostapd_data *hapd, const u8 *addr,
  104. const u8 *buf, size_t len, int ack)
  105. {
  106. struct sta_info *sta;
  107. struct hostapd_iface *iface = hapd->iface;
  108. sta = ap_get_sta(hapd, addr);
  109. if (sta == NULL && iface->num_bss > 1) {
  110. size_t j;
  111. for (j = 0; j < iface->num_bss; j++) {
  112. hapd = iface->bss[j];
  113. sta = ap_get_sta(hapd, addr);
  114. if (sta)
  115. break;
  116. }
  117. }
  118. if (sta == NULL)
  119. return;
  120. if (sta->flags & WLAN_STA_PENDING_POLL) {
  121. wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
  122. "activity poll", MAC2STR(sta->addr),
  123. ack ? "ACKed" : "did not ACK");
  124. if (ack)
  125. sta->flags &= ~WLAN_STA_PENDING_POLL;
  126. }
  127. ieee802_1x_tx_status(hapd, sta, buf, len, ack);
  128. }
  129. static const u8 * get_hdr_bssid(const struct ieee80211_hdr *hdr, size_t len)
  130. {
  131. u16 fc, type, stype;
  132. /*
  133. * PS-Poll frames are 16 bytes. All other frames are
  134. * 24 bytes or longer.
  135. */
  136. if (len < 16)
  137. return NULL;
  138. fc = le_to_host16(hdr->frame_control);
  139. type = WLAN_FC_GET_TYPE(fc);
  140. stype = WLAN_FC_GET_STYPE(fc);
  141. switch (type) {
  142. case WLAN_FC_TYPE_DATA:
  143. if (len < 24)
  144. return NULL;
  145. switch (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) {
  146. case WLAN_FC_TODS:
  147. return hdr->addr1;
  148. case WLAN_FC_FROMDS:
  149. return hdr->addr2;
  150. default:
  151. return NULL;
  152. }
  153. case WLAN_FC_TYPE_CTRL:
  154. if (stype != WLAN_FC_STYPE_PSPOLL)
  155. return NULL;
  156. return hdr->addr1;
  157. case WLAN_FC_TYPE_MGMT:
  158. return hdr->addr3;
  159. default:
  160. return NULL;
  161. }
  162. }
  163. #define HAPD_BROADCAST ((struct hostapd_data *) -1)
  164. static struct hostapd_data * get_hapd_bssid(struct hostapd_iface *iface,
  165. const u8 *bssid)
  166. {
  167. size_t i;
  168. if (bssid == NULL)
  169. return NULL;
  170. if (bssid[0] == 0xff && bssid[1] == 0xff && bssid[2] == 0xff &&
  171. bssid[3] == 0xff && bssid[4] == 0xff && bssid[5] == 0xff)
  172. return HAPD_BROADCAST;
  173. for (i = 0; i < iface->num_bss; i++) {
  174. if (os_memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0)
  175. return iface->bss[i];
  176. }
  177. return NULL;
  178. }
  179. void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd,
  180. const struct ieee80211_hdr *hdr, size_t len)
  181. {
  182. struct sta_info *sta;
  183. const u8 *addr;
  184. hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
  185. if (hapd == NULL || hapd == HAPD_BROADCAST)
  186. return;
  187. addr = hdr->addr2;
  188. sta = ap_get_sta(hapd, addr);
  189. if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
  190. wpa_printf(MSG_DEBUG, "Data/PS-poll frame from not associated "
  191. "STA " MACSTR, MAC2STR(addr));
  192. if (sta && (sta->flags & WLAN_STA_AUTH))
  193. hostapd_sta_disassoc(
  194. hapd, addr,
  195. WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
  196. else
  197. hostapd_sta_deauth(
  198. hapd, addr,
  199. WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
  200. }
  201. }
  202. int hostapd_notif_new_sta(struct hostapd_data *hapd, const u8 *addr)
  203. {
  204. struct sta_info *sta = ap_get_sta(hapd, addr);
  205. if (sta)
  206. return 0;
  207. wpa_printf(MSG_DEBUG, "Data frame from unknown STA " MACSTR
  208. " - adding a new STA", MAC2STR(addr));
  209. sta = ap_sta_add(hapd, addr);
  210. if (sta) {
  211. hostapd_new_assoc_sta(hapd, sta, 0);
  212. } else {
  213. wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR,
  214. MAC2STR(addr));
  215. return -1;
  216. }
  217. return 0;
  218. }
  219. int hostapd_notif_assoc(struct hostapd_data *hapd, const u8 *addr,
  220. const u8 *ie, size_t ielen)
  221. {
  222. struct sta_info *sta;
  223. int new_assoc, res;
  224. hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
  225. HOSTAPD_LEVEL_INFO, "associated");
  226. sta = ap_get_sta(hapd, addr);
  227. if (sta) {
  228. accounting_sta_stop(hapd, sta);
  229. } else {
  230. sta = ap_sta_add(hapd, addr);
  231. if (sta == NULL)
  232. return -1;
  233. }
  234. sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS);
  235. if (hapd->conf->wpa) {
  236. if (ie == NULL || ielen == 0) {
  237. if (hapd->conf->wps_state) {
  238. wpa_printf(MSG_DEBUG, "STA did not include "
  239. "WPA/RSN IE in (Re)Association "
  240. "Request - possible WPS use");
  241. sta->flags |= WLAN_STA_MAYBE_WPS;
  242. goto skip_wpa_check;
  243. }
  244. wpa_printf(MSG_DEBUG, "No WPA/RSN IE from STA");
  245. return -1;
  246. }
  247. if (hapd->conf->wps_state && ie[0] == 0xdd && ie[1] >= 4 &&
  248. os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
  249. sta->flags |= WLAN_STA_WPS;
  250. goto skip_wpa_check;
  251. }
  252. if (sta->wpa_sm == NULL)
  253. sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
  254. sta->addr);
  255. if (sta->wpa_sm == NULL) {
  256. wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
  257. "machine");
  258. return -1;
  259. }
  260. res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
  261. ie, ielen, NULL, 0);
  262. if (res != WPA_IE_OK) {
  263. int resp;
  264. wpa_printf(MSG_DEBUG, "WPA/RSN information element "
  265. "rejected? (res %u)", res);
  266. wpa_hexdump(MSG_DEBUG, "IE", ie, ielen);
  267. if (res == WPA_INVALID_GROUP)
  268. resp = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
  269. else if (res == WPA_INVALID_PAIRWISE)
  270. resp = WLAN_REASON_PAIRWISE_CIPHER_NOT_VALID;
  271. else if (res == WPA_INVALID_AKMP)
  272. resp = WLAN_REASON_AKMP_NOT_VALID;
  273. #ifdef CONFIG_IEEE80211W
  274. else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
  275. resp = WLAN_REASON_INVALID_IE;
  276. else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
  277. resp = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
  278. #endif /* CONFIG_IEEE80211W */
  279. else
  280. resp = WLAN_REASON_INVALID_IE;
  281. hostapd_sta_disassoc(hapd, sta->addr, resp);
  282. ap_free_sta(hapd, sta);
  283. return -1;
  284. }
  285. } else if (hapd->conf->wps_state) {
  286. if (ie && ielen > 4 && ie[0] == 0xdd && ie[1] >= 4 &&
  287. os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
  288. sta->flags |= WLAN_STA_WPS;
  289. } else
  290. sta->flags |= WLAN_STA_MAYBE_WPS;
  291. }
  292. skip_wpa_check:
  293. new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
  294. sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
  295. wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
  296. hostapd_new_assoc_sta(hapd, sta, !new_assoc);
  297. ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
  298. return 0;
  299. }
  300. void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)
  301. {
  302. struct sta_info *sta;
  303. hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
  304. HOSTAPD_LEVEL_INFO, "disassociated");
  305. sta = ap_get_sta(hapd, addr);
  306. if (sta == NULL) {
  307. wpa_printf(MSG_DEBUG, "Disassociation notification for "
  308. "unknown STA " MACSTR, MAC2STR(addr));
  309. return;
  310. }
  311. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
  312. wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
  313. sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
  314. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  315. ap_free_sta(hapd, sta);
  316. }
  317. void hostapd_eapol_receive(struct hostapd_data *hapd, const u8 *sa,
  318. const u8 *buf, size_t len)
  319. {
  320. ieee802_1x_receive(hapd, sa, buf, len);
  321. }
  322. #ifdef NEED_AP_MLME
  323. void hostapd_mgmt_rx(struct hostapd_data *hapd, u8 *buf, size_t len,
  324. u16 stype, struct hostapd_frame_info *fi)
  325. {
  326. struct hostapd_iface *iface = hapd->iface;
  327. struct ieee80211_hdr *hdr;
  328. const u8 *bssid;
  329. hdr = (struct ieee80211_hdr *) buf;
  330. bssid = get_hdr_bssid(hdr, len);
  331. if (bssid == NULL)
  332. return;
  333. hapd = get_hapd_bssid(iface, bssid);
  334. if (hapd == NULL) {
  335. u16 fc;
  336. fc = le_to_host16(hdr->frame_control);
  337. /*
  338. * Drop frames to unknown BSSIDs except for Beacon frames which
  339. * could be used to update neighbor information.
  340. */
  341. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
  342. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
  343. hapd = iface->bss[0];
  344. else
  345. return;
  346. }
  347. if (hapd == HAPD_BROADCAST) {
  348. size_t i;
  349. for (i = 0; i < iface->num_bss; i++)
  350. ieee802_11_mgmt(iface->bss[i], buf, len, stype, fi);
  351. } else
  352. ieee802_11_mgmt(hapd, buf, len, stype, fi);
  353. }
  354. void hostapd_mgmt_tx_cb(struct hostapd_data *hapd, u8 *buf, size_t len,
  355. u16 stype, int ok)
  356. {
  357. struct ieee80211_hdr *hdr;
  358. hdr = (struct ieee80211_hdr *) buf;
  359. hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
  360. if (hapd == NULL || hapd == HAPD_BROADCAST)
  361. return;
  362. ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
  363. }
  364. #endif /* NEED_AP_MLME */
  365. void hostapd_michael_mic_failure(struct hostapd_data *hapd, const u8 *addr)
  366. {
  367. michael_mic_failure(hapd, addr, 1);
  368. }
  369. struct hostapd_data * hostapd_sta_get_bss(struct hostapd_data *hapd,
  370. const u8 *addr)
  371. {
  372. struct hostapd_iface *iface = hapd->iface;
  373. size_t j;
  374. for (j = 0; j < iface->num_bss; j++) {
  375. hapd = iface->bss[j];
  376. if (ap_get_sta(hapd, addr))
  377. return hapd;
  378. }
  379. return NULL;
  380. }
  381. #ifndef CONFIG_AP
  382. void wpa_supplicant_event(void *ctx, wpa_event_type event,
  383. union wpa_event_data *data)
  384. {
  385. struct hostapd_data *hapd = ctx;
  386. switch (event) {
  387. case EVENT_MICHAEL_MIC_FAILURE:
  388. michael_mic_failure(hapd, data->michael_mic_failure.src, 1);
  389. break;
  390. case EVENT_SCAN_RESULTS:
  391. if (hapd->iface->scan_cb)
  392. hapd->iface->scan_cb(hapd->iface);
  393. break;
  394. #ifdef CONFIG_IEEE80211R
  395. wpa_ft_rrb_rx(hapd->wpa_auth, data->ft_rrb_rx.src,
  396. data->ft_rrb_rx.data, data->ft_rrb_rx.data_len);
  397. break;
  398. #endif /* CONFIG_IEEE80211R */
  399. default:
  400. wpa_printf(MSG_DEBUG, "Unknown event %d", event);
  401. break;
  402. }
  403. }
  404. #endif /* CONFIG_AP */
  405. void hostapd_probe_req_rx(struct hostapd_data *hapd, const u8 *sa,
  406. const u8 *ie, size_t ie_len)
  407. {
  408. size_t i;
  409. for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++)
  410. hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
  411. sa, ie, ie_len);
  412. }
  413. void hostapd_button_pushed(struct hostapd_data *hapd)
  414. {
  415. hostapd_wps_button_pushed(hapd);
  416. }