drv_callbacks.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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_wme_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. sta = ap_get_sta(hapd, addr);
  106. if (sta && sta->flags & WLAN_STA_PENDING_POLL) {
  107. wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
  108. "activity poll", MAC2STR(sta->addr),
  109. ack ? "ACKed" : "did not ACK");
  110. if (ack)
  111. sta->flags &= ~WLAN_STA_PENDING_POLL;
  112. }
  113. if (sta)
  114. ieee802_1x_tx_status(hapd, sta, buf, len, ack);
  115. }
  116. void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd, const u8 *addr)
  117. {
  118. struct sta_info *sta;
  119. sta = ap_get_sta(hapd, addr);
  120. if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
  121. wpa_printf(MSG_DEBUG, "Data/PS-poll frame from not associated "
  122. "STA " MACSTR, MAC2STR(addr));
  123. if (sta && (sta->flags & WLAN_STA_AUTH))
  124. hostapd_sta_disassoc(
  125. hapd, addr,
  126. WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
  127. else
  128. hostapd_sta_deauth(
  129. hapd, addr,
  130. WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
  131. }
  132. }
  133. int hostapd_notif_assoc(struct hostapd_data *hapd, const u8 *addr,
  134. const u8 *ie, size_t ielen)
  135. {
  136. struct sta_info *sta;
  137. int new_assoc, res;
  138. hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
  139. HOSTAPD_LEVEL_INFO, "associated");
  140. sta = ap_get_sta(hapd, addr);
  141. if (sta) {
  142. accounting_sta_stop(hapd, sta);
  143. } else {
  144. sta = ap_sta_add(hapd, addr);
  145. if (sta == NULL)
  146. return -1;
  147. }
  148. sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS);
  149. if (hapd->conf->wpa) {
  150. if (ie == NULL || ielen == 0) {
  151. if (hapd->conf->wps_state) {
  152. wpa_printf(MSG_DEBUG, "STA did not include "
  153. "WPA/RSN IE in (Re)Association "
  154. "Request - possible WPS use");
  155. sta->flags |= WLAN_STA_MAYBE_WPS;
  156. goto skip_wpa_check;
  157. }
  158. wpa_printf(MSG_DEBUG, "No WPA/RSN IE from STA");
  159. return -1;
  160. }
  161. if (hapd->conf->wps_state && ie[0] == 0xdd && ie[1] >= 4 &&
  162. os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
  163. sta->flags |= WLAN_STA_WPS;
  164. goto skip_wpa_check;
  165. }
  166. if (sta->wpa_sm == NULL)
  167. sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
  168. sta->addr);
  169. if (sta->wpa_sm == NULL) {
  170. wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
  171. "machine");
  172. return -1;
  173. }
  174. res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
  175. ie, ielen, NULL, 0);
  176. if (res != WPA_IE_OK) {
  177. wpa_printf(MSG_DEBUG, "WPA/RSN information element "
  178. "rejected? (res %u)", res);
  179. wpa_hexdump(MSG_DEBUG, "IE", ie, ielen);
  180. return -1;
  181. }
  182. }
  183. skip_wpa_check:
  184. new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
  185. sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
  186. wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
  187. hostapd_new_assoc_sta(hapd, sta, !new_assoc);
  188. ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
  189. return 0;
  190. }
  191. void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)
  192. {
  193. struct sta_info *sta;
  194. hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
  195. HOSTAPD_LEVEL_INFO, "disassociated");
  196. sta = ap_get_sta(hapd, addr);
  197. if (sta == NULL) {
  198. wpa_printf(MSG_DEBUG, "Disassociation notification for "
  199. "unknown STA " MACSTR, MAC2STR(addr));
  200. return;
  201. }
  202. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
  203. wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
  204. sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
  205. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  206. ap_free_sta(hapd, sta);
  207. }
  208. void hostapd_eapol_receive(struct hostapd_data *hapd, const u8 *sa,
  209. const u8 *buf, size_t len)
  210. {
  211. ieee802_1x_receive(hapd, sa, buf, len);
  212. }
  213. void hostapd_mgmt_rx(struct hostapd_data *hapd, u8 *buf, size_t len,
  214. u16 stype, struct hostapd_frame_info *fi)
  215. {
  216. ieee802_11_mgmt(hapd, buf, len, stype, fi);
  217. }
  218. void hostapd_mgmt_tx_cb(struct hostapd_data *hapd, u8 *buf, size_t len,
  219. u16 stype, int ok)
  220. {
  221. ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
  222. }
  223. void hostapd_michael_mic_failure(struct hostapd_data *hapd, const u8 *addr)
  224. {
  225. michael_mic_failure(hapd, addr, 1);
  226. }