ieee802_11.c 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536
  1. /*
  2. * hostapd / IEEE 802.11 Management
  3. * Copyright (c) 2002-2014, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #ifndef CONFIG_NATIVE_WINDOWS
  10. #include "utils/common.h"
  11. #include "utils/eloop.h"
  12. #include "crypto/crypto.h"
  13. #include "crypto/sha256.h"
  14. #include "crypto/random.h"
  15. #include "common/ieee802_11_defs.h"
  16. #include "common/ieee802_11_common.h"
  17. #include "common/wpa_ctrl.h"
  18. #include "common/sae.h"
  19. #include "radius/radius.h"
  20. #include "radius/radius_client.h"
  21. #include "p2p/p2p.h"
  22. #include "wps/wps.h"
  23. #include "fst/fst.h"
  24. #include "hostapd.h"
  25. #include "beacon.h"
  26. #include "ieee802_11_auth.h"
  27. #include "sta_info.h"
  28. #include "ieee802_1x.h"
  29. #include "wpa_auth.h"
  30. #include "pmksa_cache_auth.h"
  31. #include "wmm.h"
  32. #include "ap_list.h"
  33. #include "accounting.h"
  34. #include "ap_config.h"
  35. #include "ap_mlme.h"
  36. #include "p2p_hostapd.h"
  37. #include "ap_drv_ops.h"
  38. #include "wnm_ap.h"
  39. #include "hw_features.h"
  40. #include "ieee802_11.h"
  41. #include "dfs.h"
  42. #include "mbo_ap.h"
  43. #include "rrm.h"
  44. #include "taxonomy.h"
  45. u8 * hostapd_eid_supp_rates(struct hostapd_data *hapd, u8 *eid)
  46. {
  47. u8 *pos = eid;
  48. int i, num, count;
  49. if (hapd->iface->current_rates == NULL)
  50. return eid;
  51. *pos++ = WLAN_EID_SUPP_RATES;
  52. num = hapd->iface->num_rates;
  53. if (hapd->iconf->ieee80211n && hapd->iconf->require_ht)
  54. num++;
  55. if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht)
  56. num++;
  57. if (num > 8) {
  58. /* rest of the rates are encoded in Extended supported
  59. * rates element */
  60. num = 8;
  61. }
  62. *pos++ = num;
  63. for (i = 0, count = 0; i < hapd->iface->num_rates && count < num;
  64. i++) {
  65. count++;
  66. *pos = hapd->iface->current_rates[i].rate / 5;
  67. if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
  68. *pos |= 0x80;
  69. pos++;
  70. }
  71. if (hapd->iconf->ieee80211n && hapd->iconf->require_ht && count < 8) {
  72. count++;
  73. *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY;
  74. }
  75. if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht && count < 8) {
  76. count++;
  77. *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY;
  78. }
  79. return pos;
  80. }
  81. u8 * hostapd_eid_ext_supp_rates(struct hostapd_data *hapd, u8 *eid)
  82. {
  83. u8 *pos = eid;
  84. int i, num, count;
  85. if (hapd->iface->current_rates == NULL)
  86. return eid;
  87. num = hapd->iface->num_rates;
  88. if (hapd->iconf->ieee80211n && hapd->iconf->require_ht)
  89. num++;
  90. if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht)
  91. num++;
  92. if (num <= 8)
  93. return eid;
  94. num -= 8;
  95. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  96. *pos++ = num;
  97. for (i = 0, count = 0; i < hapd->iface->num_rates && count < num + 8;
  98. i++) {
  99. count++;
  100. if (count <= 8)
  101. continue; /* already in SuppRates IE */
  102. *pos = hapd->iface->current_rates[i].rate / 5;
  103. if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
  104. *pos |= 0x80;
  105. pos++;
  106. }
  107. if (hapd->iconf->ieee80211n && hapd->iconf->require_ht) {
  108. count++;
  109. if (count > 8)
  110. *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY;
  111. }
  112. if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht) {
  113. count++;
  114. if (count > 8)
  115. *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY;
  116. }
  117. return pos;
  118. }
  119. u16 hostapd_own_capab_info(struct hostapd_data *hapd)
  120. {
  121. int capab = WLAN_CAPABILITY_ESS;
  122. int privacy;
  123. int dfs;
  124. int i;
  125. /* Check if any of configured channels require DFS */
  126. dfs = hostapd_is_dfs_required(hapd->iface);
  127. if (dfs < 0) {
  128. wpa_printf(MSG_WARNING, "Failed to check if DFS is required; ret=%d",
  129. dfs);
  130. dfs = 0;
  131. }
  132. if (hapd->iface->num_sta_no_short_preamble == 0 &&
  133. hapd->iconf->preamble == SHORT_PREAMBLE)
  134. capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
  135. privacy = hapd->conf->ssid.wep.keys_set;
  136. if (hapd->conf->ieee802_1x &&
  137. (hapd->conf->default_wep_key_len ||
  138. hapd->conf->individual_wep_key_len))
  139. privacy = 1;
  140. if (hapd->conf->wpa)
  141. privacy = 1;
  142. #ifdef CONFIG_HS20
  143. if (hapd->conf->osen)
  144. privacy = 1;
  145. #endif /* CONFIG_HS20 */
  146. if (privacy)
  147. capab |= WLAN_CAPABILITY_PRIVACY;
  148. if (hapd->iface->current_mode &&
  149. hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
  150. hapd->iface->num_sta_no_short_slot_time == 0)
  151. capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
  152. /*
  153. * Currently, Spectrum Management capability bit is set when directly
  154. * requested in configuration by spectrum_mgmt_required or when AP is
  155. * running on DFS channel.
  156. * TODO: Also consider driver support for TPC to set Spectrum Mgmt bit
  157. */
  158. if (hapd->iface->current_mode &&
  159. hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A &&
  160. (hapd->iconf->spectrum_mgmt_required || dfs))
  161. capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
  162. for (i = 0; i < RRM_CAPABILITIES_IE_LEN; i++) {
  163. if (hapd->conf->radio_measurements[i]) {
  164. capab |= IEEE80211_CAP_RRM;
  165. break;
  166. }
  167. }
  168. return capab;
  169. }
  170. #ifndef CONFIG_NO_RC4
  171. static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
  172. u16 auth_transaction, const u8 *challenge,
  173. int iswep)
  174. {
  175. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  176. HOSTAPD_LEVEL_DEBUG,
  177. "authentication (shared key, transaction %d)",
  178. auth_transaction);
  179. if (auth_transaction == 1) {
  180. if (!sta->challenge) {
  181. /* Generate a pseudo-random challenge */
  182. u8 key[8];
  183. sta->challenge = os_zalloc(WLAN_AUTH_CHALLENGE_LEN);
  184. if (sta->challenge == NULL)
  185. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  186. if (os_get_random(key, sizeof(key)) < 0) {
  187. os_free(sta->challenge);
  188. sta->challenge = NULL;
  189. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  190. }
  191. rc4_skip(key, sizeof(key), 0,
  192. sta->challenge, WLAN_AUTH_CHALLENGE_LEN);
  193. }
  194. return 0;
  195. }
  196. if (auth_transaction != 3)
  197. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  198. /* Transaction 3 */
  199. if (!iswep || !sta->challenge || !challenge ||
  200. os_memcmp_const(sta->challenge, challenge,
  201. WLAN_AUTH_CHALLENGE_LEN)) {
  202. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  203. HOSTAPD_LEVEL_INFO,
  204. "shared key authentication - invalid "
  205. "challenge-response");
  206. return WLAN_STATUS_CHALLENGE_FAIL;
  207. }
  208. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  209. HOSTAPD_LEVEL_DEBUG,
  210. "authentication OK (shared key)");
  211. sta->flags |= WLAN_STA_AUTH;
  212. wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
  213. os_free(sta->challenge);
  214. sta->challenge = NULL;
  215. return 0;
  216. }
  217. #endif /* CONFIG_NO_RC4 */
  218. static int send_auth_reply(struct hostapd_data *hapd,
  219. const u8 *dst, const u8 *bssid,
  220. u16 auth_alg, u16 auth_transaction, u16 resp,
  221. const u8 *ies, size_t ies_len)
  222. {
  223. struct ieee80211_mgmt *reply;
  224. u8 *buf;
  225. size_t rlen;
  226. int reply_res = WLAN_STATUS_UNSPECIFIED_FAILURE;
  227. rlen = IEEE80211_HDRLEN + sizeof(reply->u.auth) + ies_len;
  228. buf = os_zalloc(rlen);
  229. if (buf == NULL)
  230. return -1;
  231. reply = (struct ieee80211_mgmt *) buf;
  232. reply->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  233. WLAN_FC_STYPE_AUTH);
  234. os_memcpy(reply->da, dst, ETH_ALEN);
  235. os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
  236. os_memcpy(reply->bssid, bssid, ETH_ALEN);
  237. reply->u.auth.auth_alg = host_to_le16(auth_alg);
  238. reply->u.auth.auth_transaction = host_to_le16(auth_transaction);
  239. reply->u.auth.status_code = host_to_le16(resp);
  240. if (ies && ies_len)
  241. os_memcpy(reply->u.auth.variable, ies, ies_len);
  242. wpa_printf(MSG_DEBUG, "authentication reply: STA=" MACSTR
  243. " auth_alg=%d auth_transaction=%d resp=%d (IE len=%lu)",
  244. MAC2STR(dst), auth_alg, auth_transaction,
  245. resp, (unsigned long) ies_len);
  246. if (hostapd_drv_send_mlme(hapd, reply, rlen, 0) < 0)
  247. wpa_printf(MSG_INFO, "send_auth_reply: send failed");
  248. else
  249. reply_res = WLAN_STATUS_SUCCESS;
  250. os_free(buf);
  251. return reply_res;
  252. }
  253. #ifdef CONFIG_IEEE80211R
  254. static void handle_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid,
  255. u16 auth_transaction, u16 status,
  256. const u8 *ies, size_t ies_len)
  257. {
  258. struct hostapd_data *hapd = ctx;
  259. struct sta_info *sta;
  260. int reply_res;
  261. reply_res = send_auth_reply(hapd, dst, bssid, WLAN_AUTH_FT,
  262. auth_transaction, status, ies, ies_len);
  263. sta = ap_get_sta(hapd, dst);
  264. if (sta == NULL)
  265. return;
  266. if (sta->added_unassoc && (reply_res != WLAN_STATUS_SUCCESS ||
  267. status != WLAN_STATUS_SUCCESS)) {
  268. hostapd_drv_sta_remove(hapd, sta->addr);
  269. sta->added_unassoc = 0;
  270. return;
  271. }
  272. if (status != WLAN_STATUS_SUCCESS)
  273. return;
  274. hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
  275. HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
  276. sta->flags |= WLAN_STA_AUTH;
  277. mlme_authenticate_indication(hapd, sta);
  278. }
  279. #endif /* CONFIG_IEEE80211R */
  280. #ifdef CONFIG_SAE
  281. #define dot11RSNASAESync 5 /* attempts */
  282. static struct wpabuf * auth_build_sae_commit(struct hostapd_data *hapd,
  283. struct sta_info *sta, int update)
  284. {
  285. struct wpabuf *buf;
  286. if (hapd->conf->ssid.wpa_passphrase == NULL) {
  287. wpa_printf(MSG_DEBUG, "SAE: No password available");
  288. return NULL;
  289. }
  290. if (update &&
  291. sae_prepare_commit(hapd->own_addr, sta->addr,
  292. (u8 *) hapd->conf->ssid.wpa_passphrase,
  293. os_strlen(hapd->conf->ssid.wpa_passphrase),
  294. sta->sae) < 0) {
  295. wpa_printf(MSG_DEBUG, "SAE: Could not pick PWE");
  296. return NULL;
  297. }
  298. buf = wpabuf_alloc(SAE_COMMIT_MAX_LEN);
  299. if (buf == NULL)
  300. return NULL;
  301. sae_write_commit(sta->sae, buf, sta->sae->tmp ?
  302. sta->sae->tmp->anti_clogging_token : NULL);
  303. return buf;
  304. }
  305. static struct wpabuf * auth_build_sae_confirm(struct hostapd_data *hapd,
  306. struct sta_info *sta)
  307. {
  308. struct wpabuf *buf;
  309. buf = wpabuf_alloc(SAE_CONFIRM_MAX_LEN);
  310. if (buf == NULL)
  311. return NULL;
  312. sae_write_confirm(sta->sae, buf);
  313. return buf;
  314. }
  315. static int auth_sae_send_commit(struct hostapd_data *hapd,
  316. struct sta_info *sta,
  317. const u8 *bssid, int update)
  318. {
  319. struct wpabuf *data;
  320. int reply_res;
  321. data = auth_build_sae_commit(hapd, sta, update);
  322. if (data == NULL)
  323. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  324. reply_res = send_auth_reply(hapd, sta->addr, bssid, WLAN_AUTH_SAE, 1,
  325. WLAN_STATUS_SUCCESS, wpabuf_head(data),
  326. wpabuf_len(data));
  327. wpabuf_free(data);
  328. return reply_res;
  329. }
  330. static int auth_sae_send_confirm(struct hostapd_data *hapd,
  331. struct sta_info *sta,
  332. const u8 *bssid)
  333. {
  334. struct wpabuf *data;
  335. int reply_res;
  336. data = auth_build_sae_confirm(hapd, sta);
  337. if (data == NULL)
  338. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  339. reply_res = send_auth_reply(hapd, sta->addr, bssid, WLAN_AUTH_SAE, 2,
  340. WLAN_STATUS_SUCCESS, wpabuf_head(data),
  341. wpabuf_len(data));
  342. wpabuf_free(data);
  343. return reply_res;
  344. }
  345. static int use_sae_anti_clogging(struct hostapd_data *hapd)
  346. {
  347. struct sta_info *sta;
  348. unsigned int open = 0;
  349. if (hapd->conf->sae_anti_clogging_threshold == 0)
  350. return 1;
  351. for (sta = hapd->sta_list; sta; sta = sta->next) {
  352. if (!sta->sae)
  353. continue;
  354. if (sta->sae->state != SAE_COMMITTED &&
  355. sta->sae->state != SAE_CONFIRMED)
  356. continue;
  357. open++;
  358. if (open >= hapd->conf->sae_anti_clogging_threshold)
  359. return 1;
  360. }
  361. return 0;
  362. }
  363. static int check_sae_token(struct hostapd_data *hapd, const u8 *addr,
  364. const u8 *token, size_t token_len)
  365. {
  366. u8 mac[SHA256_MAC_LEN];
  367. if (token_len != SHA256_MAC_LEN)
  368. return -1;
  369. if (hmac_sha256(hapd->sae_token_key, sizeof(hapd->sae_token_key),
  370. addr, ETH_ALEN, mac) < 0 ||
  371. os_memcmp_const(token, mac, SHA256_MAC_LEN) != 0)
  372. return -1;
  373. return 0;
  374. }
  375. static struct wpabuf * auth_build_token_req(struct hostapd_data *hapd,
  376. int group, const u8 *addr)
  377. {
  378. struct wpabuf *buf;
  379. u8 *token;
  380. struct os_reltime now;
  381. os_get_reltime(&now);
  382. if (!os_reltime_initialized(&hapd->last_sae_token_key_update) ||
  383. os_reltime_expired(&now, &hapd->last_sae_token_key_update, 60)) {
  384. if (random_get_bytes(hapd->sae_token_key,
  385. sizeof(hapd->sae_token_key)) < 0)
  386. return NULL;
  387. wpa_hexdump(MSG_DEBUG, "SAE: Updated token key",
  388. hapd->sae_token_key, sizeof(hapd->sae_token_key));
  389. hapd->last_sae_token_key_update = now;
  390. }
  391. buf = wpabuf_alloc(sizeof(le16) + SHA256_MAC_LEN);
  392. if (buf == NULL)
  393. return NULL;
  394. wpabuf_put_le16(buf, group); /* Finite Cyclic Group */
  395. token = wpabuf_put(buf, SHA256_MAC_LEN);
  396. hmac_sha256(hapd->sae_token_key, sizeof(hapd->sae_token_key),
  397. addr, ETH_ALEN, token);
  398. return buf;
  399. }
  400. static int sae_check_big_sync(struct sta_info *sta)
  401. {
  402. if (sta->sae->sync > dot11RSNASAESync) {
  403. sta->sae->state = SAE_NOTHING;
  404. sta->sae->sync = 0;
  405. return -1;
  406. }
  407. return 0;
  408. }
  409. static void auth_sae_retransmit_timer(void *eloop_ctx, void *eloop_data)
  410. {
  411. struct hostapd_data *hapd = eloop_ctx;
  412. struct sta_info *sta = eloop_data;
  413. int ret;
  414. if (sae_check_big_sync(sta))
  415. return;
  416. sta->sae->sync++;
  417. wpa_printf(MSG_DEBUG, "SAE: Auth SAE retransmit timer for " MACSTR
  418. " (sync=%d state=%d)",
  419. MAC2STR(sta->addr), sta->sae->sync, sta->sae->state);
  420. switch (sta->sae->state) {
  421. case SAE_COMMITTED:
  422. ret = auth_sae_send_commit(hapd, sta, hapd->own_addr, 0);
  423. eloop_register_timeout(0,
  424. hapd->dot11RSNASAERetransPeriod * 1000,
  425. auth_sae_retransmit_timer, hapd, sta);
  426. break;
  427. case SAE_CONFIRMED:
  428. ret = auth_sae_send_confirm(hapd, sta, hapd->own_addr);
  429. eloop_register_timeout(0,
  430. hapd->dot11RSNASAERetransPeriod * 1000,
  431. auth_sae_retransmit_timer, hapd, sta);
  432. break;
  433. default:
  434. ret = -1;
  435. break;
  436. }
  437. if (ret != WLAN_STATUS_SUCCESS)
  438. wpa_printf(MSG_INFO, "SAE: Failed to retransmit: ret=%d", ret);
  439. }
  440. void sae_clear_retransmit_timer(struct hostapd_data *hapd, struct sta_info *sta)
  441. {
  442. eloop_cancel_timeout(auth_sae_retransmit_timer, hapd, sta);
  443. }
  444. static void sae_set_retransmit_timer(struct hostapd_data *hapd,
  445. struct sta_info *sta)
  446. {
  447. if (!(hapd->conf->mesh & MESH_ENABLED))
  448. return;
  449. eloop_cancel_timeout(auth_sae_retransmit_timer, hapd, sta);
  450. eloop_register_timeout(0, hapd->dot11RSNASAERetransPeriod * 1000,
  451. auth_sae_retransmit_timer, hapd, sta);
  452. }
  453. void sae_accept_sta(struct hostapd_data *hapd, struct sta_info *sta)
  454. {
  455. sta->flags |= WLAN_STA_AUTH;
  456. sta->auth_alg = WLAN_AUTH_SAE;
  457. mlme_authenticate_indication(hapd, sta);
  458. wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
  459. sta->sae->state = SAE_ACCEPTED;
  460. wpa_auth_pmksa_add_sae(hapd->wpa_auth, sta->addr,
  461. sta->sae->pmk, sta->sae->pmkid);
  462. }
  463. static int sae_sm_step(struct hostapd_data *hapd, struct sta_info *sta,
  464. const u8 *bssid, u8 auth_transaction)
  465. {
  466. int ret;
  467. if (auth_transaction != 1 && auth_transaction != 2)
  468. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  469. switch (sta->sae->state) {
  470. case SAE_NOTHING:
  471. if (auth_transaction == 1) {
  472. ret = auth_sae_send_commit(hapd, sta, bssid, 1);
  473. if (ret)
  474. return ret;
  475. sta->sae->state = SAE_COMMITTED;
  476. if (sae_process_commit(sta->sae) < 0)
  477. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  478. /*
  479. * In mesh case, both Commit and Confirm can be sent
  480. * immediately. In infrastructure BSS, only a single
  481. * Authentication frame (Commit) is expected from the AP
  482. * here and the second one (Confirm) will be sent once
  483. * the STA has sent its second Authentication frame
  484. * (Confirm).
  485. */
  486. if (hapd->conf->mesh & MESH_ENABLED) {
  487. /*
  488. * Send both Commit and Confirm immediately
  489. * based on SAE finite state machine
  490. * Nothing -> Confirm transition.
  491. */
  492. ret = auth_sae_send_confirm(hapd, sta, bssid);
  493. if (ret)
  494. return ret;
  495. sta->sae->state = SAE_CONFIRMED;
  496. } else {
  497. /*
  498. * For infrastructure BSS, send only the Commit
  499. * message now to get alternating sequence of
  500. * Authentication frames between the AP and STA.
  501. * Confirm will be sent in
  502. * Committed -> Confirmed/Accepted transition
  503. * when receiving Confirm from STA.
  504. */
  505. }
  506. sta->sae->sync = 0;
  507. sae_set_retransmit_timer(hapd, sta);
  508. } else {
  509. hostapd_logger(hapd, sta->addr,
  510. HOSTAPD_MODULE_IEEE80211,
  511. HOSTAPD_LEVEL_DEBUG,
  512. "SAE confirm before commit");
  513. }
  514. break;
  515. case SAE_COMMITTED:
  516. sae_clear_retransmit_timer(hapd, sta);
  517. if (auth_transaction == 1) {
  518. if (sae_process_commit(sta->sae) < 0)
  519. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  520. ret = auth_sae_send_confirm(hapd, sta, bssid);
  521. if (ret)
  522. return ret;
  523. sta->sae->state = SAE_CONFIRMED;
  524. sta->sae->sync = 0;
  525. sae_set_retransmit_timer(hapd, sta);
  526. } else if (hapd->conf->mesh & MESH_ENABLED) {
  527. /*
  528. * In mesh case, follow SAE finite state machine and
  529. * send Commit now, if sync count allows.
  530. */
  531. if (sae_check_big_sync(sta))
  532. return WLAN_STATUS_SUCCESS;
  533. sta->sae->sync++;
  534. ret = auth_sae_send_commit(hapd, sta, bssid, 0);
  535. if (ret)
  536. return ret;
  537. sae_set_retransmit_timer(hapd, sta);
  538. } else {
  539. /*
  540. * For instructure BSS, send the postponed Confirm from
  541. * Nothing -> Confirmed transition that was reduced to
  542. * Nothing -> Committed above.
  543. */
  544. ret = auth_sae_send_confirm(hapd, sta, bssid);
  545. if (ret)
  546. return ret;
  547. sta->sae->state = SAE_CONFIRMED;
  548. /*
  549. * Since this was triggered on Confirm RX, run another
  550. * step to get to Accepted without waiting for
  551. * additional events.
  552. */
  553. return sae_sm_step(hapd, sta, bssid, auth_transaction);
  554. }
  555. break;
  556. case SAE_CONFIRMED:
  557. sae_clear_retransmit_timer(hapd, sta);
  558. if (auth_transaction == 1) {
  559. if (sae_check_big_sync(sta))
  560. return WLAN_STATUS_SUCCESS;
  561. sta->sae->sync++;
  562. ret = auth_sae_send_commit(hapd, sta, bssid, 1);
  563. if (ret)
  564. return ret;
  565. if (sae_process_commit(sta->sae) < 0)
  566. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  567. ret = auth_sae_send_confirm(hapd, sta, bssid);
  568. if (ret)
  569. return ret;
  570. sae_set_retransmit_timer(hapd, sta);
  571. } else {
  572. sae_accept_sta(hapd, sta);
  573. }
  574. break;
  575. case SAE_ACCEPTED:
  576. if (auth_transaction == 1) {
  577. wpa_printf(MSG_DEBUG, "SAE: remove the STA (" MACSTR
  578. ") doing reauthentication",
  579. MAC2STR(sta->addr));
  580. ap_free_sta(hapd, sta);
  581. wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
  582. } else {
  583. if (sae_check_big_sync(sta))
  584. return WLAN_STATUS_SUCCESS;
  585. sta->sae->sync++;
  586. ret = auth_sae_send_confirm(hapd, sta, bssid);
  587. sae_clear_temp_data(sta->sae);
  588. if (ret)
  589. return ret;
  590. }
  591. break;
  592. default:
  593. wpa_printf(MSG_ERROR, "SAE: invalid state %d",
  594. sta->sae->state);
  595. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  596. }
  597. return WLAN_STATUS_SUCCESS;
  598. }
  599. static void sae_pick_next_group(struct hostapd_data *hapd, struct sta_info *sta)
  600. {
  601. struct sae_data *sae = sta->sae;
  602. int i, *groups = hapd->conf->sae_groups;
  603. if (sae->state != SAE_COMMITTED)
  604. return;
  605. wpa_printf(MSG_DEBUG, "SAE: Previously selected group: %d", sae->group);
  606. for (i = 0; groups && groups[i] > 0; i++) {
  607. if (sae->group == groups[i])
  608. break;
  609. }
  610. if (!groups || groups[i] <= 0) {
  611. wpa_printf(MSG_DEBUG,
  612. "SAE: Previously selected group not found from the current configuration");
  613. return;
  614. }
  615. for (;;) {
  616. i++;
  617. if (groups[i] <= 0) {
  618. wpa_printf(MSG_DEBUG,
  619. "SAE: No alternative group enabled");
  620. return;
  621. }
  622. if (sae_set_group(sae, groups[i]) < 0)
  623. continue;
  624. break;
  625. }
  626. wpa_printf(MSG_DEBUG, "SAE: Selected new group: %d", groups[i]);
  627. }
  628. static void handle_auth_sae(struct hostapd_data *hapd, struct sta_info *sta,
  629. const struct ieee80211_mgmt *mgmt, size_t len,
  630. u16 auth_transaction, u16 status_code)
  631. {
  632. int resp = WLAN_STATUS_SUCCESS;
  633. struct wpabuf *data = NULL;
  634. if (!sta->sae) {
  635. if (auth_transaction != 1 ||
  636. status_code != WLAN_STATUS_SUCCESS) {
  637. resp = -1;
  638. goto remove_sta;
  639. }
  640. sta->sae = os_zalloc(sizeof(*sta->sae));
  641. if (!sta->sae) {
  642. resp = -1;
  643. goto remove_sta;
  644. }
  645. sta->sae->state = SAE_NOTHING;
  646. sta->sae->sync = 0;
  647. }
  648. if (sta->mesh_sae_pmksa_caching) {
  649. wpa_printf(MSG_DEBUG,
  650. "SAE: Cancel use of mesh PMKSA caching because peer starts SAE authentication");
  651. wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
  652. sta->mesh_sae_pmksa_caching = 0;
  653. }
  654. if (auth_transaction == 1) {
  655. const u8 *token = NULL, *pos, *end;
  656. size_t token_len = 0;
  657. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  658. HOSTAPD_LEVEL_DEBUG,
  659. "start SAE authentication (RX commit, status=%u)",
  660. status_code);
  661. if ((hapd->conf->mesh & MESH_ENABLED) &&
  662. status_code == WLAN_STATUS_ANTI_CLOGGING_TOKEN_REQ &&
  663. sta->sae->tmp) {
  664. pos = mgmt->u.auth.variable;
  665. end = ((const u8 *) mgmt) + len;
  666. if (pos + sizeof(le16) > end) {
  667. wpa_printf(MSG_ERROR,
  668. "SAE: Too short anti-clogging token request");
  669. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  670. goto reply;
  671. }
  672. resp = sae_group_allowed(sta->sae,
  673. hapd->conf->sae_groups,
  674. WPA_GET_LE16(pos));
  675. if (resp != WLAN_STATUS_SUCCESS) {
  676. wpa_printf(MSG_ERROR,
  677. "SAE: Invalid group in anti-clogging token request");
  678. goto reply;
  679. }
  680. pos += sizeof(le16);
  681. wpabuf_free(sta->sae->tmp->anti_clogging_token);
  682. sta->sae->tmp->anti_clogging_token =
  683. wpabuf_alloc_copy(pos, end - pos);
  684. if (sta->sae->tmp->anti_clogging_token == NULL) {
  685. wpa_printf(MSG_ERROR,
  686. "SAE: Failed to alloc for anti-clogging token");
  687. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  688. goto remove_sta;
  689. }
  690. /*
  691. * IEEE Std 802.11-2012, 11.3.8.6.4: If the Status code
  692. * is 76, a new Commit Message shall be constructed
  693. * with the Anti-Clogging Token from the received
  694. * Authentication frame, and the commit-scalar and
  695. * COMMIT-ELEMENT previously sent.
  696. */
  697. resp = auth_sae_send_commit(hapd, sta, mgmt->bssid, 0);
  698. if (resp != WLAN_STATUS_SUCCESS) {
  699. wpa_printf(MSG_ERROR,
  700. "SAE: Failed to send commit message");
  701. goto remove_sta;
  702. }
  703. sta->sae->state = SAE_COMMITTED;
  704. sta->sae->sync = 0;
  705. sae_set_retransmit_timer(hapd, sta);
  706. return;
  707. }
  708. if ((hapd->conf->mesh & MESH_ENABLED) &&
  709. status_code ==
  710. WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED &&
  711. sta->sae->tmp) {
  712. wpa_printf(MSG_DEBUG,
  713. "SAE: Peer did not accept our SAE group");
  714. sae_pick_next_group(hapd, sta);
  715. goto remove_sta;
  716. }
  717. if (status_code != WLAN_STATUS_SUCCESS)
  718. goto remove_sta;
  719. resp = sae_parse_commit(sta->sae, mgmt->u.auth.variable,
  720. ((const u8 *) mgmt) + len -
  721. mgmt->u.auth.variable, &token,
  722. &token_len, hapd->conf->sae_groups);
  723. if (resp == SAE_SILENTLY_DISCARD) {
  724. wpa_printf(MSG_DEBUG,
  725. "SAE: Drop commit message from " MACSTR " due to reflection attack",
  726. MAC2STR(sta->addr));
  727. goto remove_sta;
  728. }
  729. if (token && check_sae_token(hapd, sta->addr, token, token_len)
  730. < 0) {
  731. wpa_printf(MSG_DEBUG, "SAE: Drop commit message with "
  732. "incorrect token from " MACSTR,
  733. MAC2STR(sta->addr));
  734. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  735. goto remove_sta;
  736. }
  737. if (resp != WLAN_STATUS_SUCCESS)
  738. goto reply;
  739. if (!token && use_sae_anti_clogging(hapd)) {
  740. wpa_printf(MSG_DEBUG,
  741. "SAE: Request anti-clogging token from "
  742. MACSTR, MAC2STR(sta->addr));
  743. data = auth_build_token_req(hapd, sta->sae->group,
  744. sta->addr);
  745. resp = WLAN_STATUS_ANTI_CLOGGING_TOKEN_REQ;
  746. if (hapd->conf->mesh & MESH_ENABLED)
  747. sta->sae->state = SAE_NOTHING;
  748. goto reply;
  749. }
  750. resp = sae_sm_step(hapd, sta, mgmt->bssid, auth_transaction);
  751. } else if (auth_transaction == 2) {
  752. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  753. HOSTAPD_LEVEL_DEBUG,
  754. "SAE authentication (RX confirm, status=%u)",
  755. status_code);
  756. if (status_code != WLAN_STATUS_SUCCESS)
  757. goto remove_sta;
  758. if (sta->sae->state >= SAE_CONFIRMED ||
  759. !(hapd->conf->mesh & MESH_ENABLED)) {
  760. if (sae_check_confirm(sta->sae, mgmt->u.auth.variable,
  761. ((u8 *) mgmt) + len -
  762. mgmt->u.auth.variable) < 0) {
  763. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  764. goto reply;
  765. }
  766. }
  767. resp = sae_sm_step(hapd, sta, mgmt->bssid, auth_transaction);
  768. } else {
  769. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  770. HOSTAPD_LEVEL_DEBUG,
  771. "unexpected SAE authentication transaction %u (status=%u)",
  772. auth_transaction, status_code);
  773. if (status_code != WLAN_STATUS_SUCCESS)
  774. goto remove_sta;
  775. resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
  776. }
  777. reply:
  778. if (resp != WLAN_STATUS_SUCCESS) {
  779. send_auth_reply(hapd, mgmt->sa, mgmt->bssid, WLAN_AUTH_SAE,
  780. auth_transaction, resp,
  781. data ? wpabuf_head(data) : (u8 *) "",
  782. data ? wpabuf_len(data) : 0);
  783. }
  784. remove_sta:
  785. if (sta->added_unassoc && (resp != WLAN_STATUS_SUCCESS ||
  786. status_code != WLAN_STATUS_SUCCESS)) {
  787. hostapd_drv_sta_remove(hapd, sta->addr);
  788. sta->added_unassoc = 0;
  789. }
  790. wpabuf_free(data);
  791. }
  792. /**
  793. * auth_sae_init_committed - Send COMMIT and start SAE in committed state
  794. * @hapd: BSS data for the device initiating the authentication
  795. * @sta: the peer to which commit authentication frame is sent
  796. *
  797. * This function implements Init event handling (IEEE Std 802.11-2012,
  798. * 11.3.8.6.3) in which initial COMMIT message is sent. Prior to calling, the
  799. * sta->sae structure should be initialized appropriately via a call to
  800. * sae_prepare_commit().
  801. */
  802. int auth_sae_init_committed(struct hostapd_data *hapd, struct sta_info *sta)
  803. {
  804. int ret;
  805. if (!sta->sae || !sta->sae->tmp)
  806. return -1;
  807. if (sta->sae->state != SAE_NOTHING)
  808. return -1;
  809. ret = auth_sae_send_commit(hapd, sta, hapd->own_addr, 0);
  810. if (ret)
  811. return -1;
  812. sta->sae->state = SAE_COMMITTED;
  813. sta->sae->sync = 0;
  814. sae_set_retransmit_timer(hapd, sta);
  815. return 0;
  816. }
  817. #endif /* CONFIG_SAE */
  818. static u16 wpa_res_to_status_code(int res)
  819. {
  820. if (res == WPA_INVALID_GROUP)
  821. return WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
  822. if (res == WPA_INVALID_PAIRWISE)
  823. return WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
  824. if (res == WPA_INVALID_AKMP)
  825. return WLAN_STATUS_AKMP_NOT_VALID;
  826. if (res == WPA_ALLOC_FAIL)
  827. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  828. #ifdef CONFIG_IEEE80211W
  829. if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
  830. return WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
  831. if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
  832. return WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
  833. #endif /* CONFIG_IEEE80211W */
  834. if (res == WPA_INVALID_MDIE)
  835. return WLAN_STATUS_INVALID_MDIE;
  836. if (res != WPA_IE_OK)
  837. return WLAN_STATUS_INVALID_IE;
  838. return WLAN_STATUS_SUCCESS;
  839. }
  840. #ifdef CONFIG_FILS
  841. static void handle_auth_fils_finish(struct hostapd_data *hapd,
  842. struct sta_info *sta, u16 resp,
  843. struct rsn_pmksa_cache_entry *pmksa,
  844. struct wpabuf *erp_resp,
  845. const u8 *msk, size_t msk_len);
  846. static void handle_auth_fils(struct hostapd_data *hapd, struct sta_info *sta,
  847. const struct ieee80211_mgmt *mgmt, size_t len,
  848. u16 auth_transaction, u16 status_code)
  849. {
  850. u16 resp = WLAN_STATUS_SUCCESS;
  851. const u8 *pos, *end;
  852. struct ieee802_11_elems elems;
  853. int res;
  854. struct wpa_ie_data rsn;
  855. struct rsn_pmksa_cache_entry *pmksa = NULL;
  856. if (auth_transaction != 1 || status_code != WLAN_STATUS_SUCCESS)
  857. return;
  858. pos = mgmt->u.auth.variable;
  859. end = ((const u8 *) mgmt) + len;
  860. wpa_hexdump(MSG_DEBUG, "FILS: Authentication frame fields",
  861. pos, end - pos);
  862. /* TODO: Finite Cyclic Group when using PK or PFS */
  863. /* TODO: Element when using PK or PFS */
  864. wpa_hexdump(MSG_DEBUG, "FILS: Remaining IEs", pos, end - pos);
  865. if (ieee802_11_parse_elems(pos, end - pos, &elems, 1) == ParseFailed) {
  866. wpa_printf(MSG_DEBUG, "FILS: Could not parse elements");
  867. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  868. goto fail;
  869. }
  870. /* RSNE */
  871. wpa_hexdump(MSG_DEBUG, "FILS: RSN element",
  872. elems.rsn_ie, elems.rsn_ie_len);
  873. if (!elems.rsn_ie ||
  874. wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
  875. &rsn) < 0) {
  876. wpa_printf(MSG_DEBUG, "FILS: No valid RSN element");
  877. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  878. goto fail;
  879. }
  880. if (!sta->wpa_sm)
  881. sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr,
  882. NULL);
  883. if (!sta->wpa_sm) {
  884. wpa_printf(MSG_DEBUG,
  885. "FILS: Failed to initialize RSN state machine");
  886. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  887. goto fail;
  888. }
  889. res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
  890. elems.rsn_ie - 2, elems.rsn_ie_len + 2,
  891. elems.mdie, elems.mdie_len);
  892. resp = wpa_res_to_status_code(res);
  893. if (resp != WLAN_STATUS_SUCCESS)
  894. goto fail;
  895. /* TODO: MDE when using FILS+FT */
  896. /* TODO: FTE when using FILS+FT */
  897. if (!elems.fils_nonce) {
  898. wpa_printf(MSG_DEBUG, "FILS: No FILS Nonce field");
  899. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  900. goto fail;
  901. }
  902. wpa_hexdump(MSG_DEBUG, "FILS: SNonce", elems.fils_nonce,
  903. FILS_NONCE_LEN);
  904. os_memcpy(sta->fils_snonce, elems.fils_nonce, FILS_NONCE_LEN);
  905. /* PMKID List */
  906. if (rsn.pmkid && rsn.num_pmkid > 0) {
  907. u8 num;
  908. const u8 *pmkid;
  909. wpa_hexdump(MSG_DEBUG, "FILS: PMKID List",
  910. rsn.pmkid, rsn.num_pmkid * PMKID_LEN);
  911. pmkid = rsn.pmkid;
  912. num = rsn.num_pmkid;
  913. while (num) {
  914. wpa_hexdump(MSG_DEBUG, "FILS: PMKID", pmkid, PMKID_LEN);
  915. pmksa = wpa_auth_pmksa_get(hapd->wpa_auth, sta->addr,
  916. pmkid);
  917. if (pmksa)
  918. break;
  919. pmkid += PMKID_LEN;
  920. num--;
  921. }
  922. }
  923. if (pmksa && wpa_auth_sta_key_mgmt(sta->wpa_sm) != pmksa->akmp) {
  924. wpa_printf(MSG_DEBUG,
  925. "FILS: Matching PMKSA cache entry has different AKMP (0x%x != 0x%x) - ignore",
  926. wpa_auth_sta_key_mgmt(sta->wpa_sm), pmksa->akmp);
  927. pmksa = NULL;
  928. }
  929. if (pmksa)
  930. wpa_printf(MSG_DEBUG, "FILS: Found matching PMKSA cache entry");
  931. /* FILS Session */
  932. if (!elems.fils_session) {
  933. wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
  934. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  935. goto fail;
  936. }
  937. wpa_hexdump(MSG_DEBUG, "FILS: FILS Session", elems.fils_session,
  938. FILS_SESSION_LEN);
  939. os_memcpy(sta->fils_session, elems.fils_session, FILS_SESSION_LEN);
  940. /* FILS Wrapped Data */
  941. if (elems.fils_wrapped_data) {
  942. wpa_hexdump(MSG_DEBUG, "FILS: Wrapped Data",
  943. elems.fils_wrapped_data,
  944. elems.fils_wrapped_data_len);
  945. if (!pmksa) {
  946. #ifndef CONFIG_NO_RADIUS
  947. if (!sta->eapol_sm) {
  948. sta->eapol_sm =
  949. ieee802_1x_alloc_eapol_sm(hapd, sta);
  950. }
  951. wpa_printf(MSG_DEBUG,
  952. "FILS: Forward EAP-Identity/Re-auth Start to authentication server");
  953. ieee802_1x_encapsulate_radius(
  954. hapd, sta, elems.fils_wrapped_data,
  955. elems.fils_wrapped_data_len);
  956. wpa_printf(MSG_DEBUG,
  957. "FILS: Will send Authentication frame once the response from authentication server is available");
  958. sta->flags |= WLAN_STA_PENDING_FILS_ERP;
  959. return;
  960. #else /* CONFIG_NO_RADIUS */
  961. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  962. goto fail;
  963. #endif /* CONFIG_NO_RADIUS */
  964. }
  965. }
  966. fail:
  967. handle_auth_fils_finish(hapd, sta, resp, pmksa, NULL, NULL, 0);
  968. }
  969. static void handle_auth_fils_finish(struct hostapd_data *hapd,
  970. struct sta_info *sta, u16 resp,
  971. struct rsn_pmksa_cache_entry *pmksa,
  972. struct wpabuf *erp_resp,
  973. const u8 *msk, size_t msk_len)
  974. {
  975. u8 fils_nonce[FILS_NONCE_LEN];
  976. size_t ielen;
  977. struct wpabuf *data = NULL;
  978. const u8 *ie;
  979. u8 *ie_buf = NULL;
  980. const u8 *pmk = NULL;
  981. size_t pmk_len = 0;
  982. if (resp != WLAN_STATUS_SUCCESS)
  983. goto fail;
  984. ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &ielen);
  985. if (!ie) {
  986. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  987. goto fail;
  988. }
  989. if (pmksa) {
  990. /* Add PMKID of the selected PMKSA into RSNE */
  991. ie_buf = os_malloc(ielen + 2 + 2 + PMKID_LEN);
  992. if (!ie_buf) {
  993. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  994. goto fail;
  995. }
  996. os_memcpy(ie_buf, ie, ielen);
  997. if (wpa_insert_pmkid(ie_buf, &ielen, pmksa->pmkid) < 0) {
  998. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  999. goto fail;
  1000. }
  1001. ie = ie_buf;
  1002. }
  1003. if (random_get_bytes(fils_nonce, FILS_NONCE_LEN) < 0) {
  1004. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  1005. goto fail;
  1006. }
  1007. wpa_hexdump(MSG_DEBUG, "RSN: Generated FILS Nonce",
  1008. fils_nonce, FILS_NONCE_LEN);
  1009. data = wpabuf_alloc(1000 + ielen);
  1010. if (!data) {
  1011. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  1012. goto fail;
  1013. }
  1014. /* TODO: Finite Cyclic Group when using PK or PFS */
  1015. /* TODO: Element when using PK or PFS */
  1016. /* RSNE */
  1017. wpabuf_put_data(data, ie, ielen);
  1018. /* TODO: MDE when using FILS+FT */
  1019. /* TODO: FTE when using FILS+FT */
  1020. /* FILS Nonce */
  1021. wpabuf_put_u8(data, WLAN_EID_EXTENSION); /* Element ID */
  1022. wpabuf_put_u8(data, 1 + FILS_NONCE_LEN); /* Length */
  1023. /* Element ID Extension */
  1024. wpabuf_put_u8(data, WLAN_EID_EXT_FILS_NONCE);
  1025. wpabuf_put_data(data, fils_nonce, FILS_NONCE_LEN);
  1026. /* FILS Session */
  1027. wpabuf_put_u8(data, WLAN_EID_EXTENSION); /* Element ID */
  1028. wpabuf_put_u8(data, 1 + FILS_SESSION_LEN); /* Length */
  1029. /* Element ID Extension */
  1030. wpabuf_put_u8(data, WLAN_EID_EXT_FILS_SESSION);
  1031. wpabuf_put_data(data, sta->fils_session, FILS_SESSION_LEN);
  1032. /* FILS Wrapped Data */
  1033. if (!pmksa && erp_resp) {
  1034. wpabuf_put_u8(data, WLAN_EID_EXTENSION); /* Element ID */
  1035. wpabuf_put_u8(data, 1 + wpabuf_len(erp_resp)); /* Length */
  1036. /* Element ID Extension */
  1037. wpabuf_put_u8(data, WLAN_EID_EXT_FILS_WRAPPED_DATA);
  1038. wpabuf_put_buf(data, erp_resp);
  1039. pmk = msk;
  1040. pmk_len = msk_len > PMK_LEN ? PMK_LEN : msk_len;
  1041. } else if (pmksa) {
  1042. pmk = pmksa->pmk;
  1043. pmk_len = pmksa->pmk_len;
  1044. }
  1045. if (!pmk) {
  1046. wpa_printf(MSG_DEBUG, "FILS: No PMK available");
  1047. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  1048. wpabuf_free(data);
  1049. data = NULL;
  1050. goto fail;
  1051. }
  1052. if (fils_auth_pmk_to_ptk(sta->wpa_sm, pmk, pmk_len,
  1053. sta->fils_snonce, fils_nonce) < 0) {
  1054. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  1055. wpabuf_free(data);
  1056. data = NULL;
  1057. goto fail;
  1058. }
  1059. fail:
  1060. send_auth_reply(hapd, sta->addr, hapd->own_addr, WLAN_AUTH_FILS_SK, 2,
  1061. resp,
  1062. data ? wpabuf_head(data) : (u8 *) "",
  1063. data ? wpabuf_len(data) : 0);
  1064. wpabuf_free(data);
  1065. if (resp == WLAN_STATUS_SUCCESS) {
  1066. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1067. HOSTAPD_LEVEL_DEBUG,
  1068. "authentication OK (FILS)");
  1069. sta->flags |= WLAN_STA_AUTH;
  1070. wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
  1071. sta->auth_alg = WLAN_AUTH_FILS_SK;
  1072. mlme_authenticate_indication(hapd, sta);
  1073. }
  1074. os_free(ie_buf);
  1075. }
  1076. void ieee802_11_finish_fils_auth(struct hostapd_data *hapd,
  1077. struct sta_info *sta, int success,
  1078. struct wpabuf *erp_resp,
  1079. const u8 *msk, size_t msk_len)
  1080. {
  1081. sta->flags &= ~WLAN_STA_PENDING_FILS_ERP;
  1082. handle_auth_fils_finish(hapd, sta, success ? WLAN_STATUS_SUCCESS :
  1083. WLAN_STATUS_UNSPECIFIED_FAILURE, NULL,
  1084. erp_resp, msk, msk_len);
  1085. }
  1086. #endif /* CONFIG_FILS */
  1087. static void handle_auth(struct hostapd_data *hapd,
  1088. const struct ieee80211_mgmt *mgmt, size_t len)
  1089. {
  1090. u16 auth_alg, auth_transaction, status_code;
  1091. u16 resp = WLAN_STATUS_SUCCESS;
  1092. struct sta_info *sta = NULL;
  1093. int res, reply_res;
  1094. u16 fc;
  1095. const u8 *challenge = NULL;
  1096. u32 session_timeout, acct_interim_interval;
  1097. struct vlan_description vlan_id;
  1098. struct hostapd_sta_wpa_psk_short *psk = NULL;
  1099. u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
  1100. size_t resp_ies_len = 0;
  1101. char *identity = NULL;
  1102. char *radius_cui = NULL;
  1103. u16 seq_ctrl;
  1104. os_memset(&vlan_id, 0, sizeof(vlan_id));
  1105. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
  1106. wpa_printf(MSG_INFO, "handle_auth - too short payload (len=%lu)",
  1107. (unsigned long) len);
  1108. return;
  1109. }
  1110. #ifdef CONFIG_TESTING_OPTIONS
  1111. if (hapd->iconf->ignore_auth_probability > 0.0 &&
  1112. drand48() < hapd->iconf->ignore_auth_probability) {
  1113. wpa_printf(MSG_INFO,
  1114. "TESTING: ignoring auth frame from " MACSTR,
  1115. MAC2STR(mgmt->sa));
  1116. return;
  1117. }
  1118. #endif /* CONFIG_TESTING_OPTIONS */
  1119. auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
  1120. auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
  1121. status_code = le_to_host16(mgmt->u.auth.status_code);
  1122. fc = le_to_host16(mgmt->frame_control);
  1123. seq_ctrl = le_to_host16(mgmt->seq_ctrl);
  1124. if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
  1125. 2 + WLAN_AUTH_CHALLENGE_LEN &&
  1126. mgmt->u.auth.variable[0] == WLAN_EID_CHALLENGE &&
  1127. mgmt->u.auth.variable[1] == WLAN_AUTH_CHALLENGE_LEN)
  1128. challenge = &mgmt->u.auth.variable[2];
  1129. wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
  1130. "auth_transaction=%d status_code=%d wep=%d%s "
  1131. "seq_ctrl=0x%x%s",
  1132. MAC2STR(mgmt->sa), auth_alg, auth_transaction,
  1133. status_code, !!(fc & WLAN_FC_ISWEP),
  1134. challenge ? " challenge" : "",
  1135. seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "");
  1136. #ifdef CONFIG_NO_RC4
  1137. if (auth_alg == WLAN_AUTH_SHARED_KEY) {
  1138. wpa_printf(MSG_INFO,
  1139. "Unsupported authentication algorithm (%d)",
  1140. auth_alg);
  1141. resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
  1142. goto fail;
  1143. }
  1144. #endif /* CONFIG_NO_RC4 */
  1145. if (hapd->tkip_countermeasures) {
  1146. resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
  1147. goto fail;
  1148. }
  1149. if (!(((hapd->conf->auth_algs & WPA_AUTH_ALG_OPEN) &&
  1150. auth_alg == WLAN_AUTH_OPEN) ||
  1151. #ifdef CONFIG_IEEE80211R
  1152. (hapd->conf->wpa && wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt) &&
  1153. auth_alg == WLAN_AUTH_FT) ||
  1154. #endif /* CONFIG_IEEE80211R */
  1155. #ifdef CONFIG_SAE
  1156. (hapd->conf->wpa && wpa_key_mgmt_sae(hapd->conf->wpa_key_mgmt) &&
  1157. auth_alg == WLAN_AUTH_SAE) ||
  1158. #endif /* CONFIG_SAE */
  1159. #ifdef CONFIG_FILS
  1160. (hapd->conf->wpa && wpa_key_mgmt_fils(hapd->conf->wpa_key_mgmt) &&
  1161. auth_alg == WLAN_AUTH_FILS_SK) ||
  1162. #endif /* CONFIG_FILS */
  1163. ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
  1164. auth_alg == WLAN_AUTH_SHARED_KEY))) {
  1165. wpa_printf(MSG_INFO, "Unsupported authentication algorithm (%d)",
  1166. auth_alg);
  1167. resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
  1168. goto fail;
  1169. }
  1170. if (!(auth_transaction == 1 || auth_alg == WLAN_AUTH_SAE ||
  1171. (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
  1172. wpa_printf(MSG_INFO, "Unknown authentication transaction number (%d)",
  1173. auth_transaction);
  1174. resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
  1175. goto fail;
  1176. }
  1177. if (os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
  1178. wpa_printf(MSG_INFO, "Station " MACSTR " not allowed to authenticate",
  1179. MAC2STR(mgmt->sa));
  1180. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  1181. goto fail;
  1182. }
  1183. if (hapd->conf->no_auth_if_seen_on) {
  1184. struct hostapd_data *other;
  1185. other = sta_track_seen_on(hapd->iface, mgmt->sa,
  1186. hapd->conf->no_auth_if_seen_on);
  1187. if (other) {
  1188. u8 *pos;
  1189. u32 info;
  1190. u8 op_class, channel, phytype;
  1191. wpa_printf(MSG_DEBUG, "%s: Reject authentication from "
  1192. MACSTR " since STA has been seen on %s",
  1193. hapd->conf->iface, MAC2STR(mgmt->sa),
  1194. hapd->conf->no_auth_if_seen_on);
  1195. resp = WLAN_STATUS_REJECTED_WITH_SUGGESTED_BSS_TRANSITION;
  1196. pos = &resp_ies[0];
  1197. *pos++ = WLAN_EID_NEIGHBOR_REPORT;
  1198. *pos++ = 13;
  1199. os_memcpy(pos, other->own_addr, ETH_ALEN);
  1200. pos += ETH_ALEN;
  1201. info = 0; /* TODO: BSSID Information */
  1202. WPA_PUT_LE32(pos, info);
  1203. pos += 4;
  1204. if (other->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD)
  1205. phytype = 8; /* dmg */
  1206. else if (other->iconf->ieee80211ac)
  1207. phytype = 9; /* vht */
  1208. else if (other->iconf->ieee80211n)
  1209. phytype = 7; /* ht */
  1210. else if (other->iconf->hw_mode ==
  1211. HOSTAPD_MODE_IEEE80211A)
  1212. phytype = 4; /* ofdm */
  1213. else if (other->iconf->hw_mode ==
  1214. HOSTAPD_MODE_IEEE80211G)
  1215. phytype = 6; /* erp */
  1216. else
  1217. phytype = 5; /* hrdsss */
  1218. if (ieee80211_freq_to_channel_ext(
  1219. hostapd_hw_get_freq(other,
  1220. other->iconf->channel),
  1221. other->iconf->secondary_channel,
  1222. other->iconf->ieee80211ac,
  1223. &op_class, &channel) == NUM_HOSTAPD_MODES) {
  1224. op_class = 0;
  1225. channel = other->iconf->channel;
  1226. }
  1227. *pos++ = op_class;
  1228. *pos++ = channel;
  1229. *pos++ = phytype;
  1230. resp_ies_len = pos - &resp_ies[0];
  1231. goto fail;
  1232. }
  1233. }
  1234. res = hostapd_allowed_address(hapd, mgmt->sa, (u8 *) mgmt, len,
  1235. &session_timeout,
  1236. &acct_interim_interval, &vlan_id,
  1237. &psk, &identity, &radius_cui);
  1238. if (res == HOSTAPD_ACL_REJECT) {
  1239. wpa_printf(MSG_INFO, "Station " MACSTR " not allowed to authenticate",
  1240. MAC2STR(mgmt->sa));
  1241. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  1242. goto fail;
  1243. }
  1244. if (res == HOSTAPD_ACL_PENDING) {
  1245. wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
  1246. " waiting for an external authentication",
  1247. MAC2STR(mgmt->sa));
  1248. /* Authentication code will re-send the authentication frame
  1249. * after it has received (and cached) information from the
  1250. * external source. */
  1251. return;
  1252. }
  1253. sta = ap_get_sta(hapd, mgmt->sa);
  1254. if (sta) {
  1255. sta->flags &= ~WLAN_STA_PENDING_FILS_ERP;
  1256. if ((fc & WLAN_FC_RETRY) &&
  1257. sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
  1258. sta->last_seq_ctrl == seq_ctrl &&
  1259. sta->last_subtype == WLAN_FC_STYPE_AUTH) {
  1260. hostapd_logger(hapd, sta->addr,
  1261. HOSTAPD_MODULE_IEEE80211,
  1262. HOSTAPD_LEVEL_DEBUG,
  1263. "Drop repeated authentication frame seq_ctrl=0x%x",
  1264. seq_ctrl);
  1265. return;
  1266. }
  1267. #ifdef CONFIG_MESH
  1268. if ((hapd->conf->mesh & MESH_ENABLED) &&
  1269. sta->plink_state == PLINK_BLOCKED) {
  1270. wpa_printf(MSG_DEBUG, "Mesh peer " MACSTR
  1271. " is blocked - drop Authentication frame",
  1272. MAC2STR(mgmt->sa));
  1273. return;
  1274. }
  1275. #endif /* CONFIG_MESH */
  1276. } else {
  1277. #ifdef CONFIG_MESH
  1278. if (hapd->conf->mesh & MESH_ENABLED) {
  1279. /* if the mesh peer is not available, we don't do auth.
  1280. */
  1281. wpa_printf(MSG_DEBUG, "Mesh peer " MACSTR
  1282. " not yet known - drop Authentication frame",
  1283. MAC2STR(mgmt->sa));
  1284. /*
  1285. * Save a copy of the frame so that it can be processed
  1286. * if a new peer entry is added shortly after this.
  1287. */
  1288. wpabuf_free(hapd->mesh_pending_auth);
  1289. hapd->mesh_pending_auth = wpabuf_alloc_copy(mgmt, len);
  1290. os_get_reltime(&hapd->mesh_pending_auth_time);
  1291. return;
  1292. }
  1293. #endif /* CONFIG_MESH */
  1294. sta = ap_sta_add(hapd, mgmt->sa);
  1295. if (!sta) {
  1296. resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
  1297. goto fail;
  1298. }
  1299. }
  1300. sta->last_seq_ctrl = seq_ctrl;
  1301. sta->last_subtype = WLAN_FC_STYPE_AUTH;
  1302. if (vlan_id.notempty &&
  1303. !hostapd_vlan_valid(hapd->conf->vlan, &vlan_id)) {
  1304. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
  1305. HOSTAPD_LEVEL_INFO,
  1306. "Invalid VLAN %d%s received from RADIUS server",
  1307. vlan_id.untagged,
  1308. vlan_id.tagged[0] ? "+" : "");
  1309. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  1310. goto fail;
  1311. }
  1312. if (ap_sta_set_vlan(hapd, sta, &vlan_id) < 0) {
  1313. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  1314. goto fail;
  1315. }
  1316. if (sta->vlan_id)
  1317. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
  1318. HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
  1319. hostapd_free_psk_list(sta->psk);
  1320. if (hapd->conf->wpa_psk_radius != PSK_RADIUS_IGNORED) {
  1321. sta->psk = psk;
  1322. psk = NULL;
  1323. } else {
  1324. sta->psk = NULL;
  1325. }
  1326. sta->identity = identity;
  1327. identity = NULL;
  1328. sta->radius_cui = radius_cui;
  1329. radius_cui = NULL;
  1330. sta->flags &= ~WLAN_STA_PREAUTH;
  1331. ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
  1332. if (hapd->conf->acct_interim_interval == 0 && acct_interim_interval)
  1333. sta->acct_interim_interval = acct_interim_interval;
  1334. if (res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
  1335. ap_sta_session_timeout(hapd, sta, session_timeout);
  1336. else
  1337. ap_sta_no_session_timeout(hapd, sta);
  1338. /*
  1339. * If the driver supports full AP client state, add a station to the
  1340. * driver before sending authentication reply to make sure the driver
  1341. * has resources, and not to go through the entire authentication and
  1342. * association handshake, and fail it at the end.
  1343. *
  1344. * If this is not the first transaction, in a multi-step authentication
  1345. * algorithm, the station already exists in the driver
  1346. * (sta->added_unassoc = 1) so skip it.
  1347. *
  1348. * In mesh mode, the station was already added to the driver when the
  1349. * NEW_PEER_CANDIDATE event is received.
  1350. */
  1351. if (FULL_AP_CLIENT_STATE_SUPP(hapd->iface->drv_flags) &&
  1352. !(hapd->conf->mesh & MESH_ENABLED) &&
  1353. !(sta->added_unassoc)) {
  1354. /*
  1355. * If a station that is already associated to the AP, is trying
  1356. * to authenticate again, remove the STA entry, in order to make
  1357. * sure the STA PS state gets cleared and configuration gets
  1358. * updated. To handle this, station's added_unassoc flag is
  1359. * cleared once the station has completed association.
  1360. */
  1361. hostapd_drv_sta_remove(hapd, sta->addr);
  1362. sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_AUTH |
  1363. WLAN_STA_AUTHORIZED);
  1364. if (hostapd_sta_add(hapd, sta->addr, 0, 0, NULL, 0, 0,
  1365. NULL, NULL, sta->flags, 0, 0, 0, 0)) {
  1366. hostapd_logger(hapd, sta->addr,
  1367. HOSTAPD_MODULE_IEEE80211,
  1368. HOSTAPD_LEVEL_NOTICE,
  1369. "Could not add STA to kernel driver");
  1370. resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
  1371. goto fail;
  1372. }
  1373. sta->added_unassoc = 1;
  1374. }
  1375. switch (auth_alg) {
  1376. case WLAN_AUTH_OPEN:
  1377. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1378. HOSTAPD_LEVEL_DEBUG,
  1379. "authentication OK (open system)");
  1380. sta->flags |= WLAN_STA_AUTH;
  1381. wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
  1382. sta->auth_alg = WLAN_AUTH_OPEN;
  1383. mlme_authenticate_indication(hapd, sta);
  1384. break;
  1385. #ifndef CONFIG_NO_RC4
  1386. case WLAN_AUTH_SHARED_KEY:
  1387. resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
  1388. fc & WLAN_FC_ISWEP);
  1389. sta->auth_alg = WLAN_AUTH_SHARED_KEY;
  1390. mlme_authenticate_indication(hapd, sta);
  1391. if (sta->challenge && auth_transaction == 1) {
  1392. resp_ies[0] = WLAN_EID_CHALLENGE;
  1393. resp_ies[1] = WLAN_AUTH_CHALLENGE_LEN;
  1394. os_memcpy(resp_ies + 2, sta->challenge,
  1395. WLAN_AUTH_CHALLENGE_LEN);
  1396. resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
  1397. }
  1398. break;
  1399. #endif /* CONFIG_NO_RC4 */
  1400. #ifdef CONFIG_IEEE80211R
  1401. case WLAN_AUTH_FT:
  1402. sta->auth_alg = WLAN_AUTH_FT;
  1403. if (sta->wpa_sm == NULL)
  1404. sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
  1405. sta->addr, NULL);
  1406. if (sta->wpa_sm == NULL) {
  1407. wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
  1408. "state machine");
  1409. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  1410. goto fail;
  1411. }
  1412. wpa_ft_process_auth(sta->wpa_sm, mgmt->bssid,
  1413. auth_transaction, mgmt->u.auth.variable,
  1414. len - IEEE80211_HDRLEN -
  1415. sizeof(mgmt->u.auth),
  1416. handle_auth_ft_finish, hapd);
  1417. /* handle_auth_ft_finish() callback will complete auth. */
  1418. return;
  1419. #endif /* CONFIG_IEEE80211R */
  1420. #ifdef CONFIG_SAE
  1421. case WLAN_AUTH_SAE:
  1422. #ifdef CONFIG_MESH
  1423. if (status_code == WLAN_STATUS_SUCCESS &&
  1424. hapd->conf->mesh & MESH_ENABLED) {
  1425. if (sta->wpa_sm == NULL)
  1426. sta->wpa_sm =
  1427. wpa_auth_sta_init(hapd->wpa_auth,
  1428. sta->addr, NULL);
  1429. if (sta->wpa_sm == NULL) {
  1430. wpa_printf(MSG_DEBUG,
  1431. "SAE: Failed to initialize WPA state machine");
  1432. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  1433. goto fail;
  1434. }
  1435. }
  1436. #endif /* CONFIG_MESH */
  1437. handle_auth_sae(hapd, sta, mgmt, len, auth_transaction,
  1438. status_code);
  1439. return;
  1440. #endif /* CONFIG_SAE */
  1441. #ifdef CONFIG_FILS
  1442. case WLAN_AUTH_FILS_SK:
  1443. handle_auth_fils(hapd, sta, mgmt, len, auth_transaction,
  1444. status_code);
  1445. return;
  1446. #endif /* CONFIG_FILS */
  1447. }
  1448. fail:
  1449. os_free(identity);
  1450. os_free(radius_cui);
  1451. hostapd_free_psk_list(psk);
  1452. reply_res = send_auth_reply(hapd, mgmt->sa, mgmt->bssid, auth_alg,
  1453. auth_transaction + 1, resp, resp_ies,
  1454. resp_ies_len);
  1455. if (sta && sta->added_unassoc && (resp != WLAN_STATUS_SUCCESS ||
  1456. reply_res != WLAN_STATUS_SUCCESS)) {
  1457. hostapd_drv_sta_remove(hapd, sta->addr);
  1458. sta->added_unassoc = 0;
  1459. }
  1460. }
  1461. int hostapd_get_aid(struct hostapd_data *hapd, struct sta_info *sta)
  1462. {
  1463. int i, j = 32, aid;
  1464. /* get a unique AID */
  1465. if (sta->aid > 0) {
  1466. wpa_printf(MSG_DEBUG, " old AID %d", sta->aid);
  1467. return 0;
  1468. }
  1469. if (TEST_FAIL())
  1470. return -1;
  1471. for (i = 0; i < AID_WORDS; i++) {
  1472. if (hapd->sta_aid[i] == (u32) -1)
  1473. continue;
  1474. for (j = 0; j < 32; j++) {
  1475. if (!(hapd->sta_aid[i] & BIT(j)))
  1476. break;
  1477. }
  1478. if (j < 32)
  1479. break;
  1480. }
  1481. if (j == 32)
  1482. return -1;
  1483. aid = i * 32 + j + 1;
  1484. if (aid > 2007)
  1485. return -1;
  1486. sta->aid = aid;
  1487. hapd->sta_aid[i] |= BIT(j);
  1488. wpa_printf(MSG_DEBUG, " new AID %d", sta->aid);
  1489. return 0;
  1490. }
  1491. static u16 check_ssid(struct hostapd_data *hapd, struct sta_info *sta,
  1492. const u8 *ssid_ie, size_t ssid_ie_len)
  1493. {
  1494. if (ssid_ie == NULL)
  1495. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  1496. if (ssid_ie_len != hapd->conf->ssid.ssid_len ||
  1497. os_memcmp(ssid_ie, hapd->conf->ssid.ssid, ssid_ie_len) != 0) {
  1498. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1499. HOSTAPD_LEVEL_INFO,
  1500. "Station tried to associate with unknown SSID "
  1501. "'%s'", wpa_ssid_txt(ssid_ie, ssid_ie_len));
  1502. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  1503. }
  1504. return WLAN_STATUS_SUCCESS;
  1505. }
  1506. static u16 check_wmm(struct hostapd_data *hapd, struct sta_info *sta,
  1507. const u8 *wmm_ie, size_t wmm_ie_len)
  1508. {
  1509. sta->flags &= ~WLAN_STA_WMM;
  1510. sta->qosinfo = 0;
  1511. if (wmm_ie && hapd->conf->wmm_enabled) {
  1512. struct wmm_information_element *wmm;
  1513. if (!hostapd_eid_wmm_valid(hapd, wmm_ie, wmm_ie_len)) {
  1514. hostapd_logger(hapd, sta->addr,
  1515. HOSTAPD_MODULE_WPA,
  1516. HOSTAPD_LEVEL_DEBUG,
  1517. "invalid WMM element in association "
  1518. "request");
  1519. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  1520. }
  1521. sta->flags |= WLAN_STA_WMM;
  1522. wmm = (struct wmm_information_element *) wmm_ie;
  1523. sta->qosinfo = wmm->qos_info;
  1524. }
  1525. return WLAN_STATUS_SUCCESS;
  1526. }
  1527. static u16 copy_supp_rates(struct hostapd_data *hapd, struct sta_info *sta,
  1528. struct ieee802_11_elems *elems)
  1529. {
  1530. if (!elems->supp_rates) {
  1531. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1532. HOSTAPD_LEVEL_DEBUG,
  1533. "No supported rates element in AssocReq");
  1534. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  1535. }
  1536. if (elems->supp_rates_len + elems->ext_supp_rates_len >
  1537. sizeof(sta->supported_rates)) {
  1538. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1539. HOSTAPD_LEVEL_DEBUG,
  1540. "Invalid supported rates element length %d+%d",
  1541. elems->supp_rates_len,
  1542. elems->ext_supp_rates_len);
  1543. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  1544. }
  1545. sta->supported_rates_len = merge_byte_arrays(
  1546. sta->supported_rates, sizeof(sta->supported_rates),
  1547. elems->supp_rates, elems->supp_rates_len,
  1548. elems->ext_supp_rates, elems->ext_supp_rates_len);
  1549. return WLAN_STATUS_SUCCESS;
  1550. }
  1551. static u16 check_ext_capab(struct hostapd_data *hapd, struct sta_info *sta,
  1552. const u8 *ext_capab_ie, size_t ext_capab_ie_len)
  1553. {
  1554. #ifdef CONFIG_INTERWORKING
  1555. /* check for QoS Map support */
  1556. if (ext_capab_ie_len >= 5) {
  1557. if (ext_capab_ie[4] & 0x01)
  1558. sta->qos_map_enabled = 1;
  1559. }
  1560. #endif /* CONFIG_INTERWORKING */
  1561. if (ext_capab_ie_len > 0)
  1562. sta->ecsa_supported = !!(ext_capab_ie[0] & BIT(2));
  1563. return WLAN_STATUS_SUCCESS;
  1564. }
  1565. static u16 check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
  1566. const u8 *ies, size_t ies_len, int reassoc)
  1567. {
  1568. struct ieee802_11_elems elems;
  1569. u16 resp;
  1570. const u8 *wpa_ie;
  1571. size_t wpa_ie_len;
  1572. const u8 *p2p_dev_addr = NULL;
  1573. if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
  1574. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1575. HOSTAPD_LEVEL_INFO, "Station sent an invalid "
  1576. "association request");
  1577. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  1578. }
  1579. resp = check_ssid(hapd, sta, elems.ssid, elems.ssid_len);
  1580. if (resp != WLAN_STATUS_SUCCESS)
  1581. return resp;
  1582. resp = check_wmm(hapd, sta, elems.wmm, elems.wmm_len);
  1583. if (resp != WLAN_STATUS_SUCCESS)
  1584. return resp;
  1585. resp = check_ext_capab(hapd, sta, elems.ext_capab, elems.ext_capab_len);
  1586. if (resp != WLAN_STATUS_SUCCESS)
  1587. return resp;
  1588. resp = copy_supp_rates(hapd, sta, &elems);
  1589. if (resp != WLAN_STATUS_SUCCESS)
  1590. return resp;
  1591. #ifdef CONFIG_IEEE80211N
  1592. resp = copy_sta_ht_capab(hapd, sta, elems.ht_capabilities);
  1593. if (resp != WLAN_STATUS_SUCCESS)
  1594. return resp;
  1595. if (hapd->iconf->ieee80211n && hapd->iconf->require_ht &&
  1596. !(sta->flags & WLAN_STA_HT)) {
  1597. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1598. HOSTAPD_LEVEL_INFO, "Station does not support "
  1599. "mandatory HT PHY - reject association");
  1600. return WLAN_STATUS_ASSOC_DENIED_NO_HT;
  1601. }
  1602. #endif /* CONFIG_IEEE80211N */
  1603. #ifdef CONFIG_IEEE80211AC
  1604. if (hapd->iconf->ieee80211ac) {
  1605. resp = copy_sta_vht_capab(hapd, sta, elems.vht_capabilities);
  1606. if (resp != WLAN_STATUS_SUCCESS)
  1607. return resp;
  1608. resp = set_sta_vht_opmode(hapd, sta, elems.vht_opmode_notif);
  1609. if (resp != WLAN_STATUS_SUCCESS)
  1610. return resp;
  1611. }
  1612. if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht &&
  1613. !(sta->flags & WLAN_STA_VHT)) {
  1614. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1615. HOSTAPD_LEVEL_INFO, "Station does not support "
  1616. "mandatory VHT PHY - reject association");
  1617. return WLAN_STATUS_ASSOC_DENIED_NO_VHT;
  1618. }
  1619. if (hapd->conf->vendor_vht && !elems.vht_capabilities) {
  1620. resp = copy_sta_vendor_vht(hapd, sta, elems.vendor_vht,
  1621. elems.vendor_vht_len);
  1622. if (resp != WLAN_STATUS_SUCCESS)
  1623. return resp;
  1624. }
  1625. #endif /* CONFIG_IEEE80211AC */
  1626. #ifdef CONFIG_P2P
  1627. if (elems.p2p) {
  1628. wpabuf_free(sta->p2p_ie);
  1629. sta->p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
  1630. P2P_IE_VENDOR_TYPE);
  1631. if (sta->p2p_ie)
  1632. p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
  1633. } else {
  1634. wpabuf_free(sta->p2p_ie);
  1635. sta->p2p_ie = NULL;
  1636. }
  1637. #endif /* CONFIG_P2P */
  1638. if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
  1639. wpa_ie = elems.rsn_ie;
  1640. wpa_ie_len = elems.rsn_ie_len;
  1641. } else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
  1642. elems.wpa_ie) {
  1643. wpa_ie = elems.wpa_ie;
  1644. wpa_ie_len = elems.wpa_ie_len;
  1645. } else {
  1646. wpa_ie = NULL;
  1647. wpa_ie_len = 0;
  1648. }
  1649. #ifdef CONFIG_WPS
  1650. sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
  1651. if (hapd->conf->wps_state && elems.wps_ie) {
  1652. wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)Association "
  1653. "Request - assume WPS is used");
  1654. sta->flags |= WLAN_STA_WPS;
  1655. wpabuf_free(sta->wps_ie);
  1656. sta->wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
  1657. WPS_IE_VENDOR_TYPE);
  1658. if (sta->wps_ie && wps_is_20(sta->wps_ie)) {
  1659. wpa_printf(MSG_DEBUG, "WPS: STA supports WPS 2.0");
  1660. sta->flags |= WLAN_STA_WPS2;
  1661. }
  1662. wpa_ie = NULL;
  1663. wpa_ie_len = 0;
  1664. if (sta->wps_ie && wps_validate_assoc_req(sta->wps_ie) < 0) {
  1665. wpa_printf(MSG_DEBUG, "WPS: Invalid WPS IE in "
  1666. "(Re)Association Request - reject");
  1667. return WLAN_STATUS_INVALID_IE;
  1668. }
  1669. } else if (hapd->conf->wps_state && wpa_ie == NULL) {
  1670. wpa_printf(MSG_DEBUG, "STA did not include WPA/RSN IE in "
  1671. "(Re)Association Request - possible WPS use");
  1672. sta->flags |= WLAN_STA_MAYBE_WPS;
  1673. } else
  1674. #endif /* CONFIG_WPS */
  1675. if (hapd->conf->wpa && wpa_ie == NULL) {
  1676. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1677. HOSTAPD_LEVEL_INFO,
  1678. "No WPA/RSN IE in association request");
  1679. return WLAN_STATUS_INVALID_IE;
  1680. }
  1681. if (hapd->conf->wpa && wpa_ie) {
  1682. int res;
  1683. wpa_ie -= 2;
  1684. wpa_ie_len += 2;
  1685. if (sta->wpa_sm == NULL)
  1686. sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
  1687. sta->addr,
  1688. p2p_dev_addr);
  1689. if (sta->wpa_sm == NULL) {
  1690. wpa_printf(MSG_WARNING, "Failed to initialize WPA "
  1691. "state machine");
  1692. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  1693. }
  1694. res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
  1695. wpa_ie, wpa_ie_len,
  1696. elems.mdie, elems.mdie_len);
  1697. resp = wpa_res_to_status_code(res);
  1698. if (resp != WLAN_STATUS_SUCCESS)
  1699. return resp;
  1700. #ifdef CONFIG_IEEE80211W
  1701. if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
  1702. sta->sa_query_count > 0)
  1703. ap_check_sa_query_timeout(hapd, sta);
  1704. if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
  1705. (!reassoc || sta->auth_alg != WLAN_AUTH_FT)) {
  1706. /*
  1707. * STA has already been associated with MFP and SA
  1708. * Query timeout has not been reached. Reject the
  1709. * association attempt temporarily and start SA Query,
  1710. * if one is not pending.
  1711. */
  1712. if (sta->sa_query_count == 0)
  1713. ap_sta_start_sa_query(hapd, sta);
  1714. return WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
  1715. }
  1716. if (wpa_auth_uses_mfp(sta->wpa_sm))
  1717. sta->flags |= WLAN_STA_MFP;
  1718. else
  1719. sta->flags &= ~WLAN_STA_MFP;
  1720. #endif /* CONFIG_IEEE80211W */
  1721. #ifdef CONFIG_IEEE80211R
  1722. if (sta->auth_alg == WLAN_AUTH_FT) {
  1723. if (!reassoc) {
  1724. wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
  1725. "to use association (not "
  1726. "re-association) with FT auth_alg",
  1727. MAC2STR(sta->addr));
  1728. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  1729. }
  1730. resp = wpa_ft_validate_reassoc(sta->wpa_sm, ies,
  1731. ies_len);
  1732. if (resp != WLAN_STATUS_SUCCESS)
  1733. return resp;
  1734. }
  1735. #endif /* CONFIG_IEEE80211R */
  1736. #ifdef CONFIG_SAE
  1737. if (wpa_auth_uses_sae(sta->wpa_sm) &&
  1738. sta->auth_alg == WLAN_AUTH_OPEN) {
  1739. struct rsn_pmksa_cache_entry *sa;
  1740. sa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
  1741. if (!sa || sa->akmp != WPA_KEY_MGMT_SAE) {
  1742. wpa_printf(MSG_DEBUG,
  1743. "SAE: No PMKSA cache entry found for "
  1744. MACSTR, MAC2STR(sta->addr));
  1745. return WLAN_STATUS_INVALID_PMKID;
  1746. }
  1747. wpa_printf(MSG_DEBUG, "SAE: " MACSTR
  1748. " using PMKSA caching", MAC2STR(sta->addr));
  1749. } else if (wpa_auth_uses_sae(sta->wpa_sm) &&
  1750. sta->auth_alg != WLAN_AUTH_SAE &&
  1751. !(sta->auth_alg == WLAN_AUTH_FT &&
  1752. wpa_auth_uses_ft_sae(sta->wpa_sm))) {
  1753. wpa_printf(MSG_DEBUG, "SAE: " MACSTR " tried to use "
  1754. "SAE AKM after non-SAE auth_alg %u",
  1755. MAC2STR(sta->addr), sta->auth_alg);
  1756. return WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
  1757. }
  1758. #endif /* CONFIG_SAE */
  1759. #ifdef CONFIG_IEEE80211N
  1760. if ((sta->flags & (WLAN_STA_HT | WLAN_STA_VHT)) &&
  1761. wpa_auth_get_pairwise(sta->wpa_sm) == WPA_CIPHER_TKIP) {
  1762. hostapd_logger(hapd, sta->addr,
  1763. HOSTAPD_MODULE_IEEE80211,
  1764. HOSTAPD_LEVEL_INFO,
  1765. "Station tried to use TKIP with HT "
  1766. "association");
  1767. return WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
  1768. }
  1769. #endif /* CONFIG_IEEE80211N */
  1770. #ifdef CONFIG_HS20
  1771. } else if (hapd->conf->osen) {
  1772. if (elems.osen == NULL) {
  1773. hostapd_logger(
  1774. hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1775. HOSTAPD_LEVEL_INFO,
  1776. "No HS 2.0 OSEN element in association request");
  1777. return WLAN_STATUS_INVALID_IE;
  1778. }
  1779. wpa_printf(MSG_DEBUG, "HS 2.0: OSEN association");
  1780. if (sta->wpa_sm == NULL)
  1781. sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
  1782. sta->addr, NULL);
  1783. if (sta->wpa_sm == NULL) {
  1784. wpa_printf(MSG_WARNING, "Failed to initialize WPA "
  1785. "state machine");
  1786. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  1787. }
  1788. if (wpa_validate_osen(hapd->wpa_auth, sta->wpa_sm,
  1789. elems.osen - 2, elems.osen_len + 2) < 0)
  1790. return WLAN_STATUS_INVALID_IE;
  1791. #endif /* CONFIG_HS20 */
  1792. } else
  1793. wpa_auth_sta_no_wpa(sta->wpa_sm);
  1794. #ifdef CONFIG_P2P
  1795. p2p_group_notif_assoc(hapd->p2p_group, sta->addr, ies, ies_len);
  1796. #endif /* CONFIG_P2P */
  1797. #ifdef CONFIG_HS20
  1798. wpabuf_free(sta->hs20_ie);
  1799. if (elems.hs20 && elems.hs20_len > 4) {
  1800. sta->hs20_ie = wpabuf_alloc_copy(elems.hs20 + 4,
  1801. elems.hs20_len - 4);
  1802. } else
  1803. sta->hs20_ie = NULL;
  1804. #endif /* CONFIG_HS20 */
  1805. #ifdef CONFIG_FST
  1806. wpabuf_free(sta->mb_ies);
  1807. if (hapd->iface->fst)
  1808. sta->mb_ies = mb_ies_by_info(&elems.mb_ies);
  1809. else
  1810. sta->mb_ies = NULL;
  1811. #endif /* CONFIG_FST */
  1812. #ifdef CONFIG_MBO
  1813. mbo_ap_check_sta_assoc(hapd, sta, &elems);
  1814. if (hapd->conf->mbo_enabled && (hapd->conf->wpa & 2) &&
  1815. elems.mbo && sta->cell_capa && !(sta->flags & WLAN_STA_MFP) &&
  1816. hapd->conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
  1817. wpa_printf(MSG_INFO,
  1818. "MBO: Reject WPA2 association without PMF");
  1819. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  1820. }
  1821. #endif /* CONFIG_MBO */
  1822. ap_copy_sta_supp_op_classes(sta, elems.supp_op_classes,
  1823. elems.supp_op_classes_len);
  1824. if ((sta->capability & WLAN_CAPABILITY_RADIO_MEASUREMENT) &&
  1825. elems.rrm_enabled &&
  1826. elems.rrm_enabled_len >= sizeof(sta->rrm_enabled_capa))
  1827. os_memcpy(sta->rrm_enabled_capa, elems.rrm_enabled,
  1828. sizeof(sta->rrm_enabled_capa));
  1829. return WLAN_STATUS_SUCCESS;
  1830. }
  1831. static void send_deauth(struct hostapd_data *hapd, const u8 *addr,
  1832. u16 reason_code)
  1833. {
  1834. int send_len;
  1835. struct ieee80211_mgmt reply;
  1836. os_memset(&reply, 0, sizeof(reply));
  1837. reply.frame_control =
  1838. IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_DEAUTH);
  1839. os_memcpy(reply.da, addr, ETH_ALEN);
  1840. os_memcpy(reply.sa, hapd->own_addr, ETH_ALEN);
  1841. os_memcpy(reply.bssid, hapd->own_addr, ETH_ALEN);
  1842. send_len = IEEE80211_HDRLEN + sizeof(reply.u.deauth);
  1843. reply.u.deauth.reason_code = host_to_le16(reason_code);
  1844. if (hostapd_drv_send_mlme(hapd, &reply, send_len, 0) < 0)
  1845. wpa_printf(MSG_INFO, "Failed to send deauth: %s",
  1846. strerror(errno));
  1847. }
  1848. static int add_associated_sta(struct hostapd_data *hapd,
  1849. struct sta_info *sta)
  1850. {
  1851. struct ieee80211_ht_capabilities ht_cap;
  1852. struct ieee80211_vht_capabilities vht_cap;
  1853. /*
  1854. * Remove the STA entry to ensure the STA PS state gets cleared and
  1855. * configuration gets updated. This is relevant for cases, such as
  1856. * FT-over-the-DS, where a station re-associates back to the same AP but
  1857. * skips the authentication flow, or if working with a driver that
  1858. * does not support full AP client state.
  1859. */
  1860. if (!sta->added_unassoc)
  1861. hostapd_drv_sta_remove(hapd, sta->addr);
  1862. #ifdef CONFIG_IEEE80211N
  1863. if (sta->flags & WLAN_STA_HT)
  1864. hostapd_get_ht_capab(hapd, sta->ht_capabilities, &ht_cap);
  1865. #endif /* CONFIG_IEEE80211N */
  1866. #ifdef CONFIG_IEEE80211AC
  1867. if (sta->flags & WLAN_STA_VHT)
  1868. hostapd_get_vht_capab(hapd, sta->vht_capabilities, &vht_cap);
  1869. #endif /* CONFIG_IEEE80211AC */
  1870. /*
  1871. * Add the station with forced WLAN_STA_ASSOC flag. The sta->flags
  1872. * will be set when the ACK frame for the (Re)Association Response frame
  1873. * is processed (TX status driver event).
  1874. */
  1875. if (hostapd_sta_add(hapd, sta->addr, sta->aid, sta->capability,
  1876. sta->supported_rates, sta->supported_rates_len,
  1877. sta->listen_interval,
  1878. sta->flags & WLAN_STA_HT ? &ht_cap : NULL,
  1879. sta->flags & WLAN_STA_VHT ? &vht_cap : NULL,
  1880. sta->flags | WLAN_STA_ASSOC, sta->qosinfo,
  1881. sta->vht_opmode, sta->p2p_ie ? 1 : 0,
  1882. sta->added_unassoc)) {
  1883. hostapd_logger(hapd, sta->addr,
  1884. HOSTAPD_MODULE_IEEE80211, HOSTAPD_LEVEL_NOTICE,
  1885. "Could not %s STA to kernel driver",
  1886. sta->added_unassoc ? "set" : "add");
  1887. if (sta->added_unassoc) {
  1888. hostapd_drv_sta_remove(hapd, sta->addr);
  1889. sta->added_unassoc = 0;
  1890. }
  1891. return -1;
  1892. }
  1893. sta->added_unassoc = 0;
  1894. return 0;
  1895. }
  1896. static u16 send_assoc_resp(struct hostapd_data *hapd, struct sta_info *sta,
  1897. u16 status_code, int reassoc, const u8 *ies,
  1898. size_t ies_len)
  1899. {
  1900. int send_len;
  1901. u8 buf[sizeof(struct ieee80211_mgmt) + 1024];
  1902. struct ieee80211_mgmt *reply;
  1903. u8 *p;
  1904. os_memset(buf, 0, sizeof(buf));
  1905. reply = (struct ieee80211_mgmt *) buf;
  1906. reply->frame_control =
  1907. IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  1908. (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
  1909. WLAN_FC_STYPE_ASSOC_RESP));
  1910. os_memcpy(reply->da, sta->addr, ETH_ALEN);
  1911. os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
  1912. os_memcpy(reply->bssid, hapd->own_addr, ETH_ALEN);
  1913. send_len = IEEE80211_HDRLEN;
  1914. send_len += sizeof(reply->u.assoc_resp);
  1915. reply->u.assoc_resp.capab_info =
  1916. host_to_le16(hostapd_own_capab_info(hapd));
  1917. reply->u.assoc_resp.status_code = host_to_le16(status_code);
  1918. reply->u.assoc_resp.aid = host_to_le16(sta->aid | BIT(14) | BIT(15));
  1919. /* Supported rates */
  1920. p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
  1921. /* Extended supported rates */
  1922. p = hostapd_eid_ext_supp_rates(hapd, p);
  1923. #ifdef CONFIG_IEEE80211R
  1924. if (status_code == WLAN_STATUS_SUCCESS) {
  1925. /* IEEE 802.11r: Mobility Domain Information, Fast BSS
  1926. * Transition Information, RSN, [RIC Response] */
  1927. p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
  1928. buf + sizeof(buf) - p,
  1929. sta->auth_alg, ies, ies_len);
  1930. }
  1931. #endif /* CONFIG_IEEE80211R */
  1932. #ifdef CONFIG_IEEE80211W
  1933. if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
  1934. p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
  1935. #endif /* CONFIG_IEEE80211W */
  1936. #ifdef CONFIG_IEEE80211N
  1937. p = hostapd_eid_ht_capabilities(hapd, p);
  1938. p = hostapd_eid_ht_operation(hapd, p);
  1939. #endif /* CONFIG_IEEE80211N */
  1940. #ifdef CONFIG_IEEE80211AC
  1941. if (hapd->iconf->ieee80211ac && !hapd->conf->disable_11ac) {
  1942. u32 nsts = 0, sta_nsts;
  1943. if (hapd->conf->use_sta_nsts && sta->vht_capabilities) {
  1944. struct ieee80211_vht_capabilities *capa;
  1945. nsts = (hapd->iface->conf->vht_capab >>
  1946. VHT_CAP_BEAMFORMEE_STS_OFFSET) & 7;
  1947. capa = sta->vht_capabilities;
  1948. sta_nsts = (le_to_host32(capa->vht_capabilities_info) >>
  1949. VHT_CAP_BEAMFORMEE_STS_OFFSET) & 7;
  1950. if (nsts < sta_nsts)
  1951. nsts = 0;
  1952. else
  1953. nsts = sta_nsts;
  1954. }
  1955. p = hostapd_eid_vht_capabilities(hapd, p, nsts);
  1956. p = hostapd_eid_vht_operation(hapd, p);
  1957. }
  1958. #endif /* CONFIG_IEEE80211AC */
  1959. p = hostapd_eid_ext_capab(hapd, p);
  1960. p = hostapd_eid_bss_max_idle_period(hapd, p);
  1961. if (sta->qos_map_enabled)
  1962. p = hostapd_eid_qos_map_set(hapd, p);
  1963. #ifdef CONFIG_FST
  1964. if (hapd->iface->fst_ies) {
  1965. os_memcpy(p, wpabuf_head(hapd->iface->fst_ies),
  1966. wpabuf_len(hapd->iface->fst_ies));
  1967. p += wpabuf_len(hapd->iface->fst_ies);
  1968. }
  1969. #endif /* CONFIG_FST */
  1970. #ifdef CONFIG_IEEE80211AC
  1971. if (hapd->conf->vendor_vht && (sta->flags & WLAN_STA_VENDOR_VHT))
  1972. p = hostapd_eid_vendor_vht(hapd, p);
  1973. #endif /* CONFIG_IEEE80211AC */
  1974. if (sta->flags & WLAN_STA_WMM)
  1975. p = hostapd_eid_wmm(hapd, p);
  1976. #ifdef CONFIG_WPS
  1977. if ((sta->flags & WLAN_STA_WPS) ||
  1978. ((sta->flags & WLAN_STA_MAYBE_WPS) && hapd->conf->wpa)) {
  1979. struct wpabuf *wps = wps_build_assoc_resp_ie();
  1980. if (wps) {
  1981. os_memcpy(p, wpabuf_head(wps), wpabuf_len(wps));
  1982. p += wpabuf_len(wps);
  1983. wpabuf_free(wps);
  1984. }
  1985. }
  1986. #endif /* CONFIG_WPS */
  1987. #ifdef CONFIG_P2P
  1988. if (sta->p2p_ie && hapd->p2p_group) {
  1989. struct wpabuf *p2p_resp_ie;
  1990. enum p2p_status_code status;
  1991. switch (status_code) {
  1992. case WLAN_STATUS_SUCCESS:
  1993. status = P2P_SC_SUCCESS;
  1994. break;
  1995. case WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA:
  1996. status = P2P_SC_FAIL_LIMIT_REACHED;
  1997. break;
  1998. default:
  1999. status = P2P_SC_FAIL_INVALID_PARAMS;
  2000. break;
  2001. }
  2002. p2p_resp_ie = p2p_group_assoc_resp_ie(hapd->p2p_group, status);
  2003. if (p2p_resp_ie) {
  2004. os_memcpy(p, wpabuf_head(p2p_resp_ie),
  2005. wpabuf_len(p2p_resp_ie));
  2006. p += wpabuf_len(p2p_resp_ie);
  2007. wpabuf_free(p2p_resp_ie);
  2008. }
  2009. }
  2010. #endif /* CONFIG_P2P */
  2011. #ifdef CONFIG_P2P_MANAGER
  2012. if (hapd->conf->p2p & P2P_MANAGE)
  2013. p = hostapd_eid_p2p_manage(hapd, p);
  2014. #endif /* CONFIG_P2P_MANAGER */
  2015. p = hostapd_eid_mbo(hapd, p, buf + sizeof(buf) - p);
  2016. if (hapd->conf->assocresp_elements &&
  2017. (size_t) (buf + sizeof(buf) - p) >=
  2018. wpabuf_len(hapd->conf->assocresp_elements)) {
  2019. os_memcpy(p, wpabuf_head(hapd->conf->assocresp_elements),
  2020. wpabuf_len(hapd->conf->assocresp_elements));
  2021. p += wpabuf_len(hapd->conf->assocresp_elements);
  2022. }
  2023. send_len += p - reply->u.assoc_resp.variable;
  2024. #ifdef CONFIG_FILS
  2025. if ((sta->auth_alg == WLAN_AUTH_FILS_SK ||
  2026. sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
  2027. sta->auth_alg == WLAN_AUTH_FILS_PK) &&
  2028. status_code == WLAN_STATUS_SUCCESS) {
  2029. struct ieee802_11_elems elems;
  2030. if (ieee802_11_parse_elems(ies, ies_len, &elems, 0) ==
  2031. ParseFailed || !elems.fils_session)
  2032. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  2033. /* FILS Session */
  2034. *p++ = WLAN_EID_EXTENSION; /* Element ID */
  2035. *p++ = 1 + FILS_SESSION_LEN; /* Length */
  2036. *p++ = WLAN_EID_EXT_FILS_SESSION; /* Element ID Extension */
  2037. os_memcpy(p, elems.fils_session, FILS_SESSION_LEN);
  2038. send_len += 2 + 1 + FILS_SESSION_LEN;
  2039. send_len = fils_encrypt_assoc(sta->wpa_sm, buf, send_len,
  2040. sizeof(buf));
  2041. if (send_len < 0)
  2042. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  2043. }
  2044. #endif /* CONFIG_FILS */
  2045. if (hostapd_drv_send_mlme(hapd, reply, send_len, 0) < 0) {
  2046. wpa_printf(MSG_INFO, "Failed to send assoc resp: %s",
  2047. strerror(errno));
  2048. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  2049. }
  2050. return WLAN_STATUS_SUCCESS;
  2051. }
  2052. static void handle_assoc(struct hostapd_data *hapd,
  2053. const struct ieee80211_mgmt *mgmt, size_t len,
  2054. int reassoc)
  2055. {
  2056. u16 capab_info, listen_interval, seq_ctrl, fc;
  2057. u16 resp = WLAN_STATUS_SUCCESS, reply_res;
  2058. const u8 *pos;
  2059. int left, i;
  2060. struct sta_info *sta;
  2061. u8 *tmp = NULL;
  2062. if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
  2063. sizeof(mgmt->u.assoc_req))) {
  2064. wpa_printf(MSG_INFO, "handle_assoc(reassoc=%d) - too short payload (len=%lu)",
  2065. reassoc, (unsigned long) len);
  2066. return;
  2067. }
  2068. #ifdef CONFIG_TESTING_OPTIONS
  2069. if (reassoc) {
  2070. if (hapd->iconf->ignore_reassoc_probability > 0.0 &&
  2071. drand48() < hapd->iconf->ignore_reassoc_probability) {
  2072. wpa_printf(MSG_INFO,
  2073. "TESTING: ignoring reassoc request from "
  2074. MACSTR, MAC2STR(mgmt->sa));
  2075. return;
  2076. }
  2077. } else {
  2078. if (hapd->iconf->ignore_assoc_probability > 0.0 &&
  2079. drand48() < hapd->iconf->ignore_assoc_probability) {
  2080. wpa_printf(MSG_INFO,
  2081. "TESTING: ignoring assoc request from "
  2082. MACSTR, MAC2STR(mgmt->sa));
  2083. return;
  2084. }
  2085. }
  2086. #endif /* CONFIG_TESTING_OPTIONS */
  2087. fc = le_to_host16(mgmt->frame_control);
  2088. seq_ctrl = le_to_host16(mgmt->seq_ctrl);
  2089. if (reassoc) {
  2090. capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
  2091. listen_interval = le_to_host16(
  2092. mgmt->u.reassoc_req.listen_interval);
  2093. wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
  2094. " capab_info=0x%02x listen_interval=%d current_ap="
  2095. MACSTR " seq_ctrl=0x%x%s",
  2096. MAC2STR(mgmt->sa), capab_info, listen_interval,
  2097. MAC2STR(mgmt->u.reassoc_req.current_ap),
  2098. seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "");
  2099. left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
  2100. pos = mgmt->u.reassoc_req.variable;
  2101. } else {
  2102. capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
  2103. listen_interval = le_to_host16(
  2104. mgmt->u.assoc_req.listen_interval);
  2105. wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
  2106. " capab_info=0x%02x listen_interval=%d "
  2107. "seq_ctrl=0x%x%s",
  2108. MAC2STR(mgmt->sa), capab_info, listen_interval,
  2109. seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "");
  2110. left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
  2111. pos = mgmt->u.assoc_req.variable;
  2112. }
  2113. sta = ap_get_sta(hapd, mgmt->sa);
  2114. #ifdef CONFIG_IEEE80211R
  2115. if (sta && sta->auth_alg == WLAN_AUTH_FT &&
  2116. (sta->flags & WLAN_STA_AUTH) == 0) {
  2117. wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
  2118. "prior to authentication since it is using "
  2119. "over-the-DS FT", MAC2STR(mgmt->sa));
  2120. /*
  2121. * Mark station as authenticated, to avoid adding station
  2122. * entry in the driver as associated and not authenticated
  2123. */
  2124. sta->flags |= WLAN_STA_AUTH;
  2125. } else
  2126. #endif /* CONFIG_IEEE80211R */
  2127. if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
  2128. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  2129. HOSTAPD_LEVEL_INFO, "Station tried to "
  2130. "associate before authentication "
  2131. "(aid=%d flags=0x%x)",
  2132. sta ? sta->aid : -1,
  2133. sta ? sta->flags : 0);
  2134. send_deauth(hapd, mgmt->sa,
  2135. WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA);
  2136. return;
  2137. }
  2138. if ((fc & WLAN_FC_RETRY) &&
  2139. sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
  2140. sta->last_seq_ctrl == seq_ctrl &&
  2141. sta->last_subtype == reassoc ? WLAN_FC_STYPE_REASSOC_REQ :
  2142. WLAN_FC_STYPE_ASSOC_REQ) {
  2143. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  2144. HOSTAPD_LEVEL_DEBUG,
  2145. "Drop repeated association frame seq_ctrl=0x%x",
  2146. seq_ctrl);
  2147. return;
  2148. }
  2149. sta->last_seq_ctrl = seq_ctrl;
  2150. sta->last_subtype = reassoc ? WLAN_FC_STYPE_REASSOC_REQ :
  2151. WLAN_FC_STYPE_ASSOC_REQ;
  2152. if (hapd->tkip_countermeasures) {
  2153. resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
  2154. goto fail;
  2155. }
  2156. if (listen_interval > hapd->conf->max_listen_interval) {
  2157. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  2158. HOSTAPD_LEVEL_DEBUG,
  2159. "Too large Listen Interval (%d)",
  2160. listen_interval);
  2161. resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
  2162. goto fail;
  2163. }
  2164. #ifdef CONFIG_MBO
  2165. if (hapd->conf->mbo_enabled && hapd->mbo_assoc_disallow) {
  2166. resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
  2167. goto fail;
  2168. }
  2169. #endif /* CONFIG_MBO */
  2170. /*
  2171. * sta->capability is used in check_assoc_ies() for RRM enabled
  2172. * capability element.
  2173. */
  2174. sta->capability = capab_info;
  2175. #ifdef CONFIG_FILS
  2176. if (sta->auth_alg == WLAN_AUTH_FILS_SK ||
  2177. sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
  2178. sta->auth_alg == WLAN_AUTH_FILS_PK) {
  2179. /* The end of the payload is encrypted. Need to decrypt it
  2180. * before parsing. */
  2181. tmp = os_malloc(left);
  2182. if (!tmp) {
  2183. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  2184. goto fail;
  2185. }
  2186. os_memcpy(tmp, pos, left);
  2187. left = fils_decrypt_assoc(sta->wpa_sm, sta->fils_session, mgmt,
  2188. len, tmp, left);
  2189. if (left < 0) {
  2190. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  2191. goto fail;
  2192. }
  2193. pos = tmp;
  2194. }
  2195. #endif /* CONFIG_FILS */
  2196. /* followed by SSID and Supported rates; and HT capabilities if 802.11n
  2197. * is used */
  2198. resp = check_assoc_ies(hapd, sta, pos, left, reassoc);
  2199. if (resp != WLAN_STATUS_SUCCESS)
  2200. goto fail;
  2201. if (hostapd_get_aid(hapd, sta) < 0) {
  2202. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  2203. HOSTAPD_LEVEL_INFO, "No room for more AIDs");
  2204. resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
  2205. goto fail;
  2206. }
  2207. sta->listen_interval = listen_interval;
  2208. if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
  2209. sta->flags |= WLAN_STA_NONERP;
  2210. for (i = 0; i < sta->supported_rates_len; i++) {
  2211. if ((sta->supported_rates[i] & 0x7f) > 22) {
  2212. sta->flags &= ~WLAN_STA_NONERP;
  2213. break;
  2214. }
  2215. }
  2216. if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
  2217. sta->nonerp_set = 1;
  2218. hapd->iface->num_sta_non_erp++;
  2219. if (hapd->iface->num_sta_non_erp == 1)
  2220. ieee802_11_set_beacons(hapd->iface);
  2221. }
  2222. if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
  2223. !sta->no_short_slot_time_set) {
  2224. sta->no_short_slot_time_set = 1;
  2225. hapd->iface->num_sta_no_short_slot_time++;
  2226. if (hapd->iface->current_mode->mode ==
  2227. HOSTAPD_MODE_IEEE80211G &&
  2228. hapd->iface->num_sta_no_short_slot_time == 1)
  2229. ieee802_11_set_beacons(hapd->iface);
  2230. }
  2231. if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
  2232. sta->flags |= WLAN_STA_SHORT_PREAMBLE;
  2233. else
  2234. sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
  2235. if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
  2236. !sta->no_short_preamble_set) {
  2237. sta->no_short_preamble_set = 1;
  2238. hapd->iface->num_sta_no_short_preamble++;
  2239. if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
  2240. && hapd->iface->num_sta_no_short_preamble == 1)
  2241. ieee802_11_set_beacons(hapd->iface);
  2242. }
  2243. #ifdef CONFIG_IEEE80211N
  2244. update_ht_state(hapd, sta);
  2245. #endif /* CONFIG_IEEE80211N */
  2246. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  2247. HOSTAPD_LEVEL_DEBUG,
  2248. "association OK (aid %d)", sta->aid);
  2249. /* Station will be marked associated, after it acknowledges AssocResp
  2250. */
  2251. sta->flags |= WLAN_STA_ASSOC_REQ_OK;
  2252. #ifdef CONFIG_IEEE80211W
  2253. if ((sta->flags & WLAN_STA_MFP) && sta->sa_query_timed_out) {
  2254. wpa_printf(MSG_DEBUG, "Allowing %sassociation after timed out "
  2255. "SA Query procedure", reassoc ? "re" : "");
  2256. /* TODO: Send a protected Disassociate frame to the STA using
  2257. * the old key and Reason Code "Previous Authentication no
  2258. * longer valid". Make sure this is only sent protected since
  2259. * unprotected frame would be received by the STA that is now
  2260. * trying to associate.
  2261. */
  2262. }
  2263. #endif /* CONFIG_IEEE80211W */
  2264. /* Make sure that the previously registered inactivity timer will not
  2265. * remove the STA immediately. */
  2266. sta->timeout_next = STA_NULLFUNC;
  2267. #ifdef CONFIG_TAXONOMY
  2268. taxonomy_sta_info_assoc_req(hapd, sta, pos, left);
  2269. #endif /* CONFIG_TAXONOMY */
  2270. fail:
  2271. /*
  2272. * In case of a successful response, add the station to the driver.
  2273. * Otherwise, the kernel may ignore Data frames before we process the
  2274. * ACK frame (TX status). In case of a failure, this station will be
  2275. * removed.
  2276. *
  2277. * Note that this is not compliant with the IEEE 802.11 standard that
  2278. * states that a non-AP station should transition into the
  2279. * authenticated/associated state only after the station acknowledges
  2280. * the (Re)Association Response frame. However, still do this as:
  2281. *
  2282. * 1. In case the station does not acknowledge the (Re)Association
  2283. * Response frame, it will be removed.
  2284. * 2. Data frames will be dropped in the kernel until the station is
  2285. * set into authorized state, and there are no significant known
  2286. * issues with processing other non-Data Class 3 frames during this
  2287. * window.
  2288. */
  2289. if (resp == WLAN_STATUS_SUCCESS && add_associated_sta(hapd, sta))
  2290. resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
  2291. reply_res = send_assoc_resp(hapd, sta, resp, reassoc, pos, left);
  2292. os_free(tmp);
  2293. /*
  2294. * Remove the station in case tranmission of a success response fails
  2295. * (the STA was added associated to the driver) or if the station was
  2296. * previously added unassociated.
  2297. */
  2298. if ((reply_res != WLAN_STATUS_SUCCESS &&
  2299. resp == WLAN_STATUS_SUCCESS) || sta->added_unassoc) {
  2300. hostapd_drv_sta_remove(hapd, sta->addr);
  2301. sta->added_unassoc = 0;
  2302. }
  2303. }
  2304. static void handle_disassoc(struct hostapd_data *hapd,
  2305. const struct ieee80211_mgmt *mgmt, size_t len)
  2306. {
  2307. struct sta_info *sta;
  2308. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
  2309. wpa_printf(MSG_INFO, "handle_disassoc - too short payload (len=%lu)",
  2310. (unsigned long) len);
  2311. return;
  2312. }
  2313. wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
  2314. MAC2STR(mgmt->sa),
  2315. le_to_host16(mgmt->u.disassoc.reason_code));
  2316. sta = ap_get_sta(hapd, mgmt->sa);
  2317. if (sta == NULL) {
  2318. wpa_printf(MSG_INFO, "Station " MACSTR " trying to disassociate, but it is not associated",
  2319. MAC2STR(mgmt->sa));
  2320. return;
  2321. }
  2322. ap_sta_set_authorized(hapd, sta, 0);
  2323. sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
  2324. sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
  2325. wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
  2326. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  2327. HOSTAPD_LEVEL_INFO, "disassociated");
  2328. sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
  2329. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  2330. /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
  2331. * authenticated. */
  2332. accounting_sta_stop(hapd, sta);
  2333. ieee802_1x_free_station(hapd, sta);
  2334. if (sta->ipaddr)
  2335. hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
  2336. ap_sta_ip6addr_del(hapd, sta);
  2337. hostapd_drv_sta_remove(hapd, sta->addr);
  2338. sta->added_unassoc = 0;
  2339. if (sta->timeout_next == STA_NULLFUNC ||
  2340. sta->timeout_next == STA_DISASSOC) {
  2341. sta->timeout_next = STA_DEAUTH;
  2342. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  2343. eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
  2344. hapd, sta);
  2345. }
  2346. mlme_disassociate_indication(
  2347. hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
  2348. }
  2349. static void handle_deauth(struct hostapd_data *hapd,
  2350. const struct ieee80211_mgmt *mgmt, size_t len)
  2351. {
  2352. struct sta_info *sta;
  2353. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
  2354. wpa_msg(hapd->msg_ctx, MSG_DEBUG, "handle_deauth - too short "
  2355. "payload (len=%lu)", (unsigned long) len);
  2356. return;
  2357. }
  2358. wpa_msg(hapd->msg_ctx, MSG_DEBUG, "deauthentication: STA=" MACSTR
  2359. " reason_code=%d",
  2360. MAC2STR(mgmt->sa), le_to_host16(mgmt->u.deauth.reason_code));
  2361. sta = ap_get_sta(hapd, mgmt->sa);
  2362. if (sta == NULL) {
  2363. wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR " trying "
  2364. "to deauthenticate, but it is not authenticated",
  2365. MAC2STR(mgmt->sa));
  2366. return;
  2367. }
  2368. ap_sta_set_authorized(hapd, sta, 0);
  2369. sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
  2370. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
  2371. WLAN_STA_ASSOC_REQ_OK);
  2372. wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
  2373. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  2374. HOSTAPD_LEVEL_DEBUG, "deauthenticated");
  2375. mlme_deauthenticate_indication(
  2376. hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
  2377. sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
  2378. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  2379. ap_free_sta(hapd, sta);
  2380. }
  2381. static void handle_beacon(struct hostapd_data *hapd,
  2382. const struct ieee80211_mgmt *mgmt, size_t len,
  2383. struct hostapd_frame_info *fi)
  2384. {
  2385. struct ieee802_11_elems elems;
  2386. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
  2387. wpa_printf(MSG_INFO, "handle_beacon - too short payload (len=%lu)",
  2388. (unsigned long) len);
  2389. return;
  2390. }
  2391. (void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
  2392. len - (IEEE80211_HDRLEN +
  2393. sizeof(mgmt->u.beacon)), &elems,
  2394. 0);
  2395. ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
  2396. }
  2397. #ifdef CONFIG_IEEE80211W
  2398. static int hostapd_sa_query_action(struct hostapd_data *hapd,
  2399. const struct ieee80211_mgmt *mgmt,
  2400. size_t len)
  2401. {
  2402. const u8 *end;
  2403. end = mgmt->u.action.u.sa_query_resp.trans_id +
  2404. WLAN_SA_QUERY_TR_ID_LEN;
  2405. if (((u8 *) mgmt) + len < end) {
  2406. wpa_printf(MSG_DEBUG, "IEEE 802.11: Too short SA Query Action "
  2407. "frame (len=%lu)", (unsigned long) len);
  2408. return 0;
  2409. }
  2410. ieee802_11_sa_query_action(hapd, mgmt->sa,
  2411. mgmt->u.action.u.sa_query_resp.action,
  2412. mgmt->u.action.u.sa_query_resp.trans_id);
  2413. return 1;
  2414. }
  2415. static int robust_action_frame(u8 category)
  2416. {
  2417. return category != WLAN_ACTION_PUBLIC &&
  2418. category != WLAN_ACTION_HT;
  2419. }
  2420. #endif /* CONFIG_IEEE80211W */
  2421. static int handle_action(struct hostapd_data *hapd,
  2422. const struct ieee80211_mgmt *mgmt, size_t len)
  2423. {
  2424. struct sta_info *sta;
  2425. sta = ap_get_sta(hapd, mgmt->sa);
  2426. if (len < IEEE80211_HDRLEN + 1) {
  2427. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  2428. HOSTAPD_LEVEL_DEBUG,
  2429. "handle_action - too short payload (len=%lu)",
  2430. (unsigned long) len);
  2431. return 0;
  2432. }
  2433. if (mgmt->u.action.category != WLAN_ACTION_PUBLIC &&
  2434. (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))) {
  2435. wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored Action "
  2436. "frame (category=%u) from unassociated STA " MACSTR,
  2437. MAC2STR(mgmt->sa), mgmt->u.action.category);
  2438. return 0;
  2439. }
  2440. #ifdef CONFIG_IEEE80211W
  2441. if (sta && (sta->flags & WLAN_STA_MFP) &&
  2442. !(mgmt->frame_control & host_to_le16(WLAN_FC_ISWEP)) &&
  2443. robust_action_frame(mgmt->u.action.category)) {
  2444. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  2445. HOSTAPD_LEVEL_DEBUG,
  2446. "Dropped unprotected Robust Action frame from "
  2447. "an MFP STA");
  2448. return 0;
  2449. }
  2450. #endif /* CONFIG_IEEE80211W */
  2451. if (sta) {
  2452. u16 fc = le_to_host16(mgmt->frame_control);
  2453. u16 seq_ctrl = le_to_host16(mgmt->seq_ctrl);
  2454. if ((fc & WLAN_FC_RETRY) &&
  2455. sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
  2456. sta->last_seq_ctrl == seq_ctrl &&
  2457. sta->last_subtype == WLAN_FC_STYPE_ACTION) {
  2458. hostapd_logger(hapd, sta->addr,
  2459. HOSTAPD_MODULE_IEEE80211,
  2460. HOSTAPD_LEVEL_DEBUG,
  2461. "Drop repeated action frame seq_ctrl=0x%x",
  2462. seq_ctrl);
  2463. return 1;
  2464. }
  2465. sta->last_seq_ctrl = seq_ctrl;
  2466. sta->last_subtype = WLAN_FC_STYPE_ACTION;
  2467. }
  2468. switch (mgmt->u.action.category) {
  2469. #ifdef CONFIG_IEEE80211R
  2470. case WLAN_ACTION_FT:
  2471. if (!sta ||
  2472. wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
  2473. len - IEEE80211_HDRLEN))
  2474. break;
  2475. return 1;
  2476. #endif /* CONFIG_IEEE80211R */
  2477. case WLAN_ACTION_WMM:
  2478. hostapd_wmm_action(hapd, mgmt, len);
  2479. return 1;
  2480. #ifdef CONFIG_IEEE80211W
  2481. case WLAN_ACTION_SA_QUERY:
  2482. return hostapd_sa_query_action(hapd, mgmt, len);
  2483. #endif /* CONFIG_IEEE80211W */
  2484. #ifdef CONFIG_WNM
  2485. case WLAN_ACTION_WNM:
  2486. ieee802_11_rx_wnm_action_ap(hapd, mgmt, len);
  2487. return 1;
  2488. #endif /* CONFIG_WNM */
  2489. #ifdef CONFIG_FST
  2490. case WLAN_ACTION_FST:
  2491. if (hapd->iface->fst)
  2492. fst_rx_action(hapd->iface->fst, mgmt, len);
  2493. else
  2494. wpa_printf(MSG_DEBUG,
  2495. "FST: Ignore FST Action frame - no FST attached");
  2496. return 1;
  2497. #endif /* CONFIG_FST */
  2498. case WLAN_ACTION_PUBLIC:
  2499. case WLAN_ACTION_PROTECTED_DUAL:
  2500. #ifdef CONFIG_IEEE80211N
  2501. if (len >= IEEE80211_HDRLEN + 2 &&
  2502. mgmt->u.action.u.public_action.action ==
  2503. WLAN_PA_20_40_BSS_COEX) {
  2504. wpa_printf(MSG_DEBUG,
  2505. "HT20/40 coex mgmt frame received from STA "
  2506. MACSTR, MAC2STR(mgmt->sa));
  2507. hostapd_2040_coex_action(hapd, mgmt, len);
  2508. }
  2509. #endif /* CONFIG_IEEE80211N */
  2510. if (hapd->public_action_cb) {
  2511. hapd->public_action_cb(hapd->public_action_cb_ctx,
  2512. (u8 *) mgmt, len,
  2513. hapd->iface->freq);
  2514. }
  2515. if (hapd->public_action_cb2) {
  2516. hapd->public_action_cb2(hapd->public_action_cb2_ctx,
  2517. (u8 *) mgmt, len,
  2518. hapd->iface->freq);
  2519. }
  2520. if (hapd->public_action_cb || hapd->public_action_cb2)
  2521. return 1;
  2522. break;
  2523. case WLAN_ACTION_VENDOR_SPECIFIC:
  2524. if (hapd->vendor_action_cb) {
  2525. if (hapd->vendor_action_cb(hapd->vendor_action_cb_ctx,
  2526. (u8 *) mgmt, len,
  2527. hapd->iface->freq) == 0)
  2528. return 1;
  2529. }
  2530. break;
  2531. case WLAN_ACTION_RADIO_MEASUREMENT:
  2532. hostapd_handle_radio_measurement(hapd, (const u8 *) mgmt, len);
  2533. return 1;
  2534. }
  2535. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  2536. HOSTAPD_LEVEL_DEBUG,
  2537. "handle_action - unknown action category %d or invalid "
  2538. "frame",
  2539. mgmt->u.action.category);
  2540. if (!is_multicast_ether_addr(mgmt->da) &&
  2541. !(mgmt->u.action.category & 0x80) &&
  2542. !is_multicast_ether_addr(mgmt->sa)) {
  2543. struct ieee80211_mgmt *resp;
  2544. /*
  2545. * IEEE 802.11-REVma/D9.0 - 7.3.1.11
  2546. * Return the Action frame to the source without change
  2547. * except that MSB of the Category set to 1.
  2548. */
  2549. wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
  2550. "frame back to sender");
  2551. resp = os_malloc(len);
  2552. if (resp == NULL)
  2553. return 0;
  2554. os_memcpy(resp, mgmt, len);
  2555. os_memcpy(resp->da, resp->sa, ETH_ALEN);
  2556. os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
  2557. os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
  2558. resp->u.action.category |= 0x80;
  2559. if (hostapd_drv_send_mlme(hapd, resp, len, 0) < 0) {
  2560. wpa_printf(MSG_ERROR, "IEEE 802.11: Failed to send "
  2561. "Action frame");
  2562. }
  2563. os_free(resp);
  2564. }
  2565. return 1;
  2566. }
  2567. /**
  2568. * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
  2569. * @hapd: hostapd BSS data structure (the BSS to which the management frame was
  2570. * sent to)
  2571. * @buf: management frame data (starting from IEEE 802.11 header)
  2572. * @len: length of frame data in octets
  2573. * @fi: meta data about received frame (signal level, etc.)
  2574. *
  2575. * Process all incoming IEEE 802.11 management frames. This will be called for
  2576. * each frame received from the kernel driver through wlan#ap interface. In
  2577. * addition, it can be called to re-inserted pending frames (e.g., when using
  2578. * external RADIUS server as an MAC ACL).
  2579. */
  2580. int ieee802_11_mgmt(struct hostapd_data *hapd, const u8 *buf, size_t len,
  2581. struct hostapd_frame_info *fi)
  2582. {
  2583. struct ieee80211_mgmt *mgmt;
  2584. u16 fc, stype;
  2585. int ret = 0;
  2586. if (len < 24)
  2587. return 0;
  2588. mgmt = (struct ieee80211_mgmt *) buf;
  2589. fc = le_to_host16(mgmt->frame_control);
  2590. stype = WLAN_FC_GET_STYPE(fc);
  2591. if (stype == WLAN_FC_STYPE_BEACON) {
  2592. handle_beacon(hapd, mgmt, len, fi);
  2593. return 1;
  2594. }
  2595. if (!is_broadcast_ether_addr(mgmt->bssid) &&
  2596. #ifdef CONFIG_P2P
  2597. /* Invitation responses can be sent with the peer MAC as BSSID */
  2598. !((hapd->conf->p2p & P2P_GROUP_OWNER) &&
  2599. stype == WLAN_FC_STYPE_ACTION) &&
  2600. #endif /* CONFIG_P2P */
  2601. #ifdef CONFIG_MESH
  2602. !(hapd->conf->mesh & MESH_ENABLED) &&
  2603. #endif /* CONFIG_MESH */
  2604. os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0) {
  2605. wpa_printf(MSG_INFO, "MGMT: BSSID=" MACSTR " not our address",
  2606. MAC2STR(mgmt->bssid));
  2607. return 0;
  2608. }
  2609. if (stype == WLAN_FC_STYPE_PROBE_REQ) {
  2610. handle_probe_req(hapd, mgmt, len, fi->ssi_signal);
  2611. return 1;
  2612. }
  2613. if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
  2614. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  2615. HOSTAPD_LEVEL_DEBUG,
  2616. "MGMT: DA=" MACSTR " not our address",
  2617. MAC2STR(mgmt->da));
  2618. return 0;
  2619. }
  2620. if (hapd->iconf->track_sta_max_num)
  2621. sta_track_add(hapd->iface, mgmt->sa);
  2622. switch (stype) {
  2623. case WLAN_FC_STYPE_AUTH:
  2624. wpa_printf(MSG_DEBUG, "mgmt::auth");
  2625. handle_auth(hapd, mgmt, len);
  2626. ret = 1;
  2627. break;
  2628. case WLAN_FC_STYPE_ASSOC_REQ:
  2629. wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
  2630. handle_assoc(hapd, mgmt, len, 0);
  2631. ret = 1;
  2632. break;
  2633. case WLAN_FC_STYPE_REASSOC_REQ:
  2634. wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
  2635. handle_assoc(hapd, mgmt, len, 1);
  2636. ret = 1;
  2637. break;
  2638. case WLAN_FC_STYPE_DISASSOC:
  2639. wpa_printf(MSG_DEBUG, "mgmt::disassoc");
  2640. handle_disassoc(hapd, mgmt, len);
  2641. ret = 1;
  2642. break;
  2643. case WLAN_FC_STYPE_DEAUTH:
  2644. wpa_msg(hapd->msg_ctx, MSG_DEBUG, "mgmt::deauth");
  2645. handle_deauth(hapd, mgmt, len);
  2646. ret = 1;
  2647. break;
  2648. case WLAN_FC_STYPE_ACTION:
  2649. wpa_printf(MSG_DEBUG, "mgmt::action");
  2650. ret = handle_action(hapd, mgmt, len);
  2651. break;
  2652. default:
  2653. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  2654. HOSTAPD_LEVEL_DEBUG,
  2655. "unknown mgmt frame subtype %d", stype);
  2656. break;
  2657. }
  2658. return ret;
  2659. }
  2660. static void handle_auth_cb(struct hostapd_data *hapd,
  2661. const struct ieee80211_mgmt *mgmt,
  2662. size_t len, int ok)
  2663. {
  2664. u16 auth_alg, auth_transaction, status_code;
  2665. struct sta_info *sta;
  2666. sta = ap_get_sta(hapd, mgmt->da);
  2667. if (!sta) {
  2668. wpa_printf(MSG_INFO, "handle_auth_cb: STA " MACSTR " not found",
  2669. MAC2STR(mgmt->da));
  2670. return;
  2671. }
  2672. auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
  2673. auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
  2674. status_code = le_to_host16(mgmt->u.auth.status_code);
  2675. if (!ok) {
  2676. hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
  2677. HOSTAPD_LEVEL_NOTICE,
  2678. "did not acknowledge authentication response");
  2679. goto fail;
  2680. }
  2681. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
  2682. wpa_printf(MSG_INFO, "handle_auth_cb - too short payload (len=%lu)",
  2683. (unsigned long) len);
  2684. goto fail;
  2685. }
  2686. if (status_code == WLAN_STATUS_SUCCESS &&
  2687. ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
  2688. (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
  2689. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  2690. HOSTAPD_LEVEL_INFO, "authenticated");
  2691. sta->flags |= WLAN_STA_AUTH;
  2692. if (sta->added_unassoc)
  2693. hostapd_set_sta_flags(hapd, sta);
  2694. return;
  2695. }
  2696. fail:
  2697. if (status_code != WLAN_STATUS_SUCCESS && sta->added_unassoc) {
  2698. hostapd_drv_sta_remove(hapd, sta->addr);
  2699. sta->added_unassoc = 0;
  2700. }
  2701. }
  2702. static void hostapd_set_wds_encryption(struct hostapd_data *hapd,
  2703. struct sta_info *sta,
  2704. char *ifname_wds)
  2705. {
  2706. int i;
  2707. struct hostapd_ssid *ssid = &hapd->conf->ssid;
  2708. if (hapd->conf->ieee802_1x || hapd->conf->wpa)
  2709. return;
  2710. for (i = 0; i < 4; i++) {
  2711. if (ssid->wep.key[i] &&
  2712. hostapd_drv_set_key(ifname_wds, hapd, WPA_ALG_WEP, NULL, i,
  2713. i == ssid->wep.idx, NULL, 0,
  2714. ssid->wep.key[i], ssid->wep.len[i])) {
  2715. wpa_printf(MSG_WARNING,
  2716. "Could not set WEP keys for WDS interface; %s",
  2717. ifname_wds);
  2718. break;
  2719. }
  2720. }
  2721. }
  2722. static void handle_assoc_cb(struct hostapd_data *hapd,
  2723. const struct ieee80211_mgmt *mgmt,
  2724. size_t len, int reassoc, int ok)
  2725. {
  2726. u16 status;
  2727. struct sta_info *sta;
  2728. int new_assoc = 1;
  2729. sta = ap_get_sta(hapd, mgmt->da);
  2730. if (!sta) {
  2731. wpa_printf(MSG_INFO, "handle_assoc_cb: STA " MACSTR " not found",
  2732. MAC2STR(mgmt->da));
  2733. return;
  2734. }
  2735. if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
  2736. sizeof(mgmt->u.assoc_resp))) {
  2737. wpa_printf(MSG_INFO,
  2738. "handle_assoc_cb(reassoc=%d) - too short payload (len=%lu)",
  2739. reassoc, (unsigned long) len);
  2740. hostapd_drv_sta_remove(hapd, sta->addr);
  2741. return;
  2742. }
  2743. if (reassoc)
  2744. status = le_to_host16(mgmt->u.reassoc_resp.status_code);
  2745. else
  2746. status = le_to_host16(mgmt->u.assoc_resp.status_code);
  2747. if (!ok) {
  2748. hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
  2749. HOSTAPD_LEVEL_DEBUG,
  2750. "did not acknowledge association response");
  2751. sta->flags &= ~WLAN_STA_ASSOC_REQ_OK;
  2752. /* The STA is added only in case of SUCCESS */
  2753. if (status == WLAN_STATUS_SUCCESS)
  2754. hostapd_drv_sta_remove(hapd, sta->addr);
  2755. return;
  2756. }
  2757. if (status != WLAN_STATUS_SUCCESS)
  2758. return;
  2759. /* Stop previous accounting session, if one is started, and allocate
  2760. * new session id for the new session. */
  2761. accounting_sta_stop(hapd, sta);
  2762. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  2763. HOSTAPD_LEVEL_INFO,
  2764. "associated (aid %d)",
  2765. sta->aid);
  2766. if (sta->flags & WLAN_STA_ASSOC)
  2767. new_assoc = 0;
  2768. sta->flags |= WLAN_STA_ASSOC;
  2769. sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
  2770. if ((!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) ||
  2771. sta->auth_alg == WLAN_AUTH_FT) {
  2772. /*
  2773. * Open, static WEP, or FT protocol; no separate authorization
  2774. * step.
  2775. */
  2776. ap_sta_set_authorized(hapd, sta, 1);
  2777. }
  2778. if (reassoc)
  2779. mlme_reassociate_indication(hapd, sta);
  2780. else
  2781. mlme_associate_indication(hapd, sta);
  2782. #ifdef CONFIG_IEEE80211W
  2783. sta->sa_query_timed_out = 0;
  2784. #endif /* CONFIG_IEEE80211W */
  2785. if (sta->flags & WLAN_STA_WDS) {
  2786. int ret;
  2787. char ifname_wds[IFNAMSIZ + 1];
  2788. ret = hostapd_set_wds_sta(hapd, ifname_wds, sta->addr,
  2789. sta->aid, 1);
  2790. if (!ret)
  2791. hostapd_set_wds_encryption(hapd, sta, ifname_wds);
  2792. }
  2793. if (sta->eapol_sm == NULL) {
  2794. /*
  2795. * This STA does not use RADIUS server for EAP authentication,
  2796. * so bind it to the selected VLAN interface now, since the
  2797. * interface selection is not going to change anymore.
  2798. */
  2799. if (ap_sta_bind_vlan(hapd, sta) < 0)
  2800. return;
  2801. } else if (sta->vlan_id) {
  2802. /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
  2803. if (ap_sta_bind_vlan(hapd, sta) < 0)
  2804. return;
  2805. }
  2806. hostapd_set_sta_flags(hapd, sta);
  2807. if (sta->auth_alg == WLAN_AUTH_FT)
  2808. wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
  2809. else
  2810. wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
  2811. hapd->new_assoc_sta_cb(hapd, sta, !new_assoc);
  2812. ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
  2813. if (sta->pending_eapol_rx) {
  2814. struct os_reltime now, age;
  2815. os_get_reltime(&now);
  2816. os_reltime_sub(&now, &sta->pending_eapol_rx->rx_time, &age);
  2817. if (age.sec == 0 && age.usec < 200000) {
  2818. wpa_printf(MSG_DEBUG,
  2819. "Process pending EAPOL frame that was received from " MACSTR " just before association notification",
  2820. MAC2STR(sta->addr));
  2821. ieee802_1x_receive(
  2822. hapd, mgmt->da,
  2823. wpabuf_head(sta->pending_eapol_rx->buf),
  2824. wpabuf_len(sta->pending_eapol_rx->buf));
  2825. }
  2826. wpabuf_free(sta->pending_eapol_rx->buf);
  2827. os_free(sta->pending_eapol_rx);
  2828. sta->pending_eapol_rx = NULL;
  2829. }
  2830. }
  2831. static void handle_deauth_cb(struct hostapd_data *hapd,
  2832. const struct ieee80211_mgmt *mgmt,
  2833. size_t len, int ok)
  2834. {
  2835. struct sta_info *sta;
  2836. if (is_multicast_ether_addr(mgmt->da))
  2837. return;
  2838. sta = ap_get_sta(hapd, mgmt->da);
  2839. if (!sta) {
  2840. wpa_printf(MSG_DEBUG, "handle_deauth_cb: STA " MACSTR
  2841. " not found", MAC2STR(mgmt->da));
  2842. return;
  2843. }
  2844. if (ok)
  2845. wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged deauth",
  2846. MAC2STR(sta->addr));
  2847. else
  2848. wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
  2849. "deauth", MAC2STR(sta->addr));
  2850. ap_sta_deauth_cb(hapd, sta);
  2851. }
  2852. static void handle_disassoc_cb(struct hostapd_data *hapd,
  2853. const struct ieee80211_mgmt *mgmt,
  2854. size_t len, int ok)
  2855. {
  2856. struct sta_info *sta;
  2857. if (is_multicast_ether_addr(mgmt->da))
  2858. return;
  2859. sta = ap_get_sta(hapd, mgmt->da);
  2860. if (!sta) {
  2861. wpa_printf(MSG_DEBUG, "handle_disassoc_cb: STA " MACSTR
  2862. " not found", MAC2STR(mgmt->da));
  2863. return;
  2864. }
  2865. if (ok)
  2866. wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged disassoc",
  2867. MAC2STR(sta->addr));
  2868. else
  2869. wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
  2870. "disassoc", MAC2STR(sta->addr));
  2871. ap_sta_disassoc_cb(hapd, sta);
  2872. }
  2873. /**
  2874. * ieee802_11_mgmt_cb - Process management frame TX status callback
  2875. * @hapd: hostapd BSS data structure (the BSS from which the management frame
  2876. * was sent from)
  2877. * @buf: management frame data (starting from IEEE 802.11 header)
  2878. * @len: length of frame data in octets
  2879. * @stype: management frame subtype from frame control field
  2880. * @ok: Whether the frame was ACK'ed
  2881. */
  2882. void ieee802_11_mgmt_cb(struct hostapd_data *hapd, const u8 *buf, size_t len,
  2883. u16 stype, int ok)
  2884. {
  2885. const struct ieee80211_mgmt *mgmt;
  2886. mgmt = (const struct ieee80211_mgmt *) buf;
  2887. #ifdef CONFIG_TESTING_OPTIONS
  2888. if (hapd->ext_mgmt_frame_handling) {
  2889. wpa_msg(hapd->msg_ctx, MSG_INFO, "MGMT-TX-STATUS stype=%u ok=%d",
  2890. stype, ok);
  2891. return;
  2892. }
  2893. #endif /* CONFIG_TESTING_OPTIONS */
  2894. switch (stype) {
  2895. case WLAN_FC_STYPE_AUTH:
  2896. wpa_printf(MSG_DEBUG, "mgmt::auth cb");
  2897. handle_auth_cb(hapd, mgmt, len, ok);
  2898. break;
  2899. case WLAN_FC_STYPE_ASSOC_RESP:
  2900. wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
  2901. handle_assoc_cb(hapd, mgmt, len, 0, ok);
  2902. break;
  2903. case WLAN_FC_STYPE_REASSOC_RESP:
  2904. wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
  2905. handle_assoc_cb(hapd, mgmt, len, 1, ok);
  2906. break;
  2907. case WLAN_FC_STYPE_PROBE_RESP:
  2908. wpa_printf(MSG_EXCESSIVE, "mgmt::proberesp cb ok=%d", ok);
  2909. break;
  2910. case WLAN_FC_STYPE_DEAUTH:
  2911. wpa_printf(MSG_DEBUG, "mgmt::deauth cb");
  2912. handle_deauth_cb(hapd, mgmt, len, ok);
  2913. break;
  2914. case WLAN_FC_STYPE_DISASSOC:
  2915. wpa_printf(MSG_DEBUG, "mgmt::disassoc cb");
  2916. handle_disassoc_cb(hapd, mgmt, len, ok);
  2917. break;
  2918. case WLAN_FC_STYPE_ACTION:
  2919. wpa_printf(MSG_DEBUG, "mgmt::action cb ok=%d", ok);
  2920. break;
  2921. default:
  2922. wpa_printf(MSG_INFO, "unknown mgmt cb frame subtype %d", stype);
  2923. break;
  2924. }
  2925. }
  2926. int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
  2927. {
  2928. /* TODO */
  2929. return 0;
  2930. }
  2931. int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
  2932. char *buf, size_t buflen)
  2933. {
  2934. /* TODO */
  2935. return 0;
  2936. }
  2937. void hostapd_tx_status(struct hostapd_data *hapd, const u8 *addr,
  2938. const u8 *buf, size_t len, int ack)
  2939. {
  2940. struct sta_info *sta;
  2941. struct hostapd_iface *iface = hapd->iface;
  2942. sta = ap_get_sta(hapd, addr);
  2943. if (sta == NULL && iface->num_bss > 1) {
  2944. size_t j;
  2945. for (j = 0; j < iface->num_bss; j++) {
  2946. hapd = iface->bss[j];
  2947. sta = ap_get_sta(hapd, addr);
  2948. if (sta)
  2949. break;
  2950. }
  2951. }
  2952. if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))
  2953. return;
  2954. if (sta->flags & WLAN_STA_PENDING_POLL) {
  2955. wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
  2956. "activity poll", MAC2STR(sta->addr),
  2957. ack ? "ACKed" : "did not ACK");
  2958. if (ack)
  2959. sta->flags &= ~WLAN_STA_PENDING_POLL;
  2960. }
  2961. ieee802_1x_tx_status(hapd, sta, buf, len, ack);
  2962. }
  2963. void hostapd_eapol_tx_status(struct hostapd_data *hapd, const u8 *dst,
  2964. const u8 *data, size_t len, int ack)
  2965. {
  2966. struct sta_info *sta;
  2967. struct hostapd_iface *iface = hapd->iface;
  2968. sta = ap_get_sta(hapd, dst);
  2969. if (sta == NULL && iface->num_bss > 1) {
  2970. size_t j;
  2971. for (j = 0; j < iface->num_bss; j++) {
  2972. hapd = iface->bss[j];
  2973. sta = ap_get_sta(hapd, dst);
  2974. if (sta)
  2975. break;
  2976. }
  2977. }
  2978. if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
  2979. wpa_printf(MSG_DEBUG, "Ignore TX status for Data frame to STA "
  2980. MACSTR " that is not currently associated",
  2981. MAC2STR(dst));
  2982. return;
  2983. }
  2984. ieee802_1x_eapol_tx_status(hapd, sta, data, len, ack);
  2985. }
  2986. void hostapd_client_poll_ok(struct hostapd_data *hapd, const u8 *addr)
  2987. {
  2988. struct sta_info *sta;
  2989. struct hostapd_iface *iface = hapd->iface;
  2990. sta = ap_get_sta(hapd, addr);
  2991. if (sta == NULL && iface->num_bss > 1) {
  2992. size_t j;
  2993. for (j = 0; j < iface->num_bss; j++) {
  2994. hapd = iface->bss[j];
  2995. sta = ap_get_sta(hapd, addr);
  2996. if (sta)
  2997. break;
  2998. }
  2999. }
  3000. if (sta == NULL)
  3001. return;
  3002. wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_POLL_OK MACSTR,
  3003. MAC2STR(sta->addr));
  3004. if (!(sta->flags & WLAN_STA_PENDING_POLL))
  3005. return;
  3006. wpa_printf(MSG_DEBUG, "STA " MACSTR " ACKed pending "
  3007. "activity poll", MAC2STR(sta->addr));
  3008. sta->flags &= ~WLAN_STA_PENDING_POLL;
  3009. }
  3010. void ieee802_11_rx_from_unknown(struct hostapd_data *hapd, const u8 *src,
  3011. int wds)
  3012. {
  3013. struct sta_info *sta;
  3014. sta = ap_get_sta(hapd, src);
  3015. if (sta && (sta->flags & WLAN_STA_ASSOC)) {
  3016. if (!hapd->conf->wds_sta)
  3017. return;
  3018. if (wds && !(sta->flags & WLAN_STA_WDS)) {
  3019. int ret;
  3020. char ifname_wds[IFNAMSIZ + 1];
  3021. wpa_printf(MSG_DEBUG, "Enable 4-address WDS mode for "
  3022. "STA " MACSTR " (aid %u)",
  3023. MAC2STR(sta->addr), sta->aid);
  3024. sta->flags |= WLAN_STA_WDS;
  3025. ret = hostapd_set_wds_sta(hapd, ifname_wds,
  3026. sta->addr, sta->aid, 1);
  3027. if (!ret)
  3028. hostapd_set_wds_encryption(hapd, sta,
  3029. ifname_wds);
  3030. }
  3031. return;
  3032. }
  3033. wpa_printf(MSG_DEBUG, "Data/PS-poll frame from not associated STA "
  3034. MACSTR, MAC2STR(src));
  3035. if (is_multicast_ether_addr(src)) {
  3036. /* Broadcast bit set in SA?! Ignore the frame silently. */
  3037. return;
  3038. }
  3039. if (sta && (sta->flags & WLAN_STA_ASSOC_REQ_OK)) {
  3040. wpa_printf(MSG_DEBUG, "Association Response to the STA has "
  3041. "already been sent, but no TX status yet known - "
  3042. "ignore Class 3 frame issue with " MACSTR,
  3043. MAC2STR(src));
  3044. return;
  3045. }
  3046. if (sta && (sta->flags & WLAN_STA_AUTH))
  3047. hostapd_drv_sta_disassoc(
  3048. hapd, src,
  3049. WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
  3050. else
  3051. hostapd_drv_sta_deauth(
  3052. hapd, src,
  3053. WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
  3054. }
  3055. #endif /* CONFIG_NATIVE_WINDOWS */