p2p_supplicant.c 259 KB

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