p2p_supplicant.c 253 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976
  1. /*
  2. * wpa_supplicant - P2P
  3. * Copyright (c) 2009-2010, Atheros Communications
  4. * Copyright (c) 2010-2014, Jouni Malinen <j@w1.fi>
  5. *
  6. * This software may be distributed under the terms of the BSD license.
  7. * See README for more details.
  8. */
  9. #include "includes.h"
  10. #include "common.h"
  11. #include "eloop.h"
  12. #include "common/ieee802_11_common.h"
  13. #include "common/ieee802_11_defs.h"
  14. #include "common/wpa_ctrl.h"
  15. #include "wps/wps_i.h"
  16. #include "p2p/p2p.h"
  17. #include "ap/hostapd.h"
  18. #include "ap/ap_config.h"
  19. #include "ap/sta_info.h"
  20. #include "ap/ap_drv_ops.h"
  21. #include "ap/wps_hostapd.h"
  22. #include "ap/p2p_hostapd.h"
  23. #include "ap/dfs.h"
  24. #include "eapol_supp/eapol_supp_sm.h"
  25. #include "rsn_supp/wpa.h"
  26. #include "wpa_supplicant_i.h"
  27. #include "driver_i.h"
  28. #include "ap.h"
  29. #include "config_ssid.h"
  30. #include "config.h"
  31. #include "notify.h"
  32. #include "scan.h"
  33. #include "bss.h"
  34. #include "offchannel.h"
  35. #include "wps_supplicant.h"
  36. #include "p2p_supplicant.h"
  37. #include "wifi_display.h"
  38. /*
  39. * How many times to try to scan to find the GO before giving up on join
  40. * request.
  41. */
  42. #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
  43. #define P2P_AUTO_PD_SCAN_ATTEMPTS 5
  44. /**
  45. * Defines time interval in seconds when a GO needs to evacuate a frequency that
  46. * it is currently using, but is no longer valid for P2P use cases.
  47. */
  48. #define P2P_GO_FREQ_CHANGE_TIME 5
  49. /**
  50. * Defines CSA parameters which are used when GO evacuates the no longer valid
  51. * channel (and if the driver supports channel switch).
  52. */
  53. #define P2P_GO_CSA_COUNT 7
  54. #define P2P_GO_CSA_BLOCK_TX 0
  55. #ifndef P2P_MAX_CLIENT_IDLE
  56. /*
  57. * How many seconds to try to reconnect to the GO when connection in P2P client
  58. * role has been lost.
  59. */
  60. #define P2P_MAX_CLIENT_IDLE 10
  61. #endif /* P2P_MAX_CLIENT_IDLE */
  62. #ifndef P2P_MAX_INITIAL_CONN_WAIT
  63. /*
  64. * How many seconds to wait for initial 4-way handshake to get completed after
  65. * WPS provisioning step or after the re-invocation of a persistent group on a
  66. * P2P Client.
  67. */
  68. #define P2P_MAX_INITIAL_CONN_WAIT 10
  69. #endif /* P2P_MAX_INITIAL_CONN_WAIT */
  70. #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
  71. /*
  72. * How many seconds to wait for initial 4-way handshake to get completed after
  73. * WPS provisioning step on the GO. This controls the extra time the P2P
  74. * operation is considered to be in progress (e.g., to delay other scans) after
  75. * WPS provisioning has been completed on the GO during group formation.
  76. */
  77. #define P2P_MAX_INITIAL_CONN_WAIT_GO 10
  78. #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
  79. #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
  80. /*
  81. * How many seconds to wait for initial 4-way handshake to get completed after
  82. * re-invocation of a persistent group on the GO when the client is expected
  83. * to connect automatically (no user interaction).
  84. */
  85. #define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
  86. #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
  87. #define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
  88. /*
  89. * How many seconds to wait to re-attempt to move GOs, in case previous attempt
  90. * was not possible.
  91. */
  92. #define P2P_RECONSIDER_GO_MOVE_DELAY 30
  93. enum p2p_group_removal_reason {
  94. P2P_GROUP_REMOVAL_UNKNOWN,
  95. P2P_GROUP_REMOVAL_SILENT,
  96. P2P_GROUP_REMOVAL_FORMATION_FAILED,
  97. P2P_GROUP_REMOVAL_REQUESTED,
  98. P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
  99. P2P_GROUP_REMOVAL_UNAVAILABLE,
  100. P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
  101. P2P_GROUP_REMOVAL_PSK_FAILURE,
  102. P2P_GROUP_REMOVAL_FREQ_CONFLICT,
  103. P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL
  104. };
  105. static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
  106. static struct wpa_supplicant *
  107. wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
  108. int go);
  109. static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
  110. const u8 *ssid, size_t ssid_len);
  111. static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
  112. int *force_freq, int *pref_freq, int go,
  113. unsigned int *pref_freq_list,
  114. unsigned int *num_pref_freq);
  115. static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
  116. const u8 *ssid, size_t ssid_len);
  117. static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
  118. static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
  119. const u8 *dev_addr, enum p2p_wps_method wps_method,
  120. int auto_join, int freq,
  121. const u8 *ssid, size_t ssid_len);
  122. static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
  123. static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
  124. static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
  125. static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
  126. static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
  127. void *timeout_ctx);
  128. static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
  129. static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
  130. int group_added);
  131. static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
  132. static void wpas_stop_listen(void *ctx);
  133. static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
  134. static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s);
  135. static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
  136. enum wpa_driver_if_type type);
  137. static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
  138. int already_deleted);
  139. static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
  140. struct wpa_used_freq_data *freqs,
  141. unsigned int num);
  142. static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx);
  143. static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq);
  144. static void
  145. wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
  146. struct wpa_used_freq_data *freqs, unsigned int num,
  147. enum wpas_p2p_channel_update_trig trig);
  148. static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx);
  149. /*
  150. * Get the number of concurrent channels that the HW can operate, but that are
  151. * currently not in use by any of the wpa_supplicant interfaces.
  152. */
  153. static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
  154. {
  155. int *freqs;
  156. int num, unused;
  157. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  158. if (!freqs)
  159. return -1;
  160. num = get_shared_radio_freqs(wpa_s, freqs,
  161. wpa_s->num_multichan_concurrent);
  162. os_free(freqs);
  163. unused = wpa_s->num_multichan_concurrent - num;
  164. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
  165. return unused;
  166. }
  167. /*
  168. * Get the frequencies that are currently in use by one or more of the virtual
  169. * interfaces, and that are also valid for P2P operation.
  170. */
  171. static unsigned int
  172. wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
  173. struct wpa_used_freq_data *p2p_freqs,
  174. unsigned int len)
  175. {
  176. struct wpa_used_freq_data *freqs;
  177. unsigned int num, i, j;
  178. freqs = os_calloc(wpa_s->num_multichan_concurrent,
  179. sizeof(struct wpa_used_freq_data));
  180. if (!freqs)
  181. return 0;
  182. num = get_shared_radio_freqs_data(wpa_s, freqs,
  183. wpa_s->num_multichan_concurrent);
  184. os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
  185. for (i = 0, j = 0; i < num && j < len; i++) {
  186. if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
  187. p2p_freqs[j++] = freqs[i];
  188. }
  189. os_free(freqs);
  190. dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
  191. return j;
  192. }
  193. static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
  194. int freq)
  195. {
  196. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  197. return;
  198. /* Use the wpa_s used to control the P2P Device operation */
  199. wpa_s = wpa_s->global->p2p_init_wpa_s;
  200. if (wpa_s->conf->p2p_ignore_shared_freq &&
  201. freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
  202. wpas_p2p_num_unused_channels(wpa_s) > 0) {
  203. wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
  204. freq);
  205. freq = 0;
  206. }
  207. p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
  208. }
  209. static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
  210. struct wpa_scan_results *scan_res)
  211. {
  212. size_t i;
  213. if (wpa_s->p2p_scan_work) {
  214. struct wpa_radio_work *work = wpa_s->p2p_scan_work;
  215. wpa_s->p2p_scan_work = NULL;
  216. radio_work_done(work);
  217. }
  218. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  219. return;
  220. wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
  221. (int) scan_res->num);
  222. for (i = 0; i < scan_res->num; i++) {
  223. struct wpa_scan_res *bss = scan_res->res[i];
  224. struct os_reltime time_tmp_age, entry_ts;
  225. const u8 *ies;
  226. size_t ies_len;
  227. time_tmp_age.sec = bss->age / 1000;
  228. time_tmp_age.usec = (bss->age % 1000) * 1000;
  229. os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
  230. ies = (const u8 *) (bss + 1);
  231. ies_len = bss->ie_len;
  232. if (bss->beacon_ie_len > 0 &&
  233. !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
  234. wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
  235. wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
  236. MACSTR, MAC2STR(bss->bssid));
  237. ies = ies + ies_len;
  238. ies_len = bss->beacon_ie_len;
  239. }
  240. if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
  241. bss->freq, &entry_ts, bss->level,
  242. ies, ies_len) > 0)
  243. break;
  244. }
  245. p2p_scan_res_handled(wpa_s->global->p2p);
  246. }
  247. static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
  248. {
  249. struct wpa_supplicant *wpa_s = work->wpa_s;
  250. struct wpa_driver_scan_params *params = work->ctx;
  251. int ret;
  252. if (deinit) {
  253. if (!work->started) {
  254. wpa_scan_free_params(params);
  255. return;
  256. }
  257. wpa_s->p2p_scan_work = NULL;
  258. return;
  259. }
  260. ret = wpa_drv_scan(wpa_s, params);
  261. wpa_scan_free_params(params);
  262. work->ctx = NULL;
  263. if (ret) {
  264. radio_work_done(work);
  265. p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
  266. return;
  267. }
  268. p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
  269. os_get_reltime(&wpa_s->scan_trigger_time);
  270. wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
  271. wpa_s->own_scan_requested = 1;
  272. wpa_s->p2p_scan_work = work;
  273. }
  274. static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
  275. int freq)
  276. {
  277. if (wpa_s->global->p2p_24ghz_social_channels &&
  278. (freq == 2412 || freq == 2437 || freq == 2462)) {
  279. /*
  280. * Search all social channels regardless of whether these have
  281. * been disabled for P2P operating channel use to avoid missing
  282. * peers.
  283. */
  284. return 1;
  285. }
  286. return p2p_supported_freq(wpa_s->global->p2p, freq);
  287. }
  288. static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
  289. unsigned int num_req_dev_types,
  290. const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
  291. {
  292. struct wpa_supplicant *wpa_s = ctx;
  293. struct wpa_driver_scan_params *params = NULL;
  294. struct wpabuf *wps_ie, *ies;
  295. unsigned int num_channels = 0;
  296. int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
  297. size_t ielen;
  298. u8 *n, i;
  299. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  300. return -1;
  301. if (wpa_s->p2p_scan_work) {
  302. wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
  303. return -1;
  304. }
  305. params = os_zalloc(sizeof(*params));
  306. if (params == NULL)
  307. return -1;
  308. /* P2P Wildcard SSID */
  309. params->num_ssids = 1;
  310. n = os_malloc(P2P_WILDCARD_SSID_LEN);
  311. if (n == NULL)
  312. goto fail;
  313. os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
  314. params->ssids[0].ssid = n;
  315. params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
  316. wpa_s->wps->dev.p2p = 1;
  317. wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
  318. wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
  319. num_req_dev_types, req_dev_types);
  320. if (wps_ie == NULL)
  321. goto fail;
  322. ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
  323. ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
  324. if (ies == NULL) {
  325. wpabuf_free(wps_ie);
  326. goto fail;
  327. }
  328. wpabuf_put_buf(ies, wps_ie);
  329. wpabuf_free(wps_ie);
  330. p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
  331. params->p2p_probe = 1;
  332. n = os_malloc(wpabuf_len(ies));
  333. if (n == NULL) {
  334. wpabuf_free(ies);
  335. goto fail;
  336. }
  337. os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
  338. params->extra_ies = n;
  339. params->extra_ies_len = wpabuf_len(ies);
  340. wpabuf_free(ies);
  341. switch (type) {
  342. case P2P_SCAN_SOCIAL:
  343. params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
  344. sizeof(int));
  345. if (params->freqs == NULL)
  346. goto fail;
  347. for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
  348. if (wpas_p2p_search_social_channel(
  349. wpa_s, social_channels_freq[i]))
  350. params->freqs[num_channels++] =
  351. social_channels_freq[i];
  352. }
  353. params->freqs[num_channels++] = 0;
  354. break;
  355. case P2P_SCAN_FULL:
  356. break;
  357. case P2P_SCAN_SPECIFIC:
  358. params->freqs = os_calloc(2, sizeof(int));
  359. if (params->freqs == NULL)
  360. goto fail;
  361. params->freqs[0] = freq;
  362. params->freqs[1] = 0;
  363. break;
  364. case P2P_SCAN_SOCIAL_PLUS_ONE:
  365. params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
  366. sizeof(int));
  367. if (params->freqs == NULL)
  368. goto fail;
  369. for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
  370. if (wpas_p2p_search_social_channel(
  371. wpa_s, social_channels_freq[i]))
  372. params->freqs[num_channels++] =
  373. social_channels_freq[i];
  374. }
  375. if (p2p_supported_freq(wpa_s->global->p2p, freq))
  376. params->freqs[num_channels++] = freq;
  377. params->freqs[num_channels++] = 0;
  378. break;
  379. }
  380. radio_remove_works(wpa_s, "p2p-scan", 0);
  381. if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
  382. params) < 0)
  383. goto fail;
  384. return 0;
  385. fail:
  386. wpa_scan_free_params(params);
  387. return -1;
  388. }
  389. static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
  390. {
  391. switch (p2p_group_interface) {
  392. case P2P_GROUP_INTERFACE_PENDING:
  393. return WPA_IF_P2P_GROUP;
  394. case P2P_GROUP_INTERFACE_GO:
  395. return WPA_IF_P2P_GO;
  396. case P2P_GROUP_INTERFACE_CLIENT:
  397. return WPA_IF_P2P_CLIENT;
  398. }
  399. return WPA_IF_P2P_GROUP;
  400. }
  401. static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
  402. const u8 *ssid,
  403. size_t ssid_len, int *go)
  404. {
  405. struct wpa_ssid *s;
  406. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  407. for (s = wpa_s->conf->ssid; s; s = s->next) {
  408. if (s->disabled != 0 || !s->p2p_group ||
  409. s->ssid_len != ssid_len ||
  410. os_memcmp(ssid, s->ssid, ssid_len) != 0)
  411. continue;
  412. if (s->mode == WPAS_MODE_P2P_GO &&
  413. s != wpa_s->current_ssid)
  414. continue;
  415. if (go)
  416. *go = s->mode == WPAS_MODE_P2P_GO;
  417. return wpa_s;
  418. }
  419. }
  420. return NULL;
  421. }
  422. static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
  423. {
  424. struct wpa_supplicant *wpa_s = eloop_ctx;
  425. wpa_printf(MSG_DEBUG,
  426. "P2P: Complete previously requested removal of %s",
  427. wpa_s->ifname);
  428. wpas_p2p_disconnect(wpa_s);
  429. }
  430. static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
  431. struct wpa_supplicant *calling_wpa_s)
  432. {
  433. if (calling_wpa_s == wpa_s && wpa_s &&
  434. wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
  435. /*
  436. * The calling wpa_s instance is going to be removed. Do that
  437. * from an eloop callback to keep the instance available until
  438. * the caller has returned. This my be needed, e.g., to provide
  439. * control interface responses on the per-interface socket.
  440. */
  441. if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
  442. wpa_s, NULL) < 0)
  443. return -1;
  444. return 0;
  445. }
  446. return wpas_p2p_disconnect(wpa_s);
  447. }
  448. /* Determine total number of clients in active groups where we are the GO */
  449. static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
  450. {
  451. unsigned int count = 0;
  452. struct wpa_ssid *s;
  453. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  454. for (s = wpa_s->conf->ssid; s; s = s->next) {
  455. wpa_printf(MSG_DEBUG,
  456. "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
  457. wpa_s, s, s->disabled, s->p2p_group,
  458. s->mode);
  459. if (!s->disabled && s->p2p_group &&
  460. s->mode == WPAS_MODE_P2P_GO) {
  461. count += p2p_get_group_num_members(
  462. wpa_s->p2p_group);
  463. }
  464. }
  465. }
  466. return count;
  467. }
  468. static unsigned int p2p_is_active_persistent_group(struct wpa_supplicant *wpa_s)
  469. {
  470. return !wpa_s->p2p_mgmt && wpa_s->current_ssid &&
  471. !wpa_s->current_ssid->disabled &&
  472. wpa_s->current_ssid->p2p_group &&
  473. wpa_s->current_ssid->p2p_persistent_group;
  474. }
  475. static unsigned int p2p_is_active_persistent_go(struct wpa_supplicant *wpa_s)
  476. {
  477. return p2p_is_active_persistent_group(wpa_s) &&
  478. wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO;
  479. }
  480. /* Find an interface for a P2P group where we are the GO */
  481. static struct wpa_supplicant *
  482. wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
  483. {
  484. struct wpa_supplicant *save = NULL;
  485. if (!wpa_s)
  486. return NULL;
  487. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  488. if (!p2p_is_active_persistent_go(wpa_s))
  489. continue;
  490. /* Prefer a group with connected clients */
  491. if (p2p_get_group_num_members(wpa_s->p2p_group))
  492. return wpa_s;
  493. save = wpa_s;
  494. }
  495. /* No group with connected clients, so pick the one without (if any) */
  496. return save;
  497. }
  498. static unsigned int p2p_is_active_persistent_cli(struct wpa_supplicant *wpa_s)
  499. {
  500. return p2p_is_active_persistent_group(wpa_s) &&
  501. wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
  502. }
  503. /* Find an interface for a P2P group where we are the P2P Client */
  504. static struct wpa_supplicant *
  505. wpas_p2p_get_cli_group(struct wpa_supplicant *wpa_s)
  506. {
  507. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  508. if (p2p_is_active_persistent_cli(wpa_s))
  509. return wpa_s;
  510. }
  511. return NULL;
  512. }
  513. /* Find a persistent group where we are the GO */
  514. static struct wpa_ssid *
  515. wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
  516. {
  517. struct wpa_ssid *s;
  518. for (s = wpa_s->conf->ssid; s; s = s->next) {
  519. if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
  520. return s;
  521. }
  522. return NULL;
  523. }
  524. static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role,
  525. unsigned int *force_freq,
  526. unsigned int *pref_freq)
  527. {
  528. struct wpa_supplicant *wpa_s = ctx;
  529. struct wpa_ssid *s;
  530. u8 conncap = P2PS_SETUP_NONE;
  531. unsigned int owned_members = 0;
  532. struct wpa_supplicant *go_wpa_s, *cli_wpa_s;
  533. struct wpa_ssid *persistent_go;
  534. int p2p_no_group_iface;
  535. unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
  536. wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
  537. if (force_freq)
  538. *force_freq = 0;
  539. if (pref_freq)
  540. *pref_freq = 0;
  541. size = P2P_MAX_PREF_CHANNELS;
  542. if (force_freq && pref_freq &&
  543. !wpas_p2p_setup_freqs(wpa_s, 0, (int *) force_freq,
  544. (int *) pref_freq, 0, pref_freq_list, &size))
  545. wpas_p2p_set_own_freq_preference(wpa_s,
  546. *force_freq ? *force_freq :
  547. *pref_freq);
  548. /*
  549. * For non-concurrent capable devices:
  550. * If persistent_go, then no new.
  551. * If GO, then no client.
  552. * If client, then no GO.
  553. */
  554. go_wpa_s = wpas_p2p_get_go_group(wpa_s);
  555. if (go_wpa_s)
  556. owned_members = p2p_get_group_num_members(go_wpa_s->p2p_group);
  557. persistent_go = wpas_p2p_get_persistent_go(wpa_s);
  558. p2p_no_group_iface = !wpas_p2p_create_iface(wpa_s);
  559. cli_wpa_s = wpas_p2p_get_cli_group(wpa_s);
  560. wpa_printf(MSG_DEBUG,
  561. "P2P: GO(iface)=%p members=%u CLI(iface)=%p persistent(ssid)=%p",
  562. go_wpa_s, owned_members, cli_wpa_s, persistent_go);
  563. /* If not concurrent, restrict our choices */
  564. if (p2p_no_group_iface) {
  565. wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
  566. if (cli_wpa_s)
  567. return P2PS_SETUP_NONE;
  568. if (go_wpa_s) {
  569. if (role == P2PS_SETUP_CLIENT ||
  570. incoming == P2PS_SETUP_GROUP_OWNER ||
  571. p2p_client_limit_reached(go_wpa_s->p2p_group))
  572. return P2PS_SETUP_NONE;
  573. return P2PS_SETUP_GROUP_OWNER;
  574. }
  575. if (persistent_go) {
  576. if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
  577. if (!incoming)
  578. return P2PS_SETUP_GROUP_OWNER |
  579. P2PS_SETUP_CLIENT;
  580. if (incoming == P2PS_SETUP_NEW) {
  581. u8 r;
  582. if (os_get_random(&r, sizeof(r)) < 0 ||
  583. (r & 1))
  584. return P2PS_SETUP_CLIENT;
  585. return P2PS_SETUP_GROUP_OWNER;
  586. }
  587. }
  588. }
  589. }
  590. /* If a required role has been specified, handle it here */
  591. if (role && role != P2PS_SETUP_NEW) {
  592. switch (incoming) {
  593. case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
  594. case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
  595. /*
  596. * Peer has an active GO, so if the role allows it and
  597. * we do not have any active roles, become client.
  598. */
  599. if ((role & P2PS_SETUP_CLIENT) && !go_wpa_s &&
  600. !cli_wpa_s)
  601. return P2PS_SETUP_CLIENT;
  602. /* fall through */
  603. case P2PS_SETUP_NONE:
  604. case P2PS_SETUP_NEW:
  605. conncap = role;
  606. goto grp_owner;
  607. case P2PS_SETUP_GROUP_OWNER:
  608. /*
  609. * Must be a complimentary role - cannot be a client to
  610. * more than one peer.
  611. */
  612. if (incoming == role || cli_wpa_s)
  613. return P2PS_SETUP_NONE;
  614. return P2PS_SETUP_CLIENT;
  615. case P2PS_SETUP_CLIENT:
  616. /* Must be a complimentary role */
  617. if (incoming != role) {
  618. conncap = P2PS_SETUP_GROUP_OWNER;
  619. goto grp_owner;
  620. }
  621. default:
  622. return P2PS_SETUP_NONE;
  623. }
  624. }
  625. /*
  626. * For now, we only will support ownership of one group, and being a
  627. * client of one group. Therefore, if we have either an existing GO
  628. * group, or an existing client group, we will not do a new GO
  629. * negotiation, but rather try to re-use the existing groups.
  630. */
  631. switch (incoming) {
  632. case P2PS_SETUP_NONE:
  633. case P2PS_SETUP_NEW:
  634. if (cli_wpa_s)
  635. conncap = P2PS_SETUP_GROUP_OWNER;
  636. else if (!owned_members)
  637. conncap = P2PS_SETUP_NEW;
  638. else if (incoming == P2PS_SETUP_NONE)
  639. conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
  640. else
  641. conncap = P2PS_SETUP_CLIENT;
  642. break;
  643. case P2PS_SETUP_CLIENT:
  644. conncap = P2PS_SETUP_GROUP_OWNER;
  645. break;
  646. case P2PS_SETUP_GROUP_OWNER:
  647. if (!cli_wpa_s)
  648. conncap = P2PS_SETUP_CLIENT;
  649. break;
  650. case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
  651. case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
  652. /*
  653. * Peer has an active GO, so if the role allows it and
  654. * we do not have any active roles, become client.
  655. */
  656. if ((role & P2PS_SETUP_CLIENT) && !go_wpa_s && !cli_wpa_s)
  657. return P2PS_SETUP_CLIENT;
  658. if (cli_wpa_s)
  659. conncap = P2PS_SETUP_GROUP_OWNER;
  660. else {
  661. u8 r;
  662. if (os_get_random(&r, sizeof(r)) < 0 ||
  663. (r & 1))
  664. conncap = P2PS_SETUP_CLIENT;
  665. else
  666. conncap = P2PS_SETUP_GROUP_OWNER;
  667. }
  668. break;
  669. default:
  670. return P2PS_SETUP_NONE;
  671. }
  672. grp_owner:
  673. if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
  674. (!incoming && (conncap & P2PS_SETUP_NEW))) {
  675. if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
  676. conncap &= ~P2PS_SETUP_GROUP_OWNER;
  677. s = wpas_p2p_get_persistent_go(wpa_s);
  678. if (!s && !go_wpa_s && p2p_no_group_iface) {
  679. p2p_set_intended_addr(wpa_s->global->p2p,
  680. wpa_s->own_addr);
  681. } else if (!s && !go_wpa_s) {
  682. if (wpas_p2p_add_group_interface(wpa_s,
  683. WPA_IF_P2P_GO) < 0) {
  684. wpa_printf(MSG_ERROR,
  685. "P2P: Failed to allocate a new interface for the group");
  686. return P2PS_SETUP_NONE;
  687. }
  688. wpa_s->global->pending_group_iface_for_p2ps = 1;
  689. p2p_set_intended_addr(wpa_s->global->p2p,
  690. wpa_s->pending_interface_addr);
  691. }
  692. }
  693. return conncap;
  694. }
  695. static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
  696. enum p2p_group_removal_reason removal_reason)
  697. {
  698. struct wpa_ssid *ssid;
  699. char *gtype;
  700. const char *reason;
  701. ssid = wpa_s->current_ssid;
  702. if (ssid == NULL) {
  703. /*
  704. * The current SSID was not known, but there may still be a
  705. * pending P2P group interface waiting for provisioning or a
  706. * P2P group that is trying to reconnect.
  707. */
  708. ssid = wpa_s->conf->ssid;
  709. while (ssid) {
  710. if (ssid->p2p_group && ssid->disabled != 2)
  711. break;
  712. ssid = ssid->next;
  713. }
  714. if (ssid == NULL &&
  715. wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
  716. {
  717. wpa_printf(MSG_ERROR, "P2P: P2P group interface "
  718. "not found");
  719. return -1;
  720. }
  721. }
  722. if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
  723. gtype = "GO";
  724. else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
  725. (ssid && ssid->mode == WPAS_MODE_INFRA)) {
  726. wpa_s->reassociate = 0;
  727. wpa_s->disconnected = 1;
  728. gtype = "client";
  729. } else
  730. gtype = "GO";
  731. if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
  732. wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
  733. if (os_strcmp(gtype, "client") == 0) {
  734. wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  735. if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
  736. wpa_s, NULL)) {
  737. wpa_printf(MSG_DEBUG,
  738. "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
  739. removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
  740. eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
  741. wpa_s, NULL);
  742. }
  743. }
  744. if (wpa_s->cross_connect_in_use) {
  745. wpa_s->cross_connect_in_use = 0;
  746. wpa_msg_global(wpa_s->parent, MSG_INFO,
  747. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  748. wpa_s->ifname, wpa_s->cross_connect_uplink);
  749. }
  750. switch (removal_reason) {
  751. case P2P_GROUP_REMOVAL_REQUESTED:
  752. reason = " reason=REQUESTED";
  753. break;
  754. case P2P_GROUP_REMOVAL_FORMATION_FAILED:
  755. reason = " reason=FORMATION_FAILED";
  756. break;
  757. case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
  758. reason = " reason=IDLE";
  759. break;
  760. case P2P_GROUP_REMOVAL_UNAVAILABLE:
  761. reason = " reason=UNAVAILABLE";
  762. break;
  763. case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
  764. reason = " reason=GO_ENDING_SESSION";
  765. break;
  766. case P2P_GROUP_REMOVAL_PSK_FAILURE:
  767. reason = " reason=PSK_FAILURE";
  768. break;
  769. case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
  770. reason = " reason=FREQ_CONFLICT";
  771. break;
  772. default:
  773. reason = "";
  774. break;
  775. }
  776. if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
  777. wpa_msg_global(wpa_s->parent, MSG_INFO,
  778. P2P_EVENT_GROUP_REMOVED "%s %s%s",
  779. wpa_s->ifname, gtype, reason);
  780. }
  781. if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
  782. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
  783. if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
  784. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
  785. if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  786. wpa_s->parent, NULL) > 0) {
  787. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
  788. "timeout");
  789. wpa_s->p2p_in_provisioning = 0;
  790. wpas_p2p_group_formation_failed(wpa_s, 1);
  791. }
  792. wpa_s->p2p_in_invitation = 0;
  793. eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
  794. eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
  795. /*
  796. * Make sure wait for the first client does not remain active after the
  797. * group has been removed.
  798. */
  799. wpa_s->global->p2p_go_wait_client.sec = 0;
  800. if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
  801. struct wpa_global *global;
  802. char *ifname;
  803. enum wpa_driver_if_type type;
  804. wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
  805. wpa_s->ifname);
  806. global = wpa_s->global;
  807. ifname = os_strdup(wpa_s->ifname);
  808. type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
  809. eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
  810. wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
  811. wpa_s = global->ifaces;
  812. if (wpa_s && ifname)
  813. wpa_drv_if_remove(wpa_s, type, ifname);
  814. os_free(ifname);
  815. return 1;
  816. }
  817. if (!wpa_s->p2p_go_group_formation_completed) {
  818. wpa_s->global->p2p_group_formation = NULL;
  819. wpa_s->p2p_in_provisioning = 0;
  820. }
  821. wpa_s->show_group_started = 0;
  822. os_free(wpa_s->go_params);
  823. wpa_s->go_params = NULL;
  824. os_free(wpa_s->p2p_group_common_freqs);
  825. wpa_s->p2p_group_common_freqs = NULL;
  826. wpa_s->p2p_group_common_freqs_num = 0;
  827. wpa_s->waiting_presence_resp = 0;
  828. wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
  829. if (ssid && (ssid->p2p_group ||
  830. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
  831. (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
  832. int id = ssid->id;
  833. if (ssid == wpa_s->current_ssid) {
  834. wpa_sm_set_config(wpa_s->wpa, NULL);
  835. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  836. wpa_s->current_ssid = NULL;
  837. }
  838. /*
  839. * Networks objects created during any P2P activities are not
  840. * exposed out as they might/will confuse certain non-P2P aware
  841. * applications since these network objects won't behave like
  842. * regular ones.
  843. *
  844. * Likewise, we don't send out network removed signals for such
  845. * network objects.
  846. */
  847. wpa_config_remove_network(wpa_s->conf, id);
  848. wpa_supplicant_clear_status(wpa_s);
  849. wpa_supplicant_cancel_sched_scan(wpa_s);
  850. } else {
  851. wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
  852. "found");
  853. }
  854. if (wpa_s->ap_iface)
  855. wpa_supplicant_ap_deinit(wpa_s);
  856. else
  857. wpa_drv_deinit_p2p_cli(wpa_s);
  858. os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
  859. return 0;
  860. }
  861. static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
  862. u8 *go_dev_addr,
  863. const u8 *ssid, size_t ssid_len)
  864. {
  865. struct wpa_bss *bss;
  866. const u8 *bssid;
  867. struct wpabuf *p2p;
  868. u8 group_capab;
  869. const u8 *addr;
  870. if (wpa_s->go_params)
  871. bssid = wpa_s->go_params->peer_interface_addr;
  872. else
  873. bssid = wpa_s->bssid;
  874. bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
  875. if (bss == NULL && wpa_s->go_params &&
  876. !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
  877. bss = wpa_bss_get_p2p_dev_addr(
  878. wpa_s, wpa_s->go_params->peer_device_addr);
  879. if (bss == NULL) {
  880. u8 iface_addr[ETH_ALEN];
  881. if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
  882. iface_addr) == 0)
  883. bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
  884. }
  885. if (bss == NULL) {
  886. wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
  887. "group is persistent - BSS " MACSTR " not found",
  888. MAC2STR(bssid));
  889. return 0;
  890. }
  891. p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  892. if (p2p == NULL)
  893. p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
  894. P2P_IE_VENDOR_TYPE);
  895. if (p2p == NULL) {
  896. wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
  897. "group is persistent - BSS " MACSTR
  898. " did not include P2P IE", MAC2STR(bssid));
  899. wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
  900. (u8 *) (bss + 1), bss->ie_len);
  901. wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
  902. ((u8 *) bss + 1) + bss->ie_len,
  903. bss->beacon_ie_len);
  904. return 0;
  905. }
  906. group_capab = p2p_get_group_capab(p2p);
  907. addr = p2p_get_go_dev_addr(p2p);
  908. wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
  909. "group_capab=0x%x", group_capab);
  910. if (addr) {
  911. os_memcpy(go_dev_addr, addr, ETH_ALEN);
  912. wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
  913. MAC2STR(addr));
  914. } else
  915. os_memset(go_dev_addr, 0, ETH_ALEN);
  916. wpabuf_free(p2p);
  917. wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
  918. "go_dev_addr=" MACSTR,
  919. MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
  920. return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
  921. }
  922. static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
  923. struct wpa_ssid *ssid,
  924. const u8 *go_dev_addr)
  925. {
  926. struct wpa_ssid *s;
  927. int changed = 0;
  928. wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
  929. "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
  930. for (s = wpa_s->conf->ssid; s; s = s->next) {
  931. if (s->disabled == 2 &&
  932. os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
  933. s->ssid_len == ssid->ssid_len &&
  934. os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
  935. break;
  936. }
  937. if (s) {
  938. wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
  939. "entry");
  940. if (ssid->passphrase && !s->passphrase)
  941. changed = 1;
  942. else if (ssid->passphrase && s->passphrase &&
  943. os_strcmp(ssid->passphrase, s->passphrase) != 0)
  944. changed = 1;
  945. } else {
  946. wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
  947. "entry");
  948. changed = 1;
  949. s = wpa_config_add_network(wpa_s->conf);
  950. if (s == NULL)
  951. return -1;
  952. /*
  953. * Instead of network_added we emit persistent_group_added
  954. * notification. Also to keep the defense checks in
  955. * persistent_group obj registration method, we set the
  956. * relevant flags in s to designate it as a persistent group.
  957. */
  958. s->p2p_group = 1;
  959. s->p2p_persistent_group = 1;
  960. wpas_notify_persistent_group_added(wpa_s, s);
  961. wpa_config_set_network_defaults(s);
  962. }
  963. s->p2p_group = 1;
  964. s->p2p_persistent_group = 1;
  965. s->disabled = 2;
  966. s->bssid_set = 1;
  967. os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
  968. s->mode = ssid->mode;
  969. s->auth_alg = WPA_AUTH_ALG_OPEN;
  970. s->key_mgmt = WPA_KEY_MGMT_PSK;
  971. s->proto = WPA_PROTO_RSN;
  972. s->pairwise_cipher = WPA_CIPHER_CCMP;
  973. s->export_keys = 1;
  974. if (ssid->passphrase) {
  975. os_free(s->passphrase);
  976. s->passphrase = os_strdup(ssid->passphrase);
  977. }
  978. if (ssid->psk_set) {
  979. s->psk_set = 1;
  980. os_memcpy(s->psk, ssid->psk, 32);
  981. }
  982. if (s->passphrase && !s->psk_set)
  983. wpa_config_update_psk(s);
  984. if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
  985. os_free(s->ssid);
  986. s->ssid = os_malloc(ssid->ssid_len);
  987. }
  988. if (s->ssid) {
  989. s->ssid_len = ssid->ssid_len;
  990. os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
  991. }
  992. if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
  993. dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
  994. wpa_s->global->add_psk = NULL;
  995. changed = 1;
  996. }
  997. if (changed && wpa_s->conf->update_config &&
  998. wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  999. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  1000. }
  1001. return s->id;
  1002. }
  1003. static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
  1004. const u8 *addr)
  1005. {
  1006. struct wpa_ssid *ssid, *s;
  1007. u8 *n;
  1008. size_t i;
  1009. int found = 0;
  1010. struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
  1011. ssid = wpa_s->current_ssid;
  1012. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  1013. !ssid->p2p_persistent_group)
  1014. return;
  1015. for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
  1016. if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
  1017. continue;
  1018. if (s->ssid_len == ssid->ssid_len &&
  1019. os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
  1020. break;
  1021. }
  1022. if (s == NULL)
  1023. return;
  1024. for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
  1025. if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
  1026. ETH_ALEN) != 0)
  1027. continue;
  1028. if (i == s->num_p2p_clients - 1)
  1029. return; /* already the most recent entry */
  1030. /* move the entry to mark it most recent */
  1031. os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
  1032. s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
  1033. (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
  1034. os_memcpy(s->p2p_client_list +
  1035. (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
  1036. ETH_ALEN);
  1037. os_memset(s->p2p_client_list +
  1038. (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
  1039. 0xff, ETH_ALEN);
  1040. found = 1;
  1041. break;
  1042. }
  1043. if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
  1044. n = os_realloc_array(s->p2p_client_list,
  1045. s->num_p2p_clients + 1, 2 * ETH_ALEN);
  1046. if (n == NULL)
  1047. return;
  1048. os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
  1049. ETH_ALEN);
  1050. os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
  1051. 0xff, ETH_ALEN);
  1052. s->p2p_client_list = n;
  1053. s->num_p2p_clients++;
  1054. } else if (!found && s->p2p_client_list) {
  1055. /* Not enough room for an additional entry - drop the oldest
  1056. * entry */
  1057. os_memmove(s->p2p_client_list,
  1058. s->p2p_client_list + 2 * ETH_ALEN,
  1059. (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
  1060. os_memcpy(s->p2p_client_list +
  1061. (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
  1062. addr, ETH_ALEN);
  1063. os_memset(s->p2p_client_list +
  1064. (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
  1065. 0xff, ETH_ALEN);
  1066. }
  1067. if (p2p_wpa_s->conf->update_config &&
  1068. wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
  1069. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  1070. }
  1071. static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
  1072. int go, struct wpa_ssid *ssid, int freq,
  1073. const u8 *psk, const char *passphrase,
  1074. const u8 *go_dev_addr, int persistent,
  1075. const char *extra)
  1076. {
  1077. const char *ssid_txt;
  1078. char psk_txt[65];
  1079. if (psk)
  1080. wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
  1081. else
  1082. psk_txt[0] = '\0';
  1083. if (ssid)
  1084. ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
  1085. else
  1086. ssid_txt = "";
  1087. if (passphrase && passphrase[0] == '\0')
  1088. passphrase = NULL;
  1089. /*
  1090. * Include PSK/passphrase only in the control interface message and
  1091. * leave it out from the debug log entry.
  1092. */
  1093. wpa_msg_global_ctrl(wpa_s->parent, MSG_INFO,
  1094. P2P_EVENT_GROUP_STARTED
  1095. "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
  1096. MACSTR "%s%s",
  1097. wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
  1098. psk ? " psk=" : "", psk_txt,
  1099. passphrase ? " passphrase=\"" : "",
  1100. passphrase ? passphrase : "",
  1101. passphrase ? "\"" : "",
  1102. MAC2STR(go_dev_addr),
  1103. persistent ? " [PERSISTENT]" : "", extra);
  1104. wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
  1105. "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
  1106. wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
  1107. MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
  1108. extra);
  1109. }
  1110. static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
  1111. int success, int already_deleted)
  1112. {
  1113. struct wpa_ssid *ssid;
  1114. int client;
  1115. int persistent;
  1116. u8 go_dev_addr[ETH_ALEN];
  1117. int network_id = -1;
  1118. /*
  1119. * This callback is likely called for the main interface. Update wpa_s
  1120. * to use the group interface if a new interface was created for the
  1121. * group.
  1122. */
  1123. if (wpa_s->global->p2p_group_formation)
  1124. wpa_s = wpa_s->global->p2p_group_formation;
  1125. if (wpa_s->p2p_go_group_formation_completed) {
  1126. wpa_s->global->p2p_group_formation = NULL;
  1127. wpa_s->p2p_in_provisioning = 0;
  1128. }
  1129. wpa_s->p2p_in_invitation = 0;
  1130. wpa_s->group_formation_reported = 1;
  1131. if (!success) {
  1132. wpa_msg_global(wpa_s->parent, MSG_INFO,
  1133. P2P_EVENT_GROUP_FORMATION_FAILURE);
  1134. wpas_notify_p2p_group_formation_failure(wpa_s, "");
  1135. if (already_deleted)
  1136. return;
  1137. wpas_p2p_group_delete(wpa_s,
  1138. P2P_GROUP_REMOVAL_FORMATION_FAILED);
  1139. return;
  1140. }
  1141. wpa_msg_global(wpa_s->parent, MSG_INFO,
  1142. P2P_EVENT_GROUP_FORMATION_SUCCESS);
  1143. ssid = wpa_s->current_ssid;
  1144. if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
  1145. ssid->mode = WPAS_MODE_P2P_GO;
  1146. p2p_group_notif_formation_done(wpa_s->p2p_group);
  1147. wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
  1148. }
  1149. persistent = 0;
  1150. if (ssid) {
  1151. client = ssid->mode == WPAS_MODE_INFRA;
  1152. if (ssid->mode == WPAS_MODE_P2P_GO) {
  1153. persistent = ssid->p2p_persistent_group;
  1154. os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
  1155. ETH_ALEN);
  1156. } else
  1157. persistent = wpas_p2p_persistent_group(wpa_s,
  1158. go_dev_addr,
  1159. ssid->ssid,
  1160. ssid->ssid_len);
  1161. } else {
  1162. client = wpa_s->p2p_group_interface ==
  1163. P2P_GROUP_INTERFACE_CLIENT;
  1164. os_memset(go_dev_addr, 0, ETH_ALEN);
  1165. }
  1166. wpa_s->show_group_started = 0;
  1167. if (client) {
  1168. /*
  1169. * Indicate event only after successfully completed 4-way
  1170. * handshake, i.e., when the interface is ready for data
  1171. * packets.
  1172. */
  1173. wpa_s->show_group_started = 1;
  1174. } else {
  1175. wpas_p2p_group_started(wpa_s, 1, ssid,
  1176. ssid ? ssid->frequency : 0,
  1177. ssid && ssid->passphrase == NULL &&
  1178. ssid->psk_set ? ssid->psk : NULL,
  1179. ssid ? ssid->passphrase : NULL,
  1180. go_dev_addr, persistent, "");
  1181. wpas_p2p_cross_connect_setup(wpa_s);
  1182. wpas_p2p_set_group_idle_timeout(wpa_s);
  1183. }
  1184. if (persistent)
  1185. network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
  1186. ssid, go_dev_addr);
  1187. else {
  1188. os_free(wpa_s->global->add_psk);
  1189. wpa_s->global->add_psk = NULL;
  1190. }
  1191. if (network_id < 0 && ssid)
  1192. network_id = ssid->id;
  1193. if (!client) {
  1194. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
  1195. os_get_reltime(&wpa_s->global->p2p_go_wait_client);
  1196. }
  1197. }
  1198. struct send_action_work {
  1199. unsigned int freq;
  1200. u8 dst[ETH_ALEN];
  1201. u8 src[ETH_ALEN];
  1202. u8 bssid[ETH_ALEN];
  1203. size_t len;
  1204. unsigned int wait_time;
  1205. u8 buf[0];
  1206. };
  1207. static void wpas_p2p_free_send_action_work(struct wpa_supplicant *wpa_s)
  1208. {
  1209. struct send_action_work *awork = wpa_s->p2p_send_action_work->ctx;
  1210. wpa_printf(MSG_DEBUG,
  1211. "P2P: Free Action frame radio work @%p (freq=%u dst="
  1212. MACSTR " src=" MACSTR " bssid=" MACSTR " wait_time=%u)",
  1213. wpa_s->p2p_send_action_work, awork->freq,
  1214. MAC2STR(awork->dst), MAC2STR(awork->src),
  1215. MAC2STR(awork->bssid), awork->wait_time);
  1216. wpa_hexdump(MSG_DEBUG, "P2P: Freeing pending Action frame",
  1217. awork->buf, awork->len);
  1218. os_free(awork);
  1219. wpa_s->p2p_send_action_work->ctx = NULL;
  1220. radio_work_done(wpa_s->p2p_send_action_work);
  1221. wpa_s->p2p_send_action_work = NULL;
  1222. }
  1223. static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
  1224. void *timeout_ctx)
  1225. {
  1226. struct wpa_supplicant *wpa_s = eloop_ctx;
  1227. if (!wpa_s->p2p_send_action_work)
  1228. return;
  1229. wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
  1230. wpas_p2p_free_send_action_work(wpa_s);
  1231. }
  1232. static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
  1233. {
  1234. if (wpa_s->p2p_send_action_work) {
  1235. struct send_action_work *awork;
  1236. awork = wpa_s->p2p_send_action_work->ctx;
  1237. wpa_printf(MSG_DEBUG,
  1238. "P2P: Clear Action TX work @%p (wait_time=%u)",
  1239. wpa_s->p2p_send_action_work, awork->wait_time);
  1240. if (awork->wait_time == 0) {
  1241. wpas_p2p_free_send_action_work(wpa_s);
  1242. } else {
  1243. /*
  1244. * In theory, this should not be needed, but number of
  1245. * places in the P2P code is still using non-zero wait
  1246. * time for the last Action frame in the sequence and
  1247. * some of these do not call send_action_done().
  1248. */
  1249. eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
  1250. wpa_s, NULL);
  1251. eloop_register_timeout(
  1252. 0, awork->wait_time * 1000,
  1253. wpas_p2p_send_action_work_timeout,
  1254. wpa_s, NULL);
  1255. }
  1256. }
  1257. }
  1258. static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
  1259. unsigned int freq,
  1260. const u8 *dst, const u8 *src,
  1261. const u8 *bssid,
  1262. const u8 *data, size_t data_len,
  1263. enum offchannel_send_action_result
  1264. result)
  1265. {
  1266. enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
  1267. wpas_p2p_action_tx_clear(wpa_s);
  1268. if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
  1269. return;
  1270. switch (result) {
  1271. case OFFCHANNEL_SEND_ACTION_SUCCESS:
  1272. res = P2P_SEND_ACTION_SUCCESS;
  1273. break;
  1274. case OFFCHANNEL_SEND_ACTION_NO_ACK:
  1275. res = P2P_SEND_ACTION_NO_ACK;
  1276. break;
  1277. case OFFCHANNEL_SEND_ACTION_FAILED:
  1278. res = P2P_SEND_ACTION_FAILED;
  1279. break;
  1280. }
  1281. p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
  1282. if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
  1283. wpa_s->pending_pd_before_join &&
  1284. (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
  1285. os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
  1286. wpa_s->p2p_fallback_to_go_neg) {
  1287. wpa_s->pending_pd_before_join = 0;
  1288. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
  1289. "during p2p_connect-auto");
  1290. wpa_msg_global(wpa_s->parent, MSG_INFO,
  1291. P2P_EVENT_FALLBACK_TO_GO_NEG
  1292. "reason=no-ACK-to-PD-Req");
  1293. wpas_p2p_fallback_to_go_neg(wpa_s, 0);
  1294. return;
  1295. }
  1296. }
  1297. static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
  1298. {
  1299. struct wpa_supplicant *wpa_s = work->wpa_s;
  1300. struct send_action_work *awork = work->ctx;
  1301. if (deinit) {
  1302. if (work->started) {
  1303. eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
  1304. wpa_s, NULL);
  1305. wpa_s->p2p_send_action_work = NULL;
  1306. offchannel_send_action_done(wpa_s);
  1307. }
  1308. os_free(awork);
  1309. return;
  1310. }
  1311. if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
  1312. awork->bssid, awork->buf, awork->len,
  1313. awork->wait_time,
  1314. wpas_p2p_send_action_tx_status, 1) < 0) {
  1315. os_free(awork);
  1316. radio_work_done(work);
  1317. return;
  1318. }
  1319. wpa_s->p2p_send_action_work = work;
  1320. }
  1321. static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
  1322. unsigned int freq, const u8 *dst,
  1323. const u8 *src, const u8 *bssid, const u8 *buf,
  1324. size_t len, unsigned int wait_time)
  1325. {
  1326. struct send_action_work *awork;
  1327. if (wpa_s->p2p_send_action_work) {
  1328. wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
  1329. return -1;
  1330. }
  1331. awork = os_zalloc(sizeof(*awork) + len);
  1332. if (awork == NULL)
  1333. return -1;
  1334. awork->freq = freq;
  1335. os_memcpy(awork->dst, dst, ETH_ALEN);
  1336. os_memcpy(awork->src, src, ETH_ALEN);
  1337. os_memcpy(awork->bssid, bssid, ETH_ALEN);
  1338. awork->len = len;
  1339. awork->wait_time = wait_time;
  1340. os_memcpy(awork->buf, buf, len);
  1341. if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
  1342. wpas_send_action_cb, awork) < 0) {
  1343. os_free(awork);
  1344. return -1;
  1345. }
  1346. return 0;
  1347. }
  1348. static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
  1349. const u8 *src, const u8 *bssid, const u8 *buf,
  1350. size_t len, unsigned int wait_time)
  1351. {
  1352. struct wpa_supplicant *wpa_s = ctx;
  1353. int listen_freq = -1, send_freq = -1;
  1354. if (wpa_s->p2p_listen_work)
  1355. listen_freq = wpa_s->p2p_listen_work->freq;
  1356. if (wpa_s->p2p_send_action_work)
  1357. send_freq = wpa_s->p2p_send_action_work->freq;
  1358. if (listen_freq != (int) freq && send_freq != (int) freq) {
  1359. wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
  1360. listen_freq, send_freq);
  1361. return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
  1362. len, wait_time);
  1363. }
  1364. wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
  1365. return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
  1366. wait_time,
  1367. wpas_p2p_send_action_tx_status, 1);
  1368. }
  1369. static void wpas_send_action_done(void *ctx)
  1370. {
  1371. struct wpa_supplicant *wpa_s = ctx;
  1372. if (wpa_s->p2p_send_action_work) {
  1373. eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
  1374. wpa_s, NULL);
  1375. os_free(wpa_s->p2p_send_action_work->ctx);
  1376. radio_work_done(wpa_s->p2p_send_action_work);
  1377. wpa_s->p2p_send_action_work = NULL;
  1378. }
  1379. offchannel_send_action_done(wpa_s);
  1380. }
  1381. static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
  1382. struct p2p_go_neg_results *params)
  1383. {
  1384. if (wpa_s->go_params == NULL) {
  1385. wpa_s->go_params = os_malloc(sizeof(*params));
  1386. if (wpa_s->go_params == NULL)
  1387. return -1;
  1388. }
  1389. os_memcpy(wpa_s->go_params, params, sizeof(*params));
  1390. return 0;
  1391. }
  1392. static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
  1393. struct p2p_go_neg_results *res)
  1394. {
  1395. wpa_s->group_formation_reported = 0;
  1396. wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
  1397. " dev_addr " MACSTR " wps_method %d",
  1398. MAC2STR(res->peer_interface_addr),
  1399. MAC2STR(res->peer_device_addr), res->wps_method);
  1400. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
  1401. res->ssid, res->ssid_len);
  1402. wpa_supplicant_ap_deinit(wpa_s);
  1403. wpas_copy_go_neg_results(wpa_s, res);
  1404. if (res->wps_method == WPS_PBC) {
  1405. wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
  1406. #ifdef CONFIG_WPS_NFC
  1407. } else if (res->wps_method == WPS_NFC) {
  1408. wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
  1409. res->peer_interface_addr,
  1410. wpa_s->parent->p2p_oob_dev_pw,
  1411. wpa_s->parent->p2p_oob_dev_pw_id, 1,
  1412. wpa_s->parent->p2p_oob_dev_pw_id ==
  1413. DEV_PW_NFC_CONNECTION_HANDOVER ?
  1414. wpa_s->parent->p2p_peer_oob_pubkey_hash :
  1415. NULL,
  1416. NULL, 0, 0);
  1417. #endif /* CONFIG_WPS_NFC */
  1418. } else {
  1419. u16 dev_pw_id = DEV_PW_DEFAULT;
  1420. if (wpa_s->p2p_wps_method == WPS_P2PS)
  1421. dev_pw_id = DEV_PW_P2PS_DEFAULT;
  1422. if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
  1423. dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
  1424. wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
  1425. wpa_s->p2p_pin, 1, dev_pw_id);
  1426. }
  1427. }
  1428. static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
  1429. struct wpa_ssid *ssid)
  1430. {
  1431. struct wpa_ssid *persistent;
  1432. struct psk_list_entry *psk;
  1433. struct hostapd_data *hapd;
  1434. if (!wpa_s->ap_iface)
  1435. return;
  1436. persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
  1437. ssid->ssid_len);
  1438. if (persistent == NULL)
  1439. return;
  1440. hapd = wpa_s->ap_iface->bss[0];
  1441. dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
  1442. list) {
  1443. struct hostapd_wpa_psk *hpsk;
  1444. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
  1445. MACSTR " psk=%d",
  1446. MAC2STR(psk->addr), psk->p2p);
  1447. hpsk = os_zalloc(sizeof(*hpsk));
  1448. if (hpsk == NULL)
  1449. break;
  1450. os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
  1451. if (psk->p2p)
  1452. os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
  1453. else
  1454. os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
  1455. hpsk->next = hapd->conf->ssid.wpa_psk;
  1456. hapd->conf->ssid.wpa_psk = hpsk;
  1457. }
  1458. }
  1459. static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
  1460. {
  1461. unsigned int i;
  1462. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies (len=%u):",
  1463. wpa_s->p2p_group_common_freqs_num);
  1464. for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++)
  1465. wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d",
  1466. i, wpa_s->p2p_group_common_freqs[i]);
  1467. }
  1468. static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
  1469. struct p2p_go_neg_results *params)
  1470. {
  1471. unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
  1472. wpa_s->p2p_group_common_freqs_num = 0;
  1473. os_free(wpa_s->p2p_group_common_freqs);
  1474. wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
  1475. if (!wpa_s->p2p_group_common_freqs)
  1476. return;
  1477. for (i = 0; i < len; i++) {
  1478. if (!wpa_s->go_params->freq_list[i])
  1479. break;
  1480. wpa_s->p2p_group_common_freqs[i] =
  1481. wpa_s->go_params->freq_list[i];
  1482. }
  1483. wpa_s->p2p_group_common_freqs_num = i;
  1484. }
  1485. static void p2p_config_write(struct wpa_supplicant *wpa_s)
  1486. {
  1487. #ifndef CONFIG_NO_CONFIG_WRITE
  1488. if (wpa_s->parent->conf->update_config &&
  1489. wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
  1490. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  1491. #endif /* CONFIG_NO_CONFIG_WRITE */
  1492. }
  1493. static void p2p_go_configured(void *ctx, void *data)
  1494. {
  1495. struct wpa_supplicant *wpa_s = ctx;
  1496. struct p2p_go_neg_results *params = data;
  1497. struct wpa_ssid *ssid;
  1498. int network_id = -1;
  1499. p2p_go_save_group_common_freqs(wpa_s, params);
  1500. p2p_go_dump_common_freqs(wpa_s);
  1501. ssid = wpa_s->current_ssid;
  1502. if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
  1503. wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
  1504. if (wpa_s->global->p2p_group_formation == wpa_s)
  1505. wpa_s->global->p2p_group_formation = NULL;
  1506. wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
  1507. params->passphrase[0] == '\0' ?
  1508. params->psk : NULL,
  1509. params->passphrase,
  1510. wpa_s->global->p2p_dev_addr,
  1511. params->persistent_group, "");
  1512. wpa_s->group_formation_reported = 1;
  1513. if (wpa_s->parent->p2ps_method_config_any) {
  1514. if (is_zero_ether_addr(wpa_s->parent->p2ps_join_addr)) {
  1515. wpa_dbg(wpa_s, MSG_DEBUG,
  1516. "P2PS: Setting default PIN for ANY");
  1517. wpa_supplicant_ap_wps_pin(wpa_s, NULL,
  1518. "12345670", NULL, 0,
  1519. 0);
  1520. } else {
  1521. wpa_dbg(wpa_s, MSG_DEBUG,
  1522. "P2PS: Setting default PIN for " MACSTR,
  1523. MAC2STR(wpa_s->parent->p2ps_join_addr));
  1524. wpa_supplicant_ap_wps_pin(
  1525. wpa_s, wpa_s->parent->p2ps_join_addr,
  1526. "12345670", NULL, 0, 0);
  1527. }
  1528. wpa_s->parent->p2ps_method_config_any = 0;
  1529. }
  1530. os_get_reltime(&wpa_s->global->p2p_go_wait_client);
  1531. if (params->persistent_group) {
  1532. network_id = wpas_p2p_store_persistent_group(
  1533. wpa_s->parent, ssid,
  1534. wpa_s->global->p2p_dev_addr);
  1535. wpas_p2p_add_psk_list(wpa_s, ssid);
  1536. }
  1537. if (network_id < 0)
  1538. network_id = ssid->id;
  1539. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
  1540. wpas_p2p_cross_connect_setup(wpa_s);
  1541. wpas_p2p_set_group_idle_timeout(wpa_s);
  1542. if (wpa_s->p2p_first_connection_timeout) {
  1543. wpa_dbg(wpa_s, MSG_DEBUG,
  1544. "P2P: Start group formation timeout of %d seconds until first data connection on GO",
  1545. wpa_s->p2p_first_connection_timeout);
  1546. wpa_s->p2p_go_group_formation_completed = 0;
  1547. wpa_s->global->p2p_group_formation = wpa_s;
  1548. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  1549. wpa_s->parent, NULL);
  1550. eloop_register_timeout(
  1551. wpa_s->p2p_first_connection_timeout, 0,
  1552. wpas_p2p_group_formation_timeout,
  1553. wpa_s->parent, NULL);
  1554. }
  1555. return;
  1556. }
  1557. wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
  1558. if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
  1559. params->peer_interface_addr)) {
  1560. wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
  1561. "filtering");
  1562. return;
  1563. }
  1564. if (params->wps_method == WPS_PBC) {
  1565. wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
  1566. params->peer_device_addr);
  1567. #ifdef CONFIG_WPS_NFC
  1568. } else if (params->wps_method == WPS_NFC) {
  1569. if (wpa_s->parent->p2p_oob_dev_pw_id !=
  1570. DEV_PW_NFC_CONNECTION_HANDOVER &&
  1571. !wpa_s->parent->p2p_oob_dev_pw) {
  1572. wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
  1573. return;
  1574. }
  1575. wpas_ap_wps_add_nfc_pw(
  1576. wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
  1577. wpa_s->parent->p2p_oob_dev_pw,
  1578. wpa_s->parent->p2p_peer_oob_pk_hash_known ?
  1579. wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
  1580. #endif /* CONFIG_WPS_NFC */
  1581. } else if (wpa_s->p2p_pin[0])
  1582. wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
  1583. wpa_s->p2p_pin, NULL, 0, 0);
  1584. os_free(wpa_s->go_params);
  1585. wpa_s->go_params = NULL;
  1586. }
  1587. static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
  1588. struct p2p_go_neg_results *params,
  1589. int group_formation)
  1590. {
  1591. struct wpa_ssid *ssid;
  1592. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
  1593. if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
  1594. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
  1595. "results");
  1596. return;
  1597. }
  1598. ssid = wpa_config_add_network(wpa_s->conf);
  1599. if (ssid == NULL) {
  1600. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
  1601. return;
  1602. }
  1603. wpa_s->show_group_started = 0;
  1604. wpa_s->p2p_go_group_formation_completed = 0;
  1605. wpa_s->group_formation_reported = 0;
  1606. os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
  1607. wpa_config_set_network_defaults(ssid);
  1608. ssid->temporary = 1;
  1609. ssid->p2p_group = 1;
  1610. ssid->p2p_persistent_group = params->persistent_group;
  1611. ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
  1612. WPAS_MODE_P2P_GO;
  1613. ssid->frequency = params->freq;
  1614. ssid->ht40 = params->ht40;
  1615. ssid->vht = params->vht;
  1616. ssid->ssid = os_zalloc(params->ssid_len + 1);
  1617. if (ssid->ssid) {
  1618. os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
  1619. ssid->ssid_len = params->ssid_len;
  1620. }
  1621. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  1622. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  1623. ssid->proto = WPA_PROTO_RSN;
  1624. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  1625. ssid->group_cipher = WPA_CIPHER_CCMP;
  1626. if (params->freq > 56160) {
  1627. /*
  1628. * Enable GCMP instead of CCMP as pairwise_cipher and
  1629. * group_cipher in 60 GHz.
  1630. */
  1631. ssid->pairwise_cipher = WPA_CIPHER_GCMP;
  1632. ssid->group_cipher = WPA_CIPHER_GCMP;
  1633. }
  1634. if (os_strlen(params->passphrase) > 0) {
  1635. ssid->passphrase = os_strdup(params->passphrase);
  1636. if (ssid->passphrase == NULL) {
  1637. wpa_msg_global(wpa_s, MSG_ERROR,
  1638. "P2P: Failed to copy passphrase for GO");
  1639. wpa_config_remove_network(wpa_s->conf, ssid->id);
  1640. return;
  1641. }
  1642. } else
  1643. ssid->passphrase = NULL;
  1644. ssid->psk_set = params->psk_set;
  1645. if (ssid->psk_set)
  1646. os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
  1647. else if (ssid->passphrase)
  1648. wpa_config_update_psk(ssid);
  1649. ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
  1650. wpa_s->ap_configured_cb = p2p_go_configured;
  1651. wpa_s->ap_configured_cb_ctx = wpa_s;
  1652. wpa_s->ap_configured_cb_data = wpa_s->go_params;
  1653. wpa_s->scan_req = NORMAL_SCAN_REQ;
  1654. wpa_s->connect_without_scan = ssid;
  1655. wpa_s->reassociate = 1;
  1656. wpa_s->disconnected = 0;
  1657. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
  1658. "start GO)");
  1659. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1660. }
  1661. static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
  1662. const struct wpa_supplicant *src)
  1663. {
  1664. struct wpa_config *d;
  1665. const struct wpa_config *s;
  1666. d = dst->conf;
  1667. s = src->conf;
  1668. #define C(n) if (s->n) d->n = os_strdup(s->n)
  1669. C(device_name);
  1670. C(manufacturer);
  1671. C(model_name);
  1672. C(model_number);
  1673. C(serial_number);
  1674. C(config_methods);
  1675. #undef C
  1676. os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
  1677. os_memcpy(d->sec_device_type, s->sec_device_type,
  1678. sizeof(d->sec_device_type));
  1679. d->num_sec_device_types = s->num_sec_device_types;
  1680. d->p2p_group_idle = s->p2p_group_idle;
  1681. d->p2p_go_freq_change_policy = s->p2p_go_freq_change_policy;
  1682. d->p2p_intra_bss = s->p2p_intra_bss;
  1683. d->persistent_reconnect = s->persistent_reconnect;
  1684. d->max_num_sta = s->max_num_sta;
  1685. d->pbc_in_m1 = s->pbc_in_m1;
  1686. d->ignore_old_scan_res = s->ignore_old_scan_res;
  1687. d->beacon_int = s->beacon_int;
  1688. d->dtim_period = s->dtim_period;
  1689. d->p2p_go_ctwindow = s->p2p_go_ctwindow;
  1690. d->disassoc_low_ack = s->disassoc_low_ack;
  1691. d->disable_scan_offload = s->disable_scan_offload;
  1692. d->passive_scan = s->passive_scan;
  1693. if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) {
  1694. d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
  1695. d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
  1696. }
  1697. d->p2p_cli_probe = s->p2p_cli_probe;
  1698. }
  1699. static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
  1700. char *ifname, size_t len)
  1701. {
  1702. char *ifname_ptr = wpa_s->ifname;
  1703. if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
  1704. os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
  1705. ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
  1706. }
  1707. os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
  1708. if (os_strlen(ifname) >= IFNAMSIZ &&
  1709. os_strlen(wpa_s->ifname) < IFNAMSIZ) {
  1710. int res;
  1711. /* Try to avoid going over the IFNAMSIZ length limit */
  1712. res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
  1713. if (os_snprintf_error(len, res) && len)
  1714. ifname[len - 1] = '\0';
  1715. }
  1716. }
  1717. static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
  1718. enum wpa_driver_if_type type)
  1719. {
  1720. char ifname[120], force_ifname[120];
  1721. if (wpa_s->pending_interface_name[0]) {
  1722. wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
  1723. "- skip creation of a new one");
  1724. if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
  1725. wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
  1726. "unknown?! ifname='%s'",
  1727. wpa_s->pending_interface_name);
  1728. return -1;
  1729. }
  1730. return 0;
  1731. }
  1732. wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
  1733. force_ifname[0] = '\0';
  1734. wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
  1735. ifname);
  1736. wpa_s->p2p_group_idx++;
  1737. wpa_s->pending_interface_type = type;
  1738. if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
  1739. wpa_s->pending_interface_addr, NULL) < 0) {
  1740. wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
  1741. "interface");
  1742. return -1;
  1743. }
  1744. if (force_ifname[0]) {
  1745. wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
  1746. force_ifname);
  1747. os_strlcpy(wpa_s->pending_interface_name, force_ifname,
  1748. sizeof(wpa_s->pending_interface_name));
  1749. } else
  1750. os_strlcpy(wpa_s->pending_interface_name, ifname,
  1751. sizeof(wpa_s->pending_interface_name));
  1752. wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
  1753. MACSTR, wpa_s->pending_interface_name,
  1754. MAC2STR(wpa_s->pending_interface_addr));
  1755. return 0;
  1756. }
  1757. static void wpas_p2p_remove_pending_group_interface(
  1758. struct wpa_supplicant *wpa_s)
  1759. {
  1760. if (!wpa_s->pending_interface_name[0] ||
  1761. is_zero_ether_addr(wpa_s->pending_interface_addr))
  1762. return; /* No pending virtual interface */
  1763. wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
  1764. wpa_s->pending_interface_name);
  1765. wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
  1766. wpa_s->pending_interface_name);
  1767. os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
  1768. wpa_s->pending_interface_name[0] = '\0';
  1769. wpa_s->global->pending_group_iface_for_p2ps = 0;
  1770. }
  1771. static struct wpa_supplicant *
  1772. wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
  1773. {
  1774. struct wpa_interface iface;
  1775. struct wpa_supplicant *group_wpa_s;
  1776. if (!wpa_s->pending_interface_name[0]) {
  1777. wpa_printf(MSG_ERROR, "P2P: No pending group interface");
  1778. if (!wpas_p2p_create_iface(wpa_s))
  1779. return NULL;
  1780. /*
  1781. * Something has forced us to remove the pending interface; try
  1782. * to create a new one and hope for the best that we will get
  1783. * the same local address.
  1784. */
  1785. if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
  1786. WPA_IF_P2P_CLIENT) < 0)
  1787. return NULL;
  1788. }
  1789. os_memset(&iface, 0, sizeof(iface));
  1790. iface.ifname = wpa_s->pending_interface_name;
  1791. iface.driver = wpa_s->driver->name;
  1792. if (wpa_s->conf->ctrl_interface == NULL &&
  1793. wpa_s->parent != wpa_s &&
  1794. wpa_s->p2p_mgmt &&
  1795. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
  1796. iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
  1797. else
  1798. iface.ctrl_interface = wpa_s->conf->ctrl_interface;
  1799. iface.driver_param = wpa_s->conf->driver_param;
  1800. group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
  1801. if (group_wpa_s == NULL) {
  1802. wpa_printf(MSG_ERROR, "P2P: Failed to create new "
  1803. "wpa_supplicant interface");
  1804. return NULL;
  1805. }
  1806. wpa_s->pending_interface_name[0] = '\0';
  1807. group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
  1808. P2P_GROUP_INTERFACE_CLIENT;
  1809. wpa_s->global->p2p_group_formation = group_wpa_s;
  1810. wpa_s->global->pending_group_iface_for_p2ps = 0;
  1811. wpas_p2p_clone_config(group_wpa_s, wpa_s);
  1812. return group_wpa_s;
  1813. }
  1814. static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
  1815. void *timeout_ctx)
  1816. {
  1817. struct wpa_supplicant *wpa_s = eloop_ctx;
  1818. wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
  1819. wpas_p2p_group_formation_failed(wpa_s, 0);
  1820. }
  1821. static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
  1822. int already_deleted)
  1823. {
  1824. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  1825. wpa_s->parent, NULL);
  1826. if (wpa_s->global->p2p)
  1827. p2p_group_formation_failed(wpa_s->global->p2p);
  1828. wpas_group_formation_completed(wpa_s, 0, already_deleted);
  1829. }
  1830. static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
  1831. {
  1832. wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
  1833. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  1834. wpa_s->parent, NULL);
  1835. eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
  1836. wpa_s->parent, NULL);
  1837. wpa_s->global->p2p_fail_on_wps_complete = 0;
  1838. }
  1839. void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
  1840. {
  1841. if (wpa_s->global->p2p_group_formation != wpa_s)
  1842. return;
  1843. /* Speed up group formation timeout since this cannot succeed */
  1844. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  1845. wpa_s->parent, NULL);
  1846. eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
  1847. wpa_s->parent, NULL);
  1848. }
  1849. static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
  1850. {
  1851. struct wpa_supplicant *wpa_s = ctx;
  1852. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  1853. wpa_drv_cancel_remain_on_channel(wpa_s);
  1854. wpa_s->off_channel_freq = 0;
  1855. wpa_s->roc_waiting_drv_freq = 0;
  1856. }
  1857. if (res->status) {
  1858. wpa_msg_global(wpa_s, MSG_INFO,
  1859. P2P_EVENT_GO_NEG_FAILURE "status=%d",
  1860. res->status);
  1861. wpas_notify_p2p_go_neg_completed(wpa_s, res);
  1862. wpas_p2p_remove_pending_group_interface(wpa_s);
  1863. return;
  1864. }
  1865. if (!res->role_go) {
  1866. /* Inform driver of the operating channel of GO. */
  1867. wpa_drv_set_prob_oper_freq(wpa_s, res->freq);
  1868. }
  1869. if (wpa_s->p2p_go_ht40)
  1870. res->ht40 = 1;
  1871. if (wpa_s->p2p_go_vht)
  1872. res->vht = 1;
  1873. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
  1874. "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
  1875. " wps_method=%s",
  1876. res->role_go ? "GO" : "client", res->freq, res->ht40,
  1877. MAC2STR(res->peer_device_addr),
  1878. MAC2STR(res->peer_interface_addr),
  1879. p2p_wps_method_text(res->wps_method));
  1880. wpas_notify_p2p_go_neg_completed(wpa_s, res);
  1881. if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
  1882. struct wpa_ssid *ssid;
  1883. ssid = wpa_config_get_network(wpa_s->conf,
  1884. wpa_s->p2p_persistent_id);
  1885. if (ssid && ssid->disabled == 2 &&
  1886. ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
  1887. size_t len = os_strlen(ssid->passphrase);
  1888. wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
  1889. "on requested persistent group");
  1890. os_memcpy(res->passphrase, ssid->passphrase, len);
  1891. res->passphrase[len] = '\0';
  1892. }
  1893. }
  1894. if (wpa_s->create_p2p_iface) {
  1895. struct wpa_supplicant *group_wpa_s =
  1896. wpas_p2p_init_group_interface(wpa_s, res->role_go);
  1897. if (group_wpa_s == NULL) {
  1898. wpas_p2p_remove_pending_group_interface(wpa_s);
  1899. eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
  1900. wpa_s, NULL);
  1901. wpas_p2p_group_formation_failed(wpa_s, 1);
  1902. return;
  1903. }
  1904. if (group_wpa_s != wpa_s) {
  1905. os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
  1906. sizeof(group_wpa_s->p2p_pin));
  1907. group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
  1908. }
  1909. os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
  1910. wpa_s->pending_interface_name[0] = '\0';
  1911. group_wpa_s->p2p_in_provisioning = 1;
  1912. if (res->role_go) {
  1913. wpas_start_wps_go(group_wpa_s, res, 1);
  1914. } else {
  1915. os_get_reltime(&group_wpa_s->scan_min_time);
  1916. wpas_start_wps_enrollee(group_wpa_s, res);
  1917. }
  1918. } else {
  1919. wpa_s->p2p_in_provisioning = 1;
  1920. wpa_s->global->p2p_group_formation = wpa_s;
  1921. if (res->role_go) {
  1922. wpas_start_wps_go(wpa_s, res, 1);
  1923. } else {
  1924. os_get_reltime(&wpa_s->scan_min_time);
  1925. wpas_start_wps_enrollee(ctx, res);
  1926. }
  1927. }
  1928. wpa_s->p2p_long_listen = 0;
  1929. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  1930. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  1931. eloop_register_timeout(15 + res->peer_config_timeout / 100,
  1932. (res->peer_config_timeout % 100) * 10000,
  1933. wpas_p2p_group_formation_timeout, wpa_s, NULL);
  1934. }
  1935. static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
  1936. u8 go_intent)
  1937. {
  1938. struct wpa_supplicant *wpa_s = ctx;
  1939. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
  1940. " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
  1941. dev_passwd_id, go_intent);
  1942. wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
  1943. }
  1944. static void wpas_dev_found(void *ctx, const u8 *addr,
  1945. const struct p2p_peer_info *info,
  1946. int new_device)
  1947. {
  1948. #ifndef CONFIG_NO_STDOUT_DEBUG
  1949. struct wpa_supplicant *wpa_s = ctx;
  1950. char devtype[WPS_DEV_TYPE_BUFSIZE];
  1951. char *wfd_dev_info_hex = NULL;
  1952. #ifdef CONFIG_WIFI_DISPLAY
  1953. wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
  1954. WFD_SUBELEM_DEVICE_INFO);
  1955. #endif /* CONFIG_WIFI_DISPLAY */
  1956. if (info->p2ps_instance) {
  1957. char str[256];
  1958. const u8 *buf = wpabuf_head(info->p2ps_instance);
  1959. size_t len = wpabuf_len(info->p2ps_instance);
  1960. while (len) {
  1961. u32 id;
  1962. u16 methods;
  1963. u8 str_len;
  1964. if (len < 4 + 2 + 1)
  1965. break;
  1966. id = WPA_GET_LE32(buf);
  1967. buf += sizeof(u32);
  1968. methods = WPA_GET_BE16(buf);
  1969. buf += sizeof(u16);
  1970. str_len = *buf++;
  1971. if (str_len > len - 4 - 2 - 1)
  1972. break;
  1973. os_memcpy(str, buf, str_len);
  1974. str[str_len] = '\0';
  1975. buf += str_len;
  1976. len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
  1977. wpa_msg_global(wpa_s, MSG_INFO,
  1978. P2P_EVENT_DEVICE_FOUND MACSTR
  1979. " p2p_dev_addr=" MACSTR
  1980. " pri_dev_type=%s name='%s'"
  1981. " config_methods=0x%x"
  1982. " dev_capab=0x%x"
  1983. " group_capab=0x%x"
  1984. " adv_id=%x asp_svc=%s%s",
  1985. MAC2STR(addr),
  1986. MAC2STR(info->p2p_device_addr),
  1987. wps_dev_type_bin2str(
  1988. info->pri_dev_type,
  1989. devtype, sizeof(devtype)),
  1990. info->device_name, methods,
  1991. info->dev_capab, info->group_capab,
  1992. id, str,
  1993. info->vendor_elems ?
  1994. " vendor_elems=1" : "");
  1995. }
  1996. goto done;
  1997. }
  1998. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
  1999. " p2p_dev_addr=" MACSTR
  2000. " pri_dev_type=%s name='%s' config_methods=0x%x "
  2001. "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
  2002. MAC2STR(addr), MAC2STR(info->p2p_device_addr),
  2003. wps_dev_type_bin2str(info->pri_dev_type, devtype,
  2004. sizeof(devtype)),
  2005. info->device_name, info->config_methods,
  2006. info->dev_capab, info->group_capab,
  2007. wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
  2008. wfd_dev_info_hex ? wfd_dev_info_hex : "",
  2009. info->vendor_elems ? " vendor_elems=1" : "",
  2010. new_device);
  2011. done:
  2012. os_free(wfd_dev_info_hex);
  2013. #endif /* CONFIG_NO_STDOUT_DEBUG */
  2014. wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
  2015. }
  2016. static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
  2017. {
  2018. struct wpa_supplicant *wpa_s = ctx;
  2019. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
  2020. "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
  2021. wpas_notify_p2p_device_lost(wpa_s, dev_addr);
  2022. }
  2023. static void wpas_find_stopped(void *ctx)
  2024. {
  2025. struct wpa_supplicant *wpa_s = ctx;
  2026. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
  2027. wpas_notify_p2p_find_stopped(wpa_s);
  2028. }
  2029. struct wpas_p2p_listen_work {
  2030. unsigned int freq;
  2031. unsigned int duration;
  2032. struct wpabuf *probe_resp_ie;
  2033. };
  2034. static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
  2035. {
  2036. if (lwork == NULL)
  2037. return;
  2038. wpabuf_free(lwork->probe_resp_ie);
  2039. os_free(lwork);
  2040. }
  2041. static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
  2042. {
  2043. struct wpas_p2p_listen_work *lwork;
  2044. if (!wpa_s->p2p_listen_work)
  2045. return;
  2046. lwork = wpa_s->p2p_listen_work->ctx;
  2047. wpas_p2p_listen_work_free(lwork);
  2048. radio_work_done(wpa_s->p2p_listen_work);
  2049. wpa_s->p2p_listen_work = NULL;
  2050. }
  2051. static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
  2052. {
  2053. struct wpa_supplicant *wpa_s = work->wpa_s;
  2054. struct wpas_p2p_listen_work *lwork = work->ctx;
  2055. unsigned int duration;
  2056. if (deinit) {
  2057. if (work->started) {
  2058. wpa_s->p2p_listen_work = NULL;
  2059. wpas_stop_listen(wpa_s);
  2060. }
  2061. wpas_p2p_listen_work_free(lwork);
  2062. return;
  2063. }
  2064. wpa_s->p2p_listen_work = work;
  2065. wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
  2066. if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
  2067. wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
  2068. "report received Probe Request frames");
  2069. wpas_p2p_listen_work_done(wpa_s);
  2070. return;
  2071. }
  2072. wpa_s->pending_listen_freq = lwork->freq;
  2073. wpa_s->pending_listen_duration = lwork->duration;
  2074. duration = lwork->duration;
  2075. #ifdef CONFIG_TESTING_OPTIONS
  2076. if (wpa_s->extra_roc_dur) {
  2077. wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
  2078. duration, duration + wpa_s->extra_roc_dur);
  2079. duration += wpa_s->extra_roc_dur;
  2080. }
  2081. #endif /* CONFIG_TESTING_OPTIONS */
  2082. if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
  2083. wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
  2084. "to remain on channel (%u MHz) for Listen "
  2085. "state", lwork->freq);
  2086. wpas_p2p_listen_work_done(wpa_s);
  2087. wpa_s->pending_listen_freq = 0;
  2088. return;
  2089. }
  2090. wpa_s->off_channel_freq = 0;
  2091. wpa_s->roc_waiting_drv_freq = lwork->freq;
  2092. }
  2093. static int wpas_start_listen(void *ctx, unsigned int freq,
  2094. unsigned int duration,
  2095. const struct wpabuf *probe_resp_ie)
  2096. {
  2097. struct wpa_supplicant *wpa_s = ctx;
  2098. struct wpas_p2p_listen_work *lwork;
  2099. if (wpa_s->p2p_listen_work) {
  2100. wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
  2101. return -1;
  2102. }
  2103. lwork = os_zalloc(sizeof(*lwork));
  2104. if (lwork == NULL)
  2105. return -1;
  2106. lwork->freq = freq;
  2107. lwork->duration = duration;
  2108. if (probe_resp_ie) {
  2109. lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
  2110. if (lwork->probe_resp_ie == NULL) {
  2111. wpas_p2p_listen_work_free(lwork);
  2112. return -1;
  2113. }
  2114. }
  2115. if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
  2116. lwork) < 0) {
  2117. wpas_p2p_listen_work_free(lwork);
  2118. return -1;
  2119. }
  2120. return 0;
  2121. }
  2122. static void wpas_stop_listen(void *ctx)
  2123. {
  2124. struct wpa_supplicant *wpa_s = ctx;
  2125. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  2126. wpa_drv_cancel_remain_on_channel(wpa_s);
  2127. wpa_s->off_channel_freq = 0;
  2128. wpa_s->roc_waiting_drv_freq = 0;
  2129. }
  2130. wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
  2131. /*
  2132. * Don't cancel Probe Request RX reporting for a connected P2P Client
  2133. * handling Probe Request frames.
  2134. */
  2135. if (!wpa_s->p2p_cli_probe)
  2136. wpa_drv_probe_req_report(wpa_s, 0);
  2137. wpas_p2p_listen_work_done(wpa_s);
  2138. }
  2139. static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
  2140. unsigned int freq)
  2141. {
  2142. struct wpa_supplicant *wpa_s = ctx;
  2143. return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
  2144. freq);
  2145. }
  2146. static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
  2147. const u8 *peer, const char *params,
  2148. unsigned int generated_pin)
  2149. {
  2150. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
  2151. " %08d%s", MAC2STR(peer), generated_pin, params);
  2152. }
  2153. static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
  2154. const u8 *peer, const char *params)
  2155. {
  2156. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
  2157. "%s", MAC2STR(peer), params);
  2158. }
  2159. static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
  2160. const u8 *dev_addr, const u8 *pri_dev_type,
  2161. const char *dev_name, u16 supp_config_methods,
  2162. u8 dev_capab, u8 group_capab, const u8 *group_id,
  2163. size_t group_id_len)
  2164. {
  2165. struct wpa_supplicant *wpa_s = ctx;
  2166. char devtype[WPS_DEV_TYPE_BUFSIZE];
  2167. char params[300];
  2168. u8 empty_dev_type[8];
  2169. unsigned int generated_pin = 0;
  2170. struct wpa_supplicant *group = NULL;
  2171. int res;
  2172. if (group_id) {
  2173. for (group = wpa_s->global->ifaces; group; group = group->next)
  2174. {
  2175. struct wpa_ssid *s = group->current_ssid;
  2176. if (s != NULL &&
  2177. s->mode == WPAS_MODE_P2P_GO &&
  2178. group_id_len - ETH_ALEN == s->ssid_len &&
  2179. os_memcmp(group_id + ETH_ALEN, s->ssid,
  2180. s->ssid_len) == 0)
  2181. break;
  2182. }
  2183. }
  2184. if (pri_dev_type == NULL) {
  2185. os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
  2186. pri_dev_type = empty_dev_type;
  2187. }
  2188. res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
  2189. " pri_dev_type=%s name='%s' config_methods=0x%x "
  2190. "dev_capab=0x%x group_capab=0x%x%s%s",
  2191. MAC2STR(dev_addr),
  2192. wps_dev_type_bin2str(pri_dev_type, devtype,
  2193. sizeof(devtype)),
  2194. dev_name, supp_config_methods, dev_capab, group_capab,
  2195. group ? " group=" : "",
  2196. group ? group->ifname : "");
  2197. if (os_snprintf_error(sizeof(params), res))
  2198. wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
  2199. params[sizeof(params) - 1] = '\0';
  2200. if (config_methods & WPS_CONFIG_DISPLAY) {
  2201. generated_pin = wps_generate_pin();
  2202. wpas_prov_disc_local_display(wpa_s, peer, params,
  2203. generated_pin);
  2204. } else if (config_methods & WPS_CONFIG_KEYPAD)
  2205. wpas_prov_disc_local_keypad(wpa_s, peer, params);
  2206. else if (config_methods & WPS_CONFIG_PUSHBUTTON)
  2207. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
  2208. MACSTR "%s", MAC2STR(peer), params);
  2209. wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
  2210. P2P_PROV_DISC_SUCCESS,
  2211. config_methods, generated_pin);
  2212. }
  2213. static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
  2214. {
  2215. struct wpa_supplicant *wpa_s = ctx;
  2216. unsigned int generated_pin = 0;
  2217. char params[20];
  2218. if (wpa_s->pending_pd_before_join &&
  2219. (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
  2220. os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
  2221. wpa_s->pending_pd_before_join = 0;
  2222. wpa_printf(MSG_DEBUG, "P2P: Starting pending "
  2223. "join-existing-group operation");
  2224. wpas_p2p_join_start(wpa_s, 0, NULL, 0);
  2225. return;
  2226. }
  2227. if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
  2228. wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
  2229. int res;
  2230. res = os_snprintf(params, sizeof(params), " peer_go=%d",
  2231. wpa_s->pending_pd_use == AUTO_PD_JOIN);
  2232. if (os_snprintf_error(sizeof(params), res))
  2233. params[sizeof(params) - 1] = '\0';
  2234. } else
  2235. params[0] = '\0';
  2236. if (config_methods & WPS_CONFIG_DISPLAY)
  2237. wpas_prov_disc_local_keypad(wpa_s, peer, params);
  2238. else if (config_methods & WPS_CONFIG_KEYPAD) {
  2239. generated_pin = wps_generate_pin();
  2240. wpas_prov_disc_local_display(wpa_s, peer, params,
  2241. generated_pin);
  2242. } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
  2243. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
  2244. MACSTR "%s", MAC2STR(peer), params);
  2245. wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
  2246. P2P_PROV_DISC_SUCCESS,
  2247. config_methods, generated_pin);
  2248. }
  2249. static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
  2250. enum p2p_prov_disc_status status,
  2251. u32 adv_id, const u8 *adv_mac,
  2252. const char *deferred_session_resp)
  2253. {
  2254. struct wpa_supplicant *wpa_s = ctx;
  2255. if (wpa_s->p2p_fallback_to_go_neg) {
  2256. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
  2257. "failed - fall back to GO Negotiation");
  2258. wpa_msg_global(wpa_s->parent, MSG_INFO,
  2259. P2P_EVENT_FALLBACK_TO_GO_NEG
  2260. "reason=PD-failed");
  2261. wpas_p2p_fallback_to_go_neg(wpa_s, 0);
  2262. return;
  2263. }
  2264. if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
  2265. wpa_s->pending_pd_before_join = 0;
  2266. wpa_printf(MSG_DEBUG, "P2P: Starting pending "
  2267. "join-existing-group operation (no ACK for PD "
  2268. "Req attempts)");
  2269. wpas_p2p_join_start(wpa_s, 0, NULL, 0);
  2270. return;
  2271. }
  2272. if (adv_id && adv_mac && deferred_session_resp) {
  2273. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
  2274. " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
  2275. " deferred_session_resp='%s'",
  2276. MAC2STR(peer), status, adv_id,
  2277. deferred_session_resp);
  2278. } else if (adv_id && adv_mac) {
  2279. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
  2280. " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
  2281. MAC2STR(peer), status, adv_id);
  2282. } else {
  2283. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
  2284. " p2p_dev_addr=" MACSTR " status=%d",
  2285. MAC2STR(peer), status);
  2286. }
  2287. wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
  2288. status, 0, 0);
  2289. }
  2290. static int freq_included(struct wpa_supplicant *wpa_s,
  2291. const struct p2p_channels *channels,
  2292. unsigned int freq)
  2293. {
  2294. if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
  2295. wpas_p2p_go_is_peer_freq(wpa_s, freq))
  2296. return 1;
  2297. return 0;
  2298. }
  2299. static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
  2300. {
  2301. unsigned int num = P2P_MAX_CHANNELS;
  2302. int *common_freqs;
  2303. int ret;
  2304. p2p_go_dump_common_freqs(wpa_s);
  2305. common_freqs = os_calloc(num, sizeof(int));
  2306. if (!common_freqs)
  2307. return;
  2308. ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
  2309. if (ret < 0) {
  2310. wpa_dbg(wpa_s, MSG_DEBUG,
  2311. "P2P: Failed to get group common freqs");
  2312. os_free(common_freqs);
  2313. return;
  2314. }
  2315. os_free(wpa_s->p2p_group_common_freqs);
  2316. wpa_s->p2p_group_common_freqs = common_freqs;
  2317. wpa_s->p2p_group_common_freqs_num = num;
  2318. p2p_go_dump_common_freqs(wpa_s);
  2319. }
  2320. /*
  2321. * Check if the given frequency is one of the possible operating frequencies
  2322. * set after the completion of the GO Negotiation.
  2323. */
  2324. static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
  2325. {
  2326. unsigned int i;
  2327. p2p_go_dump_common_freqs(wpa_s);
  2328. /* assume no restrictions */
  2329. if (!wpa_s->p2p_group_common_freqs_num)
  2330. return 1;
  2331. for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
  2332. if (wpa_s->p2p_group_common_freqs[i] == freq)
  2333. return 1;
  2334. }
  2335. return 0;
  2336. }
  2337. static int wpas_sta_check_ecsa(struct hostapd_data *hapd,
  2338. struct sta_info *sta, void *ctx)
  2339. {
  2340. int *ecsa_support = ctx;
  2341. *ecsa_support &= sta->ecsa_supported;
  2342. return 0;
  2343. }
  2344. /* Check if all the peers support eCSA */
  2345. static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s)
  2346. {
  2347. int ecsa_support = 1;
  2348. ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa,
  2349. &ecsa_support);
  2350. return ecsa_support;
  2351. }
  2352. /**
  2353. * Pick the best frequency to use from all the currently used frequencies.
  2354. */
  2355. static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
  2356. struct wpa_used_freq_data *freqs,
  2357. unsigned int num)
  2358. {
  2359. unsigned int i, c;
  2360. /* find a candidate freq that is supported by P2P */
  2361. for (c = 0; c < num; c++)
  2362. if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
  2363. break;
  2364. if (c == num)
  2365. return 0;
  2366. /* once we have a candidate, try to find a 'better' one */
  2367. for (i = c + 1; i < num; i++) {
  2368. if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
  2369. continue;
  2370. /*
  2371. * 1. Infrastructure station interfaces have higher preference.
  2372. * 2. P2P Clients have higher preference.
  2373. * 3. All others.
  2374. */
  2375. if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
  2376. c = i;
  2377. break;
  2378. }
  2379. if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
  2380. c = i;
  2381. }
  2382. return freqs[c].freq;
  2383. }
  2384. static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
  2385. const u8 *go_dev_addr, const u8 *ssid,
  2386. size_t ssid_len, int *go, u8 *group_bssid,
  2387. int *force_freq, int persistent_group,
  2388. const struct p2p_channels *channels,
  2389. int dev_pw_id)
  2390. {
  2391. struct wpa_supplicant *wpa_s = ctx;
  2392. struct wpa_ssid *s;
  2393. struct wpa_used_freq_data *freqs;
  2394. struct wpa_supplicant *grp;
  2395. int best_freq;
  2396. if (!persistent_group) {
  2397. wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
  2398. " to join an active group (SSID: %s)",
  2399. MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
  2400. if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
  2401. (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
  2402. == 0 ||
  2403. os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
  2404. wpa_printf(MSG_DEBUG, "P2P: Accept previously "
  2405. "authorized invitation");
  2406. goto accept_inv;
  2407. }
  2408. #ifdef CONFIG_WPS_NFC
  2409. if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
  2410. dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
  2411. wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
  2412. wpa_s->p2p_wps_method = WPS_NFC;
  2413. wpa_s->pending_join_wps_method = WPS_NFC;
  2414. os_memcpy(wpa_s->pending_join_dev_addr,
  2415. go_dev_addr, ETH_ALEN);
  2416. os_memcpy(wpa_s->pending_join_iface_addr,
  2417. bssid, ETH_ALEN);
  2418. goto accept_inv;
  2419. }
  2420. #endif /* CONFIG_WPS_NFC */
  2421. /*
  2422. * Do not accept the invitation automatically; notify user and
  2423. * request approval.
  2424. */
  2425. return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  2426. }
  2427. grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
  2428. if (grp) {
  2429. wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
  2430. "running persistent group");
  2431. if (*go)
  2432. os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
  2433. goto accept_inv;
  2434. }
  2435. if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
  2436. os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
  2437. wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
  2438. "invitation to re-invoke a persistent group");
  2439. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  2440. } else if (!wpa_s->conf->persistent_reconnect)
  2441. return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  2442. for (s = wpa_s->conf->ssid; s; s = s->next) {
  2443. if (s->disabled == 2 &&
  2444. os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
  2445. s->ssid_len == ssid_len &&
  2446. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  2447. break;
  2448. }
  2449. if (!s) {
  2450. wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
  2451. " requested reinvocation of an unknown group",
  2452. MAC2STR(sa));
  2453. return P2P_SC_FAIL_UNKNOWN_GROUP;
  2454. }
  2455. if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
  2456. *go = 1;
  2457. if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
  2458. wpa_printf(MSG_DEBUG, "P2P: The only available "
  2459. "interface is already in use - reject "
  2460. "invitation");
  2461. return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
  2462. }
  2463. os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
  2464. } else if (s->mode == WPAS_MODE_P2P_GO) {
  2465. *go = 1;
  2466. if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
  2467. {
  2468. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  2469. "interface address for the group");
  2470. return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
  2471. }
  2472. os_memcpy(group_bssid, wpa_s->pending_interface_addr,
  2473. ETH_ALEN);
  2474. }
  2475. accept_inv:
  2476. wpas_p2p_set_own_freq_preference(wpa_s, 0);
  2477. best_freq = 0;
  2478. freqs = os_calloc(wpa_s->num_multichan_concurrent,
  2479. sizeof(struct wpa_used_freq_data));
  2480. if (freqs) {
  2481. int num_channels = wpa_s->num_multichan_concurrent;
  2482. int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
  2483. best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
  2484. os_free(freqs);
  2485. }
  2486. /* Get one of the frequencies currently in use */
  2487. if (best_freq > 0) {
  2488. wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
  2489. wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
  2490. if (wpa_s->num_multichan_concurrent < 2 ||
  2491. wpas_p2p_num_unused_channels(wpa_s) < 1) {
  2492. wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
  2493. *force_freq = best_freq;
  2494. }
  2495. }
  2496. if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
  2497. wpas_p2p_num_unused_channels(wpa_s) > 0) {
  2498. if (*go == 0) {
  2499. /* We are the client */
  2500. wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
  2501. "running a GO but we are capable of MCC, "
  2502. "figure out the best channel to use");
  2503. *force_freq = 0;
  2504. } else if (!freq_included(wpa_s, channels, *force_freq)) {
  2505. /* We are the GO, and *force_freq is not in the
  2506. * intersection */
  2507. wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
  2508. "in intersection but we are capable of MCC, "
  2509. "figure out the best channel to use",
  2510. *force_freq);
  2511. *force_freq = 0;
  2512. }
  2513. }
  2514. return P2P_SC_SUCCESS;
  2515. }
  2516. static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
  2517. const u8 *ssid, size_t ssid_len,
  2518. const u8 *go_dev_addr, u8 status,
  2519. int op_freq)
  2520. {
  2521. struct wpa_supplicant *wpa_s = ctx;
  2522. struct wpa_ssid *s;
  2523. for (s = wpa_s->conf->ssid; s; s = s->next) {
  2524. if (s->disabled == 2 &&
  2525. s->ssid_len == ssid_len &&
  2526. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  2527. break;
  2528. }
  2529. if (status == P2P_SC_SUCCESS) {
  2530. wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
  2531. " was accepted; op_freq=%d MHz, SSID=%s",
  2532. MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
  2533. if (s) {
  2534. int go = s->mode == WPAS_MODE_P2P_GO;
  2535. wpas_p2p_group_add_persistent(
  2536. wpa_s, s, go, 0, op_freq, 0, 0, NULL,
  2537. go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
  2538. 1);
  2539. } else if (bssid) {
  2540. wpa_s->user_initiated_pd = 0;
  2541. wpas_p2p_join(wpa_s, bssid, go_dev_addr,
  2542. wpa_s->p2p_wps_method, 0, op_freq,
  2543. ssid, ssid_len);
  2544. }
  2545. return;
  2546. }
  2547. if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
  2548. wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
  2549. " was rejected (status %u)", MAC2STR(sa), status);
  2550. return;
  2551. }
  2552. if (!s) {
  2553. if (bssid) {
  2554. wpa_msg_global(wpa_s, MSG_INFO,
  2555. P2P_EVENT_INVITATION_RECEIVED
  2556. "sa=" MACSTR " go_dev_addr=" MACSTR
  2557. " bssid=" MACSTR " unknown-network",
  2558. MAC2STR(sa), MAC2STR(go_dev_addr),
  2559. MAC2STR(bssid));
  2560. } else {
  2561. wpa_msg_global(wpa_s, MSG_INFO,
  2562. P2P_EVENT_INVITATION_RECEIVED
  2563. "sa=" MACSTR " go_dev_addr=" MACSTR
  2564. " unknown-network",
  2565. MAC2STR(sa), MAC2STR(go_dev_addr));
  2566. }
  2567. wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
  2568. bssid, 0, op_freq);
  2569. return;
  2570. }
  2571. if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
  2572. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
  2573. "sa=" MACSTR " persistent=%d freq=%d",
  2574. MAC2STR(sa), s->id, op_freq);
  2575. } else {
  2576. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
  2577. "sa=" MACSTR " persistent=%d",
  2578. MAC2STR(sa), s->id);
  2579. }
  2580. wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
  2581. s->id, op_freq);
  2582. }
  2583. static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
  2584. struct wpa_ssid *ssid,
  2585. const u8 *peer, int inv)
  2586. {
  2587. size_t i;
  2588. struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
  2589. if (ssid == NULL)
  2590. return;
  2591. for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
  2592. if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
  2593. ETH_ALEN) == 0)
  2594. break;
  2595. }
  2596. if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
  2597. if (ssid->mode != WPAS_MODE_P2P_GO &&
  2598. os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
  2599. wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
  2600. "due to invitation result", ssid->id);
  2601. wpas_notify_network_removed(wpa_s, ssid);
  2602. wpa_config_remove_network(wpa_s->conf, ssid->id);
  2603. return;
  2604. }
  2605. return; /* Peer not found in client list */
  2606. }
  2607. wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
  2608. "group %d client list%s",
  2609. MAC2STR(peer), ssid->id,
  2610. inv ? " due to invitation result" : "");
  2611. os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
  2612. ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
  2613. (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
  2614. ssid->num_p2p_clients--;
  2615. if (p2p_wpa_s->conf->update_config &&
  2616. wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
  2617. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  2618. }
  2619. static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
  2620. const u8 *peer)
  2621. {
  2622. struct wpa_ssid *ssid;
  2623. wpa_s = wpa_s->global->p2p_invite_group;
  2624. if (wpa_s == NULL)
  2625. return; /* No known invitation group */
  2626. ssid = wpa_s->current_ssid;
  2627. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  2628. !ssid->p2p_persistent_group)
  2629. return; /* Not operating as a GO in persistent group */
  2630. ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
  2631. ssid->ssid, ssid->ssid_len);
  2632. wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
  2633. }
  2634. static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
  2635. const struct p2p_channels *channels,
  2636. const u8 *peer, int neg_freq,
  2637. int peer_oper_freq)
  2638. {
  2639. struct wpa_supplicant *wpa_s = ctx;
  2640. struct wpa_ssid *ssid;
  2641. int freq;
  2642. if (bssid) {
  2643. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
  2644. "status=%d " MACSTR,
  2645. status, MAC2STR(bssid));
  2646. } else {
  2647. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
  2648. "status=%d ", status);
  2649. }
  2650. wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
  2651. wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
  2652. status, MAC2STR(peer));
  2653. if (wpa_s->pending_invite_ssid_id == -1) {
  2654. if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
  2655. wpas_remove_persistent_client(wpa_s, peer);
  2656. return; /* Invitation to active group */
  2657. }
  2658. if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
  2659. wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
  2660. "invitation exchange to indicate readiness for "
  2661. "re-invocation");
  2662. }
  2663. if (status != P2P_SC_SUCCESS) {
  2664. if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
  2665. ssid = wpa_config_get_network(
  2666. wpa_s->conf, wpa_s->pending_invite_ssid_id);
  2667. wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
  2668. }
  2669. wpas_p2p_remove_pending_group_interface(wpa_s);
  2670. return;
  2671. }
  2672. ssid = wpa_config_get_network(wpa_s->conf,
  2673. wpa_s->pending_invite_ssid_id);
  2674. if (ssid == NULL) {
  2675. wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
  2676. "data matching with invitation");
  2677. return;
  2678. }
  2679. /*
  2680. * The peer could have missed our ctrl::ack frame for Invitation
  2681. * Response and continue retransmitting the frame. To reduce the
  2682. * likelihood of the peer not getting successful TX status for the
  2683. * Invitation Response frame, wait a short time here before starting
  2684. * the persistent group so that we will remain on the current channel to
  2685. * acknowledge any possible retransmission from the peer.
  2686. */
  2687. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
  2688. "starting persistent group");
  2689. os_sleep(0, 50000);
  2690. if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
  2691. freq_included(wpa_s, channels, neg_freq))
  2692. freq = neg_freq;
  2693. else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
  2694. freq_included(wpa_s, channels, peer_oper_freq))
  2695. freq = peer_oper_freq;
  2696. else
  2697. freq = 0;
  2698. wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
  2699. freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  2700. wpas_p2p_group_add_persistent(wpa_s, ssid,
  2701. ssid->mode == WPAS_MODE_P2P_GO,
  2702. wpa_s->p2p_persistent_go_freq,
  2703. freq,
  2704. wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
  2705. channels,
  2706. ssid->mode == WPAS_MODE_P2P_GO ?
  2707. P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
  2708. 0, 1);
  2709. }
  2710. static int wpas_p2p_disallowed_freq(struct wpa_global *global,
  2711. unsigned int freq)
  2712. {
  2713. if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
  2714. return 1;
  2715. return freq_range_list_includes(&global->p2p_disallow_freq, freq);
  2716. }
  2717. static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
  2718. {
  2719. reg->channel[reg->channels] = chan;
  2720. reg->channels++;
  2721. }
  2722. static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
  2723. struct p2p_channels *chan,
  2724. struct p2p_channels *cli_chan)
  2725. {
  2726. int i, cla = 0;
  2727. wpa_s->global->p2p_24ghz_social_channels = 1;
  2728. os_memset(cli_chan, 0, sizeof(*cli_chan));
  2729. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
  2730. "band");
  2731. /* Operating class 81 - 2.4 GHz band channels 1..13 */
  2732. chan->reg_class[cla].reg_class = 81;
  2733. chan->reg_class[cla].channels = 0;
  2734. for (i = 0; i < 11; i++) {
  2735. if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
  2736. wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
  2737. }
  2738. if (chan->reg_class[cla].channels)
  2739. cla++;
  2740. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
  2741. "band");
  2742. /* Operating class 115 - 5 GHz, channels 36-48 */
  2743. chan->reg_class[cla].reg_class = 115;
  2744. chan->reg_class[cla].channels = 0;
  2745. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
  2746. wpas_p2p_add_chan(&chan->reg_class[cla], 36);
  2747. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
  2748. wpas_p2p_add_chan(&chan->reg_class[cla], 40);
  2749. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
  2750. wpas_p2p_add_chan(&chan->reg_class[cla], 44);
  2751. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
  2752. wpas_p2p_add_chan(&chan->reg_class[cla], 48);
  2753. if (chan->reg_class[cla].channels)
  2754. cla++;
  2755. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
  2756. "band");
  2757. /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
  2758. chan->reg_class[cla].reg_class = 124;
  2759. chan->reg_class[cla].channels = 0;
  2760. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
  2761. wpas_p2p_add_chan(&chan->reg_class[cla], 149);
  2762. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
  2763. wpas_p2p_add_chan(&chan->reg_class[cla], 153);
  2764. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
  2765. wpas_p2p_add_chan(&chan->reg_class[cla], 157);
  2766. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
  2767. wpas_p2p_add_chan(&chan->reg_class[cla], 161);
  2768. if (chan->reg_class[cla].channels)
  2769. cla++;
  2770. chan->reg_classes = cla;
  2771. return 0;
  2772. }
  2773. static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
  2774. u16 num_modes,
  2775. enum hostapd_hw_mode mode)
  2776. {
  2777. u16 i;
  2778. for (i = 0; i < num_modes; i++) {
  2779. if (modes[i].mode == mode)
  2780. return &modes[i];
  2781. }
  2782. return NULL;
  2783. }
  2784. enum chan_allowed {
  2785. NOT_ALLOWED, NO_IR, ALLOWED
  2786. };
  2787. static int has_channel(struct wpa_global *global,
  2788. struct hostapd_hw_modes *mode, u8 chan, int *flags)
  2789. {
  2790. int i;
  2791. unsigned int freq;
  2792. freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
  2793. chan * 5;
  2794. if (wpas_p2p_disallowed_freq(global, freq))
  2795. return NOT_ALLOWED;
  2796. for (i = 0; i < mode->num_channels; i++) {
  2797. if (mode->channels[i].chan == chan) {
  2798. if (flags)
  2799. *flags = mode->channels[i].flag;
  2800. if (mode->channels[i].flag &
  2801. (HOSTAPD_CHAN_DISABLED |
  2802. HOSTAPD_CHAN_RADAR))
  2803. return NOT_ALLOWED;
  2804. if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
  2805. return NO_IR;
  2806. return ALLOWED;
  2807. }
  2808. }
  2809. return NOT_ALLOWED;
  2810. }
  2811. struct p2p_oper_class_map {
  2812. enum hostapd_hw_mode mode;
  2813. u8 op_class;
  2814. u8 min_chan;
  2815. u8 max_chan;
  2816. u8 inc;
  2817. enum { BW20, BW40PLUS, BW40MINUS, BW80, BW2160 } bw;
  2818. };
  2819. static const struct p2p_oper_class_map op_class[] = {
  2820. { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
  2821. #if 0 /* Do not enable HT40 on 2 GHz for now */
  2822. { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
  2823. { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
  2824. #endif
  2825. { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
  2826. { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
  2827. { HOSTAPD_MODE_IEEE80211A, 125, 149, 169, 4, BW20 },
  2828. { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
  2829. { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
  2830. { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
  2831. { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
  2832. /*
  2833. * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
  2834. * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
  2835. * 80 MHz, but currently use the following definition for simplicity
  2836. * (these center frequencies are not actual channels, which makes
  2837. * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
  2838. * removing invalid channels.
  2839. */
  2840. { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
  2841. { HOSTAPD_MODE_IEEE80211AD, 180, 1, 4, 1, BW2160 },
  2842. { -1, 0, 0, 0, 0, BW20 }
  2843. };
  2844. static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
  2845. struct hostapd_hw_modes *mode,
  2846. u8 channel)
  2847. {
  2848. u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
  2849. unsigned int i;
  2850. if (mode->mode != HOSTAPD_MODE_IEEE80211A)
  2851. return 0;
  2852. for (i = 0; i < ARRAY_SIZE(center_channels); i++)
  2853. /*
  2854. * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
  2855. * so the center channel is 6 channels away from the start/end.
  2856. */
  2857. if (channel >= center_channels[i] - 6 &&
  2858. channel <= center_channels[i] + 6)
  2859. return center_channels[i];
  2860. return 0;
  2861. }
  2862. static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
  2863. struct hostapd_hw_modes *mode,
  2864. u8 channel, u8 bw)
  2865. {
  2866. u8 center_chan;
  2867. int i, flags;
  2868. enum chan_allowed res, ret = ALLOWED;
  2869. center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
  2870. if (!center_chan)
  2871. return NOT_ALLOWED;
  2872. if (center_chan >= 58 && center_chan <= 138)
  2873. return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
  2874. /* check all the channels are available */
  2875. for (i = 0; i < 4; i++) {
  2876. int adj_chan = center_chan - 6 + i * 4;
  2877. res = has_channel(wpa_s->global, mode, adj_chan, &flags);
  2878. if (res == NOT_ALLOWED)
  2879. return NOT_ALLOWED;
  2880. if (res == NO_IR)
  2881. ret = NO_IR;
  2882. if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
  2883. return NOT_ALLOWED;
  2884. if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
  2885. return NOT_ALLOWED;
  2886. if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
  2887. return NOT_ALLOWED;
  2888. if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
  2889. return NOT_ALLOWED;
  2890. }
  2891. return ret;
  2892. }
  2893. static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
  2894. struct hostapd_hw_modes *mode,
  2895. u8 channel, u8 bw)
  2896. {
  2897. int flag = 0;
  2898. enum chan_allowed res, res2;
  2899. res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
  2900. if (bw == BW40MINUS) {
  2901. if (!(flag & HOSTAPD_CHAN_HT40MINUS))
  2902. return NOT_ALLOWED;
  2903. res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
  2904. } else if (bw == BW40PLUS) {
  2905. if (!(flag & HOSTAPD_CHAN_HT40PLUS))
  2906. return NOT_ALLOWED;
  2907. res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
  2908. } else if (bw == BW80) {
  2909. res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
  2910. }
  2911. if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
  2912. return NOT_ALLOWED;
  2913. if (res == NO_IR || res2 == NO_IR)
  2914. return NO_IR;
  2915. return res;
  2916. }
  2917. static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
  2918. struct p2p_channels *chan,
  2919. struct p2p_channels *cli_chan)
  2920. {
  2921. struct hostapd_hw_modes *mode;
  2922. int cla, op, cli_cla;
  2923. if (wpa_s->hw.modes == NULL) {
  2924. wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
  2925. "of all supported channels; assume dualband "
  2926. "support");
  2927. return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
  2928. }
  2929. cla = cli_cla = 0;
  2930. for (op = 0; op_class[op].op_class; op++) {
  2931. const struct p2p_oper_class_map *o = &op_class[op];
  2932. u8 ch;
  2933. struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
  2934. mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
  2935. if (mode == NULL)
  2936. continue;
  2937. if (mode->mode == HOSTAPD_MODE_IEEE80211G)
  2938. wpa_s->global->p2p_24ghz_social_channels = 1;
  2939. for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
  2940. enum chan_allowed res;
  2941. res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
  2942. if (res == ALLOWED) {
  2943. if (reg == NULL) {
  2944. wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
  2945. o->op_class);
  2946. reg = &chan->reg_class[cla];
  2947. cla++;
  2948. reg->reg_class = o->op_class;
  2949. }
  2950. reg->channel[reg->channels] = ch;
  2951. reg->channels++;
  2952. } else if (res == NO_IR &&
  2953. wpa_s->conf->p2p_add_cli_chan) {
  2954. if (cli_reg == NULL) {
  2955. wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
  2956. o->op_class);
  2957. cli_reg = &cli_chan->reg_class[cli_cla];
  2958. cli_cla++;
  2959. cli_reg->reg_class = o->op_class;
  2960. }
  2961. cli_reg->channel[cli_reg->channels] = ch;
  2962. cli_reg->channels++;
  2963. }
  2964. }
  2965. if (reg) {
  2966. wpa_hexdump(MSG_DEBUG, "P2P: Channels",
  2967. reg->channel, reg->channels);
  2968. }
  2969. if (cli_reg) {
  2970. wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
  2971. cli_reg->channel, cli_reg->channels);
  2972. }
  2973. }
  2974. chan->reg_classes = cla;
  2975. cli_chan->reg_classes = cli_cla;
  2976. return 0;
  2977. }
  2978. int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
  2979. struct hostapd_hw_modes *mode, u8 channel)
  2980. {
  2981. int op;
  2982. enum chan_allowed ret;
  2983. for (op = 0; op_class[op].op_class; op++) {
  2984. const struct p2p_oper_class_map *o = &op_class[op];
  2985. u8 ch;
  2986. for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
  2987. if (o->mode != HOSTAPD_MODE_IEEE80211A ||
  2988. (o->bw != BW40PLUS && o->bw != BW40MINUS) ||
  2989. ch != channel)
  2990. continue;
  2991. ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
  2992. if (ret == ALLOWED)
  2993. return (o->bw == BW40MINUS) ? -1 : 1;
  2994. }
  2995. }
  2996. return 0;
  2997. }
  2998. int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
  2999. struct hostapd_hw_modes *mode, u8 channel)
  3000. {
  3001. if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
  3002. return 0;
  3003. return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
  3004. }
  3005. static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
  3006. size_t buf_len)
  3007. {
  3008. struct wpa_supplicant *wpa_s = ctx;
  3009. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3010. if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
  3011. break;
  3012. }
  3013. if (wpa_s == NULL)
  3014. return -1;
  3015. return wpa_drv_get_noa(wpa_s, buf, buf_len);
  3016. }
  3017. struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
  3018. const u8 *ssid, size_t ssid_len)
  3019. {
  3020. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3021. struct wpa_ssid *s = wpa_s->current_ssid;
  3022. if (s == NULL)
  3023. continue;
  3024. if (s->mode != WPAS_MODE_P2P_GO &&
  3025. s->mode != WPAS_MODE_AP &&
  3026. s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
  3027. continue;
  3028. if (s->ssid_len != ssid_len ||
  3029. os_memcmp(ssid, s->ssid, ssid_len) != 0)
  3030. continue;
  3031. return wpa_s;
  3032. }
  3033. return NULL;
  3034. }
  3035. struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
  3036. const u8 *peer_dev_addr)
  3037. {
  3038. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3039. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3040. if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
  3041. continue;
  3042. if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
  3043. return wpa_s;
  3044. }
  3045. return NULL;
  3046. }
  3047. static int wpas_go_connected(void *ctx, const u8 *dev_addr)
  3048. {
  3049. struct wpa_supplicant *wpa_s = ctx;
  3050. return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
  3051. }
  3052. static int wpas_is_concurrent_session_active(void *ctx)
  3053. {
  3054. struct wpa_supplicant *wpa_s = ctx;
  3055. struct wpa_supplicant *ifs;
  3056. for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
  3057. if (ifs == wpa_s)
  3058. continue;
  3059. if (ifs->wpa_state > WPA_ASSOCIATED)
  3060. return 1;
  3061. }
  3062. return 0;
  3063. }
  3064. static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
  3065. {
  3066. struct wpa_supplicant *wpa_s = ctx;
  3067. wpa_msg_global(wpa_s, level, "P2P: %s", msg);
  3068. }
  3069. int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
  3070. const char *conf_p2p_dev)
  3071. {
  3072. struct wpa_interface iface;
  3073. struct wpa_supplicant *p2pdev_wpa_s;
  3074. char ifname[100];
  3075. char force_name[100];
  3076. int ret;
  3077. ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
  3078. wpa_s->ifname);
  3079. if (os_snprintf_error(sizeof(ifname), ret))
  3080. return -1;
  3081. force_name[0] = '\0';
  3082. wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
  3083. ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
  3084. force_name, wpa_s->pending_interface_addr, NULL);
  3085. if (ret < 0) {
  3086. wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
  3087. return ret;
  3088. }
  3089. os_strlcpy(wpa_s->pending_interface_name, ifname,
  3090. sizeof(wpa_s->pending_interface_name));
  3091. os_memset(&iface, 0, sizeof(iface));
  3092. iface.p2p_mgmt = 1;
  3093. iface.ifname = wpa_s->pending_interface_name;
  3094. iface.driver = wpa_s->driver->name;
  3095. iface.driver_param = wpa_s->conf->driver_param;
  3096. /*
  3097. * If a P2P Device configuration file was given, use it as the interface
  3098. * configuration file (instead of using parent's configuration file.
  3099. */
  3100. if (conf_p2p_dev) {
  3101. iface.confname = conf_p2p_dev;
  3102. iface.ctrl_interface = NULL;
  3103. } else {
  3104. iface.confname = wpa_s->confname;
  3105. iface.ctrl_interface = wpa_s->conf->ctrl_interface;
  3106. }
  3107. p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
  3108. if (!p2pdev_wpa_s) {
  3109. wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
  3110. return -1;
  3111. }
  3112. wpa_s->pending_interface_name[0] = '\0';
  3113. return 0;
  3114. }
  3115. static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
  3116. const u8 *noa, size_t noa_len)
  3117. {
  3118. struct wpa_supplicant *wpa_s, *intf = ctx;
  3119. char hex[100];
  3120. for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3121. if (wpa_s->waiting_presence_resp)
  3122. break;
  3123. }
  3124. if (!wpa_s) {
  3125. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
  3126. return;
  3127. }
  3128. wpa_s->waiting_presence_resp = 0;
  3129. wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
  3130. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
  3131. " status=%u noa=%s", MAC2STR(src), status, hex);
  3132. }
  3133. static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
  3134. size_t ssid_len, u8 *go_dev_addr,
  3135. u8 *ret_ssid, size_t *ret_ssid_len,
  3136. u8 *intended_iface_addr)
  3137. {
  3138. struct wpa_supplicant *wpa_s = ctx;
  3139. struct wpa_ssid *s;
  3140. s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
  3141. if (s) {
  3142. os_memcpy(ret_ssid, s->ssid, s->ssid_len);
  3143. *ret_ssid_len = s->ssid_len;
  3144. os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
  3145. if (s->mode != WPAS_MODE_P2P_GO) {
  3146. os_memset(intended_iface_addr, 0, ETH_ALEN);
  3147. } else if (wpas_p2p_create_iface(wpa_s)) {
  3148. if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
  3149. return 0;
  3150. os_memcpy(intended_iface_addr,
  3151. wpa_s->pending_interface_addr, ETH_ALEN);
  3152. } else {
  3153. os_memcpy(intended_iface_addr, wpa_s->own_addr,
  3154. ETH_ALEN);
  3155. }
  3156. return 1;
  3157. }
  3158. return 0;
  3159. }
  3160. static int wpas_get_go_info(void *ctx, u8 *intended_addr,
  3161. u8 *ssid, size_t *ssid_len, int *group_iface,
  3162. unsigned int *freq)
  3163. {
  3164. struct wpa_supplicant *wpa_s = ctx;
  3165. struct wpa_supplicant *go;
  3166. struct wpa_ssid *s;
  3167. /*
  3168. * group_iface will be set to 1 only if a dedicated interface for P2P
  3169. * role is required. First, we try to reuse an active GO. However,
  3170. * if it is not present, we will try to reactivate an existing
  3171. * persistent group and set group_iface to 1, so the caller will know
  3172. * that the pending interface should be used.
  3173. */
  3174. *group_iface = 0;
  3175. if (freq)
  3176. *freq = 0;
  3177. go = wpas_p2p_get_go_group(wpa_s);
  3178. if (!go) {
  3179. s = wpas_p2p_get_persistent_go(wpa_s);
  3180. *group_iface = wpas_p2p_create_iface(wpa_s);
  3181. if (s)
  3182. os_memcpy(intended_addr, s->bssid, ETH_ALEN);
  3183. else
  3184. return 0;
  3185. } else {
  3186. s = go->current_ssid;
  3187. os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
  3188. if (freq)
  3189. *freq = go->assoc_freq;
  3190. }
  3191. os_memcpy(ssid, s->ssid, s->ssid_len);
  3192. *ssid_len = s->ssid_len;
  3193. return 1;
  3194. }
  3195. static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
  3196. const u8 *ssid, size_t ssid_len)
  3197. {
  3198. struct wpa_supplicant *wpa_s = ctx;
  3199. struct wpa_ssid *s;
  3200. int save_config = 0;
  3201. size_t i;
  3202. /* Start with our first choice of Persistent Groups */
  3203. while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
  3204. if (go && ssid && ssid_len &&
  3205. s->ssid_len == ssid_len &&
  3206. os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
  3207. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  3208. break;
  3209. /* Remove stale persistent group */
  3210. if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
  3211. wpa_config_remove_network(wpa_s->conf, s->id);
  3212. save_config = 1;
  3213. continue;
  3214. }
  3215. for (i = 0; i < s->num_p2p_clients; i++) {
  3216. if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
  3217. peer, ETH_ALEN) != 0)
  3218. continue;
  3219. os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
  3220. s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
  3221. (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
  3222. break;
  3223. }
  3224. s->num_p2p_clients--;
  3225. save_config = 1;
  3226. }
  3227. if (save_config)
  3228. p2p_config_write(wpa_s);
  3229. /* Return TRUE if valid SSID remains */
  3230. return s != NULL;
  3231. }
  3232. static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
  3233. const u8 *feat_cap, size_t feat_cap_len)
  3234. {
  3235. static const char pref[] = " feature_cap=";
  3236. int ret;
  3237. buf[0] = '\0';
  3238. /*
  3239. * We expect a feature capability to contain at least one byte to be
  3240. * reported. The string buffer provided by the caller function is
  3241. * expected to be big enough to contain all bytes of the attribute for
  3242. * known specifications. This function truncates the reported bytes if
  3243. * the feature capability data exceeds the string buffer size.
  3244. */
  3245. if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
  3246. return;
  3247. os_memcpy(buf, pref, sizeof(pref));
  3248. ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
  3249. buf_len - sizeof(pref) + 1,
  3250. feat_cap, feat_cap_len);
  3251. if (ret != (2 * (int) feat_cap_len))
  3252. wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
  3253. }
  3254. static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
  3255. const u8 *adv_mac, const u8 *ses_mac,
  3256. const u8 *grp_mac, u32 adv_id, u32 ses_id,
  3257. u8 conncap, int passwd_id,
  3258. const u8 *persist_ssid,
  3259. size_t persist_ssid_size, int response_done,
  3260. int prov_start, const char *session_info,
  3261. const u8 *feat_cap, size_t feat_cap_len,
  3262. unsigned int freq)
  3263. {
  3264. struct wpa_supplicant *wpa_s = ctx;
  3265. u8 mac[ETH_ALEN];
  3266. struct wpa_ssid *persistent_go, *stale, *s = NULL;
  3267. int save_config = 0;
  3268. struct wpa_supplicant *go_wpa_s;
  3269. char feat_cap_str[256];
  3270. if (!dev)
  3271. return;
  3272. os_memset(mac, 0, ETH_ALEN);
  3273. if (!adv_mac)
  3274. adv_mac = mac;
  3275. if (!ses_mac)
  3276. ses_mac = mac;
  3277. if (!grp_mac)
  3278. grp_mac = mac;
  3279. wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
  3280. feat_cap, feat_cap_len);
  3281. if (prov_start) {
  3282. if (session_info == NULL) {
  3283. wpa_msg_global(wpa_s, MSG_INFO,
  3284. P2P_EVENT_P2PS_PROVISION_START MACSTR
  3285. " adv_id=%x conncap=%x"
  3286. " adv_mac=" MACSTR
  3287. " session=%x mac=" MACSTR
  3288. " dev_passwd_id=%d%s",
  3289. MAC2STR(dev), adv_id, conncap,
  3290. MAC2STR(adv_mac),
  3291. ses_id, MAC2STR(ses_mac),
  3292. passwd_id, feat_cap_str);
  3293. } else {
  3294. wpa_msg_global(wpa_s, MSG_INFO,
  3295. P2P_EVENT_P2PS_PROVISION_START MACSTR
  3296. " adv_id=%x conncap=%x"
  3297. " adv_mac=" MACSTR
  3298. " session=%x mac=" MACSTR
  3299. " dev_passwd_id=%d info='%s'%s",
  3300. MAC2STR(dev), adv_id, conncap,
  3301. MAC2STR(adv_mac),
  3302. ses_id, MAC2STR(ses_mac),
  3303. passwd_id, session_info, feat_cap_str);
  3304. }
  3305. return;
  3306. }
  3307. go_wpa_s = wpas_p2p_get_go_group(wpa_s);
  3308. persistent_go = wpas_p2p_get_persistent_go(wpa_s);
  3309. if (status && status != P2P_SC_SUCCESS_DEFERRED) {
  3310. if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
  3311. wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
  3312. if (persistent_go && !persistent_go->num_p2p_clients) {
  3313. /* remove empty persistent GO */
  3314. wpa_config_remove_network(wpa_s->conf,
  3315. persistent_go->id);
  3316. }
  3317. wpa_msg_global(wpa_s, MSG_INFO,
  3318. P2P_EVENT_P2PS_PROVISION_DONE MACSTR
  3319. " status=%d"
  3320. " adv_id=%x adv_mac=" MACSTR
  3321. " session=%x mac=" MACSTR "%s",
  3322. MAC2STR(dev), status,
  3323. adv_id, MAC2STR(adv_mac),
  3324. ses_id, MAC2STR(ses_mac), feat_cap_str);
  3325. return;
  3326. }
  3327. /* Clean up stale persistent groups with this device */
  3328. if (persist_ssid && persist_ssid_size)
  3329. s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
  3330. persist_ssid_size);
  3331. if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
  3332. is_zero_ether_addr(grp_mac)) {
  3333. wpa_dbg(wpa_s, MSG_ERROR,
  3334. "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
  3335. return;
  3336. }
  3337. for (;;) {
  3338. stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
  3339. if (!stale)
  3340. break;
  3341. if (s && s->ssid_len == stale->ssid_len &&
  3342. os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
  3343. os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
  3344. break;
  3345. /* Remove stale persistent group */
  3346. if (stale->mode != WPAS_MODE_P2P_GO ||
  3347. stale->num_p2p_clients <= 1) {
  3348. wpa_config_remove_network(wpa_s->conf, stale->id);
  3349. } else {
  3350. size_t i;
  3351. for (i = 0; i < stale->num_p2p_clients; i++) {
  3352. if (os_memcmp(stale->p2p_client_list +
  3353. i * ETH_ALEN,
  3354. dev, ETH_ALEN) == 0) {
  3355. os_memmove(stale->p2p_client_list +
  3356. i * ETH_ALEN,
  3357. stale->p2p_client_list +
  3358. (i + 1) * ETH_ALEN,
  3359. (stale->num_p2p_clients -
  3360. i - 1) * ETH_ALEN);
  3361. break;
  3362. }
  3363. }
  3364. stale->num_p2p_clients--;
  3365. }
  3366. save_config = 1;
  3367. }
  3368. if (save_config)
  3369. p2p_config_write(wpa_s);
  3370. if (s) {
  3371. if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
  3372. wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
  3373. if (persistent_go && s != persistent_go &&
  3374. !persistent_go->num_p2p_clients) {
  3375. /* remove empty persistent GO */
  3376. wpa_config_remove_network(wpa_s->conf,
  3377. persistent_go->id);
  3378. /* Save config */
  3379. }
  3380. wpa_msg_global(wpa_s, MSG_INFO,
  3381. P2P_EVENT_P2PS_PROVISION_DONE MACSTR
  3382. " status=%d"
  3383. " adv_id=%x adv_mac=" MACSTR
  3384. " session=%x mac=" MACSTR
  3385. " persist=%d%s",
  3386. MAC2STR(dev), status,
  3387. adv_id, MAC2STR(adv_mac),
  3388. ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
  3389. return;
  3390. }
  3391. if (conncap == P2PS_SETUP_GROUP_OWNER) {
  3392. /*
  3393. * We need to copy the interface name. Simply saving a
  3394. * pointer isn't enough, since if we use pending_interface_name
  3395. * it will be overwritten when the group is added.
  3396. */
  3397. char go_ifname[100];
  3398. go_ifname[0] = '\0';
  3399. if (!go_wpa_s) {
  3400. wpa_s->global->pending_p2ps_group = 1;
  3401. wpa_s->global->pending_p2ps_group_freq = freq;
  3402. if (!wpas_p2p_create_iface(wpa_s))
  3403. os_memcpy(go_ifname, wpa_s->ifname,
  3404. sizeof(go_ifname));
  3405. else if (wpa_s->pending_interface_name[0])
  3406. os_memcpy(go_ifname,
  3407. wpa_s->pending_interface_name,
  3408. sizeof(go_ifname));
  3409. if (!go_ifname[0]) {
  3410. wpas_p2ps_prov_complete(
  3411. wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
  3412. dev, adv_mac, ses_mac,
  3413. grp_mac, adv_id, ses_id, 0, 0,
  3414. NULL, 0, 0, 0, NULL, NULL, 0, 0);
  3415. return;
  3416. }
  3417. /* If PD Resp complete, start up the GO */
  3418. if (response_done && persistent_go) {
  3419. wpas_p2p_group_add_persistent(
  3420. wpa_s, persistent_go,
  3421. 0, 0, freq, 0, 0, NULL,
  3422. persistent_go->mode ==
  3423. WPAS_MODE_P2P_GO ?
  3424. P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
  3425. 0, 0);
  3426. } else if (response_done) {
  3427. wpas_p2p_group_add(wpa_s, 1, freq, 0, 0);
  3428. }
  3429. if (passwd_id == DEV_PW_P2PS_DEFAULT) {
  3430. os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
  3431. ETH_ALEN);
  3432. wpa_s->p2ps_method_config_any = 1;
  3433. }
  3434. } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
  3435. os_memcpy(go_ifname, go_wpa_s->ifname,
  3436. sizeof(go_ifname));
  3437. if (is_zero_ether_addr(grp_mac)) {
  3438. wpa_dbg(go_wpa_s, MSG_DEBUG,
  3439. "P2P: Setting PIN-1 for ANY");
  3440. wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
  3441. "12345670", NULL, 0,
  3442. 0);
  3443. } else {
  3444. wpa_dbg(go_wpa_s, MSG_DEBUG,
  3445. "P2P: Setting PIN-1 for " MACSTR,
  3446. MAC2STR(grp_mac));
  3447. wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
  3448. "12345670", NULL, 0,
  3449. 0);
  3450. }
  3451. os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
  3452. wpa_s->p2ps_method_config_any = 1;
  3453. }
  3454. wpa_msg_global(wpa_s, MSG_INFO,
  3455. P2P_EVENT_P2PS_PROVISION_DONE MACSTR
  3456. " status=%d conncap=%x"
  3457. " adv_id=%x adv_mac=" MACSTR
  3458. " session=%x mac=" MACSTR
  3459. " dev_passwd_id=%d go=%s%s",
  3460. MAC2STR(dev), status, conncap,
  3461. adv_id, MAC2STR(adv_mac),
  3462. ses_id, MAC2STR(ses_mac),
  3463. passwd_id, go_ifname, feat_cap_str);
  3464. return;
  3465. }
  3466. if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
  3467. wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
  3468. if (persistent_go && !persistent_go->num_p2p_clients) {
  3469. /* remove empty persistent GO */
  3470. wpa_config_remove_network(wpa_s->conf, persistent_go->id);
  3471. }
  3472. if (conncap == P2PS_SETUP_CLIENT) {
  3473. wpa_msg_global(wpa_s, MSG_INFO,
  3474. P2P_EVENT_P2PS_PROVISION_DONE MACSTR
  3475. " status=%d conncap=%x"
  3476. " adv_id=%x adv_mac=" MACSTR
  3477. " session=%x mac=" MACSTR
  3478. " dev_passwd_id=%d join=" MACSTR "%s",
  3479. MAC2STR(dev), status, conncap,
  3480. adv_id, MAC2STR(adv_mac),
  3481. ses_id, MAC2STR(ses_mac),
  3482. passwd_id, MAC2STR(grp_mac), feat_cap_str);
  3483. } else {
  3484. wpa_msg_global(wpa_s, MSG_INFO,
  3485. P2P_EVENT_P2PS_PROVISION_DONE MACSTR
  3486. " status=%d conncap=%x"
  3487. " adv_id=%x adv_mac=" MACSTR
  3488. " session=%x mac=" MACSTR
  3489. " dev_passwd_id=%d%s",
  3490. MAC2STR(dev), status, conncap,
  3491. adv_id, MAC2STR(adv_mac),
  3492. ses_id, MAC2STR(ses_mac),
  3493. passwd_id, feat_cap_str);
  3494. }
  3495. }
  3496. static int _wpas_p2p_in_progress(void *ctx)
  3497. {
  3498. struct wpa_supplicant *wpa_s = ctx;
  3499. return wpas_p2p_in_progress(wpa_s);
  3500. }
  3501. static int wpas_prov_disc_resp_cb(void *ctx)
  3502. {
  3503. struct wpa_supplicant *wpa_s = ctx;
  3504. struct wpa_ssid *persistent_go;
  3505. unsigned int freq;
  3506. if (!wpa_s->global->pending_p2ps_group)
  3507. return 0;
  3508. freq = wpa_s->global->pending_p2ps_group_freq;
  3509. wpa_s->global->pending_p2ps_group_freq = 0;
  3510. wpa_s->global->pending_p2ps_group = 0;
  3511. if (wpas_p2p_get_go_group(wpa_s))
  3512. return 0;
  3513. persistent_go = wpas_p2p_get_persistent_go(wpa_s);
  3514. if (persistent_go) {
  3515. wpas_p2p_group_add_persistent(
  3516. wpa_s, persistent_go, 0, 0, 0, 0, 0, NULL,
  3517. persistent_go->mode == WPAS_MODE_P2P_GO ?
  3518. P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0);
  3519. } else {
  3520. wpas_p2p_group_add(wpa_s, 1, freq, 0, 0);
  3521. }
  3522. return 1;
  3523. }
  3524. static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
  3525. unsigned int *len,
  3526. unsigned int *freq_list)
  3527. {
  3528. struct wpa_supplicant *wpa_s = ctx;
  3529. return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
  3530. WPA_IF_P2P_CLIENT, len, freq_list);
  3531. }
  3532. /**
  3533. * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
  3534. * @global: Pointer to global data from wpa_supplicant_init()
  3535. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3536. * Returns: 0 on success, -1 on failure
  3537. */
  3538. int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
  3539. {
  3540. struct p2p_config p2p;
  3541. int i;
  3542. if (wpa_s->conf->p2p_disabled)
  3543. return 0;
  3544. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
  3545. return 0;
  3546. if (global->p2p)
  3547. return 0;
  3548. os_memset(&p2p, 0, sizeof(p2p));
  3549. p2p.cb_ctx = wpa_s;
  3550. p2p.debug_print = wpas_p2p_debug_print;
  3551. p2p.p2p_scan = wpas_p2p_scan;
  3552. p2p.send_action = wpas_send_action;
  3553. p2p.send_action_done = wpas_send_action_done;
  3554. p2p.go_neg_completed = wpas_go_neg_completed;
  3555. p2p.go_neg_req_rx = wpas_go_neg_req_rx;
  3556. p2p.dev_found = wpas_dev_found;
  3557. p2p.dev_lost = wpas_dev_lost;
  3558. p2p.find_stopped = wpas_find_stopped;
  3559. p2p.start_listen = wpas_start_listen;
  3560. p2p.stop_listen = wpas_stop_listen;
  3561. p2p.send_probe_resp = wpas_send_probe_resp;
  3562. p2p.sd_request = wpas_sd_request;
  3563. p2p.sd_response = wpas_sd_response;
  3564. p2p.prov_disc_req = wpas_prov_disc_req;
  3565. p2p.prov_disc_resp = wpas_prov_disc_resp;
  3566. p2p.prov_disc_fail = wpas_prov_disc_fail;
  3567. p2p.invitation_process = wpas_invitation_process;
  3568. p2p.invitation_received = wpas_invitation_received;
  3569. p2p.invitation_result = wpas_invitation_result;
  3570. p2p.get_noa = wpas_get_noa;
  3571. p2p.go_connected = wpas_go_connected;
  3572. p2p.presence_resp = wpas_presence_resp;
  3573. p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
  3574. p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
  3575. p2p.get_persistent_group = wpas_get_persistent_group;
  3576. p2p.get_go_info = wpas_get_go_info;
  3577. p2p.remove_stale_groups = wpas_remove_stale_groups;
  3578. p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
  3579. p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
  3580. p2p.p2ps_group_capability = p2ps_group_capability;
  3581. p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
  3582. os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
  3583. os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
  3584. p2p.dev_name = wpa_s->conf->device_name;
  3585. p2p.manufacturer = wpa_s->conf->manufacturer;
  3586. p2p.model_name = wpa_s->conf->model_name;
  3587. p2p.model_number = wpa_s->conf->model_number;
  3588. p2p.serial_number = wpa_s->conf->serial_number;
  3589. if (wpa_s->wps) {
  3590. os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
  3591. p2p.config_methods = wpa_s->wps->config_methods;
  3592. }
  3593. if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
  3594. wpa_printf(MSG_ERROR,
  3595. "P2P: Failed to configure supported channel list");
  3596. return -1;
  3597. }
  3598. if (wpa_s->conf->p2p_listen_reg_class &&
  3599. wpa_s->conf->p2p_listen_channel) {
  3600. p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
  3601. p2p.channel = wpa_s->conf->p2p_listen_channel;
  3602. p2p.channel_forced = 1;
  3603. } else {
  3604. /*
  3605. * Pick one of the social channels randomly as the listen
  3606. * channel.
  3607. */
  3608. if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
  3609. &p2p.channel) != 0) {
  3610. wpa_printf(MSG_INFO,
  3611. "P2P: No social channels supported by the driver - do not enable P2P");
  3612. return 0;
  3613. }
  3614. p2p.channel_forced = 0;
  3615. }
  3616. wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
  3617. p2p.reg_class, p2p.channel);
  3618. if (wpa_s->conf->p2p_oper_reg_class &&
  3619. wpa_s->conf->p2p_oper_channel) {
  3620. p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
  3621. p2p.op_channel = wpa_s->conf->p2p_oper_channel;
  3622. p2p.cfg_op_channel = 1;
  3623. wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
  3624. "%d:%d", p2p.op_reg_class, p2p.op_channel);
  3625. } else {
  3626. /*
  3627. * Use random operation channel from 2.4 GHz band social
  3628. * channels (1, 6, 11) or band 60 GHz social channel (2) if no
  3629. * other preference is indicated.
  3630. */
  3631. if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
  3632. &p2p.op_channel) != 0) {
  3633. wpa_printf(MSG_ERROR,
  3634. "P2P: Failed to select random social channel as operation channel");
  3635. return -1;
  3636. }
  3637. p2p.cfg_op_channel = 0;
  3638. wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
  3639. "%d:%d", p2p.op_reg_class, p2p.op_channel);
  3640. }
  3641. if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
  3642. p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
  3643. p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
  3644. }
  3645. if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  3646. os_memcpy(p2p.country, wpa_s->conf->country, 2);
  3647. p2p.country[2] = 0x04;
  3648. } else
  3649. os_memcpy(p2p.country, "XX\x04", 3);
  3650. os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
  3651. WPS_DEV_TYPE_LEN);
  3652. p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
  3653. os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
  3654. p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
  3655. p2p.concurrent_operations = !!(wpa_s->drv_flags &
  3656. WPA_DRIVER_FLAGS_P2P_CONCURRENT);
  3657. p2p.max_peers = 100;
  3658. if (wpa_s->conf->p2p_ssid_postfix) {
  3659. p2p.ssid_postfix_len =
  3660. os_strlen(wpa_s->conf->p2p_ssid_postfix);
  3661. if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
  3662. p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
  3663. os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
  3664. p2p.ssid_postfix_len);
  3665. }
  3666. p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
  3667. p2p.max_listen = wpa_s->max_remain_on_chan;
  3668. if (wpa_s->conf->p2p_passphrase_len >= 8 &&
  3669. wpa_s->conf->p2p_passphrase_len <= 63)
  3670. p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
  3671. else
  3672. p2p.passphrase_len = 8;
  3673. global->p2p = p2p_init(&p2p);
  3674. if (global->p2p == NULL)
  3675. return -1;
  3676. global->p2p_init_wpa_s = wpa_s;
  3677. for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
  3678. if (wpa_s->conf->wps_vendor_ext[i] == NULL)
  3679. continue;
  3680. p2p_add_wps_vendor_extension(
  3681. global->p2p, wpa_s->conf->wps_vendor_ext[i]);
  3682. }
  3683. p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
  3684. return 0;
  3685. }
  3686. /**
  3687. * wpas_p2p_deinit - Deinitialize per-interface P2P data
  3688. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3689. *
  3690. * This function deinitialize per-interface P2P data.
  3691. */
  3692. void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
  3693. {
  3694. if (wpa_s->driver && wpa_s->drv_priv)
  3695. wpa_drv_probe_req_report(wpa_s, 0);
  3696. if (wpa_s->go_params) {
  3697. /* Clear any stored provisioning info */
  3698. p2p_clear_provisioning_info(
  3699. wpa_s->global->p2p,
  3700. wpa_s->go_params->peer_device_addr);
  3701. }
  3702. os_free(wpa_s->go_params);
  3703. wpa_s->go_params = NULL;
  3704. eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
  3705. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  3706. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3707. wpa_s->p2p_long_listen = 0;
  3708. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  3709. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  3710. wpas_p2p_remove_pending_group_interface(wpa_s);
  3711. eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
  3712. wpas_p2p_listen_work_done(wpa_s);
  3713. if (wpa_s->p2p_send_action_work) {
  3714. os_free(wpa_s->p2p_send_action_work->ctx);
  3715. radio_work_done(wpa_s->p2p_send_action_work);
  3716. wpa_s->p2p_send_action_work = NULL;
  3717. }
  3718. eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
  3719. wpabuf_free(wpa_s->p2p_oob_dev_pw);
  3720. wpa_s->p2p_oob_dev_pw = NULL;
  3721. os_free(wpa_s->p2p_group_common_freqs);
  3722. wpa_s->p2p_group_common_freqs = NULL;
  3723. wpa_s->p2p_group_common_freqs_num = 0;
  3724. /* TODO: remove group interface from the driver if this wpa_s instance
  3725. * is on top of a P2P group interface */
  3726. }
  3727. /**
  3728. * wpas_p2p_deinit_global - Deinitialize global P2P module
  3729. * @global: Pointer to global data from wpa_supplicant_init()
  3730. *
  3731. * This function deinitializes the global (per device) P2P module.
  3732. */
  3733. static void wpas_p2p_deinit_global(struct wpa_global *global)
  3734. {
  3735. struct wpa_supplicant *wpa_s, *tmp;
  3736. wpa_s = global->ifaces;
  3737. wpas_p2p_service_flush(global->p2p_init_wpa_s);
  3738. /* Remove remaining P2P group interfaces */
  3739. while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
  3740. wpa_s = wpa_s->next;
  3741. while (wpa_s) {
  3742. tmp = global->ifaces;
  3743. while (tmp &&
  3744. (tmp == wpa_s ||
  3745. tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
  3746. tmp = tmp->next;
  3747. }
  3748. if (tmp == NULL)
  3749. break;
  3750. /* Disconnect from the P2P group and deinit the interface */
  3751. wpas_p2p_disconnect(tmp);
  3752. }
  3753. /*
  3754. * Deinit GO data on any possibly remaining interface (if main
  3755. * interface is used as GO).
  3756. */
  3757. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3758. if (wpa_s->ap_iface)
  3759. wpas_p2p_group_deinit(wpa_s);
  3760. }
  3761. p2p_deinit(global->p2p);
  3762. global->p2p = NULL;
  3763. global->p2p_init_wpa_s = NULL;
  3764. }
  3765. static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
  3766. {
  3767. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
  3768. wpa_s->conf->p2p_no_group_iface)
  3769. return 0; /* separate interface disabled per configuration */
  3770. if (wpa_s->drv_flags &
  3771. (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
  3772. WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
  3773. return 1; /* P2P group requires a new interface in every case
  3774. */
  3775. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
  3776. return 0; /* driver does not support concurrent operations */
  3777. if (wpa_s->global->ifaces->next)
  3778. return 1; /* more that one interface already in use */
  3779. if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
  3780. return 1; /* this interface is already in use */
  3781. return 0;
  3782. }
  3783. static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
  3784. const u8 *peer_addr,
  3785. enum p2p_wps_method wps_method,
  3786. int go_intent, const u8 *own_interface_addr,
  3787. unsigned int force_freq, int persistent_group,
  3788. struct wpa_ssid *ssid, unsigned int pref_freq)
  3789. {
  3790. if (persistent_group && wpa_s->conf->persistent_reconnect)
  3791. persistent_group = 2;
  3792. /*
  3793. * Increase GO config timeout if HT40 is used since it takes some time
  3794. * to scan channels for coex purposes before the BSS can be started.
  3795. */
  3796. p2p_set_config_timeout(wpa_s->global->p2p,
  3797. wpa_s->p2p_go_ht40 ? 255 : 100, 20);
  3798. return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
  3799. go_intent, own_interface_addr, force_freq,
  3800. persistent_group, ssid ? ssid->ssid : NULL,
  3801. ssid ? ssid->ssid_len : 0,
  3802. wpa_s->p2p_pd_before_go_neg, pref_freq,
  3803. wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
  3804. 0);
  3805. }
  3806. static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
  3807. const u8 *peer_addr,
  3808. enum p2p_wps_method wps_method,
  3809. int go_intent, const u8 *own_interface_addr,
  3810. unsigned int force_freq, int persistent_group,
  3811. struct wpa_ssid *ssid, unsigned int pref_freq)
  3812. {
  3813. if (persistent_group && wpa_s->conf->persistent_reconnect)
  3814. persistent_group = 2;
  3815. return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
  3816. go_intent, own_interface_addr, force_freq,
  3817. persistent_group, ssid ? ssid->ssid : NULL,
  3818. ssid ? ssid->ssid_len : 0, pref_freq,
  3819. wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
  3820. 0);
  3821. }
  3822. static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
  3823. {
  3824. wpa_s->p2p_join_scan_count++;
  3825. wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
  3826. wpa_s->p2p_join_scan_count);
  3827. if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
  3828. wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
  3829. " for join operationg - stop join attempt",
  3830. MAC2STR(wpa_s->pending_join_iface_addr));
  3831. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3832. if (wpa_s->p2p_auto_pd) {
  3833. wpa_s->p2p_auto_pd = 0;
  3834. wpa_msg_global(wpa_s, MSG_INFO,
  3835. P2P_EVENT_PROV_DISC_FAILURE
  3836. " p2p_dev_addr=" MACSTR " status=N/A",
  3837. MAC2STR(wpa_s->pending_join_dev_addr));
  3838. return;
  3839. }
  3840. wpa_msg_global(wpa_s->parent, MSG_INFO,
  3841. P2P_EVENT_GROUP_FORMATION_FAILURE);
  3842. wpas_notify_p2p_group_formation_failure(wpa_s, "");
  3843. }
  3844. }
  3845. static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
  3846. {
  3847. int res;
  3848. unsigned int num, i;
  3849. struct wpa_used_freq_data *freqs;
  3850. if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
  3851. /* Multiple channels are supported and not all are in use */
  3852. return 0;
  3853. }
  3854. freqs = os_calloc(wpa_s->num_multichan_concurrent,
  3855. sizeof(struct wpa_used_freq_data));
  3856. if (!freqs)
  3857. return 1;
  3858. num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
  3859. wpa_s->num_multichan_concurrent);
  3860. for (i = 0; i < num; i++) {
  3861. if (freqs[i].freq == freq) {
  3862. wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
  3863. freq);
  3864. res = 0;
  3865. goto exit_free;
  3866. }
  3867. }
  3868. wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
  3869. res = 1;
  3870. exit_free:
  3871. os_free(freqs);
  3872. return res;
  3873. }
  3874. static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
  3875. const u8 *peer_dev_addr)
  3876. {
  3877. struct wpa_bss *bss;
  3878. int updated;
  3879. bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
  3880. if (bss == NULL)
  3881. return -1;
  3882. if (bss->last_update_idx < wpa_s->bss_update_idx) {
  3883. wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
  3884. "last scan");
  3885. return 0;
  3886. }
  3887. updated = os_reltime_before(&wpa_s->p2p_auto_started,
  3888. &bss->last_update);
  3889. wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
  3890. "%ld.%06ld (%supdated in last scan)",
  3891. bss->last_update.sec, bss->last_update.usec,
  3892. updated ? "": "not ");
  3893. return updated;
  3894. }
  3895. static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
  3896. struct wpa_scan_results *scan_res)
  3897. {
  3898. struct wpa_bss *bss = NULL;
  3899. int freq;
  3900. u8 iface_addr[ETH_ALEN];
  3901. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3902. if (wpa_s->global->p2p_disabled)
  3903. return;
  3904. wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
  3905. scan_res ? (int) scan_res->num : -1,
  3906. wpa_s->p2p_auto_join ? "auto_" : "");
  3907. if (scan_res)
  3908. wpas_p2p_scan_res_handler(wpa_s, scan_res);
  3909. if (wpa_s->p2p_auto_pd) {
  3910. int join = wpas_p2p_peer_go(wpa_s,
  3911. wpa_s->pending_join_dev_addr);
  3912. if (join == 0 &&
  3913. wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
  3914. wpa_s->auto_pd_scan_retry++;
  3915. bss = wpa_bss_get_bssid_latest(
  3916. wpa_s, wpa_s->pending_join_dev_addr);
  3917. if (bss) {
  3918. freq = bss->freq;
  3919. wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
  3920. "the peer " MACSTR " at %d MHz",
  3921. wpa_s->auto_pd_scan_retry,
  3922. MAC2STR(wpa_s->
  3923. pending_join_dev_addr),
  3924. freq);
  3925. wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
  3926. return;
  3927. }
  3928. }
  3929. if (join < 0)
  3930. join = 0;
  3931. wpa_s->p2p_auto_pd = 0;
  3932. wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
  3933. wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
  3934. MAC2STR(wpa_s->pending_join_dev_addr), join);
  3935. if (p2p_prov_disc_req(wpa_s->global->p2p,
  3936. wpa_s->pending_join_dev_addr, NULL,
  3937. wpa_s->pending_pd_config_methods, join,
  3938. 0, wpa_s->user_initiated_pd) < 0) {
  3939. wpa_s->p2p_auto_pd = 0;
  3940. wpa_msg_global(wpa_s, MSG_INFO,
  3941. P2P_EVENT_PROV_DISC_FAILURE
  3942. " p2p_dev_addr=" MACSTR " status=N/A",
  3943. MAC2STR(wpa_s->pending_join_dev_addr));
  3944. }
  3945. return;
  3946. }
  3947. if (wpa_s->p2p_auto_join) {
  3948. int join = wpas_p2p_peer_go(wpa_s,
  3949. wpa_s->pending_join_dev_addr);
  3950. if (join < 0) {
  3951. wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
  3952. "running a GO -> use GO Negotiation");
  3953. wpa_msg_global(wpa_s->parent, MSG_INFO,
  3954. P2P_EVENT_FALLBACK_TO_GO_NEG
  3955. "reason=peer-not-running-GO");
  3956. wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
  3957. wpa_s->p2p_pin, wpa_s->p2p_wps_method,
  3958. wpa_s->p2p_persistent_group, 0, 0, 0,
  3959. wpa_s->p2p_go_intent,
  3960. wpa_s->p2p_connect_freq,
  3961. wpa_s->p2p_persistent_id,
  3962. wpa_s->p2p_pd_before_go_neg,
  3963. wpa_s->p2p_go_ht40,
  3964. wpa_s->p2p_go_vht);
  3965. return;
  3966. }
  3967. wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
  3968. "try to join the group", join ? "" :
  3969. " in older scan");
  3970. if (!join) {
  3971. wpa_msg_global(wpa_s->parent, MSG_INFO,
  3972. P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
  3973. wpa_s->p2p_fallback_to_go_neg = 1;
  3974. }
  3975. }
  3976. freq = p2p_get_oper_freq(wpa_s->global->p2p,
  3977. wpa_s->pending_join_iface_addr);
  3978. if (freq < 0 &&
  3979. p2p_get_interface_addr(wpa_s->global->p2p,
  3980. wpa_s->pending_join_dev_addr,
  3981. iface_addr) == 0 &&
  3982. os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
  3983. && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
  3984. wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
  3985. "address for join from " MACSTR " to " MACSTR
  3986. " based on newly discovered P2P peer entry",
  3987. MAC2STR(wpa_s->pending_join_iface_addr),
  3988. MAC2STR(iface_addr));
  3989. os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
  3990. ETH_ALEN);
  3991. freq = p2p_get_oper_freq(wpa_s->global->p2p,
  3992. wpa_s->pending_join_iface_addr);
  3993. }
  3994. if (freq >= 0) {
  3995. wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
  3996. "from P2P peer table: %d MHz", freq);
  3997. }
  3998. if (wpa_s->p2p_join_ssid_len) {
  3999. wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
  4000. MACSTR " and SSID %s",
  4001. MAC2STR(wpa_s->pending_join_iface_addr),
  4002. wpa_ssid_txt(wpa_s->p2p_join_ssid,
  4003. wpa_s->p2p_join_ssid_len));
  4004. bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
  4005. wpa_s->p2p_join_ssid,
  4006. wpa_s->p2p_join_ssid_len);
  4007. }
  4008. if (!bss) {
  4009. wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
  4010. MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
  4011. bss = wpa_bss_get_bssid_latest(wpa_s,
  4012. wpa_s->pending_join_iface_addr);
  4013. }
  4014. if (bss) {
  4015. u8 dev_addr[ETH_ALEN];
  4016. freq = bss->freq;
  4017. wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
  4018. "from BSS table: %d MHz (SSID %s)", freq,
  4019. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  4020. if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
  4021. dev_addr) == 0 &&
  4022. os_memcmp(wpa_s->pending_join_dev_addr,
  4023. wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
  4024. os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
  4025. ETH_ALEN) != 0) {
  4026. wpa_printf(MSG_DEBUG,
  4027. "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
  4028. MAC2STR(dev_addr),
  4029. MAC2STR(wpa_s->pending_join_dev_addr));
  4030. os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
  4031. ETH_ALEN);
  4032. }
  4033. }
  4034. if (freq > 0) {
  4035. u16 method;
  4036. if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
  4037. wpa_msg_global(wpa_s->parent, MSG_INFO,
  4038. P2P_EVENT_GROUP_FORMATION_FAILURE
  4039. "reason=FREQ_CONFLICT");
  4040. wpas_notify_p2p_group_formation_failure(
  4041. wpa_s, "FREQ_CONFLICT");
  4042. return;
  4043. }
  4044. wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
  4045. "prior to joining an existing group (GO " MACSTR
  4046. " freq=%u MHz)",
  4047. MAC2STR(wpa_s->pending_join_dev_addr), freq);
  4048. wpa_s->pending_pd_before_join = 1;
  4049. switch (wpa_s->pending_join_wps_method) {
  4050. case WPS_PIN_DISPLAY:
  4051. method = WPS_CONFIG_KEYPAD;
  4052. break;
  4053. case WPS_PIN_KEYPAD:
  4054. method = WPS_CONFIG_DISPLAY;
  4055. break;
  4056. case WPS_PBC:
  4057. method = WPS_CONFIG_PUSHBUTTON;
  4058. break;
  4059. case WPS_P2PS:
  4060. method = WPS_CONFIG_P2PS;
  4061. break;
  4062. default:
  4063. method = 0;
  4064. break;
  4065. }
  4066. if ((p2p_get_provisioning_info(wpa_s->global->p2p,
  4067. wpa_s->pending_join_dev_addr) ==
  4068. method)) {
  4069. /*
  4070. * We have already performed provision discovery for
  4071. * joining the group. Proceed directly to join
  4072. * operation without duplicated provision discovery. */
  4073. wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
  4074. "with " MACSTR " already done - proceed to "
  4075. "join",
  4076. MAC2STR(wpa_s->pending_join_dev_addr));
  4077. wpa_s->pending_pd_before_join = 0;
  4078. goto start;
  4079. }
  4080. if (p2p_prov_disc_req(wpa_s->global->p2p,
  4081. wpa_s->pending_join_dev_addr,
  4082. NULL, method, 1,
  4083. freq, wpa_s->user_initiated_pd) < 0) {
  4084. wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
  4085. "Discovery Request before joining an "
  4086. "existing group");
  4087. wpa_s->pending_pd_before_join = 0;
  4088. goto start;
  4089. }
  4090. return;
  4091. }
  4092. wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
  4093. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  4094. eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
  4095. wpas_p2p_check_join_scan_limit(wpa_s);
  4096. return;
  4097. start:
  4098. /* Start join operation immediately */
  4099. wpas_p2p_join_start(wpa_s, 0, NULL, 0);
  4100. }
  4101. static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
  4102. const u8 *ssid, size_t ssid_len)
  4103. {
  4104. int ret;
  4105. struct wpa_driver_scan_params params;
  4106. struct wpabuf *wps_ie, *ies;
  4107. size_t ielen;
  4108. int freqs[2] = { 0, 0 };
  4109. os_memset(&params, 0, sizeof(params));
  4110. /* P2P Wildcard SSID */
  4111. params.num_ssids = 1;
  4112. if (ssid && ssid_len) {
  4113. params.ssids[0].ssid = ssid;
  4114. params.ssids[0].ssid_len = ssid_len;
  4115. os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
  4116. wpa_s->p2p_join_ssid_len = ssid_len;
  4117. } else {
  4118. params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
  4119. params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
  4120. wpa_s->p2p_join_ssid_len = 0;
  4121. }
  4122. wpa_s->wps->dev.p2p = 1;
  4123. wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
  4124. wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
  4125. NULL);
  4126. if (wps_ie == NULL) {
  4127. wpas_p2p_scan_res_join(wpa_s, NULL);
  4128. return;
  4129. }
  4130. ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
  4131. ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
  4132. if (ies == NULL) {
  4133. wpabuf_free(wps_ie);
  4134. wpas_p2p_scan_res_join(wpa_s, NULL);
  4135. return;
  4136. }
  4137. wpabuf_put_buf(ies, wps_ie);
  4138. wpabuf_free(wps_ie);
  4139. p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
  4140. params.p2p_probe = 1;
  4141. params.extra_ies = wpabuf_head(ies);
  4142. params.extra_ies_len = wpabuf_len(ies);
  4143. if (!freq) {
  4144. int oper_freq;
  4145. /*
  4146. * If freq is not provided, check the operating freq of the GO
  4147. * and use a single channel scan on if possible.
  4148. */
  4149. oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
  4150. wpa_s->pending_join_iface_addr);
  4151. if (oper_freq > 0)
  4152. freq = oper_freq;
  4153. }
  4154. if (freq > 0) {
  4155. freqs[0] = freq;
  4156. params.freqs = freqs;
  4157. }
  4158. /*
  4159. * Run a scan to update BSS table and start Provision Discovery once
  4160. * the new scan results become available.
  4161. */
  4162. ret = wpa_drv_scan(wpa_s, &params);
  4163. if (!ret) {
  4164. os_get_reltime(&wpa_s->scan_trigger_time);
  4165. wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
  4166. wpa_s->own_scan_requested = 1;
  4167. }
  4168. wpabuf_free(ies);
  4169. if (ret) {
  4170. wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
  4171. "try again later");
  4172. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  4173. eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
  4174. wpas_p2p_check_join_scan_limit(wpa_s);
  4175. }
  4176. }
  4177. static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
  4178. {
  4179. struct wpa_supplicant *wpa_s = eloop_ctx;
  4180. wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
  4181. }
  4182. static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
  4183. const u8 *dev_addr, enum p2p_wps_method wps_method,
  4184. int auto_join, int op_freq,
  4185. const u8 *ssid, size_t ssid_len)
  4186. {
  4187. wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
  4188. MACSTR " dev " MACSTR " op_freq=%d)%s",
  4189. MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
  4190. auto_join ? " (auto_join)" : "");
  4191. if (ssid && ssid_len) {
  4192. wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
  4193. wpa_ssid_txt(ssid, ssid_len));
  4194. }
  4195. wpa_s->p2p_auto_pd = 0;
  4196. wpa_s->p2p_auto_join = !!auto_join;
  4197. os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
  4198. os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
  4199. wpa_s->pending_join_wps_method = wps_method;
  4200. /* Make sure we are not running find during connection establishment */
  4201. wpas_p2p_stop_find(wpa_s);
  4202. wpa_s->p2p_join_scan_count = 0;
  4203. wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
  4204. return 0;
  4205. }
  4206. static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
  4207. const u8 *ssid, size_t ssid_len)
  4208. {
  4209. struct wpa_supplicant *group;
  4210. struct p2p_go_neg_results res;
  4211. struct wpa_bss *bss;
  4212. group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
  4213. if (group == NULL)
  4214. return -1;
  4215. if (group != wpa_s) {
  4216. os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
  4217. sizeof(group->p2p_pin));
  4218. group->p2p_wps_method = wpa_s->p2p_wps_method;
  4219. } else {
  4220. /*
  4221. * Need to mark the current interface for p2p_group_formation
  4222. * when a separate group interface is not used. This is needed
  4223. * to allow p2p_cancel stop a pending p2p_connect-join.
  4224. * wpas_p2p_init_group_interface() addresses this for the case
  4225. * where a separate group interface is used.
  4226. */
  4227. wpa_s->global->p2p_group_formation = wpa_s;
  4228. }
  4229. group->p2p_in_provisioning = 1;
  4230. group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
  4231. os_memset(&res, 0, sizeof(res));
  4232. os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
  4233. os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
  4234. ETH_ALEN);
  4235. res.wps_method = wpa_s->pending_join_wps_method;
  4236. if (freq && ssid && ssid_len) {
  4237. res.freq = freq;
  4238. res.ssid_len = ssid_len;
  4239. os_memcpy(res.ssid, ssid, ssid_len);
  4240. } else {
  4241. bss = wpa_bss_get_bssid_latest(wpa_s,
  4242. wpa_s->pending_join_iface_addr);
  4243. if (bss) {
  4244. res.freq = bss->freq;
  4245. res.ssid_len = bss->ssid_len;
  4246. os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
  4247. wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
  4248. bss->freq,
  4249. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  4250. }
  4251. }
  4252. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  4253. wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
  4254. "starting client");
  4255. wpa_drv_cancel_remain_on_channel(wpa_s);
  4256. wpa_s->off_channel_freq = 0;
  4257. wpa_s->roc_waiting_drv_freq = 0;
  4258. }
  4259. wpas_start_wps_enrollee(group, &res);
  4260. /*
  4261. * Allow a longer timeout for join-a-running-group than normal 15
  4262. * second group formation timeout since the GO may not have authorized
  4263. * our connection yet.
  4264. */
  4265. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  4266. eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
  4267. wpa_s, NULL);
  4268. return 0;
  4269. }
  4270. static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
  4271. int *force_freq, int *pref_freq, int go,
  4272. unsigned int *pref_freq_list,
  4273. unsigned int *num_pref_freq)
  4274. {
  4275. struct wpa_used_freq_data *freqs;
  4276. int res, best_freq, num_unused;
  4277. unsigned int freq_in_use = 0, num, i, max_pref_freq;
  4278. max_pref_freq = *num_pref_freq;
  4279. *num_pref_freq = 0;
  4280. freqs = os_calloc(wpa_s->num_multichan_concurrent,
  4281. sizeof(struct wpa_used_freq_data));
  4282. if (!freqs)
  4283. return -1;
  4284. num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
  4285. wpa_s->num_multichan_concurrent);
  4286. /*
  4287. * It is possible that the total number of used frequencies is bigger
  4288. * than the number of frequencies used for P2P, so get the system wide
  4289. * number of unused frequencies.
  4290. */
  4291. num_unused = wpas_p2p_num_unused_channels(wpa_s);
  4292. wpa_printf(MSG_DEBUG,
  4293. "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
  4294. freq, wpa_s->num_multichan_concurrent, num, num_unused);
  4295. if (freq > 0) {
  4296. int ret;
  4297. if (go)
  4298. ret = p2p_supported_freq(wpa_s->global->p2p, freq);
  4299. else
  4300. ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
  4301. if (!ret) {
  4302. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
  4303. ieee80211_is_dfs(freq)) {
  4304. /*
  4305. * If freq is a DFS channel and DFS is offloaded
  4306. * to the driver, allow P2P GO to use it.
  4307. */
  4308. wpa_printf(MSG_DEBUG,
  4309. "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
  4310. freq);
  4311. } else {
  4312. wpa_printf(MSG_DEBUG,
  4313. "P2P: The forced channel (%u MHz) is not supported for P2P uses",
  4314. freq);
  4315. res = -3;
  4316. goto exit_free;
  4317. }
  4318. }
  4319. for (i = 0; i < num; i++) {
  4320. if (freqs[i].freq == freq)
  4321. freq_in_use = 1;
  4322. }
  4323. if (num_unused <= 0 && !freq_in_use) {
  4324. wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
  4325. freq);
  4326. res = -2;
  4327. goto exit_free;
  4328. }
  4329. wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
  4330. "requested channel (%u MHz)", freq);
  4331. *force_freq = freq;
  4332. goto exit_ok;
  4333. }
  4334. best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
  4335. if (!wpa_s->conf->num_p2p_pref_chan && *pref_freq == 0) {
  4336. enum wpa_driver_if_type iface_type;
  4337. if (go)
  4338. iface_type = WPA_IF_P2P_GO;
  4339. else
  4340. iface_type = WPA_IF_P2P_CLIENT;
  4341. wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
  4342. best_freq, go);
  4343. res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
  4344. &max_pref_freq,
  4345. pref_freq_list);
  4346. if (!res && max_pref_freq > 0) {
  4347. *num_pref_freq = max_pref_freq;
  4348. i = 0;
  4349. while (wpas_p2p_disallowed_freq(wpa_s->global,
  4350. pref_freq_list[i]) &&
  4351. i < *num_pref_freq) {
  4352. wpa_printf(MSG_DEBUG,
  4353. "P2P: preferred_freq_list[%d]=%d is disallowed",
  4354. i, pref_freq_list[i]);
  4355. i++;
  4356. }
  4357. if (i != *num_pref_freq) {
  4358. best_freq = pref_freq_list[i];
  4359. wpa_printf(MSG_DEBUG,
  4360. "P2P: Using preferred_freq_list[%d]=%d",
  4361. i, best_freq);
  4362. } else {
  4363. wpa_printf(MSG_DEBUG,
  4364. "P2P: All driver preferred frequencies are disallowed for P2P use");
  4365. *num_pref_freq = 0;
  4366. }
  4367. } else {
  4368. wpa_printf(MSG_DEBUG,
  4369. "P2P: No preferred frequency list available");
  4370. }
  4371. }
  4372. /* We have a candidate frequency to use */
  4373. if (best_freq > 0) {
  4374. if (*pref_freq == 0 && num_unused > 0) {
  4375. wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
  4376. best_freq);
  4377. *pref_freq = best_freq;
  4378. } else {
  4379. wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
  4380. best_freq);
  4381. *force_freq = best_freq;
  4382. }
  4383. } else if (num_unused > 0) {
  4384. wpa_printf(MSG_DEBUG,
  4385. "P2P: Current operating channels are not available for P2P. Try to use another channel");
  4386. *force_freq = 0;
  4387. } else {
  4388. wpa_printf(MSG_DEBUG,
  4389. "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
  4390. res = -2;
  4391. goto exit_free;
  4392. }
  4393. exit_ok:
  4394. res = 0;
  4395. exit_free:
  4396. os_free(freqs);
  4397. return res;
  4398. }
  4399. /**
  4400. * wpas_p2p_connect - Request P2P Group Formation to be started
  4401. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  4402. * @peer_addr: Address of the peer P2P Device
  4403. * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
  4404. * @persistent_group: Whether to create a persistent group
  4405. * @auto_join: Whether to select join vs. GO Negotiation automatically
  4406. * @join: Whether to join an existing group (as a client) instead of starting
  4407. * Group Owner negotiation; @peer_addr is BSSID in that case
  4408. * @auth: Whether to only authorize the connection instead of doing that and
  4409. * initiating Group Owner negotiation
  4410. * @go_intent: GO Intent or -1 to use default
  4411. * @freq: Frequency for the group or 0 for auto-selection
  4412. * @persistent_id: Persistent group credentials to use for forcing GO
  4413. * parameters or -1 to generate new values (SSID/passphrase)
  4414. * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
  4415. * interoperability workaround when initiating group formation
  4416. * @ht40: Start GO with 40 MHz channel width
  4417. * @vht: Start GO with VHT support
  4418. * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
  4419. * failure, -2 on failure due to channel not currently available,
  4420. * -3 if forced channel is not supported
  4421. */
  4422. int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  4423. const char *pin, enum p2p_wps_method wps_method,
  4424. int persistent_group, int auto_join, int join, int auth,
  4425. int go_intent, int freq, int persistent_id, int pd,
  4426. int ht40, int vht)
  4427. {
  4428. int force_freq = 0, pref_freq = 0;
  4429. int ret = 0, res;
  4430. enum wpa_driver_if_type iftype;
  4431. const u8 *if_addr;
  4432. struct wpa_ssid *ssid = NULL;
  4433. unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
  4434. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4435. return -1;
  4436. if (persistent_id >= 0) {
  4437. ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
  4438. if (ssid == NULL || ssid->disabled != 2 ||
  4439. ssid->mode != WPAS_MODE_P2P_GO)
  4440. return -1;
  4441. }
  4442. os_free(wpa_s->global->add_psk);
  4443. wpa_s->global->add_psk = NULL;
  4444. wpa_s->global->p2p_fail_on_wps_complete = 0;
  4445. wpa_s->global->pending_p2ps_group = 0;
  4446. wpa_s->global->pending_p2ps_group_freq = 0;
  4447. wpa_s->p2ps_method_config_any = 0;
  4448. if (go_intent < 0)
  4449. go_intent = wpa_s->conf->p2p_go_intent;
  4450. if (!auth)
  4451. wpa_s->p2p_long_listen = 0;
  4452. wpa_s->p2p_wps_method = wps_method;
  4453. wpa_s->p2p_persistent_group = !!persistent_group;
  4454. wpa_s->p2p_persistent_id = persistent_id;
  4455. wpa_s->p2p_go_intent = go_intent;
  4456. wpa_s->p2p_connect_freq = freq;
  4457. wpa_s->p2p_fallback_to_go_neg = 0;
  4458. wpa_s->p2p_pd_before_go_neg = !!pd;
  4459. wpa_s->p2p_go_ht40 = !!ht40;
  4460. wpa_s->p2p_go_vht = !!vht;
  4461. if (pin)
  4462. os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
  4463. else if (wps_method == WPS_PIN_DISPLAY) {
  4464. ret = wps_generate_pin();
  4465. res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
  4466. "%08d", ret);
  4467. if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
  4468. wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
  4469. wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
  4470. wpa_s->p2p_pin);
  4471. } else
  4472. wpa_s->p2p_pin[0] = '\0';
  4473. if (join || auto_join) {
  4474. u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
  4475. if (auth) {
  4476. wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
  4477. "connect a running group from " MACSTR,
  4478. MAC2STR(peer_addr));
  4479. os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
  4480. return ret;
  4481. }
  4482. os_memcpy(dev_addr, peer_addr, ETH_ALEN);
  4483. if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
  4484. iface_addr) < 0) {
  4485. os_memcpy(iface_addr, peer_addr, ETH_ALEN);
  4486. p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
  4487. dev_addr);
  4488. }
  4489. if (auto_join) {
  4490. os_get_reltime(&wpa_s->p2p_auto_started);
  4491. wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
  4492. "%ld.%06ld",
  4493. wpa_s->p2p_auto_started.sec,
  4494. wpa_s->p2p_auto_started.usec);
  4495. }
  4496. wpa_s->user_initiated_pd = 1;
  4497. if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
  4498. auto_join, freq, NULL, 0) < 0)
  4499. return -1;
  4500. return ret;
  4501. }
  4502. size = P2P_MAX_PREF_CHANNELS;
  4503. res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
  4504. go_intent == 15, pref_freq_list, &size);
  4505. if (res)
  4506. return res;
  4507. wpas_p2p_set_own_freq_preference(wpa_s,
  4508. force_freq ? force_freq : pref_freq);
  4509. p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
  4510. wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
  4511. if (wpa_s->create_p2p_iface) {
  4512. /* Prepare to add a new interface for the group */
  4513. iftype = WPA_IF_P2P_GROUP;
  4514. if (go_intent == 15)
  4515. iftype = WPA_IF_P2P_GO;
  4516. if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
  4517. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  4518. "interface for the group");
  4519. return -1;
  4520. }
  4521. if_addr = wpa_s->pending_interface_addr;
  4522. } else {
  4523. if_addr = wpa_s->own_addr;
  4524. os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
  4525. }
  4526. if (auth) {
  4527. if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
  4528. go_intent, if_addr,
  4529. force_freq, persistent_group, ssid,
  4530. pref_freq) < 0)
  4531. return -1;
  4532. return ret;
  4533. }
  4534. if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
  4535. go_intent, if_addr, force_freq,
  4536. persistent_group, ssid, pref_freq) < 0) {
  4537. if (wpa_s->create_p2p_iface)
  4538. wpas_p2p_remove_pending_group_interface(wpa_s);
  4539. return -1;
  4540. }
  4541. return ret;
  4542. }
  4543. /**
  4544. * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
  4545. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  4546. * @freq: Frequency of the channel in MHz
  4547. * @duration: Duration of the stay on the channel in milliseconds
  4548. *
  4549. * This callback is called when the driver indicates that it has started the
  4550. * requested remain-on-channel duration.
  4551. */
  4552. void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  4553. unsigned int freq, unsigned int duration)
  4554. {
  4555. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4556. return;
  4557. wpa_printf(MSG_DEBUG, "P2P: remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d roc_waiting_drv_freq=%d freq=%u duration=%u)",
  4558. wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
  4559. wpa_s->roc_waiting_drv_freq, freq, duration);
  4560. if (wpa_s->off_channel_freq &&
  4561. wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
  4562. p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
  4563. wpa_s->pending_listen_duration);
  4564. wpa_s->pending_listen_freq = 0;
  4565. } else {
  4566. wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
  4567. wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
  4568. freq, duration);
  4569. }
  4570. }
  4571. int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
  4572. {
  4573. /* Limit maximum Listen state time based on driver limitation. */
  4574. if (timeout > wpa_s->max_remain_on_chan)
  4575. timeout = wpa_s->max_remain_on_chan;
  4576. return p2p_listen(wpa_s->global->p2p, timeout);
  4577. }
  4578. /**
  4579. * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
  4580. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  4581. * @freq: Frequency of the channel in MHz
  4582. *
  4583. * This callback is called when the driver indicates that a remain-on-channel
  4584. * operation has been completed, i.e., the duration on the requested channel
  4585. * has timed out.
  4586. */
  4587. void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  4588. unsigned int freq)
  4589. {
  4590. wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
  4591. "(p2p_long_listen=%d ms pending_action_tx=%p)",
  4592. wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
  4593. wpas_p2p_listen_work_done(wpa_s);
  4594. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4595. return;
  4596. if (wpa_s->p2p_long_listen > 0)
  4597. wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
  4598. if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
  4599. return; /* P2P module started a new operation */
  4600. if (offchannel_pending_action_tx(wpa_s))
  4601. return;
  4602. if (wpa_s->p2p_long_listen > 0) {
  4603. wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
  4604. wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
  4605. } else {
  4606. /*
  4607. * When listen duration is over, stop listen & update p2p_state
  4608. * to IDLE.
  4609. */
  4610. p2p_stop_listen(wpa_s->global->p2p);
  4611. }
  4612. }
  4613. /**
  4614. * wpas_p2p_group_remove - Remove a P2P group
  4615. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  4616. * @ifname: Network interface name of the group interface or "*" to remove all
  4617. * groups
  4618. * Returns: 0 on success, -1 on failure
  4619. *
  4620. * This function is used to remove a P2P group. This can be used to disconnect
  4621. * from a group in which the local end is a P2P Client or to end a P2P Group in
  4622. * case the local end is the Group Owner. If a virtual network interface was
  4623. * created for this group, that interface will be removed. Otherwise, only the
  4624. * configured P2P group network will be removed from the interface.
  4625. */
  4626. int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
  4627. {
  4628. struct wpa_global *global = wpa_s->global;
  4629. struct wpa_supplicant *calling_wpa_s = wpa_s;
  4630. if (os_strcmp(ifname, "*") == 0) {
  4631. struct wpa_supplicant *prev;
  4632. wpa_s = global->ifaces;
  4633. while (wpa_s) {
  4634. prev = wpa_s;
  4635. wpa_s = wpa_s->next;
  4636. if (prev->p2p_group_interface !=
  4637. NOT_P2P_GROUP_INTERFACE ||
  4638. (prev->current_ssid &&
  4639. prev->current_ssid->p2p_group))
  4640. wpas_p2p_disconnect_safely(prev, calling_wpa_s);
  4641. }
  4642. return 0;
  4643. }
  4644. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  4645. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  4646. break;
  4647. }
  4648. return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
  4649. }
  4650. static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
  4651. {
  4652. unsigned int r;
  4653. if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
  4654. unsigned int i, size = P2P_MAX_PREF_CHANNELS;
  4655. unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS];
  4656. int res;
  4657. res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
  4658. &size, pref_freq_list);
  4659. if (!res && size > 0) {
  4660. i = 0;
  4661. while (wpas_p2p_disallowed_freq(wpa_s->global,
  4662. pref_freq_list[i]) &&
  4663. i < size) {
  4664. wpa_printf(MSG_DEBUG,
  4665. "P2P: preferred_freq_list[%d]=%d is disallowed",
  4666. i, pref_freq_list[i]);
  4667. i++;
  4668. }
  4669. if (i != size) {
  4670. freq = pref_freq_list[i];
  4671. wpa_printf(MSG_DEBUG,
  4672. "P2P: Using preferred_freq_list[%d]=%d",
  4673. i, freq);
  4674. } else {
  4675. wpa_printf(MSG_DEBUG,
  4676. "P2P: All driver preferred frequencies are disallowed for P2P use");
  4677. }
  4678. } else {
  4679. wpa_printf(MSG_DEBUG,
  4680. "P2P: No preferred frequency list available");
  4681. }
  4682. }
  4683. if (freq == 2) {
  4684. wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
  4685. "band");
  4686. if (wpa_s->best_24_freq > 0 &&
  4687. p2p_supported_freq_go(wpa_s->global->p2p,
  4688. wpa_s->best_24_freq)) {
  4689. freq = wpa_s->best_24_freq;
  4690. wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
  4691. "channel: %d MHz", freq);
  4692. } else {
  4693. if (os_get_random((u8 *) &r, sizeof(r)) < 0)
  4694. return -1;
  4695. freq = 2412 + (r % 3) * 25;
  4696. wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
  4697. "channel: %d MHz", freq);
  4698. }
  4699. }
  4700. if (freq == 5) {
  4701. wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
  4702. "band");
  4703. if (wpa_s->best_5_freq > 0 &&
  4704. p2p_supported_freq_go(wpa_s->global->p2p,
  4705. wpa_s->best_5_freq)) {
  4706. freq = wpa_s->best_5_freq;
  4707. wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
  4708. "channel: %d MHz", freq);
  4709. } else {
  4710. if (os_get_random((u8 *) &r, sizeof(r)) < 0)
  4711. return -1;
  4712. freq = 5180 + (r % 4) * 20;
  4713. if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
  4714. wpa_printf(MSG_DEBUG, "P2P: Could not select "
  4715. "5 GHz channel for P2P group");
  4716. return -1;
  4717. }
  4718. wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
  4719. "channel: %d MHz", freq);
  4720. }
  4721. }
  4722. if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
  4723. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
  4724. ieee80211_is_dfs(freq)) {
  4725. /*
  4726. * If freq is a DFS channel and DFS is offloaded to the
  4727. * driver, allow P2P GO to use it.
  4728. */
  4729. wpa_printf(MSG_DEBUG, "P2P: "
  4730. "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
  4731. __func__, freq);
  4732. return freq;
  4733. }
  4734. wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
  4735. "(%u MHz) is not supported for P2P uses",
  4736. freq);
  4737. return -1;
  4738. }
  4739. return freq;
  4740. }
  4741. static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
  4742. const struct p2p_channels *channels,
  4743. int freq)
  4744. {
  4745. if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
  4746. p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
  4747. freq_included(wpa_s, channels, freq))
  4748. return 1;
  4749. return 0;
  4750. }
  4751. static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
  4752. struct p2p_go_neg_results *params,
  4753. const struct p2p_channels *channels)
  4754. {
  4755. unsigned int i, r;
  4756. /* first try some random selection of the social channels */
  4757. if (os_get_random((u8 *) &r, sizeof(r)) < 0)
  4758. return;
  4759. for (i = 0; i < 3; i++) {
  4760. params->freq = 2412 + ((r + i) % 3) * 25;
  4761. if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
  4762. goto out;
  4763. }
  4764. /* try all other channels in operating class 81 */
  4765. for (i = 0; i < 11; i++) {
  4766. params->freq = 2412 + i * 5;
  4767. /* skip social channels; covered in the previous loop */
  4768. if (params->freq == 2412 ||
  4769. params->freq == 2437 ||
  4770. params->freq == 2462)
  4771. continue;
  4772. if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
  4773. goto out;
  4774. }
  4775. /* try all channels in operating class 115 */
  4776. for (i = 0; i < 4; i++) {
  4777. params->freq = 5180 + i * 20;
  4778. if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
  4779. freq_included(wpa_s, channels, params->freq) &&
  4780. p2p_supported_freq(wpa_s->global->p2p, params->freq))
  4781. goto out;
  4782. }
  4783. /* try all channels in operating class 124 */
  4784. for (i = 0; i < 4; i++) {
  4785. params->freq = 5745 + i * 20;
  4786. if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
  4787. freq_included(wpa_s, channels, params->freq) &&
  4788. p2p_supported_freq(wpa_s->global->p2p, params->freq))
  4789. goto out;
  4790. }
  4791. /* try social channel class 180 channel 2 */
  4792. params->freq = 58320 + 1 * 2160;
  4793. if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
  4794. freq_included(wpa_s, channels, params->freq) &&
  4795. p2p_supported_freq(wpa_s->global->p2p, params->freq))
  4796. goto out;
  4797. /* try all channels in reg. class 180 */
  4798. for (i = 0; i < 4; i++) {
  4799. params->freq = 58320 + i * 2160;
  4800. if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
  4801. freq_included(wpa_s, channels, params->freq) &&
  4802. p2p_supported_freq(wpa_s->global->p2p, params->freq))
  4803. goto out;
  4804. }
  4805. params->freq = 0;
  4806. wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
  4807. return;
  4808. out:
  4809. wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
  4810. params->freq);
  4811. }
  4812. static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
  4813. struct p2p_go_neg_results *params,
  4814. int freq, int ht40, int vht,
  4815. const struct p2p_channels *channels)
  4816. {
  4817. struct wpa_used_freq_data *freqs;
  4818. unsigned int cand;
  4819. unsigned int num, i;
  4820. int ignore_no_freqs = 0;
  4821. os_memset(params, 0, sizeof(*params));
  4822. params->role_go = 1;
  4823. params->ht40 = ht40;
  4824. params->vht = vht;
  4825. freqs = os_calloc(wpa_s->num_multichan_concurrent,
  4826. sizeof(struct wpa_used_freq_data));
  4827. if (!freqs)
  4828. return -1;
  4829. num = get_shared_radio_freqs_data(wpa_s, freqs,
  4830. wpa_s->num_multichan_concurrent);
  4831. if (wpa_s->current_ssid &&
  4832. wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
  4833. wpa_s->wpa_state == WPA_COMPLETED) {
  4834. wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
  4835. __func__);
  4836. /*
  4837. * If the frequency selection is done for an active P2P GO that
  4838. * is not sharing a frequency, allow to select a new frequency
  4839. * even if there are no unused frequencies as we are about to
  4840. * move the P2P GO so its frequency can be re-used.
  4841. */
  4842. for (i = 0; i < num; i++) {
  4843. if (freqs[i].freq == wpa_s->current_ssid->frequency &&
  4844. freqs[i].flags == 0) {
  4845. ignore_no_freqs = 1;
  4846. break;
  4847. }
  4848. }
  4849. }
  4850. /* try using the forced freq */
  4851. if (freq) {
  4852. if (!wpas_p2p_supported_freq_go(wpa_s, channels, freq)) {
  4853. wpa_printf(MSG_DEBUG,
  4854. "P2P: Forced GO freq %d MHz not accepted",
  4855. freq);
  4856. goto fail;
  4857. }
  4858. for (i = 0; i < num; i++) {
  4859. if (freqs[i].freq == freq) {
  4860. wpa_printf(MSG_DEBUG,
  4861. "P2P: forced freq (%d MHz) is also shared",
  4862. freq);
  4863. params->freq = freq;
  4864. goto success;
  4865. }
  4866. }
  4867. if (!ignore_no_freqs &&
  4868. wpas_p2p_num_unused_channels(wpa_s) <= 0) {
  4869. wpa_printf(MSG_DEBUG,
  4870. "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
  4871. freq);
  4872. goto fail;
  4873. }
  4874. wpa_printf(MSG_DEBUG,
  4875. "P2P: force GO freq (%d MHz) on a free channel",
  4876. freq);
  4877. params->freq = freq;
  4878. goto success;
  4879. }
  4880. /* consider using one of the shared frequencies */
  4881. if (num) {
  4882. cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
  4883. if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
  4884. wpa_printf(MSG_DEBUG,
  4885. "P2P: Use shared freq (%d MHz) for GO",
  4886. freq);
  4887. params->freq = cand;
  4888. goto success;
  4889. }
  4890. /* try using one of the shared freqs */
  4891. for (i = 0; i < num; i++) {
  4892. if (wpas_p2p_supported_freq_go(wpa_s, channels,
  4893. freqs[i].freq)) {
  4894. wpa_printf(MSG_DEBUG,
  4895. "P2P: Use shared freq (%d MHz) for GO",
  4896. freq);
  4897. params->freq = freqs[i].freq;
  4898. goto success;
  4899. }
  4900. }
  4901. }
  4902. if (!ignore_no_freqs &&
  4903. wpas_p2p_num_unused_channels(wpa_s) <= 0) {
  4904. wpa_printf(MSG_DEBUG,
  4905. "P2P: Cannot force GO on any of the channels we are already using");
  4906. goto fail;
  4907. }
  4908. /* try using the setting from the configuration file */
  4909. if (wpa_s->conf->p2p_oper_reg_class == 81 &&
  4910. wpa_s->conf->p2p_oper_channel >= 1 &&
  4911. wpa_s->conf->p2p_oper_channel <= 11 &&
  4912. wpas_p2p_supported_freq_go(
  4913. wpa_s, channels,
  4914. 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
  4915. params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
  4916. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
  4917. "frequency %d MHz", params->freq);
  4918. goto success;
  4919. }
  4920. if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
  4921. wpa_s->conf->p2p_oper_reg_class == 116 ||
  4922. wpa_s->conf->p2p_oper_reg_class == 117 ||
  4923. wpa_s->conf->p2p_oper_reg_class == 124 ||
  4924. wpa_s->conf->p2p_oper_reg_class == 125 ||
  4925. wpa_s->conf->p2p_oper_reg_class == 126 ||
  4926. wpa_s->conf->p2p_oper_reg_class == 127) &&
  4927. wpas_p2p_supported_freq_go(wpa_s, channels,
  4928. 5000 +
  4929. 5 * wpa_s->conf->p2p_oper_channel)) {
  4930. params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
  4931. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
  4932. "frequency %d MHz", params->freq);
  4933. goto success;
  4934. }
  4935. /* Try using best channels */
  4936. if (wpa_s->conf->p2p_oper_channel == 0 &&
  4937. wpa_s->best_overall_freq > 0 &&
  4938. wpas_p2p_supported_freq_go(wpa_s, channels,
  4939. wpa_s->best_overall_freq)) {
  4940. params->freq = wpa_s->best_overall_freq;
  4941. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
  4942. "channel %d MHz", params->freq);
  4943. goto success;
  4944. }
  4945. if (wpa_s->conf->p2p_oper_channel == 0 &&
  4946. wpa_s->best_24_freq > 0 &&
  4947. wpas_p2p_supported_freq_go(wpa_s, channels,
  4948. wpa_s->best_24_freq)) {
  4949. params->freq = wpa_s->best_24_freq;
  4950. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
  4951. "channel %d MHz", params->freq);
  4952. goto success;
  4953. }
  4954. if (wpa_s->conf->p2p_oper_channel == 0 &&
  4955. wpa_s->best_5_freq > 0 &&
  4956. wpas_p2p_supported_freq_go(wpa_s, channels,
  4957. wpa_s->best_5_freq)) {
  4958. params->freq = wpa_s->best_5_freq;
  4959. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
  4960. "channel %d MHz", params->freq);
  4961. goto success;
  4962. }
  4963. /* try using preferred channels */
  4964. cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
  4965. if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
  4966. params->freq = cand;
  4967. wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
  4968. "channels", params->freq);
  4969. goto success;
  4970. }
  4971. /* Try using one of the group common freqs */
  4972. if (wpa_s->p2p_group_common_freqs) {
  4973. for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
  4974. cand = wpa_s->p2p_group_common_freqs[i];
  4975. if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
  4976. params->freq = cand;
  4977. wpa_printf(MSG_DEBUG,
  4978. "P2P: Use freq %d MHz common with the peer",
  4979. params->freq);
  4980. goto success;
  4981. }
  4982. }
  4983. }
  4984. /* no preference, select some channel */
  4985. wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
  4986. if (params->freq == 0) {
  4987. wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
  4988. goto fail;
  4989. }
  4990. success:
  4991. os_free(freqs);
  4992. return 0;
  4993. fail:
  4994. os_free(freqs);
  4995. return -1;
  4996. }
  4997. static struct wpa_supplicant *
  4998. wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
  4999. int go)
  5000. {
  5001. struct wpa_supplicant *group_wpa_s;
  5002. if (!wpas_p2p_create_iface(wpa_s)) {
  5003. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
  5004. "operations");
  5005. wpa_s->p2p_first_connection_timeout = 0;
  5006. return wpa_s;
  5007. }
  5008. if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
  5009. WPA_IF_P2P_CLIENT) < 0) {
  5010. wpa_msg_global(wpa_s, MSG_ERROR,
  5011. "P2P: Failed to add group interface");
  5012. return NULL;
  5013. }
  5014. group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
  5015. if (group_wpa_s == NULL) {
  5016. wpa_msg_global(wpa_s, MSG_ERROR,
  5017. "P2P: Failed to initialize group interface");
  5018. wpas_p2p_remove_pending_group_interface(wpa_s);
  5019. return NULL;
  5020. }
  5021. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
  5022. group_wpa_s->ifname);
  5023. group_wpa_s->p2p_first_connection_timeout = 0;
  5024. return group_wpa_s;
  5025. }
  5026. /**
  5027. * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
  5028. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  5029. * @persistent_group: Whether to create a persistent group
  5030. * @freq: Frequency for the group or 0 to indicate no hardcoding
  5031. * @ht40: Start GO with 40 MHz channel width
  5032. * @vht: Start GO with VHT support
  5033. * Returns: 0 on success, -1 on failure
  5034. *
  5035. * This function creates a new P2P group with the local end as the Group Owner,
  5036. * i.e., without using Group Owner Negotiation.
  5037. */
  5038. int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
  5039. int freq, int ht40, int vht)
  5040. {
  5041. struct p2p_go_neg_results params;
  5042. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5043. return -1;
  5044. os_free(wpa_s->global->add_psk);
  5045. wpa_s->global->add_psk = NULL;
  5046. /* Make sure we are not running find during connection establishment */
  5047. wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
  5048. wpas_p2p_stop_find_oper(wpa_s);
  5049. freq = wpas_p2p_select_go_freq(wpa_s, freq);
  5050. if (freq < 0)
  5051. return -1;
  5052. if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, NULL))
  5053. return -1;
  5054. if (params.freq &&
  5055. !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
  5056. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
  5057. ieee80211_is_dfs(params.freq)) {
  5058. /*
  5059. * If freq is a DFS channel and DFS is offloaded to the
  5060. * driver, allow P2P GO to use it.
  5061. */
  5062. wpa_printf(MSG_DEBUG,
  5063. "P2P: %s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to driver",
  5064. __func__, params.freq);
  5065. } else {
  5066. wpa_printf(MSG_DEBUG,
  5067. "P2P: The selected channel for GO (%u MHz) is not supported for P2P uses",
  5068. params.freq);
  5069. return -1;
  5070. }
  5071. }
  5072. p2p_go_params(wpa_s->global->p2p, &params);
  5073. params.persistent_group = persistent_group;
  5074. wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
  5075. if (wpa_s == NULL)
  5076. return -1;
  5077. wpas_start_wps_go(wpa_s, &params, 0);
  5078. return 0;
  5079. }
  5080. static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
  5081. struct wpa_ssid *params, int addr_allocated,
  5082. int freq, int force_scan)
  5083. {
  5084. struct wpa_ssid *ssid;
  5085. wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
  5086. if (wpa_s == NULL)
  5087. return -1;
  5088. if (force_scan)
  5089. os_get_reltime(&wpa_s->scan_min_time);
  5090. wpa_s->p2p_last_4way_hs_fail = NULL;
  5091. wpa_supplicant_ap_deinit(wpa_s);
  5092. ssid = wpa_config_add_network(wpa_s->conf);
  5093. if (ssid == NULL)
  5094. return -1;
  5095. os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
  5096. wpa_config_set_network_defaults(ssid);
  5097. ssid->temporary = 1;
  5098. ssid->proto = WPA_PROTO_RSN;
  5099. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  5100. ssid->group_cipher = WPA_CIPHER_CCMP;
  5101. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  5102. ssid->ssid = os_malloc(params->ssid_len);
  5103. if (ssid->ssid == NULL) {
  5104. wpa_config_remove_network(wpa_s->conf, ssid->id);
  5105. return -1;
  5106. }
  5107. os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
  5108. ssid->ssid_len = params->ssid_len;
  5109. ssid->p2p_group = 1;
  5110. ssid->export_keys = 1;
  5111. if (params->psk_set) {
  5112. os_memcpy(ssid->psk, params->psk, 32);
  5113. ssid->psk_set = 1;
  5114. }
  5115. if (params->passphrase)
  5116. ssid->passphrase = os_strdup(params->passphrase);
  5117. wpa_s->show_group_started = 1;
  5118. wpa_s->p2p_in_invitation = 1;
  5119. wpa_s->p2p_invite_go_freq = freq;
  5120. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
  5121. NULL);
  5122. eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
  5123. wpas_p2p_group_formation_timeout,
  5124. wpa_s->parent, NULL);
  5125. wpa_supplicant_select_network(wpa_s, ssid);
  5126. return 0;
  5127. }
  5128. int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
  5129. struct wpa_ssid *ssid, int addr_allocated,
  5130. int force_freq, int neg_freq, int ht40,
  5131. int vht, const struct p2p_channels *channels,
  5132. int connection_timeout, int force_scan)
  5133. {
  5134. struct p2p_go_neg_results params;
  5135. int go = 0, freq;
  5136. if (ssid->disabled != 2 || ssid->ssid == NULL)
  5137. return -1;
  5138. if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
  5139. go == (ssid->mode == WPAS_MODE_P2P_GO)) {
  5140. wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
  5141. "already running");
  5142. if (go == 0 &&
  5143. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  5144. wpa_s->parent, NULL)) {
  5145. /*
  5146. * This can happen if Invitation Response frame was lost
  5147. * and the peer (GO of a persistent group) tries to
  5148. * invite us again. Reschedule the timeout to avoid
  5149. * terminating the wait for the connection too early
  5150. * since we now know that the peer is still trying to
  5151. * invite us instead of having already started the GO.
  5152. */
  5153. wpa_printf(MSG_DEBUG,
  5154. "P2P: Reschedule group formation timeout since peer is still trying to invite us");
  5155. eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
  5156. wpas_p2p_group_formation_timeout,
  5157. wpa_s->parent, NULL);
  5158. }
  5159. return 0;
  5160. }
  5161. os_free(wpa_s->global->add_psk);
  5162. wpa_s->global->add_psk = NULL;
  5163. /* Make sure we are not running find during connection establishment */
  5164. wpas_p2p_stop_find_oper(wpa_s);
  5165. wpa_s->p2p_fallback_to_go_neg = 0;
  5166. if (ssid->mode == WPAS_MODE_P2P_GO) {
  5167. if (force_freq > 0) {
  5168. freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
  5169. if (freq < 0)
  5170. return -1;
  5171. } else {
  5172. freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
  5173. if (freq < 0 ||
  5174. (freq > 0 && !freq_included(wpa_s, channels, freq)))
  5175. freq = 0;
  5176. }
  5177. } else if (ssid->mode == WPAS_MODE_INFRA) {
  5178. freq = neg_freq;
  5179. if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
  5180. struct os_reltime now;
  5181. struct wpa_bss *bss =
  5182. wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
  5183. os_get_reltime(&now);
  5184. if (bss &&
  5185. !os_reltime_expired(&now, &bss->last_update, 5) &&
  5186. freq_included(wpa_s, channels, bss->freq))
  5187. freq = bss->freq;
  5188. else
  5189. freq = 0;
  5190. }
  5191. return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
  5192. force_scan);
  5193. } else {
  5194. return -1;
  5195. }
  5196. if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, channels))
  5197. return -1;
  5198. params.role_go = 1;
  5199. params.psk_set = ssid->psk_set;
  5200. if (params.psk_set)
  5201. os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
  5202. if (ssid->passphrase) {
  5203. if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
  5204. wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
  5205. "persistent group");
  5206. return -1;
  5207. }
  5208. os_strlcpy(params.passphrase, ssid->passphrase,
  5209. sizeof(params.passphrase));
  5210. }
  5211. os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
  5212. params.ssid_len = ssid->ssid_len;
  5213. params.persistent_group = 1;
  5214. wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
  5215. if (wpa_s == NULL)
  5216. return -1;
  5217. p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
  5218. wpa_s->p2p_first_connection_timeout = connection_timeout;
  5219. wpas_start_wps_go(wpa_s, &params, 0);
  5220. return 0;
  5221. }
  5222. static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
  5223. struct wpabuf *proberesp_ies)
  5224. {
  5225. struct wpa_supplicant *wpa_s = ctx;
  5226. if (wpa_s->ap_iface) {
  5227. struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
  5228. if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
  5229. wpabuf_free(beacon_ies);
  5230. wpabuf_free(proberesp_ies);
  5231. return;
  5232. }
  5233. if (beacon_ies) {
  5234. wpabuf_free(hapd->p2p_beacon_ie);
  5235. hapd->p2p_beacon_ie = beacon_ies;
  5236. }
  5237. wpabuf_free(hapd->p2p_probe_resp_ie);
  5238. hapd->p2p_probe_resp_ie = proberesp_ies;
  5239. } else {
  5240. wpabuf_free(beacon_ies);
  5241. wpabuf_free(proberesp_ies);
  5242. }
  5243. wpa_supplicant_ap_update_beacon(wpa_s);
  5244. }
  5245. static void wpas_p2p_idle_update(void *ctx, int idle)
  5246. {
  5247. struct wpa_supplicant *wpa_s = ctx;
  5248. if (!wpa_s->ap_iface)
  5249. return;
  5250. wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
  5251. if (idle) {
  5252. if (wpa_s->global->p2p_fail_on_wps_complete &&
  5253. wpa_s->p2p_in_provisioning) {
  5254. wpas_p2p_grpform_fail_after_wps(wpa_s);
  5255. return;
  5256. }
  5257. wpas_p2p_set_group_idle_timeout(wpa_s);
  5258. } else
  5259. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  5260. }
  5261. struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
  5262. struct wpa_ssid *ssid)
  5263. {
  5264. struct p2p_group *group;
  5265. struct p2p_group_config *cfg;
  5266. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5267. return NULL;
  5268. cfg = os_zalloc(sizeof(*cfg));
  5269. if (cfg == NULL)
  5270. return NULL;
  5271. if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
  5272. cfg->persistent_group = 2;
  5273. else if (ssid->p2p_persistent_group)
  5274. cfg->persistent_group = 1;
  5275. os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
  5276. if (wpa_s->max_stations &&
  5277. wpa_s->max_stations < wpa_s->conf->max_num_sta)
  5278. cfg->max_clients = wpa_s->max_stations;
  5279. else
  5280. cfg->max_clients = wpa_s->conf->max_num_sta;
  5281. os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
  5282. cfg->ssid_len = ssid->ssid_len;
  5283. cfg->freq = ssid->frequency;
  5284. cfg->cb_ctx = wpa_s;
  5285. cfg->ie_update = wpas_p2p_ie_update;
  5286. cfg->idle_update = wpas_p2p_idle_update;
  5287. group = p2p_group_init(wpa_s->global->p2p, cfg);
  5288. if (group == NULL)
  5289. os_free(cfg);
  5290. if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
  5291. p2p_group_notif_formation_done(group);
  5292. wpa_s->p2p_group = group;
  5293. return group;
  5294. }
  5295. void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  5296. int registrar)
  5297. {
  5298. struct wpa_ssid *ssid = wpa_s->current_ssid;
  5299. if (!wpa_s->p2p_in_provisioning) {
  5300. wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
  5301. "provisioning not in progress");
  5302. return;
  5303. }
  5304. if (ssid && ssid->mode == WPAS_MODE_INFRA) {
  5305. u8 go_dev_addr[ETH_ALEN];
  5306. os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
  5307. wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
  5308. ssid->ssid_len);
  5309. /* Clear any stored provisioning info */
  5310. p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
  5311. }
  5312. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
  5313. NULL);
  5314. wpa_s->p2p_go_group_formation_completed = 1;
  5315. if (ssid && ssid->mode == WPAS_MODE_INFRA) {
  5316. /*
  5317. * Use a separate timeout for initial data connection to
  5318. * complete to allow the group to be removed automatically if
  5319. * something goes wrong in this step before the P2P group idle
  5320. * timeout mechanism is taken into use.
  5321. */
  5322. wpa_dbg(wpa_s, MSG_DEBUG,
  5323. "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
  5324. P2P_MAX_INITIAL_CONN_WAIT);
  5325. eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
  5326. wpas_p2p_group_formation_timeout,
  5327. wpa_s->parent, NULL);
  5328. } else if (ssid) {
  5329. /*
  5330. * Use a separate timeout for initial data connection to
  5331. * complete to allow the group to be removed automatically if
  5332. * the client does not complete data connection successfully.
  5333. */
  5334. wpa_dbg(wpa_s, MSG_DEBUG,
  5335. "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
  5336. P2P_MAX_INITIAL_CONN_WAIT_GO);
  5337. eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
  5338. wpas_p2p_group_formation_timeout,
  5339. wpa_s->parent, NULL);
  5340. /*
  5341. * Complete group formation on first successful data connection
  5342. */
  5343. wpa_s->p2p_go_group_formation_completed = 0;
  5344. }
  5345. if (wpa_s->global->p2p)
  5346. p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
  5347. wpas_group_formation_completed(wpa_s, 1, 0);
  5348. }
  5349. void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
  5350. struct wps_event_fail *fail)
  5351. {
  5352. if (!wpa_s->p2p_in_provisioning) {
  5353. wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
  5354. "provisioning not in progress");
  5355. return;
  5356. }
  5357. if (wpa_s->go_params) {
  5358. p2p_clear_provisioning_info(
  5359. wpa_s->global->p2p,
  5360. wpa_s->go_params->peer_device_addr);
  5361. }
  5362. wpas_notify_p2p_wps_failed(wpa_s, fail);
  5363. if (wpa_s == wpa_s->global->p2p_group_formation) {
  5364. /*
  5365. * Allow some time for the failed WPS negotiation exchange to
  5366. * complete, but remove the group since group formation cannot
  5367. * succeed after provisioning failure.
  5368. */
  5369. wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
  5370. wpa_s->global->p2p_fail_on_wps_complete = 1;
  5371. eloop_deplete_timeout(0, 50000,
  5372. wpas_p2p_group_formation_timeout,
  5373. wpa_s->parent, NULL);
  5374. }
  5375. }
  5376. int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
  5377. {
  5378. if (!wpa_s->global->p2p_fail_on_wps_complete ||
  5379. !wpa_s->p2p_in_provisioning)
  5380. return 0;
  5381. wpas_p2p_grpform_fail_after_wps(wpa_s);
  5382. return 1;
  5383. }
  5384. int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  5385. const char *config_method,
  5386. enum wpas_p2p_prov_disc_use use,
  5387. struct p2ps_provision *p2ps_prov)
  5388. {
  5389. u16 config_methods;
  5390. wpa_s->global->pending_p2ps_group = 0;
  5391. wpa_s->global->pending_p2ps_group_freq = 0;
  5392. wpa_s->p2p_fallback_to_go_neg = 0;
  5393. wpa_s->pending_pd_use = NORMAL_PD;
  5394. if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
  5395. p2ps_prov->conncap = p2ps_group_capability(
  5396. wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
  5397. &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
  5398. wpa_printf(MSG_DEBUG,
  5399. "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
  5400. __func__, p2ps_prov->conncap,
  5401. p2ps_prov->adv_id, p2ps_prov->conncap,
  5402. p2ps_prov->status, p2ps_prov->info);
  5403. config_methods = 0;
  5404. } else if (os_strncmp(config_method, "display", 7) == 0)
  5405. config_methods = WPS_CONFIG_DISPLAY;
  5406. else if (os_strncmp(config_method, "keypad", 6) == 0)
  5407. config_methods = WPS_CONFIG_KEYPAD;
  5408. else if (os_strncmp(config_method, "pbc", 3) == 0 ||
  5409. os_strncmp(config_method, "pushbutton", 10) == 0)
  5410. config_methods = WPS_CONFIG_PUSHBUTTON;
  5411. else {
  5412. wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
  5413. os_free(p2ps_prov);
  5414. return -1;
  5415. }
  5416. if (use == WPAS_P2P_PD_AUTO) {
  5417. os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
  5418. wpa_s->pending_pd_config_methods = config_methods;
  5419. wpa_s->p2p_auto_pd = 1;
  5420. wpa_s->p2p_auto_join = 0;
  5421. wpa_s->pending_pd_before_join = 0;
  5422. wpa_s->auto_pd_scan_retry = 0;
  5423. wpas_p2p_stop_find(wpa_s);
  5424. wpa_s->p2p_join_scan_count = 0;
  5425. os_get_reltime(&wpa_s->p2p_auto_started);
  5426. wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
  5427. wpa_s->p2p_auto_started.sec,
  5428. wpa_s->p2p_auto_started.usec);
  5429. wpas_p2p_join_scan(wpa_s, NULL);
  5430. return 0;
  5431. }
  5432. if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
  5433. os_free(p2ps_prov);
  5434. return -1;
  5435. }
  5436. return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
  5437. config_methods, use == WPAS_P2P_PD_FOR_JOIN,
  5438. 0, 1);
  5439. }
  5440. int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
  5441. char *end)
  5442. {
  5443. return p2p_scan_result_text(ies, ies_len, buf, end);
  5444. }
  5445. static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
  5446. {
  5447. if (!offchannel_pending_action_tx(wpa_s))
  5448. return;
  5449. wpas_p2p_action_tx_clear(wpa_s);
  5450. wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
  5451. "operation request");
  5452. offchannel_clear_pending_action_tx(wpa_s);
  5453. }
  5454. int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
  5455. enum p2p_discovery_type type,
  5456. unsigned int num_req_dev_types, const u8 *req_dev_types,
  5457. const u8 *dev_id, unsigned int search_delay,
  5458. u8 seek_cnt, const char **seek_string, int freq)
  5459. {
  5460. wpas_p2p_clear_pending_action_tx(wpa_s);
  5461. wpa_s->p2p_long_listen = 0;
  5462. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
  5463. wpa_s->p2p_in_provisioning)
  5464. return -1;
  5465. wpa_supplicant_cancel_sched_scan(wpa_s);
  5466. return p2p_find(wpa_s->global->p2p, timeout, type,
  5467. num_req_dev_types, req_dev_types, dev_id,
  5468. search_delay, seek_cnt, seek_string, freq);
  5469. }
  5470. static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
  5471. struct wpa_scan_results *scan_res)
  5472. {
  5473. wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
  5474. if (wpa_s->p2p_scan_work) {
  5475. struct wpa_radio_work *work = wpa_s->p2p_scan_work;
  5476. wpa_s->p2p_scan_work = NULL;
  5477. radio_work_done(work);
  5478. }
  5479. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5480. return;
  5481. /*
  5482. * Indicate that results have been processed so that the P2P module can
  5483. * continue pending tasks.
  5484. */
  5485. p2p_scan_res_handled(wpa_s->global->p2p);
  5486. }
  5487. static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
  5488. {
  5489. wpas_p2p_clear_pending_action_tx(wpa_s);
  5490. wpa_s->p2p_long_listen = 0;
  5491. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  5492. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  5493. if (wpa_s->global->p2p)
  5494. p2p_stop_find(wpa_s->global->p2p);
  5495. if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
  5496. wpa_printf(MSG_DEBUG,
  5497. "P2P: Do not consider the scan results after stop_find");
  5498. wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
  5499. }
  5500. }
  5501. void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
  5502. {
  5503. wpas_p2p_stop_find_oper(wpa_s);
  5504. if (!wpa_s->global->pending_group_iface_for_p2ps)
  5505. wpas_p2p_remove_pending_group_interface(wpa_s);
  5506. }
  5507. static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
  5508. {
  5509. struct wpa_supplicant *wpa_s = eloop_ctx;
  5510. wpa_s->p2p_long_listen = 0;
  5511. }
  5512. int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
  5513. {
  5514. int res;
  5515. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5516. return -1;
  5517. wpa_supplicant_cancel_sched_scan(wpa_s);
  5518. wpas_p2p_clear_pending_action_tx(wpa_s);
  5519. if (timeout == 0) {
  5520. /*
  5521. * This is a request for unlimited Listen state. However, at
  5522. * least for now, this is mapped to a Listen state for one
  5523. * hour.
  5524. */
  5525. timeout = 3600;
  5526. }
  5527. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  5528. wpa_s->p2p_long_listen = 0;
  5529. /*
  5530. * Stop previous find/listen operation to avoid trying to request a new
  5531. * remain-on-channel operation while the driver is still running the
  5532. * previous one.
  5533. */
  5534. if (wpa_s->global->p2p)
  5535. p2p_stop_find(wpa_s->global->p2p);
  5536. res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
  5537. if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
  5538. wpa_s->p2p_long_listen = timeout * 1000;
  5539. eloop_register_timeout(timeout, 0,
  5540. wpas_p2p_long_listen_timeout,
  5541. wpa_s, NULL);
  5542. }
  5543. return res;
  5544. }
  5545. int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  5546. u8 *buf, size_t len, int p2p_group)
  5547. {
  5548. struct wpabuf *p2p_ie;
  5549. int ret;
  5550. if (wpa_s->global->p2p_disabled)
  5551. return -1;
  5552. /*
  5553. * Advertize mandatory cross connection capability even on
  5554. * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
  5555. */
  5556. if (wpa_s->conf->p2p_disabled && p2p_group)
  5557. return -1;
  5558. if (wpa_s->global->p2p == NULL)
  5559. return -1;
  5560. if (bss == NULL)
  5561. return -1;
  5562. p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  5563. ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
  5564. p2p_group, p2p_ie);
  5565. wpabuf_free(p2p_ie);
  5566. return ret;
  5567. }
  5568. int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
  5569. const u8 *dst, const u8 *bssid,
  5570. const u8 *ie, size_t ie_len,
  5571. unsigned int rx_freq, int ssi_signal)
  5572. {
  5573. if (wpa_s->global->p2p_disabled)
  5574. return 0;
  5575. if (wpa_s->global->p2p == NULL)
  5576. return 0;
  5577. switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
  5578. ie, ie_len, rx_freq)) {
  5579. case P2P_PREQ_NOT_P2P:
  5580. wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
  5581. ssi_signal);
  5582. /* fall through */
  5583. case P2P_PREQ_MALFORMED:
  5584. case P2P_PREQ_NOT_LISTEN:
  5585. case P2P_PREQ_NOT_PROCESSED:
  5586. default: /* make gcc happy */
  5587. return 0;
  5588. case P2P_PREQ_PROCESSED:
  5589. return 1;
  5590. }
  5591. }
  5592. void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
  5593. const u8 *sa, const u8 *bssid,
  5594. u8 category, const u8 *data, size_t len, int freq)
  5595. {
  5596. if (wpa_s->global->p2p_disabled)
  5597. return;
  5598. if (wpa_s->global->p2p == NULL)
  5599. return;
  5600. p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
  5601. freq);
  5602. }
  5603. void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
  5604. {
  5605. if (wpa_s->global->p2p_disabled)
  5606. return;
  5607. if (wpa_s->global->p2p == NULL)
  5608. return;
  5609. p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
  5610. }
  5611. static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
  5612. {
  5613. p2p_group_deinit(wpa_s->p2p_group);
  5614. wpa_s->p2p_group = NULL;
  5615. wpa_s->ap_configured_cb = NULL;
  5616. wpa_s->ap_configured_cb_ctx = NULL;
  5617. wpa_s->ap_configured_cb_data = NULL;
  5618. wpa_s->connect_without_scan = NULL;
  5619. }
  5620. int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
  5621. {
  5622. wpa_s->p2p_long_listen = 0;
  5623. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5624. return -1;
  5625. return p2p_reject(wpa_s->global->p2p, addr);
  5626. }
  5627. /* Invite to reinvoke a persistent group */
  5628. int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  5629. struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
  5630. int ht40, int vht, int pref_freq)
  5631. {
  5632. enum p2p_invite_role role;
  5633. u8 *bssid = NULL;
  5634. int force_freq = 0;
  5635. int res;
  5636. int no_pref_freq_given = pref_freq == 0;
  5637. unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
  5638. wpa_s->global->p2p_invite_group = NULL;
  5639. if (peer_addr)
  5640. os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
  5641. else
  5642. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  5643. wpa_s->p2p_persistent_go_freq = freq;
  5644. wpa_s->p2p_go_ht40 = !!ht40;
  5645. if (ssid->mode == WPAS_MODE_P2P_GO) {
  5646. role = P2P_INVITE_ROLE_GO;
  5647. if (peer_addr == NULL) {
  5648. wpa_printf(MSG_DEBUG, "P2P: Missing peer "
  5649. "address in invitation command");
  5650. return -1;
  5651. }
  5652. if (wpas_p2p_create_iface(wpa_s)) {
  5653. if (wpas_p2p_add_group_interface(wpa_s,
  5654. WPA_IF_P2P_GO) < 0) {
  5655. wpa_printf(MSG_ERROR, "P2P: Failed to "
  5656. "allocate a new interface for the "
  5657. "group");
  5658. return -1;
  5659. }
  5660. bssid = wpa_s->pending_interface_addr;
  5661. } else
  5662. bssid = wpa_s->own_addr;
  5663. } else {
  5664. role = P2P_INVITE_ROLE_CLIENT;
  5665. peer_addr = ssid->bssid;
  5666. }
  5667. wpa_s->pending_invite_ssid_id = ssid->id;
  5668. size = P2P_MAX_PREF_CHANNELS;
  5669. res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
  5670. role == P2P_INVITE_ROLE_GO,
  5671. pref_freq_list, &size);
  5672. if (res)
  5673. return res;
  5674. p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
  5675. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5676. return -1;
  5677. if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
  5678. no_pref_freq_given && pref_freq > 0 &&
  5679. wpa_s->num_multichan_concurrent > 1 &&
  5680. wpas_p2p_num_unused_channels(wpa_s) > 0) {
  5681. wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
  5682. pref_freq);
  5683. pref_freq = 0;
  5684. }
  5685. /*
  5686. * Stop any find/listen operations before invitation and possibly
  5687. * connection establishment.
  5688. */
  5689. wpas_p2p_stop_find_oper(wpa_s);
  5690. return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
  5691. ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
  5692. 1, pref_freq, -1);
  5693. }
  5694. /* Invite to join an active group */
  5695. int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
  5696. const u8 *peer_addr, const u8 *go_dev_addr)
  5697. {
  5698. struct wpa_global *global = wpa_s->global;
  5699. enum p2p_invite_role role;
  5700. u8 *bssid = NULL;
  5701. struct wpa_ssid *ssid;
  5702. int persistent;
  5703. int freq = 0, force_freq = 0, pref_freq = 0;
  5704. int res;
  5705. unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
  5706. wpa_s->p2p_persistent_go_freq = 0;
  5707. wpa_s->p2p_go_ht40 = 0;
  5708. wpa_s->p2p_go_vht = 0;
  5709. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  5710. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  5711. break;
  5712. }
  5713. if (wpa_s == NULL) {
  5714. wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
  5715. return -1;
  5716. }
  5717. ssid = wpa_s->current_ssid;
  5718. if (ssid == NULL) {
  5719. wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
  5720. "invitation");
  5721. return -1;
  5722. }
  5723. wpa_s->global->p2p_invite_group = wpa_s;
  5724. persistent = ssid->p2p_persistent_group &&
  5725. wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
  5726. ssid->ssid, ssid->ssid_len);
  5727. if (ssid->mode == WPAS_MODE_P2P_GO) {
  5728. role = P2P_INVITE_ROLE_ACTIVE_GO;
  5729. bssid = wpa_s->own_addr;
  5730. if (go_dev_addr == NULL)
  5731. go_dev_addr = wpa_s->global->p2p_dev_addr;
  5732. freq = ssid->frequency;
  5733. } else {
  5734. role = P2P_INVITE_ROLE_CLIENT;
  5735. if (wpa_s->wpa_state < WPA_ASSOCIATED) {
  5736. wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
  5737. "invite to current group");
  5738. return -1;
  5739. }
  5740. bssid = wpa_s->bssid;
  5741. if (go_dev_addr == NULL &&
  5742. !is_zero_ether_addr(wpa_s->go_dev_addr))
  5743. go_dev_addr = wpa_s->go_dev_addr;
  5744. freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
  5745. (int) wpa_s->assoc_freq;
  5746. }
  5747. wpa_s->parent->pending_invite_ssid_id = -1;
  5748. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5749. return -1;
  5750. size = P2P_MAX_PREF_CHANNELS;
  5751. res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
  5752. role == P2P_INVITE_ROLE_ACTIVE_GO,
  5753. pref_freq_list, &size);
  5754. if (res)
  5755. return res;
  5756. wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
  5757. return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
  5758. ssid->ssid, ssid->ssid_len, force_freq,
  5759. go_dev_addr, persistent, pref_freq, -1);
  5760. }
  5761. void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
  5762. {
  5763. struct wpa_ssid *ssid = wpa_s->current_ssid;
  5764. u8 go_dev_addr[ETH_ALEN];
  5765. int network_id = -1;
  5766. int persistent;
  5767. int freq;
  5768. u8 ip[3 * 4];
  5769. char ip_addr[100];
  5770. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
  5771. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  5772. wpa_s->parent, NULL);
  5773. }
  5774. if (!wpa_s->show_group_started || !ssid)
  5775. return;
  5776. wpa_s->show_group_started = 0;
  5777. os_memset(go_dev_addr, 0, ETH_ALEN);
  5778. if (ssid->bssid_set)
  5779. os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
  5780. persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
  5781. ssid->ssid_len);
  5782. os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
  5783. if (wpa_s->global->p2p_group_formation == wpa_s)
  5784. wpa_s->global->p2p_group_formation = NULL;
  5785. freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
  5786. (int) wpa_s->assoc_freq;
  5787. ip_addr[0] = '\0';
  5788. if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
  5789. int res;
  5790. res = os_snprintf(ip_addr, sizeof(ip_addr),
  5791. " ip_addr=%u.%u.%u.%u "
  5792. "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
  5793. ip[0], ip[1], ip[2], ip[3],
  5794. ip[4], ip[5], ip[6], ip[7],
  5795. ip[8], ip[9], ip[10], ip[11]);
  5796. if (os_snprintf_error(sizeof(ip_addr), res))
  5797. ip_addr[0] = '\0';
  5798. }
  5799. wpas_p2p_group_started(wpa_s, 0, ssid, freq,
  5800. ssid->passphrase == NULL && ssid->psk_set ?
  5801. ssid->psk : NULL,
  5802. ssid->passphrase, go_dev_addr, persistent,
  5803. ip_addr);
  5804. if (persistent)
  5805. network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
  5806. ssid, go_dev_addr);
  5807. if (network_id < 0)
  5808. network_id = ssid->id;
  5809. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
  5810. }
  5811. int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
  5812. u32 interval1, u32 duration2, u32 interval2)
  5813. {
  5814. int ret;
  5815. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5816. return -1;
  5817. if (wpa_s->wpa_state < WPA_ASSOCIATED ||
  5818. wpa_s->current_ssid == NULL ||
  5819. wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
  5820. return -1;
  5821. ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
  5822. wpa_s->own_addr, wpa_s->assoc_freq,
  5823. duration1, interval1, duration2, interval2);
  5824. if (ret == 0)
  5825. wpa_s->waiting_presence_resp = 1;
  5826. return ret;
  5827. }
  5828. int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
  5829. unsigned int interval)
  5830. {
  5831. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5832. return -1;
  5833. return p2p_ext_listen(wpa_s->global->p2p, period, interval);
  5834. }
  5835. static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
  5836. {
  5837. if (wpa_s->current_ssid == NULL) {
  5838. /*
  5839. * current_ssid can be cleared when P2P client interface gets
  5840. * disconnected, so assume this interface was used as P2P
  5841. * client.
  5842. */
  5843. return 1;
  5844. }
  5845. return wpa_s->current_ssid->p2p_group &&
  5846. wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
  5847. }
  5848. static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
  5849. {
  5850. struct wpa_supplicant *wpa_s = eloop_ctx;
  5851. if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
  5852. wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
  5853. "disabled");
  5854. return;
  5855. }
  5856. wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
  5857. "group");
  5858. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
  5859. }
  5860. static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
  5861. {
  5862. int timeout;
  5863. if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
  5864. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
  5865. if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
  5866. return;
  5867. timeout = wpa_s->conf->p2p_group_idle;
  5868. if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
  5869. (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
  5870. timeout = P2P_MAX_CLIENT_IDLE;
  5871. if (timeout == 0)
  5872. return;
  5873. if (timeout < 0) {
  5874. if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
  5875. timeout = 0; /* special client mode no-timeout */
  5876. else
  5877. return;
  5878. }
  5879. if (wpa_s->p2p_in_provisioning) {
  5880. /*
  5881. * Use the normal group formation timeout during the
  5882. * provisioning phase to avoid terminating this process too
  5883. * early due to group idle timeout.
  5884. */
  5885. wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
  5886. "during provisioning");
  5887. return;
  5888. }
  5889. if (wpa_s->show_group_started) {
  5890. /*
  5891. * Use the normal group formation timeout between the end of
  5892. * the provisioning phase and completion of 4-way handshake to
  5893. * avoid terminating this process too early due to group idle
  5894. * timeout.
  5895. */
  5896. wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
  5897. "while waiting for initial 4-way handshake to "
  5898. "complete");
  5899. return;
  5900. }
  5901. wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
  5902. timeout);
  5903. eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
  5904. wpa_s, NULL);
  5905. }
  5906. /* Returns 1 if the interface was removed */
  5907. int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
  5908. u16 reason_code, const u8 *ie, size_t ie_len,
  5909. int locally_generated)
  5910. {
  5911. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5912. return 0;
  5913. if (!locally_generated)
  5914. p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
  5915. ie_len);
  5916. if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
  5917. wpa_s->current_ssid &&
  5918. wpa_s->current_ssid->p2p_group &&
  5919. wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
  5920. wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
  5921. "session is ending");
  5922. if (wpas_p2p_group_delete(wpa_s,
  5923. P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
  5924. > 0)
  5925. return 1;
  5926. }
  5927. return 0;
  5928. }
  5929. void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
  5930. u16 reason_code, const u8 *ie, size_t ie_len,
  5931. int locally_generated)
  5932. {
  5933. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5934. return;
  5935. if (!locally_generated)
  5936. p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
  5937. ie_len);
  5938. }
  5939. void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
  5940. {
  5941. struct p2p_data *p2p = wpa_s->global->p2p;
  5942. if (p2p == NULL)
  5943. return;
  5944. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
  5945. return;
  5946. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
  5947. p2p_set_dev_name(p2p, wpa_s->conf->device_name);
  5948. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
  5949. p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
  5950. if (wpa_s->wps &&
  5951. (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
  5952. p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
  5953. if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
  5954. p2p_set_uuid(p2p, wpa_s->wps->uuid);
  5955. if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
  5956. p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
  5957. p2p_set_model_name(p2p, wpa_s->conf->model_name);
  5958. p2p_set_model_number(p2p, wpa_s->conf->model_number);
  5959. p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
  5960. }
  5961. if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
  5962. p2p_set_sec_dev_types(p2p,
  5963. (void *) wpa_s->conf->sec_device_type,
  5964. wpa_s->conf->num_sec_device_types);
  5965. if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
  5966. int i;
  5967. p2p_remove_wps_vendor_extensions(p2p);
  5968. for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
  5969. if (wpa_s->conf->wps_vendor_ext[i] == NULL)
  5970. continue;
  5971. p2p_add_wps_vendor_extension(
  5972. p2p, wpa_s->conf->wps_vendor_ext[i]);
  5973. }
  5974. }
  5975. if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
  5976. wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  5977. char country[3];
  5978. country[0] = wpa_s->conf->country[0];
  5979. country[1] = wpa_s->conf->country[1];
  5980. country[2] = 0x04;
  5981. p2p_set_country(p2p, country);
  5982. }
  5983. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
  5984. p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
  5985. wpa_s->conf->p2p_ssid_postfix ?
  5986. os_strlen(wpa_s->conf->p2p_ssid_postfix) :
  5987. 0);
  5988. }
  5989. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
  5990. p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
  5991. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
  5992. u8 reg_class, channel;
  5993. int ret;
  5994. unsigned int r;
  5995. u8 channel_forced;
  5996. if (wpa_s->conf->p2p_listen_reg_class &&
  5997. wpa_s->conf->p2p_listen_channel) {
  5998. reg_class = wpa_s->conf->p2p_listen_reg_class;
  5999. channel = wpa_s->conf->p2p_listen_channel;
  6000. channel_forced = 1;
  6001. } else {
  6002. reg_class = 81;
  6003. /*
  6004. * Pick one of the social channels randomly as the
  6005. * listen channel.
  6006. */
  6007. if (os_get_random((u8 *) &r, sizeof(r)) < 0)
  6008. channel = 1;
  6009. else
  6010. channel = 1 + (r % 3) * 5;
  6011. channel_forced = 0;
  6012. }
  6013. ret = p2p_set_listen_channel(p2p, reg_class, channel,
  6014. channel_forced);
  6015. if (ret)
  6016. wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
  6017. "failed: %d", ret);
  6018. }
  6019. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
  6020. u8 op_reg_class, op_channel, cfg_op_channel;
  6021. int ret = 0;
  6022. unsigned int r;
  6023. if (wpa_s->conf->p2p_oper_reg_class &&
  6024. wpa_s->conf->p2p_oper_channel) {
  6025. op_reg_class = wpa_s->conf->p2p_oper_reg_class;
  6026. op_channel = wpa_s->conf->p2p_oper_channel;
  6027. cfg_op_channel = 1;
  6028. } else {
  6029. op_reg_class = 81;
  6030. /*
  6031. * Use random operation channel from (1, 6, 11)
  6032. *if no other preference is indicated.
  6033. */
  6034. if (os_get_random((u8 *) &r, sizeof(r)) < 0)
  6035. op_channel = 1;
  6036. else
  6037. op_channel = 1 + (r % 3) * 5;
  6038. cfg_op_channel = 0;
  6039. }
  6040. ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
  6041. cfg_op_channel);
  6042. if (ret)
  6043. wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
  6044. "failed: %d", ret);
  6045. }
  6046. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
  6047. if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
  6048. wpa_s->conf->p2p_pref_chan) < 0) {
  6049. wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
  6050. "update failed");
  6051. }
  6052. if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
  6053. wpa_printf(MSG_ERROR, "P2P: No GO channel list "
  6054. "update failed");
  6055. }
  6056. }
  6057. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
  6058. p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
  6059. }
  6060. int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
  6061. int duration)
  6062. {
  6063. if (!wpa_s->ap_iface)
  6064. return -1;
  6065. return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
  6066. duration);
  6067. }
  6068. int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
  6069. {
  6070. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  6071. return -1;
  6072. wpa_s->global->cross_connection = enabled;
  6073. p2p_set_cross_connect(wpa_s->global->p2p, enabled);
  6074. if (!enabled) {
  6075. struct wpa_supplicant *iface;
  6076. for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
  6077. {
  6078. if (iface->cross_connect_enabled == 0)
  6079. continue;
  6080. iface->cross_connect_enabled = 0;
  6081. iface->cross_connect_in_use = 0;
  6082. wpa_msg_global(iface->parent, MSG_INFO,
  6083. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  6084. iface->ifname,
  6085. iface->cross_connect_uplink);
  6086. }
  6087. }
  6088. return 0;
  6089. }
  6090. static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
  6091. {
  6092. struct wpa_supplicant *iface;
  6093. if (!uplink->global->cross_connection)
  6094. return;
  6095. for (iface = uplink->global->ifaces; iface; iface = iface->next) {
  6096. if (!iface->cross_connect_enabled)
  6097. continue;
  6098. if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
  6099. 0)
  6100. continue;
  6101. if (iface->ap_iface == NULL)
  6102. continue;
  6103. if (iface->cross_connect_in_use)
  6104. continue;
  6105. iface->cross_connect_in_use = 1;
  6106. wpa_msg_global(iface->parent, MSG_INFO,
  6107. P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
  6108. iface->ifname, iface->cross_connect_uplink);
  6109. }
  6110. }
  6111. static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
  6112. {
  6113. struct wpa_supplicant *iface;
  6114. for (iface = uplink->global->ifaces; iface; iface = iface->next) {
  6115. if (!iface->cross_connect_enabled)
  6116. continue;
  6117. if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
  6118. 0)
  6119. continue;
  6120. if (!iface->cross_connect_in_use)
  6121. continue;
  6122. wpa_msg_global(iface->parent, MSG_INFO,
  6123. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  6124. iface->ifname, iface->cross_connect_uplink);
  6125. iface->cross_connect_in_use = 0;
  6126. }
  6127. }
  6128. void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
  6129. {
  6130. if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
  6131. wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
  6132. wpa_s->cross_connect_disallowed)
  6133. wpas_p2p_disable_cross_connect(wpa_s);
  6134. else
  6135. wpas_p2p_enable_cross_connect(wpa_s);
  6136. if (!wpa_s->ap_iface &&
  6137. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
  6138. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
  6139. }
  6140. void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
  6141. {
  6142. wpas_p2p_disable_cross_connect(wpa_s);
  6143. if (!wpa_s->ap_iface &&
  6144. !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
  6145. wpa_s, NULL))
  6146. wpas_p2p_set_group_idle_timeout(wpa_s);
  6147. }
  6148. static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
  6149. {
  6150. struct wpa_supplicant *iface;
  6151. if (!wpa_s->global->cross_connection)
  6152. return;
  6153. for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
  6154. if (iface == wpa_s)
  6155. continue;
  6156. if (iface->drv_flags &
  6157. WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
  6158. continue;
  6159. if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
  6160. iface != wpa_s->parent)
  6161. continue;
  6162. wpa_s->cross_connect_enabled = 1;
  6163. os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
  6164. sizeof(wpa_s->cross_connect_uplink));
  6165. wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
  6166. "%s to %s whenever uplink is available",
  6167. wpa_s->ifname, wpa_s->cross_connect_uplink);
  6168. if (iface->ap_iface || iface->current_ssid == NULL ||
  6169. iface->current_ssid->mode != WPAS_MODE_INFRA ||
  6170. iface->cross_connect_disallowed ||
  6171. iface->wpa_state != WPA_COMPLETED)
  6172. break;
  6173. wpa_s->cross_connect_in_use = 1;
  6174. wpa_msg_global(wpa_s->parent, MSG_INFO,
  6175. P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
  6176. wpa_s->ifname, wpa_s->cross_connect_uplink);
  6177. break;
  6178. }
  6179. }
  6180. int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
  6181. {
  6182. if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
  6183. !wpa_s->p2p_in_provisioning)
  6184. return 0; /* not P2P client operation */
  6185. wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
  6186. "session overlap");
  6187. if (wpa_s != wpa_s->parent)
  6188. wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
  6189. wpas_p2p_group_formation_failed(wpa_s, 0);
  6190. return 1;
  6191. }
  6192. void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
  6193. {
  6194. struct wpa_supplicant *wpa_s = eloop_ctx;
  6195. wpas_p2p_notif_pbc_overlap(wpa_s);
  6196. }
  6197. void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
  6198. enum wpas_p2p_channel_update_trig trig)
  6199. {
  6200. struct p2p_channels chan, cli_chan;
  6201. struct wpa_used_freq_data *freqs = NULL;
  6202. unsigned int num = wpa_s->num_multichan_concurrent;
  6203. if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
  6204. return;
  6205. freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
  6206. if (!freqs)
  6207. return;
  6208. num = get_shared_radio_freqs_data(wpa_s, freqs, num);
  6209. os_memset(&chan, 0, sizeof(chan));
  6210. os_memset(&cli_chan, 0, sizeof(cli_chan));
  6211. if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
  6212. wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
  6213. "channel list");
  6214. return;
  6215. }
  6216. p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
  6217. wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
  6218. /*
  6219. * The used frequencies map changed, so it is possible that a GO is
  6220. * using a channel that is no longer valid for P2P use. It is also
  6221. * possible that due to policy consideration, it would be preferable to
  6222. * move it to a frequency already used by other station interfaces.
  6223. */
  6224. wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
  6225. os_free(freqs);
  6226. }
  6227. static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
  6228. struct wpa_scan_results *scan_res)
  6229. {
  6230. wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
  6231. }
  6232. int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
  6233. {
  6234. struct wpa_global *global = wpa_s->global;
  6235. int found = 0;
  6236. const u8 *peer;
  6237. if (global->p2p == NULL)
  6238. return -1;
  6239. wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
  6240. if (wpa_s->pending_interface_name[0] &&
  6241. !is_zero_ether_addr(wpa_s->pending_interface_addr))
  6242. found = 1;
  6243. peer = p2p_get_go_neg_peer(global->p2p);
  6244. if (peer) {
  6245. wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
  6246. MACSTR, MAC2STR(peer));
  6247. p2p_unauthorize(global->p2p, peer);
  6248. found = 1;
  6249. }
  6250. if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
  6251. wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
  6252. wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
  6253. found = 1;
  6254. }
  6255. if (wpa_s->pending_pd_before_join) {
  6256. wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
  6257. wpa_s->pending_pd_before_join = 0;
  6258. found = 1;
  6259. }
  6260. wpas_p2p_stop_find(wpa_s);
  6261. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  6262. if (wpa_s == global->p2p_group_formation &&
  6263. (wpa_s->p2p_in_provisioning ||
  6264. wpa_s->parent->pending_interface_type ==
  6265. WPA_IF_P2P_CLIENT)) {
  6266. wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
  6267. "formation found - cancelling",
  6268. wpa_s->ifname);
  6269. found = 1;
  6270. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  6271. wpa_s->parent, NULL);
  6272. if (wpa_s->p2p_in_provisioning) {
  6273. wpas_group_formation_completed(wpa_s, 0, 0);
  6274. break;
  6275. }
  6276. wpas_p2p_group_delete(wpa_s,
  6277. P2P_GROUP_REMOVAL_REQUESTED);
  6278. break;
  6279. } else if (wpa_s->p2p_in_invitation) {
  6280. wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
  6281. wpa_s->ifname);
  6282. found = 1;
  6283. wpas_p2p_group_formation_failed(wpa_s, 0);
  6284. }
  6285. }
  6286. if (!found) {
  6287. wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
  6288. return -1;
  6289. }
  6290. return 0;
  6291. }
  6292. void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
  6293. {
  6294. if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
  6295. return;
  6296. wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
  6297. "being available anymore");
  6298. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
  6299. }
  6300. void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
  6301. int freq_24, int freq_5, int freq_overall)
  6302. {
  6303. struct p2p_data *p2p = wpa_s->global->p2p;
  6304. if (p2p == NULL)
  6305. return;
  6306. p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
  6307. }
  6308. int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
  6309. {
  6310. u8 peer[ETH_ALEN];
  6311. struct p2p_data *p2p = wpa_s->global->p2p;
  6312. if (p2p == NULL)
  6313. return -1;
  6314. if (hwaddr_aton(addr, peer))
  6315. return -1;
  6316. return p2p_unauthorize(p2p, peer);
  6317. }
  6318. /**
  6319. * wpas_p2p_disconnect - Disconnect from a P2P Group
  6320. * @wpa_s: Pointer to wpa_supplicant data
  6321. * Returns: 0 on success, -1 on failure
  6322. *
  6323. * This can be used to disconnect from a group in which the local end is a P2P
  6324. * Client or to end a P2P Group in case the local end is the Group Owner. If a
  6325. * virtual network interface was created for this group, that interface will be
  6326. * removed. Otherwise, only the configured P2P group network will be removed
  6327. * from the interface.
  6328. */
  6329. int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
  6330. {
  6331. if (wpa_s == NULL)
  6332. return -1;
  6333. return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
  6334. -1 : 0;
  6335. }
  6336. int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
  6337. {
  6338. int ret;
  6339. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  6340. return 0;
  6341. ret = p2p_in_progress(wpa_s->global->p2p);
  6342. if (ret == 0) {
  6343. /*
  6344. * Check whether there is an ongoing WPS provisioning step (or
  6345. * other parts of group formation) on another interface since
  6346. * p2p_in_progress() does not report this to avoid issues for
  6347. * scans during such provisioning step.
  6348. */
  6349. if (wpa_s->global->p2p_group_formation &&
  6350. wpa_s->global->p2p_group_formation != wpa_s) {
  6351. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
  6352. "in group formation",
  6353. wpa_s->global->p2p_group_formation->ifname);
  6354. ret = 1;
  6355. }
  6356. }
  6357. if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
  6358. struct os_reltime now;
  6359. os_get_reltime(&now);
  6360. if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
  6361. P2P_MAX_INITIAL_CONN_WAIT_GO)) {
  6362. /* Wait for the first client has expired */
  6363. wpa_s->global->p2p_go_wait_client.sec = 0;
  6364. } else {
  6365. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
  6366. ret = 1;
  6367. }
  6368. }
  6369. return ret;
  6370. }
  6371. void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
  6372. struct wpa_ssid *ssid)
  6373. {
  6374. if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
  6375. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  6376. wpa_s->parent, NULL) > 0) {
  6377. /**
  6378. * Remove the network by scheduling the group formation
  6379. * timeout to happen immediately. The teardown code
  6380. * needs to be scheduled to run asynch later so that we
  6381. * don't delete data from under ourselves unexpectedly.
  6382. * Calling wpas_p2p_group_formation_timeout directly
  6383. * causes a series of crashes in WPS failure scenarios.
  6384. */
  6385. wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
  6386. "P2P group network getting removed");
  6387. eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
  6388. wpa_s->parent, NULL);
  6389. }
  6390. }
  6391. struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
  6392. const u8 *addr, const u8 *ssid,
  6393. size_t ssid_len)
  6394. {
  6395. struct wpa_ssid *s;
  6396. size_t i;
  6397. for (s = wpa_s->conf->ssid; s; s = s->next) {
  6398. if (s->disabled != 2)
  6399. continue;
  6400. if (ssid &&
  6401. (ssid_len != s->ssid_len ||
  6402. os_memcmp(ssid, s->ssid, ssid_len) != 0))
  6403. continue;
  6404. if (addr == NULL) {
  6405. if (s->mode == WPAS_MODE_P2P_GO)
  6406. return s;
  6407. continue;
  6408. }
  6409. if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
  6410. return s; /* peer is GO in the persistent group */
  6411. if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
  6412. continue;
  6413. for (i = 0; i < s->num_p2p_clients; i++) {
  6414. if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
  6415. addr, ETH_ALEN) == 0)
  6416. return s; /* peer is P2P client in persistent
  6417. * group */
  6418. }
  6419. }
  6420. return NULL;
  6421. }
  6422. void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
  6423. const u8 *addr)
  6424. {
  6425. if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  6426. wpa_s->parent, NULL) > 0) {
  6427. /*
  6428. * This can happen if WPS provisioning step is not terminated
  6429. * cleanly (e.g., P2P Client does not send WSC_Done). Since the
  6430. * peer was able to connect, there is no need to time out group
  6431. * formation after this, though. In addition, this is used with
  6432. * the initial connection wait on the GO as a separate formation
  6433. * timeout and as such, expected to be hit after the initial WPS
  6434. * provisioning step.
  6435. */
  6436. wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
  6437. if (!wpa_s->p2p_go_group_formation_completed &&
  6438. !wpa_s->group_formation_reported) {
  6439. /*
  6440. * GO has not yet notified group formation success since
  6441. * the WPS step was not completed cleanly. Do that
  6442. * notification now since the P2P Client was able to
  6443. * connect and as such, must have received the
  6444. * credential from the WPS step.
  6445. */
  6446. if (wpa_s->global->p2p)
  6447. p2p_wps_success_cb(wpa_s->global->p2p, addr);
  6448. wpas_group_formation_completed(wpa_s, 1, 0);
  6449. }
  6450. }
  6451. if (!wpa_s->p2p_go_group_formation_completed) {
  6452. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
  6453. wpa_s->p2p_go_group_formation_completed = 1;
  6454. wpa_s->global->p2p_group_formation = NULL;
  6455. wpa_s->p2p_in_provisioning = 0;
  6456. wpa_s->p2p_in_invitation = 0;
  6457. }
  6458. wpa_s->global->p2p_go_wait_client.sec = 0;
  6459. if (addr == NULL)
  6460. return;
  6461. wpas_p2p_add_persistent_group_client(wpa_s, addr);
  6462. }
  6463. static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
  6464. int group_added)
  6465. {
  6466. struct wpa_supplicant *group = wpa_s;
  6467. int ret = 0;
  6468. if (wpa_s->global->p2p_group_formation)
  6469. group = wpa_s->global->p2p_group_formation;
  6470. wpa_s = wpa_s->global->p2p_init_wpa_s;
  6471. offchannel_send_action_done(wpa_s);
  6472. if (group_added)
  6473. ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
  6474. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
  6475. wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
  6476. wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
  6477. 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
  6478. wpa_s->p2p_persistent_id,
  6479. wpa_s->p2p_pd_before_go_neg,
  6480. wpa_s->p2p_go_ht40,
  6481. wpa_s->p2p_go_vht);
  6482. return ret;
  6483. }
  6484. int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
  6485. {
  6486. int res;
  6487. if (!wpa_s->p2p_fallback_to_go_neg ||
  6488. wpa_s->p2p_in_provisioning <= 5)
  6489. return 0;
  6490. if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
  6491. return 0; /* peer operating as a GO */
  6492. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
  6493. "fallback to GO Negotiation");
  6494. wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
  6495. "reason=GO-not-found");
  6496. res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
  6497. return res == 1 ? 2 : 1;
  6498. }
  6499. unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
  6500. {
  6501. struct wpa_supplicant *ifs;
  6502. if (wpa_s->wpa_state > WPA_SCANNING) {
  6503. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
  6504. "concurrent operation",
  6505. wpa_s->conf->p2p_search_delay);
  6506. return wpa_s->conf->p2p_search_delay;
  6507. }
  6508. dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
  6509. radio_list) {
  6510. if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
  6511. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
  6512. "delay due to concurrent operation on "
  6513. "interface %s",
  6514. wpa_s->conf->p2p_search_delay,
  6515. ifs->ifname);
  6516. return wpa_s->conf->p2p_search_delay;
  6517. }
  6518. }
  6519. return 0;
  6520. }
  6521. static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
  6522. struct wpa_ssid *s, const u8 *addr,
  6523. int iface_addr)
  6524. {
  6525. struct psk_list_entry *psk, *tmp;
  6526. int changed = 0;
  6527. dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
  6528. list) {
  6529. if ((iface_addr && !psk->p2p &&
  6530. os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
  6531. (!iface_addr && psk->p2p &&
  6532. os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
  6533. wpa_dbg(wpa_s, MSG_DEBUG,
  6534. "P2P: Remove persistent group PSK list entry for "
  6535. MACSTR " p2p=%u",
  6536. MAC2STR(psk->addr), psk->p2p);
  6537. dl_list_del(&psk->list);
  6538. os_free(psk);
  6539. changed++;
  6540. }
  6541. }
  6542. return changed;
  6543. }
  6544. void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
  6545. const u8 *p2p_dev_addr,
  6546. const u8 *psk, size_t psk_len)
  6547. {
  6548. struct wpa_ssid *ssid = wpa_s->current_ssid;
  6549. struct wpa_ssid *persistent;
  6550. struct psk_list_entry *p, *last;
  6551. if (psk_len != sizeof(p->psk))
  6552. return;
  6553. if (p2p_dev_addr) {
  6554. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
  6555. " p2p_dev_addr=" MACSTR,
  6556. MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
  6557. if (is_zero_ether_addr(p2p_dev_addr))
  6558. p2p_dev_addr = NULL;
  6559. } else {
  6560. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
  6561. MAC2STR(mac_addr));
  6562. }
  6563. if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
  6564. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
  6565. /* To be added to persistent group once created */
  6566. if (wpa_s->global->add_psk == NULL) {
  6567. wpa_s->global->add_psk = os_zalloc(sizeof(*p));
  6568. if (wpa_s->global->add_psk == NULL)
  6569. return;
  6570. }
  6571. p = wpa_s->global->add_psk;
  6572. if (p2p_dev_addr) {
  6573. p->p2p = 1;
  6574. os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
  6575. } else {
  6576. p->p2p = 0;
  6577. os_memcpy(p->addr, mac_addr, ETH_ALEN);
  6578. }
  6579. os_memcpy(p->psk, psk, psk_len);
  6580. return;
  6581. }
  6582. if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
  6583. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
  6584. return;
  6585. }
  6586. persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
  6587. ssid->ssid_len);
  6588. if (!persistent) {
  6589. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
  6590. return;
  6591. }
  6592. p = os_zalloc(sizeof(*p));
  6593. if (p == NULL)
  6594. return;
  6595. if (p2p_dev_addr) {
  6596. p->p2p = 1;
  6597. os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
  6598. } else {
  6599. p->p2p = 0;
  6600. os_memcpy(p->addr, mac_addr, ETH_ALEN);
  6601. }
  6602. os_memcpy(p->psk, psk, psk_len);
  6603. if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
  6604. (last = dl_list_last(&persistent->psk_list,
  6605. struct psk_list_entry, list))) {
  6606. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
  6607. MACSTR " (p2p=%u) to make room for a new one",
  6608. MAC2STR(last->addr), last->p2p);
  6609. dl_list_del(&last->list);
  6610. os_free(last);
  6611. }
  6612. wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
  6613. p2p_dev_addr ? p2p_dev_addr : mac_addr,
  6614. p2p_dev_addr == NULL);
  6615. if (p2p_dev_addr) {
  6616. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
  6617. MACSTR, MAC2STR(p2p_dev_addr));
  6618. } else {
  6619. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
  6620. MAC2STR(mac_addr));
  6621. }
  6622. dl_list_add(&persistent->psk_list, &p->list);
  6623. if (wpa_s->parent->conf->update_config &&
  6624. wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
  6625. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  6626. }
  6627. static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
  6628. struct wpa_ssid *s, const u8 *addr,
  6629. int iface_addr)
  6630. {
  6631. int res;
  6632. res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
  6633. if (res > 0 && wpa_s->conf->update_config &&
  6634. wpa_config_write(wpa_s->confname, wpa_s->conf))
  6635. wpa_dbg(wpa_s, MSG_DEBUG,
  6636. "P2P: Failed to update configuration");
  6637. }
  6638. static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
  6639. const u8 *peer, int iface_addr)
  6640. {
  6641. struct hostapd_data *hapd;
  6642. struct hostapd_wpa_psk *psk, *prev, *rem;
  6643. struct sta_info *sta;
  6644. if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
  6645. wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
  6646. return;
  6647. /* Remove per-station PSK entry */
  6648. hapd = wpa_s->ap_iface->bss[0];
  6649. prev = NULL;
  6650. psk = hapd->conf->ssid.wpa_psk;
  6651. while (psk) {
  6652. if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
  6653. (!iface_addr &&
  6654. os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
  6655. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
  6656. MACSTR " iface_addr=%d",
  6657. MAC2STR(peer), iface_addr);
  6658. if (prev)
  6659. prev->next = psk->next;
  6660. else
  6661. hapd->conf->ssid.wpa_psk = psk->next;
  6662. rem = psk;
  6663. psk = psk->next;
  6664. os_free(rem);
  6665. } else {
  6666. prev = psk;
  6667. psk = psk->next;
  6668. }
  6669. }
  6670. /* Disconnect from group */
  6671. if (iface_addr)
  6672. sta = ap_get_sta(hapd, peer);
  6673. else
  6674. sta = ap_get_sta_p2p(hapd, peer);
  6675. if (sta) {
  6676. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
  6677. " (iface_addr=%d) from group",
  6678. MAC2STR(peer), iface_addr);
  6679. hostapd_drv_sta_deauth(hapd, sta->addr,
  6680. WLAN_REASON_DEAUTH_LEAVING);
  6681. ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
  6682. }
  6683. }
  6684. void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
  6685. int iface_addr)
  6686. {
  6687. struct wpa_ssid *s;
  6688. struct wpa_supplicant *w;
  6689. struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
  6690. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
  6691. /* Remove from any persistent group */
  6692. for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
  6693. if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
  6694. continue;
  6695. if (!iface_addr)
  6696. wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
  6697. wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
  6698. }
  6699. /* Remove from any operating group */
  6700. for (w = wpa_s->global->ifaces; w; w = w->next)
  6701. wpas_p2p_remove_client_go(w, peer, iface_addr);
  6702. }
  6703. static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
  6704. {
  6705. struct wpa_supplicant *wpa_s = eloop_ctx;
  6706. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
  6707. }
  6708. static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
  6709. {
  6710. struct wpa_supplicant *wpa_s = eloop_ctx;
  6711. wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
  6712. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
  6713. }
  6714. int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
  6715. struct wpa_ssid *ssid)
  6716. {
  6717. struct wpa_supplicant *iface;
  6718. for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
  6719. if (!iface->current_ssid ||
  6720. iface->current_ssid->frequency == freq ||
  6721. (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
  6722. !iface->current_ssid->p2p_group))
  6723. continue;
  6724. /* Remove the connection with least priority */
  6725. if (!wpas_is_p2p_prioritized(iface)) {
  6726. /* STA connection has priority over existing
  6727. * P2P connection, so remove the interface. */
  6728. wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
  6729. eloop_register_timeout(0, 0,
  6730. wpas_p2p_group_freq_conflict,
  6731. iface, NULL);
  6732. /* If connection in progress is P2P connection, do not
  6733. * proceed for the connection. */
  6734. if (wpa_s == iface)
  6735. return -1;
  6736. else
  6737. return 0;
  6738. } else {
  6739. /* P2P connection has priority, disable the STA network
  6740. */
  6741. wpa_supplicant_disable_network(wpa_s->global->ifaces,
  6742. ssid);
  6743. wpa_msg(wpa_s->global->ifaces, MSG_INFO,
  6744. WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
  6745. os_memset(wpa_s->global->ifaces->pending_bssid, 0,
  6746. ETH_ALEN);
  6747. /* If P2P connection is in progress, continue
  6748. * connecting...*/
  6749. if (wpa_s == iface)
  6750. return 0;
  6751. else
  6752. return -1;
  6753. }
  6754. }
  6755. return 0;
  6756. }
  6757. int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
  6758. {
  6759. struct wpa_ssid *ssid = wpa_s->current_ssid;
  6760. if (ssid == NULL || !ssid->p2p_group)
  6761. return 0;
  6762. if (wpa_s->p2p_last_4way_hs_fail &&
  6763. wpa_s->p2p_last_4way_hs_fail == ssid) {
  6764. u8 go_dev_addr[ETH_ALEN];
  6765. struct wpa_ssid *persistent;
  6766. if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
  6767. ssid->ssid,
  6768. ssid->ssid_len) <= 0) {
  6769. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
  6770. goto disconnect;
  6771. }
  6772. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
  6773. MACSTR, MAC2STR(go_dev_addr));
  6774. persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
  6775. ssid->ssid,
  6776. ssid->ssid_len);
  6777. if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
  6778. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
  6779. goto disconnect;
  6780. }
  6781. wpa_msg_global(wpa_s->parent, MSG_INFO,
  6782. P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
  6783. persistent->id);
  6784. disconnect:
  6785. wpa_s->p2p_last_4way_hs_fail = NULL;
  6786. /*
  6787. * Remove the group from a timeout to avoid issues with caller
  6788. * continuing to use the interface if this is on a P2P group
  6789. * interface.
  6790. */
  6791. eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
  6792. wpa_s, NULL);
  6793. return 1;
  6794. }
  6795. wpa_s->p2p_last_4way_hs_fail = ssid;
  6796. return 0;
  6797. }
  6798. #ifdef CONFIG_WPS_NFC
  6799. static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
  6800. struct wpabuf *p2p)
  6801. {
  6802. struct wpabuf *ret;
  6803. size_t wsc_len;
  6804. if (p2p == NULL) {
  6805. wpabuf_free(wsc);
  6806. wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
  6807. return NULL;
  6808. }
  6809. wsc_len = wsc ? wpabuf_len(wsc) : 0;
  6810. ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
  6811. if (ret == NULL) {
  6812. wpabuf_free(wsc);
  6813. wpabuf_free(p2p);
  6814. return NULL;
  6815. }
  6816. wpabuf_put_be16(ret, wsc_len);
  6817. if (wsc)
  6818. wpabuf_put_buf(ret, wsc);
  6819. wpabuf_put_be16(ret, wpabuf_len(p2p));
  6820. wpabuf_put_buf(ret, p2p);
  6821. wpabuf_free(wsc);
  6822. wpabuf_free(p2p);
  6823. wpa_hexdump_buf(MSG_DEBUG,
  6824. "P2P: Generated NFC connection handover message", ret);
  6825. if (ndef && ret) {
  6826. struct wpabuf *tmp;
  6827. tmp = ndef_build_p2p(ret);
  6828. wpabuf_free(ret);
  6829. if (tmp == NULL) {
  6830. wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
  6831. return NULL;
  6832. }
  6833. ret = tmp;
  6834. }
  6835. return ret;
  6836. }
  6837. static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
  6838. struct wpa_ssid **ssid, u8 *go_dev_addr)
  6839. {
  6840. struct wpa_supplicant *iface;
  6841. if (go_dev_addr)
  6842. os_memset(go_dev_addr, 0, ETH_ALEN);
  6843. if (ssid)
  6844. *ssid = NULL;
  6845. for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
  6846. if (iface->wpa_state < WPA_ASSOCIATING ||
  6847. iface->current_ssid == NULL || iface->assoc_freq == 0 ||
  6848. !iface->current_ssid->p2p_group ||
  6849. iface->current_ssid->mode != WPAS_MODE_INFRA)
  6850. continue;
  6851. if (ssid)
  6852. *ssid = iface->current_ssid;
  6853. if (go_dev_addr)
  6854. os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
  6855. return iface->assoc_freq;
  6856. }
  6857. return 0;
  6858. }
  6859. struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
  6860. int ndef)
  6861. {
  6862. struct wpabuf *wsc, *p2p;
  6863. struct wpa_ssid *ssid;
  6864. u8 go_dev_addr[ETH_ALEN];
  6865. int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
  6866. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
  6867. wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
  6868. return NULL;
  6869. }
  6870. if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
  6871. wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
  6872. &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
  6873. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
  6874. return NULL;
  6875. }
  6876. if (cli_freq == 0) {
  6877. wsc = wps_build_nfc_handover_req_p2p(
  6878. wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
  6879. } else
  6880. wsc = NULL;
  6881. p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
  6882. go_dev_addr, ssid ? ssid->ssid : NULL,
  6883. ssid ? ssid->ssid_len : 0);
  6884. return wpas_p2p_nfc_handover(ndef, wsc, p2p);
  6885. }
  6886. struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
  6887. int ndef, int tag)
  6888. {
  6889. struct wpabuf *wsc, *p2p;
  6890. struct wpa_ssid *ssid;
  6891. u8 go_dev_addr[ETH_ALEN];
  6892. int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
  6893. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  6894. return NULL;
  6895. if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
  6896. wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
  6897. &wpa_s->conf->wps_nfc_dh_privkey) < 0)
  6898. return NULL;
  6899. if (cli_freq == 0) {
  6900. wsc = wps_build_nfc_handover_sel_p2p(
  6901. wpa_s->parent->wps,
  6902. tag ? wpa_s->conf->wps_nfc_dev_pw_id :
  6903. DEV_PW_NFC_CONNECTION_HANDOVER,
  6904. wpa_s->conf->wps_nfc_dh_pubkey,
  6905. tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
  6906. } else
  6907. wsc = NULL;
  6908. p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
  6909. go_dev_addr, ssid ? ssid->ssid : NULL,
  6910. ssid ? ssid->ssid_len : 0);
  6911. return wpas_p2p_nfc_handover(ndef, wsc, p2p);
  6912. }
  6913. static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
  6914. struct p2p_nfc_params *params)
  6915. {
  6916. wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
  6917. "connection handover (freq=%d)",
  6918. params->go_freq);
  6919. if (params->go_freq && params->go_ssid_len) {
  6920. wpa_s->p2p_wps_method = WPS_NFC;
  6921. wpa_s->pending_join_wps_method = WPS_NFC;
  6922. os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
  6923. os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
  6924. ETH_ALEN);
  6925. return wpas_p2p_join_start(wpa_s, params->go_freq,
  6926. params->go_ssid,
  6927. params->go_ssid_len);
  6928. }
  6929. return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
  6930. WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
  6931. params->go_freq, -1, 0, 1, 1);
  6932. }
  6933. static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
  6934. struct p2p_nfc_params *params, int tag)
  6935. {
  6936. int res, persistent;
  6937. struct wpa_ssid *ssid;
  6938. wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
  6939. "connection handover");
  6940. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  6941. ssid = wpa_s->current_ssid;
  6942. if (ssid == NULL)
  6943. continue;
  6944. if (ssid->mode != WPAS_MODE_P2P_GO)
  6945. continue;
  6946. if (wpa_s->ap_iface == NULL)
  6947. continue;
  6948. break;
  6949. }
  6950. if (wpa_s == NULL) {
  6951. wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
  6952. return -1;
  6953. }
  6954. if (wpa_s->parent->p2p_oob_dev_pw_id !=
  6955. DEV_PW_NFC_CONNECTION_HANDOVER &&
  6956. !wpa_s->parent->p2p_oob_dev_pw) {
  6957. wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
  6958. return -1;
  6959. }
  6960. res = wpas_ap_wps_add_nfc_pw(
  6961. wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
  6962. wpa_s->parent->p2p_oob_dev_pw,
  6963. wpa_s->parent->p2p_peer_oob_pk_hash_known ?
  6964. wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
  6965. if (res)
  6966. return res;
  6967. if (!tag) {
  6968. wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
  6969. return 0;
  6970. }
  6971. if (!params->peer ||
  6972. !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
  6973. return 0;
  6974. wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
  6975. " to join", MAC2STR(params->peer->p2p_device_addr));
  6976. wpa_s->global->p2p_invite_group = wpa_s;
  6977. persistent = ssid->p2p_persistent_group &&
  6978. wpas_p2p_get_persistent(wpa_s->parent,
  6979. params->peer->p2p_device_addr,
  6980. ssid->ssid, ssid->ssid_len);
  6981. wpa_s->parent->pending_invite_ssid_id = -1;
  6982. return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
  6983. P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
  6984. ssid->ssid, ssid->ssid_len, ssid->frequency,
  6985. wpa_s->global->p2p_dev_addr, persistent, 0,
  6986. wpa_s->parent->p2p_oob_dev_pw_id);
  6987. }
  6988. static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
  6989. struct p2p_nfc_params *params,
  6990. int forced_freq)
  6991. {
  6992. wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
  6993. "connection handover");
  6994. return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
  6995. WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
  6996. forced_freq, -1, 0, 1, 1);
  6997. }
  6998. static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
  6999. struct p2p_nfc_params *params,
  7000. int forced_freq)
  7001. {
  7002. int res;
  7003. wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
  7004. "connection handover");
  7005. res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
  7006. WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
  7007. forced_freq, -1, 0, 1, 1);
  7008. if (res)
  7009. return res;
  7010. res = wpas_p2p_listen(wpa_s, 60);
  7011. if (res) {
  7012. p2p_unauthorize(wpa_s->global->p2p,
  7013. params->peer->p2p_device_addr);
  7014. }
  7015. return res;
  7016. }
  7017. static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
  7018. const struct wpabuf *data,
  7019. int sel, int tag, int forced_freq)
  7020. {
  7021. const u8 *pos, *end;
  7022. u16 len, id;
  7023. struct p2p_nfc_params params;
  7024. int res;
  7025. os_memset(&params, 0, sizeof(params));
  7026. params.sel = sel;
  7027. wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
  7028. pos = wpabuf_head(data);
  7029. end = pos + wpabuf_len(data);
  7030. if (end - pos < 2) {
  7031. wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
  7032. "attributes");
  7033. return -1;
  7034. }
  7035. len = WPA_GET_BE16(pos);
  7036. pos += 2;
  7037. if (len > end - pos) {
  7038. wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
  7039. "attributes");
  7040. return -1;
  7041. }
  7042. params.wsc_attr = pos;
  7043. params.wsc_len = len;
  7044. pos += len;
  7045. if (end - pos < 2) {
  7046. wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
  7047. "attributes");
  7048. return -1;
  7049. }
  7050. len = WPA_GET_BE16(pos);
  7051. pos += 2;
  7052. if (len > end - pos) {
  7053. wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
  7054. "attributes");
  7055. return -1;
  7056. }
  7057. params.p2p_attr = pos;
  7058. params.p2p_len = len;
  7059. pos += len;
  7060. wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
  7061. params.wsc_attr, params.wsc_len);
  7062. wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
  7063. params.p2p_attr, params.p2p_len);
  7064. if (pos < end) {
  7065. wpa_hexdump(MSG_DEBUG,
  7066. "P2P: Ignored extra data after P2P attributes",
  7067. pos, end - pos);
  7068. }
  7069. res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
  7070. if (res)
  7071. return res;
  7072. if (params.next_step == NO_ACTION)
  7073. return 0;
  7074. if (params.next_step == BOTH_GO) {
  7075. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
  7076. MAC2STR(params.peer->p2p_device_addr));
  7077. return 0;
  7078. }
  7079. if (params.next_step == PEER_CLIENT) {
  7080. if (!is_zero_ether_addr(params.go_dev_addr)) {
  7081. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
  7082. "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
  7083. " ssid=\"%s\"",
  7084. MAC2STR(params.peer->p2p_device_addr),
  7085. params.go_freq,
  7086. MAC2STR(params.go_dev_addr),
  7087. wpa_ssid_txt(params.go_ssid,
  7088. params.go_ssid_len));
  7089. } else {
  7090. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
  7091. "peer=" MACSTR " freq=%d",
  7092. MAC2STR(params.peer->p2p_device_addr),
  7093. params.go_freq);
  7094. }
  7095. return 0;
  7096. }
  7097. if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
  7098. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
  7099. MACSTR, MAC2STR(params.peer->p2p_device_addr));
  7100. return 0;
  7101. }
  7102. wpabuf_free(wpa_s->p2p_oob_dev_pw);
  7103. wpa_s->p2p_oob_dev_pw = NULL;
  7104. if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
  7105. wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
  7106. "received");
  7107. return -1;
  7108. }
  7109. id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
  7110. wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
  7111. wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
  7112. params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
  7113. os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
  7114. params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
  7115. wpa_s->p2p_peer_oob_pk_hash_known = 1;
  7116. if (tag) {
  7117. if (id < 0x10) {
  7118. wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
  7119. "peer OOB Device Password Id %u", id);
  7120. return -1;
  7121. }
  7122. wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
  7123. "Device Password Id %u", id);
  7124. wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
  7125. params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
  7126. params.oob_dev_pw_len -
  7127. WPS_OOB_PUBKEY_HASH_LEN - 2);
  7128. wpa_s->p2p_oob_dev_pw_id = id;
  7129. wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
  7130. params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
  7131. params.oob_dev_pw_len -
  7132. WPS_OOB_PUBKEY_HASH_LEN - 2);
  7133. if (wpa_s->p2p_oob_dev_pw == NULL)
  7134. return -1;
  7135. if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
  7136. wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
  7137. &wpa_s->conf->wps_nfc_dh_privkey) < 0)
  7138. return -1;
  7139. } else {
  7140. wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
  7141. "without Device Password");
  7142. wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
  7143. }
  7144. switch (params.next_step) {
  7145. case NO_ACTION:
  7146. case BOTH_GO:
  7147. case PEER_CLIENT:
  7148. /* already covered above */
  7149. return 0;
  7150. case JOIN_GROUP:
  7151. return wpas_p2p_nfc_join_group(wpa_s, &params);
  7152. case AUTH_JOIN:
  7153. return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
  7154. case INIT_GO_NEG:
  7155. return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
  7156. case RESP_GO_NEG:
  7157. /* TODO: use own OOB Dev Pw */
  7158. return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
  7159. }
  7160. return -1;
  7161. }
  7162. int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
  7163. const struct wpabuf *data, int forced_freq)
  7164. {
  7165. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  7166. return -1;
  7167. return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
  7168. }
  7169. int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
  7170. const struct wpabuf *req,
  7171. const struct wpabuf *sel, int forced_freq)
  7172. {
  7173. struct wpabuf *tmp;
  7174. int ret;
  7175. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  7176. return -1;
  7177. wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
  7178. wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
  7179. wpabuf_head(req), wpabuf_len(req));
  7180. wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
  7181. wpabuf_head(sel), wpabuf_len(sel));
  7182. if (forced_freq)
  7183. wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
  7184. tmp = ndef_parse_p2p(init ? sel : req);
  7185. if (tmp == NULL) {
  7186. wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
  7187. return -1;
  7188. }
  7189. ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
  7190. forced_freq);
  7191. wpabuf_free(tmp);
  7192. return ret;
  7193. }
  7194. int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
  7195. {
  7196. const u8 *if_addr;
  7197. int go_intent = wpa_s->conf->p2p_go_intent;
  7198. struct wpa_supplicant *iface;
  7199. if (wpa_s->global->p2p == NULL)
  7200. return -1;
  7201. if (!enabled) {
  7202. wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
  7203. for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
  7204. {
  7205. if (!iface->ap_iface)
  7206. continue;
  7207. hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
  7208. }
  7209. p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
  7210. 0, NULL);
  7211. if (wpa_s->p2p_nfc_tag_enabled)
  7212. wpas_p2p_remove_pending_group_interface(wpa_s);
  7213. wpa_s->p2p_nfc_tag_enabled = 0;
  7214. return 0;
  7215. }
  7216. if (wpa_s->global->p2p_disabled)
  7217. return -1;
  7218. if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
  7219. wpa_s->conf->wps_nfc_dh_privkey == NULL ||
  7220. wpa_s->conf->wps_nfc_dev_pw == NULL ||
  7221. wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
  7222. wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
  7223. "to allow static handover cases");
  7224. return -1;
  7225. }
  7226. wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
  7227. wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
  7228. wpabuf_free(wpa_s->p2p_oob_dev_pw);
  7229. wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
  7230. if (wpa_s->p2p_oob_dev_pw == NULL)
  7231. return -1;
  7232. wpa_s->p2p_peer_oob_pk_hash_known = 0;
  7233. if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
  7234. wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
  7235. /*
  7236. * P2P Group Interface present and the command came on group
  7237. * interface, so enable the token for the current interface.
  7238. */
  7239. wpa_s->create_p2p_iface = 0;
  7240. } else {
  7241. wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
  7242. }
  7243. if (wpa_s->create_p2p_iface) {
  7244. enum wpa_driver_if_type iftype;
  7245. /* Prepare to add a new interface for the group */
  7246. iftype = WPA_IF_P2P_GROUP;
  7247. if (go_intent == 15)
  7248. iftype = WPA_IF_P2P_GO;
  7249. if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
  7250. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  7251. "interface for the group");
  7252. return -1;
  7253. }
  7254. if_addr = wpa_s->pending_interface_addr;
  7255. } else
  7256. if_addr = wpa_s->own_addr;
  7257. wpa_s->p2p_nfc_tag_enabled = enabled;
  7258. for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
  7259. struct hostapd_data *hapd;
  7260. if (iface->ap_iface == NULL)
  7261. continue;
  7262. hapd = iface->ap_iface->bss[0];
  7263. wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
  7264. hapd->conf->wps_nfc_dh_pubkey =
  7265. wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
  7266. wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
  7267. hapd->conf->wps_nfc_dh_privkey =
  7268. wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
  7269. wpabuf_free(hapd->conf->wps_nfc_dev_pw);
  7270. hapd->conf->wps_nfc_dev_pw =
  7271. wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
  7272. hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
  7273. if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
  7274. wpa_dbg(iface, MSG_DEBUG,
  7275. "P2P: Failed to enable NFC Tag for GO");
  7276. }
  7277. }
  7278. p2p_set_authorized_oob_dev_pw_id(
  7279. wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
  7280. if_addr);
  7281. return 0;
  7282. }
  7283. #endif /* CONFIG_WPS_NFC */
  7284. static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
  7285. struct wpa_used_freq_data *freqs,
  7286. unsigned int num)
  7287. {
  7288. u8 curr_chan, cand, chan;
  7289. unsigned int i;
  7290. /*
  7291. * If possible, optimize the Listen channel to be a channel that is
  7292. * already used by one of the other interfaces.
  7293. */
  7294. if (!wpa_s->conf->p2p_optimize_listen_chan)
  7295. return;
  7296. if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
  7297. return;
  7298. curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
  7299. for (i = 0, cand = 0; i < num; i++) {
  7300. ieee80211_freq_to_chan(freqs[i].freq, &chan);
  7301. if (curr_chan == chan) {
  7302. cand = 0;
  7303. break;
  7304. }
  7305. if (chan == 1 || chan == 6 || chan == 11)
  7306. cand = chan;
  7307. }
  7308. if (cand) {
  7309. wpa_dbg(wpa_s, MSG_DEBUG,
  7310. "P2P: Update Listen channel to %u based on operating channel",
  7311. cand);
  7312. p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
  7313. }
  7314. }
  7315. static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
  7316. {
  7317. struct hostapd_config *conf;
  7318. struct p2p_go_neg_results params;
  7319. struct csa_settings csa_settings;
  7320. struct wpa_ssid *current_ssid = wpa_s->current_ssid;
  7321. int old_freq = current_ssid->frequency;
  7322. int ret;
  7323. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
  7324. wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
  7325. return -1;
  7326. }
  7327. /*
  7328. * TODO: This function may not always work correctly. For example,
  7329. * when we have a running GO and a BSS on a DFS channel.
  7330. */
  7331. if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, NULL)) {
  7332. wpa_dbg(wpa_s, MSG_DEBUG,
  7333. "P2P CSA: Failed to select new frequency for GO");
  7334. return -1;
  7335. }
  7336. if (current_ssid->frequency == params.freq) {
  7337. wpa_dbg(wpa_s, MSG_DEBUG,
  7338. "P2P CSA: Selected same frequency - not moving GO");
  7339. return 0;
  7340. }
  7341. conf = hostapd_config_defaults();
  7342. if (!conf) {
  7343. wpa_dbg(wpa_s, MSG_DEBUG,
  7344. "P2P CSA: Failed to allocate default config");
  7345. return -1;
  7346. }
  7347. current_ssid->frequency = params.freq;
  7348. if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
  7349. wpa_dbg(wpa_s, MSG_DEBUG,
  7350. "P2P CSA: Failed to create new GO config");
  7351. ret = -1;
  7352. goto out;
  7353. }
  7354. if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode) {
  7355. wpa_dbg(wpa_s, MSG_DEBUG,
  7356. "P2P CSA: CSA to a different band is not supported");
  7357. ret = -1;
  7358. goto out;
  7359. }
  7360. os_memset(&csa_settings, 0, sizeof(csa_settings));
  7361. csa_settings.cs_count = P2P_GO_CSA_COUNT;
  7362. csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
  7363. csa_settings.freq_params.freq = params.freq;
  7364. csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
  7365. csa_settings.freq_params.ht_enabled = conf->ieee80211n;
  7366. csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
  7367. if (conf->ieee80211ac) {
  7368. int freq1 = 0, freq2 = 0;
  7369. u8 chan, opclass;
  7370. if (ieee80211_freq_to_channel_ext(params.freq,
  7371. conf->secondary_channel,
  7372. conf->vht_oper_chwidth,
  7373. &opclass, &chan) ==
  7374. NUM_HOSTAPD_MODES) {
  7375. wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
  7376. ret = -1;
  7377. goto out;
  7378. }
  7379. if (conf->vht_oper_centr_freq_seg0_idx)
  7380. freq1 = ieee80211_chan_to_freq(
  7381. NULL, opclass,
  7382. conf->vht_oper_centr_freq_seg0_idx);
  7383. if (conf->vht_oper_centr_freq_seg1_idx)
  7384. freq2 = ieee80211_chan_to_freq(
  7385. NULL, opclass,
  7386. conf->vht_oper_centr_freq_seg1_idx);
  7387. if (freq1 < 0 || freq2 < 0) {
  7388. wpa_dbg(wpa_s, MSG_DEBUG,
  7389. "P2P CSA: Selected invalid VHT center freqs");
  7390. ret = -1;
  7391. goto out;
  7392. }
  7393. csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
  7394. csa_settings.freq_params.center_freq1 = freq1;
  7395. csa_settings.freq_params.center_freq2 = freq2;
  7396. switch (conf->vht_oper_chwidth) {
  7397. case VHT_CHANWIDTH_80MHZ:
  7398. case VHT_CHANWIDTH_80P80MHZ:
  7399. csa_settings.freq_params.bandwidth = 80;
  7400. break;
  7401. case VHT_CHANWIDTH_160MHZ:
  7402. csa_settings.freq_params.bandwidth = 160;
  7403. break;
  7404. }
  7405. }
  7406. ret = ap_switch_channel(wpa_s, &csa_settings);
  7407. out:
  7408. current_ssid->frequency = old_freq;
  7409. hostapd_config_free(conf);
  7410. return ret;
  7411. }
  7412. static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
  7413. {
  7414. struct p2p_go_neg_results params;
  7415. struct wpa_ssid *current_ssid = wpa_s->current_ssid;
  7416. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
  7417. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
  7418. current_ssid->frequency);
  7419. /* Stop the AP functionality */
  7420. /* TODO: Should do this in a way that does not indicated to possible
  7421. * P2P Clients in the group that the group is terminated. */
  7422. wpa_supplicant_ap_deinit(wpa_s);
  7423. /* Reselect the GO frequency */
  7424. if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, NULL)) {
  7425. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
  7426. wpas_p2p_group_delete(wpa_s,
  7427. P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
  7428. return;
  7429. }
  7430. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
  7431. params.freq);
  7432. if (params.freq &&
  7433. !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
  7434. wpa_printf(MSG_DEBUG,
  7435. "P2P: Selected freq (%u MHz) is not valid for P2P",
  7436. params.freq);
  7437. wpas_p2p_group_delete(wpa_s,
  7438. P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
  7439. return;
  7440. }
  7441. /* Update the frequency */
  7442. current_ssid->frequency = params.freq;
  7443. wpa_s->connect_without_scan = current_ssid;
  7444. wpa_s->reassociate = 1;
  7445. wpa_s->disconnected = 0;
  7446. wpa_supplicant_req_scan(wpa_s, 0, 0);
  7447. }
  7448. static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
  7449. {
  7450. struct wpa_supplicant *wpa_s = eloop_ctx;
  7451. if (!wpa_s->ap_iface || !wpa_s->current_ssid)
  7452. return;
  7453. wpas_p2p_go_update_common_freqs(wpa_s);
  7454. /* Do not move GO in the middle of a CSA */
  7455. if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
  7456. wpa_printf(MSG_DEBUG,
  7457. "P2P: CSA is in progress - not moving GO");
  7458. return;
  7459. }
  7460. /*
  7461. * First, try a channel switch flow. If it is not supported or fails,
  7462. * take down the GO and bring it up again.
  7463. */
  7464. if (wpas_p2p_move_go_csa(wpa_s) < 0)
  7465. wpas_p2p_move_go_no_csa(wpa_s);
  7466. }
  7467. static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
  7468. {
  7469. struct wpa_supplicant *wpa_s = eloop_ctx;
  7470. struct wpa_used_freq_data *freqs = NULL;
  7471. unsigned int num = wpa_s->num_multichan_concurrent;
  7472. freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
  7473. if (!freqs)
  7474. return;
  7475. num = get_shared_radio_freqs_data(wpa_s, freqs, num);
  7476. /* Previous attempt to move a GO was not possible -- try again. */
  7477. wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
  7478. WPAS_P2P_CHANNEL_UPDATE_ANY);
  7479. os_free(freqs);
  7480. }
  7481. /*
  7482. * Consider moving a GO from its currently used frequency:
  7483. * 1. It is possible that due to regulatory consideration the frequency
  7484. * can no longer be used and there is a need to evacuate the GO.
  7485. * 2. It is possible that due to MCC considerations, it would be preferable
  7486. * to move the GO to a channel that is currently used by some other
  7487. * station interface.
  7488. *
  7489. * In case a frequency that became invalid is once again valid, cancel a
  7490. * previously initiated GO frequency change.
  7491. */
  7492. static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
  7493. struct wpa_used_freq_data *freqs,
  7494. unsigned int num)
  7495. {
  7496. unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
  7497. unsigned int timeout;
  7498. int freq;
  7499. wpas_p2p_go_update_common_freqs(wpa_s);
  7500. freq = wpa_s->current_ssid->frequency;
  7501. for (i = 0, invalid_freq = 0; i < num; i++) {
  7502. if (freqs[i].freq == freq) {
  7503. flags = freqs[i].flags;
  7504. /* The channel is invalid, must change it */
  7505. if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
  7506. wpa_dbg(wpa_s, MSG_DEBUG,
  7507. "P2P: Freq=%d MHz no longer valid for GO",
  7508. freq);
  7509. invalid_freq = 1;
  7510. }
  7511. } else if (freqs[i].flags == 0) {
  7512. /* Freq is not used by any other station interface */
  7513. continue;
  7514. } else if (!p2p_supported_freq(wpa_s->global->p2p,
  7515. freqs[i].freq)) {
  7516. /* Freq is not valid for P2P use cases */
  7517. continue;
  7518. } else if (wpa_s->conf->p2p_go_freq_change_policy ==
  7519. P2P_GO_FREQ_MOVE_SCM) {
  7520. policy_move = 1;
  7521. } else if (wpa_s->conf->p2p_go_freq_change_policy ==
  7522. P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
  7523. wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
  7524. policy_move = 1;
  7525. } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
  7526. P2P_GO_FREQ_MOVE_SCM_ECSA) &&
  7527. wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
  7528. if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
  7529. policy_move = 1;
  7530. } else if ((wpa_s->drv_flags &
  7531. WPA_DRIVER_FLAGS_AP_CSA) &&
  7532. wpas_p2p_go_clients_support_ecsa(wpa_s)) {
  7533. u8 chan;
  7534. /*
  7535. * We do not support CSA between bands, so move
  7536. * GO only within the same band.
  7537. */
  7538. if (wpa_s->ap_iface->current_mode->mode ==
  7539. ieee80211_freq_to_chan(freqs[i].freq,
  7540. &chan))
  7541. policy_move = 1;
  7542. }
  7543. }
  7544. }
  7545. wpa_dbg(wpa_s, MSG_DEBUG,
  7546. "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
  7547. invalid_freq, policy_move, flags);
  7548. /*
  7549. * The channel is valid, or we are going to have a policy move, so
  7550. * cancel timeout.
  7551. */
  7552. if (!invalid_freq || policy_move) {
  7553. wpa_dbg(wpa_s, MSG_DEBUG,
  7554. "P2P: Cancel a GO move from freq=%d MHz", freq);
  7555. eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
  7556. if (wpas_p2p_in_progress(wpa_s)) {
  7557. wpa_dbg(wpa_s, MSG_DEBUG,
  7558. "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
  7559. eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
  7560. wpa_s, NULL);
  7561. eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
  7562. wpas_p2p_reconsider_moving_go,
  7563. wpa_s, NULL);
  7564. return;
  7565. }
  7566. }
  7567. if (!invalid_freq && (!policy_move || flags != 0)) {
  7568. wpa_dbg(wpa_s, MSG_DEBUG,
  7569. "P2P: Not initiating a GO frequency change");
  7570. return;
  7571. }
  7572. /*
  7573. * Do not consider moving GO if it is in the middle of a CSA. When the
  7574. * CSA is finished this flow should be retriggered.
  7575. */
  7576. if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
  7577. wpa_dbg(wpa_s, MSG_DEBUG,
  7578. "P2P: Not initiating a GO frequency change - CSA is in progress");
  7579. return;
  7580. }
  7581. if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
  7582. timeout = P2P_GO_FREQ_CHANGE_TIME;
  7583. else
  7584. timeout = 0;
  7585. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
  7586. freq, timeout);
  7587. eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
  7588. eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
  7589. }
  7590. static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
  7591. struct wpa_used_freq_data *freqs,
  7592. unsigned int num,
  7593. enum wpas_p2p_channel_update_trig trig)
  7594. {
  7595. struct wpa_supplicant *ifs;
  7596. eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
  7597. NULL);
  7598. /*
  7599. * Travers all the radio interfaces, and for each GO interface, check
  7600. * if there is a need to move the GO from the frequency it is using,
  7601. * or in case the frequency is valid again, cancel the evacuation flow.
  7602. */
  7603. dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
  7604. radio_list) {
  7605. if (ifs->current_ssid == NULL ||
  7606. ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
  7607. continue;
  7608. /*
  7609. * The GO was just started or completed channel switch, no need
  7610. * to move it.
  7611. */
  7612. if (wpa_s == ifs &&
  7613. (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
  7614. trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
  7615. wpa_dbg(wpa_s, MSG_DEBUG,
  7616. "P2P: GO move - schedule re-consideration");
  7617. eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
  7618. wpas_p2p_reconsider_moving_go,
  7619. wpa_s, NULL);
  7620. continue;
  7621. }
  7622. wpas_p2p_consider_moving_one_go(ifs, freqs, num);
  7623. }
  7624. }
  7625. void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
  7626. {
  7627. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  7628. return;
  7629. wpas_p2p_update_channel_list(wpa_s,
  7630. WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
  7631. }
  7632. void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
  7633. {
  7634. if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
  7635. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
  7636. "the management interface is being removed");
  7637. wpas_p2p_deinit_global(wpa_s->global);
  7638. }
  7639. }
  7640. void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
  7641. {
  7642. if (wpa_s->ap_iface->bss)
  7643. wpa_s->ap_iface->bss[0]->p2p_group = NULL;
  7644. wpas_p2p_group_deinit(wpa_s);
  7645. }