drv_callbacks.c 11 KB

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