p2p_supplicant.c 263 KB

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