wpa_supplicant.c 145 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243
  1. /*
  2. * WPA Supplicant
  3. * Copyright (c) 2003-2014, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. *
  8. * This file implements functions for registering and unregistering
  9. * %wpa_supplicant interfaces. In addition, this file contains number of
  10. * functions for managing network connections.
  11. */
  12. #include "includes.h"
  13. #include "common.h"
  14. #include "crypto/random.h"
  15. #include "crypto/sha1.h"
  16. #include "eapol_supp/eapol_supp_sm.h"
  17. #include "eap_peer/eap.h"
  18. #include "eap_peer/eap_proxy.h"
  19. #include "eap_server/eap_methods.h"
  20. #include "rsn_supp/wpa.h"
  21. #include "eloop.h"
  22. #include "config.h"
  23. #include "utils/ext_password.h"
  24. #include "l2_packet/l2_packet.h"
  25. #include "wpa_supplicant_i.h"
  26. #include "driver_i.h"
  27. #include "ctrl_iface.h"
  28. #include "pcsc_funcs.h"
  29. #include "common/version.h"
  30. #include "rsn_supp/preauth.h"
  31. #include "rsn_supp/pmksa_cache.h"
  32. #include "common/wpa_ctrl.h"
  33. #include "common/ieee802_11_defs.h"
  34. #include "p2p/p2p.h"
  35. #include "blacklist.h"
  36. #include "wpas_glue.h"
  37. #include "wps_supplicant.h"
  38. #include "ibss_rsn.h"
  39. #include "sme.h"
  40. #include "gas_query.h"
  41. #include "ap.h"
  42. #include "p2p_supplicant.h"
  43. #include "wifi_display.h"
  44. #include "notify.h"
  45. #include "bgscan.h"
  46. #include "autoscan.h"
  47. #include "bss.h"
  48. #include "scan.h"
  49. #include "offchannel.h"
  50. #include "hs20_supplicant.h"
  51. #include "wnm_sta.h"
  52. #include "wpas_kay.h"
  53. #include "mesh.h"
  54. const char *wpa_supplicant_version =
  55. "wpa_supplicant v" VERSION_STR "\n"
  56. "Copyright (c) 2003-2014, Jouni Malinen <j@w1.fi> and contributors";
  57. const char *wpa_supplicant_license =
  58. "This software may be distributed under the terms of the BSD license.\n"
  59. "See README for more details.\n"
  60. #ifdef EAP_TLS_OPENSSL
  61. "\nThis product includes software developed by the OpenSSL Project\n"
  62. "for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
  63. #endif /* EAP_TLS_OPENSSL */
  64. ;
  65. #ifndef CONFIG_NO_STDOUT_DEBUG
  66. /* Long text divided into parts in order to fit in C89 strings size limits. */
  67. const char *wpa_supplicant_full_license1 =
  68. "";
  69. const char *wpa_supplicant_full_license2 =
  70. "This software may be distributed under the terms of the BSD license.\n"
  71. "\n"
  72. "Redistribution and use in source and binary forms, with or without\n"
  73. "modification, are permitted provided that the following conditions are\n"
  74. "met:\n"
  75. "\n";
  76. const char *wpa_supplicant_full_license3 =
  77. "1. Redistributions of source code must retain the above copyright\n"
  78. " notice, this list of conditions and the following disclaimer.\n"
  79. "\n"
  80. "2. Redistributions in binary form must reproduce the above copyright\n"
  81. " notice, this list of conditions and the following disclaimer in the\n"
  82. " documentation and/or other materials provided with the distribution.\n"
  83. "\n";
  84. const char *wpa_supplicant_full_license4 =
  85. "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
  86. " names of its contributors may be used to endorse or promote products\n"
  87. " derived from this software without specific prior written permission.\n"
  88. "\n"
  89. "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
  90. "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
  91. "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
  92. "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
  93. const char *wpa_supplicant_full_license5 =
  94. "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
  95. "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
  96. "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
  97. "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
  98. "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
  99. "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
  100. "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
  101. "\n";
  102. #endif /* CONFIG_NO_STDOUT_DEBUG */
  103. /* Configure default/group WEP keys for static WEP */
  104. int wpa_set_wep_keys(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
  105. {
  106. int i, set = 0;
  107. for (i = 0; i < NUM_WEP_KEYS; i++) {
  108. if (ssid->wep_key_len[i] == 0)
  109. continue;
  110. set = 1;
  111. wpa_drv_set_key(wpa_s, WPA_ALG_WEP, NULL,
  112. i, i == ssid->wep_tx_keyidx, NULL, 0,
  113. ssid->wep_key[i], ssid->wep_key_len[i]);
  114. }
  115. return set;
  116. }
  117. int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
  118. struct wpa_ssid *ssid)
  119. {
  120. u8 key[32];
  121. size_t keylen;
  122. enum wpa_alg alg;
  123. u8 seq[6] = { 0 };
  124. int ret;
  125. /* IBSS/WPA-None uses only one key (Group) for both receiving and
  126. * sending unicast and multicast packets. */
  127. if (ssid->mode != WPAS_MODE_IBSS) {
  128. wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid mode %d (not "
  129. "IBSS/ad-hoc) for WPA-None", ssid->mode);
  130. return -1;
  131. }
  132. if (!ssid->psk_set) {
  133. wpa_msg(wpa_s, MSG_INFO, "WPA: No PSK configured for "
  134. "WPA-None");
  135. return -1;
  136. }
  137. switch (wpa_s->group_cipher) {
  138. case WPA_CIPHER_CCMP:
  139. os_memcpy(key, ssid->psk, 16);
  140. keylen = 16;
  141. alg = WPA_ALG_CCMP;
  142. break;
  143. case WPA_CIPHER_GCMP:
  144. os_memcpy(key, ssid->psk, 16);
  145. keylen = 16;
  146. alg = WPA_ALG_GCMP;
  147. break;
  148. case WPA_CIPHER_TKIP:
  149. /* WPA-None uses the same Michael MIC key for both TX and RX */
  150. os_memcpy(key, ssid->psk, 16 + 8);
  151. os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
  152. keylen = 32;
  153. alg = WPA_ALG_TKIP;
  154. break;
  155. default:
  156. wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid group cipher %d for "
  157. "WPA-None", wpa_s->group_cipher);
  158. return -1;
  159. }
  160. /* TODO: should actually remember the previously used seq#, both for TX
  161. * and RX from each STA.. */
  162. ret = wpa_drv_set_key(wpa_s, alg, NULL, 0, 1, seq, 6, key, keylen);
  163. os_memset(key, 0, sizeof(key));
  164. return ret;
  165. }
  166. static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
  167. {
  168. struct wpa_supplicant *wpa_s = eloop_ctx;
  169. const u8 *bssid = wpa_s->bssid;
  170. if (is_zero_ether_addr(bssid))
  171. bssid = wpa_s->pending_bssid;
  172. wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
  173. MAC2STR(bssid));
  174. wpa_blacklist_add(wpa_s, bssid);
  175. wpa_sm_notify_disassoc(wpa_s->wpa);
  176. wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  177. wpa_s->reassociate = 1;
  178. /*
  179. * If we timed out, the AP or the local radio may be busy.
  180. * So, wait a second until scanning again.
  181. */
  182. wpa_supplicant_req_scan(wpa_s, 1, 0);
  183. }
  184. /**
  185. * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
  186. * @wpa_s: Pointer to wpa_supplicant data
  187. * @sec: Number of seconds after which to time out authentication
  188. * @usec: Number of microseconds after which to time out authentication
  189. *
  190. * This function is used to schedule a timeout for the current authentication
  191. * attempt.
  192. */
  193. void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
  194. int sec, int usec)
  195. {
  196. if (wpa_s->conf->ap_scan == 0 &&
  197. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
  198. return;
  199. wpa_dbg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
  200. "%d usec", sec, usec);
  201. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  202. eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
  203. }
  204. /**
  205. * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
  206. * @wpa_s: Pointer to wpa_supplicant data
  207. *
  208. * This function is used to cancel authentication timeout scheduled with
  209. * wpa_supplicant_req_auth_timeout() and it is called when authentication has
  210. * been completed.
  211. */
  212. void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
  213. {
  214. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
  215. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  216. wpa_blacklist_del(wpa_s, wpa_s->bssid);
  217. }
  218. /**
  219. * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
  220. * @wpa_s: Pointer to wpa_supplicant data
  221. *
  222. * This function is used to configure EAPOL state machine based on the selected
  223. * authentication mode.
  224. */
  225. void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
  226. {
  227. #ifdef IEEE8021X_EAPOL
  228. struct eapol_config eapol_conf;
  229. struct wpa_ssid *ssid = wpa_s->current_ssid;
  230. #ifdef CONFIG_IBSS_RSN
  231. if (ssid->mode == WPAS_MODE_IBSS &&
  232. wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
  233. wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
  234. /*
  235. * RSN IBSS authentication is per-STA and we can disable the
  236. * per-BSSID EAPOL authentication.
  237. */
  238. eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
  239. eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
  240. eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
  241. return;
  242. }
  243. #endif /* CONFIG_IBSS_RSN */
  244. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  245. eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
  246. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  247. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
  248. eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
  249. else
  250. eapol_sm_notify_portControl(wpa_s->eapol, Auto);
  251. os_memset(&eapol_conf, 0, sizeof(eapol_conf));
  252. if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  253. eapol_conf.accept_802_1x_keys = 1;
  254. eapol_conf.required_keys = 0;
  255. if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
  256. eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
  257. }
  258. if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
  259. eapol_conf.required_keys |=
  260. EAPOL_REQUIRE_KEY_BROADCAST;
  261. }
  262. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)
  263. eapol_conf.required_keys = 0;
  264. }
  265. eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
  266. eapol_conf.workaround = ssid->eap_workaround;
  267. eapol_conf.eap_disabled =
  268. !wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
  269. wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
  270. wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
  271. eapol_conf.external_sim = wpa_s->conf->external_sim;
  272. #ifdef CONFIG_WPS
  273. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
  274. eapol_conf.wps |= EAPOL_LOCAL_WPS_IN_USE;
  275. if (wpa_s->current_bss) {
  276. struct wpabuf *ie;
  277. ie = wpa_bss_get_vendor_ie_multi(wpa_s->current_bss,
  278. WPS_IE_VENDOR_TYPE);
  279. if (ie) {
  280. if (wps_is_20(ie))
  281. eapol_conf.wps |=
  282. EAPOL_PEER_IS_WPS20_AP;
  283. wpabuf_free(ie);
  284. }
  285. }
  286. }
  287. #endif /* CONFIG_WPS */
  288. eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
  289. ieee802_1x_alloc_kay_sm(wpa_s, ssid);
  290. #endif /* IEEE8021X_EAPOL */
  291. }
  292. /**
  293. * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
  294. * @wpa_s: Pointer to wpa_supplicant data
  295. * @ssid: Configuration data for the network
  296. *
  297. * This function is used to configure WPA state machine and related parameters
  298. * to a mode where WPA is not enabled. This is called as part of the
  299. * authentication configuration when the selected network does not use WPA.
  300. */
  301. void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
  302. struct wpa_ssid *ssid)
  303. {
  304. int i;
  305. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
  306. wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
  307. else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
  308. wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
  309. else
  310. wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
  311. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
  312. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
  313. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  314. wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
  315. wpa_s->group_cipher = WPA_CIPHER_NONE;
  316. wpa_s->mgmt_group_cipher = 0;
  317. for (i = 0; i < NUM_WEP_KEYS; i++) {
  318. if (ssid->wep_key_len[i] > 5) {
  319. wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
  320. wpa_s->group_cipher = WPA_CIPHER_WEP104;
  321. break;
  322. } else if (ssid->wep_key_len[i] > 0) {
  323. wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
  324. wpa_s->group_cipher = WPA_CIPHER_WEP40;
  325. break;
  326. }
  327. }
  328. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
  329. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
  330. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
  331. wpa_s->pairwise_cipher);
  332. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
  333. #ifdef CONFIG_IEEE80211W
  334. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
  335. wpa_s->mgmt_group_cipher);
  336. #endif /* CONFIG_IEEE80211W */
  337. pmksa_cache_clear_current(wpa_s->wpa);
  338. }
  339. void free_hw_features(struct wpa_supplicant *wpa_s)
  340. {
  341. int i;
  342. if (wpa_s->hw.modes == NULL)
  343. return;
  344. for (i = 0; i < wpa_s->hw.num_modes; i++) {
  345. os_free(wpa_s->hw.modes[i].channels);
  346. os_free(wpa_s->hw.modes[i].rates);
  347. }
  348. os_free(wpa_s->hw.modes);
  349. wpa_s->hw.modes = NULL;
  350. }
  351. static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
  352. {
  353. int i;
  354. bgscan_deinit(wpa_s);
  355. autoscan_deinit(wpa_s);
  356. scard_deinit(wpa_s->scard);
  357. wpa_s->scard = NULL;
  358. wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
  359. eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
  360. l2_packet_deinit(wpa_s->l2);
  361. wpa_s->l2 = NULL;
  362. if (wpa_s->l2_br) {
  363. l2_packet_deinit(wpa_s->l2_br);
  364. wpa_s->l2_br = NULL;
  365. }
  366. #ifdef CONFIG_TESTING_OPTIONS
  367. l2_packet_deinit(wpa_s->l2_test);
  368. wpa_s->l2_test = NULL;
  369. #endif /* CONFIG_TESTING_OPTIONS */
  370. if (wpa_s->conf != NULL) {
  371. struct wpa_ssid *ssid;
  372. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
  373. wpas_notify_network_removed(wpa_s, ssid);
  374. }
  375. os_free(wpa_s->confname);
  376. wpa_s->confname = NULL;
  377. os_free(wpa_s->confanother);
  378. wpa_s->confanother = NULL;
  379. wpa_sm_set_eapol(wpa_s->wpa, NULL);
  380. eapol_sm_deinit(wpa_s->eapol);
  381. wpa_s->eapol = NULL;
  382. rsn_preauth_deinit(wpa_s->wpa);
  383. #ifdef CONFIG_TDLS
  384. wpa_tdls_deinit(wpa_s->wpa);
  385. #endif /* CONFIG_TDLS */
  386. pmksa_candidate_free(wpa_s->wpa);
  387. wpa_sm_deinit(wpa_s->wpa);
  388. wpa_s->wpa = NULL;
  389. wpa_blacklist_clear(wpa_s);
  390. wpa_bss_deinit(wpa_s);
  391. wpa_supplicant_cancel_delayed_sched_scan(wpa_s);
  392. wpa_supplicant_cancel_scan(wpa_s);
  393. wpa_supplicant_cancel_auth_timeout(wpa_s);
  394. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
  395. #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
  396. eloop_cancel_timeout(wpa_supplicant_delayed_mic_error_report,
  397. wpa_s, NULL);
  398. #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  399. wpas_wps_deinit(wpa_s);
  400. wpabuf_free(wpa_s->pending_eapol_rx);
  401. wpa_s->pending_eapol_rx = NULL;
  402. #ifdef CONFIG_IBSS_RSN
  403. ibss_rsn_deinit(wpa_s->ibss_rsn);
  404. wpa_s->ibss_rsn = NULL;
  405. #endif /* CONFIG_IBSS_RSN */
  406. sme_deinit(wpa_s);
  407. #ifdef CONFIG_AP
  408. wpa_supplicant_ap_deinit(wpa_s);
  409. #endif /* CONFIG_AP */
  410. wpas_p2p_deinit(wpa_s);
  411. #ifdef CONFIG_OFFCHANNEL
  412. offchannel_deinit(wpa_s);
  413. #endif /* CONFIG_OFFCHANNEL */
  414. wpa_supplicant_cancel_sched_scan(wpa_s);
  415. os_free(wpa_s->next_scan_freqs);
  416. wpa_s->next_scan_freqs = NULL;
  417. os_free(wpa_s->manual_scan_freqs);
  418. wpa_s->manual_scan_freqs = NULL;
  419. os_free(wpa_s->manual_sched_scan_freqs);
  420. wpa_s->manual_sched_scan_freqs = NULL;
  421. gas_query_deinit(wpa_s->gas);
  422. wpa_s->gas = NULL;
  423. free_hw_features(wpa_s);
  424. ieee802_1x_dealloc_kay_sm(wpa_s);
  425. os_free(wpa_s->bssid_filter);
  426. wpa_s->bssid_filter = NULL;
  427. os_free(wpa_s->disallow_aps_bssid);
  428. wpa_s->disallow_aps_bssid = NULL;
  429. os_free(wpa_s->disallow_aps_ssid);
  430. wpa_s->disallow_aps_ssid = NULL;
  431. wnm_bss_keep_alive_deinit(wpa_s);
  432. #ifdef CONFIG_WNM
  433. wnm_deallocate_memory(wpa_s);
  434. #endif /* CONFIG_WNM */
  435. ext_password_deinit(wpa_s->ext_pw);
  436. wpa_s->ext_pw = NULL;
  437. wpabuf_free(wpa_s->last_gas_resp);
  438. wpa_s->last_gas_resp = NULL;
  439. wpabuf_free(wpa_s->prev_gas_resp);
  440. wpa_s->prev_gas_resp = NULL;
  441. os_free(wpa_s->last_scan_res);
  442. wpa_s->last_scan_res = NULL;
  443. #ifdef CONFIG_HS20
  444. hs20_deinit(wpa_s);
  445. #endif /* CONFIG_HS20 */
  446. for (i = 0; i < NUM_VENDOR_ELEM_FRAMES; i++) {
  447. wpabuf_free(wpa_s->vendor_elem[i]);
  448. wpa_s->vendor_elem[i] = NULL;
  449. }
  450. wmm_ac_notify_disassoc(wpa_s);
  451. }
  452. /**
  453. * wpa_clear_keys - Clear keys configured for the driver
  454. * @wpa_s: Pointer to wpa_supplicant data
  455. * @addr: Previously used BSSID or %NULL if not available
  456. *
  457. * This function clears the encryption keys that has been previously configured
  458. * for the driver.
  459. */
  460. void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
  461. {
  462. int i, max;
  463. #ifdef CONFIG_IEEE80211W
  464. max = 6;
  465. #else /* CONFIG_IEEE80211W */
  466. max = 4;
  467. #endif /* CONFIG_IEEE80211W */
  468. /* MLME-DELETEKEYS.request */
  469. for (i = 0; i < max; i++) {
  470. if (wpa_s->keys_cleared & BIT(i))
  471. continue;
  472. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, i, 0, NULL, 0,
  473. NULL, 0);
  474. }
  475. if (!(wpa_s->keys_cleared & BIT(0)) && addr &&
  476. !is_zero_ether_addr(addr)) {
  477. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
  478. 0);
  479. /* MLME-SETPROTECTION.request(None) */
  480. wpa_drv_mlme_setprotection(
  481. wpa_s, addr,
  482. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  483. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  484. }
  485. wpa_s->keys_cleared = (u32) -1;
  486. }
  487. /**
  488. * wpa_supplicant_state_txt - Get the connection state name as a text string
  489. * @state: State (wpa_state; WPA_*)
  490. * Returns: The state name as a printable text string
  491. */
  492. const char * wpa_supplicant_state_txt(enum wpa_states state)
  493. {
  494. switch (state) {
  495. case WPA_DISCONNECTED:
  496. return "DISCONNECTED";
  497. case WPA_INACTIVE:
  498. return "INACTIVE";
  499. case WPA_INTERFACE_DISABLED:
  500. return "INTERFACE_DISABLED";
  501. case WPA_SCANNING:
  502. return "SCANNING";
  503. case WPA_AUTHENTICATING:
  504. return "AUTHENTICATING";
  505. case WPA_ASSOCIATING:
  506. return "ASSOCIATING";
  507. case WPA_ASSOCIATED:
  508. return "ASSOCIATED";
  509. case WPA_4WAY_HANDSHAKE:
  510. return "4WAY_HANDSHAKE";
  511. case WPA_GROUP_HANDSHAKE:
  512. return "GROUP_HANDSHAKE";
  513. case WPA_COMPLETED:
  514. return "COMPLETED";
  515. default:
  516. return "UNKNOWN";
  517. }
  518. }
  519. #ifdef CONFIG_BGSCAN
  520. static void wpa_supplicant_start_bgscan(struct wpa_supplicant *wpa_s)
  521. {
  522. const char *name;
  523. if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan)
  524. name = wpa_s->current_ssid->bgscan;
  525. else
  526. name = wpa_s->conf->bgscan;
  527. if (name == NULL || name[0] == '\0')
  528. return;
  529. if (wpas_driver_bss_selection(wpa_s))
  530. return;
  531. if (wpa_s->current_ssid == wpa_s->bgscan_ssid)
  532. return;
  533. #ifdef CONFIG_P2P
  534. if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
  535. return;
  536. #endif /* CONFIG_P2P */
  537. bgscan_deinit(wpa_s);
  538. if (wpa_s->current_ssid) {
  539. if (bgscan_init(wpa_s, wpa_s->current_ssid, name)) {
  540. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
  541. "bgscan");
  542. /*
  543. * Live without bgscan; it is only used as a roaming
  544. * optimization, so the initial connection is not
  545. * affected.
  546. */
  547. } else {
  548. struct wpa_scan_results *scan_res;
  549. wpa_s->bgscan_ssid = wpa_s->current_ssid;
  550. scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL,
  551. 0);
  552. if (scan_res) {
  553. bgscan_notify_scan(wpa_s, scan_res);
  554. wpa_scan_results_free(scan_res);
  555. }
  556. }
  557. } else
  558. wpa_s->bgscan_ssid = NULL;
  559. }
  560. static void wpa_supplicant_stop_bgscan(struct wpa_supplicant *wpa_s)
  561. {
  562. if (wpa_s->bgscan_ssid != NULL) {
  563. bgscan_deinit(wpa_s);
  564. wpa_s->bgscan_ssid = NULL;
  565. }
  566. }
  567. #endif /* CONFIG_BGSCAN */
  568. static void wpa_supplicant_start_autoscan(struct wpa_supplicant *wpa_s)
  569. {
  570. if (autoscan_init(wpa_s, 0))
  571. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize autoscan");
  572. }
  573. static void wpa_supplicant_stop_autoscan(struct wpa_supplicant *wpa_s)
  574. {
  575. autoscan_deinit(wpa_s);
  576. }
  577. void wpa_supplicant_reinit_autoscan(struct wpa_supplicant *wpa_s)
  578. {
  579. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  580. wpa_s->wpa_state == WPA_SCANNING) {
  581. autoscan_deinit(wpa_s);
  582. wpa_supplicant_start_autoscan(wpa_s);
  583. }
  584. }
  585. /**
  586. * wpa_supplicant_set_state - Set current connection state
  587. * @wpa_s: Pointer to wpa_supplicant data
  588. * @state: The new connection state
  589. *
  590. * This function is called whenever the connection state changes, e.g.,
  591. * association is completed for WPA/WPA2 4-Way Handshake is started.
  592. */
  593. void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
  594. enum wpa_states state)
  595. {
  596. enum wpa_states old_state = wpa_s->wpa_state;
  597. wpa_dbg(wpa_s, MSG_DEBUG, "State: %s -> %s",
  598. wpa_supplicant_state_txt(wpa_s->wpa_state),
  599. wpa_supplicant_state_txt(state));
  600. if (state == WPA_INTERFACE_DISABLED) {
  601. /* Assure normal scan when interface is restored */
  602. wpa_s->normal_scans = 0;
  603. }
  604. if (state == WPA_COMPLETED) {
  605. wpas_connect_work_done(wpa_s);
  606. /* Reinitialize normal_scan counter */
  607. wpa_s->normal_scans = 0;
  608. }
  609. if (state != WPA_SCANNING)
  610. wpa_supplicant_notify_scanning(wpa_s, 0);
  611. if (state == WPA_COMPLETED && wpa_s->new_connection) {
  612. struct wpa_ssid *ssid = wpa_s->current_ssid;
  613. #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
  614. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
  615. MACSTR " completed [id=%d id_str=%s]",
  616. MAC2STR(wpa_s->bssid),
  617. ssid ? ssid->id : -1,
  618. ssid && ssid->id_str ? ssid->id_str : "");
  619. #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
  620. wpas_clear_temp_disabled(wpa_s, ssid, 1);
  621. wpa_s->extra_blacklist_count = 0;
  622. wpa_s->new_connection = 0;
  623. wpa_drv_set_operstate(wpa_s, 1);
  624. #ifndef IEEE8021X_EAPOL
  625. wpa_drv_set_supp_port(wpa_s, 1);
  626. #endif /* IEEE8021X_EAPOL */
  627. wpa_s->after_wps = 0;
  628. wpa_s->known_wps_freq = 0;
  629. wpas_p2p_completed(wpa_s);
  630. sme_sched_obss_scan(wpa_s, 1);
  631. } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
  632. state == WPA_ASSOCIATED) {
  633. wpa_s->new_connection = 1;
  634. wpa_drv_set_operstate(wpa_s, 0);
  635. #ifndef IEEE8021X_EAPOL
  636. wpa_drv_set_supp_port(wpa_s, 0);
  637. #endif /* IEEE8021X_EAPOL */
  638. sme_sched_obss_scan(wpa_s, 0);
  639. }
  640. wpa_s->wpa_state = state;
  641. #ifdef CONFIG_BGSCAN
  642. if (state == WPA_COMPLETED)
  643. wpa_supplicant_start_bgscan(wpa_s);
  644. else if (state < WPA_ASSOCIATED)
  645. wpa_supplicant_stop_bgscan(wpa_s);
  646. #endif /* CONFIG_BGSCAN */
  647. if (state == WPA_AUTHENTICATING)
  648. wpa_supplicant_stop_autoscan(wpa_s);
  649. if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
  650. wpa_supplicant_start_autoscan(wpa_s);
  651. if (old_state >= WPA_ASSOCIATED && wpa_s->wpa_state < WPA_ASSOCIATED)
  652. wmm_ac_notify_disassoc(wpa_s);
  653. if (wpa_s->wpa_state != old_state) {
  654. wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
  655. /*
  656. * Notify the P2P Device interface about a state change in one
  657. * of the interfaces.
  658. */
  659. wpas_p2p_indicate_state_change(wpa_s);
  660. if (wpa_s->wpa_state == WPA_COMPLETED ||
  661. old_state == WPA_COMPLETED)
  662. wpas_notify_auth_changed(wpa_s);
  663. }
  664. }
  665. void wpa_supplicant_terminate_proc(struct wpa_global *global)
  666. {
  667. int pending = 0;
  668. #ifdef CONFIG_WPS
  669. struct wpa_supplicant *wpa_s = global->ifaces;
  670. while (wpa_s) {
  671. struct wpa_supplicant *next = wpa_s->next;
  672. if (wpas_wps_terminate_pending(wpa_s) == 1)
  673. pending = 1;
  674. #ifdef CONFIG_P2P
  675. if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE ||
  676. (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group))
  677. wpas_p2p_disconnect(wpa_s);
  678. #endif /* CONFIG_P2P */
  679. wpa_s = next;
  680. }
  681. #endif /* CONFIG_WPS */
  682. if (pending)
  683. return;
  684. eloop_terminate();
  685. }
  686. static void wpa_supplicant_terminate(int sig, void *signal_ctx)
  687. {
  688. struct wpa_global *global = signal_ctx;
  689. wpa_supplicant_terminate_proc(global);
  690. }
  691. void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
  692. {
  693. enum wpa_states old_state = wpa_s->wpa_state;
  694. wpa_s->pairwise_cipher = 0;
  695. wpa_s->group_cipher = 0;
  696. wpa_s->mgmt_group_cipher = 0;
  697. wpa_s->key_mgmt = 0;
  698. if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED)
  699. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  700. if (wpa_s->wpa_state != old_state)
  701. wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
  702. }
  703. /**
  704. * wpa_supplicant_reload_configuration - Reload configuration data
  705. * @wpa_s: Pointer to wpa_supplicant data
  706. * Returns: 0 on success or -1 if configuration parsing failed
  707. *
  708. * This function can be used to request that the configuration data is reloaded
  709. * (e.g., after configuration file change). This function is reloading
  710. * configuration only for one interface, so this may need to be called multiple
  711. * times if %wpa_supplicant is controlling multiple interfaces and all
  712. * interfaces need reconfiguration.
  713. */
  714. int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
  715. {
  716. struct wpa_config *conf;
  717. int reconf_ctrl;
  718. int old_ap_scan;
  719. if (wpa_s->confname == NULL)
  720. return -1;
  721. conf = wpa_config_read(wpa_s->confname, NULL);
  722. if (conf == NULL) {
  723. wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
  724. "file '%s' - exiting", wpa_s->confname);
  725. return -1;
  726. }
  727. wpa_config_read(wpa_s->confanother, conf);
  728. conf->changed_parameters = (unsigned int) -1;
  729. reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
  730. || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
  731. os_strcmp(conf->ctrl_interface,
  732. wpa_s->conf->ctrl_interface) != 0);
  733. if (reconf_ctrl && wpa_s->ctrl_iface) {
  734. wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
  735. wpa_s->ctrl_iface = NULL;
  736. }
  737. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  738. if (wpa_s->current_ssid) {
  739. wpa_supplicant_deauthenticate(wpa_s,
  740. WLAN_REASON_DEAUTH_LEAVING);
  741. }
  742. /*
  743. * TODO: should notify EAPOL SM about changes in opensc_engine_path,
  744. * pkcs11_engine_path, pkcs11_module_path, openssl_ciphers.
  745. */
  746. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  747. /*
  748. * Clear forced success to clear EAP state for next
  749. * authentication.
  750. */
  751. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  752. }
  753. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  754. wpa_sm_set_config(wpa_s->wpa, NULL);
  755. wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
  756. wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
  757. rsn_preauth_deinit(wpa_s->wpa);
  758. old_ap_scan = wpa_s->conf->ap_scan;
  759. wpa_config_free(wpa_s->conf);
  760. wpa_s->conf = conf;
  761. if (old_ap_scan != wpa_s->conf->ap_scan)
  762. wpas_notify_ap_scan_changed(wpa_s);
  763. if (reconf_ctrl)
  764. wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
  765. wpa_supplicant_update_config(wpa_s);
  766. wpa_supplicant_clear_status(wpa_s);
  767. if (wpa_supplicant_enabled_networks(wpa_s)) {
  768. wpa_s->reassociate = 1;
  769. wpa_supplicant_req_scan(wpa_s, 0, 0);
  770. }
  771. wpa_dbg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
  772. return 0;
  773. }
  774. static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
  775. {
  776. struct wpa_global *global = signal_ctx;
  777. struct wpa_supplicant *wpa_s;
  778. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  779. wpa_dbg(wpa_s, MSG_DEBUG, "Signal %d received - reconfiguring",
  780. sig);
  781. if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
  782. wpa_supplicant_terminate_proc(global);
  783. }
  784. }
  785. }
  786. static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
  787. struct wpa_ssid *ssid,
  788. struct wpa_ie_data *ie)
  789. {
  790. int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
  791. if (ret) {
  792. if (ret == -2) {
  793. wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
  794. "from association info");
  795. }
  796. return -1;
  797. }
  798. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set "
  799. "cipher suites");
  800. if (!(ie->group_cipher & ssid->group_cipher)) {
  801. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
  802. "cipher 0x%x (mask 0x%x) - reject",
  803. ie->group_cipher, ssid->group_cipher);
  804. return -1;
  805. }
  806. if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
  807. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
  808. "cipher 0x%x (mask 0x%x) - reject",
  809. ie->pairwise_cipher, ssid->pairwise_cipher);
  810. return -1;
  811. }
  812. if (!(ie->key_mgmt & ssid->key_mgmt)) {
  813. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
  814. "management 0x%x (mask 0x%x) - reject",
  815. ie->key_mgmt, ssid->key_mgmt);
  816. return -1;
  817. }
  818. #ifdef CONFIG_IEEE80211W
  819. if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
  820. (ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
  821. wpa_s->conf->pmf : ssid->ieee80211w) ==
  822. MGMT_FRAME_PROTECTION_REQUIRED) {
  823. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
  824. "that does not support management frame protection - "
  825. "reject");
  826. return -1;
  827. }
  828. #endif /* CONFIG_IEEE80211W */
  829. return 0;
  830. }
  831. /**
  832. * wpa_supplicant_set_suites - Set authentication and encryption parameters
  833. * @wpa_s: Pointer to wpa_supplicant data
  834. * @bss: Scan results for the selected BSS, or %NULL if not available
  835. * @ssid: Configuration data for the selected network
  836. * @wpa_ie: Buffer for the WPA/RSN IE
  837. * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
  838. * used buffer length in case the functions returns success.
  839. * Returns: 0 on success or -1 on failure
  840. *
  841. * This function is used to configure authentication and encryption parameters
  842. * based on the network configuration and scan result for the selected BSS (if
  843. * available).
  844. */
  845. int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
  846. struct wpa_bss *bss, struct wpa_ssid *ssid,
  847. u8 *wpa_ie, size_t *wpa_ie_len)
  848. {
  849. struct wpa_ie_data ie;
  850. int sel, proto;
  851. const u8 *bss_wpa, *bss_rsn, *bss_osen;
  852. if (bss) {
  853. bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  854. bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  855. bss_osen = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
  856. } else
  857. bss_wpa = bss_rsn = bss_osen = NULL;
  858. if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
  859. wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
  860. (ie.group_cipher & ssid->group_cipher) &&
  861. (ie.pairwise_cipher & ssid->pairwise_cipher) &&
  862. (ie.key_mgmt & ssid->key_mgmt)) {
  863. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
  864. proto = WPA_PROTO_RSN;
  865. } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
  866. wpa_parse_wpa_ie(bss_wpa, 2 + bss_wpa[1], &ie) == 0 &&
  867. (ie.group_cipher & ssid->group_cipher) &&
  868. (ie.pairwise_cipher & ssid->pairwise_cipher) &&
  869. (ie.key_mgmt & ssid->key_mgmt)) {
  870. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
  871. proto = WPA_PROTO_WPA;
  872. #ifdef CONFIG_HS20
  873. } else if (bss_osen && (ssid->proto & WPA_PROTO_OSEN)) {
  874. wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: using OSEN");
  875. /* TODO: parse OSEN element */
  876. os_memset(&ie, 0, sizeof(ie));
  877. ie.group_cipher = WPA_CIPHER_CCMP;
  878. ie.pairwise_cipher = WPA_CIPHER_CCMP;
  879. ie.key_mgmt = WPA_KEY_MGMT_OSEN;
  880. proto = WPA_PROTO_OSEN;
  881. #endif /* CONFIG_HS20 */
  882. } else if (bss) {
  883. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
  884. wpa_dbg(wpa_s, MSG_DEBUG,
  885. "WPA: ssid proto=0x%x pairwise_cipher=0x%x group_cipher=0x%x key_mgmt=0x%x",
  886. ssid->proto, ssid->pairwise_cipher, ssid->group_cipher,
  887. ssid->key_mgmt);
  888. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: BSS " MACSTR " ssid='%s'%s%s%s",
  889. MAC2STR(bss->bssid),
  890. wpa_ssid_txt(bss->ssid, bss->ssid_len),
  891. bss_wpa ? " WPA" : "",
  892. bss_rsn ? " RSN" : "",
  893. bss_osen ? " OSEN" : "");
  894. if (bss_rsn) {
  895. wpa_hexdump(MSG_DEBUG, "RSN", bss_rsn, 2 + bss_rsn[1]);
  896. if (wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie)) {
  897. wpa_dbg(wpa_s, MSG_DEBUG,
  898. "Could not parse RSN element");
  899. } else {
  900. wpa_dbg(wpa_s, MSG_DEBUG,
  901. "RSN: pairwise_cipher=0x%x group_cipher=0x%x key_mgmt=0x%x",
  902. ie.pairwise_cipher, ie.group_cipher,
  903. ie.key_mgmt);
  904. }
  905. }
  906. if (bss_wpa) {
  907. wpa_hexdump(MSG_DEBUG, "WPA", bss_wpa, 2 + bss_wpa[1]);
  908. if (wpa_parse_wpa_ie(bss_wpa, 2 + bss_wpa[1], &ie)) {
  909. wpa_dbg(wpa_s, MSG_DEBUG,
  910. "Could not parse WPA element");
  911. } else {
  912. wpa_dbg(wpa_s, MSG_DEBUG,
  913. "WPA: pairwise_cipher=0x%x group_cipher=0x%x key_mgmt=0x%x",
  914. ie.pairwise_cipher, ie.group_cipher,
  915. ie.key_mgmt);
  916. }
  917. }
  918. return -1;
  919. } else {
  920. if (ssid->proto & WPA_PROTO_OSEN)
  921. proto = WPA_PROTO_OSEN;
  922. else if (ssid->proto & WPA_PROTO_RSN)
  923. proto = WPA_PROTO_RSN;
  924. else
  925. proto = WPA_PROTO_WPA;
  926. if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
  927. os_memset(&ie, 0, sizeof(ie));
  928. ie.group_cipher = ssid->group_cipher;
  929. ie.pairwise_cipher = ssid->pairwise_cipher;
  930. ie.key_mgmt = ssid->key_mgmt;
  931. #ifdef CONFIG_IEEE80211W
  932. ie.mgmt_group_cipher =
  933. ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION ?
  934. WPA_CIPHER_AES_128_CMAC : 0;
  935. #endif /* CONFIG_IEEE80211W */
  936. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Set cipher suites "
  937. "based on configuration");
  938. } else
  939. proto = ie.proto;
  940. }
  941. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected cipher suites: group %d "
  942. "pairwise %d key_mgmt %d proto %d",
  943. ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
  944. #ifdef CONFIG_IEEE80211W
  945. if (ssid->ieee80211w) {
  946. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
  947. ie.mgmt_group_cipher);
  948. }
  949. #endif /* CONFIG_IEEE80211W */
  950. wpa_s->wpa_proto = proto;
  951. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
  952. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
  953. !!(ssid->proto & (WPA_PROTO_RSN | WPA_PROTO_OSEN)));
  954. if (bss || !wpa_s->ap_ies_from_associnfo) {
  955. if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
  956. bss_wpa ? 2 + bss_wpa[1] : 0) ||
  957. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
  958. bss_rsn ? 2 + bss_rsn[1] : 0))
  959. return -1;
  960. }
  961. sel = ie.group_cipher & ssid->group_cipher;
  962. wpa_s->group_cipher = wpa_pick_group_cipher(sel);
  963. if (wpa_s->group_cipher < 0) {
  964. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select group "
  965. "cipher");
  966. return -1;
  967. }
  968. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK %s",
  969. wpa_cipher_txt(wpa_s->group_cipher));
  970. sel = ie.pairwise_cipher & ssid->pairwise_cipher;
  971. wpa_s->pairwise_cipher = wpa_pick_pairwise_cipher(sel, 1);
  972. if (wpa_s->pairwise_cipher < 0) {
  973. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select pairwise "
  974. "cipher");
  975. return -1;
  976. }
  977. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK %s",
  978. wpa_cipher_txt(wpa_s->pairwise_cipher));
  979. sel = ie.key_mgmt & ssid->key_mgmt;
  980. #ifdef CONFIG_SAE
  981. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE))
  982. sel &= ~(WPA_KEY_MGMT_SAE | WPA_KEY_MGMT_FT_SAE);
  983. #endif /* CONFIG_SAE */
  984. if (0) {
  985. } else if (sel & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
  986. wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SUITE_B;
  987. wpa_dbg(wpa_s, MSG_DEBUG,
  988. "WPA: using KEY_MGMT 802.1X with Suite B");
  989. #ifdef CONFIG_IEEE80211R
  990. } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
  991. wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
  992. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
  993. } else if (sel & WPA_KEY_MGMT_FT_PSK) {
  994. wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
  995. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
  996. #endif /* CONFIG_IEEE80211R */
  997. #ifdef CONFIG_SAE
  998. } else if (sel & WPA_KEY_MGMT_SAE) {
  999. wpa_s->key_mgmt = WPA_KEY_MGMT_SAE;
  1000. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT SAE");
  1001. } else if (sel & WPA_KEY_MGMT_FT_SAE) {
  1002. wpa_s->key_mgmt = WPA_KEY_MGMT_FT_SAE;
  1003. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT FT/SAE");
  1004. #endif /* CONFIG_SAE */
  1005. #ifdef CONFIG_IEEE80211W
  1006. } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  1007. wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
  1008. wpa_dbg(wpa_s, MSG_DEBUG,
  1009. "WPA: using KEY_MGMT 802.1X with SHA256");
  1010. } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
  1011. wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
  1012. wpa_dbg(wpa_s, MSG_DEBUG,
  1013. "WPA: using KEY_MGMT PSK with SHA256");
  1014. #endif /* CONFIG_IEEE80211W */
  1015. } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
  1016. wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  1017. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
  1018. } else if (sel & WPA_KEY_MGMT_PSK) {
  1019. wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
  1020. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
  1021. } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
  1022. wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
  1023. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
  1024. #ifdef CONFIG_HS20
  1025. } else if (sel & WPA_KEY_MGMT_OSEN) {
  1026. wpa_s->key_mgmt = WPA_KEY_MGMT_OSEN;
  1027. wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: using KEY_MGMT OSEN");
  1028. #endif /* CONFIG_HS20 */
  1029. } else {
  1030. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select "
  1031. "authenticated key management type");
  1032. return -1;
  1033. }
  1034. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
  1035. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
  1036. wpa_s->pairwise_cipher);
  1037. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
  1038. #ifdef CONFIG_IEEE80211W
  1039. sel = ie.mgmt_group_cipher;
  1040. if ((ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
  1041. wpa_s->conf->pmf : ssid->ieee80211w) == NO_MGMT_FRAME_PROTECTION ||
  1042. !(ie.capabilities & WPA_CAPABILITY_MFPC))
  1043. sel = 0;
  1044. if (sel & WPA_CIPHER_AES_128_CMAC) {
  1045. wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
  1046. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
  1047. "AES-128-CMAC");
  1048. } else if (sel & WPA_CIPHER_BIP_GMAC_128) {
  1049. wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_GMAC_128;
  1050. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
  1051. "BIP-GMAC-128");
  1052. } else if (sel & WPA_CIPHER_BIP_GMAC_256) {
  1053. wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_GMAC_256;
  1054. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
  1055. "BIP-GMAC-256");
  1056. } else if (sel & WPA_CIPHER_BIP_CMAC_256) {
  1057. wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_CMAC_256;
  1058. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
  1059. "BIP-CMAC-256");
  1060. } else {
  1061. wpa_s->mgmt_group_cipher = 0;
  1062. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
  1063. }
  1064. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
  1065. wpa_s->mgmt_group_cipher);
  1066. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP,
  1067. (ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
  1068. wpa_s->conf->pmf : ssid->ieee80211w));
  1069. #endif /* CONFIG_IEEE80211W */
  1070. if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
  1071. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to generate WPA IE");
  1072. return -1;
  1073. }
  1074. if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt)) {
  1075. wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN, NULL);
  1076. #ifndef CONFIG_NO_PBKDF2
  1077. if (bss && ssid->bssid_set && ssid->ssid_len == 0 &&
  1078. ssid->passphrase) {
  1079. u8 psk[PMK_LEN];
  1080. pbkdf2_sha1(ssid->passphrase, bss->ssid, bss->ssid_len,
  1081. 4096, psk, PMK_LEN);
  1082. wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
  1083. psk, PMK_LEN);
  1084. wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN, NULL);
  1085. os_memset(psk, 0, sizeof(psk));
  1086. }
  1087. #endif /* CONFIG_NO_PBKDF2 */
  1088. #ifdef CONFIG_EXT_PASSWORD
  1089. if (ssid->ext_psk) {
  1090. struct wpabuf *pw = ext_password_get(wpa_s->ext_pw,
  1091. ssid->ext_psk);
  1092. char pw_str[64 + 1];
  1093. u8 psk[PMK_LEN];
  1094. if (pw == NULL) {
  1095. wpa_msg(wpa_s, MSG_INFO, "EXT PW: No PSK "
  1096. "found from external storage");
  1097. return -1;
  1098. }
  1099. if (wpabuf_len(pw) < 8 || wpabuf_len(pw) > 64) {
  1100. wpa_msg(wpa_s, MSG_INFO, "EXT PW: Unexpected "
  1101. "PSK length %d in external storage",
  1102. (int) wpabuf_len(pw));
  1103. ext_password_free(pw);
  1104. return -1;
  1105. }
  1106. os_memcpy(pw_str, wpabuf_head(pw), wpabuf_len(pw));
  1107. pw_str[wpabuf_len(pw)] = '\0';
  1108. #ifndef CONFIG_NO_PBKDF2
  1109. if (wpabuf_len(pw) >= 8 && wpabuf_len(pw) < 64 && bss)
  1110. {
  1111. pbkdf2_sha1(pw_str, bss->ssid, bss->ssid_len,
  1112. 4096, psk, PMK_LEN);
  1113. os_memset(pw_str, 0, sizeof(pw_str));
  1114. wpa_hexdump_key(MSG_MSGDUMP, "PSK (from "
  1115. "external passphrase)",
  1116. psk, PMK_LEN);
  1117. wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN, NULL);
  1118. os_memset(psk, 0, sizeof(psk));
  1119. } else
  1120. #endif /* CONFIG_NO_PBKDF2 */
  1121. if (wpabuf_len(pw) == 2 * PMK_LEN) {
  1122. if (hexstr2bin(pw_str, psk, PMK_LEN) < 0) {
  1123. wpa_msg(wpa_s, MSG_INFO, "EXT PW: "
  1124. "Invalid PSK hex string");
  1125. os_memset(pw_str, 0, sizeof(pw_str));
  1126. ext_password_free(pw);
  1127. return -1;
  1128. }
  1129. wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN, NULL);
  1130. os_memset(psk, 0, sizeof(psk));
  1131. } else {
  1132. wpa_msg(wpa_s, MSG_INFO, "EXT PW: No suitable "
  1133. "PSK available");
  1134. os_memset(pw_str, 0, sizeof(pw_str));
  1135. ext_password_free(pw);
  1136. return -1;
  1137. }
  1138. os_memset(pw_str, 0, sizeof(pw_str));
  1139. ext_password_free(pw);
  1140. }
  1141. #endif /* CONFIG_EXT_PASSWORD */
  1142. } else
  1143. wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
  1144. return 0;
  1145. }
  1146. static void wpas_ext_capab_byte(struct wpa_supplicant *wpa_s, u8 *pos, int idx)
  1147. {
  1148. *pos = 0x00;
  1149. switch (idx) {
  1150. case 0: /* Bits 0-7 */
  1151. break;
  1152. case 1: /* Bits 8-15 */
  1153. break;
  1154. case 2: /* Bits 16-23 */
  1155. #ifdef CONFIG_WNM
  1156. *pos |= 0x02; /* Bit 17 - WNM-Sleep Mode */
  1157. *pos |= 0x08; /* Bit 19 - BSS Transition */
  1158. #endif /* CONFIG_WNM */
  1159. break;
  1160. case 3: /* Bits 24-31 */
  1161. #ifdef CONFIG_WNM
  1162. *pos |= 0x02; /* Bit 25 - SSID List */
  1163. #endif /* CONFIG_WNM */
  1164. #ifdef CONFIG_INTERWORKING
  1165. if (wpa_s->conf->interworking)
  1166. *pos |= 0x80; /* Bit 31 - Interworking */
  1167. #endif /* CONFIG_INTERWORKING */
  1168. break;
  1169. case 4: /* Bits 32-39 */
  1170. #ifdef CONFIG_INTERWORKING
  1171. if (wpa_s->drv_flags / WPA_DRIVER_FLAGS_QOS_MAPPING)
  1172. *pos |= 0x01; /* Bit 32 - QoS Map */
  1173. #endif /* CONFIG_INTERWORKING */
  1174. break;
  1175. case 5: /* Bits 40-47 */
  1176. #ifdef CONFIG_HS20
  1177. if (wpa_s->conf->hs20)
  1178. *pos |= 0x40; /* Bit 46 - WNM-Notification */
  1179. #endif /* CONFIG_HS20 */
  1180. break;
  1181. case 6: /* Bits 48-55 */
  1182. break;
  1183. }
  1184. }
  1185. int wpas_build_ext_capab(struct wpa_supplicant *wpa_s, u8 *buf, size_t buflen)
  1186. {
  1187. u8 *pos = buf;
  1188. u8 len = 6, i;
  1189. if (len < wpa_s->extended_capa_len)
  1190. len = wpa_s->extended_capa_len;
  1191. if (buflen < (size_t) len + 2) {
  1192. wpa_printf(MSG_INFO,
  1193. "Not enough room for building extended capabilities element");
  1194. return -1;
  1195. }
  1196. *pos++ = WLAN_EID_EXT_CAPAB;
  1197. *pos++ = len;
  1198. for (i = 0; i < len; i++, pos++) {
  1199. wpas_ext_capab_byte(wpa_s, pos, i);
  1200. if (i < wpa_s->extended_capa_len) {
  1201. *pos &= ~wpa_s->extended_capa_mask[i];
  1202. *pos |= wpa_s->extended_capa[i];
  1203. }
  1204. }
  1205. while (len > 0 && buf[1 + len] == 0) {
  1206. len--;
  1207. buf[1] = len;
  1208. }
  1209. if (len == 0)
  1210. return 0;
  1211. return 2 + len;
  1212. }
  1213. static int wpas_valid_bss(struct wpa_supplicant *wpa_s,
  1214. struct wpa_bss *test_bss)
  1215. {
  1216. struct wpa_bss *bss;
  1217. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1218. if (bss == test_bss)
  1219. return 1;
  1220. }
  1221. return 0;
  1222. }
  1223. static int wpas_valid_ssid(struct wpa_supplicant *wpa_s,
  1224. struct wpa_ssid *test_ssid)
  1225. {
  1226. struct wpa_ssid *ssid;
  1227. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  1228. if (ssid == test_ssid)
  1229. return 1;
  1230. }
  1231. return 0;
  1232. }
  1233. int wpas_valid_bss_ssid(struct wpa_supplicant *wpa_s, struct wpa_bss *test_bss,
  1234. struct wpa_ssid *test_ssid)
  1235. {
  1236. if (test_bss && !wpas_valid_bss(wpa_s, test_bss))
  1237. return 0;
  1238. return test_ssid == NULL || wpas_valid_ssid(wpa_s, test_ssid);
  1239. }
  1240. void wpas_connect_work_free(struct wpa_connect_work *cwork)
  1241. {
  1242. if (cwork == NULL)
  1243. return;
  1244. os_free(cwork);
  1245. }
  1246. void wpas_connect_work_done(struct wpa_supplicant *wpa_s)
  1247. {
  1248. struct wpa_connect_work *cwork;
  1249. struct wpa_radio_work *work = wpa_s->connect_work;
  1250. if (!work)
  1251. return;
  1252. wpa_s->connect_work = NULL;
  1253. cwork = work->ctx;
  1254. work->ctx = NULL;
  1255. wpas_connect_work_free(cwork);
  1256. radio_work_done(work);
  1257. }
  1258. int wpas_update_random_addr(struct wpa_supplicant *wpa_s, int style)
  1259. {
  1260. struct os_reltime now;
  1261. u8 addr[ETH_ALEN];
  1262. os_get_reltime(&now);
  1263. if (wpa_s->last_mac_addr_style == style &&
  1264. wpa_s->last_mac_addr_change.sec != 0 &&
  1265. !os_reltime_expired(&now, &wpa_s->last_mac_addr_change,
  1266. wpa_s->conf->rand_addr_lifetime)) {
  1267. wpa_msg(wpa_s, MSG_DEBUG,
  1268. "Previously selected random MAC address has not yet expired");
  1269. return 0;
  1270. }
  1271. switch (style) {
  1272. case 1:
  1273. if (random_mac_addr(addr) < 0)
  1274. return -1;
  1275. break;
  1276. case 2:
  1277. os_memcpy(addr, wpa_s->perm_addr, ETH_ALEN);
  1278. if (random_mac_addr_keep_oui(addr) < 0)
  1279. return -1;
  1280. break;
  1281. default:
  1282. return -1;
  1283. }
  1284. if (wpa_drv_set_mac_addr(wpa_s, addr) < 0) {
  1285. wpa_msg(wpa_s, MSG_INFO,
  1286. "Failed to set random MAC address");
  1287. return -1;
  1288. }
  1289. os_get_reltime(&wpa_s->last_mac_addr_change);
  1290. wpa_s->mac_addr_changed = 1;
  1291. wpa_s->last_mac_addr_style = style;
  1292. if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
  1293. wpa_msg(wpa_s, MSG_INFO,
  1294. "Could not update MAC address information");
  1295. return -1;
  1296. }
  1297. wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
  1298. MAC2STR(addr));
  1299. return 0;
  1300. }
  1301. int wpas_update_random_addr_disassoc(struct wpa_supplicant *wpa_s)
  1302. {
  1303. if (wpa_s->wpa_state >= WPA_AUTHENTICATING ||
  1304. !wpa_s->conf->preassoc_mac_addr)
  1305. return 0;
  1306. return wpas_update_random_addr(wpa_s, wpa_s->conf->preassoc_mac_addr);
  1307. }
  1308. static void wpas_start_assoc_cb(struct wpa_radio_work *work, int deinit);
  1309. /**
  1310. * wpa_supplicant_associate - Request association
  1311. * @wpa_s: Pointer to wpa_supplicant data
  1312. * @bss: Scan results for the selected BSS, or %NULL if not available
  1313. * @ssid: Configuration data for the selected network
  1314. *
  1315. * This function is used to request %wpa_supplicant to associate with a BSS.
  1316. */
  1317. void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
  1318. struct wpa_bss *bss, struct wpa_ssid *ssid)
  1319. {
  1320. struct wpa_connect_work *cwork;
  1321. int rand_style;
  1322. if (ssid->mac_addr == -1)
  1323. rand_style = wpa_s->conf->mac_addr;
  1324. else
  1325. rand_style = ssid->mac_addr;
  1326. if (wpa_s->last_ssid == ssid) {
  1327. wpa_dbg(wpa_s, MSG_DEBUG, "Re-association to the same ESS");
  1328. } else if (rand_style > 0) {
  1329. if (wpas_update_random_addr(wpa_s, rand_style) < 0)
  1330. return;
  1331. wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
  1332. } else if (wpa_s->mac_addr_changed) {
  1333. if (wpa_drv_set_mac_addr(wpa_s, NULL) < 0) {
  1334. wpa_msg(wpa_s, MSG_INFO,
  1335. "Could not restore permanent MAC address");
  1336. return;
  1337. }
  1338. wpa_s->mac_addr_changed = 0;
  1339. if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
  1340. wpa_msg(wpa_s, MSG_INFO,
  1341. "Could not update MAC address information");
  1342. return;
  1343. }
  1344. wpa_msg(wpa_s, MSG_DEBUG, "Using permanent MAC address");
  1345. }
  1346. wpa_s->last_ssid = ssid;
  1347. #ifdef CONFIG_IBSS_RSN
  1348. ibss_rsn_deinit(wpa_s->ibss_rsn);
  1349. wpa_s->ibss_rsn = NULL;
  1350. #endif /* CONFIG_IBSS_RSN */
  1351. if (ssid->mode == WPAS_MODE_AP || ssid->mode == WPAS_MODE_P2P_GO ||
  1352. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
  1353. #ifdef CONFIG_AP
  1354. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
  1355. wpa_msg(wpa_s, MSG_INFO, "Driver does not support AP "
  1356. "mode");
  1357. return;
  1358. }
  1359. if (wpa_supplicant_create_ap(wpa_s, ssid) < 0) {
  1360. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  1361. if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
  1362. wpas_p2p_ap_setup_failed(wpa_s);
  1363. return;
  1364. }
  1365. wpa_s->current_bss = bss;
  1366. #else /* CONFIG_AP */
  1367. wpa_msg(wpa_s, MSG_ERROR, "AP mode support not included in "
  1368. "the build");
  1369. #endif /* CONFIG_AP */
  1370. return;
  1371. }
  1372. if (ssid->mode == WPAS_MODE_MESH) {
  1373. #ifdef CONFIG_MESH
  1374. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MESH)) {
  1375. wpa_msg(wpa_s, MSG_INFO,
  1376. "Driver does not support mesh mode");
  1377. return;
  1378. }
  1379. if (bss)
  1380. ssid->frequency = bss->freq;
  1381. if (wpa_supplicant_join_mesh(wpa_s, ssid) < 0) {
  1382. wpa_msg(wpa_s, MSG_ERROR, "Could not join mesh");
  1383. return;
  1384. }
  1385. wpa_s->current_bss = bss;
  1386. wpa_msg_ctrl(wpa_s, MSG_INFO, MESH_GROUP_STARTED
  1387. "ssid=\"%s\" id=%d",
  1388. wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
  1389. ssid->id);
  1390. #else /* CONFIG_MESH */
  1391. wpa_msg(wpa_s, MSG_ERROR,
  1392. "mesh mode support not included in the build");
  1393. #endif /* CONFIG_MESH */
  1394. return;
  1395. }
  1396. #ifdef CONFIG_TDLS
  1397. if (bss)
  1398. wpa_tdls_ap_ies(wpa_s->wpa, (const u8 *) (bss + 1),
  1399. bss->ie_len);
  1400. #endif /* CONFIG_TDLS */
  1401. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
  1402. ssid->mode == IEEE80211_MODE_INFRA) {
  1403. sme_authenticate(wpa_s, bss, ssid);
  1404. return;
  1405. }
  1406. if (wpa_s->connect_work) {
  1407. wpa_dbg(wpa_s, MSG_DEBUG, "Reject wpa_supplicant_associate() call since connect_work exist");
  1408. return;
  1409. }
  1410. if (radio_work_pending(wpa_s, "connect")) {
  1411. wpa_dbg(wpa_s, MSG_DEBUG, "Reject wpa_supplicant_associate() call since pending work exist");
  1412. return;
  1413. }
  1414. cwork = os_zalloc(sizeof(*cwork));
  1415. if (cwork == NULL)
  1416. return;
  1417. cwork->bss = bss;
  1418. cwork->ssid = ssid;
  1419. if (radio_add_work(wpa_s, bss ? bss->freq : 0, "connect", 1,
  1420. wpas_start_assoc_cb, cwork) < 0) {
  1421. os_free(cwork);
  1422. }
  1423. }
  1424. static void wpas_start_assoc_cb(struct wpa_radio_work *work, int deinit)
  1425. {
  1426. struct wpa_connect_work *cwork = work->ctx;
  1427. struct wpa_bss *bss = cwork->bss;
  1428. struct wpa_ssid *ssid = cwork->ssid;
  1429. struct wpa_supplicant *wpa_s = work->wpa_s;
  1430. u8 wpa_ie[200];
  1431. size_t wpa_ie_len;
  1432. int use_crypt, ret, i, bssid_changed;
  1433. int algs = WPA_AUTH_ALG_OPEN;
  1434. unsigned int cipher_pairwise, cipher_group;
  1435. struct wpa_driver_associate_params params;
  1436. int wep_keys_set = 0;
  1437. int assoc_failed = 0;
  1438. struct wpa_ssid *old_ssid;
  1439. #ifdef CONFIG_HT_OVERRIDES
  1440. struct ieee80211_ht_capabilities htcaps;
  1441. struct ieee80211_ht_capabilities htcaps_mask;
  1442. #endif /* CONFIG_HT_OVERRIDES */
  1443. #ifdef CONFIG_VHT_OVERRIDES
  1444. struct ieee80211_vht_capabilities vhtcaps;
  1445. struct ieee80211_vht_capabilities vhtcaps_mask;
  1446. #endif /* CONFIG_VHT_OVERRIDES */
  1447. if (deinit) {
  1448. if (work->started) {
  1449. wpa_s->connect_work = NULL;
  1450. /* cancel possible auth. timeout */
  1451. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s,
  1452. NULL);
  1453. }
  1454. wpas_connect_work_free(cwork);
  1455. return;
  1456. }
  1457. wpa_s->connect_work = work;
  1458. if (cwork->bss_removed || !wpas_valid_bss_ssid(wpa_s, bss, ssid)) {
  1459. wpa_dbg(wpa_s, MSG_DEBUG, "BSS/SSID entry for association not valid anymore - drop connection attempt");
  1460. wpas_connect_work_done(wpa_s);
  1461. return;
  1462. }
  1463. os_memset(&params, 0, sizeof(params));
  1464. wpa_s->reassociate = 0;
  1465. wpa_s->eap_expected_failure = 0;
  1466. if (bss &&
  1467. (!wpas_driver_bss_selection(wpa_s) || wpas_wps_searching(wpa_s))) {
  1468. #ifdef CONFIG_IEEE80211R
  1469. const u8 *ie, *md = NULL;
  1470. #endif /* CONFIG_IEEE80211R */
  1471. wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
  1472. " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
  1473. wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
  1474. bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
  1475. os_memset(wpa_s->bssid, 0, ETH_ALEN);
  1476. os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
  1477. if (bssid_changed)
  1478. wpas_notify_bssid_changed(wpa_s);
  1479. #ifdef CONFIG_IEEE80211R
  1480. ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
  1481. if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
  1482. md = ie + 2;
  1483. wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
  1484. if (md) {
  1485. /* Prepare for the next transition */
  1486. wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
  1487. }
  1488. #endif /* CONFIG_IEEE80211R */
  1489. #ifdef CONFIG_WPS
  1490. } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
  1491. wpa_s->conf->ap_scan == 2 &&
  1492. (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  1493. /* Use ap_scan==1 style network selection to find the network
  1494. */
  1495. wpas_connect_work_done(wpa_s);
  1496. wpa_s->scan_req = MANUAL_SCAN_REQ;
  1497. wpa_s->reassociate = 1;
  1498. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1499. return;
  1500. #endif /* CONFIG_WPS */
  1501. } else {
  1502. wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
  1503. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  1504. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  1505. }
  1506. wpa_supplicant_cancel_sched_scan(wpa_s);
  1507. wpa_supplicant_cancel_scan(wpa_s);
  1508. /* Starting new association, so clear the possibly used WPA IE from the
  1509. * previous association. */
  1510. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  1511. #ifdef IEEE8021X_EAPOL
  1512. if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1513. if (ssid->leap) {
  1514. if (ssid->non_leap == 0)
  1515. algs = WPA_AUTH_ALG_LEAP;
  1516. else
  1517. algs |= WPA_AUTH_ALG_LEAP;
  1518. }
  1519. }
  1520. #endif /* IEEE8021X_EAPOL */
  1521. wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
  1522. if (ssid->auth_alg) {
  1523. algs = ssid->auth_alg;
  1524. wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
  1525. "0x%x", algs);
  1526. }
  1527. if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
  1528. wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
  1529. wpa_key_mgmt_wpa(ssid->key_mgmt)) {
  1530. int try_opportunistic;
  1531. try_opportunistic = (ssid->proactive_key_caching < 0 ?
  1532. wpa_s->conf->okc :
  1533. ssid->proactive_key_caching) &&
  1534. (ssid->proto & WPA_PROTO_RSN);
  1535. if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
  1536. ssid, try_opportunistic) == 0)
  1537. eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
  1538. wpa_ie_len = sizeof(wpa_ie);
  1539. if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
  1540. wpa_ie, &wpa_ie_len)) {
  1541. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
  1542. "key management and encryption suites");
  1543. wpas_connect_work_done(wpa_s);
  1544. return;
  1545. }
  1546. } else if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && bss &&
  1547. wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt)) {
  1548. /*
  1549. * Both WPA and non-WPA IEEE 802.1X enabled in configuration -
  1550. * use non-WPA since the scan results did not indicate that the
  1551. * AP is using WPA or WPA2.
  1552. */
  1553. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  1554. wpa_ie_len = 0;
  1555. wpa_s->wpa_proto = 0;
  1556. } else if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
  1557. wpa_ie_len = sizeof(wpa_ie);
  1558. if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
  1559. wpa_ie, &wpa_ie_len)) {
  1560. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
  1561. "key management and encryption suites (no "
  1562. "scan results)");
  1563. wpas_connect_work_done(wpa_s);
  1564. return;
  1565. }
  1566. #ifdef CONFIG_WPS
  1567. } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
  1568. struct wpabuf *wps_ie;
  1569. wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
  1570. if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
  1571. wpa_ie_len = wpabuf_len(wps_ie);
  1572. os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
  1573. } else
  1574. wpa_ie_len = 0;
  1575. wpabuf_free(wps_ie);
  1576. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  1577. if (!bss || (bss->caps & IEEE80211_CAP_PRIVACY))
  1578. params.wps = WPS_MODE_PRIVACY;
  1579. else
  1580. params.wps = WPS_MODE_OPEN;
  1581. wpa_s->wpa_proto = 0;
  1582. #endif /* CONFIG_WPS */
  1583. } else {
  1584. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  1585. wpa_ie_len = 0;
  1586. wpa_s->wpa_proto = 0;
  1587. }
  1588. #ifdef CONFIG_P2P
  1589. if (wpa_s->global->p2p) {
  1590. u8 *pos;
  1591. size_t len;
  1592. int res;
  1593. pos = wpa_ie + wpa_ie_len;
  1594. len = sizeof(wpa_ie) - wpa_ie_len;
  1595. res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len,
  1596. ssid->p2p_group);
  1597. if (res >= 0)
  1598. wpa_ie_len += res;
  1599. }
  1600. wpa_s->cross_connect_disallowed = 0;
  1601. if (bss) {
  1602. struct wpabuf *p2p;
  1603. p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  1604. if (p2p) {
  1605. wpa_s->cross_connect_disallowed =
  1606. p2p_get_cross_connect_disallowed(p2p);
  1607. wpabuf_free(p2p);
  1608. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: WLAN AP %s cross "
  1609. "connection",
  1610. wpa_s->cross_connect_disallowed ?
  1611. "disallows" : "allows");
  1612. }
  1613. }
  1614. os_memset(wpa_s->p2p_ip_addr_info, 0, sizeof(wpa_s->p2p_ip_addr_info));
  1615. #endif /* CONFIG_P2P */
  1616. #ifdef CONFIG_HS20
  1617. if (is_hs20_network(wpa_s, ssid, bss)) {
  1618. struct wpabuf *hs20;
  1619. hs20 = wpabuf_alloc(20);
  1620. if (hs20) {
  1621. int pps_mo_id = hs20_get_pps_mo_id(wpa_s, ssid);
  1622. size_t len;
  1623. wpas_hs20_add_indication(hs20, pps_mo_id);
  1624. len = sizeof(wpa_ie) - wpa_ie_len;
  1625. if (wpabuf_len(hs20) <= len) {
  1626. os_memcpy(wpa_ie + wpa_ie_len,
  1627. wpabuf_head(hs20), wpabuf_len(hs20));
  1628. wpa_ie_len += wpabuf_len(hs20);
  1629. }
  1630. wpabuf_free(hs20);
  1631. }
  1632. }
  1633. #endif /* CONFIG_HS20 */
  1634. /*
  1635. * Workaround: Add Extended Capabilities element only if the AP
  1636. * included this element in Beacon/Probe Response frames. Some older
  1637. * APs seem to have interoperability issues if this element is
  1638. * included, so while the standard may require us to include the
  1639. * element in all cases, it is justifiable to skip it to avoid
  1640. * interoperability issues.
  1641. */
  1642. if (!bss || wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB)) {
  1643. u8 ext_capab[18];
  1644. int ext_capab_len;
  1645. ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
  1646. sizeof(ext_capab));
  1647. if (ext_capab_len > 0) {
  1648. u8 *pos = wpa_ie;
  1649. if (wpa_ie_len > 0 && pos[0] == WLAN_EID_RSN)
  1650. pos += 2 + pos[1];
  1651. os_memmove(pos + ext_capab_len, pos,
  1652. wpa_ie_len - (pos - wpa_ie));
  1653. wpa_ie_len += ext_capab_len;
  1654. os_memcpy(pos, ext_capab, ext_capab_len);
  1655. }
  1656. }
  1657. wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
  1658. use_crypt = 1;
  1659. cipher_pairwise = wpa_s->pairwise_cipher;
  1660. cipher_group = wpa_s->group_cipher;
  1661. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  1662. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1663. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
  1664. use_crypt = 0;
  1665. if (wpa_set_wep_keys(wpa_s, ssid)) {
  1666. use_crypt = 1;
  1667. wep_keys_set = 1;
  1668. }
  1669. }
  1670. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
  1671. use_crypt = 0;
  1672. #ifdef IEEE8021X_EAPOL
  1673. if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1674. if ((ssid->eapol_flags &
  1675. (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
  1676. EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
  1677. !wep_keys_set) {
  1678. use_crypt = 0;
  1679. } else {
  1680. /* Assume that dynamic WEP-104 keys will be used and
  1681. * set cipher suites in order for drivers to expect
  1682. * encryption. */
  1683. cipher_pairwise = cipher_group = WPA_CIPHER_WEP104;
  1684. }
  1685. }
  1686. #endif /* IEEE8021X_EAPOL */
  1687. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  1688. /* Set the key before (and later after) association */
  1689. wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
  1690. }
  1691. wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
  1692. if (bss) {
  1693. params.ssid = bss->ssid;
  1694. params.ssid_len = bss->ssid_len;
  1695. if (!wpas_driver_bss_selection(wpa_s) || ssid->bssid_set) {
  1696. wpa_printf(MSG_DEBUG, "Limit connection to BSSID "
  1697. MACSTR " freq=%u MHz based on scan results "
  1698. "(bssid_set=%d)",
  1699. MAC2STR(bss->bssid), bss->freq,
  1700. ssid->bssid_set);
  1701. params.bssid = bss->bssid;
  1702. params.freq.freq = bss->freq;
  1703. }
  1704. params.bssid_hint = bss->bssid;
  1705. params.freq_hint = bss->freq;
  1706. } else {
  1707. params.ssid = ssid->ssid;
  1708. params.ssid_len = ssid->ssid_len;
  1709. }
  1710. if (ssid->mode == WPAS_MODE_IBSS && ssid->bssid_set &&
  1711. wpa_s->conf->ap_scan == 2) {
  1712. params.bssid = ssid->bssid;
  1713. params.fixed_bssid = 1;
  1714. }
  1715. /* Initial frequency for IBSS/mesh */
  1716. if ((ssid->mode == WPAS_MODE_IBSS || ssid->mode == WPAS_MODE_MESH) &&
  1717. ssid->frequency > 0 && params.freq.freq == 0) {
  1718. enum hostapd_hw_mode hw_mode;
  1719. u8 channel;
  1720. params.freq.freq = ssid->frequency;
  1721. hw_mode = ieee80211_freq_to_chan(ssid->frequency, &channel);
  1722. for (i = 0; wpa_s->hw.modes && i < wpa_s->hw.num_modes; i++) {
  1723. if (wpa_s->hw.modes[i].mode == hw_mode) {
  1724. struct hostapd_hw_modes *mode;
  1725. mode = &wpa_s->hw.modes[i];
  1726. params.freq.ht_enabled = ht_supported(mode);
  1727. break;
  1728. }
  1729. }
  1730. }
  1731. if (ssid->mode == WPAS_MODE_IBSS) {
  1732. if (ssid->beacon_int)
  1733. params.beacon_int = ssid->beacon_int;
  1734. else
  1735. params.beacon_int = wpa_s->conf->beacon_int;
  1736. }
  1737. params.wpa_ie = wpa_ie;
  1738. params.wpa_ie_len = wpa_ie_len;
  1739. params.pairwise_suite = cipher_pairwise;
  1740. params.group_suite = cipher_group;
  1741. params.key_mgmt_suite = wpa_s->key_mgmt;
  1742. params.wpa_proto = wpa_s->wpa_proto;
  1743. params.auth_alg = algs;
  1744. params.mode = ssid->mode;
  1745. params.bg_scan_period = ssid->bg_scan_period;
  1746. for (i = 0; i < NUM_WEP_KEYS; i++) {
  1747. if (ssid->wep_key_len[i])
  1748. params.wep_key[i] = ssid->wep_key[i];
  1749. params.wep_key_len[i] = ssid->wep_key_len[i];
  1750. }
  1751. params.wep_tx_keyidx = ssid->wep_tx_keyidx;
  1752. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
  1753. (params.key_mgmt_suite == WPA_KEY_MGMT_PSK ||
  1754. params.key_mgmt_suite == WPA_KEY_MGMT_FT_PSK)) {
  1755. params.passphrase = ssid->passphrase;
  1756. if (ssid->psk_set)
  1757. params.psk = ssid->psk;
  1758. }
  1759. if (wpa_s->conf->key_mgmt_offload) {
  1760. if (params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
  1761. params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
  1762. params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B)
  1763. params.req_key_mgmt_offload =
  1764. ssid->proactive_key_caching < 0 ?
  1765. wpa_s->conf->okc : ssid->proactive_key_caching;
  1766. else
  1767. params.req_key_mgmt_offload = 1;
  1768. if ((params.key_mgmt_suite == WPA_KEY_MGMT_PSK ||
  1769. params.key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
  1770. params.key_mgmt_suite == WPA_KEY_MGMT_FT_PSK) &&
  1771. ssid->psk_set)
  1772. params.psk = ssid->psk;
  1773. }
  1774. params.drop_unencrypted = use_crypt;
  1775. #ifdef CONFIG_IEEE80211W
  1776. params.mgmt_frame_protection =
  1777. ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
  1778. wpa_s->conf->pmf : ssid->ieee80211w;
  1779. if (params.mgmt_frame_protection != NO_MGMT_FRAME_PROTECTION && bss) {
  1780. const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  1781. struct wpa_ie_data ie;
  1782. if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
  1783. ie.capabilities &
  1784. (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
  1785. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected AP supports "
  1786. "MFP: require MFP");
  1787. params.mgmt_frame_protection =
  1788. MGMT_FRAME_PROTECTION_REQUIRED;
  1789. }
  1790. }
  1791. #endif /* CONFIG_IEEE80211W */
  1792. params.p2p = ssid->p2p_group;
  1793. if (wpa_s->parent->set_sta_uapsd)
  1794. params.uapsd = wpa_s->parent->sta_uapsd;
  1795. else
  1796. params.uapsd = -1;
  1797. #ifdef CONFIG_HT_OVERRIDES
  1798. os_memset(&htcaps, 0, sizeof(htcaps));
  1799. os_memset(&htcaps_mask, 0, sizeof(htcaps_mask));
  1800. params.htcaps = (u8 *) &htcaps;
  1801. params.htcaps_mask = (u8 *) &htcaps_mask;
  1802. wpa_supplicant_apply_ht_overrides(wpa_s, ssid, &params);
  1803. #endif /* CONFIG_HT_OVERRIDES */
  1804. #ifdef CONFIG_VHT_OVERRIDES
  1805. os_memset(&vhtcaps, 0, sizeof(vhtcaps));
  1806. os_memset(&vhtcaps_mask, 0, sizeof(vhtcaps_mask));
  1807. params.vhtcaps = &vhtcaps;
  1808. params.vhtcaps_mask = &vhtcaps_mask;
  1809. wpa_supplicant_apply_vht_overrides(wpa_s, wpa_s->current_ssid, &params);
  1810. #endif /* CONFIG_VHT_OVERRIDES */
  1811. #ifdef CONFIG_P2P
  1812. /*
  1813. * If multi-channel concurrency is not supported, check for any
  1814. * frequency conflict. In case of any frequency conflict, remove the
  1815. * least prioritized connection.
  1816. */
  1817. if (wpa_s->num_multichan_concurrent < 2) {
  1818. int freq, num;
  1819. num = get_shared_radio_freqs(wpa_s, &freq, 1);
  1820. if (num > 0 && freq > 0 && freq != params.freq.freq) {
  1821. wpa_printf(MSG_DEBUG,
  1822. "Assoc conflicting freq found (%d != %d)",
  1823. freq, params.freq.freq);
  1824. if (wpas_p2p_handle_frequency_conflicts(
  1825. wpa_s, params.freq.freq, ssid) < 0) {
  1826. wpas_connect_work_done(wpa_s);
  1827. return;
  1828. }
  1829. }
  1830. }
  1831. #endif /* CONFIG_P2P */
  1832. ret = wpa_drv_associate(wpa_s, &params);
  1833. if (ret < 0) {
  1834. wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
  1835. "failed");
  1836. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SANE_ERROR_CODES) {
  1837. /*
  1838. * The driver is known to mean what is saying, so we
  1839. * can stop right here; the association will not
  1840. * succeed.
  1841. */
  1842. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  1843. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  1844. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  1845. return;
  1846. }
  1847. /* try to continue anyway; new association will be tried again
  1848. * after timeout */
  1849. assoc_failed = 1;
  1850. }
  1851. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  1852. /* Set the key after the association just in case association
  1853. * cleared the previously configured key. */
  1854. wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
  1855. /* No need to timeout authentication since there is no key
  1856. * management. */
  1857. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1858. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  1859. #ifdef CONFIG_IBSS_RSN
  1860. } else if (ssid->mode == WPAS_MODE_IBSS &&
  1861. wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
  1862. wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
  1863. /*
  1864. * RSN IBSS authentication is per-STA and we can disable the
  1865. * per-BSSID authentication.
  1866. */
  1867. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1868. #endif /* CONFIG_IBSS_RSN */
  1869. } else {
  1870. /* Timeout for IEEE 802.11 authentication and association */
  1871. int timeout = 60;
  1872. if (assoc_failed) {
  1873. /* give IBSS a bit more time */
  1874. timeout = ssid->mode == WPAS_MODE_IBSS ? 10 : 5;
  1875. } else if (wpa_s->conf->ap_scan == 1) {
  1876. /* give IBSS a bit more time */
  1877. timeout = ssid->mode == WPAS_MODE_IBSS ? 20 : 10;
  1878. }
  1879. wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
  1880. }
  1881. if (wep_keys_set &&
  1882. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC)) {
  1883. /* Set static WEP keys again */
  1884. wpa_set_wep_keys(wpa_s, ssid);
  1885. }
  1886. if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
  1887. /*
  1888. * Do not allow EAP session resumption between different
  1889. * network configurations.
  1890. */
  1891. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1892. }
  1893. old_ssid = wpa_s->current_ssid;
  1894. wpa_s->current_ssid = ssid;
  1895. wpa_s->current_bss = bss;
  1896. wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
  1897. wpa_supplicant_initiate_eapol(wpa_s);
  1898. if (old_ssid != wpa_s->current_ssid)
  1899. wpas_notify_network_changed(wpa_s);
  1900. }
  1901. static void wpa_supplicant_clear_connection(struct wpa_supplicant *wpa_s,
  1902. const u8 *addr)
  1903. {
  1904. struct wpa_ssid *old_ssid;
  1905. wpas_connect_work_done(wpa_s);
  1906. wpa_clear_keys(wpa_s, addr);
  1907. old_ssid = wpa_s->current_ssid;
  1908. wpa_supplicant_mark_disassoc(wpa_s);
  1909. wpa_sm_set_config(wpa_s->wpa, NULL);
  1910. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  1911. if (old_ssid != wpa_s->current_ssid)
  1912. wpas_notify_network_changed(wpa_s);
  1913. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  1914. }
  1915. /**
  1916. * wpa_supplicant_deauthenticate - Deauthenticate the current connection
  1917. * @wpa_s: Pointer to wpa_supplicant data
  1918. * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
  1919. *
  1920. * This function is used to request %wpa_supplicant to deauthenticate from the
  1921. * current AP.
  1922. */
  1923. void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
  1924. int reason_code)
  1925. {
  1926. u8 *addr = NULL;
  1927. union wpa_event_data event;
  1928. int zero_addr = 0;
  1929. wpa_dbg(wpa_s, MSG_DEBUG, "Request to deauthenticate - bssid=" MACSTR
  1930. " pending_bssid=" MACSTR " reason=%d state=%s",
  1931. MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
  1932. reason_code, wpa_supplicant_state_txt(wpa_s->wpa_state));
  1933. if (!is_zero_ether_addr(wpa_s->bssid))
  1934. addr = wpa_s->bssid;
  1935. else if (!is_zero_ether_addr(wpa_s->pending_bssid) &&
  1936. (wpa_s->wpa_state == WPA_AUTHENTICATING ||
  1937. wpa_s->wpa_state == WPA_ASSOCIATING))
  1938. addr = wpa_s->pending_bssid;
  1939. else if (wpa_s->wpa_state == WPA_ASSOCIATING) {
  1940. /*
  1941. * When using driver-based BSS selection, we may not know the
  1942. * BSSID with which we are currently trying to associate. We
  1943. * need to notify the driver of this disconnection even in such
  1944. * a case, so use the all zeros address here.
  1945. */
  1946. addr = wpa_s->bssid;
  1947. zero_addr = 1;
  1948. }
  1949. #ifdef CONFIG_TDLS
  1950. wpa_tdls_teardown_peers(wpa_s->wpa);
  1951. #endif /* CONFIG_TDLS */
  1952. #ifdef CONFIG_MESH
  1953. if (wpa_s->ifmsh) {
  1954. wpa_msg_ctrl(wpa_s, MSG_INFO, MESH_GROUP_REMOVED "%s",
  1955. wpa_s->ifname);
  1956. wpa_supplicant_leave_mesh(wpa_s);
  1957. }
  1958. #endif /* CONFIG_MESH */
  1959. if (addr) {
  1960. wpa_drv_deauthenticate(wpa_s, addr, reason_code);
  1961. os_memset(&event, 0, sizeof(event));
  1962. event.deauth_info.reason_code = (u16) reason_code;
  1963. event.deauth_info.locally_generated = 1;
  1964. wpa_supplicant_event(wpa_s, EVENT_DEAUTH, &event);
  1965. if (zero_addr)
  1966. addr = NULL;
  1967. }
  1968. wpa_supplicant_clear_connection(wpa_s, addr);
  1969. }
  1970. static void wpa_supplicant_enable_one_network(struct wpa_supplicant *wpa_s,
  1971. struct wpa_ssid *ssid)
  1972. {
  1973. if (!ssid || !ssid->disabled || ssid->disabled == 2)
  1974. return;
  1975. ssid->disabled = 0;
  1976. wpas_clear_temp_disabled(wpa_s, ssid, 1);
  1977. wpas_notify_network_enabled_changed(wpa_s, ssid);
  1978. /*
  1979. * Try to reassociate since there is no current configuration and a new
  1980. * network was made available.
  1981. */
  1982. if (!wpa_s->current_ssid && !wpa_s->disconnected)
  1983. wpa_s->reassociate = 1;
  1984. }
  1985. /**
  1986. * wpa_supplicant_enable_network - Mark a configured network as enabled
  1987. * @wpa_s: wpa_supplicant structure for a network interface
  1988. * @ssid: wpa_ssid structure for a configured network or %NULL
  1989. *
  1990. * Enables the specified network or all networks if no network specified.
  1991. */
  1992. void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
  1993. struct wpa_ssid *ssid)
  1994. {
  1995. if (ssid == NULL) {
  1996. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
  1997. wpa_supplicant_enable_one_network(wpa_s, ssid);
  1998. } else
  1999. wpa_supplicant_enable_one_network(wpa_s, ssid);
  2000. if (wpa_s->reassociate && !wpa_s->disconnected) {
  2001. if (wpa_s->sched_scanning) {
  2002. wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to add "
  2003. "new network to scan filters");
  2004. wpa_supplicant_cancel_sched_scan(wpa_s);
  2005. }
  2006. if (wpa_supplicant_fast_associate(wpa_s) != 1)
  2007. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2008. }
  2009. }
  2010. /**
  2011. * wpa_supplicant_disable_network - Mark a configured network as disabled
  2012. * @wpa_s: wpa_supplicant structure for a network interface
  2013. * @ssid: wpa_ssid structure for a configured network or %NULL
  2014. *
  2015. * Disables the specified network or all networks if no network specified.
  2016. */
  2017. void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
  2018. struct wpa_ssid *ssid)
  2019. {
  2020. struct wpa_ssid *other_ssid;
  2021. int was_disabled;
  2022. if (ssid == NULL) {
  2023. if (wpa_s->sched_scanning)
  2024. wpa_supplicant_cancel_sched_scan(wpa_s);
  2025. for (other_ssid = wpa_s->conf->ssid; other_ssid;
  2026. other_ssid = other_ssid->next) {
  2027. was_disabled = other_ssid->disabled;
  2028. if (was_disabled == 2)
  2029. continue; /* do not change persistent P2P group
  2030. * data */
  2031. other_ssid->disabled = 1;
  2032. if (was_disabled != other_ssid->disabled)
  2033. wpas_notify_network_enabled_changed(
  2034. wpa_s, other_ssid);
  2035. }
  2036. if (wpa_s->current_ssid)
  2037. wpa_supplicant_deauthenticate(
  2038. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  2039. } else if (ssid->disabled != 2) {
  2040. if (ssid == wpa_s->current_ssid)
  2041. wpa_supplicant_deauthenticate(
  2042. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  2043. was_disabled = ssid->disabled;
  2044. ssid->disabled = 1;
  2045. if (was_disabled != ssid->disabled) {
  2046. wpas_notify_network_enabled_changed(wpa_s, ssid);
  2047. if (wpa_s->sched_scanning) {
  2048. wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan "
  2049. "to remove network from filters");
  2050. wpa_supplicant_cancel_sched_scan(wpa_s);
  2051. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2052. }
  2053. }
  2054. }
  2055. }
  2056. /**
  2057. * wpa_supplicant_select_network - Attempt association with a network
  2058. * @wpa_s: wpa_supplicant structure for a network interface
  2059. * @ssid: wpa_ssid structure for a configured network or %NULL for any network
  2060. */
  2061. void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
  2062. struct wpa_ssid *ssid)
  2063. {
  2064. struct wpa_ssid *other_ssid;
  2065. int disconnected = 0;
  2066. if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid) {
  2067. wpa_supplicant_deauthenticate(
  2068. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  2069. disconnected = 1;
  2070. }
  2071. if (ssid)
  2072. wpas_clear_temp_disabled(wpa_s, ssid, 1);
  2073. /*
  2074. * Mark all other networks disabled or mark all networks enabled if no
  2075. * network specified.
  2076. */
  2077. for (other_ssid = wpa_s->conf->ssid; other_ssid;
  2078. other_ssid = other_ssid->next) {
  2079. int was_disabled = other_ssid->disabled;
  2080. if (was_disabled == 2)
  2081. continue; /* do not change persistent P2P group data */
  2082. other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
  2083. if (was_disabled && !other_ssid->disabled)
  2084. wpas_clear_temp_disabled(wpa_s, other_ssid, 0);
  2085. if (was_disabled != other_ssid->disabled)
  2086. wpas_notify_network_enabled_changed(wpa_s, other_ssid);
  2087. }
  2088. if (ssid && ssid == wpa_s->current_ssid && wpa_s->current_ssid) {
  2089. /* We are already associated with the selected network */
  2090. wpa_printf(MSG_DEBUG, "Already associated with the "
  2091. "selected network - do nothing");
  2092. return;
  2093. }
  2094. if (ssid) {
  2095. wpa_s->current_ssid = ssid;
  2096. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  2097. wpa_s->connect_without_scan =
  2098. (ssid->mode == WPAS_MODE_MESH) ? ssid : NULL;
  2099. } else {
  2100. wpa_s->connect_without_scan = NULL;
  2101. }
  2102. wpa_s->disconnected = 0;
  2103. wpa_s->reassociate = 1;
  2104. if (wpa_s->connect_without_scan ||
  2105. wpa_supplicant_fast_associate(wpa_s) != 1)
  2106. wpa_supplicant_req_scan(wpa_s, 0, disconnected ? 100000 : 0);
  2107. if (ssid)
  2108. wpas_notify_network_selected(wpa_s, ssid);
  2109. }
  2110. /**
  2111. * wpas_set_pkcs11_engine_and_module_path - Set PKCS #11 engine and module path
  2112. * @wpa_s: wpa_supplicant structure for a network interface
  2113. * @pkcs11_engine_path: PKCS #11 engine path or NULL
  2114. * @pkcs11_module_path: PKCS #11 module path or NULL
  2115. * Returns: 0 on success; -1 on failure
  2116. *
  2117. * Sets the PKCS #11 engine and module path. Both have to be NULL or a valid
  2118. * path. If resetting the EAPOL state machine with the new PKCS #11 engine and
  2119. * module path fails the paths will be reset to the default value (NULL).
  2120. */
  2121. int wpas_set_pkcs11_engine_and_module_path(struct wpa_supplicant *wpa_s,
  2122. const char *pkcs11_engine_path,
  2123. const char *pkcs11_module_path)
  2124. {
  2125. char *pkcs11_engine_path_copy = NULL;
  2126. char *pkcs11_module_path_copy = NULL;
  2127. if (pkcs11_engine_path != NULL) {
  2128. pkcs11_engine_path_copy = os_strdup(pkcs11_engine_path);
  2129. if (pkcs11_engine_path_copy == NULL)
  2130. return -1;
  2131. }
  2132. if (pkcs11_module_path != NULL) {
  2133. pkcs11_module_path_copy = os_strdup(pkcs11_module_path);
  2134. if (pkcs11_module_path_copy == NULL) {
  2135. os_free(pkcs11_engine_path_copy);
  2136. return -1;
  2137. }
  2138. }
  2139. os_free(wpa_s->conf->pkcs11_engine_path);
  2140. os_free(wpa_s->conf->pkcs11_module_path);
  2141. wpa_s->conf->pkcs11_engine_path = pkcs11_engine_path_copy;
  2142. wpa_s->conf->pkcs11_module_path = pkcs11_module_path_copy;
  2143. wpa_sm_set_eapol(wpa_s->wpa, NULL);
  2144. eapol_sm_deinit(wpa_s->eapol);
  2145. wpa_s->eapol = NULL;
  2146. if (wpa_supplicant_init_eapol(wpa_s)) {
  2147. /* Error -> Reset paths to the default value (NULL) once. */
  2148. if (pkcs11_engine_path != NULL && pkcs11_module_path != NULL)
  2149. wpas_set_pkcs11_engine_and_module_path(wpa_s, NULL,
  2150. NULL);
  2151. return -1;
  2152. }
  2153. wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
  2154. return 0;
  2155. }
  2156. /**
  2157. * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
  2158. * @wpa_s: wpa_supplicant structure for a network interface
  2159. * @ap_scan: AP scan mode
  2160. * Returns: 0 if succeed or -1 if ap_scan has an invalid value
  2161. *
  2162. */
  2163. int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
  2164. {
  2165. int old_ap_scan;
  2166. if (ap_scan < 0 || ap_scan > 2)
  2167. return -1;
  2168. #ifdef ANDROID
  2169. if (ap_scan == 2 && ap_scan != wpa_s->conf->ap_scan &&
  2170. wpa_s->wpa_state >= WPA_ASSOCIATING &&
  2171. wpa_s->wpa_state < WPA_COMPLETED) {
  2172. wpa_printf(MSG_ERROR, "ap_scan = %d (%d) rejected while "
  2173. "associating", wpa_s->conf->ap_scan, ap_scan);
  2174. return 0;
  2175. }
  2176. #endif /* ANDROID */
  2177. old_ap_scan = wpa_s->conf->ap_scan;
  2178. wpa_s->conf->ap_scan = ap_scan;
  2179. if (old_ap_scan != wpa_s->conf->ap_scan)
  2180. wpas_notify_ap_scan_changed(wpa_s);
  2181. return 0;
  2182. }
  2183. /**
  2184. * wpa_supplicant_set_bss_expiration_age - Set BSS entry expiration age
  2185. * @wpa_s: wpa_supplicant structure for a network interface
  2186. * @expire_age: Expiration age in seconds
  2187. * Returns: 0 if succeed or -1 if expire_age has an invalid value
  2188. *
  2189. */
  2190. int wpa_supplicant_set_bss_expiration_age(struct wpa_supplicant *wpa_s,
  2191. unsigned int bss_expire_age)
  2192. {
  2193. if (bss_expire_age < 10) {
  2194. wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration age %u",
  2195. bss_expire_age);
  2196. return -1;
  2197. }
  2198. wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration age: %d sec",
  2199. bss_expire_age);
  2200. wpa_s->conf->bss_expiration_age = bss_expire_age;
  2201. return 0;
  2202. }
  2203. /**
  2204. * wpa_supplicant_set_bss_expiration_count - Set BSS entry expiration scan count
  2205. * @wpa_s: wpa_supplicant structure for a network interface
  2206. * @expire_count: number of scans after which an unseen BSS is reclaimed
  2207. * Returns: 0 if succeed or -1 if expire_count has an invalid value
  2208. *
  2209. */
  2210. int wpa_supplicant_set_bss_expiration_count(struct wpa_supplicant *wpa_s,
  2211. unsigned int bss_expire_count)
  2212. {
  2213. if (bss_expire_count < 1) {
  2214. wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration count %u",
  2215. bss_expire_count);
  2216. return -1;
  2217. }
  2218. wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration scan count: %u",
  2219. bss_expire_count);
  2220. wpa_s->conf->bss_expiration_scan_count = bss_expire_count;
  2221. return 0;
  2222. }
  2223. /**
  2224. * wpa_supplicant_set_scan_interval - Set scan interval
  2225. * @wpa_s: wpa_supplicant structure for a network interface
  2226. * @scan_interval: scan interval in seconds
  2227. * Returns: 0 if succeed or -1 if scan_interval has an invalid value
  2228. *
  2229. */
  2230. int wpa_supplicant_set_scan_interval(struct wpa_supplicant *wpa_s,
  2231. int scan_interval)
  2232. {
  2233. if (scan_interval < 0) {
  2234. wpa_msg(wpa_s, MSG_ERROR, "Invalid scan interval %d",
  2235. scan_interval);
  2236. return -1;
  2237. }
  2238. wpa_msg(wpa_s, MSG_DEBUG, "Setting scan interval: %d sec",
  2239. scan_interval);
  2240. wpa_supplicant_update_scan_int(wpa_s, scan_interval);
  2241. return 0;
  2242. }
  2243. /**
  2244. * wpa_supplicant_set_debug_params - Set global debug params
  2245. * @global: wpa_global structure
  2246. * @debug_level: debug level
  2247. * @debug_timestamp: determines if show timestamp in debug data
  2248. * @debug_show_keys: determines if show keys in debug data
  2249. * Returns: 0 if succeed or -1 if debug_level has wrong value
  2250. */
  2251. int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
  2252. int debug_timestamp, int debug_show_keys)
  2253. {
  2254. int old_level, old_timestamp, old_show_keys;
  2255. /* check for allowed debuglevels */
  2256. if (debug_level != MSG_EXCESSIVE &&
  2257. debug_level != MSG_MSGDUMP &&
  2258. debug_level != MSG_DEBUG &&
  2259. debug_level != MSG_INFO &&
  2260. debug_level != MSG_WARNING &&
  2261. debug_level != MSG_ERROR)
  2262. return -1;
  2263. old_level = wpa_debug_level;
  2264. old_timestamp = wpa_debug_timestamp;
  2265. old_show_keys = wpa_debug_show_keys;
  2266. wpa_debug_level = debug_level;
  2267. wpa_debug_timestamp = debug_timestamp ? 1 : 0;
  2268. wpa_debug_show_keys = debug_show_keys ? 1 : 0;
  2269. if (wpa_debug_level != old_level)
  2270. wpas_notify_debug_level_changed(global);
  2271. if (wpa_debug_timestamp != old_timestamp)
  2272. wpas_notify_debug_timestamp_changed(global);
  2273. if (wpa_debug_show_keys != old_show_keys)
  2274. wpas_notify_debug_show_keys_changed(global);
  2275. return 0;
  2276. }
  2277. /**
  2278. * wpa_supplicant_get_ssid - Get a pointer to the current network structure
  2279. * @wpa_s: Pointer to wpa_supplicant data
  2280. * Returns: A pointer to the current network structure or %NULL on failure
  2281. */
  2282. struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
  2283. {
  2284. struct wpa_ssid *entry;
  2285. u8 ssid[MAX_SSID_LEN];
  2286. int res;
  2287. size_t ssid_len;
  2288. u8 bssid[ETH_ALEN];
  2289. int wired;
  2290. res = wpa_drv_get_ssid(wpa_s, ssid);
  2291. if (res < 0) {
  2292. wpa_msg(wpa_s, MSG_WARNING, "Could not read SSID from "
  2293. "driver");
  2294. return NULL;
  2295. }
  2296. ssid_len = res;
  2297. if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
  2298. wpa_msg(wpa_s, MSG_WARNING, "Could not read BSSID from "
  2299. "driver");
  2300. return NULL;
  2301. }
  2302. wired = wpa_s->conf->ap_scan == 0 &&
  2303. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
  2304. entry = wpa_s->conf->ssid;
  2305. while (entry) {
  2306. if (!wpas_network_disabled(wpa_s, entry) &&
  2307. ((ssid_len == entry->ssid_len &&
  2308. os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
  2309. (!entry->bssid_set ||
  2310. os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
  2311. return entry;
  2312. #ifdef CONFIG_WPS
  2313. if (!wpas_network_disabled(wpa_s, entry) &&
  2314. (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
  2315. (entry->ssid == NULL || entry->ssid_len == 0) &&
  2316. (!entry->bssid_set ||
  2317. os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
  2318. return entry;
  2319. #endif /* CONFIG_WPS */
  2320. if (!wpas_network_disabled(wpa_s, entry) && entry->bssid_set &&
  2321. entry->ssid_len == 0 &&
  2322. os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0)
  2323. return entry;
  2324. entry = entry->next;
  2325. }
  2326. return NULL;
  2327. }
  2328. static int select_driver(struct wpa_supplicant *wpa_s, int i)
  2329. {
  2330. struct wpa_global *global = wpa_s->global;
  2331. if (wpa_drivers[i]->global_init && global->drv_priv[i] == NULL) {
  2332. global->drv_priv[i] = wpa_drivers[i]->global_init();
  2333. if (global->drv_priv[i] == NULL) {
  2334. wpa_printf(MSG_ERROR, "Failed to initialize driver "
  2335. "'%s'", wpa_drivers[i]->name);
  2336. return -1;
  2337. }
  2338. }
  2339. wpa_s->driver = wpa_drivers[i];
  2340. wpa_s->global_drv_priv = global->drv_priv[i];
  2341. return 0;
  2342. }
  2343. static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
  2344. const char *name)
  2345. {
  2346. int i;
  2347. size_t len;
  2348. const char *pos, *driver = name;
  2349. if (wpa_s == NULL)
  2350. return -1;
  2351. if (wpa_drivers[0] == NULL) {
  2352. wpa_msg(wpa_s, MSG_ERROR, "No driver interfaces build into "
  2353. "wpa_supplicant");
  2354. return -1;
  2355. }
  2356. if (name == NULL) {
  2357. /* default to first driver in the list */
  2358. return select_driver(wpa_s, 0);
  2359. }
  2360. do {
  2361. pos = os_strchr(driver, ',');
  2362. if (pos)
  2363. len = pos - driver;
  2364. else
  2365. len = os_strlen(driver);
  2366. for (i = 0; wpa_drivers[i]; i++) {
  2367. if (os_strlen(wpa_drivers[i]->name) == len &&
  2368. os_strncmp(driver, wpa_drivers[i]->name, len) ==
  2369. 0) {
  2370. /* First driver that succeeds wins */
  2371. if (select_driver(wpa_s, i) == 0)
  2372. return 0;
  2373. }
  2374. }
  2375. driver = pos + 1;
  2376. } while (pos);
  2377. wpa_msg(wpa_s, MSG_ERROR, "Unsupported driver '%s'", name);
  2378. return -1;
  2379. }
  2380. /**
  2381. * wpa_supplicant_rx_eapol - Deliver a received EAPOL frame to wpa_supplicant
  2382. * @ctx: Context pointer (wpa_s); this is the ctx variable registered
  2383. * with struct wpa_driver_ops::init()
  2384. * @src_addr: Source address of the EAPOL frame
  2385. * @buf: EAPOL data starting from the EAPOL header (i.e., no Ethernet header)
  2386. * @len: Length of the EAPOL data
  2387. *
  2388. * This function is called for each received EAPOL frame. Most driver
  2389. * interfaces rely on more generic OS mechanism for receiving frames through
  2390. * l2_packet, but if such a mechanism is not available, the driver wrapper may
  2391. * take care of received EAPOL frames and deliver them to the core supplicant
  2392. * code by calling this function.
  2393. */
  2394. void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
  2395. const u8 *buf, size_t len)
  2396. {
  2397. struct wpa_supplicant *wpa_s = ctx;
  2398. wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
  2399. wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
  2400. #ifdef CONFIG_PEERKEY
  2401. if (wpa_s->wpa_state > WPA_ASSOCIATED && wpa_s->current_ssid &&
  2402. wpa_s->current_ssid->peerkey &&
  2403. !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
  2404. wpa_sm_rx_eapol_peerkey(wpa_s->wpa, src_addr, buf, len) == 1) {
  2405. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: Processed PeerKey EAPOL-Key");
  2406. return;
  2407. }
  2408. #endif /* CONFIG_PEERKEY */
  2409. if (wpa_s->wpa_state < WPA_ASSOCIATED ||
  2410. (wpa_s->last_eapol_matches_bssid &&
  2411. #ifdef CONFIG_AP
  2412. !wpa_s->ap_iface &&
  2413. #endif /* CONFIG_AP */
  2414. os_memcmp(src_addr, wpa_s->bssid, ETH_ALEN) != 0)) {
  2415. /*
  2416. * There is possible race condition between receiving the
  2417. * association event and the EAPOL frame since they are coming
  2418. * through different paths from the driver. In order to avoid
  2419. * issues in trying to process the EAPOL frame before receiving
  2420. * association information, lets queue it for processing until
  2421. * the association event is received. This may also be needed in
  2422. * driver-based roaming case, so also use src_addr != BSSID as a
  2423. * trigger if we have previously confirmed that the
  2424. * Authenticator uses BSSID as the src_addr (which is not the
  2425. * case with wired IEEE 802.1X).
  2426. */
  2427. wpa_dbg(wpa_s, MSG_DEBUG, "Not associated - Delay processing "
  2428. "of received EAPOL frame (state=%s bssid=" MACSTR ")",
  2429. wpa_supplicant_state_txt(wpa_s->wpa_state),
  2430. MAC2STR(wpa_s->bssid));
  2431. wpabuf_free(wpa_s->pending_eapol_rx);
  2432. wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
  2433. if (wpa_s->pending_eapol_rx) {
  2434. os_get_reltime(&wpa_s->pending_eapol_rx_time);
  2435. os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
  2436. ETH_ALEN);
  2437. }
  2438. return;
  2439. }
  2440. wpa_s->last_eapol_matches_bssid =
  2441. os_memcmp(src_addr, wpa_s->bssid, ETH_ALEN) == 0;
  2442. #ifdef CONFIG_AP
  2443. if (wpa_s->ap_iface) {
  2444. wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
  2445. return;
  2446. }
  2447. #endif /* CONFIG_AP */
  2448. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
  2449. wpa_dbg(wpa_s, MSG_DEBUG, "Ignored received EAPOL frame since "
  2450. "no key management is configured");
  2451. return;
  2452. }
  2453. if (wpa_s->eapol_received == 0 &&
  2454. (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
  2455. !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
  2456. wpa_s->wpa_state != WPA_COMPLETED) &&
  2457. (wpa_s->current_ssid == NULL ||
  2458. wpa_s->current_ssid->mode != IEEE80211_MODE_IBSS)) {
  2459. /* Timeout for completing IEEE 802.1X and WPA authentication */
  2460. wpa_supplicant_req_auth_timeout(
  2461. wpa_s,
  2462. (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
  2463. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
  2464. wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
  2465. 70 : 10, 0);
  2466. }
  2467. wpa_s->eapol_received++;
  2468. if (wpa_s->countermeasures) {
  2469. wpa_msg(wpa_s, MSG_INFO, "WPA: Countermeasures - dropped "
  2470. "EAPOL packet");
  2471. return;
  2472. }
  2473. #ifdef CONFIG_IBSS_RSN
  2474. if (wpa_s->current_ssid &&
  2475. wpa_s->current_ssid->mode == WPAS_MODE_IBSS) {
  2476. ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
  2477. return;
  2478. }
  2479. #endif /* CONFIG_IBSS_RSN */
  2480. /* Source address of the incoming EAPOL frame could be compared to the
  2481. * current BSSID. However, it is possible that a centralized
  2482. * Authenticator could be using another MAC address than the BSSID of
  2483. * an AP, so just allow any address to be used for now. The replies are
  2484. * still sent to the current BSSID (if available), though. */
  2485. os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
  2486. if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
  2487. eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
  2488. return;
  2489. wpa_drv_poll(wpa_s);
  2490. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
  2491. wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
  2492. else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
  2493. /*
  2494. * Set portValid = TRUE here since we are going to skip 4-way
  2495. * handshake processing which would normally set portValid. We
  2496. * need this to allow the EAPOL state machines to be completed
  2497. * without going through EAPOL-Key handshake.
  2498. */
  2499. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  2500. }
  2501. }
  2502. int wpa_supplicant_update_mac_addr(struct wpa_supplicant *wpa_s)
  2503. {
  2504. if ((!wpa_s->p2p_mgmt ||
  2505. !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) &&
  2506. !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)) {
  2507. l2_packet_deinit(wpa_s->l2);
  2508. wpa_s->l2 = l2_packet_init(wpa_s->ifname,
  2509. wpa_drv_get_mac_addr(wpa_s),
  2510. ETH_P_EAPOL,
  2511. wpa_supplicant_rx_eapol, wpa_s, 0);
  2512. if (wpa_s->l2 == NULL)
  2513. return -1;
  2514. } else {
  2515. const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
  2516. if (addr)
  2517. os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
  2518. }
  2519. if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
  2520. wpa_msg(wpa_s, MSG_ERROR, "Failed to get own L2 address");
  2521. return -1;
  2522. }
  2523. wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
  2524. return 0;
  2525. }
  2526. static void wpa_supplicant_rx_eapol_bridge(void *ctx, const u8 *src_addr,
  2527. const u8 *buf, size_t len)
  2528. {
  2529. struct wpa_supplicant *wpa_s = ctx;
  2530. const struct l2_ethhdr *eth;
  2531. if (len < sizeof(*eth))
  2532. return;
  2533. eth = (const struct l2_ethhdr *) buf;
  2534. if (os_memcmp(eth->h_dest, wpa_s->own_addr, ETH_ALEN) != 0 &&
  2535. !(eth->h_dest[0] & 0x01)) {
  2536. wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
  2537. " (bridge - not for this interface - ignore)",
  2538. MAC2STR(src_addr), MAC2STR(eth->h_dest));
  2539. return;
  2540. }
  2541. wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
  2542. " (bridge)", MAC2STR(src_addr), MAC2STR(eth->h_dest));
  2543. wpa_supplicant_rx_eapol(wpa_s, src_addr, buf + sizeof(*eth),
  2544. len - sizeof(*eth));
  2545. }
  2546. /**
  2547. * wpa_supplicant_driver_init - Initialize driver interface parameters
  2548. * @wpa_s: Pointer to wpa_supplicant data
  2549. * Returns: 0 on success, -1 on failure
  2550. *
  2551. * This function is called to initialize driver interface parameters.
  2552. * wpa_drv_init() must have been called before this function to initialize the
  2553. * driver interface.
  2554. */
  2555. int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
  2556. {
  2557. static int interface_count = 0;
  2558. if (wpa_supplicant_update_mac_addr(wpa_s) < 0)
  2559. return -1;
  2560. wpa_dbg(wpa_s, MSG_DEBUG, "Own MAC address: " MACSTR,
  2561. MAC2STR(wpa_s->own_addr));
  2562. os_memcpy(wpa_s->perm_addr, wpa_s->own_addr, ETH_ALEN);
  2563. wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
  2564. if (wpa_s->bridge_ifname[0]) {
  2565. wpa_dbg(wpa_s, MSG_DEBUG, "Receiving packets from bridge "
  2566. "interface '%s'", wpa_s->bridge_ifname);
  2567. wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
  2568. wpa_s->own_addr,
  2569. ETH_P_EAPOL,
  2570. wpa_supplicant_rx_eapol_bridge,
  2571. wpa_s, 1);
  2572. if (wpa_s->l2_br == NULL) {
  2573. wpa_msg(wpa_s, MSG_ERROR, "Failed to open l2_packet "
  2574. "connection for the bridge interface '%s'",
  2575. wpa_s->bridge_ifname);
  2576. return -1;
  2577. }
  2578. }
  2579. wpa_clear_keys(wpa_s, NULL);
  2580. /* Make sure that TKIP countermeasures are not left enabled (could
  2581. * happen if wpa_supplicant is killed during countermeasures. */
  2582. wpa_drv_set_countermeasures(wpa_s, 0);
  2583. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: flushing PMKID list in the driver");
  2584. wpa_drv_flush_pmkid(wpa_s);
  2585. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  2586. wpa_s->prev_scan_wildcard = 0;
  2587. if (wpa_supplicant_enabled_networks(wpa_s)) {
  2588. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  2589. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  2590. interface_count = 0;
  2591. }
  2592. #ifndef ANDROID
  2593. if (!wpa_s->p2p_mgmt &&
  2594. wpa_supplicant_delayed_sched_scan(wpa_s,
  2595. interface_count % 3,
  2596. 100000))
  2597. wpa_supplicant_req_scan(wpa_s, interface_count % 3,
  2598. 100000);
  2599. #endif /* ANDROID */
  2600. interface_count++;
  2601. } else
  2602. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  2603. return 0;
  2604. }
  2605. static int wpa_supplicant_daemon(const char *pid_file)
  2606. {
  2607. wpa_printf(MSG_DEBUG, "Daemonize..");
  2608. return os_daemonize(pid_file);
  2609. }
  2610. static struct wpa_supplicant * wpa_supplicant_alloc(void)
  2611. {
  2612. struct wpa_supplicant *wpa_s;
  2613. wpa_s = os_zalloc(sizeof(*wpa_s));
  2614. if (wpa_s == NULL)
  2615. return NULL;
  2616. wpa_s->scan_req = INITIAL_SCAN_REQ;
  2617. wpa_s->scan_interval = 5;
  2618. wpa_s->new_connection = 1;
  2619. wpa_s->parent = wpa_s;
  2620. wpa_s->sched_scanning = 0;
  2621. return wpa_s;
  2622. }
  2623. #ifdef CONFIG_HT_OVERRIDES
  2624. static int wpa_set_htcap_mcs(struct wpa_supplicant *wpa_s,
  2625. struct ieee80211_ht_capabilities *htcaps,
  2626. struct ieee80211_ht_capabilities *htcaps_mask,
  2627. const char *ht_mcs)
  2628. {
  2629. /* parse ht_mcs into hex array */
  2630. int i;
  2631. const char *tmp = ht_mcs;
  2632. char *end = NULL;
  2633. /* If ht_mcs is null, do not set anything */
  2634. if (!ht_mcs)
  2635. return 0;
  2636. /* This is what we are setting in the kernel */
  2637. os_memset(&htcaps->supported_mcs_set, 0, IEEE80211_HT_MCS_MASK_LEN);
  2638. wpa_msg(wpa_s, MSG_DEBUG, "set_htcap, ht_mcs -:%s:-", ht_mcs);
  2639. for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
  2640. errno = 0;
  2641. long v = strtol(tmp, &end, 16);
  2642. if (errno == 0) {
  2643. wpa_msg(wpa_s, MSG_DEBUG,
  2644. "htcap value[%i]: %ld end: %p tmp: %p",
  2645. i, v, end, tmp);
  2646. if (end == tmp)
  2647. break;
  2648. htcaps->supported_mcs_set[i] = v;
  2649. tmp = end;
  2650. } else {
  2651. wpa_msg(wpa_s, MSG_ERROR,
  2652. "Failed to parse ht-mcs: %s, error: %s\n",
  2653. ht_mcs, strerror(errno));
  2654. return -1;
  2655. }
  2656. }
  2657. /*
  2658. * If we were able to parse any values, then set mask for the MCS set.
  2659. */
  2660. if (i) {
  2661. os_memset(&htcaps_mask->supported_mcs_set, 0xff,
  2662. IEEE80211_HT_MCS_MASK_LEN - 1);
  2663. /* skip the 3 reserved bits */
  2664. htcaps_mask->supported_mcs_set[IEEE80211_HT_MCS_MASK_LEN - 1] =
  2665. 0x1f;
  2666. }
  2667. return 0;
  2668. }
  2669. static int wpa_disable_max_amsdu(struct wpa_supplicant *wpa_s,
  2670. struct ieee80211_ht_capabilities *htcaps,
  2671. struct ieee80211_ht_capabilities *htcaps_mask,
  2672. int disabled)
  2673. {
  2674. le16 msk;
  2675. wpa_msg(wpa_s, MSG_DEBUG, "set_disable_max_amsdu: %d", disabled);
  2676. if (disabled == -1)
  2677. return 0;
  2678. msk = host_to_le16(HT_CAP_INFO_MAX_AMSDU_SIZE);
  2679. htcaps_mask->ht_capabilities_info |= msk;
  2680. if (disabled)
  2681. htcaps->ht_capabilities_info &= msk;
  2682. else
  2683. htcaps->ht_capabilities_info |= msk;
  2684. return 0;
  2685. }
  2686. static int wpa_set_ampdu_factor(struct wpa_supplicant *wpa_s,
  2687. struct ieee80211_ht_capabilities *htcaps,
  2688. struct ieee80211_ht_capabilities *htcaps_mask,
  2689. int factor)
  2690. {
  2691. wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_factor: %d", factor);
  2692. if (factor == -1)
  2693. return 0;
  2694. if (factor < 0 || factor > 3) {
  2695. wpa_msg(wpa_s, MSG_ERROR, "ampdu_factor: %d out of range. "
  2696. "Must be 0-3 or -1", factor);
  2697. return -EINVAL;
  2698. }
  2699. htcaps_mask->a_mpdu_params |= 0x3; /* 2 bits for factor */
  2700. htcaps->a_mpdu_params &= ~0x3;
  2701. htcaps->a_mpdu_params |= factor & 0x3;
  2702. return 0;
  2703. }
  2704. static int wpa_set_ampdu_density(struct wpa_supplicant *wpa_s,
  2705. struct ieee80211_ht_capabilities *htcaps,
  2706. struct ieee80211_ht_capabilities *htcaps_mask,
  2707. int density)
  2708. {
  2709. wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_density: %d", density);
  2710. if (density == -1)
  2711. return 0;
  2712. if (density < 0 || density > 7) {
  2713. wpa_msg(wpa_s, MSG_ERROR,
  2714. "ampdu_density: %d out of range. Must be 0-7 or -1.",
  2715. density);
  2716. return -EINVAL;
  2717. }
  2718. htcaps_mask->a_mpdu_params |= 0x1C;
  2719. htcaps->a_mpdu_params &= ~(0x1C);
  2720. htcaps->a_mpdu_params |= (density << 2) & 0x1C;
  2721. return 0;
  2722. }
  2723. static int wpa_set_disable_ht40(struct wpa_supplicant *wpa_s,
  2724. struct ieee80211_ht_capabilities *htcaps,
  2725. struct ieee80211_ht_capabilities *htcaps_mask,
  2726. int disabled)
  2727. {
  2728. /* Masking these out disables HT40 */
  2729. le16 msk = host_to_le16(HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET |
  2730. HT_CAP_INFO_SHORT_GI40MHZ);
  2731. wpa_msg(wpa_s, MSG_DEBUG, "set_disable_ht40: %d", disabled);
  2732. if (disabled)
  2733. htcaps->ht_capabilities_info &= ~msk;
  2734. else
  2735. htcaps->ht_capabilities_info |= msk;
  2736. htcaps_mask->ht_capabilities_info |= msk;
  2737. return 0;
  2738. }
  2739. static int wpa_set_disable_sgi(struct wpa_supplicant *wpa_s,
  2740. struct ieee80211_ht_capabilities *htcaps,
  2741. struct ieee80211_ht_capabilities *htcaps_mask,
  2742. int disabled)
  2743. {
  2744. /* Masking these out disables SGI */
  2745. le16 msk = host_to_le16(HT_CAP_INFO_SHORT_GI20MHZ |
  2746. HT_CAP_INFO_SHORT_GI40MHZ);
  2747. wpa_msg(wpa_s, MSG_DEBUG, "set_disable_sgi: %d", disabled);
  2748. if (disabled)
  2749. htcaps->ht_capabilities_info &= ~msk;
  2750. else
  2751. htcaps->ht_capabilities_info |= msk;
  2752. htcaps_mask->ht_capabilities_info |= msk;
  2753. return 0;
  2754. }
  2755. static int wpa_set_disable_ldpc(struct wpa_supplicant *wpa_s,
  2756. struct ieee80211_ht_capabilities *htcaps,
  2757. struct ieee80211_ht_capabilities *htcaps_mask,
  2758. int disabled)
  2759. {
  2760. /* Masking these out disables LDPC */
  2761. le16 msk = host_to_le16(HT_CAP_INFO_LDPC_CODING_CAP);
  2762. wpa_msg(wpa_s, MSG_DEBUG, "set_disable_ldpc: %d", disabled);
  2763. if (disabled)
  2764. htcaps->ht_capabilities_info &= ~msk;
  2765. else
  2766. htcaps->ht_capabilities_info |= msk;
  2767. htcaps_mask->ht_capabilities_info |= msk;
  2768. return 0;
  2769. }
  2770. void wpa_supplicant_apply_ht_overrides(
  2771. struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
  2772. struct wpa_driver_associate_params *params)
  2773. {
  2774. struct ieee80211_ht_capabilities *htcaps;
  2775. struct ieee80211_ht_capabilities *htcaps_mask;
  2776. if (!ssid)
  2777. return;
  2778. params->disable_ht = ssid->disable_ht;
  2779. if (!params->htcaps || !params->htcaps_mask)
  2780. return;
  2781. htcaps = (struct ieee80211_ht_capabilities *) params->htcaps;
  2782. htcaps_mask = (struct ieee80211_ht_capabilities *) params->htcaps_mask;
  2783. wpa_set_htcap_mcs(wpa_s, htcaps, htcaps_mask, ssid->ht_mcs);
  2784. wpa_disable_max_amsdu(wpa_s, htcaps, htcaps_mask,
  2785. ssid->disable_max_amsdu);
  2786. wpa_set_ampdu_factor(wpa_s, htcaps, htcaps_mask, ssid->ampdu_factor);
  2787. wpa_set_ampdu_density(wpa_s, htcaps, htcaps_mask, ssid->ampdu_density);
  2788. wpa_set_disable_ht40(wpa_s, htcaps, htcaps_mask, ssid->disable_ht40);
  2789. wpa_set_disable_sgi(wpa_s, htcaps, htcaps_mask, ssid->disable_sgi);
  2790. wpa_set_disable_ldpc(wpa_s, htcaps, htcaps_mask, ssid->disable_ldpc);
  2791. if (ssid->ht40_intolerant) {
  2792. le16 bit = host_to_le16(HT_CAP_INFO_40MHZ_INTOLERANT);
  2793. htcaps->ht_capabilities_info |= bit;
  2794. htcaps_mask->ht_capabilities_info |= bit;
  2795. }
  2796. }
  2797. #endif /* CONFIG_HT_OVERRIDES */
  2798. #ifdef CONFIG_VHT_OVERRIDES
  2799. void wpa_supplicant_apply_vht_overrides(
  2800. struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
  2801. struct wpa_driver_associate_params *params)
  2802. {
  2803. struct ieee80211_vht_capabilities *vhtcaps;
  2804. struct ieee80211_vht_capabilities *vhtcaps_mask;
  2805. if (!ssid)
  2806. return;
  2807. params->disable_vht = ssid->disable_vht;
  2808. vhtcaps = (void *) params->vhtcaps;
  2809. vhtcaps_mask = (void *) params->vhtcaps_mask;
  2810. if (!vhtcaps || !vhtcaps_mask)
  2811. return;
  2812. vhtcaps->vht_capabilities_info = ssid->vht_capa;
  2813. vhtcaps_mask->vht_capabilities_info = ssid->vht_capa_mask;
  2814. #ifdef CONFIG_HT_OVERRIDES
  2815. /* if max ampdu is <= 3, we have to make the HT cap the same */
  2816. if (ssid->vht_capa_mask & VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX) {
  2817. int max_ampdu;
  2818. max_ampdu = (ssid->vht_capa &
  2819. VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX) >>
  2820. VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX_SHIFT;
  2821. max_ampdu = max_ampdu < 3 ? max_ampdu : 3;
  2822. wpa_set_ampdu_factor(wpa_s,
  2823. (void *) params->htcaps,
  2824. (void *) params->htcaps_mask,
  2825. max_ampdu);
  2826. }
  2827. #endif /* CONFIG_HT_OVERRIDES */
  2828. #define OVERRIDE_MCS(i) \
  2829. if (ssid->vht_tx_mcs_nss_ ##i >= 0) { \
  2830. vhtcaps_mask->vht_supported_mcs_set.tx_map |= \
  2831. 3 << 2 * (i - 1); \
  2832. vhtcaps->vht_supported_mcs_set.tx_map |= \
  2833. ssid->vht_tx_mcs_nss_ ##i << 2 * (i - 1); \
  2834. } \
  2835. if (ssid->vht_rx_mcs_nss_ ##i >= 0) { \
  2836. vhtcaps_mask->vht_supported_mcs_set.rx_map |= \
  2837. 3 << 2 * (i - 1); \
  2838. vhtcaps->vht_supported_mcs_set.rx_map |= \
  2839. ssid->vht_rx_mcs_nss_ ##i << 2 * (i - 1); \
  2840. }
  2841. OVERRIDE_MCS(1);
  2842. OVERRIDE_MCS(2);
  2843. OVERRIDE_MCS(3);
  2844. OVERRIDE_MCS(4);
  2845. OVERRIDE_MCS(5);
  2846. OVERRIDE_MCS(6);
  2847. OVERRIDE_MCS(7);
  2848. OVERRIDE_MCS(8);
  2849. }
  2850. #endif /* CONFIG_VHT_OVERRIDES */
  2851. static int pcsc_reader_init(struct wpa_supplicant *wpa_s)
  2852. {
  2853. #ifdef PCSC_FUNCS
  2854. size_t len;
  2855. if (!wpa_s->conf->pcsc_reader)
  2856. return 0;
  2857. wpa_s->scard = scard_init(wpa_s->conf->pcsc_reader);
  2858. if (!wpa_s->scard)
  2859. return 1;
  2860. if (wpa_s->conf->pcsc_pin &&
  2861. scard_set_pin(wpa_s->scard, wpa_s->conf->pcsc_pin) < 0) {
  2862. scard_deinit(wpa_s->scard);
  2863. wpa_s->scard = NULL;
  2864. wpa_msg(wpa_s, MSG_ERROR, "PC/SC PIN validation failed");
  2865. return -1;
  2866. }
  2867. len = sizeof(wpa_s->imsi) - 1;
  2868. if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) {
  2869. scard_deinit(wpa_s->scard);
  2870. wpa_s->scard = NULL;
  2871. wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI");
  2872. return -1;
  2873. }
  2874. wpa_s->imsi[len] = '\0';
  2875. wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard);
  2876. wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)",
  2877. wpa_s->imsi, wpa_s->mnc_len);
  2878. wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
  2879. eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
  2880. #endif /* PCSC_FUNCS */
  2881. return 0;
  2882. }
  2883. int wpas_init_ext_pw(struct wpa_supplicant *wpa_s)
  2884. {
  2885. char *val, *pos;
  2886. ext_password_deinit(wpa_s->ext_pw);
  2887. wpa_s->ext_pw = NULL;
  2888. eapol_sm_set_ext_pw_ctx(wpa_s->eapol, NULL);
  2889. if (!wpa_s->conf->ext_password_backend)
  2890. return 0;
  2891. val = os_strdup(wpa_s->conf->ext_password_backend);
  2892. if (val == NULL)
  2893. return -1;
  2894. pos = os_strchr(val, ':');
  2895. if (pos)
  2896. *pos++ = '\0';
  2897. wpa_printf(MSG_DEBUG, "EXT PW: Initialize backend '%s'", val);
  2898. wpa_s->ext_pw = ext_password_init(val, pos);
  2899. os_free(val);
  2900. if (wpa_s->ext_pw == NULL) {
  2901. wpa_printf(MSG_DEBUG, "EXT PW: Failed to initialize backend");
  2902. return -1;
  2903. }
  2904. eapol_sm_set_ext_pw_ctx(wpa_s->eapol, wpa_s->ext_pw);
  2905. return 0;
  2906. }
  2907. static int wpas_set_wowlan_triggers(struct wpa_supplicant *wpa_s,
  2908. const struct wpa_driver_capa *capa)
  2909. {
  2910. struct wowlan_triggers *triggers;
  2911. int ret = 0;
  2912. if (!wpa_s->conf->wowlan_triggers)
  2913. return 0;
  2914. triggers = wpa_get_wowlan_triggers(wpa_s->conf->wowlan_triggers, capa);
  2915. if (triggers) {
  2916. ret = wpa_drv_wowlan(wpa_s, triggers);
  2917. os_free(triggers);
  2918. }
  2919. return ret;
  2920. }
  2921. static struct wpa_radio * radio_add_interface(struct wpa_supplicant *wpa_s,
  2922. const char *rn)
  2923. {
  2924. struct wpa_supplicant *iface = wpa_s->global->ifaces;
  2925. struct wpa_radio *radio;
  2926. while (rn && iface) {
  2927. radio = iface->radio;
  2928. if (radio && os_strcmp(rn, radio->name) == 0) {
  2929. wpa_printf(MSG_DEBUG, "Add interface %s to existing radio %s",
  2930. wpa_s->ifname, rn);
  2931. dl_list_add(&radio->ifaces, &wpa_s->radio_list);
  2932. return radio;
  2933. }
  2934. iface = iface->next;
  2935. }
  2936. wpa_printf(MSG_DEBUG, "Add interface %s to a new radio %s",
  2937. wpa_s->ifname, rn ? rn : "N/A");
  2938. radio = os_zalloc(sizeof(*radio));
  2939. if (radio == NULL)
  2940. return NULL;
  2941. if (rn)
  2942. os_strlcpy(radio->name, rn, sizeof(radio->name));
  2943. dl_list_init(&radio->ifaces);
  2944. dl_list_init(&radio->work);
  2945. dl_list_add(&radio->ifaces, &wpa_s->radio_list);
  2946. return radio;
  2947. }
  2948. static void radio_work_free(struct wpa_radio_work *work)
  2949. {
  2950. if (work->wpa_s->scan_work == work) {
  2951. /* This should not really happen. */
  2952. wpa_dbg(work->wpa_s, MSG_INFO, "Freeing radio work '%s'@%p (started=%d) that is marked as scan_work",
  2953. work->type, work, work->started);
  2954. work->wpa_s->scan_work = NULL;
  2955. }
  2956. #ifdef CONFIG_P2P
  2957. if (work->wpa_s->p2p_scan_work == work) {
  2958. /* This should not really happen. */
  2959. wpa_dbg(work->wpa_s, MSG_INFO, "Freeing radio work '%s'@%p (started=%d) that is marked as p2p_scan_work",
  2960. work->type, work, work->started);
  2961. work->wpa_s->p2p_scan_work = NULL;
  2962. }
  2963. #endif /* CONFIG_P2P */
  2964. dl_list_del(&work->list);
  2965. os_free(work);
  2966. }
  2967. static void radio_start_next_work(void *eloop_ctx, void *timeout_ctx)
  2968. {
  2969. struct wpa_radio *radio = eloop_ctx;
  2970. struct wpa_radio_work *work;
  2971. struct os_reltime now, diff;
  2972. struct wpa_supplicant *wpa_s;
  2973. work = dl_list_first(&radio->work, struct wpa_radio_work, list);
  2974. if (work == NULL)
  2975. return;
  2976. if (work->started)
  2977. return; /* already started and still in progress */
  2978. wpa_s = dl_list_first(&radio->ifaces, struct wpa_supplicant,
  2979. radio_list);
  2980. if (wpa_s && wpa_s->external_scan_running) {
  2981. wpa_printf(MSG_DEBUG, "Delay radio work start until externally triggered scan completes");
  2982. return;
  2983. }
  2984. os_get_reltime(&now);
  2985. os_reltime_sub(&now, &work->time, &diff);
  2986. wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting radio work '%s'@%p after %ld.%06ld second wait",
  2987. work->type, work, diff.sec, diff.usec);
  2988. work->started = 1;
  2989. work->time = now;
  2990. work->cb(work, 0);
  2991. }
  2992. /*
  2993. * This function removes both started and pending radio works running on
  2994. * the provided interface's radio.
  2995. * Prior to the removal of the radio work, its callback (cb) is called with
  2996. * deinit set to be 1. Each work's callback is responsible for clearing its
  2997. * internal data and restoring to a correct state.
  2998. * @wpa_s: wpa_supplicant data
  2999. * @type: type of works to be removed
  3000. * @remove_all: 1 to remove all the works on this radio, 0 to remove only
  3001. * this interface's works.
  3002. */
  3003. void radio_remove_works(struct wpa_supplicant *wpa_s,
  3004. const char *type, int remove_all)
  3005. {
  3006. struct wpa_radio_work *work, *tmp;
  3007. struct wpa_radio *radio = wpa_s->radio;
  3008. dl_list_for_each_safe(work, tmp, &radio->work, struct wpa_radio_work,
  3009. list) {
  3010. if (type && os_strcmp(type, work->type) != 0)
  3011. continue;
  3012. /* skip other ifaces' works */
  3013. if (!remove_all && work->wpa_s != wpa_s)
  3014. continue;
  3015. wpa_dbg(wpa_s, MSG_DEBUG, "Remove radio work '%s'@%p%s",
  3016. work->type, work, work->started ? " (started)" : "");
  3017. work->cb(work, 1);
  3018. radio_work_free(work);
  3019. }
  3020. /* in case we removed the started work */
  3021. radio_work_check_next(wpa_s);
  3022. }
  3023. static void radio_remove_interface(struct wpa_supplicant *wpa_s)
  3024. {
  3025. struct wpa_radio *radio = wpa_s->radio;
  3026. if (!radio)
  3027. return;
  3028. wpa_printf(MSG_DEBUG, "Remove interface %s from radio %s",
  3029. wpa_s->ifname, radio->name);
  3030. dl_list_del(&wpa_s->radio_list);
  3031. radio_remove_works(wpa_s, NULL, 0);
  3032. wpa_s->radio = NULL;
  3033. if (!dl_list_empty(&radio->ifaces))
  3034. return; /* Interfaces remain for this radio */
  3035. wpa_printf(MSG_DEBUG, "Remove radio %s", radio->name);
  3036. eloop_cancel_timeout(radio_start_next_work, radio, NULL);
  3037. os_free(radio);
  3038. }
  3039. void radio_work_check_next(struct wpa_supplicant *wpa_s)
  3040. {
  3041. struct wpa_radio *radio = wpa_s->radio;
  3042. if (dl_list_empty(&radio->work))
  3043. return;
  3044. if (wpa_s->ext_work_in_progress) {
  3045. wpa_printf(MSG_DEBUG,
  3046. "External radio work in progress - delay start of pending item");
  3047. return;
  3048. }
  3049. eloop_cancel_timeout(radio_start_next_work, radio, NULL);
  3050. eloop_register_timeout(0, 0, radio_start_next_work, radio, NULL);
  3051. }
  3052. /**
  3053. * radio_add_work - Add a radio work item
  3054. * @wpa_s: Pointer to wpa_supplicant data
  3055. * @freq: Frequency of the offchannel operation in MHz or 0
  3056. * @type: Unique identifier for each type of work
  3057. * @next: Force as the next work to be executed
  3058. * @cb: Callback function for indicating when radio is available
  3059. * @ctx: Context pointer for the work (work->ctx in cb())
  3060. * Returns: 0 on success, -1 on failure
  3061. *
  3062. * This function is used to request time for an operation that requires
  3063. * exclusive radio control. Once the radio is available, the registered callback
  3064. * function will be called. radio_work_done() must be called once the exclusive
  3065. * radio operation has been completed, so that the radio is freed for other
  3066. * operations. The special case of deinit=1 is used to free the context data
  3067. * during interface removal. That does not allow the callback function to start
  3068. * the radio operation, i.e., it must free any resources allocated for the radio
  3069. * work and return.
  3070. *
  3071. * The @freq parameter can be used to indicate a single channel on which the
  3072. * offchannel operation will occur. This may allow multiple radio work
  3073. * operations to be performed in parallel if they apply for the same channel.
  3074. * Setting this to 0 indicates that the work item may use multiple channels or
  3075. * requires exclusive control of the radio.
  3076. */
  3077. int radio_add_work(struct wpa_supplicant *wpa_s, unsigned int freq,
  3078. const char *type, int next,
  3079. void (*cb)(struct wpa_radio_work *work, int deinit),
  3080. void *ctx)
  3081. {
  3082. struct wpa_radio_work *work;
  3083. int was_empty;
  3084. work = os_zalloc(sizeof(*work));
  3085. if (work == NULL)
  3086. return -1;
  3087. wpa_dbg(wpa_s, MSG_DEBUG, "Add radio work '%s'@%p", type, work);
  3088. os_get_reltime(&work->time);
  3089. work->freq = freq;
  3090. work->type = type;
  3091. work->wpa_s = wpa_s;
  3092. work->cb = cb;
  3093. work->ctx = ctx;
  3094. was_empty = dl_list_empty(&wpa_s->radio->work);
  3095. if (next)
  3096. dl_list_add(&wpa_s->radio->work, &work->list);
  3097. else
  3098. dl_list_add_tail(&wpa_s->radio->work, &work->list);
  3099. if (was_empty) {
  3100. wpa_dbg(wpa_s, MSG_DEBUG, "First radio work item in the queue - schedule start immediately");
  3101. radio_work_check_next(wpa_s);
  3102. }
  3103. return 0;
  3104. }
  3105. /**
  3106. * radio_work_done - Indicate that a radio work item has been completed
  3107. * @work: Completed work
  3108. *
  3109. * This function is called once the callback function registered with
  3110. * radio_add_work() has completed its work.
  3111. */
  3112. void radio_work_done(struct wpa_radio_work *work)
  3113. {
  3114. struct wpa_supplicant *wpa_s = work->wpa_s;
  3115. struct os_reltime now, diff;
  3116. unsigned int started = work->started;
  3117. os_get_reltime(&now);
  3118. os_reltime_sub(&now, &work->time, &diff);
  3119. wpa_dbg(wpa_s, MSG_DEBUG, "Radio work '%s'@%p %s in %ld.%06ld seconds",
  3120. work->type, work, started ? "done" : "canceled",
  3121. diff.sec, diff.usec);
  3122. radio_work_free(work);
  3123. if (started)
  3124. radio_work_check_next(wpa_s);
  3125. }
  3126. struct wpa_radio_work *
  3127. radio_work_pending(struct wpa_supplicant *wpa_s, const char *type)
  3128. {
  3129. struct wpa_radio_work *work;
  3130. struct wpa_radio *radio = wpa_s->radio;
  3131. dl_list_for_each(work, &radio->work, struct wpa_radio_work, list) {
  3132. if (work->wpa_s == wpa_s && os_strcmp(work->type, type) == 0)
  3133. return work;
  3134. }
  3135. return NULL;
  3136. }
  3137. static int wpas_init_driver(struct wpa_supplicant *wpa_s,
  3138. struct wpa_interface *iface)
  3139. {
  3140. const char *ifname, *driver, *rn;
  3141. driver = iface->driver;
  3142. next_driver:
  3143. if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
  3144. return -1;
  3145. wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
  3146. if (wpa_s->drv_priv == NULL) {
  3147. const char *pos;
  3148. pos = driver ? os_strchr(driver, ',') : NULL;
  3149. if (pos) {
  3150. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
  3151. "driver interface - try next driver wrapper");
  3152. driver = pos + 1;
  3153. goto next_driver;
  3154. }
  3155. wpa_msg(wpa_s, MSG_ERROR, "Failed to initialize driver "
  3156. "interface");
  3157. return -1;
  3158. }
  3159. if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
  3160. wpa_msg(wpa_s, MSG_ERROR, "Driver interface rejected "
  3161. "driver_param '%s'", wpa_s->conf->driver_param);
  3162. return -1;
  3163. }
  3164. ifname = wpa_drv_get_ifname(wpa_s);
  3165. if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
  3166. wpa_dbg(wpa_s, MSG_DEBUG, "Driver interface replaced "
  3167. "interface name with '%s'", ifname);
  3168. os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
  3169. }
  3170. rn = wpa_driver_get_radio_name(wpa_s);
  3171. if (rn && rn[0] == '\0')
  3172. rn = NULL;
  3173. wpa_s->radio = radio_add_interface(wpa_s, rn);
  3174. if (wpa_s->radio == NULL)
  3175. return -1;
  3176. return 0;
  3177. }
  3178. static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
  3179. struct wpa_interface *iface)
  3180. {
  3181. struct wpa_driver_capa capa;
  3182. int capa_res;
  3183. wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
  3184. "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
  3185. iface->confname ? iface->confname : "N/A",
  3186. iface->driver ? iface->driver : "default",
  3187. iface->ctrl_interface ? iface->ctrl_interface : "N/A",
  3188. iface->bridge_ifname ? iface->bridge_ifname : "N/A");
  3189. if (iface->confname) {
  3190. #ifdef CONFIG_BACKEND_FILE
  3191. wpa_s->confname = os_rel2abs_path(iface->confname);
  3192. if (wpa_s->confname == NULL) {
  3193. wpa_printf(MSG_ERROR, "Failed to get absolute path "
  3194. "for configuration file '%s'.",
  3195. iface->confname);
  3196. return -1;
  3197. }
  3198. wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
  3199. iface->confname, wpa_s->confname);
  3200. #else /* CONFIG_BACKEND_FILE */
  3201. wpa_s->confname = os_strdup(iface->confname);
  3202. #endif /* CONFIG_BACKEND_FILE */
  3203. wpa_s->conf = wpa_config_read(wpa_s->confname, NULL);
  3204. if (wpa_s->conf == NULL) {
  3205. wpa_printf(MSG_ERROR, "Failed to read or parse "
  3206. "configuration '%s'.", wpa_s->confname);
  3207. return -1;
  3208. }
  3209. wpa_s->confanother = os_rel2abs_path(iface->confanother);
  3210. wpa_config_read(wpa_s->confanother, wpa_s->conf);
  3211. /*
  3212. * Override ctrl_interface and driver_param if set on command
  3213. * line.
  3214. */
  3215. if (iface->ctrl_interface) {
  3216. os_free(wpa_s->conf->ctrl_interface);
  3217. wpa_s->conf->ctrl_interface =
  3218. os_strdup(iface->ctrl_interface);
  3219. }
  3220. if (iface->driver_param) {
  3221. os_free(wpa_s->conf->driver_param);
  3222. wpa_s->conf->driver_param =
  3223. os_strdup(iface->driver_param);
  3224. }
  3225. if (iface->p2p_mgmt && !iface->ctrl_interface) {
  3226. os_free(wpa_s->conf->ctrl_interface);
  3227. wpa_s->conf->ctrl_interface = NULL;
  3228. }
  3229. } else
  3230. wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
  3231. iface->driver_param);
  3232. if (wpa_s->conf == NULL) {
  3233. wpa_printf(MSG_ERROR, "\nNo configuration found.");
  3234. return -1;
  3235. }
  3236. if (iface->ifname == NULL) {
  3237. wpa_printf(MSG_ERROR, "\nInterface name is required.");
  3238. return -1;
  3239. }
  3240. if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
  3241. wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
  3242. iface->ifname);
  3243. return -1;
  3244. }
  3245. os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
  3246. if (iface->bridge_ifname) {
  3247. if (os_strlen(iface->bridge_ifname) >=
  3248. sizeof(wpa_s->bridge_ifname)) {
  3249. wpa_printf(MSG_ERROR, "\nToo long bridge interface "
  3250. "name '%s'.", iface->bridge_ifname);
  3251. return -1;
  3252. }
  3253. os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
  3254. sizeof(wpa_s->bridge_ifname));
  3255. }
  3256. /* RSNA Supplicant Key Management - INITIALIZE */
  3257. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  3258. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  3259. /* Initialize driver interface and register driver event handler before
  3260. * L2 receive handler so that association events are processed before
  3261. * EAPOL-Key packets if both become available for the same select()
  3262. * call. */
  3263. if (wpas_init_driver(wpa_s, iface) < 0)
  3264. return -1;
  3265. if (wpa_supplicant_init_wpa(wpa_s) < 0)
  3266. return -1;
  3267. wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
  3268. wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
  3269. NULL);
  3270. wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
  3271. if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
  3272. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
  3273. wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
  3274. wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
  3275. "dot11RSNAConfigPMKLifetime");
  3276. return -1;
  3277. }
  3278. if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
  3279. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
  3280. wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
  3281. wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
  3282. "dot11RSNAConfigPMKReauthThreshold");
  3283. return -1;
  3284. }
  3285. if (wpa_s->conf->dot11RSNAConfigSATimeout &&
  3286. wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
  3287. wpa_s->conf->dot11RSNAConfigSATimeout)) {
  3288. wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
  3289. "dot11RSNAConfigSATimeout");
  3290. return -1;
  3291. }
  3292. wpa_s->hw.modes = wpa_drv_get_hw_feature_data(wpa_s,
  3293. &wpa_s->hw.num_modes,
  3294. &wpa_s->hw.flags);
  3295. capa_res = wpa_drv_get_capa(wpa_s, &capa);
  3296. if (capa_res == 0) {
  3297. wpa_s->drv_capa_known = 1;
  3298. wpa_s->drv_flags = capa.flags;
  3299. wpa_s->drv_enc = capa.enc;
  3300. wpa_s->drv_smps_modes = capa.smps_modes;
  3301. wpa_s->drv_rrm_flags = capa.rrm_flags;
  3302. wpa_s->probe_resp_offloads = capa.probe_resp_offloads;
  3303. wpa_s->max_scan_ssids = capa.max_scan_ssids;
  3304. wpa_s->max_sched_scan_ssids = capa.max_sched_scan_ssids;
  3305. wpa_s->sched_scan_supported = capa.sched_scan_supported;
  3306. wpa_s->max_match_sets = capa.max_match_sets;
  3307. wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
  3308. wpa_s->max_stations = capa.max_stations;
  3309. wpa_s->extended_capa = capa.extended_capa;
  3310. wpa_s->extended_capa_mask = capa.extended_capa_mask;
  3311. wpa_s->extended_capa_len = capa.extended_capa_len;
  3312. wpa_s->num_multichan_concurrent =
  3313. capa.num_multichan_concurrent;
  3314. wpa_s->wmm_ac_supported = capa.wmm_ac_supported;
  3315. }
  3316. if (wpa_s->max_remain_on_chan == 0)
  3317. wpa_s->max_remain_on_chan = 1000;
  3318. /*
  3319. * Only take p2p_mgmt parameters when P2P Device is supported.
  3320. * Doing it here as it determines whether l2_packet_init() will be done
  3321. * during wpa_supplicant_driver_init().
  3322. */
  3323. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)
  3324. wpa_s->p2p_mgmt = iface->p2p_mgmt;
  3325. else
  3326. iface->p2p_mgmt = 1;
  3327. if (wpa_s->num_multichan_concurrent == 0)
  3328. wpa_s->num_multichan_concurrent = 1;
  3329. if (wpa_supplicant_driver_init(wpa_s) < 0)
  3330. return -1;
  3331. #ifdef CONFIG_TDLS
  3332. if ((!iface->p2p_mgmt ||
  3333. !(wpa_s->drv_flags &
  3334. WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) &&
  3335. wpa_tdls_init(wpa_s->wpa))
  3336. return -1;
  3337. #endif /* CONFIG_TDLS */
  3338. if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
  3339. wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
  3340. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to set country");
  3341. return -1;
  3342. }
  3343. if (wpas_wps_init(wpa_s))
  3344. return -1;
  3345. if (wpa_supplicant_init_eapol(wpa_s) < 0)
  3346. return -1;
  3347. wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
  3348. wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
  3349. if (wpa_s->ctrl_iface == NULL) {
  3350. wpa_printf(MSG_ERROR,
  3351. "Failed to initialize control interface '%s'.\n"
  3352. "You may have another wpa_supplicant process "
  3353. "already running or the file was\n"
  3354. "left by an unclean termination of wpa_supplicant "
  3355. "in which case you will need\n"
  3356. "to manually remove this file before starting "
  3357. "wpa_supplicant again.\n",
  3358. wpa_s->conf->ctrl_interface);
  3359. return -1;
  3360. }
  3361. wpa_s->gas = gas_query_init(wpa_s);
  3362. if (wpa_s->gas == NULL) {
  3363. wpa_printf(MSG_ERROR, "Failed to initialize GAS query");
  3364. return -1;
  3365. }
  3366. if (iface->p2p_mgmt && wpas_p2p_init(wpa_s->global, wpa_s) < 0) {
  3367. wpa_msg(wpa_s, MSG_ERROR, "Failed to init P2P");
  3368. return -1;
  3369. }
  3370. if (wpa_bss_init(wpa_s) < 0)
  3371. return -1;
  3372. /*
  3373. * Set Wake-on-WLAN triggers, if configured.
  3374. * Note: We don't restore/remove the triggers on shutdown (it doesn't
  3375. * have effect anyway when the interface is down).
  3376. */
  3377. if (capa_res == 0 && wpas_set_wowlan_triggers(wpa_s, &capa) < 0)
  3378. return -1;
  3379. #ifdef CONFIG_EAP_PROXY
  3380. {
  3381. size_t len;
  3382. wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol, wpa_s->imsi,
  3383. &len);
  3384. if (wpa_s->mnc_len > 0) {
  3385. wpa_s->imsi[len] = '\0';
  3386. wpa_printf(MSG_DEBUG, "eap_proxy: IMSI %s (MNC length %d)",
  3387. wpa_s->imsi, wpa_s->mnc_len);
  3388. } else {
  3389. wpa_printf(MSG_DEBUG, "eap_proxy: IMSI not available");
  3390. }
  3391. }
  3392. #endif /* CONFIG_EAP_PROXY */
  3393. if (pcsc_reader_init(wpa_s) < 0)
  3394. return -1;
  3395. if (wpas_init_ext_pw(wpa_s) < 0)
  3396. return -1;
  3397. wpas_rrm_reset(wpa_s);
  3398. return 0;
  3399. }
  3400. static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
  3401. int notify, int terminate)
  3402. {
  3403. struct wpa_global *global = wpa_s->global;
  3404. struct wpa_supplicant *iface, *prev;
  3405. if (wpa_s == wpa_s->parent)
  3406. wpas_p2p_group_remove(wpa_s, "*");
  3407. iface = global->ifaces;
  3408. while (iface) {
  3409. if (iface == wpa_s || iface->parent != wpa_s) {
  3410. iface = iface->next;
  3411. continue;
  3412. }
  3413. wpa_printf(MSG_DEBUG,
  3414. "Remove remaining child interface %s from parent %s",
  3415. iface->ifname, wpa_s->ifname);
  3416. prev = iface;
  3417. iface = iface->next;
  3418. wpa_supplicant_remove_iface(global, prev, terminate);
  3419. }
  3420. wpa_s->disconnected = 1;
  3421. if (wpa_s->drv_priv) {
  3422. wpa_supplicant_deauthenticate(wpa_s,
  3423. WLAN_REASON_DEAUTH_LEAVING);
  3424. wpa_drv_set_countermeasures(wpa_s, 0);
  3425. wpa_clear_keys(wpa_s, NULL);
  3426. }
  3427. wpa_supplicant_cleanup(wpa_s);
  3428. wpas_p2p_deinit_iface(wpa_s);
  3429. wpas_ctrl_radio_work_flush(wpa_s);
  3430. radio_remove_interface(wpa_s);
  3431. if (wpa_s->drv_priv)
  3432. wpa_drv_deinit(wpa_s);
  3433. if (notify)
  3434. wpas_notify_iface_removed(wpa_s);
  3435. if (terminate)
  3436. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING);
  3437. if (wpa_s->ctrl_iface) {
  3438. wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
  3439. wpa_s->ctrl_iface = NULL;
  3440. }
  3441. #ifdef CONFIG_MESH
  3442. if (wpa_s->ifmsh) {
  3443. wpa_supplicant_mesh_iface_deinit(wpa_s, wpa_s->ifmsh);
  3444. wpa_s->ifmsh = NULL;
  3445. }
  3446. #endif /* CONFIG_MESH */
  3447. if (wpa_s->conf != NULL) {
  3448. wpa_config_free(wpa_s->conf);
  3449. wpa_s->conf = NULL;
  3450. }
  3451. os_free(wpa_s);
  3452. }
  3453. /**
  3454. * wpa_supplicant_add_iface - Add a new network interface
  3455. * @global: Pointer to global data from wpa_supplicant_init()
  3456. * @iface: Interface configuration options
  3457. * Returns: Pointer to the created interface or %NULL on failure
  3458. *
  3459. * This function is used to add new network interfaces for %wpa_supplicant.
  3460. * This can be called before wpa_supplicant_run() to add interfaces before the
  3461. * main event loop has been started. In addition, new interfaces can be added
  3462. * dynamically while %wpa_supplicant is already running. This could happen,
  3463. * e.g., when a hotplug network adapter is inserted.
  3464. */
  3465. struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
  3466. struct wpa_interface *iface)
  3467. {
  3468. struct wpa_supplicant *wpa_s;
  3469. struct wpa_interface t_iface;
  3470. struct wpa_ssid *ssid;
  3471. if (global == NULL || iface == NULL)
  3472. return NULL;
  3473. wpa_s = wpa_supplicant_alloc();
  3474. if (wpa_s == NULL)
  3475. return NULL;
  3476. wpa_s->global = global;
  3477. t_iface = *iface;
  3478. if (global->params.override_driver) {
  3479. wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
  3480. "('%s' -> '%s')",
  3481. iface->driver, global->params.override_driver);
  3482. t_iface.driver = global->params.override_driver;
  3483. }
  3484. if (global->params.override_ctrl_interface) {
  3485. wpa_printf(MSG_DEBUG, "Override interface parameter: "
  3486. "ctrl_interface ('%s' -> '%s')",
  3487. iface->ctrl_interface,
  3488. global->params.override_ctrl_interface);
  3489. t_iface.ctrl_interface =
  3490. global->params.override_ctrl_interface;
  3491. }
  3492. if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
  3493. wpa_printf(MSG_DEBUG, "Failed to add interface %s",
  3494. iface->ifname);
  3495. wpa_supplicant_deinit_iface(wpa_s, 0, 0);
  3496. return NULL;
  3497. }
  3498. if (iface->p2p_mgmt == 0) {
  3499. /* Notify the control interfaces about new iface */
  3500. if (wpas_notify_iface_added(wpa_s)) {
  3501. wpa_supplicant_deinit_iface(wpa_s, 1, 0);
  3502. return NULL;
  3503. }
  3504. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
  3505. wpas_notify_network_added(wpa_s, ssid);
  3506. }
  3507. wpa_s->next = global->ifaces;
  3508. global->ifaces = wpa_s;
  3509. wpa_dbg(wpa_s, MSG_DEBUG, "Added interface %s", wpa_s->ifname);
  3510. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  3511. #ifdef CONFIG_P2P
  3512. if (wpa_s->global->p2p == NULL &&
  3513. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
  3514. wpas_p2p_add_p2pdev_interface(wpa_s, iface->conf_p2p_dev) < 0) {
  3515. wpa_printf(MSG_INFO,
  3516. "P2P: Failed to enable P2P Device interface");
  3517. /* Try to continue without. P2P will be disabled. */
  3518. }
  3519. #endif /* CONFIG_P2P */
  3520. return wpa_s;
  3521. }
  3522. /**
  3523. * wpa_supplicant_remove_iface - Remove a network interface
  3524. * @global: Pointer to global data from wpa_supplicant_init()
  3525. * @wpa_s: Pointer to the network interface to be removed
  3526. * Returns: 0 if interface was removed, -1 if interface was not found
  3527. *
  3528. * This function can be used to dynamically remove network interfaces from
  3529. * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
  3530. * addition, this function is used to remove all remaining interfaces when
  3531. * %wpa_supplicant is terminated.
  3532. */
  3533. int wpa_supplicant_remove_iface(struct wpa_global *global,
  3534. struct wpa_supplicant *wpa_s,
  3535. int terminate)
  3536. {
  3537. struct wpa_supplicant *prev;
  3538. #ifdef CONFIG_MESH
  3539. unsigned int mesh_if_created = wpa_s->mesh_if_created;
  3540. char *ifname = NULL;
  3541. #endif /* CONFIG_MESH */
  3542. /* Remove interface from the global list of interfaces */
  3543. prev = global->ifaces;
  3544. if (prev == wpa_s) {
  3545. global->ifaces = wpa_s->next;
  3546. } else {
  3547. while (prev && prev->next != wpa_s)
  3548. prev = prev->next;
  3549. if (prev == NULL)
  3550. return -1;
  3551. prev->next = wpa_s->next;
  3552. }
  3553. wpa_dbg(wpa_s, MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
  3554. #ifdef CONFIG_MESH
  3555. if (mesh_if_created) {
  3556. ifname = os_strdup(wpa_s->ifname);
  3557. if (ifname == NULL) {
  3558. wpa_dbg(wpa_s, MSG_ERROR,
  3559. "mesh: Failed to malloc ifname");
  3560. return -1;
  3561. }
  3562. }
  3563. #endif /* CONFIG_MESH */
  3564. if (global->p2p_group_formation == wpa_s)
  3565. global->p2p_group_formation = NULL;
  3566. if (global->p2p_invite_group == wpa_s)
  3567. global->p2p_invite_group = NULL;
  3568. wpa_supplicant_deinit_iface(wpa_s, 1, terminate);
  3569. #ifdef CONFIG_MESH
  3570. if (mesh_if_created) {
  3571. wpa_drv_if_remove(global->ifaces, WPA_IF_MESH, ifname);
  3572. os_free(ifname);
  3573. }
  3574. #endif /* CONFIG_MESH */
  3575. return 0;
  3576. }
  3577. /**
  3578. * wpa_supplicant_get_eap_mode - Get the current EAP mode
  3579. * @wpa_s: Pointer to the network interface
  3580. * Returns: Pointer to the eap mode or the string "UNKNOWN" if not found
  3581. */
  3582. const char * wpa_supplicant_get_eap_mode(struct wpa_supplicant *wpa_s)
  3583. {
  3584. const char *eapol_method;
  3585. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) == 0 &&
  3586. wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  3587. return "NO-EAP";
  3588. }
  3589. eapol_method = eapol_sm_get_method_name(wpa_s->eapol);
  3590. if (eapol_method == NULL)
  3591. return "UNKNOWN-EAP";
  3592. return eapol_method;
  3593. }
  3594. /**
  3595. * wpa_supplicant_get_iface - Get a new network interface
  3596. * @global: Pointer to global data from wpa_supplicant_init()
  3597. * @ifname: Interface name
  3598. * Returns: Pointer to the interface or %NULL if not found
  3599. */
  3600. struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
  3601. const char *ifname)
  3602. {
  3603. struct wpa_supplicant *wpa_s;
  3604. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3605. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  3606. return wpa_s;
  3607. }
  3608. return NULL;
  3609. }
  3610. #ifndef CONFIG_NO_WPA_MSG
  3611. static const char * wpa_supplicant_msg_ifname_cb(void *ctx)
  3612. {
  3613. struct wpa_supplicant *wpa_s = ctx;
  3614. if (wpa_s == NULL)
  3615. return NULL;
  3616. return wpa_s->ifname;
  3617. }
  3618. #endif /* CONFIG_NO_WPA_MSG */
  3619. /**
  3620. * wpa_supplicant_init - Initialize %wpa_supplicant
  3621. * @params: Parameters for %wpa_supplicant
  3622. * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
  3623. *
  3624. * This function is used to initialize %wpa_supplicant. After successful
  3625. * initialization, the returned data pointer can be used to add and remove
  3626. * network interfaces, and eventually, to deinitialize %wpa_supplicant.
  3627. */
  3628. struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
  3629. {
  3630. struct wpa_global *global;
  3631. int ret, i;
  3632. if (params == NULL)
  3633. return NULL;
  3634. #ifdef CONFIG_DRIVER_NDIS
  3635. {
  3636. void driver_ndis_init_ops(void);
  3637. driver_ndis_init_ops();
  3638. }
  3639. #endif /* CONFIG_DRIVER_NDIS */
  3640. #ifndef CONFIG_NO_WPA_MSG
  3641. wpa_msg_register_ifname_cb(wpa_supplicant_msg_ifname_cb);
  3642. #endif /* CONFIG_NO_WPA_MSG */
  3643. if (params->wpa_debug_file_path)
  3644. wpa_debug_open_file(params->wpa_debug_file_path);
  3645. else
  3646. wpa_debug_setup_stdout();
  3647. if (params->wpa_debug_syslog)
  3648. wpa_debug_open_syslog();
  3649. if (params->wpa_debug_tracing) {
  3650. ret = wpa_debug_open_linux_tracing();
  3651. if (ret) {
  3652. wpa_printf(MSG_ERROR,
  3653. "Failed to enable trace logging");
  3654. return NULL;
  3655. }
  3656. }
  3657. ret = eap_register_methods();
  3658. if (ret) {
  3659. wpa_printf(MSG_ERROR, "Failed to register EAP methods");
  3660. if (ret == -2)
  3661. wpa_printf(MSG_ERROR, "Two or more EAP methods used "
  3662. "the same EAP type.");
  3663. return NULL;
  3664. }
  3665. global = os_zalloc(sizeof(*global));
  3666. if (global == NULL)
  3667. return NULL;
  3668. dl_list_init(&global->p2p_srv_bonjour);
  3669. dl_list_init(&global->p2p_srv_upnp);
  3670. global->params.daemonize = params->daemonize;
  3671. global->params.wait_for_monitor = params->wait_for_monitor;
  3672. global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
  3673. if (params->pid_file)
  3674. global->params.pid_file = os_strdup(params->pid_file);
  3675. if (params->ctrl_interface)
  3676. global->params.ctrl_interface =
  3677. os_strdup(params->ctrl_interface);
  3678. if (params->ctrl_interface_group)
  3679. global->params.ctrl_interface_group =
  3680. os_strdup(params->ctrl_interface_group);
  3681. if (params->override_driver)
  3682. global->params.override_driver =
  3683. os_strdup(params->override_driver);
  3684. if (params->override_ctrl_interface)
  3685. global->params.override_ctrl_interface =
  3686. os_strdup(params->override_ctrl_interface);
  3687. wpa_debug_level = global->params.wpa_debug_level =
  3688. params->wpa_debug_level;
  3689. wpa_debug_show_keys = global->params.wpa_debug_show_keys =
  3690. params->wpa_debug_show_keys;
  3691. wpa_debug_timestamp = global->params.wpa_debug_timestamp =
  3692. params->wpa_debug_timestamp;
  3693. wpa_printf(MSG_DEBUG, "wpa_supplicant v" VERSION_STR);
  3694. if (eloop_init()) {
  3695. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  3696. wpa_supplicant_deinit(global);
  3697. return NULL;
  3698. }
  3699. random_init(params->entropy_file);
  3700. global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
  3701. if (global->ctrl_iface == NULL) {
  3702. wpa_supplicant_deinit(global);
  3703. return NULL;
  3704. }
  3705. if (wpas_notify_supplicant_initialized(global)) {
  3706. wpa_supplicant_deinit(global);
  3707. return NULL;
  3708. }
  3709. for (i = 0; wpa_drivers[i]; i++)
  3710. global->drv_count++;
  3711. if (global->drv_count == 0) {
  3712. wpa_printf(MSG_ERROR, "No drivers enabled");
  3713. wpa_supplicant_deinit(global);
  3714. return NULL;
  3715. }
  3716. global->drv_priv = os_calloc(global->drv_count, sizeof(void *));
  3717. if (global->drv_priv == NULL) {
  3718. wpa_supplicant_deinit(global);
  3719. return NULL;
  3720. }
  3721. #ifdef CONFIG_WIFI_DISPLAY
  3722. if (wifi_display_init(global) < 0) {
  3723. wpa_printf(MSG_ERROR, "Failed to initialize Wi-Fi Display");
  3724. wpa_supplicant_deinit(global);
  3725. return NULL;
  3726. }
  3727. #endif /* CONFIG_WIFI_DISPLAY */
  3728. return global;
  3729. }
  3730. /**
  3731. * wpa_supplicant_run - Run the %wpa_supplicant main event loop
  3732. * @global: Pointer to global data from wpa_supplicant_init()
  3733. * Returns: 0 after successful event loop run, -1 on failure
  3734. *
  3735. * This function starts the main event loop and continues running as long as
  3736. * there are any remaining events. In most cases, this function is running as
  3737. * long as the %wpa_supplicant process in still in use.
  3738. */
  3739. int wpa_supplicant_run(struct wpa_global *global)
  3740. {
  3741. struct wpa_supplicant *wpa_s;
  3742. if (global->params.daemonize &&
  3743. wpa_supplicant_daemon(global->params.pid_file))
  3744. return -1;
  3745. if (global->params.wait_for_monitor) {
  3746. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
  3747. if (wpa_s->ctrl_iface)
  3748. wpa_supplicant_ctrl_iface_wait(
  3749. wpa_s->ctrl_iface);
  3750. }
  3751. eloop_register_signal_terminate(wpa_supplicant_terminate, global);
  3752. eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
  3753. eloop_run();
  3754. return 0;
  3755. }
  3756. /**
  3757. * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
  3758. * @global: Pointer to global data from wpa_supplicant_init()
  3759. *
  3760. * This function is called to deinitialize %wpa_supplicant and to free all
  3761. * allocated resources. Remaining network interfaces will also be removed.
  3762. */
  3763. void wpa_supplicant_deinit(struct wpa_global *global)
  3764. {
  3765. int i;
  3766. if (global == NULL)
  3767. return;
  3768. #ifdef CONFIG_WIFI_DISPLAY
  3769. wifi_display_deinit(global);
  3770. #endif /* CONFIG_WIFI_DISPLAY */
  3771. while (global->ifaces)
  3772. wpa_supplicant_remove_iface(global, global->ifaces, 1);
  3773. if (global->ctrl_iface)
  3774. wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
  3775. wpas_notify_supplicant_deinitialized(global);
  3776. eap_peer_unregister_methods();
  3777. #ifdef CONFIG_AP
  3778. eap_server_unregister_methods();
  3779. #endif /* CONFIG_AP */
  3780. for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
  3781. if (!global->drv_priv[i])
  3782. continue;
  3783. wpa_drivers[i]->global_deinit(global->drv_priv[i]);
  3784. }
  3785. os_free(global->drv_priv);
  3786. random_deinit();
  3787. eloop_destroy();
  3788. if (global->params.pid_file) {
  3789. os_daemonize_terminate(global->params.pid_file);
  3790. os_free(global->params.pid_file);
  3791. }
  3792. os_free(global->params.ctrl_interface);
  3793. os_free(global->params.ctrl_interface_group);
  3794. os_free(global->params.override_driver);
  3795. os_free(global->params.override_ctrl_interface);
  3796. os_free(global->p2p_disallow_freq.range);
  3797. os_free(global->p2p_go_avoid_freq.range);
  3798. os_free(global->add_psk);
  3799. os_free(global);
  3800. wpa_debug_close_syslog();
  3801. wpa_debug_close_file();
  3802. wpa_debug_close_linux_tracing();
  3803. }
  3804. void wpa_supplicant_update_config(struct wpa_supplicant *wpa_s)
  3805. {
  3806. if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
  3807. wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  3808. char country[3];
  3809. country[0] = wpa_s->conf->country[0];
  3810. country[1] = wpa_s->conf->country[1];
  3811. country[2] = '\0';
  3812. if (wpa_drv_set_country(wpa_s, country) < 0) {
  3813. wpa_printf(MSG_ERROR, "Failed to set country code "
  3814. "'%s'", country);
  3815. }
  3816. }
  3817. if (wpa_s->conf->changed_parameters & CFG_CHANGED_EXT_PW_BACKEND)
  3818. wpas_init_ext_pw(wpa_s);
  3819. #ifdef CONFIG_WPS
  3820. wpas_wps_update_config(wpa_s);
  3821. #endif /* CONFIG_WPS */
  3822. wpas_p2p_update_config(wpa_s);
  3823. wpa_s->conf->changed_parameters = 0;
  3824. }
  3825. void add_freq(int *freqs, int *num_freqs, int freq)
  3826. {
  3827. int i;
  3828. for (i = 0; i < *num_freqs; i++) {
  3829. if (freqs[i] == freq)
  3830. return;
  3831. }
  3832. freqs[*num_freqs] = freq;
  3833. (*num_freqs)++;
  3834. }
  3835. static int * get_bss_freqs_in_ess(struct wpa_supplicant *wpa_s)
  3836. {
  3837. struct wpa_bss *bss, *cbss;
  3838. const int max_freqs = 10;
  3839. int *freqs;
  3840. int num_freqs = 0;
  3841. freqs = os_calloc(max_freqs + 1, sizeof(int));
  3842. if (freqs == NULL)
  3843. return NULL;
  3844. cbss = wpa_s->current_bss;
  3845. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  3846. if (bss == cbss)
  3847. continue;
  3848. if (bss->ssid_len == cbss->ssid_len &&
  3849. os_memcmp(bss->ssid, cbss->ssid, bss->ssid_len) == 0 &&
  3850. wpa_blacklist_get(wpa_s, bss->bssid) == NULL) {
  3851. add_freq(freqs, &num_freqs, bss->freq);
  3852. if (num_freqs == max_freqs)
  3853. break;
  3854. }
  3855. }
  3856. if (num_freqs == 0) {
  3857. os_free(freqs);
  3858. freqs = NULL;
  3859. }
  3860. return freqs;
  3861. }
  3862. void wpas_connection_failed(struct wpa_supplicant *wpa_s, const u8 *bssid)
  3863. {
  3864. int timeout;
  3865. int count;
  3866. int *freqs = NULL;
  3867. wpas_connect_work_done(wpa_s);
  3868. /*
  3869. * Remove possible authentication timeout since the connection failed.
  3870. */
  3871. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  3872. if (wpa_s->disconnected) {
  3873. /*
  3874. * There is no point in blacklisting the AP if this event is
  3875. * generated based on local request to disconnect.
  3876. */
  3877. wpa_dbg(wpa_s, MSG_DEBUG, "Ignore connection failure "
  3878. "indication since interface has been put into "
  3879. "disconnected state");
  3880. return;
  3881. }
  3882. /*
  3883. * Add the failed BSSID into the blacklist and speed up next scan
  3884. * attempt if there could be other APs that could accept association.
  3885. * The current blacklist count indicates how many times we have tried
  3886. * connecting to this AP and multiple attempts mean that other APs are
  3887. * either not available or has already been tried, so that we can start
  3888. * increasing the delay here to avoid constant scanning.
  3889. */
  3890. count = wpa_blacklist_add(wpa_s, bssid);
  3891. if (count == 1 && wpa_s->current_bss) {
  3892. /*
  3893. * This BSS was not in the blacklist before. If there is
  3894. * another BSS available for the same ESS, we should try that
  3895. * next. Otherwise, we may as well try this one once more
  3896. * before allowing other, likely worse, ESSes to be considered.
  3897. */
  3898. freqs = get_bss_freqs_in_ess(wpa_s);
  3899. if (freqs) {
  3900. wpa_dbg(wpa_s, MSG_DEBUG, "Another BSS in this ESS "
  3901. "has been seen; try it next");
  3902. wpa_blacklist_add(wpa_s, bssid);
  3903. /*
  3904. * On the next scan, go through only the known channels
  3905. * used in this ESS based on previous scans to speed up
  3906. * common load balancing use case.
  3907. */
  3908. os_free(wpa_s->next_scan_freqs);
  3909. wpa_s->next_scan_freqs = freqs;
  3910. }
  3911. }
  3912. /*
  3913. * Add previous failure count in case the temporary blacklist was
  3914. * cleared due to no other BSSes being available.
  3915. */
  3916. count += wpa_s->extra_blacklist_count;
  3917. if (count > 3 && wpa_s->current_ssid) {
  3918. wpa_printf(MSG_DEBUG, "Continuous association failures - "
  3919. "consider temporary network disabling");
  3920. wpas_auth_failed(wpa_s, "CONN_FAILED");
  3921. }
  3922. switch (count) {
  3923. case 1:
  3924. timeout = 100;
  3925. break;
  3926. case 2:
  3927. timeout = 500;
  3928. break;
  3929. case 3:
  3930. timeout = 1000;
  3931. break;
  3932. case 4:
  3933. timeout = 5000;
  3934. break;
  3935. default:
  3936. timeout = 10000;
  3937. break;
  3938. }
  3939. wpa_dbg(wpa_s, MSG_DEBUG, "Blacklist count %d --> request scan in %d "
  3940. "ms", count, timeout);
  3941. /*
  3942. * TODO: if more than one possible AP is available in scan results,
  3943. * could try the other ones before requesting a new scan.
  3944. */
  3945. wpa_supplicant_req_scan(wpa_s, timeout / 1000,
  3946. 1000 * (timeout % 1000));
  3947. }
  3948. int wpas_driver_bss_selection(struct wpa_supplicant *wpa_s)
  3949. {
  3950. return wpa_s->conf->ap_scan == 2 ||
  3951. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION);
  3952. }
  3953. #if defined(CONFIG_CTRL_IFACE) || defined(CONFIG_CTRL_IFACE_DBUS_NEW)
  3954. int wpa_supplicant_ctrl_iface_ctrl_rsp_handle(struct wpa_supplicant *wpa_s,
  3955. struct wpa_ssid *ssid,
  3956. const char *field,
  3957. const char *value)
  3958. {
  3959. #ifdef IEEE8021X_EAPOL
  3960. struct eap_peer_config *eap = &ssid->eap;
  3961. wpa_printf(MSG_DEBUG, "CTRL_IFACE: response handle field=%s", field);
  3962. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: response value",
  3963. (const u8 *) value, os_strlen(value));
  3964. switch (wpa_supplicant_ctrl_req_from_string(field)) {
  3965. case WPA_CTRL_REQ_EAP_IDENTITY:
  3966. os_free(eap->identity);
  3967. eap->identity = (u8 *) os_strdup(value);
  3968. eap->identity_len = os_strlen(value);
  3969. eap->pending_req_identity = 0;
  3970. if (ssid == wpa_s->current_ssid)
  3971. wpa_s->reassociate = 1;
  3972. break;
  3973. case WPA_CTRL_REQ_EAP_PASSWORD:
  3974. bin_clear_free(eap->password, eap->password_len);
  3975. eap->password = (u8 *) os_strdup(value);
  3976. eap->password_len = os_strlen(value);
  3977. eap->pending_req_password = 0;
  3978. if (ssid == wpa_s->current_ssid)
  3979. wpa_s->reassociate = 1;
  3980. break;
  3981. case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
  3982. bin_clear_free(eap->new_password, eap->new_password_len);
  3983. eap->new_password = (u8 *) os_strdup(value);
  3984. eap->new_password_len = os_strlen(value);
  3985. eap->pending_req_new_password = 0;
  3986. if (ssid == wpa_s->current_ssid)
  3987. wpa_s->reassociate = 1;
  3988. break;
  3989. case WPA_CTRL_REQ_EAP_PIN:
  3990. str_clear_free(eap->pin);
  3991. eap->pin = os_strdup(value);
  3992. eap->pending_req_pin = 0;
  3993. if (ssid == wpa_s->current_ssid)
  3994. wpa_s->reassociate = 1;
  3995. break;
  3996. case WPA_CTRL_REQ_EAP_OTP:
  3997. bin_clear_free(eap->otp, eap->otp_len);
  3998. eap->otp = (u8 *) os_strdup(value);
  3999. eap->otp_len = os_strlen(value);
  4000. os_free(eap->pending_req_otp);
  4001. eap->pending_req_otp = NULL;
  4002. eap->pending_req_otp_len = 0;
  4003. break;
  4004. case WPA_CTRL_REQ_EAP_PASSPHRASE:
  4005. str_clear_free(eap->private_key_passwd);
  4006. eap->private_key_passwd = os_strdup(value);
  4007. eap->pending_req_passphrase = 0;
  4008. if (ssid == wpa_s->current_ssid)
  4009. wpa_s->reassociate = 1;
  4010. break;
  4011. case WPA_CTRL_REQ_SIM:
  4012. str_clear_free(eap->external_sim_resp);
  4013. eap->external_sim_resp = os_strdup(value);
  4014. break;
  4015. default:
  4016. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown field '%s'", field);
  4017. return -1;
  4018. }
  4019. return 0;
  4020. #else /* IEEE8021X_EAPOL */
  4021. wpa_printf(MSG_DEBUG, "CTRL_IFACE: IEEE 802.1X not included");
  4022. return -1;
  4023. #endif /* IEEE8021X_EAPOL */
  4024. }
  4025. #endif /* CONFIG_CTRL_IFACE || CONFIG_CTRL_IFACE_DBUS_NEW */
  4026. int wpas_network_disabled(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
  4027. {
  4028. int i;
  4029. unsigned int drv_enc;
  4030. if (ssid == NULL)
  4031. return 1;
  4032. if (ssid->disabled)
  4033. return 1;
  4034. if (wpa_s && wpa_s->drv_capa_known)
  4035. drv_enc = wpa_s->drv_enc;
  4036. else
  4037. drv_enc = (unsigned int) -1;
  4038. for (i = 0; i < NUM_WEP_KEYS; i++) {
  4039. size_t len = ssid->wep_key_len[i];
  4040. if (len == 0)
  4041. continue;
  4042. if (len == 5 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP40))
  4043. continue;
  4044. if (len == 13 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP104))
  4045. continue;
  4046. if (len == 16 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP128))
  4047. continue;
  4048. return 1; /* invalid WEP key */
  4049. }
  4050. if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt) && !ssid->psk_set &&
  4051. (!ssid->passphrase || ssid->ssid_len != 0) && !ssid->ext_psk)
  4052. return 1;
  4053. return 0;
  4054. }
  4055. int wpas_is_p2p_prioritized(struct wpa_supplicant *wpa_s)
  4056. {
  4057. if (wpa_s->global->conc_pref == WPA_CONC_PREF_P2P)
  4058. return 1;
  4059. if (wpa_s->global->conc_pref == WPA_CONC_PREF_STA)
  4060. return 0;
  4061. return -1;
  4062. }
  4063. void wpas_auth_failed(struct wpa_supplicant *wpa_s, char *reason)
  4064. {
  4065. struct wpa_ssid *ssid = wpa_s->current_ssid;
  4066. int dur;
  4067. struct os_reltime now;
  4068. if (ssid == NULL) {
  4069. wpa_printf(MSG_DEBUG, "Authentication failure but no known "
  4070. "SSID block");
  4071. return;
  4072. }
  4073. if (ssid->key_mgmt == WPA_KEY_MGMT_WPS)
  4074. return;
  4075. ssid->auth_failures++;
  4076. #ifdef CONFIG_P2P
  4077. if (ssid->p2p_group &&
  4078. (wpa_s->p2p_in_provisioning || wpa_s->show_group_started)) {
  4079. /*
  4080. * Skip the wait time since there is a short timeout on the
  4081. * connection to a P2P group.
  4082. */
  4083. return;
  4084. }
  4085. #endif /* CONFIG_P2P */
  4086. if (ssid->auth_failures > 50)
  4087. dur = 300;
  4088. else if (ssid->auth_failures > 10)
  4089. dur = 120;
  4090. else if (ssid->auth_failures > 5)
  4091. dur = 90;
  4092. else if (ssid->auth_failures > 3)
  4093. dur = 60;
  4094. else if (ssid->auth_failures > 2)
  4095. dur = 30;
  4096. else if (ssid->auth_failures > 1)
  4097. dur = 20;
  4098. else
  4099. dur = 10;
  4100. if (ssid->auth_failures > 1 &&
  4101. wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt))
  4102. dur += os_random() % (ssid->auth_failures * 10);
  4103. os_get_reltime(&now);
  4104. if (now.sec + dur <= ssid->disabled_until.sec)
  4105. return;
  4106. ssid->disabled_until.sec = now.sec + dur;
  4107. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TEMP_DISABLED
  4108. "id=%d ssid=\"%s\" auth_failures=%u duration=%d reason=%s",
  4109. ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
  4110. ssid->auth_failures, dur, reason);
  4111. }
  4112. void wpas_clear_temp_disabled(struct wpa_supplicant *wpa_s,
  4113. struct wpa_ssid *ssid, int clear_failures)
  4114. {
  4115. if (ssid == NULL)
  4116. return;
  4117. if (ssid->disabled_until.sec) {
  4118. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_REENABLED
  4119. "id=%d ssid=\"%s\"",
  4120. ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  4121. }
  4122. ssid->disabled_until.sec = 0;
  4123. ssid->disabled_until.usec = 0;
  4124. if (clear_failures)
  4125. ssid->auth_failures = 0;
  4126. }
  4127. int disallowed_bssid(struct wpa_supplicant *wpa_s, const u8 *bssid)
  4128. {
  4129. size_t i;
  4130. if (wpa_s->disallow_aps_bssid == NULL)
  4131. return 0;
  4132. for (i = 0; i < wpa_s->disallow_aps_bssid_count; i++) {
  4133. if (os_memcmp(wpa_s->disallow_aps_bssid + i * ETH_ALEN,
  4134. bssid, ETH_ALEN) == 0)
  4135. return 1;
  4136. }
  4137. return 0;
  4138. }
  4139. int disallowed_ssid(struct wpa_supplicant *wpa_s, const u8 *ssid,
  4140. size_t ssid_len)
  4141. {
  4142. size_t i;
  4143. if (wpa_s->disallow_aps_ssid == NULL || ssid == NULL)
  4144. return 0;
  4145. for (i = 0; i < wpa_s->disallow_aps_ssid_count; i++) {
  4146. struct wpa_ssid_value *s = &wpa_s->disallow_aps_ssid[i];
  4147. if (ssid_len == s->ssid_len &&
  4148. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  4149. return 1;
  4150. }
  4151. return 0;
  4152. }
  4153. /**
  4154. * wpas_request_connection - Request a new connection
  4155. * @wpa_s: Pointer to the network interface
  4156. *
  4157. * This function is used to request a new connection to be found. It will mark
  4158. * the interface to allow reassociation and request a new scan to find a
  4159. * suitable network to connect to.
  4160. */
  4161. void wpas_request_connection(struct wpa_supplicant *wpa_s)
  4162. {
  4163. wpa_s->normal_scans = 0;
  4164. wpa_s->scan_req = NORMAL_SCAN_REQ;
  4165. wpa_supplicant_reinit_autoscan(wpa_s);
  4166. wpa_s->extra_blacklist_count = 0;
  4167. wpa_s->disconnected = 0;
  4168. wpa_s->reassociate = 1;
  4169. if (wpa_supplicant_fast_associate(wpa_s) != 1)
  4170. wpa_supplicant_req_scan(wpa_s, 0, 0);
  4171. }
  4172. void dump_freq_data(struct wpa_supplicant *wpa_s, const char *title,
  4173. struct wpa_used_freq_data *freqs_data,
  4174. unsigned int len)
  4175. {
  4176. unsigned int i;
  4177. wpa_dbg(wpa_s, MSG_DEBUG, "Shared frequencies (len=%u): %s",
  4178. len, title);
  4179. for (i = 0; i < len; i++) {
  4180. struct wpa_used_freq_data *cur = &freqs_data[i];
  4181. wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d, flags=0x%X",
  4182. i, cur->freq, cur->flags);
  4183. }
  4184. }
  4185. /*
  4186. * Find the operating frequencies of any of the virtual interfaces that
  4187. * are using the same radio as the current interface, and in addition, get
  4188. * information about the interface types that are using the frequency.
  4189. */
  4190. int get_shared_radio_freqs_data(struct wpa_supplicant *wpa_s,
  4191. struct wpa_used_freq_data *freqs_data,
  4192. unsigned int len)
  4193. {
  4194. struct wpa_supplicant *ifs;
  4195. u8 bssid[ETH_ALEN];
  4196. int freq;
  4197. unsigned int idx = 0, i;
  4198. wpa_dbg(wpa_s, MSG_DEBUG,
  4199. "Determining shared radio frequencies (max len %u)", len);
  4200. os_memset(freqs_data, 0, sizeof(struct wpa_used_freq_data) * len);
  4201. dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
  4202. radio_list) {
  4203. if (idx == len)
  4204. break;
  4205. if (ifs->current_ssid == NULL || ifs->assoc_freq == 0)
  4206. continue;
  4207. if (ifs->current_ssid->mode == WPAS_MODE_AP ||
  4208. ifs->current_ssid->mode == WPAS_MODE_P2P_GO)
  4209. freq = ifs->current_ssid->frequency;
  4210. else if (wpa_drv_get_bssid(ifs, bssid) == 0)
  4211. freq = ifs->assoc_freq;
  4212. else
  4213. continue;
  4214. /* Hold only distinct freqs */
  4215. for (i = 0; i < idx; i++)
  4216. if (freqs_data[i].freq == freq)
  4217. break;
  4218. if (i == idx)
  4219. freqs_data[idx++].freq = freq;
  4220. if (ifs->current_ssid->mode == WPAS_MODE_INFRA) {
  4221. freqs_data[i].flags = ifs->current_ssid->p2p_group ?
  4222. WPA_FREQ_USED_BY_P2P_CLIENT :
  4223. WPA_FREQ_USED_BY_INFRA_STATION;
  4224. }
  4225. }
  4226. dump_freq_data(wpa_s, "completed iteration", freqs_data, idx);
  4227. return idx;
  4228. }
  4229. /*
  4230. * Find the operating frequencies of any of the virtual interfaces that
  4231. * are using the same radio as the current interface.
  4232. */
  4233. int get_shared_radio_freqs(struct wpa_supplicant *wpa_s,
  4234. int *freq_array, unsigned int len)
  4235. {
  4236. struct wpa_used_freq_data *freqs_data;
  4237. int num, i;
  4238. os_memset(freq_array, 0, sizeof(int) * len);
  4239. freqs_data = os_calloc(len, sizeof(struct wpa_used_freq_data));
  4240. if (!freqs_data)
  4241. return -1;
  4242. num = get_shared_radio_freqs_data(wpa_s, freqs_data, len);
  4243. for (i = 0; i < num; i++)
  4244. freq_array[i] = freqs_data[i].freq;
  4245. os_free(freqs_data);
  4246. return num;
  4247. }
  4248. static void wpas_rrm_neighbor_rep_timeout_handler(void *data, void *user_ctx)
  4249. {
  4250. struct rrm_data *rrm = data;
  4251. if (!rrm->notify_neighbor_rep) {
  4252. wpa_printf(MSG_ERROR,
  4253. "RRM: Unexpected neighbor report timeout");
  4254. return;
  4255. }
  4256. wpa_printf(MSG_DEBUG, "RRM: Notifying neighbor report - NONE");
  4257. rrm->notify_neighbor_rep(rrm->neighbor_rep_cb_ctx, NULL);
  4258. rrm->notify_neighbor_rep = NULL;
  4259. rrm->neighbor_rep_cb_ctx = NULL;
  4260. }
  4261. /*
  4262. * wpas_rrm_reset - Clear and reset all RRM data in wpa_supplicant
  4263. * @wpa_s: Pointer to wpa_supplicant
  4264. */
  4265. void wpas_rrm_reset(struct wpa_supplicant *wpa_s)
  4266. {
  4267. wpa_s->rrm.rrm_used = 0;
  4268. eloop_cancel_timeout(wpas_rrm_neighbor_rep_timeout_handler, &wpa_s->rrm,
  4269. NULL);
  4270. if (wpa_s->rrm.notify_neighbor_rep)
  4271. wpas_rrm_neighbor_rep_timeout_handler(&wpa_s->rrm, NULL);
  4272. wpa_s->rrm.next_neighbor_rep_token = 1;
  4273. }
  4274. /*
  4275. * wpas_rrm_process_neighbor_rep - Handle incoming neighbor report
  4276. * @wpa_s: Pointer to wpa_supplicant
  4277. * @report: Neighbor report buffer, prefixed by a 1-byte dialog token
  4278. * @report_len: Length of neighbor report buffer
  4279. */
  4280. void wpas_rrm_process_neighbor_rep(struct wpa_supplicant *wpa_s,
  4281. const u8 *report, size_t report_len)
  4282. {
  4283. struct wpabuf *neighbor_rep;
  4284. wpa_hexdump(MSG_DEBUG, "RRM: New Neighbor Report", report, report_len);
  4285. if (report_len < 1)
  4286. return;
  4287. if (report[0] != wpa_s->rrm.next_neighbor_rep_token - 1) {
  4288. wpa_printf(MSG_DEBUG,
  4289. "RRM: Discarding neighbor report with token %d (expected %d)",
  4290. report[0], wpa_s->rrm.next_neighbor_rep_token - 1);
  4291. return;
  4292. }
  4293. eloop_cancel_timeout(wpas_rrm_neighbor_rep_timeout_handler, &wpa_s->rrm,
  4294. NULL);
  4295. if (!wpa_s->rrm.notify_neighbor_rep) {
  4296. wpa_printf(MSG_ERROR, "RRM: Unexpected neighbor report");
  4297. return;
  4298. }
  4299. /* skipping the first byte, which is only an id (dialog token) */
  4300. neighbor_rep = wpabuf_alloc(report_len - 1);
  4301. if (neighbor_rep == NULL)
  4302. return;
  4303. wpabuf_put_data(neighbor_rep, report + 1, report_len - 1);
  4304. wpa_printf(MSG_DEBUG, "RRM: Notifying neighbor report (token = %d)",
  4305. report[0]);
  4306. wpa_s->rrm.notify_neighbor_rep(wpa_s->rrm.neighbor_rep_cb_ctx,
  4307. neighbor_rep);
  4308. wpa_s->rrm.notify_neighbor_rep = NULL;
  4309. wpa_s->rrm.neighbor_rep_cb_ctx = NULL;
  4310. }
  4311. /**
  4312. * wpas_rrm_send_neighbor_rep_request - Request a neighbor report from our AP
  4313. * @wpa_s: Pointer to wpa_supplicant
  4314. * @ssid: if not null, this is sent in the request. Otherwise, no SSID IE
  4315. * is sent in the request.
  4316. * @cb: Callback function to be called once the requested report arrives, or
  4317. * timed out after RRM_NEIGHBOR_REPORT_TIMEOUT seconds.
  4318. * In the former case, 'neighbor_rep' is a newly allocated wpabuf, and it's
  4319. * the requester's responsibility to free it.
  4320. * In the latter case NULL will be sent in 'neighbor_rep'.
  4321. * @cb_ctx: Context value to send the callback function
  4322. * Returns: 0 in case of success, negative error code otherwise
  4323. *
  4324. * In case there is a previous request which has not been answered yet, the
  4325. * new request fails. The caller may retry after RRM_NEIGHBOR_REPORT_TIMEOUT.
  4326. * Request must contain a callback function.
  4327. */
  4328. int wpas_rrm_send_neighbor_rep_request(struct wpa_supplicant *wpa_s,
  4329. const struct wpa_ssid *ssid,
  4330. void (*cb)(void *ctx,
  4331. struct wpabuf *neighbor_rep),
  4332. void *cb_ctx)
  4333. {
  4334. struct wpabuf *buf;
  4335. const u8 *rrm_ie;
  4336. if (wpa_s->wpa_state != WPA_COMPLETED || wpa_s->current_ssid == NULL) {
  4337. wpa_printf(MSG_DEBUG, "RRM: No connection, no RRM.");
  4338. return -ENOTCONN;
  4339. }
  4340. if (!wpa_s->rrm.rrm_used) {
  4341. wpa_printf(MSG_DEBUG, "RRM: No RRM in current connection.");
  4342. return -EOPNOTSUPP;
  4343. }
  4344. rrm_ie = wpa_bss_get_ie(wpa_s->current_bss,
  4345. WLAN_EID_RRM_ENABLED_CAPABILITIES);
  4346. if (!rrm_ie || !(wpa_s->current_bss->caps & IEEE80211_CAP_RRM) ||
  4347. !(rrm_ie[2] & WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
  4348. wpa_printf(MSG_DEBUG,
  4349. "RRM: No network support for Neighbor Report.");
  4350. return -EOPNOTSUPP;
  4351. }
  4352. if (!cb) {
  4353. wpa_printf(MSG_DEBUG,
  4354. "RRM: Neighbor Report request must provide a callback.");
  4355. return -EINVAL;
  4356. }
  4357. /* Refuse if there's a live request */
  4358. if (wpa_s->rrm.notify_neighbor_rep) {
  4359. wpa_printf(MSG_DEBUG,
  4360. "RRM: Currently handling previous Neighbor Report.");
  4361. return -EBUSY;
  4362. }
  4363. /* 3 = action category + action code + dialog token */
  4364. buf = wpabuf_alloc(3 + (ssid ? 2 + ssid->ssid_len : 0));
  4365. if (buf == NULL) {
  4366. wpa_printf(MSG_DEBUG,
  4367. "RRM: Failed to allocate Neighbor Report Request");
  4368. return -ENOMEM;
  4369. }
  4370. wpa_printf(MSG_DEBUG, "RRM: Neighbor report request (for %s), token=%d",
  4371. (ssid ? wpa_ssid_txt(ssid->ssid, ssid->ssid_len) : ""),
  4372. wpa_s->rrm.next_neighbor_rep_token);
  4373. wpabuf_put_u8(buf, WLAN_ACTION_RADIO_MEASUREMENT);
  4374. wpabuf_put_u8(buf, WLAN_RRM_NEIGHBOR_REPORT_REQUEST);
  4375. wpabuf_put_u8(buf, wpa_s->rrm.next_neighbor_rep_token);
  4376. if (ssid) {
  4377. wpabuf_put_u8(buf, WLAN_EID_SSID);
  4378. wpabuf_put_u8(buf, ssid->ssid_len);
  4379. wpabuf_put_data(buf, ssid->ssid, ssid->ssid_len);
  4380. }
  4381. wpa_s->rrm.next_neighbor_rep_token++;
  4382. if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
  4383. wpa_s->own_addr, wpa_s->bssid,
  4384. wpabuf_head(buf), wpabuf_len(buf), 0) < 0) {
  4385. wpa_printf(MSG_DEBUG,
  4386. "RRM: Failed to send Neighbor Report Request");
  4387. wpabuf_free(buf);
  4388. return -ECANCELED;
  4389. }
  4390. wpa_s->rrm.neighbor_rep_cb_ctx = cb_ctx;
  4391. wpa_s->rrm.notify_neighbor_rep = cb;
  4392. eloop_register_timeout(RRM_NEIGHBOR_REPORT_TIMEOUT, 0,
  4393. wpas_rrm_neighbor_rep_timeout_handler,
  4394. &wpa_s->rrm, NULL);
  4395. wpabuf_free(buf);
  4396. return 0;
  4397. }
  4398. void wpas_rrm_handle_link_measurement_request(struct wpa_supplicant *wpa_s,
  4399. const u8 *src,
  4400. const u8 *frame, size_t len,
  4401. int rssi)
  4402. {
  4403. struct wpabuf *buf;
  4404. const struct rrm_link_measurement_request *req;
  4405. struct rrm_link_measurement_report report;
  4406. if (wpa_s->wpa_state != WPA_COMPLETED) {
  4407. wpa_printf(MSG_INFO,
  4408. "RRM: Ignoring link measurement request. Not associated");
  4409. return;
  4410. }
  4411. if (!wpa_s->rrm.rrm_used) {
  4412. wpa_printf(MSG_INFO,
  4413. "RRM: Ignoring link measurement request. Not RRM network");
  4414. return;
  4415. }
  4416. if (!(wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION)) {
  4417. wpa_printf(MSG_INFO,
  4418. "RRM: Measurement report failed. TX power insertion not supported");
  4419. return;
  4420. }
  4421. req = (const struct rrm_link_measurement_request *) frame;
  4422. if (len < sizeof(*req)) {
  4423. wpa_printf(MSG_INFO,
  4424. "RRM: Link measurement report failed. Request too short");
  4425. return;
  4426. }
  4427. os_memset(&report, 0, sizeof(report));
  4428. report.tpc.eid = WLAN_EID_TPC_REPORT;
  4429. report.tpc.len = 2;
  4430. report.rsni = 255; /* 255 indicates that RSNI is not available */
  4431. report.dialog_token = req->dialog_token;
  4432. /*
  4433. * It's possible to estimate RCPI based on RSSI in dBm. This
  4434. * calculation will not reflect the correct value for high rates,
  4435. * but it's good enough for Action frames which are transmitted
  4436. * with up to 24 Mbps rates.
  4437. */
  4438. if (!rssi)
  4439. report.rcpi = 255; /* not available */
  4440. else if (rssi < -110)
  4441. report.rcpi = 0;
  4442. else if (rssi > 0)
  4443. report.rcpi = 220;
  4444. else
  4445. report.rcpi = (rssi + 110) * 2;
  4446. /* action_category + action_code */
  4447. buf = wpabuf_alloc(2 + sizeof(report));
  4448. if (buf == NULL) {
  4449. wpa_printf(MSG_ERROR,
  4450. "RRM: Link measurement report failed. Buffer allocation failed");
  4451. return;
  4452. }
  4453. wpabuf_put_u8(buf, WLAN_ACTION_RADIO_MEASUREMENT);
  4454. wpabuf_put_u8(buf, WLAN_RRM_LINK_MEASUREMENT_REPORT);
  4455. wpabuf_put_data(buf, &report, sizeof(report));
  4456. wpa_hexdump(MSG_DEBUG, "RRM: Link measurement report:",
  4457. wpabuf_head(buf), wpabuf_len(buf));
  4458. if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, src,
  4459. wpa_s->own_addr, wpa_s->bssid,
  4460. wpabuf_head(buf), wpabuf_len(buf), 0)) {
  4461. wpa_printf(MSG_ERROR,
  4462. "RRM: Link measurement report failed. Send action failed");
  4463. }
  4464. wpabuf_free(buf);
  4465. }