p2p_supplicant.c 262 KB

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