ieee802_11.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841
  1. /*
  2. * hostapd / IEEE 802.11 Management
  3. * Copyright (c) 2002-2008, Jouni Malinen <j@w1.fi>
  4. * Copyright (c) 2007-2008, Intel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Alternatively, this software may be distributed under the terms of BSD
  11. * license.
  12. *
  13. * See README and COPYING for more details.
  14. */
  15. #include "includes.h"
  16. #ifndef CONFIG_NATIVE_WINDOWS
  17. #include <net/if.h>
  18. #include "eloop.h"
  19. #include "hostapd.h"
  20. #include "ieee802_11.h"
  21. #include "beacon.h"
  22. #include "hw_features.h"
  23. #include "radius/radius.h"
  24. #include "radius/radius_client.h"
  25. #include "ieee802_11_auth.h"
  26. #include "sta_info.h"
  27. #include "rc4.h"
  28. #include "ieee802_1x.h"
  29. #include "wpa.h"
  30. #include "wme.h"
  31. #include "ap_list.h"
  32. #include "accounting.h"
  33. #include "driver.h"
  34. #include "mlme.h"
  35. u8 * hostapd_eid_supp_rates(struct hostapd_data *hapd, u8 *eid)
  36. {
  37. u8 *pos = eid;
  38. int i, num, count;
  39. if (hapd->iface->current_rates == NULL)
  40. return eid;
  41. *pos++ = WLAN_EID_SUPP_RATES;
  42. num = hapd->iface->num_rates;
  43. if (num > 8) {
  44. /* rest of the rates are encoded in Extended supported
  45. * rates element */
  46. num = 8;
  47. }
  48. *pos++ = num;
  49. count = 0;
  50. for (i = 0, count = 0; i < hapd->iface->num_rates && count < num;
  51. i++) {
  52. count++;
  53. *pos = hapd->iface->current_rates[i].rate / 5;
  54. if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
  55. *pos |= 0x80;
  56. pos++;
  57. }
  58. return pos;
  59. }
  60. u8 * hostapd_eid_ext_supp_rates(struct hostapd_data *hapd, u8 *eid)
  61. {
  62. u8 *pos = eid;
  63. int i, num, count;
  64. if (hapd->iface->current_rates == NULL)
  65. return eid;
  66. num = hapd->iface->num_rates;
  67. if (num <= 8)
  68. return eid;
  69. num -= 8;
  70. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  71. *pos++ = num;
  72. count = 0;
  73. for (i = 0, count = 0; i < hapd->iface->num_rates && count < num + 8;
  74. i++) {
  75. count++;
  76. if (count <= 8)
  77. continue; /* already in SuppRates IE */
  78. *pos = hapd->iface->current_rates[i].rate / 5;
  79. if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
  80. *pos |= 0x80;
  81. pos++;
  82. }
  83. return pos;
  84. }
  85. u8 * hostapd_eid_ht_capabilities_info(struct hostapd_data *hapd, u8 *eid)
  86. {
  87. #ifdef CONFIG_IEEE80211N
  88. struct ieee80211_ht_capability *cap;
  89. u8 *pos = eid;
  90. if (!hapd->iconf->ieee80211n)
  91. return eid;
  92. *pos++ = WLAN_EID_HT_CAP;
  93. *pos++ = sizeof(*cap);
  94. cap = (struct ieee80211_ht_capability *) pos;
  95. os_memset(cap, 0, sizeof(*cap));
  96. SET_2BIT_U8(&cap->mac_ht_params_info,
  97. MAC_HT_PARAM_INFO_MAX_RX_AMPDU_FACTOR_OFFSET,
  98. MAX_RX_AMPDU_FACTOR_64KB);
  99. cap->capabilities_info = host_to_le16(hapd->iconf->ht_capab);
  100. cap->supported_mcs_set[0] = 0xff;
  101. cap->supported_mcs_set[1] = 0xff;
  102. pos += sizeof(*cap);
  103. return pos;
  104. #else /* CONFIG_IEEE80211N */
  105. return eid;
  106. #endif /* CONFIG_IEEE80211N */
  107. }
  108. u8 * hostapd_eid_ht_operation(struct hostapd_data *hapd, u8 *eid)
  109. {
  110. #ifdef CONFIG_IEEE80211N
  111. struct ieee80211_ht_operation *oper;
  112. u8 *pos = eid;
  113. if (!hapd->iconf->ieee80211n)
  114. return eid;
  115. *pos++ = WLAN_EID_HT_OPERATION;
  116. *pos++ = sizeof(*oper);
  117. oper = (struct ieee80211_ht_operation *) pos;
  118. os_memset(oper, 0, sizeof(*oper));
  119. oper->operation_mode = host_to_le16(hapd->iface->ht_op_mode);
  120. pos += sizeof(*oper);
  121. return pos;
  122. #else /* CONFIG_IEEE80211N */
  123. return eid;
  124. #endif /* CONFIG_IEEE80211N */
  125. }
  126. #ifdef CONFIG_IEEE80211N
  127. /*
  128. op_mode
  129. Set to 0 (HT pure) under the followign conditions
  130. - all STAs in the BSS are 20/40 MHz HT in 20/40 MHz BSS or
  131. - all STAs in the BSS are 20 MHz HT in 20 MHz BSS
  132. Set to 1 (HT non-member protection) if there may be non-HT STAs
  133. in both the primary and the secondary channel
  134. Set to 2 if only HT STAs are associated in BSS,
  135. however and at least one 20 MHz HT STA is associated
  136. Set to 3 (HT mixed mode) when one or more non-HT STAs are associated
  137. (currently non-GF HT station is considered as non-HT STA also)
  138. */
  139. int hostapd_ht_operation_update(struct hostapd_iface *iface)
  140. {
  141. u16 cur_op_mode, new_op_mode;
  142. int op_mode_changes = 0;
  143. if (!iface->conf->ieee80211n || iface->conf->ht_op_mode_fixed)
  144. return 0;
  145. wpa_printf(MSG_DEBUG, "%s current operation mode=0x%X",
  146. __func__, iface->ht_op_mode);
  147. if (!(iface->ht_op_mode & HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT)
  148. && iface->num_sta_ht_no_gf) {
  149. iface->ht_op_mode |=
  150. HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT;
  151. op_mode_changes++;
  152. } else if ((iface->ht_op_mode &
  153. HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT) &&
  154. iface->num_sta_ht_no_gf == 0) {
  155. iface->ht_op_mode &=
  156. ~HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT;
  157. op_mode_changes++;
  158. }
  159. if (!(iface->ht_op_mode & HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT) &&
  160. (iface->num_sta_no_ht || iface->olbc_ht)) {
  161. iface->ht_op_mode |= HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT;
  162. op_mode_changes++;
  163. } else if ((iface->ht_op_mode &
  164. HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT) &&
  165. (iface->num_sta_no_ht == 0 && !iface->olbc_ht)) {
  166. iface->ht_op_mode &=
  167. ~HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT;
  168. op_mode_changes++;
  169. }
  170. /* Note: currently we switch to the MIXED op mode if HT non-greenfield
  171. * station is associated. Probably it's a theoretical case, since
  172. * it looks like all known HT STAs support greenfield.
  173. */
  174. new_op_mode = 0;
  175. if (iface->num_sta_no_ht ||
  176. (iface->ht_op_mode & HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT))
  177. new_op_mode = OP_MODE_MIXED;
  178. else if ((iface->conf->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)
  179. && iface->num_sta_ht_20mhz)
  180. new_op_mode = OP_MODE_20MHZ_HT_STA_ASSOCED;
  181. else if (iface->olbc_ht)
  182. new_op_mode = OP_MODE_MAY_BE_LEGACY_STAS;
  183. else
  184. new_op_mode = OP_MODE_PURE;
  185. cur_op_mode = iface->ht_op_mode & HT_INFO_OPERATION_MODE_OP_MODE_MASK;
  186. if (cur_op_mode != new_op_mode) {
  187. iface->ht_op_mode &= ~HT_INFO_OPERATION_MODE_OP_MODE_MASK;
  188. iface->ht_op_mode |= new_op_mode;
  189. op_mode_changes++;
  190. }
  191. wpa_printf(MSG_DEBUG, "%s new operation mode=0x%X changes=%d",
  192. __func__, iface->ht_op_mode, op_mode_changes);
  193. return op_mode_changes;
  194. }
  195. #endif /* CONFIG_IEEE80211N */
  196. u16 hostapd_own_capab_info(struct hostapd_data *hapd, struct sta_info *sta,
  197. int probe)
  198. {
  199. int capab = WLAN_CAPABILITY_ESS;
  200. int privacy;
  201. if (hapd->iface->num_sta_no_short_preamble == 0 &&
  202. hapd->iconf->preamble == SHORT_PREAMBLE)
  203. capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
  204. privacy = hapd->conf->ssid.wep.keys_set;
  205. if (hapd->conf->ieee802_1x &&
  206. (hapd->conf->default_wep_key_len ||
  207. hapd->conf->individual_wep_key_len))
  208. privacy = 1;
  209. if (hapd->conf->wpa)
  210. privacy = 1;
  211. if (sta) {
  212. int policy, def_klen;
  213. if (probe && sta->ssid_probe) {
  214. policy = sta->ssid_probe->security_policy;
  215. def_klen = sta->ssid_probe->wep.default_len;
  216. } else {
  217. policy = sta->ssid->security_policy;
  218. def_klen = sta->ssid->wep.default_len;
  219. }
  220. privacy = policy != SECURITY_PLAINTEXT;
  221. if (policy == SECURITY_IEEE_802_1X && def_klen == 0)
  222. privacy = 0;
  223. }
  224. if (privacy)
  225. capab |= WLAN_CAPABILITY_PRIVACY;
  226. if (hapd->iface->current_mode &&
  227. hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
  228. hapd->iface->num_sta_no_short_slot_time == 0)
  229. capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
  230. return capab;
  231. }
  232. #ifdef CONFIG_IEEE80211W
  233. static u8 * hostapd_eid_assoc_comeback_time(struct hostapd_data *hapd,
  234. struct sta_info *sta, u8 *eid)
  235. {
  236. u8 *pos = eid;
  237. u32 timeout;
  238. *pos++ = WLAN_EID_ASSOC_COMEBACK_TIME;
  239. *pos++ = 4;
  240. timeout = (hapd->conf->assoc_ping_attempts - sta->ping_count + 1) *
  241. hapd->conf->assoc_ping_timeout;
  242. WPA_PUT_LE32(pos, timeout);
  243. pos += 4;
  244. return pos;
  245. }
  246. #endif /* CONFIG_IEEE80211W */
  247. void ieee802_11_print_ssid(char *buf, const u8 *ssid, u8 len)
  248. {
  249. int i;
  250. if (len > HOSTAPD_MAX_SSID_LEN)
  251. len = HOSTAPD_MAX_SSID_LEN;
  252. for (i = 0; i < len; i++) {
  253. if (ssid[i] >= 32 && ssid[i] < 127)
  254. buf[i] = ssid[i];
  255. else
  256. buf[i] = '.';
  257. }
  258. buf[len] = '\0';
  259. }
  260. void ieee802_11_send_deauth(struct hostapd_data *hapd, u8 *addr, u16 reason)
  261. {
  262. struct ieee80211_mgmt mgmt;
  263. hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
  264. HOSTAPD_LEVEL_DEBUG,
  265. "deauthenticate - reason %d", reason);
  266. os_memset(&mgmt, 0, sizeof(mgmt));
  267. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  268. WLAN_FC_STYPE_DEAUTH);
  269. os_memcpy(mgmt.da, addr, ETH_ALEN);
  270. os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
  271. os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
  272. mgmt.u.deauth.reason_code = host_to_le16(reason);
  273. if (hostapd_send_mgmt_frame(hapd, &mgmt, IEEE80211_HDRLEN +
  274. sizeof(mgmt.u.deauth), 0) < 0)
  275. perror("ieee802_11_send_deauth: send");
  276. }
  277. static void ieee802_11_sta_authenticate(void *eloop_ctx, void *timeout_ctx)
  278. {
  279. struct hostapd_data *hapd = eloop_ctx;
  280. struct ieee80211_mgmt mgmt;
  281. char ssid_txt[33];
  282. if (hapd->assoc_ap_state == WAIT_BEACON)
  283. hapd->assoc_ap_state = AUTHENTICATE;
  284. if (hapd->assoc_ap_state != AUTHENTICATE)
  285. return;
  286. ieee802_11_print_ssid(ssid_txt, (u8 *) hapd->assoc_ap_ssid,
  287. hapd->assoc_ap_ssid_len);
  288. printf("Authenticate with AP " MACSTR " SSID=%s (as station)\n",
  289. MAC2STR(hapd->conf->assoc_ap_addr), ssid_txt);
  290. os_memset(&mgmt, 0, sizeof(mgmt));
  291. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  292. WLAN_FC_STYPE_AUTH);
  293. os_memcpy(mgmt.da, hapd->conf->assoc_ap_addr, ETH_ALEN);
  294. os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
  295. os_memcpy(mgmt.bssid, hapd->conf->assoc_ap_addr, ETH_ALEN);
  296. mgmt.u.auth.auth_alg = host_to_le16(WLAN_AUTH_OPEN);
  297. mgmt.u.auth.auth_transaction = host_to_le16(1);
  298. mgmt.u.auth.status_code = host_to_le16(0);
  299. if (hostapd_send_mgmt_frame(hapd, &mgmt, IEEE80211_HDRLEN +
  300. sizeof(mgmt.u.auth), 0) < 0)
  301. perror("ieee802_11_sta_authenticate: send");
  302. /* Try to authenticate again, if this attempt fails or times out. */
  303. eloop_register_timeout(5, 0, ieee802_11_sta_authenticate, hapd, NULL);
  304. }
  305. static void ieee802_11_sta_associate(void *eloop_ctx, void *timeout_ctx)
  306. {
  307. struct hostapd_data *hapd = eloop_ctx;
  308. u8 buf[256];
  309. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
  310. u8 *p;
  311. char ssid_txt[33];
  312. if (hapd->assoc_ap_state == AUTHENTICATE)
  313. hapd->assoc_ap_state = ASSOCIATE;
  314. if (hapd->assoc_ap_state != ASSOCIATE)
  315. return;
  316. ieee802_11_print_ssid(ssid_txt, (u8 *) hapd->assoc_ap_ssid,
  317. hapd->assoc_ap_ssid_len);
  318. printf("Associate with AP " MACSTR " SSID=%s (as station)\n",
  319. MAC2STR(hapd->conf->assoc_ap_addr), ssid_txt);
  320. os_memset(mgmt, 0, sizeof(*mgmt));
  321. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  322. WLAN_FC_STYPE_ASSOC_REQ);
  323. os_memcpy(mgmt->da, hapd->conf->assoc_ap_addr, ETH_ALEN);
  324. os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
  325. os_memcpy(mgmt->bssid, hapd->conf->assoc_ap_addr, ETH_ALEN);
  326. mgmt->u.assoc_req.capab_info = host_to_le16(0);
  327. mgmt->u.assoc_req.listen_interval = host_to_le16(1);
  328. p = &mgmt->u.assoc_req.variable[0];
  329. *p++ = WLAN_EID_SSID;
  330. *p++ = hapd->assoc_ap_ssid_len;
  331. os_memcpy(p, hapd->assoc_ap_ssid, hapd->assoc_ap_ssid_len);
  332. p += hapd->assoc_ap_ssid_len;
  333. p = hostapd_eid_supp_rates(hapd, p);
  334. p = hostapd_eid_ext_supp_rates(hapd, p);
  335. if (hostapd_send_mgmt_frame(hapd, mgmt, p - (u8 *) mgmt, 0) < 0)
  336. perror("ieee802_11_sta_associate: send");
  337. /* Try to authenticate again, if this attempt fails or times out. */
  338. eloop_register_timeout(5, 0, ieee802_11_sta_associate, hapd, NULL);
  339. }
  340. static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
  341. u16 auth_transaction, u8 *challenge, int iswep)
  342. {
  343. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  344. HOSTAPD_LEVEL_DEBUG,
  345. "authentication (shared key, transaction %d)",
  346. auth_transaction);
  347. if (auth_transaction == 1) {
  348. if (!sta->challenge) {
  349. /* Generate a pseudo-random challenge */
  350. u8 key[8];
  351. time_t now;
  352. int r;
  353. sta->challenge = os_zalloc(WLAN_AUTH_CHALLENGE_LEN);
  354. if (sta->challenge == NULL)
  355. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  356. now = time(NULL);
  357. r = random();
  358. os_memcpy(key, &now, 4);
  359. os_memcpy(key + 4, &r, 4);
  360. rc4(sta->challenge, WLAN_AUTH_CHALLENGE_LEN,
  361. key, sizeof(key));
  362. }
  363. return 0;
  364. }
  365. if (auth_transaction != 3)
  366. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  367. /* Transaction 3 */
  368. if (!iswep || !sta->challenge || !challenge ||
  369. os_memcmp(sta->challenge, challenge, WLAN_AUTH_CHALLENGE_LEN)) {
  370. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  371. HOSTAPD_LEVEL_INFO,
  372. "shared key authentication - invalid "
  373. "challenge-response");
  374. return WLAN_STATUS_CHALLENGE_FAIL;
  375. }
  376. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  377. HOSTAPD_LEVEL_DEBUG,
  378. "authentication OK (shared key)");
  379. #ifdef IEEE80211_REQUIRE_AUTH_ACK
  380. /* Station will be marked authenticated if it ACKs the
  381. * authentication reply. */
  382. #else
  383. sta->flags |= WLAN_STA_AUTH;
  384. wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
  385. #endif
  386. os_free(sta->challenge);
  387. sta->challenge = NULL;
  388. return 0;
  389. }
  390. static void send_auth_reply(struct hostapd_data *hapd,
  391. const u8 *dst, const u8 *bssid,
  392. u16 auth_alg, u16 auth_transaction, u16 resp,
  393. const u8 *ies, size_t ies_len)
  394. {
  395. struct ieee80211_mgmt *reply;
  396. u8 *buf;
  397. size_t rlen;
  398. rlen = IEEE80211_HDRLEN + sizeof(reply->u.auth) + ies_len;
  399. buf = os_zalloc(rlen);
  400. if (buf == NULL)
  401. return;
  402. reply = (struct ieee80211_mgmt *) buf;
  403. reply->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  404. WLAN_FC_STYPE_AUTH);
  405. os_memcpy(reply->da, dst, ETH_ALEN);
  406. os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
  407. os_memcpy(reply->bssid, bssid, ETH_ALEN);
  408. reply->u.auth.auth_alg = host_to_le16(auth_alg);
  409. reply->u.auth.auth_transaction = host_to_le16(auth_transaction);
  410. reply->u.auth.status_code = host_to_le16(resp);
  411. if (ies && ies_len)
  412. os_memcpy(reply->u.auth.variable, ies, ies_len);
  413. wpa_printf(MSG_DEBUG, "authentication reply: STA=" MACSTR
  414. " auth_alg=%d auth_transaction=%d resp=%d (IE len=%lu)",
  415. MAC2STR(dst), auth_alg, auth_transaction,
  416. resp, (unsigned long) ies_len);
  417. if (hostapd_send_mgmt_frame(hapd, reply, rlen, 0) < 0)
  418. perror("send_auth_reply: send");
  419. os_free(buf);
  420. }
  421. #ifdef CONFIG_IEEE80211R
  422. static void handle_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid,
  423. u16 auth_transaction, u16 status,
  424. const u8 *ies, size_t ies_len)
  425. {
  426. struct hostapd_data *hapd = ctx;
  427. struct sta_info *sta;
  428. send_auth_reply(hapd, dst, bssid, WLAN_AUTH_FT, auth_transaction,
  429. status, ies, ies_len);
  430. if (status != WLAN_STATUS_SUCCESS)
  431. return;
  432. sta = ap_get_sta(hapd, dst);
  433. if (sta == NULL)
  434. return;
  435. hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
  436. HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
  437. sta->flags |= WLAN_STA_AUTH;
  438. mlme_authenticate_indication(hapd, sta);
  439. }
  440. #endif /* CONFIG_IEEE80211R */
  441. static void handle_auth(struct hostapd_data *hapd, struct ieee80211_mgmt *mgmt,
  442. size_t len)
  443. {
  444. u16 auth_alg, auth_transaction, status_code;
  445. u16 resp = WLAN_STATUS_SUCCESS;
  446. struct sta_info *sta = NULL;
  447. int res;
  448. u16 fc;
  449. u8 *challenge = NULL;
  450. u32 session_timeout, acct_interim_interval;
  451. int vlan_id = 0;
  452. u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
  453. size_t resp_ies_len = 0;
  454. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
  455. printf("handle_auth - too short payload (len=%lu)\n",
  456. (unsigned long) len);
  457. return;
  458. }
  459. auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
  460. auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
  461. status_code = le_to_host16(mgmt->u.auth.status_code);
  462. fc = le_to_host16(mgmt->frame_control);
  463. if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
  464. 2 + WLAN_AUTH_CHALLENGE_LEN &&
  465. mgmt->u.auth.variable[0] == WLAN_EID_CHALLENGE &&
  466. mgmt->u.auth.variable[1] == WLAN_AUTH_CHALLENGE_LEN)
  467. challenge = &mgmt->u.auth.variable[2];
  468. wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
  469. "auth_transaction=%d status_code=%d wep=%d%s",
  470. MAC2STR(mgmt->sa), auth_alg, auth_transaction,
  471. status_code, !!(fc & WLAN_FC_ISWEP),
  472. challenge ? " challenge" : "");
  473. if (hapd->assoc_ap_state == AUTHENTICATE && auth_transaction == 2 &&
  474. os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0 &&
  475. os_memcmp(mgmt->bssid, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0) {
  476. if (status_code != 0) {
  477. printf("Authentication (as station) with AP "
  478. MACSTR " failed (status_code=%d)\n",
  479. MAC2STR(hapd->conf->assoc_ap_addr),
  480. status_code);
  481. return;
  482. }
  483. printf("Authenticated (as station) with AP " MACSTR "\n",
  484. MAC2STR(hapd->conf->assoc_ap_addr));
  485. ieee802_11_sta_associate(hapd, NULL);
  486. return;
  487. }
  488. if (hapd->tkip_countermeasures) {
  489. resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
  490. goto fail;
  491. }
  492. if (!(((hapd->conf->auth_algs & WPA_AUTH_ALG_OPEN) &&
  493. auth_alg == WLAN_AUTH_OPEN) ||
  494. #ifdef CONFIG_IEEE80211R
  495. (hapd->conf->wpa &&
  496. (hapd->conf->wpa_key_mgmt &
  497. (WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_FT_PSK)) &&
  498. auth_alg == WLAN_AUTH_FT) ||
  499. #endif /* CONFIG_IEEE80211R */
  500. ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
  501. auth_alg == WLAN_AUTH_SHARED_KEY))) {
  502. printf("Unsupported authentication algorithm (%d)\n",
  503. auth_alg);
  504. resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
  505. goto fail;
  506. }
  507. if (!(auth_transaction == 1 ||
  508. (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
  509. printf("Unknown authentication transaction number (%d)\n",
  510. auth_transaction);
  511. resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
  512. goto fail;
  513. }
  514. if (os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
  515. printf("Station " MACSTR " not allowed to authenticate.\n",
  516. MAC2STR(mgmt->sa));
  517. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  518. goto fail;
  519. }
  520. res = hostapd_allowed_address(hapd, mgmt->sa, (u8 *) mgmt, len,
  521. &session_timeout,
  522. &acct_interim_interval, &vlan_id);
  523. if (res == HOSTAPD_ACL_REJECT) {
  524. printf("Station " MACSTR " not allowed to authenticate.\n",
  525. MAC2STR(mgmt->sa));
  526. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  527. goto fail;
  528. }
  529. if (res == HOSTAPD_ACL_PENDING) {
  530. wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
  531. " waiting for an external authentication",
  532. MAC2STR(mgmt->sa));
  533. /* Authentication code will re-send the authentication frame
  534. * after it has received (and cached) information from the
  535. * external source. */
  536. return;
  537. }
  538. sta = ap_sta_add(hapd, mgmt->sa);
  539. if (!sta) {
  540. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  541. goto fail;
  542. }
  543. if (vlan_id > 0) {
  544. if (hostapd_get_vlan_id_ifname(hapd->conf->vlan,
  545. sta->vlan_id) == NULL) {
  546. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
  547. HOSTAPD_LEVEL_INFO, "Invalid VLAN ID "
  548. "%d received from RADIUS server",
  549. vlan_id);
  550. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  551. goto fail;
  552. }
  553. sta->vlan_id = vlan_id;
  554. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
  555. HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
  556. }
  557. sta->flags &= ~WLAN_STA_PREAUTH;
  558. ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
  559. if (hapd->conf->radius->acct_interim_interval == 0 &&
  560. acct_interim_interval)
  561. sta->acct_interim_interval = acct_interim_interval;
  562. if (res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
  563. ap_sta_session_timeout(hapd, sta, session_timeout);
  564. else
  565. ap_sta_no_session_timeout(hapd, sta);
  566. switch (auth_alg) {
  567. case WLAN_AUTH_OPEN:
  568. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  569. HOSTAPD_LEVEL_DEBUG,
  570. "authentication OK (open system)");
  571. #ifdef IEEE80211_REQUIRE_AUTH_ACK
  572. /* Station will be marked authenticated if it ACKs the
  573. * authentication reply. */
  574. #else
  575. sta->flags |= WLAN_STA_AUTH;
  576. wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
  577. sta->auth_alg = WLAN_AUTH_OPEN;
  578. mlme_authenticate_indication(hapd, sta);
  579. #endif
  580. break;
  581. case WLAN_AUTH_SHARED_KEY:
  582. resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
  583. fc & WLAN_FC_ISWEP);
  584. sta->auth_alg = WLAN_AUTH_SHARED_KEY;
  585. mlme_authenticate_indication(hapd, sta);
  586. if (sta->challenge && auth_transaction == 1) {
  587. resp_ies[0] = WLAN_EID_CHALLENGE;
  588. resp_ies[1] = WLAN_AUTH_CHALLENGE_LEN;
  589. os_memcpy(resp_ies + 2, sta->challenge,
  590. WLAN_AUTH_CHALLENGE_LEN);
  591. resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
  592. }
  593. break;
  594. #ifdef CONFIG_IEEE80211R
  595. case WLAN_AUTH_FT:
  596. sta->auth_alg = WLAN_AUTH_FT;
  597. if (sta->wpa_sm == NULL)
  598. sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
  599. sta->addr);
  600. if (sta->wpa_sm == NULL) {
  601. wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
  602. "state machine");
  603. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  604. goto fail;
  605. }
  606. wpa_ft_process_auth(sta->wpa_sm, mgmt->bssid,
  607. auth_transaction, mgmt->u.auth.variable,
  608. len - IEEE80211_HDRLEN -
  609. sizeof(mgmt->u.auth),
  610. handle_auth_ft_finish, hapd);
  611. /* handle_auth_ft_finish() callback will complete auth. */
  612. return;
  613. #endif /* CONFIG_IEEE80211R */
  614. }
  615. fail:
  616. send_auth_reply(hapd, mgmt->sa, mgmt->bssid, auth_alg,
  617. auth_transaction + 1, resp, resp_ies, resp_ies_len);
  618. }
  619. static void handle_assoc(struct hostapd_data *hapd,
  620. struct ieee80211_mgmt *mgmt, size_t len, int reassoc)
  621. {
  622. u16 capab_info, listen_interval;
  623. u16 resp = WLAN_STATUS_SUCCESS;
  624. u8 *pos, *wpa_ie;
  625. size_t wpa_ie_len;
  626. int send_deauth = 0, send_len, left, i;
  627. struct sta_info *sta;
  628. struct ieee802_11_elems elems;
  629. u8 buf[sizeof(struct ieee80211_mgmt) + 512];
  630. struct ieee80211_mgmt *reply;
  631. if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
  632. sizeof(mgmt->u.assoc_req))) {
  633. printf("handle_assoc(reassoc=%d) - too short payload (len=%lu)"
  634. "\n", reassoc, (unsigned long) len);
  635. return;
  636. }
  637. if (reassoc) {
  638. capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
  639. listen_interval = le_to_host16(
  640. mgmt->u.reassoc_req.listen_interval);
  641. wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
  642. " capab_info=0x%02x listen_interval=%d current_ap="
  643. MACSTR,
  644. MAC2STR(mgmt->sa), capab_info, listen_interval,
  645. MAC2STR(mgmt->u.reassoc_req.current_ap));
  646. left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
  647. pos = mgmt->u.reassoc_req.variable;
  648. } else {
  649. capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
  650. listen_interval = le_to_host16(
  651. mgmt->u.assoc_req.listen_interval);
  652. wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
  653. " capab_info=0x%02x listen_interval=%d",
  654. MAC2STR(mgmt->sa), capab_info, listen_interval);
  655. left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
  656. pos = mgmt->u.assoc_req.variable;
  657. }
  658. sta = ap_get_sta(hapd, mgmt->sa);
  659. #ifdef CONFIG_IEEE80211R
  660. if (sta && sta->auth_alg == WLAN_AUTH_FT &&
  661. (sta->flags & WLAN_STA_AUTH) == 0) {
  662. wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
  663. "prior to authentication since it is using "
  664. "over-the-DS FT", MAC2STR(mgmt->sa));
  665. } else
  666. #endif /* CONFIG_IEEE80211R */
  667. if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
  668. printf("STA " MACSTR " trying to associate before "
  669. "authentication\n", MAC2STR(mgmt->sa));
  670. if (sta) {
  671. printf(" sta: addr=" MACSTR " aid=%d flags=0x%04x\n",
  672. MAC2STR(sta->addr), sta->aid, sta->flags);
  673. }
  674. send_deauth = 1;
  675. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  676. goto fail;
  677. }
  678. if (hapd->tkip_countermeasures) {
  679. resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
  680. goto fail;
  681. }
  682. if (listen_interval > hapd->conf->max_listen_interval) {
  683. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  684. HOSTAPD_LEVEL_DEBUG,
  685. "Too large Listen Interval (%d)",
  686. listen_interval);
  687. resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
  688. goto fail;
  689. }
  690. sta->capability = capab_info;
  691. /* followed by SSID and Supported rates; and HT capabilities if 802.11n
  692. * is used */
  693. if (ieee802_11_parse_elems(pos, left, &elems, 1) == ParseFailed ||
  694. !elems.ssid) {
  695. printf("STA " MACSTR " sent invalid association request\n",
  696. MAC2STR(sta->addr));
  697. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  698. goto fail;
  699. }
  700. if (elems.ssid_len != hapd->conf->ssid.ssid_len ||
  701. os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) != 0)
  702. {
  703. char ssid_txt[33];
  704. ieee802_11_print_ssid(ssid_txt, elems.ssid, elems.ssid_len);
  705. printf("Station " MACSTR " tried to associate with "
  706. "unknown SSID '%s'\n", MAC2STR(sta->addr), ssid_txt);
  707. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  708. goto fail;
  709. }
  710. sta->flags &= ~WLAN_STA_WME;
  711. if (elems.wme && hapd->conf->wme_enabled) {
  712. if (hostapd_eid_wme_valid(hapd, elems.wme, elems.wme_len))
  713. hostapd_logger(hapd, sta->addr,
  714. HOSTAPD_MODULE_WPA,
  715. HOSTAPD_LEVEL_DEBUG,
  716. "invalid WME element in association "
  717. "request");
  718. else
  719. sta->flags |= WLAN_STA_WME;
  720. }
  721. if (!elems.supp_rates) {
  722. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  723. HOSTAPD_LEVEL_DEBUG,
  724. "No supported rates element in AssocReq");
  725. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  726. goto fail;
  727. }
  728. if (elems.supp_rates_len > sizeof(sta->supported_rates)) {
  729. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  730. HOSTAPD_LEVEL_DEBUG,
  731. "Invalid supported rates element length %d",
  732. elems.supp_rates_len);
  733. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  734. goto fail;
  735. }
  736. os_memset(sta->supported_rates, 0, sizeof(sta->supported_rates));
  737. os_memcpy(sta->supported_rates, elems.supp_rates,
  738. elems.supp_rates_len);
  739. sta->supported_rates_len = elems.supp_rates_len;
  740. if (elems.ext_supp_rates) {
  741. if (elems.supp_rates_len + elems.ext_supp_rates_len >
  742. sizeof(sta->supported_rates)) {
  743. hostapd_logger(hapd, mgmt->sa,
  744. HOSTAPD_MODULE_IEEE80211,
  745. HOSTAPD_LEVEL_DEBUG,
  746. "Invalid supported rates element length"
  747. " %d+%d", elems.supp_rates_len,
  748. elems.ext_supp_rates_len);
  749. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  750. goto fail;
  751. }
  752. os_memcpy(sta->supported_rates + elems.supp_rates_len,
  753. elems.ext_supp_rates, elems.ext_supp_rates_len);
  754. sta->supported_rates_len += elems.ext_supp_rates_len;
  755. }
  756. #ifdef CONFIG_IEEE80211N
  757. /* save HT capabilities in the sta object */
  758. os_memset(&sta->ht_capabilities, 0, sizeof(sta->ht_capabilities));
  759. if (elems.ht_capabilities &&
  760. elems.ht_capabilities_len >= sizeof(struct ieee80211_ht_capability)
  761. && (sta->flags & WLAN_STA_WME)) {
  762. /* note: without WMM capability, treat the sta as non-HT */
  763. sta->flags |= WLAN_STA_HT;
  764. sta->ht_capabilities.id = WLAN_EID_HT_CAP;
  765. sta->ht_capabilities.length =
  766. sizeof(struct ieee80211_ht_capability);
  767. os_memcpy(&sta->ht_capabilities.data,
  768. elems.ht_capabilities,
  769. sizeof(struct ieee80211_ht_capability));
  770. } else
  771. sta->flags &= ~WLAN_STA_HT;
  772. #endif /* CONFIG_IEEE80211N */
  773. if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
  774. wpa_ie = elems.rsn_ie;
  775. wpa_ie_len = elems.rsn_ie_len;
  776. } else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
  777. elems.wpa_ie) {
  778. wpa_ie = elems.wpa_ie;
  779. wpa_ie_len = elems.wpa_ie_len;
  780. } else {
  781. wpa_ie = NULL;
  782. wpa_ie_len = 0;
  783. }
  784. if (hapd->conf->wpa && wpa_ie == NULL) {
  785. printf("STA " MACSTR ": No WPA/RSN IE in association "
  786. "request\n", MAC2STR(sta->addr));
  787. resp = WLAN_STATUS_INVALID_IE;
  788. goto fail;
  789. }
  790. if (hapd->conf->wpa && wpa_ie) {
  791. int res;
  792. wpa_ie -= 2;
  793. wpa_ie_len += 2;
  794. if (sta->wpa_sm == NULL)
  795. sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
  796. sta->addr);
  797. if (sta->wpa_sm == NULL) {
  798. printf("Failed to initialize WPA state machine\n");
  799. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  800. goto fail;
  801. }
  802. res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
  803. wpa_ie, wpa_ie_len,
  804. elems.mdie, elems.mdie_len);
  805. if (res == WPA_INVALID_GROUP)
  806. resp = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
  807. else if (res == WPA_INVALID_PAIRWISE)
  808. resp = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
  809. else if (res == WPA_INVALID_AKMP)
  810. resp = WLAN_STATUS_AKMP_NOT_VALID;
  811. else if (res == WPA_ALLOC_FAIL)
  812. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  813. #ifdef CONFIG_IEEE80211W
  814. else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
  815. resp = WLAN_STATUS_UNSPECIFIED_FAILURE; /* FIX */
  816. else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
  817. resp = WLAN_STATUS_UNSPECIFIED_FAILURE; /* FIX */
  818. #endif /* CONFIG_IEEE80211W */
  819. else if (res == WPA_INVALID_MDIE)
  820. resp = WLAN_STATUS_INVALID_MDIE;
  821. else if (res != WPA_IE_OK)
  822. resp = WLAN_STATUS_INVALID_IE;
  823. if (resp != WLAN_STATUS_SUCCESS)
  824. goto fail;
  825. #ifdef CONFIG_IEEE80211W
  826. if ((sta->flags & WLAN_STA_MFP) && !sta->ping_timed_out) {
  827. /*
  828. * STA has already been associated with MFP and ping
  829. * timeout has not been reached. Reject the
  830. * association attempt temporarily and start ping, if
  831. * one is not pending.
  832. */
  833. if (sta->ping_count == 0)
  834. ap_sta_start_ping(hapd, sta);
  835. resp = WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
  836. goto fail;
  837. }
  838. if (wpa_auth_uses_mfp(sta->wpa_sm))
  839. sta->flags |= WLAN_STA_MFP;
  840. else
  841. sta->flags &= ~WLAN_STA_MFP;
  842. #endif /* CONFIG_IEEE80211W */
  843. #ifdef CONFIG_IEEE80211R
  844. if (sta->auth_alg == WLAN_AUTH_FT) {
  845. if (!reassoc) {
  846. wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
  847. "to use association (not "
  848. "re-association) with FT auth_alg",
  849. MAC2STR(sta->addr));
  850. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  851. goto fail;
  852. }
  853. resp = wpa_ft_validate_reassoc(sta->wpa_sm, pos, left);
  854. if (resp != WLAN_STATUS_SUCCESS)
  855. goto fail;
  856. }
  857. #endif /* CONFIG_IEEE80211R */
  858. }
  859. if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
  860. sta->flags |= WLAN_STA_NONERP;
  861. for (i = 0; i < sta->supported_rates_len; i++) {
  862. if ((sta->supported_rates[i] & 0x7f) > 22) {
  863. sta->flags &= ~WLAN_STA_NONERP;
  864. break;
  865. }
  866. }
  867. if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
  868. sta->nonerp_set = 1;
  869. hapd->iface->num_sta_non_erp++;
  870. if (hapd->iface->num_sta_non_erp == 1)
  871. ieee802_11_set_beacons(hapd->iface);
  872. }
  873. if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
  874. !sta->no_short_slot_time_set) {
  875. sta->no_short_slot_time_set = 1;
  876. hapd->iface->num_sta_no_short_slot_time++;
  877. if (hapd->iface->current_mode->mode ==
  878. HOSTAPD_MODE_IEEE80211G &&
  879. hapd->iface->num_sta_no_short_slot_time == 1)
  880. ieee802_11_set_beacons(hapd->iface);
  881. }
  882. if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
  883. sta->flags |= WLAN_STA_SHORT_PREAMBLE;
  884. else
  885. sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
  886. if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
  887. !sta->no_short_preamble_set) {
  888. sta->no_short_preamble_set = 1;
  889. hapd->iface->num_sta_no_short_preamble++;
  890. if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
  891. && hapd->iface->num_sta_no_short_preamble == 1)
  892. ieee802_11_set_beacons(hapd->iface);
  893. }
  894. #ifdef CONFIG_IEEE80211N
  895. if (sta->flags & WLAN_STA_HT) {
  896. if ((sta->ht_capabilities.data.capabilities_info &
  897. HT_CAP_INFO_GREEN_FIELD) == 0) {
  898. hapd->iface->num_sta_ht_no_gf++;
  899. wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - no "
  900. "greenfield, num of non-gf stations %d",
  901. __func__, MAC2STR(sta->addr),
  902. hapd->iface->num_sta_ht_no_gf);
  903. }
  904. if ((sta->ht_capabilities.data.capabilities_info &
  905. HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) == 0) {
  906. hapd->iface->num_sta_ht_20mhz++;
  907. wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - 20 MHz HT, "
  908. "num of 20MHz HT STAs %d",
  909. __func__, MAC2STR(sta->addr),
  910. hapd->iface->num_sta_ht_20mhz);
  911. }
  912. } else {
  913. hapd->iface->num_sta_no_ht++;
  914. if (hapd->iconf->ieee80211n) {
  915. wpa_printf(MSG_DEBUG, "%s STA " MACSTR
  916. " - no HT, num of non-HT stations %d",
  917. __func__, MAC2STR(sta->addr),
  918. hapd->iface->num_sta_no_ht);
  919. }
  920. }
  921. if (hostapd_ht_operation_update(hapd->iface) > 0)
  922. ieee802_11_set_beacons(hapd->iface);
  923. #endif /* CONFIG_IEEE80211N */
  924. /* get a unique AID */
  925. if (sta->aid > 0) {
  926. wpa_printf(MSG_DEBUG, " old AID %d", sta->aid);
  927. } else {
  928. for (sta->aid = 1; sta->aid <= MAX_AID_TABLE_SIZE; sta->aid++)
  929. if (hapd->sta_aid[sta->aid - 1] == NULL)
  930. break;
  931. if (sta->aid > MAX_AID_TABLE_SIZE) {
  932. sta->aid = 0;
  933. resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
  934. wpa_printf(MSG_ERROR, " no room for more AIDs");
  935. goto fail;
  936. } else {
  937. hapd->sta_aid[sta->aid - 1] = sta;
  938. wpa_printf(MSG_DEBUG, " new AID %d", sta->aid);
  939. }
  940. }
  941. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  942. HOSTAPD_LEVEL_DEBUG,
  943. "association OK (aid %d)", sta->aid);
  944. /* Station will be marked associated, after it acknowledges AssocResp
  945. */
  946. if (reassoc) {
  947. os_memcpy(sta->previous_ap, mgmt->u.reassoc_req.current_ap,
  948. ETH_ALEN);
  949. }
  950. if (sta->last_assoc_req)
  951. os_free(sta->last_assoc_req);
  952. sta->last_assoc_req = os_malloc(len);
  953. if (sta->last_assoc_req)
  954. os_memcpy(sta->last_assoc_req, mgmt, len);
  955. /* Make sure that the previously registered inactivity timer will not
  956. * remove the STA immediately. */
  957. sta->timeout_next = STA_NULLFUNC;
  958. fail:
  959. os_memset(buf, 0, sizeof(buf));
  960. reply = (struct ieee80211_mgmt *) buf;
  961. reply->frame_control =
  962. IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  963. (send_deauth ? WLAN_FC_STYPE_DEAUTH :
  964. (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
  965. WLAN_FC_STYPE_ASSOC_RESP)));
  966. os_memcpy(reply->da, mgmt->sa, ETH_ALEN);
  967. os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
  968. os_memcpy(reply->bssid, mgmt->bssid, ETH_ALEN);
  969. send_len = IEEE80211_HDRLEN;
  970. if (send_deauth) {
  971. send_len += sizeof(reply->u.deauth);
  972. reply->u.deauth.reason_code = host_to_le16(resp);
  973. } else {
  974. u8 *p;
  975. send_len += sizeof(reply->u.assoc_resp);
  976. reply->u.assoc_resp.capab_info =
  977. host_to_le16(hostapd_own_capab_info(hapd, sta, 0));
  978. reply->u.assoc_resp.status_code = host_to_le16(resp);
  979. reply->u.assoc_resp.aid = host_to_le16((sta ? sta->aid : 0)
  980. | BIT(14) | BIT(15));
  981. /* Supported rates */
  982. p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
  983. /* Extended supported rates */
  984. p = hostapd_eid_ext_supp_rates(hapd, p);
  985. if (sta->flags & WLAN_STA_WME)
  986. p = hostapd_eid_wme(hapd, p);
  987. p = hostapd_eid_ht_capabilities_info(hapd, p);
  988. p = hostapd_eid_ht_operation(hapd, p);
  989. #ifdef CONFIG_IEEE80211R
  990. if (resp == WLAN_STATUS_SUCCESS) {
  991. /* IEEE 802.11r: Mobility Domain Information, Fast BSS
  992. * Transition Information, RSN */
  993. p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
  994. buf + sizeof(buf) - p,
  995. sta->auth_alg);
  996. }
  997. #endif /* CONFIG_IEEE80211R */
  998. #ifdef CONFIG_IEEE80211W
  999. if (resp == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
  1000. p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
  1001. #endif /* CONFIG_IEEE80211W */
  1002. send_len += p - reply->u.assoc_resp.variable;
  1003. }
  1004. if (hostapd_send_mgmt_frame(hapd, reply, send_len, 0) < 0)
  1005. perror("handle_assoc: send");
  1006. }
  1007. static void handle_assoc_resp(struct hostapd_data *hapd,
  1008. struct ieee80211_mgmt *mgmt, size_t len)
  1009. {
  1010. u16 status_code, aid;
  1011. if (hapd->assoc_ap_state != ASSOCIATE) {
  1012. printf("Unexpected association response received from " MACSTR
  1013. "\n", MAC2STR(mgmt->sa));
  1014. return;
  1015. }
  1016. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_resp)) {
  1017. printf("handle_assoc_resp - too short payload (len=%lu)\n",
  1018. (unsigned long) len);
  1019. return;
  1020. }
  1021. if (os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) != 0 ||
  1022. os_memcmp(mgmt->bssid, hapd->conf->assoc_ap_addr, ETH_ALEN) != 0) {
  1023. printf("Received association response from unexpected address "
  1024. "(SA=" MACSTR " BSSID=" MACSTR "\n",
  1025. MAC2STR(mgmt->sa), MAC2STR(mgmt->bssid));
  1026. return;
  1027. }
  1028. status_code = le_to_host16(mgmt->u.assoc_resp.status_code);
  1029. aid = le_to_host16(mgmt->u.assoc_resp.aid);
  1030. aid &= ~(BIT(14) | BIT(15));
  1031. if (status_code != 0) {
  1032. printf("Association (as station) with AP " MACSTR " failed "
  1033. "(status_code=%d)\n",
  1034. MAC2STR(hapd->conf->assoc_ap_addr), status_code);
  1035. /* Try to authenticate again */
  1036. hapd->assoc_ap_state = AUTHENTICATE;
  1037. eloop_register_timeout(5, 0, ieee802_11_sta_authenticate,
  1038. hapd, NULL);
  1039. }
  1040. printf("Associated (as station) with AP " MACSTR " (aid=%d)\n",
  1041. MAC2STR(hapd->conf->assoc_ap_addr), aid);
  1042. hapd->assoc_ap_aid = aid;
  1043. hapd->assoc_ap_state = ASSOCIATED;
  1044. if (hostapd_set_assoc_ap(hapd, hapd->conf->assoc_ap_addr)) {
  1045. printf("Could not set associated AP address to kernel "
  1046. "driver.\n");
  1047. }
  1048. }
  1049. static void handle_disassoc(struct hostapd_data *hapd,
  1050. struct ieee80211_mgmt *mgmt, size_t len)
  1051. {
  1052. struct sta_info *sta;
  1053. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
  1054. printf("handle_disassoc - too short payload (len=%lu)\n",
  1055. (unsigned long) len);
  1056. return;
  1057. }
  1058. wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
  1059. MAC2STR(mgmt->sa),
  1060. le_to_host16(mgmt->u.disassoc.reason_code));
  1061. if (hapd->assoc_ap_state != DO_NOT_ASSOC &&
  1062. os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0) {
  1063. printf("Assoc AP " MACSTR " sent disassociation "
  1064. "(reason_code=%d) - try to authenticate\n",
  1065. MAC2STR(hapd->conf->assoc_ap_addr),
  1066. le_to_host16(mgmt->u.disassoc.reason_code));
  1067. hapd->assoc_ap_state = AUTHENTICATE;
  1068. ieee802_11_sta_authenticate(hapd, NULL);
  1069. eloop_register_timeout(0, 500000, ieee802_11_sta_authenticate,
  1070. hapd, NULL);
  1071. return;
  1072. }
  1073. sta = ap_get_sta(hapd, mgmt->sa);
  1074. if (sta == NULL) {
  1075. printf("Station " MACSTR " trying to disassociate, but it "
  1076. "is not associated.\n", MAC2STR(mgmt->sa));
  1077. return;
  1078. }
  1079. sta->flags &= ~WLAN_STA_ASSOC;
  1080. wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
  1081. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1082. HOSTAPD_LEVEL_INFO, "disassociated");
  1083. sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
  1084. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  1085. /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
  1086. * authenticated. */
  1087. accounting_sta_stop(hapd, sta);
  1088. ieee802_1x_free_station(sta);
  1089. hostapd_sta_remove(hapd, sta->addr);
  1090. if (sta->timeout_next == STA_NULLFUNC ||
  1091. sta->timeout_next == STA_DISASSOC) {
  1092. sta->timeout_next = STA_DEAUTH;
  1093. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  1094. eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
  1095. hapd, sta);
  1096. }
  1097. mlme_disassociate_indication(
  1098. hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
  1099. }
  1100. static void handle_deauth(struct hostapd_data *hapd,
  1101. struct ieee80211_mgmt *mgmt, size_t len)
  1102. {
  1103. struct sta_info *sta;
  1104. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
  1105. printf("handle_deauth - too short payload (len=%lu)\n",
  1106. (unsigned long) len);
  1107. return;
  1108. }
  1109. wpa_printf(MSG_DEBUG, "deauthentication: STA=" MACSTR
  1110. " reason_code=%d",
  1111. MAC2STR(mgmt->sa),
  1112. le_to_host16(mgmt->u.deauth.reason_code));
  1113. if (hapd->assoc_ap_state != DO_NOT_ASSOC &&
  1114. os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0) {
  1115. printf("Assoc AP " MACSTR " sent deauthentication "
  1116. "(reason_code=%d) - try to authenticate\n",
  1117. MAC2STR(hapd->conf->assoc_ap_addr),
  1118. le_to_host16(mgmt->u.deauth.reason_code));
  1119. hapd->assoc_ap_state = AUTHENTICATE;
  1120. eloop_register_timeout(0, 500000, ieee802_11_sta_authenticate,
  1121. hapd, NULL);
  1122. return;
  1123. }
  1124. sta = ap_get_sta(hapd, mgmt->sa);
  1125. if (sta == NULL) {
  1126. printf("Station " MACSTR " trying to deauthenticate, but it "
  1127. "is not authenticated.\n", MAC2STR(mgmt->sa));
  1128. return;
  1129. }
  1130. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
  1131. wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
  1132. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1133. HOSTAPD_LEVEL_DEBUG, "deauthenticated");
  1134. mlme_deauthenticate_indication(
  1135. hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
  1136. sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
  1137. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  1138. ap_free_sta(hapd, sta);
  1139. }
  1140. static void handle_beacon(struct hostapd_data *hapd,
  1141. struct ieee80211_mgmt *mgmt, size_t len,
  1142. struct hostapd_frame_info *fi)
  1143. {
  1144. struct ieee802_11_elems elems;
  1145. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
  1146. printf("handle_beacon - too short payload (len=%lu)\n",
  1147. (unsigned long) len);
  1148. return;
  1149. }
  1150. (void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
  1151. len - (IEEE80211_HDRLEN +
  1152. sizeof(mgmt->u.beacon)), &elems,
  1153. 0);
  1154. if (hapd->assoc_ap_state == WAIT_BEACON &&
  1155. os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0) {
  1156. if (elems.ssid && elems.ssid_len <= 32) {
  1157. os_memcpy(hapd->assoc_ap_ssid, elems.ssid,
  1158. elems.ssid_len);
  1159. hapd->assoc_ap_ssid[elems.ssid_len] = '\0';
  1160. hapd->assoc_ap_ssid_len = elems.ssid_len;
  1161. }
  1162. ieee802_11_sta_authenticate(hapd, NULL);
  1163. }
  1164. ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
  1165. }
  1166. #ifdef CONFIG_IEEE80211W
  1167. static void hostapd_ping_action(struct hostapd_data *hapd,
  1168. struct ieee80211_mgmt *mgmt, size_t len)
  1169. {
  1170. struct sta_info *sta;
  1171. u8 *end;
  1172. int i;
  1173. end = mgmt->u.action.u.ping_resp.trans_id + WLAN_PING_TRANS_ID_LEN;
  1174. if (((u8 *) mgmt) + len < end) {
  1175. wpa_printf(MSG_DEBUG, "IEEE 802.11: Too short Ping Action "
  1176. "frame (len=%lu)", (unsigned long) len);
  1177. return;
  1178. }
  1179. if (mgmt->u.action.u.ping_resp.action != WLAN_PING_RESPONSE) {
  1180. wpa_printf(MSG_DEBUG, "IEEE 802.11: Unexpected Ping Action %d",
  1181. mgmt->u.action.u.ping_resp.action);
  1182. return;
  1183. }
  1184. /* MLME-PING.confirm */
  1185. sta = ap_get_sta(hapd, mgmt->sa);
  1186. if (sta == NULL || sta->ping_trans_id == NULL) {
  1187. wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching STA with "
  1188. "pending ping request found");
  1189. return;
  1190. }
  1191. for (i = 0; i < sta->ping_count; i++) {
  1192. if (os_memcmp(sta->ping_trans_id + i * WLAN_PING_TRANS_ID_LEN,
  1193. mgmt->u.action.u.ping_resp.trans_id,
  1194. WLAN_PING_TRANS_ID_LEN) == 0)
  1195. break;
  1196. }
  1197. if (i >= sta->ping_count) {
  1198. wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching ping "
  1199. "transaction identifier found");
  1200. return;
  1201. }
  1202. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1203. HOSTAPD_LEVEL_DEBUG, "Reply to pending ping received");
  1204. ap_sta_stop_ping(hapd, sta);
  1205. }
  1206. #endif /* CONFIG_IEEE80211W */
  1207. static void handle_action(struct hostapd_data *hapd,
  1208. struct ieee80211_mgmt *mgmt, size_t len)
  1209. {
  1210. if (len < IEEE80211_HDRLEN + 1) {
  1211. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1212. HOSTAPD_LEVEL_DEBUG,
  1213. "handle_action - too short payload (len=%lu)",
  1214. (unsigned long) len);
  1215. return;
  1216. }
  1217. switch (mgmt->u.action.category) {
  1218. #ifdef CONFIG_IEEE80211R
  1219. case WLAN_ACTION_FT:
  1220. {
  1221. struct sta_info *sta;
  1222. sta = ap_get_sta(hapd, mgmt->sa);
  1223. if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
  1224. wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored FT Action "
  1225. "frame from unassociated STA " MACSTR,
  1226. MAC2STR(mgmt->sa));
  1227. return;
  1228. }
  1229. if (wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
  1230. len - IEEE80211_HDRLEN))
  1231. break;
  1232. return;
  1233. }
  1234. #endif /* CONFIG_IEEE80211R */
  1235. case WLAN_ACTION_WMM:
  1236. hostapd_wme_action(hapd, mgmt, len);
  1237. return;
  1238. #ifdef CONFIG_IEEE80211W
  1239. case WLAN_ACTION_PING:
  1240. hostapd_ping_action(hapd, mgmt, len);
  1241. return;
  1242. #endif /* CONFIG_IEEE80211W */
  1243. }
  1244. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1245. HOSTAPD_LEVEL_DEBUG,
  1246. "handle_action - unknown action category %d or invalid "
  1247. "frame",
  1248. mgmt->u.action.category);
  1249. if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
  1250. !(mgmt->sa[0] & 0x01)) {
  1251. /*
  1252. * IEEE 802.11-REVma/D9.0 - 7.3.1.11
  1253. * Return the Action frame to the source without change
  1254. * except that MSB of the Category set to 1.
  1255. */
  1256. wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
  1257. "frame back to sender");
  1258. os_memcpy(mgmt->da, mgmt->sa, ETH_ALEN);
  1259. os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
  1260. os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
  1261. mgmt->u.action.category |= 0x80;
  1262. hostapd_send_mgmt_frame(hapd, mgmt, len, 0);
  1263. }
  1264. }
  1265. /**
  1266. * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
  1267. * @hapd: hostapd BSS data structure (the BSS to which the management frame was
  1268. * sent to)
  1269. * @buf: management frame data (starting from IEEE 802.11 header)
  1270. * @len: length of frame data in octets
  1271. * @stype: management frame subtype from frame control field
  1272. *
  1273. * Process all incoming IEEE 802.11 management frames. This will be called for
  1274. * each frame received from the kernel driver through wlan#ap interface. In
  1275. * addition, it can be called to re-inserted pending frames (e.g., when using
  1276. * external RADIUS server as an MAC ACL).
  1277. */
  1278. void ieee802_11_mgmt(struct hostapd_data *hapd, u8 *buf, size_t len, u16 stype,
  1279. struct hostapd_frame_info *fi)
  1280. {
  1281. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
  1282. int broadcast;
  1283. if (stype == WLAN_FC_STYPE_BEACON) {
  1284. handle_beacon(hapd, mgmt, len, fi);
  1285. return;
  1286. }
  1287. if (fi && fi->passive_scan)
  1288. return;
  1289. broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
  1290. mgmt->bssid[2] == 0xff && mgmt->bssid[3] == 0xff &&
  1291. mgmt->bssid[4] == 0xff && mgmt->bssid[5] == 0xff;
  1292. if (!broadcast &&
  1293. os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0 &&
  1294. (hapd->assoc_ap_state == DO_NOT_ASSOC ||
  1295. os_memcmp(mgmt->bssid, hapd->conf->assoc_ap_addr, ETH_ALEN) != 0))
  1296. {
  1297. printf("MGMT: BSSID=" MACSTR " not our address\n",
  1298. MAC2STR(mgmt->bssid));
  1299. return;
  1300. }
  1301. if (stype == WLAN_FC_STYPE_PROBE_REQ) {
  1302. handle_probe_req(hapd, mgmt, len);
  1303. return;
  1304. }
  1305. if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
  1306. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1307. HOSTAPD_LEVEL_DEBUG,
  1308. "MGMT: DA=" MACSTR " not our address",
  1309. MAC2STR(mgmt->da));
  1310. return;
  1311. }
  1312. switch (stype) {
  1313. case WLAN_FC_STYPE_AUTH:
  1314. wpa_printf(MSG_DEBUG, "mgmt::auth");
  1315. handle_auth(hapd, mgmt, len);
  1316. break;
  1317. case WLAN_FC_STYPE_ASSOC_REQ:
  1318. wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
  1319. handle_assoc(hapd, mgmt, len, 0);
  1320. break;
  1321. case WLAN_FC_STYPE_ASSOC_RESP:
  1322. wpa_printf(MSG_DEBUG, "mgmt::assoc_resp");
  1323. handle_assoc_resp(hapd, mgmt, len);
  1324. break;
  1325. case WLAN_FC_STYPE_REASSOC_REQ:
  1326. wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
  1327. handle_assoc(hapd, mgmt, len, 1);
  1328. break;
  1329. case WLAN_FC_STYPE_DISASSOC:
  1330. wpa_printf(MSG_DEBUG, "mgmt::disassoc");
  1331. handle_disassoc(hapd, mgmt, len);
  1332. break;
  1333. case WLAN_FC_STYPE_DEAUTH:
  1334. wpa_printf(MSG_DEBUG, "mgmt::deauth");
  1335. handle_deauth(hapd, mgmt, len);
  1336. break;
  1337. case WLAN_FC_STYPE_ACTION:
  1338. wpa_printf(MSG_DEBUG, "mgmt::action");
  1339. handle_action(hapd, mgmt, len);
  1340. break;
  1341. default:
  1342. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1343. HOSTAPD_LEVEL_DEBUG,
  1344. "unknown mgmt frame subtype %d", stype);
  1345. break;
  1346. }
  1347. }
  1348. static void handle_auth_cb(struct hostapd_data *hapd,
  1349. struct ieee80211_mgmt *mgmt,
  1350. size_t len, int ok)
  1351. {
  1352. u16 auth_alg, auth_transaction, status_code;
  1353. struct sta_info *sta;
  1354. if (!ok) {
  1355. hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
  1356. HOSTAPD_LEVEL_NOTICE,
  1357. "did not acknowledge authentication response");
  1358. return;
  1359. }
  1360. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
  1361. printf("handle_auth_cb - too short payload (len=%lu)\n",
  1362. (unsigned long) len);
  1363. return;
  1364. }
  1365. auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
  1366. auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
  1367. status_code = le_to_host16(mgmt->u.auth.status_code);
  1368. sta = ap_get_sta(hapd, mgmt->da);
  1369. if (!sta) {
  1370. printf("handle_auth_cb: STA " MACSTR " not found\n",
  1371. MAC2STR(mgmt->da));
  1372. return;
  1373. }
  1374. if (status_code == WLAN_STATUS_SUCCESS &&
  1375. ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
  1376. (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
  1377. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1378. HOSTAPD_LEVEL_INFO, "authenticated");
  1379. sta->flags |= WLAN_STA_AUTH;
  1380. }
  1381. }
  1382. static void handle_assoc_cb(struct hostapd_data *hapd,
  1383. struct ieee80211_mgmt *mgmt,
  1384. size_t len, int reassoc, int ok)
  1385. {
  1386. u16 status;
  1387. struct sta_info *sta;
  1388. int new_assoc = 1;
  1389. struct ht_cap_ie *ht_cap = NULL;
  1390. if (!ok) {
  1391. hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
  1392. HOSTAPD_LEVEL_DEBUG,
  1393. "did not acknowledge association response");
  1394. return;
  1395. }
  1396. if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
  1397. sizeof(mgmt->u.assoc_resp))) {
  1398. printf("handle_assoc_cb(reassoc=%d) - too short payload "
  1399. "(len=%lu)\n", reassoc, (unsigned long) len);
  1400. return;
  1401. }
  1402. if (reassoc)
  1403. status = le_to_host16(mgmt->u.reassoc_resp.status_code);
  1404. else
  1405. status = le_to_host16(mgmt->u.assoc_resp.status_code);
  1406. sta = ap_get_sta(hapd, mgmt->da);
  1407. if (!sta) {
  1408. printf("handle_assoc_cb: STA " MACSTR " not found\n",
  1409. MAC2STR(mgmt->da));
  1410. return;
  1411. }
  1412. if (status != WLAN_STATUS_SUCCESS)
  1413. goto fail;
  1414. /* Stop previous accounting session, if one is started, and allocate
  1415. * new session id for the new session. */
  1416. accounting_sta_stop(hapd, sta);
  1417. accounting_sta_get_id(hapd, sta);
  1418. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1419. HOSTAPD_LEVEL_INFO,
  1420. "associated (aid %d, accounting session %08X-%08X)",
  1421. sta->aid, sta->acct_session_id_hi,
  1422. sta->acct_session_id_lo);
  1423. if (sta->flags & WLAN_STA_ASSOC)
  1424. new_assoc = 0;
  1425. sta->flags |= WLAN_STA_ASSOC;
  1426. if (reassoc)
  1427. mlme_reassociate_indication(hapd, sta);
  1428. else
  1429. mlme_associate_indication(hapd, sta);
  1430. #ifdef CONFIG_IEEE80211N
  1431. if (sta->flags & WLAN_STA_HT)
  1432. ht_cap = &sta->ht_capabilities;
  1433. #endif /* CONFIG_IEEE80211N */
  1434. #ifdef CONFIG_IEEE80211W
  1435. sta->ping_timed_out = 0;
  1436. #endif /* CONFIG_IEEE80211W */
  1437. if (hostapd_sta_add(hapd->conf->iface, hapd, sta->addr, sta->aid,
  1438. sta->capability, sta->supported_rates,
  1439. sta->supported_rates_len, 0, sta->listen_interval,
  1440. ht_cap))
  1441. {
  1442. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1443. HOSTAPD_LEVEL_NOTICE,
  1444. "Could not add STA to kernel driver");
  1445. }
  1446. if (sta->eapol_sm == NULL) {
  1447. /*
  1448. * This STA does not use RADIUS server for EAP authentication,
  1449. * so bind it to the selected VLAN interface now, since the
  1450. * interface selection is not going to change anymore.
  1451. */
  1452. ap_sta_bind_vlan(hapd, sta, 0);
  1453. } else if (sta->vlan_id) {
  1454. /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
  1455. ap_sta_bind_vlan(hapd, sta, 0);
  1456. }
  1457. if (sta->flags & WLAN_STA_SHORT_PREAMBLE) {
  1458. hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
  1459. WLAN_STA_SHORT_PREAMBLE, ~0);
  1460. } else {
  1461. hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
  1462. 0, ~WLAN_STA_SHORT_PREAMBLE);
  1463. }
  1464. if (sta->auth_alg == WLAN_AUTH_FT)
  1465. wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
  1466. else
  1467. wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
  1468. hostapd_new_assoc_sta(hapd, sta, !new_assoc);
  1469. ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
  1470. fail:
  1471. /* Copy of the association request is not needed anymore */
  1472. if (sta->last_assoc_req) {
  1473. os_free(sta->last_assoc_req);
  1474. sta->last_assoc_req = NULL;
  1475. }
  1476. }
  1477. void ieee802_11_mgmt_cb(struct hostapd_data *hapd, u8 *buf, size_t len,
  1478. u16 stype, int ok)
  1479. {
  1480. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
  1481. switch (stype) {
  1482. case WLAN_FC_STYPE_AUTH:
  1483. wpa_printf(MSG_DEBUG, "mgmt::auth cb");
  1484. handle_auth_cb(hapd, mgmt, len, ok);
  1485. break;
  1486. case WLAN_FC_STYPE_ASSOC_RESP:
  1487. wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
  1488. handle_assoc_cb(hapd, mgmt, len, 0, ok);
  1489. break;
  1490. case WLAN_FC_STYPE_REASSOC_RESP:
  1491. wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
  1492. handle_assoc_cb(hapd, mgmt, len, 1, ok);
  1493. break;
  1494. case WLAN_FC_STYPE_PROBE_RESP:
  1495. wpa_printf(MSG_DEBUG, "mgmt::proberesp cb");
  1496. break;
  1497. case WLAN_FC_STYPE_DEAUTH:
  1498. /* ignore */
  1499. break;
  1500. case WLAN_FC_STYPE_ACTION:
  1501. wpa_printf(MSG_DEBUG, "mgmt::action cb");
  1502. break;
  1503. default:
  1504. printf("unknown mgmt cb frame subtype %d\n", stype);
  1505. break;
  1506. }
  1507. }
  1508. static void ieee80211_tkip_countermeasures_stop(void *eloop_ctx,
  1509. void *timeout_ctx)
  1510. {
  1511. struct hostapd_data *hapd = eloop_ctx;
  1512. hapd->tkip_countermeasures = 0;
  1513. hostapd_set_countermeasures(hapd, 0);
  1514. hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
  1515. HOSTAPD_LEVEL_INFO, "TKIP countermeasures ended");
  1516. }
  1517. static void ieee80211_tkip_countermeasures_start(struct hostapd_data *hapd)
  1518. {
  1519. struct sta_info *sta;
  1520. hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
  1521. HOSTAPD_LEVEL_INFO, "TKIP countermeasures initiated");
  1522. wpa_auth_countermeasures_start(hapd->wpa_auth);
  1523. hapd->tkip_countermeasures = 1;
  1524. hostapd_set_countermeasures(hapd, 1);
  1525. wpa_gtk_rekey(hapd->wpa_auth);
  1526. eloop_cancel_timeout(ieee80211_tkip_countermeasures_stop, hapd, NULL);
  1527. eloop_register_timeout(60, 0, ieee80211_tkip_countermeasures_stop,
  1528. hapd, NULL);
  1529. for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
  1530. hostapd_sta_deauth(hapd, sta->addr,
  1531. WLAN_REASON_MICHAEL_MIC_FAILURE);
  1532. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
  1533. WLAN_STA_AUTHORIZED);
  1534. hostapd_sta_remove(hapd, sta->addr);
  1535. }
  1536. }
  1537. void ieee80211_michael_mic_failure(struct hostapd_data *hapd, const u8 *addr,
  1538. int local)
  1539. {
  1540. time_t now;
  1541. if (addr && local) {
  1542. struct sta_info *sta = ap_get_sta(hapd, addr);
  1543. if (sta != NULL) {
  1544. wpa_auth_sta_local_mic_failure_report(sta->wpa_sm);
  1545. hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
  1546. HOSTAPD_LEVEL_INFO,
  1547. "Michael MIC failure detected in "
  1548. "received frame");
  1549. mlme_michaelmicfailure_indication(hapd, addr);
  1550. } else {
  1551. wpa_printf(MSG_DEBUG,
  1552. "MLME-MICHAELMICFAILURE.indication "
  1553. "for not associated STA (" MACSTR
  1554. ") ignored", MAC2STR(addr));
  1555. return;
  1556. }
  1557. }
  1558. time(&now);
  1559. if (now > hapd->michael_mic_failure + 60) {
  1560. hapd->michael_mic_failures = 1;
  1561. } else {
  1562. hapd->michael_mic_failures++;
  1563. if (hapd->michael_mic_failures > 1)
  1564. ieee80211_tkip_countermeasures_start(hapd);
  1565. }
  1566. hapd->michael_mic_failure = now;
  1567. }
  1568. int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
  1569. {
  1570. /* TODO */
  1571. return 0;
  1572. }
  1573. int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
  1574. char *buf, size_t buflen)
  1575. {
  1576. /* TODO */
  1577. return 0;
  1578. }
  1579. #endif /* CONFIG_NATIVE_WINDOWS */