p2p_supplicant.c 259 KB

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