p2p_supplicant.c 259 KB

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