drv_callbacks.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. wpa_printf(MSG_DEBUG, "WPA/RSN information element "
  246. "rejected? (res %u)", res);
  247. wpa_hexdump(MSG_DEBUG, "IE", ie, ielen);
  248. return -1;
  249. }
  250. } else if (hapd->conf->wps_state) {
  251. if (ie && ielen > 4 && ie[0] == 0xdd && ie[1] >= 4 &&
  252. os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
  253. sta->flags |= WLAN_STA_WPS;
  254. } else
  255. sta->flags |= WLAN_STA_MAYBE_WPS;
  256. }
  257. skip_wpa_check:
  258. new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
  259. sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
  260. wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
  261. hostapd_new_assoc_sta(hapd, sta, !new_assoc);
  262. ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
  263. return 0;
  264. }
  265. void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)
  266. {
  267. struct sta_info *sta;
  268. hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
  269. HOSTAPD_LEVEL_INFO, "disassociated");
  270. sta = ap_get_sta(hapd, addr);
  271. if (sta == NULL) {
  272. wpa_printf(MSG_DEBUG, "Disassociation notification for "
  273. "unknown STA " MACSTR, MAC2STR(addr));
  274. return;
  275. }
  276. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
  277. wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
  278. sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
  279. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  280. ap_free_sta(hapd, sta);
  281. }
  282. void hostapd_eapol_receive(struct hostapd_data *hapd, const u8 *sa,
  283. const u8 *buf, size_t len)
  284. {
  285. ieee802_1x_receive(hapd, sa, buf, len);
  286. }
  287. #ifdef NEED_AP_MLME
  288. void hostapd_mgmt_rx(struct hostapd_data *hapd, u8 *buf, size_t len,
  289. u16 stype, struct hostapd_frame_info *fi)
  290. {
  291. struct hostapd_iface *iface = hapd->iface;
  292. struct ieee80211_hdr *hdr;
  293. const u8 *bssid;
  294. hdr = (struct ieee80211_hdr *) buf;
  295. bssid = get_hdr_bssid(hdr, len);
  296. if (bssid == NULL)
  297. return;
  298. hapd = get_hapd_bssid(iface, bssid);
  299. if (hapd == NULL) {
  300. u16 fc;
  301. fc = le_to_host16(hdr->frame_control);
  302. /*
  303. * Drop frames to unknown BSSIDs except for Beacon frames which
  304. * could be used to update neighbor information.
  305. */
  306. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
  307. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
  308. hapd = iface->bss[0];
  309. else
  310. return;
  311. }
  312. if (hapd == HAPD_BROADCAST) {
  313. size_t i;
  314. for (i = 0; i < iface->num_bss; i++)
  315. ieee802_11_mgmt(iface->bss[i], buf, len, stype, fi);
  316. } else
  317. ieee802_11_mgmt(hapd, buf, len, stype, fi);
  318. }
  319. void hostapd_mgmt_tx_cb(struct hostapd_data *hapd, u8 *buf, size_t len,
  320. u16 stype, int ok)
  321. {
  322. struct ieee80211_hdr *hdr;
  323. hdr = (struct ieee80211_hdr *) buf;
  324. hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
  325. if (hapd == NULL || hapd == HAPD_BROADCAST)
  326. return;
  327. ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
  328. }
  329. #endif /* NEED_AP_MLME */
  330. void hostapd_michael_mic_failure(struct hostapd_data *hapd, const u8 *addr)
  331. {
  332. michael_mic_failure(hapd, addr, 1);
  333. }
  334. struct hostapd_data * hostapd_sta_get_bss(struct hostapd_data *hapd,
  335. const u8 *addr)
  336. {
  337. struct hostapd_iface *iface = hapd->iface;
  338. size_t j;
  339. for (j = 0; j < iface->num_bss; j++) {
  340. hapd = iface->bss[j];
  341. if (ap_get_sta(hapd, addr))
  342. return hapd;
  343. }
  344. return NULL;
  345. }
  346. #ifndef CONFIG_AP
  347. void wpa_supplicant_event(void *ctx, wpa_event_type event,
  348. union wpa_event_data *data)
  349. {
  350. struct hostapd_data *hapd = ctx;
  351. switch (event) {
  352. case EVENT_MICHAEL_MIC_FAILURE:
  353. michael_mic_failure(hapd, data->michael_mic_failure.src, 1);
  354. break;
  355. case EVENT_SCAN_RESULTS:
  356. if (hapd->iface->scan_cb)
  357. hapd->iface->scan_cb(hapd->iface);
  358. break;
  359. default:
  360. wpa_printf(MSG_DEBUG, "Unknown event %d", event);
  361. break;
  362. }
  363. }
  364. #endif /* CONFIG_AP */
  365. void hostapd_probe_req_rx(struct hostapd_data *hapd, const u8 *sa,
  366. const u8 *ie, size_t ie_len)
  367. {
  368. size_t i;
  369. for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++)
  370. hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
  371. sa, ie, ie_len);
  372. }