p2p_supplicant.c 253 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991
  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. struct wpa_supplicant *group_if =
  2655. wpa_s->global->p2p_invite_group;
  2656. if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
  2657. wpas_remove_persistent_client(wpa_s, peer);
  2658. /*
  2659. * Invitation to an active group. If this is successful and we
  2660. * are the GO, set the client wait to postpone some concurrent
  2661. * operations and to allow provisioning and connection to happen
  2662. * more quickly.
  2663. */
  2664. if (status == P2P_SC_SUCCESS &&
  2665. group_if && group_if->current_ssid &&
  2666. group_if->current_ssid->mode == WPAS_MODE_P2P_GO)
  2667. os_get_reltime(&wpa_s->global->p2p_go_wait_client);
  2668. return;
  2669. }
  2670. if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
  2671. wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
  2672. "invitation exchange to indicate readiness for "
  2673. "re-invocation");
  2674. }
  2675. if (status != P2P_SC_SUCCESS) {
  2676. if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
  2677. ssid = wpa_config_get_network(
  2678. wpa_s->conf, wpa_s->pending_invite_ssid_id);
  2679. wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
  2680. }
  2681. wpas_p2p_remove_pending_group_interface(wpa_s);
  2682. return;
  2683. }
  2684. ssid = wpa_config_get_network(wpa_s->conf,
  2685. wpa_s->pending_invite_ssid_id);
  2686. if (ssid == NULL) {
  2687. wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
  2688. "data matching with invitation");
  2689. return;
  2690. }
  2691. /*
  2692. * The peer could have missed our ctrl::ack frame for Invitation
  2693. * Response and continue retransmitting the frame. To reduce the
  2694. * likelihood of the peer not getting successful TX status for the
  2695. * Invitation Response frame, wait a short time here before starting
  2696. * the persistent group so that we will remain on the current channel to
  2697. * acknowledge any possible retransmission from the peer.
  2698. */
  2699. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
  2700. "starting persistent group");
  2701. os_sleep(0, 50000);
  2702. if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
  2703. freq_included(wpa_s, channels, neg_freq))
  2704. freq = neg_freq;
  2705. else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
  2706. freq_included(wpa_s, channels, peer_oper_freq))
  2707. freq = peer_oper_freq;
  2708. else
  2709. freq = 0;
  2710. wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
  2711. freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  2712. wpas_p2p_group_add_persistent(wpa_s, ssid,
  2713. ssid->mode == WPAS_MODE_P2P_GO,
  2714. wpa_s->p2p_persistent_go_freq,
  2715. freq,
  2716. wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
  2717. channels,
  2718. ssid->mode == WPAS_MODE_P2P_GO ?
  2719. P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
  2720. 0, 1);
  2721. }
  2722. static int wpas_p2p_disallowed_freq(struct wpa_global *global,
  2723. unsigned int freq)
  2724. {
  2725. if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
  2726. return 1;
  2727. return freq_range_list_includes(&global->p2p_disallow_freq, freq);
  2728. }
  2729. static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
  2730. {
  2731. reg->channel[reg->channels] = chan;
  2732. reg->channels++;
  2733. }
  2734. static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
  2735. struct p2p_channels *chan,
  2736. struct p2p_channels *cli_chan)
  2737. {
  2738. int i, cla = 0;
  2739. wpa_s->global->p2p_24ghz_social_channels = 1;
  2740. os_memset(cli_chan, 0, sizeof(*cli_chan));
  2741. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
  2742. "band");
  2743. /* Operating class 81 - 2.4 GHz band channels 1..13 */
  2744. chan->reg_class[cla].reg_class = 81;
  2745. chan->reg_class[cla].channels = 0;
  2746. for (i = 0; i < 11; i++) {
  2747. if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
  2748. wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
  2749. }
  2750. if (chan->reg_class[cla].channels)
  2751. cla++;
  2752. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
  2753. "band");
  2754. /* Operating class 115 - 5 GHz, channels 36-48 */
  2755. chan->reg_class[cla].reg_class = 115;
  2756. chan->reg_class[cla].channels = 0;
  2757. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
  2758. wpas_p2p_add_chan(&chan->reg_class[cla], 36);
  2759. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
  2760. wpas_p2p_add_chan(&chan->reg_class[cla], 40);
  2761. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
  2762. wpas_p2p_add_chan(&chan->reg_class[cla], 44);
  2763. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
  2764. wpas_p2p_add_chan(&chan->reg_class[cla], 48);
  2765. if (chan->reg_class[cla].channels)
  2766. cla++;
  2767. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
  2768. "band");
  2769. /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
  2770. chan->reg_class[cla].reg_class = 124;
  2771. chan->reg_class[cla].channels = 0;
  2772. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
  2773. wpas_p2p_add_chan(&chan->reg_class[cla], 149);
  2774. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
  2775. wpas_p2p_add_chan(&chan->reg_class[cla], 153);
  2776. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
  2777. wpas_p2p_add_chan(&chan->reg_class[cla], 157);
  2778. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
  2779. wpas_p2p_add_chan(&chan->reg_class[cla], 161);
  2780. if (chan->reg_class[cla].channels)
  2781. cla++;
  2782. chan->reg_classes = cla;
  2783. return 0;
  2784. }
  2785. static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
  2786. u16 num_modes,
  2787. enum hostapd_hw_mode mode)
  2788. {
  2789. u16 i;
  2790. for (i = 0; i < num_modes; i++) {
  2791. if (modes[i].mode == mode)
  2792. return &modes[i];
  2793. }
  2794. return NULL;
  2795. }
  2796. enum chan_allowed {
  2797. NOT_ALLOWED, NO_IR, ALLOWED
  2798. };
  2799. static int has_channel(struct wpa_global *global,
  2800. struct hostapd_hw_modes *mode, u8 chan, int *flags)
  2801. {
  2802. int i;
  2803. unsigned int freq;
  2804. freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
  2805. chan * 5;
  2806. if (wpas_p2p_disallowed_freq(global, freq))
  2807. return NOT_ALLOWED;
  2808. for (i = 0; i < mode->num_channels; i++) {
  2809. if (mode->channels[i].chan == chan) {
  2810. if (flags)
  2811. *flags = mode->channels[i].flag;
  2812. if (mode->channels[i].flag &
  2813. (HOSTAPD_CHAN_DISABLED |
  2814. HOSTAPD_CHAN_RADAR))
  2815. return NOT_ALLOWED;
  2816. if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
  2817. return NO_IR;
  2818. return ALLOWED;
  2819. }
  2820. }
  2821. return NOT_ALLOWED;
  2822. }
  2823. struct p2p_oper_class_map {
  2824. enum hostapd_hw_mode mode;
  2825. u8 op_class;
  2826. u8 min_chan;
  2827. u8 max_chan;
  2828. u8 inc;
  2829. enum { BW20, BW40PLUS, BW40MINUS, BW80, BW2160 } bw;
  2830. };
  2831. static const struct p2p_oper_class_map op_class[] = {
  2832. { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
  2833. #if 0 /* Do not enable HT40 on 2 GHz for now */
  2834. { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
  2835. { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
  2836. #endif
  2837. { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
  2838. { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
  2839. { HOSTAPD_MODE_IEEE80211A, 125, 149, 169, 4, BW20 },
  2840. { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
  2841. { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
  2842. { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
  2843. { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
  2844. /*
  2845. * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
  2846. * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
  2847. * 80 MHz, but currently use the following definition for simplicity
  2848. * (these center frequencies are not actual channels, which makes
  2849. * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
  2850. * removing invalid channels.
  2851. */
  2852. { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
  2853. { HOSTAPD_MODE_IEEE80211AD, 180, 1, 4, 1, BW2160 },
  2854. { -1, 0, 0, 0, 0, BW20 }
  2855. };
  2856. static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
  2857. struct hostapd_hw_modes *mode,
  2858. u8 channel)
  2859. {
  2860. u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
  2861. unsigned int i;
  2862. if (mode->mode != HOSTAPD_MODE_IEEE80211A)
  2863. return 0;
  2864. for (i = 0; i < ARRAY_SIZE(center_channels); i++)
  2865. /*
  2866. * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
  2867. * so the center channel is 6 channels away from the start/end.
  2868. */
  2869. if (channel >= center_channels[i] - 6 &&
  2870. channel <= center_channels[i] + 6)
  2871. return center_channels[i];
  2872. return 0;
  2873. }
  2874. static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
  2875. struct hostapd_hw_modes *mode,
  2876. u8 channel, u8 bw)
  2877. {
  2878. u8 center_chan;
  2879. int i, flags;
  2880. enum chan_allowed res, ret = ALLOWED;
  2881. center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
  2882. if (!center_chan)
  2883. return NOT_ALLOWED;
  2884. if (center_chan >= 58 && center_chan <= 138)
  2885. return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
  2886. /* check all the channels are available */
  2887. for (i = 0; i < 4; i++) {
  2888. int adj_chan = center_chan - 6 + i * 4;
  2889. res = has_channel(wpa_s->global, mode, adj_chan, &flags);
  2890. if (res == NOT_ALLOWED)
  2891. return NOT_ALLOWED;
  2892. if (res == NO_IR)
  2893. ret = NO_IR;
  2894. if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
  2895. return NOT_ALLOWED;
  2896. if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
  2897. return NOT_ALLOWED;
  2898. if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
  2899. return NOT_ALLOWED;
  2900. if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
  2901. return NOT_ALLOWED;
  2902. }
  2903. return ret;
  2904. }
  2905. static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
  2906. struct hostapd_hw_modes *mode,
  2907. u8 channel, u8 bw)
  2908. {
  2909. int flag = 0;
  2910. enum chan_allowed res, res2;
  2911. res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
  2912. if (bw == BW40MINUS) {
  2913. if (!(flag & HOSTAPD_CHAN_HT40MINUS))
  2914. return NOT_ALLOWED;
  2915. res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
  2916. } else if (bw == BW40PLUS) {
  2917. if (!(flag & HOSTAPD_CHAN_HT40PLUS))
  2918. return NOT_ALLOWED;
  2919. res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
  2920. } else if (bw == BW80) {
  2921. res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
  2922. }
  2923. if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
  2924. return NOT_ALLOWED;
  2925. if (res == NO_IR || res2 == NO_IR)
  2926. return NO_IR;
  2927. return res;
  2928. }
  2929. static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
  2930. struct p2p_channels *chan,
  2931. struct p2p_channels *cli_chan)
  2932. {
  2933. struct hostapd_hw_modes *mode;
  2934. int cla, op, cli_cla;
  2935. if (wpa_s->hw.modes == NULL) {
  2936. wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
  2937. "of all supported channels; assume dualband "
  2938. "support");
  2939. return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
  2940. }
  2941. cla = cli_cla = 0;
  2942. for (op = 0; op_class[op].op_class; op++) {
  2943. const struct p2p_oper_class_map *o = &op_class[op];
  2944. u8 ch;
  2945. struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
  2946. mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
  2947. if (mode == NULL)
  2948. continue;
  2949. if (mode->mode == HOSTAPD_MODE_IEEE80211G)
  2950. wpa_s->global->p2p_24ghz_social_channels = 1;
  2951. for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
  2952. enum chan_allowed res;
  2953. res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
  2954. if (res == ALLOWED) {
  2955. if (reg == NULL) {
  2956. wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
  2957. o->op_class);
  2958. reg = &chan->reg_class[cla];
  2959. cla++;
  2960. reg->reg_class = o->op_class;
  2961. }
  2962. reg->channel[reg->channels] = ch;
  2963. reg->channels++;
  2964. } else if (res == NO_IR &&
  2965. wpa_s->conf->p2p_add_cli_chan) {
  2966. if (cli_reg == NULL) {
  2967. wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
  2968. o->op_class);
  2969. cli_reg = &cli_chan->reg_class[cli_cla];
  2970. cli_cla++;
  2971. cli_reg->reg_class = o->op_class;
  2972. }
  2973. cli_reg->channel[cli_reg->channels] = ch;
  2974. cli_reg->channels++;
  2975. }
  2976. }
  2977. if (reg) {
  2978. wpa_hexdump(MSG_DEBUG, "P2P: Channels",
  2979. reg->channel, reg->channels);
  2980. }
  2981. if (cli_reg) {
  2982. wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
  2983. cli_reg->channel, cli_reg->channels);
  2984. }
  2985. }
  2986. chan->reg_classes = cla;
  2987. cli_chan->reg_classes = cli_cla;
  2988. return 0;
  2989. }
  2990. int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
  2991. struct hostapd_hw_modes *mode, u8 channel)
  2992. {
  2993. int op;
  2994. enum chan_allowed ret;
  2995. for (op = 0; op_class[op].op_class; op++) {
  2996. const struct p2p_oper_class_map *o = &op_class[op];
  2997. u8 ch;
  2998. for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
  2999. if (o->mode != HOSTAPD_MODE_IEEE80211A ||
  3000. (o->bw != BW40PLUS && o->bw != BW40MINUS) ||
  3001. ch != channel)
  3002. continue;
  3003. ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
  3004. if (ret == ALLOWED)
  3005. return (o->bw == BW40MINUS) ? -1 : 1;
  3006. }
  3007. }
  3008. return 0;
  3009. }
  3010. int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
  3011. struct hostapd_hw_modes *mode, u8 channel)
  3012. {
  3013. if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
  3014. return 0;
  3015. return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
  3016. }
  3017. static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
  3018. size_t buf_len)
  3019. {
  3020. struct wpa_supplicant *wpa_s = ctx;
  3021. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3022. if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
  3023. break;
  3024. }
  3025. if (wpa_s == NULL)
  3026. return -1;
  3027. return wpa_drv_get_noa(wpa_s, buf, buf_len);
  3028. }
  3029. struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
  3030. const u8 *ssid, size_t ssid_len)
  3031. {
  3032. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3033. struct wpa_ssid *s = wpa_s->current_ssid;
  3034. if (s == NULL)
  3035. continue;
  3036. if (s->mode != WPAS_MODE_P2P_GO &&
  3037. s->mode != WPAS_MODE_AP &&
  3038. s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
  3039. continue;
  3040. if (s->ssid_len != ssid_len ||
  3041. os_memcmp(ssid, s->ssid, ssid_len) != 0)
  3042. continue;
  3043. return wpa_s;
  3044. }
  3045. return NULL;
  3046. }
  3047. struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
  3048. const u8 *peer_dev_addr)
  3049. {
  3050. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3051. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3052. if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
  3053. continue;
  3054. if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
  3055. return wpa_s;
  3056. }
  3057. return NULL;
  3058. }
  3059. static int wpas_go_connected(void *ctx, const u8 *dev_addr)
  3060. {
  3061. struct wpa_supplicant *wpa_s = ctx;
  3062. return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
  3063. }
  3064. static int wpas_is_concurrent_session_active(void *ctx)
  3065. {
  3066. struct wpa_supplicant *wpa_s = ctx;
  3067. struct wpa_supplicant *ifs;
  3068. for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
  3069. if (ifs == wpa_s)
  3070. continue;
  3071. if (ifs->wpa_state > WPA_ASSOCIATED)
  3072. return 1;
  3073. }
  3074. return 0;
  3075. }
  3076. static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
  3077. {
  3078. struct wpa_supplicant *wpa_s = ctx;
  3079. wpa_msg_global(wpa_s, level, "P2P: %s", msg);
  3080. }
  3081. int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
  3082. const char *conf_p2p_dev)
  3083. {
  3084. struct wpa_interface iface;
  3085. struct wpa_supplicant *p2pdev_wpa_s;
  3086. char ifname[100];
  3087. char force_name[100];
  3088. int ret;
  3089. ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
  3090. wpa_s->ifname);
  3091. if (os_snprintf_error(sizeof(ifname), ret))
  3092. return -1;
  3093. force_name[0] = '\0';
  3094. wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
  3095. ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
  3096. force_name, wpa_s->pending_interface_addr, NULL);
  3097. if (ret < 0) {
  3098. wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
  3099. return ret;
  3100. }
  3101. os_strlcpy(wpa_s->pending_interface_name, ifname,
  3102. sizeof(wpa_s->pending_interface_name));
  3103. os_memset(&iface, 0, sizeof(iface));
  3104. iface.p2p_mgmt = 1;
  3105. iface.ifname = wpa_s->pending_interface_name;
  3106. iface.driver = wpa_s->driver->name;
  3107. iface.driver_param = wpa_s->conf->driver_param;
  3108. /*
  3109. * If a P2P Device configuration file was given, use it as the interface
  3110. * configuration file (instead of using parent's configuration file.
  3111. */
  3112. if (conf_p2p_dev) {
  3113. iface.confname = conf_p2p_dev;
  3114. iface.ctrl_interface = NULL;
  3115. } else {
  3116. iface.confname = wpa_s->confname;
  3117. iface.ctrl_interface = wpa_s->conf->ctrl_interface;
  3118. }
  3119. p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
  3120. if (!p2pdev_wpa_s) {
  3121. wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
  3122. return -1;
  3123. }
  3124. wpa_s->pending_interface_name[0] = '\0';
  3125. return 0;
  3126. }
  3127. static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
  3128. const u8 *noa, size_t noa_len)
  3129. {
  3130. struct wpa_supplicant *wpa_s, *intf = ctx;
  3131. char hex[100];
  3132. for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3133. if (wpa_s->waiting_presence_resp)
  3134. break;
  3135. }
  3136. if (!wpa_s) {
  3137. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
  3138. return;
  3139. }
  3140. wpa_s->waiting_presence_resp = 0;
  3141. wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
  3142. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
  3143. " status=%u noa=%s", MAC2STR(src), status, hex);
  3144. }
  3145. static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
  3146. size_t ssid_len, u8 *go_dev_addr,
  3147. u8 *ret_ssid, size_t *ret_ssid_len,
  3148. u8 *intended_iface_addr)
  3149. {
  3150. struct wpa_supplicant *wpa_s = ctx;
  3151. struct wpa_ssid *s;
  3152. s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
  3153. if (s) {
  3154. os_memcpy(ret_ssid, s->ssid, s->ssid_len);
  3155. *ret_ssid_len = s->ssid_len;
  3156. os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
  3157. if (s->mode != WPAS_MODE_P2P_GO) {
  3158. os_memset(intended_iface_addr, 0, ETH_ALEN);
  3159. } else if (wpas_p2p_create_iface(wpa_s)) {
  3160. if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
  3161. return 0;
  3162. os_memcpy(intended_iface_addr,
  3163. wpa_s->pending_interface_addr, ETH_ALEN);
  3164. } else {
  3165. os_memcpy(intended_iface_addr, wpa_s->own_addr,
  3166. ETH_ALEN);
  3167. }
  3168. return 1;
  3169. }
  3170. return 0;
  3171. }
  3172. static int wpas_get_go_info(void *ctx, u8 *intended_addr,
  3173. u8 *ssid, size_t *ssid_len, int *group_iface,
  3174. unsigned int *freq)
  3175. {
  3176. struct wpa_supplicant *wpa_s = ctx;
  3177. struct wpa_supplicant *go;
  3178. struct wpa_ssid *s;
  3179. /*
  3180. * group_iface will be set to 1 only if a dedicated interface for P2P
  3181. * role is required. First, we try to reuse an active GO. However,
  3182. * if it is not present, we will try to reactivate an existing
  3183. * persistent group and set group_iface to 1, so the caller will know
  3184. * that the pending interface should be used.
  3185. */
  3186. *group_iface = 0;
  3187. if (freq)
  3188. *freq = 0;
  3189. go = wpas_p2p_get_go_group(wpa_s);
  3190. if (!go) {
  3191. s = wpas_p2p_get_persistent_go(wpa_s);
  3192. *group_iface = wpas_p2p_create_iface(wpa_s);
  3193. if (s)
  3194. os_memcpy(intended_addr, s->bssid, ETH_ALEN);
  3195. else
  3196. return 0;
  3197. } else {
  3198. s = go->current_ssid;
  3199. os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
  3200. if (freq)
  3201. *freq = go->assoc_freq;
  3202. }
  3203. os_memcpy(ssid, s->ssid, s->ssid_len);
  3204. *ssid_len = s->ssid_len;
  3205. return 1;
  3206. }
  3207. static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
  3208. const u8 *ssid, size_t ssid_len)
  3209. {
  3210. struct wpa_supplicant *wpa_s = ctx;
  3211. struct wpa_ssid *s;
  3212. int save_config = 0;
  3213. size_t i;
  3214. /* Start with our first choice of Persistent Groups */
  3215. while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
  3216. if (go && ssid && ssid_len &&
  3217. s->ssid_len == ssid_len &&
  3218. os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
  3219. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  3220. break;
  3221. /* Remove stale persistent group */
  3222. if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
  3223. wpa_config_remove_network(wpa_s->conf, s->id);
  3224. save_config = 1;
  3225. continue;
  3226. }
  3227. for (i = 0; i < s->num_p2p_clients; i++) {
  3228. if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
  3229. peer, ETH_ALEN) != 0)
  3230. continue;
  3231. os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
  3232. s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
  3233. (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
  3234. break;
  3235. }
  3236. s->num_p2p_clients--;
  3237. save_config = 1;
  3238. }
  3239. if (save_config)
  3240. p2p_config_write(wpa_s);
  3241. /* Return TRUE if valid SSID remains */
  3242. return s != NULL;
  3243. }
  3244. static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
  3245. const u8 *feat_cap, size_t feat_cap_len)
  3246. {
  3247. static const char pref[] = " feature_cap=";
  3248. int ret;
  3249. buf[0] = '\0';
  3250. /*
  3251. * We expect a feature capability to contain at least one byte to be
  3252. * reported. The string buffer provided by the caller function is
  3253. * expected to be big enough to contain all bytes of the attribute for
  3254. * known specifications. This function truncates the reported bytes if
  3255. * the feature capability data exceeds the string buffer size.
  3256. */
  3257. if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
  3258. return;
  3259. os_memcpy(buf, pref, sizeof(pref));
  3260. ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
  3261. buf_len - sizeof(pref) + 1,
  3262. feat_cap, feat_cap_len);
  3263. if (ret != (2 * (int) feat_cap_len))
  3264. wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
  3265. }
  3266. static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
  3267. const u8 *adv_mac, const u8 *ses_mac,
  3268. const u8 *grp_mac, u32 adv_id, u32 ses_id,
  3269. u8 conncap, int passwd_id,
  3270. const u8 *persist_ssid,
  3271. size_t persist_ssid_size, int response_done,
  3272. int prov_start, const char *session_info,
  3273. const u8 *feat_cap, size_t feat_cap_len,
  3274. unsigned int freq)
  3275. {
  3276. struct wpa_supplicant *wpa_s = ctx;
  3277. u8 mac[ETH_ALEN];
  3278. struct wpa_ssid *persistent_go, *stale, *s = NULL;
  3279. int save_config = 0;
  3280. struct wpa_supplicant *go_wpa_s;
  3281. char feat_cap_str[256];
  3282. if (!dev)
  3283. return;
  3284. os_memset(mac, 0, ETH_ALEN);
  3285. if (!adv_mac)
  3286. adv_mac = mac;
  3287. if (!ses_mac)
  3288. ses_mac = mac;
  3289. if (!grp_mac)
  3290. grp_mac = mac;
  3291. wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
  3292. feat_cap, feat_cap_len);
  3293. if (prov_start) {
  3294. if (session_info == NULL) {
  3295. wpa_msg_global(wpa_s, MSG_INFO,
  3296. P2P_EVENT_P2PS_PROVISION_START MACSTR
  3297. " adv_id=%x conncap=%x"
  3298. " adv_mac=" MACSTR
  3299. " session=%x mac=" MACSTR
  3300. " dev_passwd_id=%d%s",
  3301. MAC2STR(dev), adv_id, conncap,
  3302. MAC2STR(adv_mac),
  3303. ses_id, MAC2STR(ses_mac),
  3304. passwd_id, feat_cap_str);
  3305. } else {
  3306. wpa_msg_global(wpa_s, MSG_INFO,
  3307. P2P_EVENT_P2PS_PROVISION_START MACSTR
  3308. " adv_id=%x conncap=%x"
  3309. " adv_mac=" MACSTR
  3310. " session=%x mac=" MACSTR
  3311. " dev_passwd_id=%d info='%s'%s",
  3312. MAC2STR(dev), adv_id, conncap,
  3313. MAC2STR(adv_mac),
  3314. ses_id, MAC2STR(ses_mac),
  3315. passwd_id, session_info, feat_cap_str);
  3316. }
  3317. return;
  3318. }
  3319. go_wpa_s = wpas_p2p_get_go_group(wpa_s);
  3320. persistent_go = wpas_p2p_get_persistent_go(wpa_s);
  3321. if (status && status != P2P_SC_SUCCESS_DEFERRED) {
  3322. if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
  3323. wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
  3324. if (persistent_go && !persistent_go->num_p2p_clients) {
  3325. /* remove empty persistent GO */
  3326. wpa_config_remove_network(wpa_s->conf,
  3327. persistent_go->id);
  3328. }
  3329. wpa_msg_global(wpa_s, MSG_INFO,
  3330. P2P_EVENT_P2PS_PROVISION_DONE MACSTR
  3331. " status=%d"
  3332. " adv_id=%x adv_mac=" MACSTR
  3333. " session=%x mac=" MACSTR "%s",
  3334. MAC2STR(dev), status,
  3335. adv_id, MAC2STR(adv_mac),
  3336. ses_id, MAC2STR(ses_mac), feat_cap_str);
  3337. return;
  3338. }
  3339. /* Clean up stale persistent groups with this device */
  3340. if (persist_ssid && persist_ssid_size)
  3341. s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
  3342. persist_ssid_size);
  3343. if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
  3344. is_zero_ether_addr(grp_mac)) {
  3345. wpa_dbg(wpa_s, MSG_ERROR,
  3346. "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
  3347. return;
  3348. }
  3349. for (;;) {
  3350. stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
  3351. if (!stale)
  3352. break;
  3353. if (s && s->ssid_len == stale->ssid_len &&
  3354. os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
  3355. os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
  3356. break;
  3357. /* Remove stale persistent group */
  3358. if (stale->mode != WPAS_MODE_P2P_GO ||
  3359. stale->num_p2p_clients <= 1) {
  3360. wpa_config_remove_network(wpa_s->conf, stale->id);
  3361. } else {
  3362. size_t i;
  3363. for (i = 0; i < stale->num_p2p_clients; i++) {
  3364. if (os_memcmp(stale->p2p_client_list +
  3365. i * ETH_ALEN,
  3366. dev, ETH_ALEN) == 0) {
  3367. os_memmove(stale->p2p_client_list +
  3368. i * ETH_ALEN,
  3369. stale->p2p_client_list +
  3370. (i + 1) * ETH_ALEN,
  3371. (stale->num_p2p_clients -
  3372. i - 1) * ETH_ALEN);
  3373. break;
  3374. }
  3375. }
  3376. stale->num_p2p_clients--;
  3377. }
  3378. save_config = 1;
  3379. }
  3380. if (save_config)
  3381. p2p_config_write(wpa_s);
  3382. if (s) {
  3383. if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
  3384. wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
  3385. if (persistent_go && s != persistent_go &&
  3386. !persistent_go->num_p2p_clients) {
  3387. /* remove empty persistent GO */
  3388. wpa_config_remove_network(wpa_s->conf,
  3389. persistent_go->id);
  3390. /* Save config */
  3391. }
  3392. wpa_msg_global(wpa_s, MSG_INFO,
  3393. P2P_EVENT_P2PS_PROVISION_DONE MACSTR
  3394. " status=%d"
  3395. " adv_id=%x adv_mac=" MACSTR
  3396. " session=%x mac=" MACSTR
  3397. " persist=%d%s",
  3398. MAC2STR(dev), status,
  3399. adv_id, MAC2STR(adv_mac),
  3400. ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
  3401. return;
  3402. }
  3403. if (conncap == P2PS_SETUP_GROUP_OWNER) {
  3404. /*
  3405. * We need to copy the interface name. Simply saving a
  3406. * pointer isn't enough, since if we use pending_interface_name
  3407. * it will be overwritten when the group is added.
  3408. */
  3409. char go_ifname[100];
  3410. go_ifname[0] = '\0';
  3411. if (!go_wpa_s) {
  3412. wpa_s->global->pending_p2ps_group = 1;
  3413. wpa_s->global->pending_p2ps_group_freq = freq;
  3414. if (!wpas_p2p_create_iface(wpa_s))
  3415. os_memcpy(go_ifname, wpa_s->ifname,
  3416. sizeof(go_ifname));
  3417. else if (wpa_s->pending_interface_name[0])
  3418. os_memcpy(go_ifname,
  3419. wpa_s->pending_interface_name,
  3420. sizeof(go_ifname));
  3421. if (!go_ifname[0]) {
  3422. wpas_p2ps_prov_complete(
  3423. wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
  3424. dev, adv_mac, ses_mac,
  3425. grp_mac, adv_id, ses_id, 0, 0,
  3426. NULL, 0, 0, 0, NULL, NULL, 0, 0);
  3427. return;
  3428. }
  3429. /* If PD Resp complete, start up the GO */
  3430. if (response_done && persistent_go) {
  3431. wpas_p2p_group_add_persistent(
  3432. wpa_s, persistent_go,
  3433. 0, 0, freq, 0, 0, NULL,
  3434. persistent_go->mode ==
  3435. WPAS_MODE_P2P_GO ?
  3436. P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
  3437. 0, 0);
  3438. } else if (response_done) {
  3439. wpas_p2p_group_add(wpa_s, 1, freq, 0, 0);
  3440. }
  3441. if (passwd_id == DEV_PW_P2PS_DEFAULT) {
  3442. os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
  3443. ETH_ALEN);
  3444. wpa_s->p2ps_method_config_any = 1;
  3445. }
  3446. } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
  3447. os_memcpy(go_ifname, go_wpa_s->ifname,
  3448. sizeof(go_ifname));
  3449. if (is_zero_ether_addr(grp_mac)) {
  3450. wpa_dbg(go_wpa_s, MSG_DEBUG,
  3451. "P2P: Setting PIN-1 for ANY");
  3452. wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
  3453. "12345670", NULL, 0,
  3454. 0);
  3455. } else {
  3456. wpa_dbg(go_wpa_s, MSG_DEBUG,
  3457. "P2P: Setting PIN-1 for " MACSTR,
  3458. MAC2STR(grp_mac));
  3459. wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
  3460. "12345670", NULL, 0,
  3461. 0);
  3462. }
  3463. os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
  3464. wpa_s->p2ps_method_config_any = 1;
  3465. }
  3466. wpa_msg_global(wpa_s, MSG_INFO,
  3467. P2P_EVENT_P2PS_PROVISION_DONE MACSTR
  3468. " status=%d conncap=%x"
  3469. " adv_id=%x adv_mac=" MACSTR
  3470. " session=%x mac=" MACSTR
  3471. " dev_passwd_id=%d go=%s%s",
  3472. MAC2STR(dev), status, conncap,
  3473. adv_id, MAC2STR(adv_mac),
  3474. ses_id, MAC2STR(ses_mac),
  3475. passwd_id, go_ifname, feat_cap_str);
  3476. return;
  3477. }
  3478. if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
  3479. wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
  3480. if (persistent_go && !persistent_go->num_p2p_clients) {
  3481. /* remove empty persistent GO */
  3482. wpa_config_remove_network(wpa_s->conf, persistent_go->id);
  3483. }
  3484. if (conncap == P2PS_SETUP_CLIENT) {
  3485. wpa_msg_global(wpa_s, MSG_INFO,
  3486. P2P_EVENT_P2PS_PROVISION_DONE MACSTR
  3487. " status=%d conncap=%x"
  3488. " adv_id=%x adv_mac=" MACSTR
  3489. " session=%x mac=" MACSTR
  3490. " dev_passwd_id=%d join=" MACSTR "%s",
  3491. MAC2STR(dev), status, conncap,
  3492. adv_id, MAC2STR(adv_mac),
  3493. ses_id, MAC2STR(ses_mac),
  3494. passwd_id, MAC2STR(grp_mac), feat_cap_str);
  3495. } else {
  3496. wpa_msg_global(wpa_s, MSG_INFO,
  3497. P2P_EVENT_P2PS_PROVISION_DONE MACSTR
  3498. " status=%d conncap=%x"
  3499. " adv_id=%x adv_mac=" MACSTR
  3500. " session=%x mac=" MACSTR
  3501. " dev_passwd_id=%d%s",
  3502. MAC2STR(dev), status, conncap,
  3503. adv_id, MAC2STR(adv_mac),
  3504. ses_id, MAC2STR(ses_mac),
  3505. passwd_id, feat_cap_str);
  3506. }
  3507. }
  3508. static int _wpas_p2p_in_progress(void *ctx)
  3509. {
  3510. struct wpa_supplicant *wpa_s = ctx;
  3511. return wpas_p2p_in_progress(wpa_s);
  3512. }
  3513. static int wpas_prov_disc_resp_cb(void *ctx)
  3514. {
  3515. struct wpa_supplicant *wpa_s = ctx;
  3516. struct wpa_ssid *persistent_go;
  3517. unsigned int freq;
  3518. if (!wpa_s->global->pending_p2ps_group)
  3519. return 0;
  3520. freq = wpa_s->global->pending_p2ps_group_freq;
  3521. wpa_s->global->pending_p2ps_group_freq = 0;
  3522. wpa_s->global->pending_p2ps_group = 0;
  3523. if (wpas_p2p_get_go_group(wpa_s))
  3524. return 0;
  3525. persistent_go = wpas_p2p_get_persistent_go(wpa_s);
  3526. if (persistent_go) {
  3527. wpas_p2p_group_add_persistent(
  3528. wpa_s, persistent_go, 0, 0, 0, 0, 0, NULL,
  3529. persistent_go->mode == WPAS_MODE_P2P_GO ?
  3530. P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0);
  3531. } else {
  3532. wpas_p2p_group_add(wpa_s, 1, freq, 0, 0);
  3533. }
  3534. return 1;
  3535. }
  3536. static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
  3537. unsigned int *len,
  3538. unsigned int *freq_list)
  3539. {
  3540. struct wpa_supplicant *wpa_s = ctx;
  3541. return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
  3542. WPA_IF_P2P_CLIENT, len, freq_list);
  3543. }
  3544. /**
  3545. * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
  3546. * @global: Pointer to global data from wpa_supplicant_init()
  3547. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3548. * Returns: 0 on success, -1 on failure
  3549. */
  3550. int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
  3551. {
  3552. struct p2p_config p2p;
  3553. int i;
  3554. if (wpa_s->conf->p2p_disabled)
  3555. return 0;
  3556. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
  3557. return 0;
  3558. if (global->p2p)
  3559. return 0;
  3560. os_memset(&p2p, 0, sizeof(p2p));
  3561. p2p.cb_ctx = wpa_s;
  3562. p2p.debug_print = wpas_p2p_debug_print;
  3563. p2p.p2p_scan = wpas_p2p_scan;
  3564. p2p.send_action = wpas_send_action;
  3565. p2p.send_action_done = wpas_send_action_done;
  3566. p2p.go_neg_completed = wpas_go_neg_completed;
  3567. p2p.go_neg_req_rx = wpas_go_neg_req_rx;
  3568. p2p.dev_found = wpas_dev_found;
  3569. p2p.dev_lost = wpas_dev_lost;
  3570. p2p.find_stopped = wpas_find_stopped;
  3571. p2p.start_listen = wpas_start_listen;
  3572. p2p.stop_listen = wpas_stop_listen;
  3573. p2p.send_probe_resp = wpas_send_probe_resp;
  3574. p2p.sd_request = wpas_sd_request;
  3575. p2p.sd_response = wpas_sd_response;
  3576. p2p.prov_disc_req = wpas_prov_disc_req;
  3577. p2p.prov_disc_resp = wpas_prov_disc_resp;
  3578. p2p.prov_disc_fail = wpas_prov_disc_fail;
  3579. p2p.invitation_process = wpas_invitation_process;
  3580. p2p.invitation_received = wpas_invitation_received;
  3581. p2p.invitation_result = wpas_invitation_result;
  3582. p2p.get_noa = wpas_get_noa;
  3583. p2p.go_connected = wpas_go_connected;
  3584. p2p.presence_resp = wpas_presence_resp;
  3585. p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
  3586. p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
  3587. p2p.get_persistent_group = wpas_get_persistent_group;
  3588. p2p.get_go_info = wpas_get_go_info;
  3589. p2p.remove_stale_groups = wpas_remove_stale_groups;
  3590. p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
  3591. p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
  3592. p2p.p2ps_group_capability = p2ps_group_capability;
  3593. p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
  3594. os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
  3595. os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
  3596. p2p.dev_name = wpa_s->conf->device_name;
  3597. p2p.manufacturer = wpa_s->conf->manufacturer;
  3598. p2p.model_name = wpa_s->conf->model_name;
  3599. p2p.model_number = wpa_s->conf->model_number;
  3600. p2p.serial_number = wpa_s->conf->serial_number;
  3601. if (wpa_s->wps) {
  3602. os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
  3603. p2p.config_methods = wpa_s->wps->config_methods;
  3604. }
  3605. if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
  3606. wpa_printf(MSG_ERROR,
  3607. "P2P: Failed to configure supported channel list");
  3608. return -1;
  3609. }
  3610. if (wpa_s->conf->p2p_listen_reg_class &&
  3611. wpa_s->conf->p2p_listen_channel) {
  3612. p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
  3613. p2p.channel = wpa_s->conf->p2p_listen_channel;
  3614. p2p.channel_forced = 1;
  3615. } else {
  3616. /*
  3617. * Pick one of the social channels randomly as the listen
  3618. * channel.
  3619. */
  3620. if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
  3621. &p2p.channel) != 0) {
  3622. wpa_printf(MSG_INFO,
  3623. "P2P: No social channels supported by the driver - do not enable P2P");
  3624. return 0;
  3625. }
  3626. p2p.channel_forced = 0;
  3627. }
  3628. wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
  3629. p2p.reg_class, p2p.channel);
  3630. if (wpa_s->conf->p2p_oper_reg_class &&
  3631. wpa_s->conf->p2p_oper_channel) {
  3632. p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
  3633. p2p.op_channel = wpa_s->conf->p2p_oper_channel;
  3634. p2p.cfg_op_channel = 1;
  3635. wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
  3636. "%d:%d", p2p.op_reg_class, p2p.op_channel);
  3637. } else {
  3638. /*
  3639. * Use random operation channel from 2.4 GHz band social
  3640. * channels (1, 6, 11) or band 60 GHz social channel (2) if no
  3641. * other preference is indicated.
  3642. */
  3643. if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
  3644. &p2p.op_channel) != 0) {
  3645. wpa_printf(MSG_ERROR,
  3646. "P2P: Failed to select random social channel as operation channel");
  3647. return -1;
  3648. }
  3649. p2p.cfg_op_channel = 0;
  3650. wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
  3651. "%d:%d", p2p.op_reg_class, p2p.op_channel);
  3652. }
  3653. if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
  3654. p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
  3655. p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
  3656. }
  3657. if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  3658. os_memcpy(p2p.country, wpa_s->conf->country, 2);
  3659. p2p.country[2] = 0x04;
  3660. } else
  3661. os_memcpy(p2p.country, "XX\x04", 3);
  3662. os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
  3663. WPS_DEV_TYPE_LEN);
  3664. p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
  3665. os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
  3666. p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
  3667. p2p.concurrent_operations = !!(wpa_s->drv_flags &
  3668. WPA_DRIVER_FLAGS_P2P_CONCURRENT);
  3669. p2p.max_peers = 100;
  3670. if (wpa_s->conf->p2p_ssid_postfix) {
  3671. p2p.ssid_postfix_len =
  3672. os_strlen(wpa_s->conf->p2p_ssid_postfix);
  3673. if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
  3674. p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
  3675. os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
  3676. p2p.ssid_postfix_len);
  3677. }
  3678. p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
  3679. p2p.max_listen = wpa_s->max_remain_on_chan;
  3680. if (wpa_s->conf->p2p_passphrase_len >= 8 &&
  3681. wpa_s->conf->p2p_passphrase_len <= 63)
  3682. p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
  3683. else
  3684. p2p.passphrase_len = 8;
  3685. global->p2p = p2p_init(&p2p);
  3686. if (global->p2p == NULL)
  3687. return -1;
  3688. global->p2p_init_wpa_s = wpa_s;
  3689. for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
  3690. if (wpa_s->conf->wps_vendor_ext[i] == NULL)
  3691. continue;
  3692. p2p_add_wps_vendor_extension(
  3693. global->p2p, wpa_s->conf->wps_vendor_ext[i]);
  3694. }
  3695. p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
  3696. return 0;
  3697. }
  3698. /**
  3699. * wpas_p2p_deinit - Deinitialize per-interface P2P data
  3700. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3701. *
  3702. * This function deinitialize per-interface P2P data.
  3703. */
  3704. void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
  3705. {
  3706. if (wpa_s->driver && wpa_s->drv_priv)
  3707. wpa_drv_probe_req_report(wpa_s, 0);
  3708. if (wpa_s->go_params) {
  3709. /* Clear any stored provisioning info */
  3710. p2p_clear_provisioning_info(
  3711. wpa_s->global->p2p,
  3712. wpa_s->go_params->peer_device_addr);
  3713. }
  3714. os_free(wpa_s->go_params);
  3715. wpa_s->go_params = NULL;
  3716. eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
  3717. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  3718. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3719. wpa_s->p2p_long_listen = 0;
  3720. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  3721. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  3722. wpas_p2p_remove_pending_group_interface(wpa_s);
  3723. eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
  3724. wpas_p2p_listen_work_done(wpa_s);
  3725. if (wpa_s->p2p_send_action_work) {
  3726. os_free(wpa_s->p2p_send_action_work->ctx);
  3727. radio_work_done(wpa_s->p2p_send_action_work);
  3728. wpa_s->p2p_send_action_work = NULL;
  3729. }
  3730. eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
  3731. wpabuf_free(wpa_s->p2p_oob_dev_pw);
  3732. wpa_s->p2p_oob_dev_pw = NULL;
  3733. os_free(wpa_s->p2p_group_common_freqs);
  3734. wpa_s->p2p_group_common_freqs = NULL;
  3735. wpa_s->p2p_group_common_freqs_num = 0;
  3736. /* TODO: remove group interface from the driver if this wpa_s instance
  3737. * is on top of a P2P group interface */
  3738. }
  3739. /**
  3740. * wpas_p2p_deinit_global - Deinitialize global P2P module
  3741. * @global: Pointer to global data from wpa_supplicant_init()
  3742. *
  3743. * This function deinitializes the global (per device) P2P module.
  3744. */
  3745. static void wpas_p2p_deinit_global(struct wpa_global *global)
  3746. {
  3747. struct wpa_supplicant *wpa_s, *tmp;
  3748. wpa_s = global->ifaces;
  3749. wpas_p2p_service_flush(global->p2p_init_wpa_s);
  3750. /* Remove remaining P2P group interfaces */
  3751. while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
  3752. wpa_s = wpa_s->next;
  3753. while (wpa_s) {
  3754. tmp = global->ifaces;
  3755. while (tmp &&
  3756. (tmp == wpa_s ||
  3757. tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
  3758. tmp = tmp->next;
  3759. }
  3760. if (tmp == NULL)
  3761. break;
  3762. /* Disconnect from the P2P group and deinit the interface */
  3763. wpas_p2p_disconnect(tmp);
  3764. }
  3765. /*
  3766. * Deinit GO data on any possibly remaining interface (if main
  3767. * interface is used as GO).
  3768. */
  3769. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3770. if (wpa_s->ap_iface)
  3771. wpas_p2p_group_deinit(wpa_s);
  3772. }
  3773. p2p_deinit(global->p2p);
  3774. global->p2p = NULL;
  3775. global->p2p_init_wpa_s = NULL;
  3776. }
  3777. static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
  3778. {
  3779. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
  3780. wpa_s->conf->p2p_no_group_iface)
  3781. return 0; /* separate interface disabled per configuration */
  3782. if (wpa_s->drv_flags &
  3783. (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
  3784. WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
  3785. return 1; /* P2P group requires a new interface in every case
  3786. */
  3787. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
  3788. return 0; /* driver does not support concurrent operations */
  3789. if (wpa_s->global->ifaces->next)
  3790. return 1; /* more that one interface already in use */
  3791. if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
  3792. return 1; /* this interface is already in use */
  3793. return 0;
  3794. }
  3795. static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
  3796. const u8 *peer_addr,
  3797. enum p2p_wps_method wps_method,
  3798. int go_intent, const u8 *own_interface_addr,
  3799. unsigned int force_freq, int persistent_group,
  3800. struct wpa_ssid *ssid, unsigned int pref_freq)
  3801. {
  3802. if (persistent_group && wpa_s->conf->persistent_reconnect)
  3803. persistent_group = 2;
  3804. /*
  3805. * Increase GO config timeout if HT40 is used since it takes some time
  3806. * to scan channels for coex purposes before the BSS can be started.
  3807. */
  3808. p2p_set_config_timeout(wpa_s->global->p2p,
  3809. wpa_s->p2p_go_ht40 ? 255 : 100, 20);
  3810. return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
  3811. go_intent, own_interface_addr, force_freq,
  3812. persistent_group, ssid ? ssid->ssid : NULL,
  3813. ssid ? ssid->ssid_len : 0,
  3814. wpa_s->p2p_pd_before_go_neg, pref_freq,
  3815. wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
  3816. 0);
  3817. }
  3818. static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
  3819. const u8 *peer_addr,
  3820. enum p2p_wps_method wps_method,
  3821. int go_intent, const u8 *own_interface_addr,
  3822. unsigned int force_freq, int persistent_group,
  3823. struct wpa_ssid *ssid, unsigned int pref_freq)
  3824. {
  3825. if (persistent_group && wpa_s->conf->persistent_reconnect)
  3826. persistent_group = 2;
  3827. return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
  3828. go_intent, own_interface_addr, force_freq,
  3829. persistent_group, ssid ? ssid->ssid : NULL,
  3830. ssid ? ssid->ssid_len : 0, pref_freq,
  3831. wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
  3832. 0);
  3833. }
  3834. static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
  3835. {
  3836. wpa_s->p2p_join_scan_count++;
  3837. wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
  3838. wpa_s->p2p_join_scan_count);
  3839. if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
  3840. wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
  3841. " for join operationg - stop join attempt",
  3842. MAC2STR(wpa_s->pending_join_iface_addr));
  3843. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3844. if (wpa_s->p2p_auto_pd) {
  3845. wpa_s->p2p_auto_pd = 0;
  3846. wpa_msg_global(wpa_s, MSG_INFO,
  3847. P2P_EVENT_PROV_DISC_FAILURE
  3848. " p2p_dev_addr=" MACSTR " status=N/A",
  3849. MAC2STR(wpa_s->pending_join_dev_addr));
  3850. return;
  3851. }
  3852. wpa_msg_global(wpa_s->parent, MSG_INFO,
  3853. P2P_EVENT_GROUP_FORMATION_FAILURE);
  3854. wpas_notify_p2p_group_formation_failure(wpa_s, "");
  3855. }
  3856. }
  3857. static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
  3858. {
  3859. int res;
  3860. unsigned int num, i;
  3861. struct wpa_used_freq_data *freqs;
  3862. if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
  3863. /* Multiple channels are supported and not all are in use */
  3864. return 0;
  3865. }
  3866. freqs = os_calloc(wpa_s->num_multichan_concurrent,
  3867. sizeof(struct wpa_used_freq_data));
  3868. if (!freqs)
  3869. return 1;
  3870. num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
  3871. wpa_s->num_multichan_concurrent);
  3872. for (i = 0; i < num; i++) {
  3873. if (freqs[i].freq == freq) {
  3874. wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
  3875. freq);
  3876. res = 0;
  3877. goto exit_free;
  3878. }
  3879. }
  3880. wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
  3881. res = 1;
  3882. exit_free:
  3883. os_free(freqs);
  3884. return res;
  3885. }
  3886. static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
  3887. const u8 *peer_dev_addr)
  3888. {
  3889. struct wpa_bss *bss;
  3890. int updated;
  3891. bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
  3892. if (bss == NULL)
  3893. return -1;
  3894. if (bss->last_update_idx < wpa_s->bss_update_idx) {
  3895. wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
  3896. "last scan");
  3897. return 0;
  3898. }
  3899. updated = os_reltime_before(&wpa_s->p2p_auto_started,
  3900. &bss->last_update);
  3901. wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
  3902. "%ld.%06ld (%supdated in last scan)",
  3903. bss->last_update.sec, bss->last_update.usec,
  3904. updated ? "": "not ");
  3905. return updated;
  3906. }
  3907. static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
  3908. struct wpa_scan_results *scan_res)
  3909. {
  3910. struct wpa_bss *bss = NULL;
  3911. int freq;
  3912. u8 iface_addr[ETH_ALEN];
  3913. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3914. if (wpa_s->global->p2p_disabled)
  3915. return;
  3916. wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
  3917. scan_res ? (int) scan_res->num : -1,
  3918. wpa_s->p2p_auto_join ? "auto_" : "");
  3919. if (scan_res)
  3920. wpas_p2p_scan_res_handler(wpa_s, scan_res);
  3921. if (wpa_s->p2p_auto_pd) {
  3922. int join = wpas_p2p_peer_go(wpa_s,
  3923. wpa_s->pending_join_dev_addr);
  3924. if (join == 0 &&
  3925. wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
  3926. wpa_s->auto_pd_scan_retry++;
  3927. bss = wpa_bss_get_bssid_latest(
  3928. wpa_s, wpa_s->pending_join_dev_addr);
  3929. if (bss) {
  3930. freq = bss->freq;
  3931. wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
  3932. "the peer " MACSTR " at %d MHz",
  3933. wpa_s->auto_pd_scan_retry,
  3934. MAC2STR(wpa_s->
  3935. pending_join_dev_addr),
  3936. freq);
  3937. wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
  3938. return;
  3939. }
  3940. }
  3941. if (join < 0)
  3942. join = 0;
  3943. wpa_s->p2p_auto_pd = 0;
  3944. wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
  3945. wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
  3946. MAC2STR(wpa_s->pending_join_dev_addr), join);
  3947. if (p2p_prov_disc_req(wpa_s->global->p2p,
  3948. wpa_s->pending_join_dev_addr, NULL,
  3949. wpa_s->pending_pd_config_methods, join,
  3950. 0, wpa_s->user_initiated_pd) < 0) {
  3951. wpa_s->p2p_auto_pd = 0;
  3952. wpa_msg_global(wpa_s, MSG_INFO,
  3953. P2P_EVENT_PROV_DISC_FAILURE
  3954. " p2p_dev_addr=" MACSTR " status=N/A",
  3955. MAC2STR(wpa_s->pending_join_dev_addr));
  3956. }
  3957. return;
  3958. }
  3959. if (wpa_s->p2p_auto_join) {
  3960. int join = wpas_p2p_peer_go(wpa_s,
  3961. wpa_s->pending_join_dev_addr);
  3962. if (join < 0) {
  3963. wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
  3964. "running a GO -> use GO Negotiation");
  3965. wpa_msg_global(wpa_s->parent, MSG_INFO,
  3966. P2P_EVENT_FALLBACK_TO_GO_NEG
  3967. "reason=peer-not-running-GO");
  3968. wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
  3969. wpa_s->p2p_pin, wpa_s->p2p_wps_method,
  3970. wpa_s->p2p_persistent_group, 0, 0, 0,
  3971. wpa_s->p2p_go_intent,
  3972. wpa_s->p2p_connect_freq,
  3973. wpa_s->p2p_persistent_id,
  3974. wpa_s->p2p_pd_before_go_neg,
  3975. wpa_s->p2p_go_ht40,
  3976. wpa_s->p2p_go_vht);
  3977. return;
  3978. }
  3979. wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
  3980. "try to join the group", join ? "" :
  3981. " in older scan");
  3982. if (!join) {
  3983. wpa_msg_global(wpa_s->parent, MSG_INFO,
  3984. P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
  3985. wpa_s->p2p_fallback_to_go_neg = 1;
  3986. }
  3987. }
  3988. freq = p2p_get_oper_freq(wpa_s->global->p2p,
  3989. wpa_s->pending_join_iface_addr);
  3990. if (freq < 0 &&
  3991. p2p_get_interface_addr(wpa_s->global->p2p,
  3992. wpa_s->pending_join_dev_addr,
  3993. iface_addr) == 0 &&
  3994. os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
  3995. && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
  3996. wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
  3997. "address for join from " MACSTR " to " MACSTR
  3998. " based on newly discovered P2P peer entry",
  3999. MAC2STR(wpa_s->pending_join_iface_addr),
  4000. MAC2STR(iface_addr));
  4001. os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
  4002. ETH_ALEN);
  4003. freq = p2p_get_oper_freq(wpa_s->global->p2p,
  4004. wpa_s->pending_join_iface_addr);
  4005. }
  4006. if (freq >= 0) {
  4007. wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
  4008. "from P2P peer table: %d MHz", freq);
  4009. }
  4010. if (wpa_s->p2p_join_ssid_len) {
  4011. wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
  4012. MACSTR " and SSID %s",
  4013. MAC2STR(wpa_s->pending_join_iface_addr),
  4014. wpa_ssid_txt(wpa_s->p2p_join_ssid,
  4015. wpa_s->p2p_join_ssid_len));
  4016. bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
  4017. wpa_s->p2p_join_ssid,
  4018. wpa_s->p2p_join_ssid_len);
  4019. }
  4020. if (!bss) {
  4021. wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
  4022. MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
  4023. bss = wpa_bss_get_bssid_latest(wpa_s,
  4024. wpa_s->pending_join_iface_addr);
  4025. }
  4026. if (bss) {
  4027. u8 dev_addr[ETH_ALEN];
  4028. freq = bss->freq;
  4029. wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
  4030. "from BSS table: %d MHz (SSID %s)", freq,
  4031. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  4032. if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
  4033. dev_addr) == 0 &&
  4034. os_memcmp(wpa_s->pending_join_dev_addr,
  4035. wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
  4036. os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
  4037. ETH_ALEN) != 0) {
  4038. wpa_printf(MSG_DEBUG,
  4039. "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
  4040. MAC2STR(dev_addr),
  4041. MAC2STR(wpa_s->pending_join_dev_addr));
  4042. os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
  4043. ETH_ALEN);
  4044. }
  4045. }
  4046. if (freq > 0) {
  4047. u16 method;
  4048. if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
  4049. wpa_msg_global(wpa_s->parent, MSG_INFO,
  4050. P2P_EVENT_GROUP_FORMATION_FAILURE
  4051. "reason=FREQ_CONFLICT");
  4052. wpas_notify_p2p_group_formation_failure(
  4053. wpa_s, "FREQ_CONFLICT");
  4054. return;
  4055. }
  4056. wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
  4057. "prior to joining an existing group (GO " MACSTR
  4058. " freq=%u MHz)",
  4059. MAC2STR(wpa_s->pending_join_dev_addr), freq);
  4060. wpa_s->pending_pd_before_join = 1;
  4061. switch (wpa_s->pending_join_wps_method) {
  4062. case WPS_PIN_DISPLAY:
  4063. method = WPS_CONFIG_KEYPAD;
  4064. break;
  4065. case WPS_PIN_KEYPAD:
  4066. method = WPS_CONFIG_DISPLAY;
  4067. break;
  4068. case WPS_PBC:
  4069. method = WPS_CONFIG_PUSHBUTTON;
  4070. break;
  4071. case WPS_P2PS:
  4072. method = WPS_CONFIG_P2PS;
  4073. break;
  4074. default:
  4075. method = 0;
  4076. break;
  4077. }
  4078. if ((p2p_get_provisioning_info(wpa_s->global->p2p,
  4079. wpa_s->pending_join_dev_addr) ==
  4080. method)) {
  4081. /*
  4082. * We have already performed provision discovery for
  4083. * joining the group. Proceed directly to join
  4084. * operation without duplicated provision discovery. */
  4085. wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
  4086. "with " MACSTR " already done - proceed to "
  4087. "join",
  4088. MAC2STR(wpa_s->pending_join_dev_addr));
  4089. wpa_s->pending_pd_before_join = 0;
  4090. goto start;
  4091. }
  4092. if (p2p_prov_disc_req(wpa_s->global->p2p,
  4093. wpa_s->pending_join_dev_addr,
  4094. NULL, method, 1,
  4095. freq, wpa_s->user_initiated_pd) < 0) {
  4096. wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
  4097. "Discovery Request before joining an "
  4098. "existing group");
  4099. wpa_s->pending_pd_before_join = 0;
  4100. goto start;
  4101. }
  4102. return;
  4103. }
  4104. wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
  4105. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  4106. eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
  4107. wpas_p2p_check_join_scan_limit(wpa_s);
  4108. return;
  4109. start:
  4110. /* Start join operation immediately */
  4111. wpas_p2p_join_start(wpa_s, 0, NULL, 0);
  4112. }
  4113. static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
  4114. const u8 *ssid, size_t ssid_len)
  4115. {
  4116. int ret;
  4117. struct wpa_driver_scan_params params;
  4118. struct wpabuf *wps_ie, *ies;
  4119. size_t ielen;
  4120. int freqs[2] = { 0, 0 };
  4121. os_memset(&params, 0, sizeof(params));
  4122. /* P2P Wildcard SSID */
  4123. params.num_ssids = 1;
  4124. if (ssid && ssid_len) {
  4125. params.ssids[0].ssid = ssid;
  4126. params.ssids[0].ssid_len = ssid_len;
  4127. os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
  4128. wpa_s->p2p_join_ssid_len = ssid_len;
  4129. } else {
  4130. params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
  4131. params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
  4132. wpa_s->p2p_join_ssid_len = 0;
  4133. }
  4134. wpa_s->wps->dev.p2p = 1;
  4135. wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
  4136. wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
  4137. NULL);
  4138. if (wps_ie == NULL) {
  4139. wpas_p2p_scan_res_join(wpa_s, NULL);
  4140. return;
  4141. }
  4142. ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
  4143. ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
  4144. if (ies == NULL) {
  4145. wpabuf_free(wps_ie);
  4146. wpas_p2p_scan_res_join(wpa_s, NULL);
  4147. return;
  4148. }
  4149. wpabuf_put_buf(ies, wps_ie);
  4150. wpabuf_free(wps_ie);
  4151. p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
  4152. params.p2p_probe = 1;
  4153. params.extra_ies = wpabuf_head(ies);
  4154. params.extra_ies_len = wpabuf_len(ies);
  4155. if (!freq) {
  4156. int oper_freq;
  4157. /*
  4158. * If freq is not provided, check the operating freq of the GO
  4159. * and use a single channel scan on if possible.
  4160. */
  4161. oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
  4162. wpa_s->pending_join_iface_addr);
  4163. if (oper_freq > 0)
  4164. freq = oper_freq;
  4165. }
  4166. if (freq > 0) {
  4167. freqs[0] = freq;
  4168. params.freqs = freqs;
  4169. }
  4170. /*
  4171. * Run a scan to update BSS table and start Provision Discovery once
  4172. * the new scan results become available.
  4173. */
  4174. ret = wpa_drv_scan(wpa_s, &params);
  4175. if (!ret) {
  4176. os_get_reltime(&wpa_s->scan_trigger_time);
  4177. wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
  4178. wpa_s->own_scan_requested = 1;
  4179. }
  4180. wpabuf_free(ies);
  4181. if (ret) {
  4182. wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
  4183. "try again later");
  4184. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  4185. eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
  4186. wpas_p2p_check_join_scan_limit(wpa_s);
  4187. }
  4188. }
  4189. static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
  4190. {
  4191. struct wpa_supplicant *wpa_s = eloop_ctx;
  4192. wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
  4193. }
  4194. static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
  4195. const u8 *dev_addr, enum p2p_wps_method wps_method,
  4196. int auto_join, int op_freq,
  4197. const u8 *ssid, size_t ssid_len)
  4198. {
  4199. wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
  4200. MACSTR " dev " MACSTR " op_freq=%d)%s",
  4201. MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
  4202. auto_join ? " (auto_join)" : "");
  4203. if (ssid && ssid_len) {
  4204. wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
  4205. wpa_ssid_txt(ssid, ssid_len));
  4206. }
  4207. wpa_s->p2p_auto_pd = 0;
  4208. wpa_s->p2p_auto_join = !!auto_join;
  4209. os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
  4210. os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
  4211. wpa_s->pending_join_wps_method = wps_method;
  4212. /* Make sure we are not running find during connection establishment */
  4213. wpas_p2p_stop_find(wpa_s);
  4214. wpa_s->p2p_join_scan_count = 0;
  4215. wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
  4216. return 0;
  4217. }
  4218. static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
  4219. const u8 *ssid, size_t ssid_len)
  4220. {
  4221. struct wpa_supplicant *group;
  4222. struct p2p_go_neg_results res;
  4223. struct wpa_bss *bss;
  4224. group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
  4225. if (group == NULL)
  4226. return -1;
  4227. if (group != wpa_s) {
  4228. os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
  4229. sizeof(group->p2p_pin));
  4230. group->p2p_wps_method = wpa_s->p2p_wps_method;
  4231. } else {
  4232. /*
  4233. * Need to mark the current interface for p2p_group_formation
  4234. * when a separate group interface is not used. This is needed
  4235. * to allow p2p_cancel stop a pending p2p_connect-join.
  4236. * wpas_p2p_init_group_interface() addresses this for the case
  4237. * where a separate group interface is used.
  4238. */
  4239. wpa_s->global->p2p_group_formation = wpa_s;
  4240. }
  4241. group->p2p_in_provisioning = 1;
  4242. group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
  4243. os_memset(&res, 0, sizeof(res));
  4244. os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
  4245. os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
  4246. ETH_ALEN);
  4247. res.wps_method = wpa_s->pending_join_wps_method;
  4248. if (freq && ssid && ssid_len) {
  4249. res.freq = freq;
  4250. res.ssid_len = ssid_len;
  4251. os_memcpy(res.ssid, ssid, ssid_len);
  4252. } else {
  4253. bss = wpa_bss_get_bssid_latest(wpa_s,
  4254. wpa_s->pending_join_iface_addr);
  4255. if (bss) {
  4256. res.freq = bss->freq;
  4257. res.ssid_len = bss->ssid_len;
  4258. os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
  4259. wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
  4260. bss->freq,
  4261. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  4262. }
  4263. }
  4264. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  4265. wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
  4266. "starting client");
  4267. wpa_drv_cancel_remain_on_channel(wpa_s);
  4268. wpa_s->off_channel_freq = 0;
  4269. wpa_s->roc_waiting_drv_freq = 0;
  4270. }
  4271. wpas_start_wps_enrollee(group, &res);
  4272. /*
  4273. * Allow a longer timeout for join-a-running-group than normal 15
  4274. * second group formation timeout since the GO may not have authorized
  4275. * our connection yet.
  4276. */
  4277. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  4278. eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
  4279. wpa_s, NULL);
  4280. return 0;
  4281. }
  4282. static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
  4283. int *force_freq, int *pref_freq, int go,
  4284. unsigned int *pref_freq_list,
  4285. unsigned int *num_pref_freq)
  4286. {
  4287. struct wpa_used_freq_data *freqs;
  4288. int res, best_freq, num_unused;
  4289. unsigned int freq_in_use = 0, num, i, max_pref_freq;
  4290. max_pref_freq = *num_pref_freq;
  4291. *num_pref_freq = 0;
  4292. freqs = os_calloc(wpa_s->num_multichan_concurrent,
  4293. sizeof(struct wpa_used_freq_data));
  4294. if (!freqs)
  4295. return -1;
  4296. num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
  4297. wpa_s->num_multichan_concurrent);
  4298. /*
  4299. * It is possible that the total number of used frequencies is bigger
  4300. * than the number of frequencies used for P2P, so get the system wide
  4301. * number of unused frequencies.
  4302. */
  4303. num_unused = wpas_p2p_num_unused_channels(wpa_s);
  4304. wpa_printf(MSG_DEBUG,
  4305. "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
  4306. freq, wpa_s->num_multichan_concurrent, num, num_unused);
  4307. if (freq > 0) {
  4308. int ret;
  4309. if (go)
  4310. ret = p2p_supported_freq(wpa_s->global->p2p, freq);
  4311. else
  4312. ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
  4313. if (!ret) {
  4314. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
  4315. ieee80211_is_dfs(freq)) {
  4316. /*
  4317. * If freq is a DFS channel and DFS is offloaded
  4318. * to the driver, allow P2P GO to use it.
  4319. */
  4320. wpa_printf(MSG_DEBUG,
  4321. "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
  4322. freq);
  4323. } else {
  4324. wpa_printf(MSG_DEBUG,
  4325. "P2P: The forced channel (%u MHz) is not supported for P2P uses",
  4326. freq);
  4327. res = -3;
  4328. goto exit_free;
  4329. }
  4330. }
  4331. for (i = 0; i < num; i++) {
  4332. if (freqs[i].freq == freq)
  4333. freq_in_use = 1;
  4334. }
  4335. if (num_unused <= 0 && !freq_in_use) {
  4336. wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
  4337. freq);
  4338. res = -2;
  4339. goto exit_free;
  4340. }
  4341. wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
  4342. "requested channel (%u MHz)", freq);
  4343. *force_freq = freq;
  4344. goto exit_ok;
  4345. }
  4346. best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
  4347. if (!wpa_s->conf->num_p2p_pref_chan && *pref_freq == 0) {
  4348. enum wpa_driver_if_type iface_type;
  4349. if (go)
  4350. iface_type = WPA_IF_P2P_GO;
  4351. else
  4352. iface_type = WPA_IF_P2P_CLIENT;
  4353. wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
  4354. best_freq, go);
  4355. res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
  4356. &max_pref_freq,
  4357. pref_freq_list);
  4358. if (!res && max_pref_freq > 0) {
  4359. *num_pref_freq = max_pref_freq;
  4360. i = 0;
  4361. while (wpas_p2p_disallowed_freq(wpa_s->global,
  4362. pref_freq_list[i]) &&
  4363. i < *num_pref_freq) {
  4364. wpa_printf(MSG_DEBUG,
  4365. "P2P: preferred_freq_list[%d]=%d is disallowed",
  4366. i, pref_freq_list[i]);
  4367. i++;
  4368. }
  4369. if (i != *num_pref_freq) {
  4370. best_freq = pref_freq_list[i];
  4371. wpa_printf(MSG_DEBUG,
  4372. "P2P: Using preferred_freq_list[%d]=%d",
  4373. i, best_freq);
  4374. } else {
  4375. wpa_printf(MSG_DEBUG,
  4376. "P2P: All driver preferred frequencies are disallowed for P2P use");
  4377. *num_pref_freq = 0;
  4378. }
  4379. } else {
  4380. wpa_printf(MSG_DEBUG,
  4381. "P2P: No preferred frequency list available");
  4382. }
  4383. }
  4384. /* We have a candidate frequency to use */
  4385. if (best_freq > 0) {
  4386. if (*pref_freq == 0 && num_unused > 0) {
  4387. wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
  4388. best_freq);
  4389. *pref_freq = best_freq;
  4390. } else {
  4391. wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
  4392. best_freq);
  4393. *force_freq = best_freq;
  4394. }
  4395. } else if (num_unused > 0) {
  4396. wpa_printf(MSG_DEBUG,
  4397. "P2P: Current operating channels are not available for P2P. Try to use another channel");
  4398. *force_freq = 0;
  4399. } else {
  4400. wpa_printf(MSG_DEBUG,
  4401. "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
  4402. res = -2;
  4403. goto exit_free;
  4404. }
  4405. exit_ok:
  4406. res = 0;
  4407. exit_free:
  4408. os_free(freqs);
  4409. return res;
  4410. }
  4411. /**
  4412. * wpas_p2p_connect - Request P2P Group Formation to be started
  4413. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  4414. * @peer_addr: Address of the peer P2P Device
  4415. * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
  4416. * @persistent_group: Whether to create a persistent group
  4417. * @auto_join: Whether to select join vs. GO Negotiation automatically
  4418. * @join: Whether to join an existing group (as a client) instead of starting
  4419. * Group Owner negotiation; @peer_addr is BSSID in that case
  4420. * @auth: Whether to only authorize the connection instead of doing that and
  4421. * initiating Group Owner negotiation
  4422. * @go_intent: GO Intent or -1 to use default
  4423. * @freq: Frequency for the group or 0 for auto-selection
  4424. * @persistent_id: Persistent group credentials to use for forcing GO
  4425. * parameters or -1 to generate new values (SSID/passphrase)
  4426. * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
  4427. * interoperability workaround when initiating group formation
  4428. * @ht40: Start GO with 40 MHz channel width
  4429. * @vht: Start GO with VHT support
  4430. * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
  4431. * failure, -2 on failure due to channel not currently available,
  4432. * -3 if forced channel is not supported
  4433. */
  4434. int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  4435. const char *pin, enum p2p_wps_method wps_method,
  4436. int persistent_group, int auto_join, int join, int auth,
  4437. int go_intent, int freq, int persistent_id, int pd,
  4438. int ht40, int vht)
  4439. {
  4440. int force_freq = 0, pref_freq = 0;
  4441. int ret = 0, res;
  4442. enum wpa_driver_if_type iftype;
  4443. const u8 *if_addr;
  4444. struct wpa_ssid *ssid = NULL;
  4445. unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
  4446. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4447. return -1;
  4448. if (persistent_id >= 0) {
  4449. ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
  4450. if (ssid == NULL || ssid->disabled != 2 ||
  4451. ssid->mode != WPAS_MODE_P2P_GO)
  4452. return -1;
  4453. }
  4454. os_free(wpa_s->global->add_psk);
  4455. wpa_s->global->add_psk = NULL;
  4456. wpa_s->global->p2p_fail_on_wps_complete = 0;
  4457. wpa_s->global->pending_p2ps_group = 0;
  4458. wpa_s->global->pending_p2ps_group_freq = 0;
  4459. wpa_s->p2ps_method_config_any = 0;
  4460. if (go_intent < 0)
  4461. go_intent = wpa_s->conf->p2p_go_intent;
  4462. if (!auth)
  4463. wpa_s->p2p_long_listen = 0;
  4464. wpa_s->p2p_wps_method = wps_method;
  4465. wpa_s->p2p_persistent_group = !!persistent_group;
  4466. wpa_s->p2p_persistent_id = persistent_id;
  4467. wpa_s->p2p_go_intent = go_intent;
  4468. wpa_s->p2p_connect_freq = freq;
  4469. wpa_s->p2p_fallback_to_go_neg = 0;
  4470. wpa_s->p2p_pd_before_go_neg = !!pd;
  4471. wpa_s->p2p_go_ht40 = !!ht40;
  4472. wpa_s->p2p_go_vht = !!vht;
  4473. if (pin)
  4474. os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
  4475. else if (wps_method == WPS_PIN_DISPLAY) {
  4476. ret = wps_generate_pin();
  4477. res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
  4478. "%08d", ret);
  4479. if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
  4480. wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
  4481. wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
  4482. wpa_s->p2p_pin);
  4483. } else
  4484. wpa_s->p2p_pin[0] = '\0';
  4485. if (join || auto_join) {
  4486. u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
  4487. if (auth) {
  4488. wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
  4489. "connect a running group from " MACSTR,
  4490. MAC2STR(peer_addr));
  4491. os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
  4492. return ret;
  4493. }
  4494. os_memcpy(dev_addr, peer_addr, ETH_ALEN);
  4495. if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
  4496. iface_addr) < 0) {
  4497. os_memcpy(iface_addr, peer_addr, ETH_ALEN);
  4498. p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
  4499. dev_addr);
  4500. }
  4501. if (auto_join) {
  4502. os_get_reltime(&wpa_s->p2p_auto_started);
  4503. wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
  4504. "%ld.%06ld",
  4505. wpa_s->p2p_auto_started.sec,
  4506. wpa_s->p2p_auto_started.usec);
  4507. }
  4508. wpa_s->user_initiated_pd = 1;
  4509. if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
  4510. auto_join, freq, NULL, 0) < 0)
  4511. return -1;
  4512. return ret;
  4513. }
  4514. size = P2P_MAX_PREF_CHANNELS;
  4515. res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
  4516. go_intent == 15, pref_freq_list, &size);
  4517. if (res)
  4518. return res;
  4519. wpas_p2p_set_own_freq_preference(wpa_s,
  4520. force_freq ? force_freq : pref_freq);
  4521. p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
  4522. wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
  4523. if (wpa_s->create_p2p_iface) {
  4524. /* Prepare to add a new interface for the group */
  4525. iftype = WPA_IF_P2P_GROUP;
  4526. if (go_intent == 15)
  4527. iftype = WPA_IF_P2P_GO;
  4528. if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
  4529. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  4530. "interface for the group");
  4531. return -1;
  4532. }
  4533. if_addr = wpa_s->pending_interface_addr;
  4534. } else {
  4535. if_addr = wpa_s->own_addr;
  4536. os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
  4537. }
  4538. if (auth) {
  4539. if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
  4540. go_intent, if_addr,
  4541. force_freq, persistent_group, ssid,
  4542. pref_freq) < 0)
  4543. return -1;
  4544. return ret;
  4545. }
  4546. if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
  4547. go_intent, if_addr, force_freq,
  4548. persistent_group, ssid, pref_freq) < 0) {
  4549. if (wpa_s->create_p2p_iface)
  4550. wpas_p2p_remove_pending_group_interface(wpa_s);
  4551. return -1;
  4552. }
  4553. return ret;
  4554. }
  4555. /**
  4556. * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
  4557. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  4558. * @freq: Frequency of the channel in MHz
  4559. * @duration: Duration of the stay on the channel in milliseconds
  4560. *
  4561. * This callback is called when the driver indicates that it has started the
  4562. * requested remain-on-channel duration.
  4563. */
  4564. void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  4565. unsigned int freq, unsigned int duration)
  4566. {
  4567. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4568. return;
  4569. 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)",
  4570. wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
  4571. wpa_s->roc_waiting_drv_freq, freq, duration);
  4572. if (wpa_s->off_channel_freq &&
  4573. wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
  4574. p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
  4575. wpa_s->pending_listen_duration);
  4576. wpa_s->pending_listen_freq = 0;
  4577. } else {
  4578. wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
  4579. wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
  4580. freq, duration);
  4581. }
  4582. }
  4583. int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
  4584. {
  4585. /* Limit maximum Listen state time based on driver limitation. */
  4586. if (timeout > wpa_s->max_remain_on_chan)
  4587. timeout = wpa_s->max_remain_on_chan;
  4588. return p2p_listen(wpa_s->global->p2p, timeout);
  4589. }
  4590. /**
  4591. * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
  4592. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  4593. * @freq: Frequency of the channel in MHz
  4594. *
  4595. * This callback is called when the driver indicates that a remain-on-channel
  4596. * operation has been completed, i.e., the duration on the requested channel
  4597. * has timed out.
  4598. */
  4599. void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  4600. unsigned int freq)
  4601. {
  4602. wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
  4603. "(p2p_long_listen=%d ms pending_action_tx=%p)",
  4604. wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
  4605. wpas_p2p_listen_work_done(wpa_s);
  4606. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4607. return;
  4608. if (wpa_s->p2p_long_listen > 0)
  4609. wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
  4610. if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
  4611. return; /* P2P module started a new operation */
  4612. if (offchannel_pending_action_tx(wpa_s))
  4613. return;
  4614. if (wpa_s->p2p_long_listen > 0) {
  4615. wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
  4616. wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
  4617. } else {
  4618. /*
  4619. * When listen duration is over, stop listen & update p2p_state
  4620. * to IDLE.
  4621. */
  4622. p2p_stop_listen(wpa_s->global->p2p);
  4623. }
  4624. }
  4625. /**
  4626. * wpas_p2p_group_remove - Remove a P2P group
  4627. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  4628. * @ifname: Network interface name of the group interface or "*" to remove all
  4629. * groups
  4630. * Returns: 0 on success, -1 on failure
  4631. *
  4632. * This function is used to remove a P2P group. This can be used to disconnect
  4633. * from a group in which the local end is a P2P Client or to end a P2P Group in
  4634. * case the local end is the Group Owner. If a virtual network interface was
  4635. * created for this group, that interface will be removed. Otherwise, only the
  4636. * configured P2P group network will be removed from the interface.
  4637. */
  4638. int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
  4639. {
  4640. struct wpa_global *global = wpa_s->global;
  4641. struct wpa_supplicant *calling_wpa_s = wpa_s;
  4642. if (os_strcmp(ifname, "*") == 0) {
  4643. struct wpa_supplicant *prev;
  4644. wpa_s = global->ifaces;
  4645. while (wpa_s) {
  4646. prev = wpa_s;
  4647. wpa_s = wpa_s->next;
  4648. if (prev->p2p_group_interface !=
  4649. NOT_P2P_GROUP_INTERFACE ||
  4650. (prev->current_ssid &&
  4651. prev->current_ssid->p2p_group))
  4652. wpas_p2p_disconnect_safely(prev, calling_wpa_s);
  4653. }
  4654. return 0;
  4655. }
  4656. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  4657. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  4658. break;
  4659. }
  4660. return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
  4661. }
  4662. static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
  4663. {
  4664. unsigned int r;
  4665. if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
  4666. unsigned int i, size = P2P_MAX_PREF_CHANNELS;
  4667. unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS];
  4668. int res;
  4669. res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
  4670. &size, pref_freq_list);
  4671. if (!res && size > 0) {
  4672. i = 0;
  4673. while (wpas_p2p_disallowed_freq(wpa_s->global,
  4674. pref_freq_list[i]) &&
  4675. i < size) {
  4676. wpa_printf(MSG_DEBUG,
  4677. "P2P: preferred_freq_list[%d]=%d is disallowed",
  4678. i, pref_freq_list[i]);
  4679. i++;
  4680. }
  4681. if (i != size) {
  4682. freq = pref_freq_list[i];
  4683. wpa_printf(MSG_DEBUG,
  4684. "P2P: Using preferred_freq_list[%d]=%d",
  4685. i, freq);
  4686. } else {
  4687. wpa_printf(MSG_DEBUG,
  4688. "P2P: All driver preferred frequencies are disallowed for P2P use");
  4689. }
  4690. } else {
  4691. wpa_printf(MSG_DEBUG,
  4692. "P2P: No preferred frequency list available");
  4693. }
  4694. }
  4695. if (freq == 2) {
  4696. wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
  4697. "band");
  4698. if (wpa_s->best_24_freq > 0 &&
  4699. p2p_supported_freq_go(wpa_s->global->p2p,
  4700. wpa_s->best_24_freq)) {
  4701. freq = wpa_s->best_24_freq;
  4702. wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
  4703. "channel: %d MHz", freq);
  4704. } else {
  4705. if (os_get_random((u8 *) &r, sizeof(r)) < 0)
  4706. return -1;
  4707. freq = 2412 + (r % 3) * 25;
  4708. wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
  4709. "channel: %d MHz", freq);
  4710. }
  4711. }
  4712. if (freq == 5) {
  4713. wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
  4714. "band");
  4715. if (wpa_s->best_5_freq > 0 &&
  4716. p2p_supported_freq_go(wpa_s->global->p2p,
  4717. wpa_s->best_5_freq)) {
  4718. freq = wpa_s->best_5_freq;
  4719. wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
  4720. "channel: %d MHz", freq);
  4721. } else {
  4722. if (os_get_random((u8 *) &r, sizeof(r)) < 0)
  4723. return -1;
  4724. freq = 5180 + (r % 4) * 20;
  4725. if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
  4726. wpa_printf(MSG_DEBUG, "P2P: Could not select "
  4727. "5 GHz channel for P2P group");
  4728. return -1;
  4729. }
  4730. wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
  4731. "channel: %d MHz", freq);
  4732. }
  4733. }
  4734. if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
  4735. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
  4736. ieee80211_is_dfs(freq)) {
  4737. /*
  4738. * If freq is a DFS channel and DFS is offloaded to the
  4739. * driver, allow P2P GO to use it.
  4740. */
  4741. wpa_printf(MSG_DEBUG, "P2P: "
  4742. "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
  4743. __func__, freq);
  4744. return freq;
  4745. }
  4746. wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
  4747. "(%u MHz) is not supported for P2P uses",
  4748. freq);
  4749. return -1;
  4750. }
  4751. return freq;
  4752. }
  4753. static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
  4754. const struct p2p_channels *channels,
  4755. int freq)
  4756. {
  4757. if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
  4758. p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
  4759. freq_included(wpa_s, channels, freq))
  4760. return 1;
  4761. return 0;
  4762. }
  4763. static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
  4764. struct p2p_go_neg_results *params,
  4765. const struct p2p_channels *channels)
  4766. {
  4767. unsigned int i, r;
  4768. /* first try some random selection of the social channels */
  4769. if (os_get_random((u8 *) &r, sizeof(r)) < 0)
  4770. return;
  4771. for (i = 0; i < 3; i++) {
  4772. params->freq = 2412 + ((r + i) % 3) * 25;
  4773. if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
  4774. goto out;
  4775. }
  4776. /* try all other channels in operating class 81 */
  4777. for (i = 0; i < 11; i++) {
  4778. params->freq = 2412 + i * 5;
  4779. /* skip social channels; covered in the previous loop */
  4780. if (params->freq == 2412 ||
  4781. params->freq == 2437 ||
  4782. params->freq == 2462)
  4783. continue;
  4784. if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
  4785. goto out;
  4786. }
  4787. /* try all channels in operating class 115 */
  4788. for (i = 0; i < 4; i++) {
  4789. params->freq = 5180 + i * 20;
  4790. if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
  4791. freq_included(wpa_s, channels, params->freq) &&
  4792. p2p_supported_freq(wpa_s->global->p2p, params->freq))
  4793. goto out;
  4794. }
  4795. /* try all channels in operating class 124 */
  4796. for (i = 0; i < 4; i++) {
  4797. params->freq = 5745 + i * 20;
  4798. if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
  4799. freq_included(wpa_s, channels, params->freq) &&
  4800. p2p_supported_freq(wpa_s->global->p2p, params->freq))
  4801. goto out;
  4802. }
  4803. /* try social channel class 180 channel 2 */
  4804. params->freq = 58320 + 1 * 2160;
  4805. if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
  4806. freq_included(wpa_s, channels, params->freq) &&
  4807. p2p_supported_freq(wpa_s->global->p2p, params->freq))
  4808. goto out;
  4809. /* try all channels in reg. class 180 */
  4810. for (i = 0; i < 4; i++) {
  4811. params->freq = 58320 + i * 2160;
  4812. if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
  4813. freq_included(wpa_s, channels, params->freq) &&
  4814. p2p_supported_freq(wpa_s->global->p2p, params->freq))
  4815. goto out;
  4816. }
  4817. params->freq = 0;
  4818. wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
  4819. return;
  4820. out:
  4821. wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
  4822. params->freq);
  4823. }
  4824. static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
  4825. struct p2p_go_neg_results *params,
  4826. int freq, int ht40, int vht,
  4827. const struct p2p_channels *channels)
  4828. {
  4829. struct wpa_used_freq_data *freqs;
  4830. unsigned int cand;
  4831. unsigned int num, i;
  4832. int ignore_no_freqs = 0;
  4833. os_memset(params, 0, sizeof(*params));
  4834. params->role_go = 1;
  4835. params->ht40 = ht40;
  4836. params->vht = vht;
  4837. freqs = os_calloc(wpa_s->num_multichan_concurrent,
  4838. sizeof(struct wpa_used_freq_data));
  4839. if (!freqs)
  4840. return -1;
  4841. num = get_shared_radio_freqs_data(wpa_s, freqs,
  4842. wpa_s->num_multichan_concurrent);
  4843. if (wpa_s->current_ssid &&
  4844. wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
  4845. wpa_s->wpa_state == WPA_COMPLETED) {
  4846. wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
  4847. __func__);
  4848. /*
  4849. * If the frequency selection is done for an active P2P GO that
  4850. * is not sharing a frequency, allow to select a new frequency
  4851. * even if there are no unused frequencies as we are about to
  4852. * move the P2P GO so its frequency can be re-used.
  4853. */
  4854. for (i = 0; i < num; i++) {
  4855. if (freqs[i].freq == wpa_s->current_ssid->frequency &&
  4856. freqs[i].flags == 0) {
  4857. ignore_no_freqs = 1;
  4858. break;
  4859. }
  4860. }
  4861. }
  4862. /* try using the forced freq */
  4863. if (freq) {
  4864. if (!wpas_p2p_supported_freq_go(wpa_s, channels, freq)) {
  4865. wpa_printf(MSG_DEBUG,
  4866. "P2P: Forced GO freq %d MHz not accepted",
  4867. freq);
  4868. goto fail;
  4869. }
  4870. for (i = 0; i < num; i++) {
  4871. if (freqs[i].freq == freq) {
  4872. wpa_printf(MSG_DEBUG,
  4873. "P2P: forced freq (%d MHz) is also shared",
  4874. freq);
  4875. params->freq = freq;
  4876. goto success;
  4877. }
  4878. }
  4879. if (!ignore_no_freqs &&
  4880. wpas_p2p_num_unused_channels(wpa_s) <= 0) {
  4881. wpa_printf(MSG_DEBUG,
  4882. "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
  4883. freq);
  4884. goto fail;
  4885. }
  4886. wpa_printf(MSG_DEBUG,
  4887. "P2P: force GO freq (%d MHz) on a free channel",
  4888. freq);
  4889. params->freq = freq;
  4890. goto success;
  4891. }
  4892. /* consider using one of the shared frequencies */
  4893. if (num) {
  4894. cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
  4895. if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
  4896. wpa_printf(MSG_DEBUG,
  4897. "P2P: Use shared freq (%d MHz) for GO",
  4898. freq);
  4899. params->freq = cand;
  4900. goto success;
  4901. }
  4902. /* try using one of the shared freqs */
  4903. for (i = 0; i < num; i++) {
  4904. if (wpas_p2p_supported_freq_go(wpa_s, channels,
  4905. freqs[i].freq)) {
  4906. wpa_printf(MSG_DEBUG,
  4907. "P2P: Use shared freq (%d MHz) for GO",
  4908. freq);
  4909. params->freq = freqs[i].freq;
  4910. goto success;
  4911. }
  4912. }
  4913. }
  4914. if (!ignore_no_freqs &&
  4915. wpas_p2p_num_unused_channels(wpa_s) <= 0) {
  4916. wpa_printf(MSG_DEBUG,
  4917. "P2P: Cannot force GO on any of the channels we are already using");
  4918. goto fail;
  4919. }
  4920. /* try using the setting from the configuration file */
  4921. if (wpa_s->conf->p2p_oper_reg_class == 81 &&
  4922. wpa_s->conf->p2p_oper_channel >= 1 &&
  4923. wpa_s->conf->p2p_oper_channel <= 11 &&
  4924. wpas_p2p_supported_freq_go(
  4925. wpa_s, channels,
  4926. 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
  4927. params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
  4928. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
  4929. "frequency %d MHz", params->freq);
  4930. goto success;
  4931. }
  4932. if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
  4933. wpa_s->conf->p2p_oper_reg_class == 116 ||
  4934. wpa_s->conf->p2p_oper_reg_class == 117 ||
  4935. wpa_s->conf->p2p_oper_reg_class == 124 ||
  4936. wpa_s->conf->p2p_oper_reg_class == 125 ||
  4937. wpa_s->conf->p2p_oper_reg_class == 126 ||
  4938. wpa_s->conf->p2p_oper_reg_class == 127) &&
  4939. wpas_p2p_supported_freq_go(wpa_s, channels,
  4940. 5000 +
  4941. 5 * wpa_s->conf->p2p_oper_channel)) {
  4942. params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
  4943. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
  4944. "frequency %d MHz", params->freq);
  4945. goto success;
  4946. }
  4947. /* Try using best channels */
  4948. if (wpa_s->conf->p2p_oper_channel == 0 &&
  4949. wpa_s->best_overall_freq > 0 &&
  4950. wpas_p2p_supported_freq_go(wpa_s, channels,
  4951. wpa_s->best_overall_freq)) {
  4952. params->freq = wpa_s->best_overall_freq;
  4953. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
  4954. "channel %d MHz", params->freq);
  4955. goto success;
  4956. }
  4957. if (wpa_s->conf->p2p_oper_channel == 0 &&
  4958. wpa_s->best_24_freq > 0 &&
  4959. wpas_p2p_supported_freq_go(wpa_s, channels,
  4960. wpa_s->best_24_freq)) {
  4961. params->freq = wpa_s->best_24_freq;
  4962. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
  4963. "channel %d MHz", params->freq);
  4964. goto success;
  4965. }
  4966. if (wpa_s->conf->p2p_oper_channel == 0 &&
  4967. wpa_s->best_5_freq > 0 &&
  4968. wpas_p2p_supported_freq_go(wpa_s, channels,
  4969. wpa_s->best_5_freq)) {
  4970. params->freq = wpa_s->best_5_freq;
  4971. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
  4972. "channel %d MHz", params->freq);
  4973. goto success;
  4974. }
  4975. /* try using preferred channels */
  4976. cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
  4977. if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
  4978. params->freq = cand;
  4979. wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
  4980. "channels", params->freq);
  4981. goto success;
  4982. }
  4983. /* Try using one of the group common freqs */
  4984. if (wpa_s->p2p_group_common_freqs) {
  4985. for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
  4986. cand = wpa_s->p2p_group_common_freqs[i];
  4987. if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
  4988. params->freq = cand;
  4989. wpa_printf(MSG_DEBUG,
  4990. "P2P: Use freq %d MHz common with the peer",
  4991. params->freq);
  4992. goto success;
  4993. }
  4994. }
  4995. }
  4996. /* no preference, select some channel */
  4997. wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
  4998. if (params->freq == 0) {
  4999. wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
  5000. goto fail;
  5001. }
  5002. success:
  5003. os_free(freqs);
  5004. return 0;
  5005. fail:
  5006. os_free(freqs);
  5007. return -1;
  5008. }
  5009. static struct wpa_supplicant *
  5010. wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
  5011. int go)
  5012. {
  5013. struct wpa_supplicant *group_wpa_s;
  5014. if (!wpas_p2p_create_iface(wpa_s)) {
  5015. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
  5016. "operations");
  5017. wpa_s->p2p_first_connection_timeout = 0;
  5018. return wpa_s;
  5019. }
  5020. if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
  5021. WPA_IF_P2P_CLIENT) < 0) {
  5022. wpa_msg_global(wpa_s, MSG_ERROR,
  5023. "P2P: Failed to add group interface");
  5024. return NULL;
  5025. }
  5026. group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
  5027. if (group_wpa_s == NULL) {
  5028. wpa_msg_global(wpa_s, MSG_ERROR,
  5029. "P2P: Failed to initialize group interface");
  5030. wpas_p2p_remove_pending_group_interface(wpa_s);
  5031. return NULL;
  5032. }
  5033. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
  5034. group_wpa_s->ifname);
  5035. group_wpa_s->p2p_first_connection_timeout = 0;
  5036. return group_wpa_s;
  5037. }
  5038. /**
  5039. * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
  5040. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  5041. * @persistent_group: Whether to create a persistent group
  5042. * @freq: Frequency for the group or 0 to indicate no hardcoding
  5043. * @ht40: Start GO with 40 MHz channel width
  5044. * @vht: Start GO with VHT support
  5045. * Returns: 0 on success, -1 on failure
  5046. *
  5047. * This function creates a new P2P group with the local end as the Group Owner,
  5048. * i.e., without using Group Owner Negotiation.
  5049. */
  5050. int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
  5051. int freq, int ht40, int vht)
  5052. {
  5053. struct p2p_go_neg_results params;
  5054. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5055. return -1;
  5056. os_free(wpa_s->global->add_psk);
  5057. wpa_s->global->add_psk = NULL;
  5058. /* Make sure we are not running find during connection establishment */
  5059. wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
  5060. wpas_p2p_stop_find_oper(wpa_s);
  5061. freq = wpas_p2p_select_go_freq(wpa_s, freq);
  5062. if (freq < 0)
  5063. return -1;
  5064. if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, NULL))
  5065. return -1;
  5066. if (params.freq &&
  5067. !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
  5068. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
  5069. ieee80211_is_dfs(params.freq)) {
  5070. /*
  5071. * If freq is a DFS channel and DFS is offloaded to the
  5072. * driver, allow P2P GO to use it.
  5073. */
  5074. wpa_printf(MSG_DEBUG,
  5075. "P2P: %s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to driver",
  5076. __func__, params.freq);
  5077. } else {
  5078. wpa_printf(MSG_DEBUG,
  5079. "P2P: The selected channel for GO (%u MHz) is not supported for P2P uses",
  5080. params.freq);
  5081. return -1;
  5082. }
  5083. }
  5084. p2p_go_params(wpa_s->global->p2p, &params);
  5085. params.persistent_group = persistent_group;
  5086. wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
  5087. if (wpa_s == NULL)
  5088. return -1;
  5089. wpas_start_wps_go(wpa_s, &params, 0);
  5090. return 0;
  5091. }
  5092. static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
  5093. struct wpa_ssid *params, int addr_allocated,
  5094. int freq, int force_scan)
  5095. {
  5096. struct wpa_ssid *ssid;
  5097. wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
  5098. if (wpa_s == NULL)
  5099. return -1;
  5100. if (force_scan)
  5101. os_get_reltime(&wpa_s->scan_min_time);
  5102. wpa_s->p2p_last_4way_hs_fail = NULL;
  5103. wpa_supplicant_ap_deinit(wpa_s);
  5104. ssid = wpa_config_add_network(wpa_s->conf);
  5105. if (ssid == NULL)
  5106. return -1;
  5107. os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
  5108. wpa_config_set_network_defaults(ssid);
  5109. ssid->temporary = 1;
  5110. ssid->proto = WPA_PROTO_RSN;
  5111. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  5112. ssid->group_cipher = WPA_CIPHER_CCMP;
  5113. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  5114. ssid->ssid = os_malloc(params->ssid_len);
  5115. if (ssid->ssid == NULL) {
  5116. wpa_config_remove_network(wpa_s->conf, ssid->id);
  5117. return -1;
  5118. }
  5119. os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
  5120. ssid->ssid_len = params->ssid_len;
  5121. ssid->p2p_group = 1;
  5122. ssid->export_keys = 1;
  5123. if (params->psk_set) {
  5124. os_memcpy(ssid->psk, params->psk, 32);
  5125. ssid->psk_set = 1;
  5126. }
  5127. if (params->passphrase)
  5128. ssid->passphrase = os_strdup(params->passphrase);
  5129. wpa_s->show_group_started = 1;
  5130. wpa_s->p2p_in_invitation = 1;
  5131. wpa_s->p2p_invite_go_freq = freq;
  5132. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
  5133. NULL);
  5134. eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
  5135. wpas_p2p_group_formation_timeout,
  5136. wpa_s->parent, NULL);
  5137. wpa_supplicant_select_network(wpa_s, ssid);
  5138. return 0;
  5139. }
  5140. int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
  5141. struct wpa_ssid *ssid, int addr_allocated,
  5142. int force_freq, int neg_freq, int ht40,
  5143. int vht, const struct p2p_channels *channels,
  5144. int connection_timeout, int force_scan)
  5145. {
  5146. struct p2p_go_neg_results params;
  5147. int go = 0, freq;
  5148. if (ssid->disabled != 2 || ssid->ssid == NULL)
  5149. return -1;
  5150. if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
  5151. go == (ssid->mode == WPAS_MODE_P2P_GO)) {
  5152. wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
  5153. "already running");
  5154. if (go == 0 &&
  5155. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  5156. wpa_s->parent, NULL)) {
  5157. /*
  5158. * This can happen if Invitation Response frame was lost
  5159. * and the peer (GO of a persistent group) tries to
  5160. * invite us again. Reschedule the timeout to avoid
  5161. * terminating the wait for the connection too early
  5162. * since we now know that the peer is still trying to
  5163. * invite us instead of having already started the GO.
  5164. */
  5165. wpa_printf(MSG_DEBUG,
  5166. "P2P: Reschedule group formation timeout since peer is still trying to invite us");
  5167. eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
  5168. wpas_p2p_group_formation_timeout,
  5169. wpa_s->parent, NULL);
  5170. }
  5171. return 0;
  5172. }
  5173. os_free(wpa_s->global->add_psk);
  5174. wpa_s->global->add_psk = NULL;
  5175. /* Make sure we are not running find during connection establishment */
  5176. wpas_p2p_stop_find_oper(wpa_s);
  5177. wpa_s->p2p_fallback_to_go_neg = 0;
  5178. if (ssid->mode == WPAS_MODE_P2P_GO) {
  5179. if (force_freq > 0) {
  5180. freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
  5181. if (freq < 0)
  5182. return -1;
  5183. } else {
  5184. freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
  5185. if (freq < 0 ||
  5186. (freq > 0 && !freq_included(wpa_s, channels, freq)))
  5187. freq = 0;
  5188. }
  5189. } else if (ssid->mode == WPAS_MODE_INFRA) {
  5190. freq = neg_freq;
  5191. if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
  5192. struct os_reltime now;
  5193. struct wpa_bss *bss =
  5194. wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
  5195. os_get_reltime(&now);
  5196. if (bss &&
  5197. !os_reltime_expired(&now, &bss->last_update, 5) &&
  5198. freq_included(wpa_s, channels, bss->freq))
  5199. freq = bss->freq;
  5200. else
  5201. freq = 0;
  5202. }
  5203. return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
  5204. force_scan);
  5205. } else {
  5206. return -1;
  5207. }
  5208. if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, channels))
  5209. return -1;
  5210. params.role_go = 1;
  5211. params.psk_set = ssid->psk_set;
  5212. if (params.psk_set)
  5213. os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
  5214. if (ssid->passphrase) {
  5215. if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
  5216. wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
  5217. "persistent group");
  5218. return -1;
  5219. }
  5220. os_strlcpy(params.passphrase, ssid->passphrase,
  5221. sizeof(params.passphrase));
  5222. }
  5223. os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
  5224. params.ssid_len = ssid->ssid_len;
  5225. params.persistent_group = 1;
  5226. wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
  5227. if (wpa_s == NULL)
  5228. return -1;
  5229. p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
  5230. wpa_s->p2p_first_connection_timeout = connection_timeout;
  5231. wpas_start_wps_go(wpa_s, &params, 0);
  5232. return 0;
  5233. }
  5234. static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
  5235. struct wpabuf *proberesp_ies)
  5236. {
  5237. struct wpa_supplicant *wpa_s = ctx;
  5238. if (wpa_s->ap_iface) {
  5239. struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
  5240. if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
  5241. wpabuf_free(beacon_ies);
  5242. wpabuf_free(proberesp_ies);
  5243. return;
  5244. }
  5245. if (beacon_ies) {
  5246. wpabuf_free(hapd->p2p_beacon_ie);
  5247. hapd->p2p_beacon_ie = beacon_ies;
  5248. }
  5249. wpabuf_free(hapd->p2p_probe_resp_ie);
  5250. hapd->p2p_probe_resp_ie = proberesp_ies;
  5251. } else {
  5252. wpabuf_free(beacon_ies);
  5253. wpabuf_free(proberesp_ies);
  5254. }
  5255. wpa_supplicant_ap_update_beacon(wpa_s);
  5256. }
  5257. static void wpas_p2p_idle_update(void *ctx, int idle)
  5258. {
  5259. struct wpa_supplicant *wpa_s = ctx;
  5260. if (!wpa_s->ap_iface)
  5261. return;
  5262. wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
  5263. if (idle) {
  5264. if (wpa_s->global->p2p_fail_on_wps_complete &&
  5265. wpa_s->p2p_in_provisioning) {
  5266. wpas_p2p_grpform_fail_after_wps(wpa_s);
  5267. return;
  5268. }
  5269. wpas_p2p_set_group_idle_timeout(wpa_s);
  5270. } else
  5271. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  5272. }
  5273. struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
  5274. struct wpa_ssid *ssid)
  5275. {
  5276. struct p2p_group *group;
  5277. struct p2p_group_config *cfg;
  5278. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5279. return NULL;
  5280. cfg = os_zalloc(sizeof(*cfg));
  5281. if (cfg == NULL)
  5282. return NULL;
  5283. if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
  5284. cfg->persistent_group = 2;
  5285. else if (ssid->p2p_persistent_group)
  5286. cfg->persistent_group = 1;
  5287. os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
  5288. if (wpa_s->max_stations &&
  5289. wpa_s->max_stations < wpa_s->conf->max_num_sta)
  5290. cfg->max_clients = wpa_s->max_stations;
  5291. else
  5292. cfg->max_clients = wpa_s->conf->max_num_sta;
  5293. os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
  5294. cfg->ssid_len = ssid->ssid_len;
  5295. cfg->freq = ssid->frequency;
  5296. cfg->cb_ctx = wpa_s;
  5297. cfg->ie_update = wpas_p2p_ie_update;
  5298. cfg->idle_update = wpas_p2p_idle_update;
  5299. group = p2p_group_init(wpa_s->global->p2p, cfg);
  5300. if (group == NULL)
  5301. os_free(cfg);
  5302. if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
  5303. p2p_group_notif_formation_done(group);
  5304. wpa_s->p2p_group = group;
  5305. return group;
  5306. }
  5307. void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  5308. int registrar)
  5309. {
  5310. struct wpa_ssid *ssid = wpa_s->current_ssid;
  5311. if (!wpa_s->p2p_in_provisioning) {
  5312. wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
  5313. "provisioning not in progress");
  5314. return;
  5315. }
  5316. if (ssid && ssid->mode == WPAS_MODE_INFRA) {
  5317. u8 go_dev_addr[ETH_ALEN];
  5318. os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
  5319. wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
  5320. ssid->ssid_len);
  5321. /* Clear any stored provisioning info */
  5322. p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
  5323. }
  5324. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
  5325. NULL);
  5326. wpa_s->p2p_go_group_formation_completed = 1;
  5327. if (ssid && ssid->mode == WPAS_MODE_INFRA) {
  5328. /*
  5329. * Use a separate timeout for initial data connection to
  5330. * complete to allow the group to be removed automatically if
  5331. * something goes wrong in this step before the P2P group idle
  5332. * timeout mechanism is taken into use.
  5333. */
  5334. wpa_dbg(wpa_s, MSG_DEBUG,
  5335. "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
  5336. P2P_MAX_INITIAL_CONN_WAIT);
  5337. eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
  5338. wpas_p2p_group_formation_timeout,
  5339. wpa_s->parent, NULL);
  5340. } else if (ssid) {
  5341. /*
  5342. * Use a separate timeout for initial data connection to
  5343. * complete to allow the group to be removed automatically if
  5344. * the client does not complete data connection successfully.
  5345. */
  5346. wpa_dbg(wpa_s, MSG_DEBUG,
  5347. "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
  5348. P2P_MAX_INITIAL_CONN_WAIT_GO);
  5349. eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
  5350. wpas_p2p_group_formation_timeout,
  5351. wpa_s->parent, NULL);
  5352. /*
  5353. * Complete group formation on first successful data connection
  5354. */
  5355. wpa_s->p2p_go_group_formation_completed = 0;
  5356. }
  5357. if (wpa_s->global->p2p)
  5358. p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
  5359. wpas_group_formation_completed(wpa_s, 1, 0);
  5360. }
  5361. void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
  5362. struct wps_event_fail *fail)
  5363. {
  5364. if (!wpa_s->p2p_in_provisioning) {
  5365. wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
  5366. "provisioning not in progress");
  5367. return;
  5368. }
  5369. if (wpa_s->go_params) {
  5370. p2p_clear_provisioning_info(
  5371. wpa_s->global->p2p,
  5372. wpa_s->go_params->peer_device_addr);
  5373. }
  5374. wpas_notify_p2p_wps_failed(wpa_s, fail);
  5375. if (wpa_s == wpa_s->global->p2p_group_formation) {
  5376. /*
  5377. * Allow some time for the failed WPS negotiation exchange to
  5378. * complete, but remove the group since group formation cannot
  5379. * succeed after provisioning failure.
  5380. */
  5381. wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
  5382. wpa_s->global->p2p_fail_on_wps_complete = 1;
  5383. eloop_deplete_timeout(0, 50000,
  5384. wpas_p2p_group_formation_timeout,
  5385. wpa_s->parent, NULL);
  5386. }
  5387. }
  5388. int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
  5389. {
  5390. if (!wpa_s->global->p2p_fail_on_wps_complete ||
  5391. !wpa_s->p2p_in_provisioning)
  5392. return 0;
  5393. wpas_p2p_grpform_fail_after_wps(wpa_s);
  5394. return 1;
  5395. }
  5396. int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  5397. const char *config_method,
  5398. enum wpas_p2p_prov_disc_use use,
  5399. struct p2ps_provision *p2ps_prov)
  5400. {
  5401. u16 config_methods;
  5402. wpa_s->global->pending_p2ps_group = 0;
  5403. wpa_s->global->pending_p2ps_group_freq = 0;
  5404. wpa_s->p2p_fallback_to_go_neg = 0;
  5405. wpa_s->pending_pd_use = NORMAL_PD;
  5406. if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
  5407. p2ps_prov->conncap = p2ps_group_capability(
  5408. wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
  5409. &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
  5410. wpa_printf(MSG_DEBUG,
  5411. "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
  5412. __func__, p2ps_prov->conncap,
  5413. p2ps_prov->adv_id, p2ps_prov->conncap,
  5414. p2ps_prov->status, p2ps_prov->info);
  5415. config_methods = 0;
  5416. } else if (os_strncmp(config_method, "display", 7) == 0)
  5417. config_methods = WPS_CONFIG_DISPLAY;
  5418. else if (os_strncmp(config_method, "keypad", 6) == 0)
  5419. config_methods = WPS_CONFIG_KEYPAD;
  5420. else if (os_strncmp(config_method, "pbc", 3) == 0 ||
  5421. os_strncmp(config_method, "pushbutton", 10) == 0)
  5422. config_methods = WPS_CONFIG_PUSHBUTTON;
  5423. else {
  5424. wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
  5425. os_free(p2ps_prov);
  5426. return -1;
  5427. }
  5428. if (use == WPAS_P2P_PD_AUTO) {
  5429. os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
  5430. wpa_s->pending_pd_config_methods = config_methods;
  5431. wpa_s->p2p_auto_pd = 1;
  5432. wpa_s->p2p_auto_join = 0;
  5433. wpa_s->pending_pd_before_join = 0;
  5434. wpa_s->auto_pd_scan_retry = 0;
  5435. wpas_p2p_stop_find(wpa_s);
  5436. wpa_s->p2p_join_scan_count = 0;
  5437. os_get_reltime(&wpa_s->p2p_auto_started);
  5438. wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
  5439. wpa_s->p2p_auto_started.sec,
  5440. wpa_s->p2p_auto_started.usec);
  5441. wpas_p2p_join_scan(wpa_s, NULL);
  5442. return 0;
  5443. }
  5444. if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
  5445. os_free(p2ps_prov);
  5446. return -1;
  5447. }
  5448. return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
  5449. config_methods, use == WPAS_P2P_PD_FOR_JOIN,
  5450. 0, 1);
  5451. }
  5452. int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
  5453. char *end)
  5454. {
  5455. return p2p_scan_result_text(ies, ies_len, buf, end);
  5456. }
  5457. static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
  5458. {
  5459. if (!offchannel_pending_action_tx(wpa_s))
  5460. return;
  5461. if (wpa_s->p2p_send_action_work)
  5462. wpas_p2p_free_send_action_work(wpa_s);
  5463. wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
  5464. "operation request");
  5465. offchannel_clear_pending_action_tx(wpa_s);
  5466. }
  5467. int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
  5468. enum p2p_discovery_type type,
  5469. unsigned int num_req_dev_types, const u8 *req_dev_types,
  5470. const u8 *dev_id, unsigned int search_delay,
  5471. u8 seek_cnt, const char **seek_string, int freq)
  5472. {
  5473. wpas_p2p_clear_pending_action_tx(wpa_s);
  5474. wpa_s->p2p_long_listen = 0;
  5475. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
  5476. wpa_s->p2p_in_provisioning)
  5477. return -1;
  5478. wpa_supplicant_cancel_sched_scan(wpa_s);
  5479. return p2p_find(wpa_s->global->p2p, timeout, type,
  5480. num_req_dev_types, req_dev_types, dev_id,
  5481. search_delay, seek_cnt, seek_string, freq);
  5482. }
  5483. static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
  5484. struct wpa_scan_results *scan_res)
  5485. {
  5486. wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
  5487. if (wpa_s->p2p_scan_work) {
  5488. struct wpa_radio_work *work = wpa_s->p2p_scan_work;
  5489. wpa_s->p2p_scan_work = NULL;
  5490. radio_work_done(work);
  5491. }
  5492. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5493. return;
  5494. /*
  5495. * Indicate that results have been processed so that the P2P module can
  5496. * continue pending tasks.
  5497. */
  5498. p2p_scan_res_handled(wpa_s->global->p2p);
  5499. }
  5500. static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
  5501. {
  5502. wpas_p2p_clear_pending_action_tx(wpa_s);
  5503. wpa_s->p2p_long_listen = 0;
  5504. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  5505. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  5506. if (wpa_s->global->p2p)
  5507. p2p_stop_find(wpa_s->global->p2p);
  5508. if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
  5509. wpa_printf(MSG_DEBUG,
  5510. "P2P: Do not consider the scan results after stop_find");
  5511. wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
  5512. }
  5513. }
  5514. void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
  5515. {
  5516. wpas_p2p_stop_find_oper(wpa_s);
  5517. if (!wpa_s->global->pending_group_iface_for_p2ps)
  5518. wpas_p2p_remove_pending_group_interface(wpa_s);
  5519. }
  5520. static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
  5521. {
  5522. struct wpa_supplicant *wpa_s = eloop_ctx;
  5523. wpa_s->p2p_long_listen = 0;
  5524. }
  5525. int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
  5526. {
  5527. int res;
  5528. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5529. return -1;
  5530. wpa_supplicant_cancel_sched_scan(wpa_s);
  5531. wpas_p2p_clear_pending_action_tx(wpa_s);
  5532. if (timeout == 0) {
  5533. /*
  5534. * This is a request for unlimited Listen state. However, at
  5535. * least for now, this is mapped to a Listen state for one
  5536. * hour.
  5537. */
  5538. timeout = 3600;
  5539. }
  5540. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  5541. wpa_s->p2p_long_listen = 0;
  5542. /*
  5543. * Stop previous find/listen operation to avoid trying to request a new
  5544. * remain-on-channel operation while the driver is still running the
  5545. * previous one.
  5546. */
  5547. if (wpa_s->global->p2p)
  5548. p2p_stop_find(wpa_s->global->p2p);
  5549. res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
  5550. if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
  5551. wpa_s->p2p_long_listen = timeout * 1000;
  5552. eloop_register_timeout(timeout, 0,
  5553. wpas_p2p_long_listen_timeout,
  5554. wpa_s, NULL);
  5555. }
  5556. return res;
  5557. }
  5558. int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  5559. u8 *buf, size_t len, int p2p_group)
  5560. {
  5561. struct wpabuf *p2p_ie;
  5562. int ret;
  5563. if (wpa_s->global->p2p_disabled)
  5564. return -1;
  5565. /*
  5566. * Advertize mandatory cross connection capability even on
  5567. * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
  5568. */
  5569. if (wpa_s->conf->p2p_disabled && p2p_group)
  5570. return -1;
  5571. if (wpa_s->global->p2p == NULL)
  5572. return -1;
  5573. if (bss == NULL)
  5574. return -1;
  5575. p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  5576. ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
  5577. p2p_group, p2p_ie);
  5578. wpabuf_free(p2p_ie);
  5579. return ret;
  5580. }
  5581. int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
  5582. const u8 *dst, const u8 *bssid,
  5583. const u8 *ie, size_t ie_len,
  5584. unsigned int rx_freq, int ssi_signal)
  5585. {
  5586. if (wpa_s->global->p2p_disabled)
  5587. return 0;
  5588. if (wpa_s->global->p2p == NULL)
  5589. return 0;
  5590. switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
  5591. ie, ie_len, rx_freq)) {
  5592. case P2P_PREQ_NOT_P2P:
  5593. wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
  5594. ssi_signal);
  5595. /* fall through */
  5596. case P2P_PREQ_MALFORMED:
  5597. case P2P_PREQ_NOT_LISTEN:
  5598. case P2P_PREQ_NOT_PROCESSED:
  5599. default: /* make gcc happy */
  5600. return 0;
  5601. case P2P_PREQ_PROCESSED:
  5602. return 1;
  5603. }
  5604. }
  5605. void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
  5606. const u8 *sa, const u8 *bssid,
  5607. u8 category, const u8 *data, size_t len, int freq)
  5608. {
  5609. if (wpa_s->global->p2p_disabled)
  5610. return;
  5611. if (wpa_s->global->p2p == NULL)
  5612. return;
  5613. p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
  5614. freq);
  5615. }
  5616. void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
  5617. {
  5618. if (wpa_s->global->p2p_disabled)
  5619. return;
  5620. if (wpa_s->global->p2p == NULL)
  5621. return;
  5622. p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
  5623. }
  5624. static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
  5625. {
  5626. p2p_group_deinit(wpa_s->p2p_group);
  5627. wpa_s->p2p_group = NULL;
  5628. wpa_s->ap_configured_cb = NULL;
  5629. wpa_s->ap_configured_cb_ctx = NULL;
  5630. wpa_s->ap_configured_cb_data = NULL;
  5631. wpa_s->connect_without_scan = NULL;
  5632. }
  5633. int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
  5634. {
  5635. wpa_s->p2p_long_listen = 0;
  5636. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5637. return -1;
  5638. return p2p_reject(wpa_s->global->p2p, addr);
  5639. }
  5640. /* Invite to reinvoke a persistent group */
  5641. int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  5642. struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
  5643. int ht40, int vht, int pref_freq)
  5644. {
  5645. enum p2p_invite_role role;
  5646. u8 *bssid = NULL;
  5647. int force_freq = 0;
  5648. int res;
  5649. int no_pref_freq_given = pref_freq == 0;
  5650. unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
  5651. wpa_s->global->p2p_invite_group = NULL;
  5652. if (peer_addr)
  5653. os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
  5654. else
  5655. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  5656. wpa_s->p2p_persistent_go_freq = freq;
  5657. wpa_s->p2p_go_ht40 = !!ht40;
  5658. if (ssid->mode == WPAS_MODE_P2P_GO) {
  5659. role = P2P_INVITE_ROLE_GO;
  5660. if (peer_addr == NULL) {
  5661. wpa_printf(MSG_DEBUG, "P2P: Missing peer "
  5662. "address in invitation command");
  5663. return -1;
  5664. }
  5665. if (wpas_p2p_create_iface(wpa_s)) {
  5666. if (wpas_p2p_add_group_interface(wpa_s,
  5667. WPA_IF_P2P_GO) < 0) {
  5668. wpa_printf(MSG_ERROR, "P2P: Failed to "
  5669. "allocate a new interface for the "
  5670. "group");
  5671. return -1;
  5672. }
  5673. bssid = wpa_s->pending_interface_addr;
  5674. } else
  5675. bssid = wpa_s->own_addr;
  5676. } else {
  5677. role = P2P_INVITE_ROLE_CLIENT;
  5678. peer_addr = ssid->bssid;
  5679. }
  5680. wpa_s->pending_invite_ssid_id = ssid->id;
  5681. size = P2P_MAX_PREF_CHANNELS;
  5682. res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
  5683. role == P2P_INVITE_ROLE_GO,
  5684. pref_freq_list, &size);
  5685. if (res)
  5686. return res;
  5687. p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
  5688. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5689. return -1;
  5690. if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
  5691. no_pref_freq_given && pref_freq > 0 &&
  5692. wpa_s->num_multichan_concurrent > 1 &&
  5693. wpas_p2p_num_unused_channels(wpa_s) > 0) {
  5694. wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
  5695. pref_freq);
  5696. pref_freq = 0;
  5697. }
  5698. /*
  5699. * Stop any find/listen operations before invitation and possibly
  5700. * connection establishment.
  5701. */
  5702. wpas_p2p_stop_find_oper(wpa_s);
  5703. return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
  5704. ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
  5705. 1, pref_freq, -1);
  5706. }
  5707. /* Invite to join an active group */
  5708. int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
  5709. const u8 *peer_addr, const u8 *go_dev_addr)
  5710. {
  5711. struct wpa_global *global = wpa_s->global;
  5712. enum p2p_invite_role role;
  5713. u8 *bssid = NULL;
  5714. struct wpa_ssid *ssid;
  5715. int persistent;
  5716. int freq = 0, force_freq = 0, pref_freq = 0;
  5717. int res;
  5718. unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
  5719. wpa_s->p2p_persistent_go_freq = 0;
  5720. wpa_s->p2p_go_ht40 = 0;
  5721. wpa_s->p2p_go_vht = 0;
  5722. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  5723. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  5724. break;
  5725. }
  5726. if (wpa_s == NULL) {
  5727. wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
  5728. return -1;
  5729. }
  5730. ssid = wpa_s->current_ssid;
  5731. if (ssid == NULL) {
  5732. wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
  5733. "invitation");
  5734. return -1;
  5735. }
  5736. wpa_s->global->p2p_invite_group = wpa_s;
  5737. persistent = ssid->p2p_persistent_group &&
  5738. wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
  5739. ssid->ssid, ssid->ssid_len);
  5740. if (ssid->mode == WPAS_MODE_P2P_GO) {
  5741. role = P2P_INVITE_ROLE_ACTIVE_GO;
  5742. bssid = wpa_s->own_addr;
  5743. if (go_dev_addr == NULL)
  5744. go_dev_addr = wpa_s->global->p2p_dev_addr;
  5745. freq = ssid->frequency;
  5746. } else {
  5747. role = P2P_INVITE_ROLE_CLIENT;
  5748. if (wpa_s->wpa_state < WPA_ASSOCIATED) {
  5749. wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
  5750. "invite to current group");
  5751. return -1;
  5752. }
  5753. bssid = wpa_s->bssid;
  5754. if (go_dev_addr == NULL &&
  5755. !is_zero_ether_addr(wpa_s->go_dev_addr))
  5756. go_dev_addr = wpa_s->go_dev_addr;
  5757. freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
  5758. (int) wpa_s->assoc_freq;
  5759. }
  5760. wpa_s->parent->pending_invite_ssid_id = -1;
  5761. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5762. return -1;
  5763. size = P2P_MAX_PREF_CHANNELS;
  5764. res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
  5765. role == P2P_INVITE_ROLE_ACTIVE_GO,
  5766. pref_freq_list, &size);
  5767. if (res)
  5768. return res;
  5769. wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
  5770. return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
  5771. ssid->ssid, ssid->ssid_len, force_freq,
  5772. go_dev_addr, persistent, pref_freq, -1);
  5773. }
  5774. void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
  5775. {
  5776. struct wpa_ssid *ssid = wpa_s->current_ssid;
  5777. u8 go_dev_addr[ETH_ALEN];
  5778. int network_id = -1;
  5779. int persistent;
  5780. int freq;
  5781. u8 ip[3 * 4];
  5782. char ip_addr[100];
  5783. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
  5784. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  5785. wpa_s->parent, NULL);
  5786. }
  5787. if (!wpa_s->show_group_started || !ssid)
  5788. return;
  5789. wpa_s->show_group_started = 0;
  5790. os_memset(go_dev_addr, 0, ETH_ALEN);
  5791. if (ssid->bssid_set)
  5792. os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
  5793. persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
  5794. ssid->ssid_len);
  5795. os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
  5796. if (wpa_s->global->p2p_group_formation == wpa_s)
  5797. wpa_s->global->p2p_group_formation = NULL;
  5798. freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
  5799. (int) wpa_s->assoc_freq;
  5800. ip_addr[0] = '\0';
  5801. if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
  5802. int res;
  5803. res = os_snprintf(ip_addr, sizeof(ip_addr),
  5804. " ip_addr=%u.%u.%u.%u "
  5805. "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
  5806. ip[0], ip[1], ip[2], ip[3],
  5807. ip[4], ip[5], ip[6], ip[7],
  5808. ip[8], ip[9], ip[10], ip[11]);
  5809. if (os_snprintf_error(sizeof(ip_addr), res))
  5810. ip_addr[0] = '\0';
  5811. }
  5812. wpas_p2p_group_started(wpa_s, 0, ssid, freq,
  5813. ssid->passphrase == NULL && ssid->psk_set ?
  5814. ssid->psk : NULL,
  5815. ssid->passphrase, go_dev_addr, persistent,
  5816. ip_addr);
  5817. if (persistent)
  5818. network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
  5819. ssid, go_dev_addr);
  5820. if (network_id < 0)
  5821. network_id = ssid->id;
  5822. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
  5823. }
  5824. int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
  5825. u32 interval1, u32 duration2, u32 interval2)
  5826. {
  5827. int ret;
  5828. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5829. return -1;
  5830. if (wpa_s->wpa_state < WPA_ASSOCIATED ||
  5831. wpa_s->current_ssid == NULL ||
  5832. wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
  5833. return -1;
  5834. ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
  5835. wpa_s->own_addr, wpa_s->assoc_freq,
  5836. duration1, interval1, duration2, interval2);
  5837. if (ret == 0)
  5838. wpa_s->waiting_presence_resp = 1;
  5839. return ret;
  5840. }
  5841. int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
  5842. unsigned int interval)
  5843. {
  5844. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5845. return -1;
  5846. return p2p_ext_listen(wpa_s->global->p2p, period, interval);
  5847. }
  5848. static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
  5849. {
  5850. if (wpa_s->current_ssid == NULL) {
  5851. /*
  5852. * current_ssid can be cleared when P2P client interface gets
  5853. * disconnected, so assume this interface was used as P2P
  5854. * client.
  5855. */
  5856. return 1;
  5857. }
  5858. return wpa_s->current_ssid->p2p_group &&
  5859. wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
  5860. }
  5861. static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
  5862. {
  5863. struct wpa_supplicant *wpa_s = eloop_ctx;
  5864. if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
  5865. wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
  5866. "disabled");
  5867. return;
  5868. }
  5869. wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
  5870. "group");
  5871. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
  5872. }
  5873. static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
  5874. {
  5875. int timeout;
  5876. if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
  5877. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
  5878. if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
  5879. return;
  5880. timeout = wpa_s->conf->p2p_group_idle;
  5881. if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
  5882. (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
  5883. timeout = P2P_MAX_CLIENT_IDLE;
  5884. if (timeout == 0)
  5885. return;
  5886. if (timeout < 0) {
  5887. if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
  5888. timeout = 0; /* special client mode no-timeout */
  5889. else
  5890. return;
  5891. }
  5892. if (wpa_s->p2p_in_provisioning) {
  5893. /*
  5894. * Use the normal group formation timeout during the
  5895. * provisioning phase to avoid terminating this process too
  5896. * early due to group idle timeout.
  5897. */
  5898. wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
  5899. "during provisioning");
  5900. return;
  5901. }
  5902. if (wpa_s->show_group_started) {
  5903. /*
  5904. * Use the normal group formation timeout between the end of
  5905. * the provisioning phase and completion of 4-way handshake to
  5906. * avoid terminating this process too early due to group idle
  5907. * timeout.
  5908. */
  5909. wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
  5910. "while waiting for initial 4-way handshake to "
  5911. "complete");
  5912. return;
  5913. }
  5914. wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
  5915. timeout);
  5916. eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
  5917. wpa_s, NULL);
  5918. }
  5919. /* Returns 1 if the interface was removed */
  5920. int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
  5921. u16 reason_code, const u8 *ie, size_t ie_len,
  5922. int locally_generated)
  5923. {
  5924. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5925. return 0;
  5926. if (!locally_generated)
  5927. p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
  5928. ie_len);
  5929. if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
  5930. wpa_s->current_ssid &&
  5931. wpa_s->current_ssid->p2p_group &&
  5932. wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
  5933. wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
  5934. "session is ending");
  5935. if (wpas_p2p_group_delete(wpa_s,
  5936. P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
  5937. > 0)
  5938. return 1;
  5939. }
  5940. return 0;
  5941. }
  5942. void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
  5943. u16 reason_code, const u8 *ie, size_t ie_len,
  5944. int locally_generated)
  5945. {
  5946. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5947. return;
  5948. if (!locally_generated)
  5949. p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
  5950. ie_len);
  5951. }
  5952. void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
  5953. {
  5954. struct p2p_data *p2p = wpa_s->global->p2p;
  5955. if (p2p == NULL)
  5956. return;
  5957. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
  5958. return;
  5959. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
  5960. p2p_set_dev_name(p2p, wpa_s->conf->device_name);
  5961. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
  5962. p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
  5963. if (wpa_s->wps &&
  5964. (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
  5965. p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
  5966. if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
  5967. p2p_set_uuid(p2p, wpa_s->wps->uuid);
  5968. if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
  5969. p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
  5970. p2p_set_model_name(p2p, wpa_s->conf->model_name);
  5971. p2p_set_model_number(p2p, wpa_s->conf->model_number);
  5972. p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
  5973. }
  5974. if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
  5975. p2p_set_sec_dev_types(p2p,
  5976. (void *) wpa_s->conf->sec_device_type,
  5977. wpa_s->conf->num_sec_device_types);
  5978. if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
  5979. int i;
  5980. p2p_remove_wps_vendor_extensions(p2p);
  5981. for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
  5982. if (wpa_s->conf->wps_vendor_ext[i] == NULL)
  5983. continue;
  5984. p2p_add_wps_vendor_extension(
  5985. p2p, wpa_s->conf->wps_vendor_ext[i]);
  5986. }
  5987. }
  5988. if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
  5989. wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  5990. char country[3];
  5991. country[0] = wpa_s->conf->country[0];
  5992. country[1] = wpa_s->conf->country[1];
  5993. country[2] = 0x04;
  5994. p2p_set_country(p2p, country);
  5995. }
  5996. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
  5997. p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
  5998. wpa_s->conf->p2p_ssid_postfix ?
  5999. os_strlen(wpa_s->conf->p2p_ssid_postfix) :
  6000. 0);
  6001. }
  6002. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
  6003. p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
  6004. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
  6005. u8 reg_class, channel;
  6006. int ret;
  6007. unsigned int r;
  6008. u8 channel_forced;
  6009. if (wpa_s->conf->p2p_listen_reg_class &&
  6010. wpa_s->conf->p2p_listen_channel) {
  6011. reg_class = wpa_s->conf->p2p_listen_reg_class;
  6012. channel = wpa_s->conf->p2p_listen_channel;
  6013. channel_forced = 1;
  6014. } else {
  6015. reg_class = 81;
  6016. /*
  6017. * Pick one of the social channels randomly as the
  6018. * listen channel.
  6019. */
  6020. if (os_get_random((u8 *) &r, sizeof(r)) < 0)
  6021. channel = 1;
  6022. else
  6023. channel = 1 + (r % 3) * 5;
  6024. channel_forced = 0;
  6025. }
  6026. ret = p2p_set_listen_channel(p2p, reg_class, channel,
  6027. channel_forced);
  6028. if (ret)
  6029. wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
  6030. "failed: %d", ret);
  6031. }
  6032. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
  6033. u8 op_reg_class, op_channel, cfg_op_channel;
  6034. int ret = 0;
  6035. unsigned int r;
  6036. if (wpa_s->conf->p2p_oper_reg_class &&
  6037. wpa_s->conf->p2p_oper_channel) {
  6038. op_reg_class = wpa_s->conf->p2p_oper_reg_class;
  6039. op_channel = wpa_s->conf->p2p_oper_channel;
  6040. cfg_op_channel = 1;
  6041. } else {
  6042. op_reg_class = 81;
  6043. /*
  6044. * Use random operation channel from (1, 6, 11)
  6045. *if no other preference is indicated.
  6046. */
  6047. if (os_get_random((u8 *) &r, sizeof(r)) < 0)
  6048. op_channel = 1;
  6049. else
  6050. op_channel = 1 + (r % 3) * 5;
  6051. cfg_op_channel = 0;
  6052. }
  6053. ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
  6054. cfg_op_channel);
  6055. if (ret)
  6056. wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
  6057. "failed: %d", ret);
  6058. }
  6059. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
  6060. if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
  6061. wpa_s->conf->p2p_pref_chan) < 0) {
  6062. wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
  6063. "update failed");
  6064. }
  6065. if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
  6066. wpa_printf(MSG_ERROR, "P2P: No GO channel list "
  6067. "update failed");
  6068. }
  6069. }
  6070. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
  6071. p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
  6072. }
  6073. int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
  6074. int duration)
  6075. {
  6076. if (!wpa_s->ap_iface)
  6077. return -1;
  6078. return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
  6079. duration);
  6080. }
  6081. int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
  6082. {
  6083. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  6084. return -1;
  6085. wpa_s->global->cross_connection = enabled;
  6086. p2p_set_cross_connect(wpa_s->global->p2p, enabled);
  6087. if (!enabled) {
  6088. struct wpa_supplicant *iface;
  6089. for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
  6090. {
  6091. if (iface->cross_connect_enabled == 0)
  6092. continue;
  6093. iface->cross_connect_enabled = 0;
  6094. iface->cross_connect_in_use = 0;
  6095. wpa_msg_global(iface->parent, MSG_INFO,
  6096. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  6097. iface->ifname,
  6098. iface->cross_connect_uplink);
  6099. }
  6100. }
  6101. return 0;
  6102. }
  6103. static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
  6104. {
  6105. struct wpa_supplicant *iface;
  6106. if (!uplink->global->cross_connection)
  6107. return;
  6108. for (iface = uplink->global->ifaces; iface; iface = iface->next) {
  6109. if (!iface->cross_connect_enabled)
  6110. continue;
  6111. if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
  6112. 0)
  6113. continue;
  6114. if (iface->ap_iface == NULL)
  6115. continue;
  6116. if (iface->cross_connect_in_use)
  6117. continue;
  6118. iface->cross_connect_in_use = 1;
  6119. wpa_msg_global(iface->parent, MSG_INFO,
  6120. P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
  6121. iface->ifname, iface->cross_connect_uplink);
  6122. }
  6123. }
  6124. static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
  6125. {
  6126. struct wpa_supplicant *iface;
  6127. for (iface = uplink->global->ifaces; iface; iface = iface->next) {
  6128. if (!iface->cross_connect_enabled)
  6129. continue;
  6130. if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
  6131. 0)
  6132. continue;
  6133. if (!iface->cross_connect_in_use)
  6134. continue;
  6135. wpa_msg_global(iface->parent, MSG_INFO,
  6136. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  6137. iface->ifname, iface->cross_connect_uplink);
  6138. iface->cross_connect_in_use = 0;
  6139. }
  6140. }
  6141. void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
  6142. {
  6143. if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
  6144. wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
  6145. wpa_s->cross_connect_disallowed)
  6146. wpas_p2p_disable_cross_connect(wpa_s);
  6147. else
  6148. wpas_p2p_enable_cross_connect(wpa_s);
  6149. if (!wpa_s->ap_iface &&
  6150. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
  6151. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
  6152. }
  6153. void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
  6154. {
  6155. wpas_p2p_disable_cross_connect(wpa_s);
  6156. if (!wpa_s->ap_iface &&
  6157. !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
  6158. wpa_s, NULL))
  6159. wpas_p2p_set_group_idle_timeout(wpa_s);
  6160. }
  6161. static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
  6162. {
  6163. struct wpa_supplicant *iface;
  6164. if (!wpa_s->global->cross_connection)
  6165. return;
  6166. for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
  6167. if (iface == wpa_s)
  6168. continue;
  6169. if (iface->drv_flags &
  6170. WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
  6171. continue;
  6172. if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
  6173. iface != wpa_s->parent)
  6174. continue;
  6175. wpa_s->cross_connect_enabled = 1;
  6176. os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
  6177. sizeof(wpa_s->cross_connect_uplink));
  6178. wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
  6179. "%s to %s whenever uplink is available",
  6180. wpa_s->ifname, wpa_s->cross_connect_uplink);
  6181. if (iface->ap_iface || iface->current_ssid == NULL ||
  6182. iface->current_ssid->mode != WPAS_MODE_INFRA ||
  6183. iface->cross_connect_disallowed ||
  6184. iface->wpa_state != WPA_COMPLETED)
  6185. break;
  6186. wpa_s->cross_connect_in_use = 1;
  6187. wpa_msg_global(wpa_s->parent, MSG_INFO,
  6188. P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
  6189. wpa_s->ifname, wpa_s->cross_connect_uplink);
  6190. break;
  6191. }
  6192. }
  6193. int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
  6194. {
  6195. if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
  6196. !wpa_s->p2p_in_provisioning)
  6197. return 0; /* not P2P client operation */
  6198. wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
  6199. "session overlap");
  6200. if (wpa_s != wpa_s->parent)
  6201. wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
  6202. wpas_p2p_group_formation_failed(wpa_s, 0);
  6203. return 1;
  6204. }
  6205. void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
  6206. {
  6207. struct wpa_supplicant *wpa_s = eloop_ctx;
  6208. wpas_p2p_notif_pbc_overlap(wpa_s);
  6209. }
  6210. void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
  6211. enum wpas_p2p_channel_update_trig trig)
  6212. {
  6213. struct p2p_channels chan, cli_chan;
  6214. struct wpa_used_freq_data *freqs = NULL;
  6215. unsigned int num = wpa_s->num_multichan_concurrent;
  6216. if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
  6217. return;
  6218. freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
  6219. if (!freqs)
  6220. return;
  6221. num = get_shared_radio_freqs_data(wpa_s, freqs, num);
  6222. os_memset(&chan, 0, sizeof(chan));
  6223. os_memset(&cli_chan, 0, sizeof(cli_chan));
  6224. if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
  6225. wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
  6226. "channel list");
  6227. return;
  6228. }
  6229. p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
  6230. wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
  6231. /*
  6232. * The used frequencies map changed, so it is possible that a GO is
  6233. * using a channel that is no longer valid for P2P use. It is also
  6234. * possible that due to policy consideration, it would be preferable to
  6235. * move it to a frequency already used by other station interfaces.
  6236. */
  6237. wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
  6238. os_free(freqs);
  6239. }
  6240. static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
  6241. struct wpa_scan_results *scan_res)
  6242. {
  6243. wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
  6244. }
  6245. int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
  6246. {
  6247. struct wpa_global *global = wpa_s->global;
  6248. int found = 0;
  6249. const u8 *peer;
  6250. if (global->p2p == NULL)
  6251. return -1;
  6252. wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
  6253. if (wpa_s->pending_interface_name[0] &&
  6254. !is_zero_ether_addr(wpa_s->pending_interface_addr))
  6255. found = 1;
  6256. peer = p2p_get_go_neg_peer(global->p2p);
  6257. if (peer) {
  6258. wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
  6259. MACSTR, MAC2STR(peer));
  6260. p2p_unauthorize(global->p2p, peer);
  6261. found = 1;
  6262. }
  6263. if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
  6264. wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
  6265. wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
  6266. found = 1;
  6267. }
  6268. if (wpa_s->pending_pd_before_join) {
  6269. wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
  6270. wpa_s->pending_pd_before_join = 0;
  6271. found = 1;
  6272. }
  6273. wpas_p2p_stop_find(wpa_s);
  6274. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  6275. if (wpa_s == global->p2p_group_formation &&
  6276. (wpa_s->p2p_in_provisioning ||
  6277. wpa_s->parent->pending_interface_type ==
  6278. WPA_IF_P2P_CLIENT)) {
  6279. wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
  6280. "formation found - cancelling",
  6281. wpa_s->ifname);
  6282. found = 1;
  6283. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  6284. wpa_s->parent, NULL);
  6285. if (wpa_s->p2p_in_provisioning) {
  6286. wpas_group_formation_completed(wpa_s, 0, 0);
  6287. break;
  6288. }
  6289. wpas_p2p_group_delete(wpa_s,
  6290. P2P_GROUP_REMOVAL_REQUESTED);
  6291. break;
  6292. } else if (wpa_s->p2p_in_invitation) {
  6293. wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
  6294. wpa_s->ifname);
  6295. found = 1;
  6296. wpas_p2p_group_formation_failed(wpa_s, 0);
  6297. }
  6298. }
  6299. if (!found) {
  6300. wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
  6301. return -1;
  6302. }
  6303. return 0;
  6304. }
  6305. void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
  6306. {
  6307. if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
  6308. return;
  6309. wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
  6310. "being available anymore");
  6311. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
  6312. }
  6313. void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
  6314. int freq_24, int freq_5, int freq_overall)
  6315. {
  6316. struct p2p_data *p2p = wpa_s->global->p2p;
  6317. if (p2p == NULL)
  6318. return;
  6319. p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
  6320. }
  6321. int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
  6322. {
  6323. u8 peer[ETH_ALEN];
  6324. struct p2p_data *p2p = wpa_s->global->p2p;
  6325. if (p2p == NULL)
  6326. return -1;
  6327. if (hwaddr_aton(addr, peer))
  6328. return -1;
  6329. return p2p_unauthorize(p2p, peer);
  6330. }
  6331. /**
  6332. * wpas_p2p_disconnect - Disconnect from a P2P Group
  6333. * @wpa_s: Pointer to wpa_supplicant data
  6334. * Returns: 0 on success, -1 on failure
  6335. *
  6336. * This can be used to disconnect from a group in which the local end is a P2P
  6337. * Client or to end a P2P Group in case the local end is the Group Owner. If a
  6338. * virtual network interface was created for this group, that interface will be
  6339. * removed. Otherwise, only the configured P2P group network will be removed
  6340. * from the interface.
  6341. */
  6342. int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
  6343. {
  6344. if (wpa_s == NULL)
  6345. return -1;
  6346. return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
  6347. -1 : 0;
  6348. }
  6349. int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
  6350. {
  6351. int ret;
  6352. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  6353. return 0;
  6354. ret = p2p_in_progress(wpa_s->global->p2p);
  6355. if (ret == 0) {
  6356. /*
  6357. * Check whether there is an ongoing WPS provisioning step (or
  6358. * other parts of group formation) on another interface since
  6359. * p2p_in_progress() does not report this to avoid issues for
  6360. * scans during such provisioning step.
  6361. */
  6362. if (wpa_s->global->p2p_group_formation &&
  6363. wpa_s->global->p2p_group_formation != wpa_s) {
  6364. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
  6365. "in group formation",
  6366. wpa_s->global->p2p_group_formation->ifname);
  6367. ret = 1;
  6368. }
  6369. }
  6370. if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
  6371. struct os_reltime now;
  6372. os_get_reltime(&now);
  6373. if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
  6374. P2P_MAX_INITIAL_CONN_WAIT_GO)) {
  6375. /* Wait for the first client has expired */
  6376. wpa_s->global->p2p_go_wait_client.sec = 0;
  6377. } else {
  6378. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
  6379. ret = 1;
  6380. }
  6381. }
  6382. return ret;
  6383. }
  6384. void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
  6385. struct wpa_ssid *ssid)
  6386. {
  6387. if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
  6388. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  6389. wpa_s->parent, NULL) > 0) {
  6390. /**
  6391. * Remove the network by scheduling the group formation
  6392. * timeout to happen immediately. The teardown code
  6393. * needs to be scheduled to run asynch later so that we
  6394. * don't delete data from under ourselves unexpectedly.
  6395. * Calling wpas_p2p_group_formation_timeout directly
  6396. * causes a series of crashes in WPS failure scenarios.
  6397. */
  6398. wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
  6399. "P2P group network getting removed");
  6400. eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
  6401. wpa_s->parent, NULL);
  6402. }
  6403. }
  6404. struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
  6405. const u8 *addr, const u8 *ssid,
  6406. size_t ssid_len)
  6407. {
  6408. struct wpa_ssid *s;
  6409. size_t i;
  6410. for (s = wpa_s->conf->ssid; s; s = s->next) {
  6411. if (s->disabled != 2)
  6412. continue;
  6413. if (ssid &&
  6414. (ssid_len != s->ssid_len ||
  6415. os_memcmp(ssid, s->ssid, ssid_len) != 0))
  6416. continue;
  6417. if (addr == NULL) {
  6418. if (s->mode == WPAS_MODE_P2P_GO)
  6419. return s;
  6420. continue;
  6421. }
  6422. if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
  6423. return s; /* peer is GO in the persistent group */
  6424. if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
  6425. continue;
  6426. for (i = 0; i < s->num_p2p_clients; i++) {
  6427. if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
  6428. addr, ETH_ALEN) == 0)
  6429. return s; /* peer is P2P client in persistent
  6430. * group */
  6431. }
  6432. }
  6433. return NULL;
  6434. }
  6435. void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
  6436. const u8 *addr)
  6437. {
  6438. if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  6439. wpa_s->parent, NULL) > 0) {
  6440. /*
  6441. * This can happen if WPS provisioning step is not terminated
  6442. * cleanly (e.g., P2P Client does not send WSC_Done). Since the
  6443. * peer was able to connect, there is no need to time out group
  6444. * formation after this, though. In addition, this is used with
  6445. * the initial connection wait on the GO as a separate formation
  6446. * timeout and as such, expected to be hit after the initial WPS
  6447. * provisioning step.
  6448. */
  6449. wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
  6450. if (!wpa_s->p2p_go_group_formation_completed &&
  6451. !wpa_s->group_formation_reported) {
  6452. /*
  6453. * GO has not yet notified group formation success since
  6454. * the WPS step was not completed cleanly. Do that
  6455. * notification now since the P2P Client was able to
  6456. * connect and as such, must have received the
  6457. * credential from the WPS step.
  6458. */
  6459. if (wpa_s->global->p2p)
  6460. p2p_wps_success_cb(wpa_s->global->p2p, addr);
  6461. wpas_group_formation_completed(wpa_s, 1, 0);
  6462. }
  6463. }
  6464. if (!wpa_s->p2p_go_group_formation_completed) {
  6465. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
  6466. wpa_s->p2p_go_group_formation_completed = 1;
  6467. wpa_s->global->p2p_group_formation = NULL;
  6468. wpa_s->p2p_in_provisioning = 0;
  6469. wpa_s->p2p_in_invitation = 0;
  6470. }
  6471. wpa_s->global->p2p_go_wait_client.sec = 0;
  6472. if (addr == NULL)
  6473. return;
  6474. wpas_p2p_add_persistent_group_client(wpa_s, addr);
  6475. }
  6476. static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
  6477. int group_added)
  6478. {
  6479. struct wpa_supplicant *group = wpa_s;
  6480. int ret = 0;
  6481. if (wpa_s->global->p2p_group_formation)
  6482. group = wpa_s->global->p2p_group_formation;
  6483. wpa_s = wpa_s->global->p2p_init_wpa_s;
  6484. offchannel_send_action_done(wpa_s);
  6485. if (group_added)
  6486. ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
  6487. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
  6488. wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
  6489. wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
  6490. 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
  6491. wpa_s->p2p_persistent_id,
  6492. wpa_s->p2p_pd_before_go_neg,
  6493. wpa_s->p2p_go_ht40,
  6494. wpa_s->p2p_go_vht);
  6495. return ret;
  6496. }
  6497. int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
  6498. {
  6499. int res;
  6500. if (!wpa_s->p2p_fallback_to_go_neg ||
  6501. wpa_s->p2p_in_provisioning <= 5)
  6502. return 0;
  6503. if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
  6504. return 0; /* peer operating as a GO */
  6505. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
  6506. "fallback to GO Negotiation");
  6507. wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
  6508. "reason=GO-not-found");
  6509. res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
  6510. return res == 1 ? 2 : 1;
  6511. }
  6512. unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
  6513. {
  6514. struct wpa_supplicant *ifs;
  6515. if (wpa_s->wpa_state > WPA_SCANNING) {
  6516. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
  6517. "concurrent operation",
  6518. wpa_s->conf->p2p_search_delay);
  6519. return wpa_s->conf->p2p_search_delay;
  6520. }
  6521. dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
  6522. radio_list) {
  6523. if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
  6524. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
  6525. "delay due to concurrent operation on "
  6526. "interface %s",
  6527. wpa_s->conf->p2p_search_delay,
  6528. ifs->ifname);
  6529. return wpa_s->conf->p2p_search_delay;
  6530. }
  6531. }
  6532. return 0;
  6533. }
  6534. static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
  6535. struct wpa_ssid *s, const u8 *addr,
  6536. int iface_addr)
  6537. {
  6538. struct psk_list_entry *psk, *tmp;
  6539. int changed = 0;
  6540. dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
  6541. list) {
  6542. if ((iface_addr && !psk->p2p &&
  6543. os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
  6544. (!iface_addr && psk->p2p &&
  6545. os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
  6546. wpa_dbg(wpa_s, MSG_DEBUG,
  6547. "P2P: Remove persistent group PSK list entry for "
  6548. MACSTR " p2p=%u",
  6549. MAC2STR(psk->addr), psk->p2p);
  6550. dl_list_del(&psk->list);
  6551. os_free(psk);
  6552. changed++;
  6553. }
  6554. }
  6555. return changed;
  6556. }
  6557. void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
  6558. const u8 *p2p_dev_addr,
  6559. const u8 *psk, size_t psk_len)
  6560. {
  6561. struct wpa_ssid *ssid = wpa_s->current_ssid;
  6562. struct wpa_ssid *persistent;
  6563. struct psk_list_entry *p, *last;
  6564. if (psk_len != sizeof(p->psk))
  6565. return;
  6566. if (p2p_dev_addr) {
  6567. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
  6568. " p2p_dev_addr=" MACSTR,
  6569. MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
  6570. if (is_zero_ether_addr(p2p_dev_addr))
  6571. p2p_dev_addr = NULL;
  6572. } else {
  6573. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
  6574. MAC2STR(mac_addr));
  6575. }
  6576. if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
  6577. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
  6578. /* To be added to persistent group once created */
  6579. if (wpa_s->global->add_psk == NULL) {
  6580. wpa_s->global->add_psk = os_zalloc(sizeof(*p));
  6581. if (wpa_s->global->add_psk == NULL)
  6582. return;
  6583. }
  6584. p = wpa_s->global->add_psk;
  6585. if (p2p_dev_addr) {
  6586. p->p2p = 1;
  6587. os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
  6588. } else {
  6589. p->p2p = 0;
  6590. os_memcpy(p->addr, mac_addr, ETH_ALEN);
  6591. }
  6592. os_memcpy(p->psk, psk, psk_len);
  6593. return;
  6594. }
  6595. if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
  6596. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
  6597. return;
  6598. }
  6599. persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
  6600. ssid->ssid_len);
  6601. if (!persistent) {
  6602. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
  6603. return;
  6604. }
  6605. p = os_zalloc(sizeof(*p));
  6606. if (p == NULL)
  6607. return;
  6608. if (p2p_dev_addr) {
  6609. p->p2p = 1;
  6610. os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
  6611. } else {
  6612. p->p2p = 0;
  6613. os_memcpy(p->addr, mac_addr, ETH_ALEN);
  6614. }
  6615. os_memcpy(p->psk, psk, psk_len);
  6616. if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
  6617. (last = dl_list_last(&persistent->psk_list,
  6618. struct psk_list_entry, list))) {
  6619. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
  6620. MACSTR " (p2p=%u) to make room for a new one",
  6621. MAC2STR(last->addr), last->p2p);
  6622. dl_list_del(&last->list);
  6623. os_free(last);
  6624. }
  6625. wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
  6626. p2p_dev_addr ? p2p_dev_addr : mac_addr,
  6627. p2p_dev_addr == NULL);
  6628. if (p2p_dev_addr) {
  6629. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
  6630. MACSTR, MAC2STR(p2p_dev_addr));
  6631. } else {
  6632. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
  6633. MAC2STR(mac_addr));
  6634. }
  6635. dl_list_add(&persistent->psk_list, &p->list);
  6636. if (wpa_s->parent->conf->update_config &&
  6637. wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
  6638. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  6639. }
  6640. static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
  6641. struct wpa_ssid *s, const u8 *addr,
  6642. int iface_addr)
  6643. {
  6644. int res;
  6645. res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
  6646. if (res > 0 && wpa_s->conf->update_config &&
  6647. wpa_config_write(wpa_s->confname, wpa_s->conf))
  6648. wpa_dbg(wpa_s, MSG_DEBUG,
  6649. "P2P: Failed to update configuration");
  6650. }
  6651. static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
  6652. const u8 *peer, int iface_addr)
  6653. {
  6654. struct hostapd_data *hapd;
  6655. struct hostapd_wpa_psk *psk, *prev, *rem;
  6656. struct sta_info *sta;
  6657. if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
  6658. wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
  6659. return;
  6660. /* Remove per-station PSK entry */
  6661. hapd = wpa_s->ap_iface->bss[0];
  6662. prev = NULL;
  6663. psk = hapd->conf->ssid.wpa_psk;
  6664. while (psk) {
  6665. if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
  6666. (!iface_addr &&
  6667. os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
  6668. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
  6669. MACSTR " iface_addr=%d",
  6670. MAC2STR(peer), iface_addr);
  6671. if (prev)
  6672. prev->next = psk->next;
  6673. else
  6674. hapd->conf->ssid.wpa_psk = psk->next;
  6675. rem = psk;
  6676. psk = psk->next;
  6677. os_free(rem);
  6678. } else {
  6679. prev = psk;
  6680. psk = psk->next;
  6681. }
  6682. }
  6683. /* Disconnect from group */
  6684. if (iface_addr)
  6685. sta = ap_get_sta(hapd, peer);
  6686. else
  6687. sta = ap_get_sta_p2p(hapd, peer);
  6688. if (sta) {
  6689. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
  6690. " (iface_addr=%d) from group",
  6691. MAC2STR(peer), iface_addr);
  6692. hostapd_drv_sta_deauth(hapd, sta->addr,
  6693. WLAN_REASON_DEAUTH_LEAVING);
  6694. ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
  6695. }
  6696. }
  6697. void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
  6698. int iface_addr)
  6699. {
  6700. struct wpa_ssid *s;
  6701. struct wpa_supplicant *w;
  6702. struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
  6703. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
  6704. /* Remove from any persistent group */
  6705. for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
  6706. if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
  6707. continue;
  6708. if (!iface_addr)
  6709. wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
  6710. wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
  6711. }
  6712. /* Remove from any operating group */
  6713. for (w = wpa_s->global->ifaces; w; w = w->next)
  6714. wpas_p2p_remove_client_go(w, peer, iface_addr);
  6715. }
  6716. static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
  6717. {
  6718. struct wpa_supplicant *wpa_s = eloop_ctx;
  6719. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
  6720. }
  6721. static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
  6722. {
  6723. struct wpa_supplicant *wpa_s = eloop_ctx;
  6724. wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
  6725. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
  6726. }
  6727. int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
  6728. struct wpa_ssid *ssid)
  6729. {
  6730. struct wpa_supplicant *iface;
  6731. for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
  6732. if (!iface->current_ssid ||
  6733. iface->current_ssid->frequency == freq ||
  6734. (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
  6735. !iface->current_ssid->p2p_group))
  6736. continue;
  6737. /* Remove the connection with least priority */
  6738. if (!wpas_is_p2p_prioritized(iface)) {
  6739. /* STA connection has priority over existing
  6740. * P2P connection, so remove the interface. */
  6741. wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
  6742. eloop_register_timeout(0, 0,
  6743. wpas_p2p_group_freq_conflict,
  6744. iface, NULL);
  6745. /* If connection in progress is P2P connection, do not
  6746. * proceed for the connection. */
  6747. if (wpa_s == iface)
  6748. return -1;
  6749. else
  6750. return 0;
  6751. } else {
  6752. /* P2P connection has priority, disable the STA network
  6753. */
  6754. wpa_supplicant_disable_network(wpa_s->global->ifaces,
  6755. ssid);
  6756. wpa_msg(wpa_s->global->ifaces, MSG_INFO,
  6757. WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
  6758. os_memset(wpa_s->global->ifaces->pending_bssid, 0,
  6759. ETH_ALEN);
  6760. /* If P2P connection is in progress, continue
  6761. * connecting...*/
  6762. if (wpa_s == iface)
  6763. return 0;
  6764. else
  6765. return -1;
  6766. }
  6767. }
  6768. return 0;
  6769. }
  6770. int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
  6771. {
  6772. struct wpa_ssid *ssid = wpa_s->current_ssid;
  6773. if (ssid == NULL || !ssid->p2p_group)
  6774. return 0;
  6775. if (wpa_s->p2p_last_4way_hs_fail &&
  6776. wpa_s->p2p_last_4way_hs_fail == ssid) {
  6777. u8 go_dev_addr[ETH_ALEN];
  6778. struct wpa_ssid *persistent;
  6779. if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
  6780. ssid->ssid,
  6781. ssid->ssid_len) <= 0) {
  6782. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
  6783. goto disconnect;
  6784. }
  6785. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
  6786. MACSTR, MAC2STR(go_dev_addr));
  6787. persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
  6788. ssid->ssid,
  6789. ssid->ssid_len);
  6790. if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
  6791. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
  6792. goto disconnect;
  6793. }
  6794. wpa_msg_global(wpa_s->parent, MSG_INFO,
  6795. P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
  6796. persistent->id);
  6797. disconnect:
  6798. wpa_s->p2p_last_4way_hs_fail = NULL;
  6799. /*
  6800. * Remove the group from a timeout to avoid issues with caller
  6801. * continuing to use the interface if this is on a P2P group
  6802. * interface.
  6803. */
  6804. eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
  6805. wpa_s, NULL);
  6806. return 1;
  6807. }
  6808. wpa_s->p2p_last_4way_hs_fail = ssid;
  6809. return 0;
  6810. }
  6811. #ifdef CONFIG_WPS_NFC
  6812. static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
  6813. struct wpabuf *p2p)
  6814. {
  6815. struct wpabuf *ret;
  6816. size_t wsc_len;
  6817. if (p2p == NULL) {
  6818. wpabuf_free(wsc);
  6819. wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
  6820. return NULL;
  6821. }
  6822. wsc_len = wsc ? wpabuf_len(wsc) : 0;
  6823. ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
  6824. if (ret == NULL) {
  6825. wpabuf_free(wsc);
  6826. wpabuf_free(p2p);
  6827. return NULL;
  6828. }
  6829. wpabuf_put_be16(ret, wsc_len);
  6830. if (wsc)
  6831. wpabuf_put_buf(ret, wsc);
  6832. wpabuf_put_be16(ret, wpabuf_len(p2p));
  6833. wpabuf_put_buf(ret, p2p);
  6834. wpabuf_free(wsc);
  6835. wpabuf_free(p2p);
  6836. wpa_hexdump_buf(MSG_DEBUG,
  6837. "P2P: Generated NFC connection handover message", ret);
  6838. if (ndef && ret) {
  6839. struct wpabuf *tmp;
  6840. tmp = ndef_build_p2p(ret);
  6841. wpabuf_free(ret);
  6842. if (tmp == NULL) {
  6843. wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
  6844. return NULL;
  6845. }
  6846. ret = tmp;
  6847. }
  6848. return ret;
  6849. }
  6850. static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
  6851. struct wpa_ssid **ssid, u8 *go_dev_addr)
  6852. {
  6853. struct wpa_supplicant *iface;
  6854. if (go_dev_addr)
  6855. os_memset(go_dev_addr, 0, ETH_ALEN);
  6856. if (ssid)
  6857. *ssid = NULL;
  6858. for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
  6859. if (iface->wpa_state < WPA_ASSOCIATING ||
  6860. iface->current_ssid == NULL || iface->assoc_freq == 0 ||
  6861. !iface->current_ssid->p2p_group ||
  6862. iface->current_ssid->mode != WPAS_MODE_INFRA)
  6863. continue;
  6864. if (ssid)
  6865. *ssid = iface->current_ssid;
  6866. if (go_dev_addr)
  6867. os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
  6868. return iface->assoc_freq;
  6869. }
  6870. return 0;
  6871. }
  6872. struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
  6873. int ndef)
  6874. {
  6875. struct wpabuf *wsc, *p2p;
  6876. struct wpa_ssid *ssid;
  6877. u8 go_dev_addr[ETH_ALEN];
  6878. int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
  6879. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
  6880. wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
  6881. return NULL;
  6882. }
  6883. if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
  6884. wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
  6885. &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
  6886. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
  6887. return NULL;
  6888. }
  6889. if (cli_freq == 0) {
  6890. wsc = wps_build_nfc_handover_req_p2p(
  6891. wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
  6892. } else
  6893. wsc = NULL;
  6894. p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
  6895. go_dev_addr, ssid ? ssid->ssid : NULL,
  6896. ssid ? ssid->ssid_len : 0);
  6897. return wpas_p2p_nfc_handover(ndef, wsc, p2p);
  6898. }
  6899. struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
  6900. int ndef, int tag)
  6901. {
  6902. struct wpabuf *wsc, *p2p;
  6903. struct wpa_ssid *ssid;
  6904. u8 go_dev_addr[ETH_ALEN];
  6905. int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
  6906. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  6907. return NULL;
  6908. if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
  6909. wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
  6910. &wpa_s->conf->wps_nfc_dh_privkey) < 0)
  6911. return NULL;
  6912. if (cli_freq == 0) {
  6913. wsc = wps_build_nfc_handover_sel_p2p(
  6914. wpa_s->parent->wps,
  6915. tag ? wpa_s->conf->wps_nfc_dev_pw_id :
  6916. DEV_PW_NFC_CONNECTION_HANDOVER,
  6917. wpa_s->conf->wps_nfc_dh_pubkey,
  6918. tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
  6919. } else
  6920. wsc = NULL;
  6921. p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
  6922. go_dev_addr, ssid ? ssid->ssid : NULL,
  6923. ssid ? ssid->ssid_len : 0);
  6924. return wpas_p2p_nfc_handover(ndef, wsc, p2p);
  6925. }
  6926. static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
  6927. struct p2p_nfc_params *params)
  6928. {
  6929. wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
  6930. "connection handover (freq=%d)",
  6931. params->go_freq);
  6932. if (params->go_freq && params->go_ssid_len) {
  6933. wpa_s->p2p_wps_method = WPS_NFC;
  6934. wpa_s->pending_join_wps_method = WPS_NFC;
  6935. os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
  6936. os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
  6937. ETH_ALEN);
  6938. return wpas_p2p_join_start(wpa_s, params->go_freq,
  6939. params->go_ssid,
  6940. params->go_ssid_len);
  6941. }
  6942. return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
  6943. WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
  6944. params->go_freq, -1, 0, 1, 1);
  6945. }
  6946. static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
  6947. struct p2p_nfc_params *params, int tag)
  6948. {
  6949. int res, persistent;
  6950. struct wpa_ssid *ssid;
  6951. wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
  6952. "connection handover");
  6953. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  6954. ssid = wpa_s->current_ssid;
  6955. if (ssid == NULL)
  6956. continue;
  6957. if (ssid->mode != WPAS_MODE_P2P_GO)
  6958. continue;
  6959. if (wpa_s->ap_iface == NULL)
  6960. continue;
  6961. break;
  6962. }
  6963. if (wpa_s == NULL) {
  6964. wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
  6965. return -1;
  6966. }
  6967. if (wpa_s->parent->p2p_oob_dev_pw_id !=
  6968. DEV_PW_NFC_CONNECTION_HANDOVER &&
  6969. !wpa_s->parent->p2p_oob_dev_pw) {
  6970. wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
  6971. return -1;
  6972. }
  6973. res = wpas_ap_wps_add_nfc_pw(
  6974. wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
  6975. wpa_s->parent->p2p_oob_dev_pw,
  6976. wpa_s->parent->p2p_peer_oob_pk_hash_known ?
  6977. wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
  6978. if (res)
  6979. return res;
  6980. if (!tag) {
  6981. wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
  6982. return 0;
  6983. }
  6984. if (!params->peer ||
  6985. !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
  6986. return 0;
  6987. wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
  6988. " to join", MAC2STR(params->peer->p2p_device_addr));
  6989. wpa_s->global->p2p_invite_group = wpa_s;
  6990. persistent = ssid->p2p_persistent_group &&
  6991. wpas_p2p_get_persistent(wpa_s->parent,
  6992. params->peer->p2p_device_addr,
  6993. ssid->ssid, ssid->ssid_len);
  6994. wpa_s->parent->pending_invite_ssid_id = -1;
  6995. return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
  6996. P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
  6997. ssid->ssid, ssid->ssid_len, ssid->frequency,
  6998. wpa_s->global->p2p_dev_addr, persistent, 0,
  6999. wpa_s->parent->p2p_oob_dev_pw_id);
  7000. }
  7001. static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
  7002. struct p2p_nfc_params *params,
  7003. int forced_freq)
  7004. {
  7005. wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
  7006. "connection handover");
  7007. return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
  7008. WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
  7009. forced_freq, -1, 0, 1, 1);
  7010. }
  7011. static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
  7012. struct p2p_nfc_params *params,
  7013. int forced_freq)
  7014. {
  7015. int res;
  7016. wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
  7017. "connection handover");
  7018. res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
  7019. WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
  7020. forced_freq, -1, 0, 1, 1);
  7021. if (res)
  7022. return res;
  7023. res = wpas_p2p_listen(wpa_s, 60);
  7024. if (res) {
  7025. p2p_unauthorize(wpa_s->global->p2p,
  7026. params->peer->p2p_device_addr);
  7027. }
  7028. return res;
  7029. }
  7030. static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
  7031. const struct wpabuf *data,
  7032. int sel, int tag, int forced_freq)
  7033. {
  7034. const u8 *pos, *end;
  7035. u16 len, id;
  7036. struct p2p_nfc_params params;
  7037. int res;
  7038. os_memset(&params, 0, sizeof(params));
  7039. params.sel = sel;
  7040. wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
  7041. pos = wpabuf_head(data);
  7042. end = pos + wpabuf_len(data);
  7043. if (end - pos < 2) {
  7044. wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
  7045. "attributes");
  7046. return -1;
  7047. }
  7048. len = WPA_GET_BE16(pos);
  7049. pos += 2;
  7050. if (len > end - pos) {
  7051. wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
  7052. "attributes");
  7053. return -1;
  7054. }
  7055. params.wsc_attr = pos;
  7056. params.wsc_len = len;
  7057. pos += len;
  7058. if (end - pos < 2) {
  7059. wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
  7060. "attributes");
  7061. return -1;
  7062. }
  7063. len = WPA_GET_BE16(pos);
  7064. pos += 2;
  7065. if (len > end - pos) {
  7066. wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
  7067. "attributes");
  7068. return -1;
  7069. }
  7070. params.p2p_attr = pos;
  7071. params.p2p_len = len;
  7072. pos += len;
  7073. wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
  7074. params.wsc_attr, params.wsc_len);
  7075. wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
  7076. params.p2p_attr, params.p2p_len);
  7077. if (pos < end) {
  7078. wpa_hexdump(MSG_DEBUG,
  7079. "P2P: Ignored extra data after P2P attributes",
  7080. pos, end - pos);
  7081. }
  7082. res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
  7083. if (res)
  7084. return res;
  7085. if (params.next_step == NO_ACTION)
  7086. return 0;
  7087. if (params.next_step == BOTH_GO) {
  7088. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
  7089. MAC2STR(params.peer->p2p_device_addr));
  7090. return 0;
  7091. }
  7092. if (params.next_step == PEER_CLIENT) {
  7093. if (!is_zero_ether_addr(params.go_dev_addr)) {
  7094. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
  7095. "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
  7096. " ssid=\"%s\"",
  7097. MAC2STR(params.peer->p2p_device_addr),
  7098. params.go_freq,
  7099. MAC2STR(params.go_dev_addr),
  7100. wpa_ssid_txt(params.go_ssid,
  7101. params.go_ssid_len));
  7102. } else {
  7103. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
  7104. "peer=" MACSTR " freq=%d",
  7105. MAC2STR(params.peer->p2p_device_addr),
  7106. params.go_freq);
  7107. }
  7108. return 0;
  7109. }
  7110. if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
  7111. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
  7112. MACSTR, MAC2STR(params.peer->p2p_device_addr));
  7113. return 0;
  7114. }
  7115. wpabuf_free(wpa_s->p2p_oob_dev_pw);
  7116. wpa_s->p2p_oob_dev_pw = NULL;
  7117. if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
  7118. wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
  7119. "received");
  7120. return -1;
  7121. }
  7122. id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
  7123. wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
  7124. wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
  7125. params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
  7126. os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
  7127. params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
  7128. wpa_s->p2p_peer_oob_pk_hash_known = 1;
  7129. if (tag) {
  7130. if (id < 0x10) {
  7131. wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
  7132. "peer OOB Device Password Id %u", id);
  7133. return -1;
  7134. }
  7135. wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
  7136. "Device Password Id %u", id);
  7137. wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
  7138. params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
  7139. params.oob_dev_pw_len -
  7140. WPS_OOB_PUBKEY_HASH_LEN - 2);
  7141. wpa_s->p2p_oob_dev_pw_id = id;
  7142. wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
  7143. params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
  7144. params.oob_dev_pw_len -
  7145. WPS_OOB_PUBKEY_HASH_LEN - 2);
  7146. if (wpa_s->p2p_oob_dev_pw == NULL)
  7147. return -1;
  7148. if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
  7149. wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
  7150. &wpa_s->conf->wps_nfc_dh_privkey) < 0)
  7151. return -1;
  7152. } else {
  7153. wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
  7154. "without Device Password");
  7155. wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
  7156. }
  7157. switch (params.next_step) {
  7158. case NO_ACTION:
  7159. case BOTH_GO:
  7160. case PEER_CLIENT:
  7161. /* already covered above */
  7162. return 0;
  7163. case JOIN_GROUP:
  7164. return wpas_p2p_nfc_join_group(wpa_s, &params);
  7165. case AUTH_JOIN:
  7166. return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
  7167. case INIT_GO_NEG:
  7168. return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
  7169. case RESP_GO_NEG:
  7170. /* TODO: use own OOB Dev Pw */
  7171. return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
  7172. }
  7173. return -1;
  7174. }
  7175. int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
  7176. const struct wpabuf *data, int forced_freq)
  7177. {
  7178. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  7179. return -1;
  7180. return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
  7181. }
  7182. int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
  7183. const struct wpabuf *req,
  7184. const struct wpabuf *sel, int forced_freq)
  7185. {
  7186. struct wpabuf *tmp;
  7187. int ret;
  7188. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  7189. return -1;
  7190. wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
  7191. wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
  7192. wpabuf_head(req), wpabuf_len(req));
  7193. wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
  7194. wpabuf_head(sel), wpabuf_len(sel));
  7195. if (forced_freq)
  7196. wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
  7197. tmp = ndef_parse_p2p(init ? sel : req);
  7198. if (tmp == NULL) {
  7199. wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
  7200. return -1;
  7201. }
  7202. ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
  7203. forced_freq);
  7204. wpabuf_free(tmp);
  7205. return ret;
  7206. }
  7207. int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
  7208. {
  7209. const u8 *if_addr;
  7210. int go_intent = wpa_s->conf->p2p_go_intent;
  7211. struct wpa_supplicant *iface;
  7212. if (wpa_s->global->p2p == NULL)
  7213. return -1;
  7214. if (!enabled) {
  7215. wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
  7216. for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
  7217. {
  7218. if (!iface->ap_iface)
  7219. continue;
  7220. hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
  7221. }
  7222. p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
  7223. 0, NULL);
  7224. if (wpa_s->p2p_nfc_tag_enabled)
  7225. wpas_p2p_remove_pending_group_interface(wpa_s);
  7226. wpa_s->p2p_nfc_tag_enabled = 0;
  7227. return 0;
  7228. }
  7229. if (wpa_s->global->p2p_disabled)
  7230. return -1;
  7231. if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
  7232. wpa_s->conf->wps_nfc_dh_privkey == NULL ||
  7233. wpa_s->conf->wps_nfc_dev_pw == NULL ||
  7234. wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
  7235. wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
  7236. "to allow static handover cases");
  7237. return -1;
  7238. }
  7239. wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
  7240. wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
  7241. wpabuf_free(wpa_s->p2p_oob_dev_pw);
  7242. wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
  7243. if (wpa_s->p2p_oob_dev_pw == NULL)
  7244. return -1;
  7245. wpa_s->p2p_peer_oob_pk_hash_known = 0;
  7246. if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
  7247. wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
  7248. /*
  7249. * P2P Group Interface present and the command came on group
  7250. * interface, so enable the token for the current interface.
  7251. */
  7252. wpa_s->create_p2p_iface = 0;
  7253. } else {
  7254. wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
  7255. }
  7256. if (wpa_s->create_p2p_iface) {
  7257. enum wpa_driver_if_type iftype;
  7258. /* Prepare to add a new interface for the group */
  7259. iftype = WPA_IF_P2P_GROUP;
  7260. if (go_intent == 15)
  7261. iftype = WPA_IF_P2P_GO;
  7262. if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
  7263. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  7264. "interface for the group");
  7265. return -1;
  7266. }
  7267. if_addr = wpa_s->pending_interface_addr;
  7268. } else
  7269. if_addr = wpa_s->own_addr;
  7270. wpa_s->p2p_nfc_tag_enabled = enabled;
  7271. for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
  7272. struct hostapd_data *hapd;
  7273. if (iface->ap_iface == NULL)
  7274. continue;
  7275. hapd = iface->ap_iface->bss[0];
  7276. wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
  7277. hapd->conf->wps_nfc_dh_pubkey =
  7278. wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
  7279. wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
  7280. hapd->conf->wps_nfc_dh_privkey =
  7281. wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
  7282. wpabuf_free(hapd->conf->wps_nfc_dev_pw);
  7283. hapd->conf->wps_nfc_dev_pw =
  7284. wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
  7285. hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
  7286. if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
  7287. wpa_dbg(iface, MSG_DEBUG,
  7288. "P2P: Failed to enable NFC Tag for GO");
  7289. }
  7290. }
  7291. p2p_set_authorized_oob_dev_pw_id(
  7292. wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
  7293. if_addr);
  7294. return 0;
  7295. }
  7296. #endif /* CONFIG_WPS_NFC */
  7297. static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
  7298. struct wpa_used_freq_data *freqs,
  7299. unsigned int num)
  7300. {
  7301. u8 curr_chan, cand, chan;
  7302. unsigned int i;
  7303. /*
  7304. * If possible, optimize the Listen channel to be a channel that is
  7305. * already used by one of the other interfaces.
  7306. */
  7307. if (!wpa_s->conf->p2p_optimize_listen_chan)
  7308. return;
  7309. if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
  7310. return;
  7311. curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
  7312. for (i = 0, cand = 0; i < num; i++) {
  7313. ieee80211_freq_to_chan(freqs[i].freq, &chan);
  7314. if (curr_chan == chan) {
  7315. cand = 0;
  7316. break;
  7317. }
  7318. if (chan == 1 || chan == 6 || chan == 11)
  7319. cand = chan;
  7320. }
  7321. if (cand) {
  7322. wpa_dbg(wpa_s, MSG_DEBUG,
  7323. "P2P: Update Listen channel to %u based on operating channel",
  7324. cand);
  7325. p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
  7326. }
  7327. }
  7328. static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
  7329. {
  7330. struct hostapd_config *conf;
  7331. struct p2p_go_neg_results params;
  7332. struct csa_settings csa_settings;
  7333. struct wpa_ssid *current_ssid = wpa_s->current_ssid;
  7334. int old_freq = current_ssid->frequency;
  7335. int ret;
  7336. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
  7337. wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
  7338. return -1;
  7339. }
  7340. /*
  7341. * TODO: This function may not always work correctly. For example,
  7342. * when we have a running GO and a BSS on a DFS channel.
  7343. */
  7344. if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, NULL)) {
  7345. wpa_dbg(wpa_s, MSG_DEBUG,
  7346. "P2P CSA: Failed to select new frequency for GO");
  7347. return -1;
  7348. }
  7349. if (current_ssid->frequency == params.freq) {
  7350. wpa_dbg(wpa_s, MSG_DEBUG,
  7351. "P2P CSA: Selected same frequency - not moving GO");
  7352. return 0;
  7353. }
  7354. conf = hostapd_config_defaults();
  7355. if (!conf) {
  7356. wpa_dbg(wpa_s, MSG_DEBUG,
  7357. "P2P CSA: Failed to allocate default config");
  7358. return -1;
  7359. }
  7360. current_ssid->frequency = params.freq;
  7361. if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
  7362. wpa_dbg(wpa_s, MSG_DEBUG,
  7363. "P2P CSA: Failed to create new GO config");
  7364. ret = -1;
  7365. goto out;
  7366. }
  7367. if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode) {
  7368. wpa_dbg(wpa_s, MSG_DEBUG,
  7369. "P2P CSA: CSA to a different band is not supported");
  7370. ret = -1;
  7371. goto out;
  7372. }
  7373. os_memset(&csa_settings, 0, sizeof(csa_settings));
  7374. csa_settings.cs_count = P2P_GO_CSA_COUNT;
  7375. csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
  7376. csa_settings.freq_params.freq = params.freq;
  7377. csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
  7378. csa_settings.freq_params.ht_enabled = conf->ieee80211n;
  7379. csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
  7380. if (conf->ieee80211ac) {
  7381. int freq1 = 0, freq2 = 0;
  7382. u8 chan, opclass;
  7383. if (ieee80211_freq_to_channel_ext(params.freq,
  7384. conf->secondary_channel,
  7385. conf->vht_oper_chwidth,
  7386. &opclass, &chan) ==
  7387. NUM_HOSTAPD_MODES) {
  7388. wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
  7389. ret = -1;
  7390. goto out;
  7391. }
  7392. if (conf->vht_oper_centr_freq_seg0_idx)
  7393. freq1 = ieee80211_chan_to_freq(
  7394. NULL, opclass,
  7395. conf->vht_oper_centr_freq_seg0_idx);
  7396. if (conf->vht_oper_centr_freq_seg1_idx)
  7397. freq2 = ieee80211_chan_to_freq(
  7398. NULL, opclass,
  7399. conf->vht_oper_centr_freq_seg1_idx);
  7400. if (freq1 < 0 || freq2 < 0) {
  7401. wpa_dbg(wpa_s, MSG_DEBUG,
  7402. "P2P CSA: Selected invalid VHT center freqs");
  7403. ret = -1;
  7404. goto out;
  7405. }
  7406. csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
  7407. csa_settings.freq_params.center_freq1 = freq1;
  7408. csa_settings.freq_params.center_freq2 = freq2;
  7409. switch (conf->vht_oper_chwidth) {
  7410. case VHT_CHANWIDTH_80MHZ:
  7411. case VHT_CHANWIDTH_80P80MHZ:
  7412. csa_settings.freq_params.bandwidth = 80;
  7413. break;
  7414. case VHT_CHANWIDTH_160MHZ:
  7415. csa_settings.freq_params.bandwidth = 160;
  7416. break;
  7417. }
  7418. }
  7419. ret = ap_switch_channel(wpa_s, &csa_settings);
  7420. out:
  7421. current_ssid->frequency = old_freq;
  7422. hostapd_config_free(conf);
  7423. return ret;
  7424. }
  7425. static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
  7426. {
  7427. struct p2p_go_neg_results params;
  7428. struct wpa_ssid *current_ssid = wpa_s->current_ssid;
  7429. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
  7430. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
  7431. current_ssid->frequency);
  7432. /* Stop the AP functionality */
  7433. /* TODO: Should do this in a way that does not indicated to possible
  7434. * P2P Clients in the group that the group is terminated. */
  7435. wpa_supplicant_ap_deinit(wpa_s);
  7436. /* Reselect the GO frequency */
  7437. if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, NULL)) {
  7438. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
  7439. wpas_p2p_group_delete(wpa_s,
  7440. P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
  7441. return;
  7442. }
  7443. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
  7444. params.freq);
  7445. if (params.freq &&
  7446. !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
  7447. wpa_printf(MSG_DEBUG,
  7448. "P2P: Selected freq (%u MHz) is not valid for P2P",
  7449. params.freq);
  7450. wpas_p2p_group_delete(wpa_s,
  7451. P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
  7452. return;
  7453. }
  7454. /* Update the frequency */
  7455. current_ssid->frequency = params.freq;
  7456. wpa_s->connect_without_scan = current_ssid;
  7457. wpa_s->reassociate = 1;
  7458. wpa_s->disconnected = 0;
  7459. wpa_supplicant_req_scan(wpa_s, 0, 0);
  7460. }
  7461. static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
  7462. {
  7463. struct wpa_supplicant *wpa_s = eloop_ctx;
  7464. if (!wpa_s->ap_iface || !wpa_s->current_ssid)
  7465. return;
  7466. wpas_p2p_go_update_common_freqs(wpa_s);
  7467. /* Do not move GO in the middle of a CSA */
  7468. if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
  7469. wpa_printf(MSG_DEBUG,
  7470. "P2P: CSA is in progress - not moving GO");
  7471. return;
  7472. }
  7473. /*
  7474. * First, try a channel switch flow. If it is not supported or fails,
  7475. * take down the GO and bring it up again.
  7476. */
  7477. if (wpas_p2p_move_go_csa(wpa_s) < 0)
  7478. wpas_p2p_move_go_no_csa(wpa_s);
  7479. }
  7480. static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
  7481. {
  7482. struct wpa_supplicant *wpa_s = eloop_ctx;
  7483. struct wpa_used_freq_data *freqs = NULL;
  7484. unsigned int num = wpa_s->num_multichan_concurrent;
  7485. freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
  7486. if (!freqs)
  7487. return;
  7488. num = get_shared_radio_freqs_data(wpa_s, freqs, num);
  7489. /* Previous attempt to move a GO was not possible -- try again. */
  7490. wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
  7491. WPAS_P2P_CHANNEL_UPDATE_ANY);
  7492. os_free(freqs);
  7493. }
  7494. /*
  7495. * Consider moving a GO from its currently used frequency:
  7496. * 1. It is possible that due to regulatory consideration the frequency
  7497. * can no longer be used and there is a need to evacuate the GO.
  7498. * 2. It is possible that due to MCC considerations, it would be preferable
  7499. * to move the GO to a channel that is currently used by some other
  7500. * station interface.
  7501. *
  7502. * In case a frequency that became invalid is once again valid, cancel a
  7503. * previously initiated GO frequency change.
  7504. */
  7505. static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
  7506. struct wpa_used_freq_data *freqs,
  7507. unsigned int num)
  7508. {
  7509. unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
  7510. unsigned int timeout;
  7511. int freq;
  7512. wpas_p2p_go_update_common_freqs(wpa_s);
  7513. freq = wpa_s->current_ssid->frequency;
  7514. for (i = 0, invalid_freq = 0; i < num; i++) {
  7515. if (freqs[i].freq == freq) {
  7516. flags = freqs[i].flags;
  7517. /* The channel is invalid, must change it */
  7518. if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
  7519. wpa_dbg(wpa_s, MSG_DEBUG,
  7520. "P2P: Freq=%d MHz no longer valid for GO",
  7521. freq);
  7522. invalid_freq = 1;
  7523. }
  7524. } else if (freqs[i].flags == 0) {
  7525. /* Freq is not used by any other station interface */
  7526. continue;
  7527. } else if (!p2p_supported_freq(wpa_s->global->p2p,
  7528. freqs[i].freq)) {
  7529. /* Freq is not valid for P2P use cases */
  7530. continue;
  7531. } else if (wpa_s->conf->p2p_go_freq_change_policy ==
  7532. P2P_GO_FREQ_MOVE_SCM) {
  7533. policy_move = 1;
  7534. } else if (wpa_s->conf->p2p_go_freq_change_policy ==
  7535. P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
  7536. wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
  7537. policy_move = 1;
  7538. } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
  7539. P2P_GO_FREQ_MOVE_SCM_ECSA) &&
  7540. wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
  7541. if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
  7542. policy_move = 1;
  7543. } else if ((wpa_s->drv_flags &
  7544. WPA_DRIVER_FLAGS_AP_CSA) &&
  7545. wpas_p2p_go_clients_support_ecsa(wpa_s)) {
  7546. u8 chan;
  7547. /*
  7548. * We do not support CSA between bands, so move
  7549. * GO only within the same band.
  7550. */
  7551. if (wpa_s->ap_iface->current_mode->mode ==
  7552. ieee80211_freq_to_chan(freqs[i].freq,
  7553. &chan))
  7554. policy_move = 1;
  7555. }
  7556. }
  7557. }
  7558. wpa_dbg(wpa_s, MSG_DEBUG,
  7559. "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
  7560. invalid_freq, policy_move, flags);
  7561. /*
  7562. * The channel is valid, or we are going to have a policy move, so
  7563. * cancel timeout.
  7564. */
  7565. if (!invalid_freq || policy_move) {
  7566. wpa_dbg(wpa_s, MSG_DEBUG,
  7567. "P2P: Cancel a GO move from freq=%d MHz", freq);
  7568. eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
  7569. if (wpas_p2p_in_progress(wpa_s)) {
  7570. wpa_dbg(wpa_s, MSG_DEBUG,
  7571. "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
  7572. eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
  7573. wpa_s, NULL);
  7574. eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
  7575. wpas_p2p_reconsider_moving_go,
  7576. wpa_s, NULL);
  7577. return;
  7578. }
  7579. }
  7580. if (!invalid_freq && (!policy_move || flags != 0)) {
  7581. wpa_dbg(wpa_s, MSG_DEBUG,
  7582. "P2P: Not initiating a GO frequency change");
  7583. return;
  7584. }
  7585. /*
  7586. * Do not consider moving GO if it is in the middle of a CSA. When the
  7587. * CSA is finished this flow should be retriggered.
  7588. */
  7589. if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
  7590. wpa_dbg(wpa_s, MSG_DEBUG,
  7591. "P2P: Not initiating a GO frequency change - CSA is in progress");
  7592. return;
  7593. }
  7594. if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
  7595. timeout = P2P_GO_FREQ_CHANGE_TIME;
  7596. else
  7597. timeout = 0;
  7598. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
  7599. freq, timeout);
  7600. eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
  7601. eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
  7602. }
  7603. static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
  7604. struct wpa_used_freq_data *freqs,
  7605. unsigned int num,
  7606. enum wpas_p2p_channel_update_trig trig)
  7607. {
  7608. struct wpa_supplicant *ifs;
  7609. eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
  7610. NULL);
  7611. /*
  7612. * Travers all the radio interfaces, and for each GO interface, check
  7613. * if there is a need to move the GO from the frequency it is using,
  7614. * or in case the frequency is valid again, cancel the evacuation flow.
  7615. */
  7616. dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
  7617. radio_list) {
  7618. if (ifs->current_ssid == NULL ||
  7619. ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
  7620. continue;
  7621. /*
  7622. * The GO was just started or completed channel switch, no need
  7623. * to move it.
  7624. */
  7625. if (wpa_s == ifs &&
  7626. (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
  7627. trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
  7628. wpa_dbg(wpa_s, MSG_DEBUG,
  7629. "P2P: GO move - schedule re-consideration");
  7630. eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
  7631. wpas_p2p_reconsider_moving_go,
  7632. wpa_s, NULL);
  7633. continue;
  7634. }
  7635. wpas_p2p_consider_moving_one_go(ifs, freqs, num);
  7636. }
  7637. }
  7638. void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
  7639. {
  7640. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  7641. return;
  7642. wpas_p2p_update_channel_list(wpa_s,
  7643. WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
  7644. }
  7645. void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
  7646. {
  7647. if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
  7648. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
  7649. "the management interface is being removed");
  7650. wpas_p2p_deinit_global(wpa_s->global);
  7651. }
  7652. }
  7653. void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
  7654. {
  7655. if (wpa_s->ap_iface->bss)
  7656. wpa_s->ap_iface->bss[0]->p2p_group = NULL;
  7657. wpas_p2p_group_deinit(wpa_s);
  7658. }