drv_callbacks.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 "hostapd.h"
  19. #include "ieee802_11.h"
  20. #include "sta_info.h"
  21. #include "accounting.h"
  22. #include "tkip_countermeasures.h"
  23. #include "iapp.h"
  24. #include "ieee802_1x.h"
  25. #include "wpa_auth.h"
  26. #include "wmm.h"
  27. #include "wps_hostapd.h"
  28. #include "ap_config.h"
  29. int hostapd_notif_new_sta(struct hostapd_data *hapd, const u8 *addr)
  30. {
  31. struct sta_info *sta = ap_get_sta(hapd, addr);
  32. if (sta)
  33. return 0;
  34. wpa_printf(MSG_DEBUG, "Data frame from unknown STA " MACSTR
  35. " - adding a new STA", MAC2STR(addr));
  36. sta = ap_sta_add(hapd, addr);
  37. if (sta) {
  38. hostapd_new_assoc_sta(hapd, sta, 0);
  39. } else {
  40. wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR,
  41. MAC2STR(addr));
  42. return -1;
  43. }
  44. return 0;
  45. }
  46. int hostapd_notif_assoc(struct hostapd_data *hapd, const u8 *addr,
  47. const u8 *ie, size_t ielen)
  48. {
  49. struct sta_info *sta;
  50. int new_assoc, res;
  51. hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
  52. HOSTAPD_LEVEL_INFO, "associated");
  53. sta = ap_get_sta(hapd, addr);
  54. if (sta) {
  55. accounting_sta_stop(hapd, sta);
  56. } else {
  57. sta = ap_sta_add(hapd, addr);
  58. if (sta == NULL)
  59. return -1;
  60. }
  61. sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS);
  62. if (hapd->conf->wpa) {
  63. if (ie == NULL || ielen == 0) {
  64. if (hapd->conf->wps_state) {
  65. wpa_printf(MSG_DEBUG, "STA did not include "
  66. "WPA/RSN IE in (Re)Association "
  67. "Request - possible WPS use");
  68. sta->flags |= WLAN_STA_MAYBE_WPS;
  69. goto skip_wpa_check;
  70. }
  71. wpa_printf(MSG_DEBUG, "No WPA/RSN IE from STA");
  72. return -1;
  73. }
  74. if (hapd->conf->wps_state && ie[0] == 0xdd && ie[1] >= 4 &&
  75. os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
  76. sta->flags |= WLAN_STA_WPS;
  77. goto skip_wpa_check;
  78. }
  79. if (sta->wpa_sm == NULL)
  80. sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
  81. sta->addr);
  82. if (sta->wpa_sm == NULL) {
  83. wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
  84. "machine");
  85. return -1;
  86. }
  87. res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
  88. ie, ielen, NULL, 0);
  89. if (res != WPA_IE_OK) {
  90. int resp;
  91. wpa_printf(MSG_DEBUG, "WPA/RSN information element "
  92. "rejected? (res %u)", res);
  93. wpa_hexdump(MSG_DEBUG, "IE", ie, ielen);
  94. if (res == WPA_INVALID_GROUP)
  95. resp = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
  96. else if (res == WPA_INVALID_PAIRWISE)
  97. resp = WLAN_REASON_PAIRWISE_CIPHER_NOT_VALID;
  98. else if (res == WPA_INVALID_AKMP)
  99. resp = WLAN_REASON_AKMP_NOT_VALID;
  100. #ifdef CONFIG_IEEE80211W
  101. else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
  102. resp = WLAN_REASON_INVALID_IE;
  103. else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
  104. resp = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
  105. #endif /* CONFIG_IEEE80211W */
  106. else
  107. resp = WLAN_REASON_INVALID_IE;
  108. hapd->drv.sta_disassoc(hapd, sta->addr, resp);
  109. ap_free_sta(hapd, sta);
  110. return -1;
  111. }
  112. } else if (hapd->conf->wps_state) {
  113. if (ie && ielen > 4 && ie[0] == 0xdd && ie[1] >= 4 &&
  114. os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
  115. sta->flags |= WLAN_STA_WPS;
  116. } else
  117. sta->flags |= WLAN_STA_MAYBE_WPS;
  118. }
  119. skip_wpa_check:
  120. new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
  121. sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
  122. wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
  123. hostapd_new_assoc_sta(hapd, sta, !new_assoc);
  124. ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
  125. return 0;
  126. }
  127. void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)
  128. {
  129. struct sta_info *sta;
  130. hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
  131. HOSTAPD_LEVEL_INFO, "disassociated");
  132. sta = ap_get_sta(hapd, addr);
  133. if (sta == NULL) {
  134. wpa_printf(MSG_DEBUG, "Disassociation notification for "
  135. "unknown STA " MACSTR, MAC2STR(addr));
  136. return;
  137. }
  138. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
  139. wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
  140. sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
  141. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  142. ap_free_sta(hapd, sta);
  143. }
  144. void hostapd_eapol_receive(struct hostapd_data *hapd, const u8 *sa,
  145. const u8 *buf, size_t len)
  146. {
  147. ieee802_1x_receive(hapd, sa, buf, len);
  148. }
  149. struct hostapd_data * hostapd_sta_get_bss(struct hostapd_data *hapd,
  150. const u8 *addr)
  151. {
  152. struct hostapd_iface *iface = hapd->iface;
  153. size_t j;
  154. for (j = 0; j < iface->num_bss; j++) {
  155. hapd = iface->bss[j];
  156. if (ap_get_sta(hapd, addr))
  157. return hapd;
  158. }
  159. return NULL;
  160. }
  161. #ifdef HOSTAPD
  162. #ifdef NEED_AP_MLME
  163. static const u8 * get_hdr_bssid(const struct ieee80211_hdr *hdr, size_t len)
  164. {
  165. u16 fc, type, stype;
  166. /*
  167. * PS-Poll frames are 16 bytes. All other frames are
  168. * 24 bytes or longer.
  169. */
  170. if (len < 16)
  171. return NULL;
  172. fc = le_to_host16(hdr->frame_control);
  173. type = WLAN_FC_GET_TYPE(fc);
  174. stype = WLAN_FC_GET_STYPE(fc);
  175. switch (type) {
  176. case WLAN_FC_TYPE_DATA:
  177. if (len < 24)
  178. return NULL;
  179. switch (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) {
  180. case WLAN_FC_FROMDS | WLAN_FC_TODS:
  181. case WLAN_FC_TODS:
  182. return hdr->addr1;
  183. case WLAN_FC_FROMDS:
  184. return hdr->addr2;
  185. default:
  186. return NULL;
  187. }
  188. case WLAN_FC_TYPE_CTRL:
  189. if (stype != WLAN_FC_STYPE_PSPOLL)
  190. return NULL;
  191. return hdr->addr1;
  192. case WLAN_FC_TYPE_MGMT:
  193. return hdr->addr3;
  194. default:
  195. return NULL;
  196. }
  197. }
  198. #define HAPD_BROADCAST ((struct hostapd_data *) -1)
  199. static struct hostapd_data * get_hapd_bssid(struct hostapd_iface *iface,
  200. const u8 *bssid)
  201. {
  202. size_t i;
  203. if (bssid == NULL)
  204. return NULL;
  205. if (bssid[0] == 0xff && bssid[1] == 0xff && bssid[2] == 0xff &&
  206. bssid[3] == 0xff && bssid[4] == 0xff && bssid[5] == 0xff)
  207. return HAPD_BROADCAST;
  208. for (i = 0; i < iface->num_bss; i++) {
  209. if (os_memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0)
  210. return iface->bss[i];
  211. }
  212. return NULL;
  213. }
  214. static void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd,
  215. const struct ieee80211_hdr *hdr,
  216. size_t len)
  217. {
  218. u16 fc = le_to_host16(hdr->frame_control);
  219. hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
  220. if (hapd == NULL || hapd == HAPD_BROADCAST)
  221. return;
  222. ieee802_11_rx_from_unknown(hapd, hdr->addr2,
  223. (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
  224. (WLAN_FC_TODS | WLAN_FC_FROMDS));
  225. }
  226. static void hostapd_mgmt_rx(struct hostapd_data *hapd, const u8 *buf,
  227. size_t len, struct hostapd_frame_info *fi)
  228. {
  229. struct hostapd_iface *iface = hapd->iface;
  230. const struct ieee80211_hdr *hdr;
  231. const u8 *bssid;
  232. hdr = (const struct ieee80211_hdr *) buf;
  233. bssid = get_hdr_bssid(hdr, len);
  234. if (bssid == NULL)
  235. return;
  236. hapd = get_hapd_bssid(iface, bssid);
  237. if (hapd == NULL) {
  238. u16 fc;
  239. fc = le_to_host16(hdr->frame_control);
  240. /*
  241. * Drop frames to unknown BSSIDs except for Beacon frames which
  242. * could be used to update neighbor information.
  243. */
  244. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
  245. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
  246. hapd = iface->bss[0];
  247. else
  248. return;
  249. }
  250. if (hapd == HAPD_BROADCAST) {
  251. size_t i;
  252. for (i = 0; i < iface->num_bss; i++)
  253. ieee802_11_mgmt(iface->bss[i], buf, len, fi);
  254. } else
  255. ieee802_11_mgmt(hapd, buf, len, fi);
  256. }
  257. static void hostapd_mgmt_tx_cb(struct hostapd_data *hapd, const u8 *buf,
  258. size_t len, u16 stype, int ok)
  259. {
  260. struct ieee80211_hdr *hdr;
  261. hdr = (struct ieee80211_hdr *) buf;
  262. hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
  263. if (hapd == NULL || hapd == HAPD_BROADCAST)
  264. return;
  265. ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
  266. }
  267. #endif /* NEED_AP_MLME */
  268. void wpa_supplicant_event(void *ctx, wpa_event_type event,
  269. union wpa_event_data *data)
  270. {
  271. struct hostapd_data *hapd = ctx;
  272. switch (event) {
  273. case EVENT_MICHAEL_MIC_FAILURE:
  274. michael_mic_failure(hapd, data->michael_mic_failure.src, 1);
  275. break;
  276. case EVENT_SCAN_RESULTS:
  277. if (hapd->iface->scan_cb)
  278. hapd->iface->scan_cb(hapd->iface);
  279. break;
  280. #ifdef CONFIG_IEEE80211R
  281. case EVENT_FT_RRB_RX:
  282. wpa_ft_rrb_rx(hapd->wpa_auth, data->ft_rrb_rx.src,
  283. data->ft_rrb_rx.data, data->ft_rrb_rx.data_len);
  284. break;
  285. #endif /* CONFIG_IEEE80211R */
  286. case EVENT_WPS_BUTTON_PUSHED:
  287. hostapd_wps_button_pushed(hapd);
  288. break;
  289. #ifdef NEED_AP_MLME
  290. case EVENT_TX_STATUS:
  291. switch (data->tx_status.type) {
  292. case WLAN_FC_TYPE_MGMT:
  293. hostapd_mgmt_tx_cb(hapd, data->tx_status.data,
  294. data->tx_status.data_len,
  295. data->tx_status.stype,
  296. data->tx_status.ack);
  297. break;
  298. case WLAN_FC_TYPE_DATA:
  299. hostapd_tx_status(hapd, data->tx_status.dst,
  300. data->tx_status.data,
  301. data->tx_status.data_len,
  302. data->tx_status.ack);
  303. break;
  304. }
  305. break;
  306. case EVENT_RX_FROM_UNKNOWN:
  307. hostapd_rx_from_unknown_sta(hapd, data->rx_from_unknown.hdr,
  308. data->rx_from_unknown.len);
  309. break;
  310. case EVENT_RX_MGMT:
  311. hostapd_mgmt_rx(hapd, data->rx_mgmt.frame,
  312. data->rx_mgmt.frame_len, data->rx_mgmt.fi);
  313. break;
  314. #endif /* NEED_AP_MLME */
  315. default:
  316. wpa_printf(MSG_DEBUG, "Unknown event %d", event);
  317. break;
  318. }
  319. }
  320. #endif /* HOSTAPD */
  321. void hostapd_probe_req_rx(struct hostapd_data *hapd, const u8 *sa,
  322. const u8 *ie, size_t ie_len)
  323. {
  324. size_t i;
  325. for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++)
  326. hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
  327. sa, ie, ie_len);
  328. }