drv_callbacks.c 11 KB

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