wpa_supplicant.c 167 KB

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