p2p_supplicant.c 260 KB

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