ieee802_11.c 125 KB

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