p2p_supplicant.c 256 KB

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