wpa_supplicant.c 153 KB

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