p2p_supplicant.c 252 KB

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