wpa_supplicant.c 154 KB

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