ieee802_11.c 121 KB

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