ieee802_11.c 123 KB

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