wpa_supplicant.c 174 KB

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