drv_callbacks.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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 "utils/includes.h"
  15. #include "utils/common.h"
  16. #include "radius/radius.h"
  17. #include "drivers/driver.h"
  18. #include "common/ieee802_11_defs.h"
  19. #include "common/ieee802_11_common.h"
  20. #include "common/wpa_ctrl.h"
  21. #include "hostapd.h"
  22. #include "ieee802_11.h"
  23. #include "sta_info.h"
  24. #include "accounting.h"
  25. #include "tkip_countermeasures.h"
  26. #include "iapp.h"
  27. #include "ieee802_1x.h"
  28. #include "wpa_auth.h"
  29. #include "wmm.h"
  30. #include "wps_hostapd.h"
  31. #include "ap_config.h"
  32. int hostapd_notif_assoc(struct hostapd_data *hapd, const u8 *addr,
  33. const u8 *ie, size_t ielen)
  34. {
  35. struct sta_info *sta;
  36. int new_assoc, res;
  37. struct ieee802_11_elems elems;
  38. if (addr == NULL) {
  39. /*
  40. * This could potentially happen with unexpected event from the
  41. * driver wrapper. This was seen at least in one case where the
  42. * driver ended up being set to station mode while hostapd was
  43. * running, so better make sure we stop processing such an
  44. * event here.
  45. */
  46. wpa_printf(MSG_DEBUG, "hostapd_notif_assoc: Skip event with "
  47. "no address");
  48. return -1;
  49. }
  50. hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
  51. HOSTAPD_LEVEL_INFO, "associated");
  52. ieee802_11_parse_elems(ie, ielen, &elems, 0);
  53. if (elems.wps_ie) {
  54. ie = elems.wps_ie - 2;
  55. ielen = elems.wps_ie_len + 2;
  56. wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)AssocReq");
  57. } else if (elems.rsn_ie) {
  58. ie = elems.rsn_ie - 2;
  59. ielen = elems.rsn_ie_len + 2;
  60. wpa_printf(MSG_DEBUG, "STA included RSN IE in (Re)AssocReq");
  61. } else if (elems.wpa_ie) {
  62. ie = elems.wpa_ie - 2;
  63. ielen = elems.wpa_ie_len + 2;
  64. wpa_printf(MSG_DEBUG, "STA included WPA IE in (Re)AssocReq");
  65. } else {
  66. ie = NULL;
  67. ielen = 0;
  68. wpa_printf(MSG_DEBUG, "STA did not include WPS/RSN/WPA IE in "
  69. "(Re)AssocReq");
  70. }
  71. sta = ap_get_sta(hapd, addr);
  72. if (sta) {
  73. accounting_sta_stop(hapd, sta);
  74. } else {
  75. sta = ap_sta_add(hapd, addr);
  76. if (sta == NULL)
  77. return -1;
  78. }
  79. sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS);
  80. if (hapd->conf->wpa) {
  81. if (ie == NULL || ielen == 0) {
  82. if (hapd->conf->wps_state) {
  83. wpa_printf(MSG_DEBUG, "STA did not include "
  84. "WPA/RSN IE in (Re)Association "
  85. "Request - possible WPS use");
  86. sta->flags |= WLAN_STA_MAYBE_WPS;
  87. goto skip_wpa_check;
  88. }
  89. wpa_printf(MSG_DEBUG, "No WPA/RSN IE from STA");
  90. return -1;
  91. }
  92. if (hapd->conf->wps_state && ie[0] == 0xdd && ie[1] >= 4 &&
  93. os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
  94. sta->flags |= WLAN_STA_WPS;
  95. goto skip_wpa_check;
  96. }
  97. if (sta->wpa_sm == NULL)
  98. sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
  99. sta->addr);
  100. if (sta->wpa_sm == NULL) {
  101. wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
  102. "machine");
  103. return -1;
  104. }
  105. res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
  106. ie, ielen, NULL, 0);
  107. if (res != WPA_IE_OK) {
  108. int resp;
  109. wpa_printf(MSG_DEBUG, "WPA/RSN information element "
  110. "rejected? (res %u)", res);
  111. wpa_hexdump(MSG_DEBUG, "IE", ie, ielen);
  112. if (res == WPA_INVALID_GROUP)
  113. resp = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
  114. else if (res == WPA_INVALID_PAIRWISE)
  115. resp = WLAN_REASON_PAIRWISE_CIPHER_NOT_VALID;
  116. else if (res == WPA_INVALID_AKMP)
  117. resp = WLAN_REASON_AKMP_NOT_VALID;
  118. #ifdef CONFIG_IEEE80211W
  119. else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
  120. resp = WLAN_REASON_INVALID_IE;
  121. else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
  122. resp = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
  123. #endif /* CONFIG_IEEE80211W */
  124. else
  125. resp = WLAN_REASON_INVALID_IE;
  126. hapd->drv.sta_disassoc(hapd, sta->addr, resp);
  127. ap_free_sta(hapd, sta);
  128. return -1;
  129. }
  130. } else if (hapd->conf->wps_state) {
  131. if (ie && ielen > 4 && ie[0] == 0xdd && ie[1] >= 4 &&
  132. os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
  133. sta->flags |= WLAN_STA_WPS;
  134. } else
  135. sta->flags |= WLAN_STA_MAYBE_WPS;
  136. }
  137. skip_wpa_check:
  138. new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
  139. sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
  140. wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
  141. hostapd_new_assoc_sta(hapd, sta, !new_assoc);
  142. ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
  143. return 0;
  144. }
  145. void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)
  146. {
  147. struct sta_info *sta;
  148. hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
  149. HOSTAPD_LEVEL_INFO, "disassociated");
  150. sta = ap_get_sta(hapd, addr);
  151. if (sta == NULL) {
  152. wpa_printf(MSG_DEBUG, "Disassociation notification for "
  153. "unknown STA " MACSTR, MAC2STR(addr));
  154. return;
  155. }
  156. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
  157. wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED MACSTR,
  158. MAC2STR(sta->addr));
  159. wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
  160. sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
  161. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  162. ap_free_sta(hapd, sta);
  163. }
  164. #ifdef HOSTAPD
  165. #ifdef NEED_AP_MLME
  166. static const u8 * get_hdr_bssid(const struct ieee80211_hdr *hdr, size_t len)
  167. {
  168. u16 fc, type, stype;
  169. /*
  170. * PS-Poll frames are 16 bytes. All other frames are
  171. * 24 bytes or longer.
  172. */
  173. if (len < 16)
  174. return NULL;
  175. fc = le_to_host16(hdr->frame_control);
  176. type = WLAN_FC_GET_TYPE(fc);
  177. stype = WLAN_FC_GET_STYPE(fc);
  178. switch (type) {
  179. case WLAN_FC_TYPE_DATA:
  180. if (len < 24)
  181. return NULL;
  182. switch (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) {
  183. case WLAN_FC_FROMDS | WLAN_FC_TODS:
  184. case WLAN_FC_TODS:
  185. return hdr->addr1;
  186. case WLAN_FC_FROMDS:
  187. return hdr->addr2;
  188. default:
  189. return NULL;
  190. }
  191. case WLAN_FC_TYPE_CTRL:
  192. if (stype != WLAN_FC_STYPE_PSPOLL)
  193. return NULL;
  194. return hdr->addr1;
  195. case WLAN_FC_TYPE_MGMT:
  196. return hdr->addr3;
  197. default:
  198. return NULL;
  199. }
  200. }
  201. #define HAPD_BROADCAST ((struct hostapd_data *) -1)
  202. static struct hostapd_data * get_hapd_bssid(struct hostapd_iface *iface,
  203. const u8 *bssid)
  204. {
  205. size_t i;
  206. if (bssid == NULL)
  207. return NULL;
  208. if (bssid[0] == 0xff && bssid[1] == 0xff && bssid[2] == 0xff &&
  209. bssid[3] == 0xff && bssid[4] == 0xff && bssid[5] == 0xff)
  210. return HAPD_BROADCAST;
  211. for (i = 0; i < iface->num_bss; i++) {
  212. if (os_memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0)
  213. return iface->bss[i];
  214. }
  215. return NULL;
  216. }
  217. static void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd,
  218. const u8 *frame, size_t len)
  219. {
  220. const struct ieee80211_hdr *hdr = (const struct ieee80211_hdr *) frame;
  221. u16 fc = le_to_host16(hdr->frame_control);
  222. hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
  223. if (hapd == NULL || hapd == HAPD_BROADCAST)
  224. return;
  225. ieee802_11_rx_from_unknown(hapd, hdr->addr2,
  226. (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
  227. (WLAN_FC_TODS | WLAN_FC_FROMDS));
  228. }
  229. static void hostapd_mgmt_rx(struct hostapd_data *hapd, struct rx_mgmt *rx_mgmt)
  230. {
  231. struct hostapd_iface *iface = hapd->iface;
  232. const struct ieee80211_hdr *hdr;
  233. const u8 *bssid;
  234. struct hostapd_frame_info fi;
  235. hdr = (const struct ieee80211_hdr *) rx_mgmt->frame;
  236. bssid = get_hdr_bssid(hdr, rx_mgmt->frame_len);
  237. if (bssid == NULL)
  238. return;
  239. hapd = get_hapd_bssid(iface, bssid);
  240. if (hapd == NULL) {
  241. u16 fc;
  242. fc = le_to_host16(hdr->frame_control);
  243. /*
  244. * Drop frames to unknown BSSIDs except for Beacon frames which
  245. * could be used to update neighbor information.
  246. */
  247. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
  248. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
  249. hapd = iface->bss[0];
  250. else
  251. return;
  252. }
  253. os_memset(&fi, 0, sizeof(fi));
  254. fi.datarate = rx_mgmt->datarate;
  255. fi.ssi_signal = rx_mgmt->ssi_signal;
  256. if (hapd == HAPD_BROADCAST) {
  257. size_t i;
  258. for (i = 0; i < iface->num_bss; i++)
  259. ieee802_11_mgmt(iface->bss[i], rx_mgmt->frame,
  260. rx_mgmt->frame_len, &fi);
  261. } else
  262. ieee802_11_mgmt(hapd, rx_mgmt->frame, rx_mgmt->frame_len, &fi);
  263. }
  264. static void hostapd_mgmt_tx_cb(struct hostapd_data *hapd, const u8 *buf,
  265. size_t len, u16 stype, int ok)
  266. {
  267. struct ieee80211_hdr *hdr;
  268. hdr = (struct ieee80211_hdr *) buf;
  269. hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
  270. if (hapd == NULL || hapd == HAPD_BROADCAST)
  271. return;
  272. ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
  273. }
  274. #endif /* NEED_AP_MLME */
  275. static int hostapd_probe_req_rx(struct hostapd_data *hapd, const u8 *sa,
  276. const u8 *ie, size_t ie_len)
  277. {
  278. size_t i;
  279. int ret = 0;
  280. for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++) {
  281. if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
  282. sa, ie, ie_len) > 0) {
  283. ret = 1;
  284. break;
  285. }
  286. }
  287. return ret;
  288. }
  289. static int hostapd_event_new_sta(struct hostapd_data *hapd, const u8 *addr)
  290. {
  291. struct sta_info *sta = ap_get_sta(hapd, addr);
  292. if (sta)
  293. return 0;
  294. wpa_printf(MSG_DEBUG, "Data frame from unknown STA " MACSTR
  295. " - adding a new STA", MAC2STR(addr));
  296. sta = ap_sta_add(hapd, addr);
  297. if (sta) {
  298. hostapd_new_assoc_sta(hapd, sta, 0);
  299. } else {
  300. wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR,
  301. MAC2STR(addr));
  302. return -1;
  303. }
  304. return 0;
  305. }
  306. static void hostapd_event_eapol_rx(struct hostapd_data *hapd, const u8 *src,
  307. const u8 *data, size_t data_len)
  308. {
  309. struct hostapd_iface *iface = hapd->iface;
  310. size_t j;
  311. for (j = 0; j < iface->num_bss; j++) {
  312. if (ap_get_sta(iface->bss[j], src)) {
  313. hapd = iface->bss[j];
  314. break;
  315. }
  316. }
  317. ieee802_1x_receive(hapd, src, data, data_len);
  318. }
  319. void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
  320. union wpa_event_data *data)
  321. {
  322. struct hostapd_data *hapd = ctx;
  323. switch (event) {
  324. case EVENT_MICHAEL_MIC_FAILURE:
  325. michael_mic_failure(hapd, data->michael_mic_failure.src, 1);
  326. break;
  327. case EVENT_SCAN_RESULTS:
  328. if (hapd->iface->scan_cb)
  329. hapd->iface->scan_cb(hapd->iface);
  330. break;
  331. #ifdef CONFIG_IEEE80211R
  332. case EVENT_FT_RRB_RX:
  333. wpa_ft_rrb_rx(hapd->wpa_auth, data->ft_rrb_rx.src,
  334. data->ft_rrb_rx.data, data->ft_rrb_rx.data_len);
  335. break;
  336. #endif /* CONFIG_IEEE80211R */
  337. case EVENT_WPS_BUTTON_PUSHED:
  338. hostapd_wps_button_pushed(hapd);
  339. break;
  340. #ifdef NEED_AP_MLME
  341. case EVENT_TX_STATUS:
  342. switch (data->tx_status.type) {
  343. case WLAN_FC_TYPE_MGMT:
  344. hostapd_mgmt_tx_cb(hapd, data->tx_status.data,
  345. data->tx_status.data_len,
  346. data->tx_status.stype,
  347. data->tx_status.ack);
  348. break;
  349. case WLAN_FC_TYPE_DATA:
  350. hostapd_tx_status(hapd, data->tx_status.dst,
  351. data->tx_status.data,
  352. data->tx_status.data_len,
  353. data->tx_status.ack);
  354. break;
  355. }
  356. break;
  357. case EVENT_RX_FROM_UNKNOWN:
  358. hostapd_rx_from_unknown_sta(hapd, data->rx_from_unknown.frame,
  359. data->rx_from_unknown.len);
  360. break;
  361. case EVENT_RX_MGMT:
  362. hostapd_mgmt_rx(hapd, &data->rx_mgmt);
  363. break;
  364. #endif /* NEED_AP_MLME */
  365. case EVENT_RX_PROBE_REQ:
  366. hostapd_probe_req_rx(hapd, data->rx_probe_req.sa,
  367. data->rx_probe_req.ie,
  368. data->rx_probe_req.ie_len);
  369. break;
  370. case EVENT_NEW_STA:
  371. hostapd_event_new_sta(hapd, data->new_sta.addr);
  372. break;
  373. case EVENT_EAPOL_RX:
  374. hostapd_event_eapol_rx(hapd, data->eapol_rx.src,
  375. data->eapol_rx.data,
  376. data->eapol_rx.data_len);
  377. break;
  378. case EVENT_ASSOC:
  379. hostapd_notif_assoc(hapd, data->assoc_info.addr,
  380. data->assoc_info.req_ies,
  381. data->assoc_info.req_ies_len);
  382. break;
  383. case EVENT_DISASSOC:
  384. if (data)
  385. hostapd_notif_disassoc(hapd, data->disassoc_info.addr);
  386. break;
  387. case EVENT_DEAUTH:
  388. if (data)
  389. hostapd_notif_disassoc(hapd, data->deauth_info.addr);
  390. break;
  391. default:
  392. wpa_printf(MSG_DEBUG, "Unknown event %d", event);
  393. break;
  394. }
  395. }
  396. #endif /* HOSTAPD */