p2p_supplicant.c 192 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921
  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/p2p_hostapd.h"
  22. #include "eapol_supp/eapol_supp_sm.h"
  23. #include "rsn_supp/wpa.h"
  24. #include "wpa_supplicant_i.h"
  25. #include "driver_i.h"
  26. #include "ap.h"
  27. #include "config_ssid.h"
  28. #include "config.h"
  29. #include "notify.h"
  30. #include "scan.h"
  31. #include "bss.h"
  32. #include "offchannel.h"
  33. #include "wps_supplicant.h"
  34. #include "p2p_supplicant.h"
  35. #include "wifi_display.h"
  36. /*
  37. * How many times to try to scan to find the GO before giving up on join
  38. * request.
  39. */
  40. #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
  41. #define P2P_AUTO_PD_SCAN_ATTEMPTS 5
  42. #ifndef P2P_MAX_CLIENT_IDLE
  43. /*
  44. * How many seconds to try to reconnect to the GO when connection in P2P client
  45. * role has been lost.
  46. */
  47. #define P2P_MAX_CLIENT_IDLE 10
  48. #endif /* P2P_MAX_CLIENT_IDLE */
  49. #ifndef P2P_MAX_INITIAL_CONN_WAIT
  50. /*
  51. * How many seconds to wait for initial 4-way handshake to get completed after
  52. * WPS provisioning step.
  53. */
  54. #define P2P_MAX_INITIAL_CONN_WAIT 10
  55. #endif /* P2P_MAX_INITIAL_CONN_WAIT */
  56. #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
  57. /*
  58. * How many seconds to wait for initial 4-way handshake to get completed after
  59. * WPS provisioning step on the GO. This controls the extra time the P2P
  60. * operation is considered to be in progress (e.g., to delay other scans) after
  61. * WPS provisioning has been completed on the GO during group formation.
  62. */
  63. #define P2P_MAX_INITIAL_CONN_WAIT_GO 10
  64. #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
  65. #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
  66. /*
  67. * How many seconds to wait for initial 4-way handshake to get completed after
  68. * re-invocation of a persistent group on the GO when the client is expected
  69. * to connect automatically (no user interaction).
  70. */
  71. #define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
  72. #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
  73. #ifndef P2P_CONCURRENT_SEARCH_DELAY
  74. #define P2P_CONCURRENT_SEARCH_DELAY 500
  75. #endif /* P2P_CONCURRENT_SEARCH_DELAY */
  76. #define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
  77. enum p2p_group_removal_reason {
  78. P2P_GROUP_REMOVAL_UNKNOWN,
  79. P2P_GROUP_REMOVAL_SILENT,
  80. P2P_GROUP_REMOVAL_FORMATION_FAILED,
  81. P2P_GROUP_REMOVAL_REQUESTED,
  82. P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
  83. P2P_GROUP_REMOVAL_UNAVAILABLE,
  84. P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
  85. P2P_GROUP_REMOVAL_PSK_FAILURE,
  86. P2P_GROUP_REMOVAL_FREQ_CONFLICT
  87. };
  88. static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
  89. static struct wpa_supplicant *
  90. wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
  91. int go);
  92. static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
  93. static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
  94. const u8 *ssid, size_t ssid_len);
  95. static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
  96. static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
  97. const u8 *dev_addr, enum p2p_wps_method wps_method,
  98. int auto_join, const u8 *ssid, size_t ssid_len);
  99. static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
  100. static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
  101. static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
  102. static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
  103. static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
  104. void *timeout_ctx);
  105. static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
  106. static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
  107. int group_added);
  108. static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
  109. /*
  110. * Get the number of concurrent channels that the HW can operate, but that are
  111. * currently not in use by any of the wpa_supplicant interfaces.
  112. */
  113. static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
  114. {
  115. int *freqs;
  116. int num, unused;
  117. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  118. if (!freqs)
  119. return -1;
  120. num = get_shared_radio_freqs(wpa_s, freqs,
  121. wpa_s->num_multichan_concurrent);
  122. os_free(freqs);
  123. unused = wpa_s->num_multichan_concurrent - num;
  124. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
  125. return unused;
  126. }
  127. /*
  128. * Get the frequencies that are currently in use by one or more of the virtual
  129. * interfaces, and that are also valid for P2P operation.
  130. */
  131. static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
  132. int *p2p_freqs, unsigned int len)
  133. {
  134. int *freqs;
  135. unsigned int num, i, j;
  136. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  137. if (!freqs)
  138. return -1;
  139. num = get_shared_radio_freqs(wpa_s, freqs,
  140. wpa_s->num_multichan_concurrent);
  141. os_memset(p2p_freqs, 0, sizeof(int) * len);
  142. for (i = 0, j = 0; i < num && j < len; i++) {
  143. if (p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
  144. p2p_freqs[j++] = freqs[i];
  145. }
  146. os_free(freqs);
  147. dump_freq_array(wpa_s, "valid for P2P", p2p_freqs, j);
  148. return j;
  149. }
  150. static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
  151. int freq)
  152. {
  153. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  154. return;
  155. if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
  156. freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
  157. wpas_p2p_num_unused_channels(wpa_s) > 0) {
  158. wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
  159. freq);
  160. freq = 0;
  161. }
  162. p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
  163. }
  164. static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
  165. struct wpa_scan_results *scan_res)
  166. {
  167. size_t i;
  168. if (wpa_s->p2p_scan_work) {
  169. struct wpa_radio_work *work = wpa_s->p2p_scan_work;
  170. wpa_s->p2p_scan_work = NULL;
  171. radio_work_done(work);
  172. }
  173. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  174. return;
  175. wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
  176. (int) scan_res->num);
  177. for (i = 0; i < scan_res->num; i++) {
  178. struct wpa_scan_res *bss = scan_res->res[i];
  179. struct os_reltime time_tmp_age, entry_ts;
  180. const u8 *ies;
  181. size_t ies_len;
  182. time_tmp_age.sec = bss->age / 1000;
  183. time_tmp_age.usec = (bss->age % 1000) * 1000;
  184. os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
  185. ies = (const u8 *) (bss + 1);
  186. ies_len = bss->ie_len;
  187. if (bss->beacon_ie_len > 0 &&
  188. !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
  189. wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
  190. wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
  191. MACSTR, MAC2STR(bss->bssid));
  192. ies = ies + ies_len;
  193. ies_len = bss->beacon_ie_len;
  194. }
  195. if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
  196. bss->freq, &entry_ts, bss->level,
  197. ies, ies_len) > 0)
  198. break;
  199. }
  200. p2p_scan_res_handled(wpa_s->global->p2p);
  201. }
  202. static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
  203. {
  204. struct wpa_supplicant *wpa_s = work->wpa_s;
  205. struct wpa_driver_scan_params *params = work->ctx;
  206. int ret;
  207. if (deinit) {
  208. wpa_scan_free_params(params);
  209. return;
  210. }
  211. ret = wpa_drv_scan(wpa_s, params);
  212. wpa_scan_free_params(params);
  213. work->ctx = NULL;
  214. if (ret) {
  215. radio_work_done(work);
  216. return;
  217. }
  218. os_get_reltime(&wpa_s->scan_trigger_time);
  219. wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
  220. wpa_s->own_scan_requested = 1;
  221. wpa_s->p2p_scan_work = work;
  222. }
  223. static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
  224. unsigned int num_req_dev_types,
  225. const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
  226. {
  227. struct wpa_supplicant *wpa_s = ctx;
  228. struct wpa_driver_scan_params *params = NULL;
  229. struct wpabuf *wps_ie, *ies;
  230. size_t ielen;
  231. u8 *n;
  232. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  233. return -1;
  234. if (wpa_s->p2p_scan_work) {
  235. wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
  236. return -1;
  237. }
  238. params = os_zalloc(sizeof(*params));
  239. if (params == NULL)
  240. return -1;
  241. /* P2P Wildcard SSID */
  242. params->num_ssids = 1;
  243. n = os_malloc(P2P_WILDCARD_SSID_LEN);
  244. if (n == NULL)
  245. goto fail;
  246. os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
  247. params->ssids[0].ssid = n;
  248. params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
  249. wpa_s->wps->dev.p2p = 1;
  250. wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
  251. wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
  252. num_req_dev_types, req_dev_types);
  253. if (wps_ie == NULL)
  254. goto fail;
  255. ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
  256. ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
  257. if (ies == NULL) {
  258. wpabuf_free(wps_ie);
  259. goto fail;
  260. }
  261. wpabuf_put_buf(ies, wps_ie);
  262. wpabuf_free(wps_ie);
  263. p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
  264. params->p2p_probe = 1;
  265. n = os_malloc(wpabuf_len(ies));
  266. if (n == NULL) {
  267. wpabuf_free(ies);
  268. goto fail;
  269. }
  270. os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
  271. params->extra_ies = n;
  272. params->extra_ies_len = wpabuf_len(ies);
  273. wpabuf_free(ies);
  274. switch (type) {
  275. case P2P_SCAN_SOCIAL:
  276. params->freqs = os_malloc(4 * sizeof(int));
  277. if (params->freqs == NULL)
  278. goto fail;
  279. params->freqs[0] = 2412;
  280. params->freqs[1] = 2437;
  281. params->freqs[2] = 2462;
  282. params->freqs[3] = 0;
  283. break;
  284. case P2P_SCAN_FULL:
  285. break;
  286. case P2P_SCAN_SOCIAL_PLUS_ONE:
  287. params->freqs = os_malloc(5 * sizeof(int));
  288. if (params->freqs == NULL)
  289. goto fail;
  290. params->freqs[0] = 2412;
  291. params->freqs[1] = 2437;
  292. params->freqs[2] = 2462;
  293. params->freqs[3] = freq;
  294. params->freqs[4] = 0;
  295. break;
  296. }
  297. radio_remove_unstarted_work(wpa_s, "p2p-scan");
  298. if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
  299. params) < 0)
  300. goto fail;
  301. return 0;
  302. fail:
  303. wpa_scan_free_params(params);
  304. return -1;
  305. }
  306. static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
  307. {
  308. switch (p2p_group_interface) {
  309. case P2P_GROUP_INTERFACE_PENDING:
  310. return WPA_IF_P2P_GROUP;
  311. case P2P_GROUP_INTERFACE_GO:
  312. return WPA_IF_P2P_GO;
  313. case P2P_GROUP_INTERFACE_CLIENT:
  314. return WPA_IF_P2P_CLIENT;
  315. }
  316. return WPA_IF_P2P_GROUP;
  317. }
  318. static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
  319. const u8 *ssid,
  320. size_t ssid_len, int *go)
  321. {
  322. struct wpa_ssid *s;
  323. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  324. for (s = wpa_s->conf->ssid; s; s = s->next) {
  325. if (s->disabled != 0 || !s->p2p_group ||
  326. s->ssid_len != ssid_len ||
  327. os_memcmp(ssid, s->ssid, ssid_len) != 0)
  328. continue;
  329. if (s->mode == WPAS_MODE_P2P_GO &&
  330. s != wpa_s->current_ssid)
  331. continue;
  332. if (go)
  333. *go = s->mode == WPAS_MODE_P2P_GO;
  334. return wpa_s;
  335. }
  336. }
  337. return NULL;
  338. }
  339. static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
  340. enum p2p_group_removal_reason removal_reason)
  341. {
  342. struct wpa_ssid *ssid;
  343. char *gtype;
  344. const char *reason;
  345. ssid = wpa_s->current_ssid;
  346. if (ssid == NULL) {
  347. /*
  348. * The current SSID was not known, but there may still be a
  349. * pending P2P group interface waiting for provisioning or a
  350. * P2P group that is trying to reconnect.
  351. */
  352. ssid = wpa_s->conf->ssid;
  353. while (ssid) {
  354. if (ssid->p2p_group && ssid->disabled != 2)
  355. break;
  356. ssid = ssid->next;
  357. }
  358. if (ssid == NULL &&
  359. wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
  360. {
  361. wpa_printf(MSG_ERROR, "P2P: P2P group interface "
  362. "not found");
  363. return -1;
  364. }
  365. }
  366. if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
  367. gtype = "GO";
  368. else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
  369. (ssid && ssid->mode == WPAS_MODE_INFRA)) {
  370. wpa_s->reassociate = 0;
  371. wpa_s->disconnected = 1;
  372. wpa_supplicant_deauthenticate(wpa_s,
  373. WLAN_REASON_DEAUTH_LEAVING);
  374. gtype = "client";
  375. } else
  376. gtype = "GO";
  377. if (wpa_s->cross_connect_in_use) {
  378. wpa_s->cross_connect_in_use = 0;
  379. wpa_msg_global(wpa_s->parent, MSG_INFO,
  380. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  381. wpa_s->ifname, wpa_s->cross_connect_uplink);
  382. }
  383. switch (removal_reason) {
  384. case P2P_GROUP_REMOVAL_REQUESTED:
  385. reason = " reason=REQUESTED";
  386. break;
  387. case P2P_GROUP_REMOVAL_FORMATION_FAILED:
  388. reason = " reason=FORMATION_FAILED";
  389. break;
  390. case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
  391. reason = " reason=IDLE";
  392. break;
  393. case P2P_GROUP_REMOVAL_UNAVAILABLE:
  394. reason = " reason=UNAVAILABLE";
  395. break;
  396. case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
  397. reason = " reason=GO_ENDING_SESSION";
  398. break;
  399. case P2P_GROUP_REMOVAL_PSK_FAILURE:
  400. reason = " reason=PSK_FAILURE";
  401. break;
  402. case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
  403. reason = " reason=FREQ_CONFLICT";
  404. break;
  405. default:
  406. reason = "";
  407. break;
  408. }
  409. if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
  410. wpa_msg_global(wpa_s->parent, MSG_INFO,
  411. P2P_EVENT_GROUP_REMOVED "%s %s%s",
  412. wpa_s->ifname, gtype, reason);
  413. }
  414. if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
  415. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
  416. if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
  417. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
  418. if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  419. wpa_s->parent, NULL) > 0) {
  420. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
  421. "timeout");
  422. wpa_s->p2p_in_provisioning = 0;
  423. }
  424. /*
  425. * Make sure wait for the first client does not remain active after the
  426. * group has been removed.
  427. */
  428. wpa_s->global->p2p_go_wait_client.sec = 0;
  429. if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
  430. wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
  431. if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
  432. struct wpa_global *global;
  433. char *ifname;
  434. enum wpa_driver_if_type type;
  435. wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
  436. wpa_s->ifname);
  437. global = wpa_s->global;
  438. ifname = os_strdup(wpa_s->ifname);
  439. type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
  440. wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
  441. wpa_s = global->ifaces;
  442. if (wpa_s && ifname)
  443. wpa_drv_if_remove(wpa_s, type, ifname);
  444. os_free(ifname);
  445. return 1;
  446. }
  447. if (!wpa_s->p2p_go_group_formation_completed) {
  448. wpa_s->global->p2p_group_formation = NULL;
  449. wpa_s->p2p_in_provisioning = 0;
  450. }
  451. wpa_s->show_group_started = 0;
  452. os_free(wpa_s->go_params);
  453. wpa_s->go_params = NULL;
  454. wpa_s->waiting_presence_resp = 0;
  455. wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
  456. if (ssid && (ssid->p2p_group ||
  457. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
  458. (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
  459. int id = ssid->id;
  460. if (ssid == wpa_s->current_ssid) {
  461. wpa_sm_set_config(wpa_s->wpa, NULL);
  462. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  463. wpa_s->current_ssid = NULL;
  464. }
  465. /*
  466. * Networks objects created during any P2P activities are not
  467. * exposed out as they might/will confuse certain non-P2P aware
  468. * applications since these network objects won't behave like
  469. * regular ones.
  470. *
  471. * Likewise, we don't send out network removed signals for such
  472. * network objects.
  473. */
  474. wpa_config_remove_network(wpa_s->conf, id);
  475. wpa_supplicant_clear_status(wpa_s);
  476. wpa_supplicant_cancel_sched_scan(wpa_s);
  477. } else {
  478. wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
  479. "found");
  480. }
  481. if (wpa_s->ap_iface)
  482. wpa_supplicant_ap_deinit(wpa_s);
  483. else
  484. wpa_drv_deinit_p2p_cli(wpa_s);
  485. return 0;
  486. }
  487. static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
  488. u8 *go_dev_addr,
  489. const u8 *ssid, size_t ssid_len)
  490. {
  491. struct wpa_bss *bss;
  492. const u8 *bssid;
  493. struct wpabuf *p2p;
  494. u8 group_capab;
  495. const u8 *addr;
  496. if (wpa_s->go_params)
  497. bssid = wpa_s->go_params->peer_interface_addr;
  498. else
  499. bssid = wpa_s->bssid;
  500. bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
  501. if (bss == NULL) {
  502. u8 iface_addr[ETH_ALEN];
  503. if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
  504. iface_addr) == 0)
  505. bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
  506. }
  507. if (bss == NULL) {
  508. wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
  509. "group is persistent - BSS " MACSTR " not found",
  510. MAC2STR(bssid));
  511. return 0;
  512. }
  513. p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  514. if (p2p == NULL)
  515. p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
  516. P2P_IE_VENDOR_TYPE);
  517. if (p2p == NULL) {
  518. wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
  519. "group is persistent - BSS " MACSTR
  520. " did not include P2P IE", MAC2STR(bssid));
  521. wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
  522. (u8 *) (bss + 1), bss->ie_len);
  523. wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
  524. ((u8 *) bss + 1) + bss->ie_len,
  525. bss->beacon_ie_len);
  526. return 0;
  527. }
  528. group_capab = p2p_get_group_capab(p2p);
  529. addr = p2p_get_go_dev_addr(p2p);
  530. wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
  531. "group_capab=0x%x", group_capab);
  532. if (addr) {
  533. os_memcpy(go_dev_addr, addr, ETH_ALEN);
  534. wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
  535. MAC2STR(addr));
  536. } else
  537. os_memset(go_dev_addr, 0, ETH_ALEN);
  538. wpabuf_free(p2p);
  539. wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
  540. "go_dev_addr=" MACSTR,
  541. MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
  542. return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
  543. }
  544. static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
  545. struct wpa_ssid *ssid,
  546. const u8 *go_dev_addr)
  547. {
  548. struct wpa_ssid *s;
  549. int changed = 0;
  550. wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
  551. "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
  552. for (s = wpa_s->conf->ssid; s; s = s->next) {
  553. if (s->disabled == 2 &&
  554. os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
  555. s->ssid_len == ssid->ssid_len &&
  556. os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
  557. break;
  558. }
  559. if (s) {
  560. wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
  561. "entry");
  562. if (ssid->passphrase && !s->passphrase)
  563. changed = 1;
  564. else if (ssid->passphrase && s->passphrase &&
  565. os_strcmp(ssid->passphrase, s->passphrase) != 0)
  566. changed = 1;
  567. } else {
  568. wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
  569. "entry");
  570. changed = 1;
  571. s = wpa_config_add_network(wpa_s->conf);
  572. if (s == NULL)
  573. return -1;
  574. /*
  575. * Instead of network_added we emit persistent_group_added
  576. * notification. Also to keep the defense checks in
  577. * persistent_group obj registration method, we set the
  578. * relevant flags in s to designate it as a persistent group.
  579. */
  580. s->p2p_group = 1;
  581. s->p2p_persistent_group = 1;
  582. wpas_notify_persistent_group_added(wpa_s, s);
  583. wpa_config_set_network_defaults(s);
  584. }
  585. s->p2p_group = 1;
  586. s->p2p_persistent_group = 1;
  587. s->disabled = 2;
  588. s->bssid_set = 1;
  589. os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
  590. s->mode = ssid->mode;
  591. s->auth_alg = WPA_AUTH_ALG_OPEN;
  592. s->key_mgmt = WPA_KEY_MGMT_PSK;
  593. s->proto = WPA_PROTO_RSN;
  594. s->pairwise_cipher = WPA_CIPHER_CCMP;
  595. s->export_keys = 1;
  596. if (ssid->passphrase) {
  597. os_free(s->passphrase);
  598. s->passphrase = os_strdup(ssid->passphrase);
  599. }
  600. if (ssid->psk_set) {
  601. s->psk_set = 1;
  602. os_memcpy(s->psk, ssid->psk, 32);
  603. }
  604. if (s->passphrase && !s->psk_set)
  605. wpa_config_update_psk(s);
  606. if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
  607. os_free(s->ssid);
  608. s->ssid = os_malloc(ssid->ssid_len);
  609. }
  610. if (s->ssid) {
  611. s->ssid_len = ssid->ssid_len;
  612. os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
  613. }
  614. if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
  615. dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
  616. wpa_s->global->add_psk = NULL;
  617. changed = 1;
  618. }
  619. #ifndef CONFIG_NO_CONFIG_WRITE
  620. if (changed && wpa_s->conf->update_config &&
  621. wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  622. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  623. }
  624. #endif /* CONFIG_NO_CONFIG_WRITE */
  625. return s->id;
  626. }
  627. static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
  628. const u8 *addr)
  629. {
  630. struct wpa_ssid *ssid, *s;
  631. u8 *n;
  632. size_t i;
  633. int found = 0;
  634. ssid = wpa_s->current_ssid;
  635. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  636. !ssid->p2p_persistent_group)
  637. return;
  638. for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
  639. if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
  640. continue;
  641. if (s->ssid_len == ssid->ssid_len &&
  642. os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
  643. break;
  644. }
  645. if (s == NULL)
  646. return;
  647. for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
  648. if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
  649. ETH_ALEN) != 0)
  650. continue;
  651. if (i == s->num_p2p_clients - 1)
  652. return; /* already the most recent entry */
  653. /* move the entry to mark it most recent */
  654. os_memmove(s->p2p_client_list + i * ETH_ALEN,
  655. s->p2p_client_list + (i + 1) * ETH_ALEN,
  656. (s->num_p2p_clients - i - 1) * ETH_ALEN);
  657. os_memcpy(s->p2p_client_list +
  658. (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
  659. found = 1;
  660. break;
  661. }
  662. if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
  663. n = os_realloc_array(s->p2p_client_list,
  664. s->num_p2p_clients + 1, ETH_ALEN);
  665. if (n == NULL)
  666. return;
  667. os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
  668. s->p2p_client_list = n;
  669. s->num_p2p_clients++;
  670. } else if (!found) {
  671. /* Not enough room for an additional entry - drop the oldest
  672. * entry */
  673. os_memmove(s->p2p_client_list,
  674. s->p2p_client_list + ETH_ALEN,
  675. (s->num_p2p_clients - 1) * ETH_ALEN);
  676. os_memcpy(s->p2p_client_list +
  677. (s->num_p2p_clients - 1) * ETH_ALEN,
  678. addr, ETH_ALEN);
  679. }
  680. #ifndef CONFIG_NO_CONFIG_WRITE
  681. if (wpa_s->parent->conf->update_config &&
  682. wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
  683. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  684. #endif /* CONFIG_NO_CONFIG_WRITE */
  685. }
  686. static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
  687. int success)
  688. {
  689. struct wpa_ssid *ssid;
  690. const char *ssid_txt;
  691. int client;
  692. int persistent;
  693. u8 go_dev_addr[ETH_ALEN];
  694. int network_id = -1;
  695. /*
  696. * This callback is likely called for the main interface. Update wpa_s
  697. * to use the group interface if a new interface was created for the
  698. * group.
  699. */
  700. if (wpa_s->global->p2p_group_formation)
  701. wpa_s = wpa_s->global->p2p_group_formation;
  702. if (wpa_s->p2p_go_group_formation_completed) {
  703. wpa_s->global->p2p_group_formation = NULL;
  704. wpa_s->p2p_in_provisioning = 0;
  705. }
  706. if (!success) {
  707. wpa_msg_global(wpa_s->parent, MSG_INFO,
  708. P2P_EVENT_GROUP_FORMATION_FAILURE);
  709. wpas_p2p_group_delete(wpa_s,
  710. P2P_GROUP_REMOVAL_FORMATION_FAILED);
  711. return;
  712. }
  713. wpa_msg_global(wpa_s->parent, MSG_INFO,
  714. P2P_EVENT_GROUP_FORMATION_SUCCESS);
  715. ssid = wpa_s->current_ssid;
  716. if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
  717. ssid->mode = WPAS_MODE_P2P_GO;
  718. p2p_group_notif_formation_done(wpa_s->p2p_group);
  719. wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
  720. }
  721. persistent = 0;
  722. if (ssid) {
  723. ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
  724. client = ssid->mode == WPAS_MODE_INFRA;
  725. if (ssid->mode == WPAS_MODE_P2P_GO) {
  726. persistent = ssid->p2p_persistent_group;
  727. os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
  728. ETH_ALEN);
  729. } else
  730. persistent = wpas_p2p_persistent_group(wpa_s,
  731. go_dev_addr,
  732. ssid->ssid,
  733. ssid->ssid_len);
  734. } else {
  735. ssid_txt = "";
  736. client = wpa_s->p2p_group_interface ==
  737. P2P_GROUP_INTERFACE_CLIENT;
  738. os_memset(go_dev_addr, 0, ETH_ALEN);
  739. }
  740. wpa_s->show_group_started = 0;
  741. if (client) {
  742. /*
  743. * Indicate event only after successfully completed 4-way
  744. * handshake, i.e., when the interface is ready for data
  745. * packets.
  746. */
  747. wpa_s->show_group_started = 1;
  748. } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
  749. char psk[65];
  750. wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
  751. wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  752. "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
  753. MACSTR "%s",
  754. wpa_s->ifname, ssid_txt, ssid->frequency, psk,
  755. MAC2STR(go_dev_addr),
  756. persistent ? " [PERSISTENT]" : "");
  757. wpas_p2p_cross_connect_setup(wpa_s);
  758. wpas_p2p_set_group_idle_timeout(wpa_s);
  759. } else {
  760. wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  761. "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
  762. "go_dev_addr=" MACSTR "%s",
  763. wpa_s->ifname, ssid_txt,
  764. ssid ? ssid->frequency : 0,
  765. ssid && ssid->passphrase ? ssid->passphrase : "",
  766. MAC2STR(go_dev_addr),
  767. persistent ? " [PERSISTENT]" : "");
  768. wpas_p2p_cross_connect_setup(wpa_s);
  769. wpas_p2p_set_group_idle_timeout(wpa_s);
  770. }
  771. if (persistent)
  772. network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
  773. ssid, go_dev_addr);
  774. else {
  775. os_free(wpa_s->global->add_psk);
  776. wpa_s->global->add_psk = NULL;
  777. }
  778. if (network_id < 0 && ssid)
  779. network_id = ssid->id;
  780. if (!client) {
  781. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
  782. os_get_reltime(&wpa_s->global->p2p_go_wait_client);
  783. }
  784. }
  785. struct send_action_work {
  786. unsigned int freq;
  787. u8 dst[ETH_ALEN];
  788. u8 src[ETH_ALEN];
  789. u8 bssid[ETH_ALEN];
  790. size_t len;
  791. unsigned int wait_time;
  792. u8 buf[0];
  793. };
  794. static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
  795. void *timeout_ctx)
  796. {
  797. struct wpa_supplicant *wpa_s = eloop_ctx;
  798. if (!wpa_s->p2p_send_action_work)
  799. return;
  800. wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
  801. os_free(wpa_s->p2p_send_action_work->ctx);
  802. radio_work_done(wpa_s->p2p_send_action_work);
  803. wpa_s->p2p_send_action_work = NULL;
  804. }
  805. static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
  806. unsigned int freq,
  807. const u8 *dst, const u8 *src,
  808. const u8 *bssid,
  809. const u8 *data, size_t data_len,
  810. enum offchannel_send_action_result
  811. result)
  812. {
  813. enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
  814. if (wpa_s->p2p_send_action_work) {
  815. struct send_action_work *awork;
  816. awork = wpa_s->p2p_send_action_work->ctx;
  817. if (awork->wait_time == 0) {
  818. os_free(awork);
  819. radio_work_done(wpa_s->p2p_send_action_work);
  820. wpa_s->p2p_send_action_work = NULL;
  821. } else {
  822. /*
  823. * In theory, this should not be needed, but number of
  824. * places in the P2P code is still using non-zero wait
  825. * time for the last Action frame in the sequence and
  826. * some of these do not call send_action_done().
  827. */
  828. eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
  829. wpa_s, NULL);
  830. eloop_register_timeout(
  831. 0, awork->wait_time * 1000,
  832. wpas_p2p_send_action_work_timeout,
  833. wpa_s, NULL);
  834. }
  835. }
  836. if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
  837. return;
  838. switch (result) {
  839. case OFFCHANNEL_SEND_ACTION_SUCCESS:
  840. res = P2P_SEND_ACTION_SUCCESS;
  841. break;
  842. case OFFCHANNEL_SEND_ACTION_NO_ACK:
  843. res = P2P_SEND_ACTION_NO_ACK;
  844. break;
  845. case OFFCHANNEL_SEND_ACTION_FAILED:
  846. res = P2P_SEND_ACTION_FAILED;
  847. break;
  848. }
  849. p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
  850. if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
  851. wpa_s->pending_pd_before_join &&
  852. (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
  853. os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
  854. wpa_s->p2p_fallback_to_go_neg) {
  855. wpa_s->pending_pd_before_join = 0;
  856. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
  857. "during p2p_connect-auto");
  858. wpas_p2p_fallback_to_go_neg(wpa_s, 0);
  859. return;
  860. }
  861. }
  862. static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
  863. {
  864. struct wpa_supplicant *wpa_s = work->wpa_s;
  865. struct send_action_work *awork = work->ctx;
  866. if (deinit) {
  867. os_free(awork);
  868. return;
  869. }
  870. if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
  871. awork->bssid, awork->buf, awork->len,
  872. awork->wait_time,
  873. wpas_p2p_send_action_tx_status, 1) < 0) {
  874. os_free(awork);
  875. radio_work_done(work);
  876. return;
  877. }
  878. wpa_s->p2p_send_action_work = work;
  879. }
  880. static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
  881. unsigned int freq, const u8 *dst,
  882. const u8 *src, const u8 *bssid, const u8 *buf,
  883. size_t len, unsigned int wait_time)
  884. {
  885. struct send_action_work *awork;
  886. if (wpa_s->p2p_send_action_work) {
  887. wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
  888. return -1;
  889. }
  890. awork = os_zalloc(sizeof(*awork) + len);
  891. if (awork == NULL)
  892. return -1;
  893. awork->freq = freq;
  894. os_memcpy(awork->dst, dst, ETH_ALEN);
  895. os_memcpy(awork->src, src, ETH_ALEN);
  896. os_memcpy(awork->bssid, bssid, ETH_ALEN);
  897. awork->len = len;
  898. awork->wait_time = wait_time;
  899. os_memcpy(awork->buf, buf, len);
  900. if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
  901. wpas_send_action_cb, awork) < 0) {
  902. os_free(awork);
  903. return -1;
  904. }
  905. return 0;
  906. }
  907. static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
  908. const u8 *src, const u8 *bssid, const u8 *buf,
  909. size_t len, unsigned int wait_time)
  910. {
  911. struct wpa_supplicant *wpa_s = ctx;
  912. int listen_freq = -1, send_freq = -1;
  913. if (wpa_s->p2p_listen_work)
  914. listen_freq = wpa_s->p2p_listen_work->freq;
  915. if (wpa_s->p2p_send_action_work)
  916. send_freq = wpa_s->p2p_send_action_work->freq;
  917. if (listen_freq != (int) freq && send_freq != (int) freq) {
  918. wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
  919. listen_freq, send_freq);
  920. return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
  921. len, wait_time);
  922. }
  923. wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
  924. return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
  925. wait_time,
  926. wpas_p2p_send_action_tx_status, 1);
  927. }
  928. static void wpas_send_action_done(void *ctx)
  929. {
  930. struct wpa_supplicant *wpa_s = ctx;
  931. if (wpa_s->p2p_send_action_work) {
  932. eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
  933. wpa_s, NULL);
  934. os_free(wpa_s->p2p_send_action_work->ctx);
  935. radio_work_done(wpa_s->p2p_send_action_work);
  936. wpa_s->p2p_send_action_work = NULL;
  937. }
  938. offchannel_send_action_done(wpa_s);
  939. }
  940. static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
  941. struct p2p_go_neg_results *params)
  942. {
  943. if (wpa_s->go_params == NULL) {
  944. wpa_s->go_params = os_malloc(sizeof(*params));
  945. if (wpa_s->go_params == NULL)
  946. return -1;
  947. }
  948. os_memcpy(wpa_s->go_params, params, sizeof(*params));
  949. return 0;
  950. }
  951. static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
  952. struct p2p_go_neg_results *res)
  953. {
  954. wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
  955. MAC2STR(res->peer_interface_addr));
  956. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
  957. res->ssid, res->ssid_len);
  958. wpa_supplicant_ap_deinit(wpa_s);
  959. wpas_copy_go_neg_results(wpa_s, res);
  960. if (res->wps_method == WPS_PBC)
  961. wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
  962. else {
  963. u16 dev_pw_id = DEV_PW_DEFAULT;
  964. if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
  965. dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
  966. wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
  967. wpa_s->p2p_pin, 1, dev_pw_id);
  968. }
  969. }
  970. static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
  971. struct wpa_ssid *ssid)
  972. {
  973. struct wpa_ssid *persistent;
  974. struct psk_list_entry *psk;
  975. struct hostapd_data *hapd;
  976. if (!wpa_s->ap_iface)
  977. return;
  978. persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
  979. ssid->ssid_len);
  980. if (persistent == NULL)
  981. return;
  982. hapd = wpa_s->ap_iface->bss[0];
  983. dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
  984. list) {
  985. struct hostapd_wpa_psk *hpsk;
  986. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
  987. MACSTR " psk=%d",
  988. MAC2STR(psk->addr), psk->p2p);
  989. hpsk = os_zalloc(sizeof(*hpsk));
  990. if (hpsk == NULL)
  991. break;
  992. os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
  993. if (psk->p2p)
  994. os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
  995. else
  996. os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
  997. hpsk->next = hapd->conf->ssid.wpa_psk;
  998. hapd->conf->ssid.wpa_psk = hpsk;
  999. }
  1000. }
  1001. static void p2p_go_configured(void *ctx, void *data)
  1002. {
  1003. struct wpa_supplicant *wpa_s = ctx;
  1004. struct p2p_go_neg_results *params = data;
  1005. struct wpa_ssid *ssid;
  1006. int network_id = -1;
  1007. ssid = wpa_s->current_ssid;
  1008. if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
  1009. wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
  1010. if (wpa_s->global->p2p_group_formation == wpa_s)
  1011. wpa_s->global->p2p_group_formation = NULL;
  1012. if (os_strlen(params->passphrase) > 0) {
  1013. wpa_msg_global(wpa_s->parent, MSG_INFO,
  1014. P2P_EVENT_GROUP_STARTED
  1015. "%s GO ssid=\"%s\" freq=%d "
  1016. "passphrase=\"%s\" go_dev_addr=" MACSTR
  1017. "%s", wpa_s->ifname,
  1018. wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
  1019. ssid->frequency, params->passphrase,
  1020. MAC2STR(wpa_s->global->p2p_dev_addr),
  1021. params->persistent_group ?
  1022. " [PERSISTENT]" : "");
  1023. } else {
  1024. char psk[65];
  1025. wpa_snprintf_hex(psk, sizeof(psk), params->psk,
  1026. sizeof(params->psk));
  1027. wpa_msg_global(wpa_s->parent, MSG_INFO,
  1028. P2P_EVENT_GROUP_STARTED
  1029. "%s GO ssid=\"%s\" freq=%d psk=%s "
  1030. "go_dev_addr=" MACSTR "%s",
  1031. wpa_s->ifname,
  1032. wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
  1033. ssid->frequency, psk,
  1034. MAC2STR(wpa_s->global->p2p_dev_addr),
  1035. params->persistent_group ?
  1036. " [PERSISTENT]" : "");
  1037. }
  1038. os_get_reltime(&wpa_s->global->p2p_go_wait_client);
  1039. if (params->persistent_group) {
  1040. network_id = wpas_p2p_store_persistent_group(
  1041. wpa_s->parent, ssid,
  1042. wpa_s->global->p2p_dev_addr);
  1043. wpas_p2p_add_psk_list(wpa_s, ssid);
  1044. }
  1045. if (network_id < 0)
  1046. network_id = ssid->id;
  1047. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
  1048. wpas_p2p_cross_connect_setup(wpa_s);
  1049. wpas_p2p_set_group_idle_timeout(wpa_s);
  1050. if (wpa_s->p2p_first_connection_timeout) {
  1051. wpa_dbg(wpa_s, MSG_DEBUG,
  1052. "P2P: Start group formation timeout of %d seconds until first data connection on GO",
  1053. wpa_s->p2p_first_connection_timeout);
  1054. wpa_s->p2p_go_group_formation_completed = 0;
  1055. wpa_s->global->p2p_group_formation = wpa_s;
  1056. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  1057. wpa_s->parent, NULL);
  1058. eloop_register_timeout(
  1059. wpa_s->p2p_first_connection_timeout, 0,
  1060. wpas_p2p_group_formation_timeout,
  1061. wpa_s->parent, NULL);
  1062. }
  1063. return;
  1064. }
  1065. wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
  1066. if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
  1067. params->peer_interface_addr)) {
  1068. wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
  1069. "filtering");
  1070. return;
  1071. }
  1072. if (params->wps_method == WPS_PBC)
  1073. wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
  1074. params->peer_device_addr);
  1075. else if (wpa_s->p2p_pin[0])
  1076. wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
  1077. wpa_s->p2p_pin, NULL, 0, 0);
  1078. os_free(wpa_s->go_params);
  1079. wpa_s->go_params = NULL;
  1080. }
  1081. static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
  1082. struct p2p_go_neg_results *params,
  1083. int group_formation)
  1084. {
  1085. struct wpa_ssid *ssid;
  1086. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
  1087. if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
  1088. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
  1089. "results");
  1090. return;
  1091. }
  1092. ssid = wpa_config_add_network(wpa_s->conf);
  1093. if (ssid == NULL) {
  1094. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
  1095. return;
  1096. }
  1097. wpa_s->show_group_started = 0;
  1098. wpa_config_set_network_defaults(ssid);
  1099. ssid->temporary = 1;
  1100. ssid->p2p_group = 1;
  1101. ssid->p2p_persistent_group = params->persistent_group;
  1102. ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
  1103. WPAS_MODE_P2P_GO;
  1104. ssid->frequency = params->freq;
  1105. ssid->ht40 = params->ht40;
  1106. ssid->vht = params->vht;
  1107. ssid->ssid = os_zalloc(params->ssid_len + 1);
  1108. if (ssid->ssid) {
  1109. os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
  1110. ssid->ssid_len = params->ssid_len;
  1111. }
  1112. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  1113. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  1114. ssid->proto = WPA_PROTO_RSN;
  1115. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  1116. if (os_strlen(params->passphrase) > 0) {
  1117. ssid->passphrase = os_strdup(params->passphrase);
  1118. if (ssid->passphrase == NULL) {
  1119. wpa_msg_global(wpa_s, MSG_ERROR,
  1120. "P2P: Failed to copy passphrase for GO");
  1121. wpa_config_remove_network(wpa_s->conf, ssid->id);
  1122. return;
  1123. }
  1124. } else
  1125. ssid->passphrase = NULL;
  1126. ssid->psk_set = params->psk_set;
  1127. if (ssid->psk_set)
  1128. os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
  1129. else if (ssid->passphrase)
  1130. wpa_config_update_psk(ssid);
  1131. ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
  1132. wpa_s->ap_configured_cb = p2p_go_configured;
  1133. wpa_s->ap_configured_cb_ctx = wpa_s;
  1134. wpa_s->ap_configured_cb_data = wpa_s->go_params;
  1135. wpa_s->connect_without_scan = ssid;
  1136. wpa_s->reassociate = 1;
  1137. wpa_s->disconnected = 0;
  1138. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
  1139. "start GO)");
  1140. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1141. }
  1142. static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
  1143. const struct wpa_supplicant *src)
  1144. {
  1145. struct wpa_config *d;
  1146. const struct wpa_config *s;
  1147. d = dst->conf;
  1148. s = src->conf;
  1149. #define C(n) if (s->n) d->n = os_strdup(s->n)
  1150. C(device_name);
  1151. C(manufacturer);
  1152. C(model_name);
  1153. C(model_number);
  1154. C(serial_number);
  1155. C(config_methods);
  1156. #undef C
  1157. os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
  1158. os_memcpy(d->sec_device_type, s->sec_device_type,
  1159. sizeof(d->sec_device_type));
  1160. d->num_sec_device_types = s->num_sec_device_types;
  1161. d->p2p_group_idle = s->p2p_group_idle;
  1162. d->p2p_intra_bss = s->p2p_intra_bss;
  1163. d->persistent_reconnect = s->persistent_reconnect;
  1164. d->max_num_sta = s->max_num_sta;
  1165. d->pbc_in_m1 = s->pbc_in_m1;
  1166. d->ignore_old_scan_res = s->ignore_old_scan_res;
  1167. d->beacon_int = s->beacon_int;
  1168. d->disassoc_low_ack = s->disassoc_low_ack;
  1169. d->disable_scan_offload = s->disable_scan_offload;
  1170. }
  1171. static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
  1172. char *ifname, size_t len)
  1173. {
  1174. char *ifname_ptr = wpa_s->ifname;
  1175. if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
  1176. os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
  1177. ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
  1178. }
  1179. os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
  1180. if (os_strlen(ifname) >= IFNAMSIZ &&
  1181. os_strlen(wpa_s->ifname) < IFNAMSIZ) {
  1182. /* Try to avoid going over the IFNAMSIZ length limit */
  1183. os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
  1184. }
  1185. }
  1186. static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
  1187. enum wpa_driver_if_type type)
  1188. {
  1189. char ifname[120], force_ifname[120];
  1190. if (wpa_s->pending_interface_name[0]) {
  1191. wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
  1192. "- skip creation of a new one");
  1193. if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
  1194. wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
  1195. "unknown?! ifname='%s'",
  1196. wpa_s->pending_interface_name);
  1197. return -1;
  1198. }
  1199. return 0;
  1200. }
  1201. wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
  1202. force_ifname[0] = '\0';
  1203. wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
  1204. ifname);
  1205. wpa_s->p2p_group_idx++;
  1206. wpa_s->pending_interface_type = type;
  1207. if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
  1208. wpa_s->pending_interface_addr, NULL) < 0) {
  1209. wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
  1210. "interface");
  1211. return -1;
  1212. }
  1213. if (force_ifname[0]) {
  1214. wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
  1215. force_ifname);
  1216. os_strlcpy(wpa_s->pending_interface_name, force_ifname,
  1217. sizeof(wpa_s->pending_interface_name));
  1218. } else
  1219. os_strlcpy(wpa_s->pending_interface_name, ifname,
  1220. sizeof(wpa_s->pending_interface_name));
  1221. wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
  1222. MACSTR, wpa_s->pending_interface_name,
  1223. MAC2STR(wpa_s->pending_interface_addr));
  1224. return 0;
  1225. }
  1226. static void wpas_p2p_remove_pending_group_interface(
  1227. struct wpa_supplicant *wpa_s)
  1228. {
  1229. if (!wpa_s->pending_interface_name[0] ||
  1230. is_zero_ether_addr(wpa_s->pending_interface_addr))
  1231. return; /* No pending virtual interface */
  1232. wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
  1233. wpa_s->pending_interface_name);
  1234. wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
  1235. wpa_s->pending_interface_name);
  1236. os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
  1237. wpa_s->pending_interface_name[0] = '\0';
  1238. }
  1239. static struct wpa_supplicant *
  1240. wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
  1241. {
  1242. struct wpa_interface iface;
  1243. struct wpa_supplicant *group_wpa_s;
  1244. if (!wpa_s->pending_interface_name[0]) {
  1245. wpa_printf(MSG_ERROR, "P2P: No pending group interface");
  1246. if (!wpas_p2p_create_iface(wpa_s))
  1247. return NULL;
  1248. /*
  1249. * Something has forced us to remove the pending interface; try
  1250. * to create a new one and hope for the best that we will get
  1251. * the same local address.
  1252. */
  1253. if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
  1254. WPA_IF_P2P_CLIENT) < 0)
  1255. return NULL;
  1256. }
  1257. os_memset(&iface, 0, sizeof(iface));
  1258. iface.ifname = wpa_s->pending_interface_name;
  1259. iface.driver = wpa_s->driver->name;
  1260. if (wpa_s->conf->ctrl_interface == NULL &&
  1261. wpa_s->parent != wpa_s &&
  1262. wpa_s->p2p_mgmt &&
  1263. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
  1264. iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
  1265. else
  1266. iface.ctrl_interface = wpa_s->conf->ctrl_interface;
  1267. iface.driver_param = wpa_s->conf->driver_param;
  1268. group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
  1269. if (group_wpa_s == NULL) {
  1270. wpa_printf(MSG_ERROR, "P2P: Failed to create new "
  1271. "wpa_supplicant interface");
  1272. return NULL;
  1273. }
  1274. wpa_s->pending_interface_name[0] = '\0';
  1275. group_wpa_s->parent = wpa_s;
  1276. group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
  1277. P2P_GROUP_INTERFACE_CLIENT;
  1278. wpa_s->global->p2p_group_formation = group_wpa_s;
  1279. wpas_p2p_clone_config(group_wpa_s, wpa_s);
  1280. return group_wpa_s;
  1281. }
  1282. static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
  1283. void *timeout_ctx)
  1284. {
  1285. struct wpa_supplicant *wpa_s = eloop_ctx;
  1286. wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
  1287. wpas_p2p_group_formation_failed(wpa_s);
  1288. }
  1289. void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
  1290. {
  1291. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  1292. wpa_s->parent, NULL);
  1293. if (wpa_s->global->p2p)
  1294. p2p_group_formation_failed(wpa_s->global->p2p);
  1295. wpas_group_formation_completed(wpa_s, 0);
  1296. }
  1297. static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
  1298. {
  1299. wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
  1300. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  1301. wpa_s->parent, NULL);
  1302. eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
  1303. wpa_s->parent, NULL);
  1304. wpa_s->global->p2p_fail_on_wps_complete = 0;
  1305. }
  1306. void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
  1307. {
  1308. if (wpa_s->global->p2p_group_formation != wpa_s)
  1309. return;
  1310. /* Speed up group formation timeout since this cannot succeed */
  1311. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  1312. wpa_s->parent, NULL);
  1313. eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
  1314. wpa_s->parent, NULL);
  1315. }
  1316. static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
  1317. {
  1318. struct wpa_supplicant *wpa_s = ctx;
  1319. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  1320. wpa_drv_cancel_remain_on_channel(wpa_s);
  1321. wpa_s->off_channel_freq = 0;
  1322. wpa_s->roc_waiting_drv_freq = 0;
  1323. }
  1324. if (res->status) {
  1325. wpa_msg_global(wpa_s, MSG_INFO,
  1326. P2P_EVENT_GO_NEG_FAILURE "status=%d",
  1327. res->status);
  1328. wpas_notify_p2p_go_neg_completed(wpa_s, res);
  1329. wpas_p2p_remove_pending_group_interface(wpa_s);
  1330. return;
  1331. }
  1332. if (wpa_s->p2p_go_ht40)
  1333. res->ht40 = 1;
  1334. if (wpa_s->p2p_go_vht)
  1335. res->vht = 1;
  1336. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
  1337. "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
  1338. " wps_method=%s",
  1339. res->role_go ? "GO" : "client", res->freq, res->ht40,
  1340. MAC2STR(res->peer_device_addr),
  1341. MAC2STR(res->peer_interface_addr),
  1342. p2p_wps_method_text(res->wps_method));
  1343. wpas_notify_p2p_go_neg_completed(wpa_s, res);
  1344. if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
  1345. struct wpa_ssid *ssid;
  1346. ssid = wpa_config_get_network(wpa_s->conf,
  1347. wpa_s->p2p_persistent_id);
  1348. if (ssid && ssid->disabled == 2 &&
  1349. ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
  1350. size_t len = os_strlen(ssid->passphrase);
  1351. wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
  1352. "on requested persistent group");
  1353. os_memcpy(res->passphrase, ssid->passphrase, len);
  1354. res->passphrase[len] = '\0';
  1355. }
  1356. }
  1357. if (wpa_s->create_p2p_iface) {
  1358. struct wpa_supplicant *group_wpa_s =
  1359. wpas_p2p_init_group_interface(wpa_s, res->role_go);
  1360. if (group_wpa_s == NULL) {
  1361. wpas_p2p_remove_pending_group_interface(wpa_s);
  1362. return;
  1363. }
  1364. if (group_wpa_s != wpa_s) {
  1365. os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
  1366. sizeof(group_wpa_s->p2p_pin));
  1367. group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
  1368. }
  1369. os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
  1370. wpa_s->pending_interface_name[0] = '\0';
  1371. group_wpa_s->p2p_in_provisioning = 1;
  1372. if (res->role_go)
  1373. wpas_start_wps_go(group_wpa_s, res, 1);
  1374. else
  1375. wpas_start_wps_enrollee(group_wpa_s, res);
  1376. } else {
  1377. wpa_s->p2p_in_provisioning = 1;
  1378. wpa_s->global->p2p_group_formation = wpa_s;
  1379. if (res->role_go)
  1380. wpas_start_wps_go(wpa_s, res, 1);
  1381. else
  1382. wpas_start_wps_enrollee(ctx, res);
  1383. }
  1384. wpa_s->p2p_long_listen = 0;
  1385. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  1386. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  1387. eloop_register_timeout(15 + res->peer_config_timeout / 100,
  1388. (res->peer_config_timeout % 100) * 10000,
  1389. wpas_p2p_group_formation_timeout, wpa_s, NULL);
  1390. }
  1391. static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
  1392. {
  1393. struct wpa_supplicant *wpa_s = ctx;
  1394. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
  1395. " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
  1396. wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
  1397. }
  1398. static void wpas_dev_found(void *ctx, const u8 *addr,
  1399. const struct p2p_peer_info *info,
  1400. int new_device)
  1401. {
  1402. #ifndef CONFIG_NO_STDOUT_DEBUG
  1403. struct wpa_supplicant *wpa_s = ctx;
  1404. char devtype[WPS_DEV_TYPE_BUFSIZE];
  1405. char *wfd_dev_info_hex = NULL;
  1406. #ifdef CONFIG_WIFI_DISPLAY
  1407. wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
  1408. WFD_SUBELEM_DEVICE_INFO);
  1409. #endif /* CONFIG_WIFI_DISPLAY */
  1410. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
  1411. " p2p_dev_addr=" MACSTR
  1412. " pri_dev_type=%s name='%s' config_methods=0x%x "
  1413. "dev_capab=0x%x group_capab=0x%x%s%s",
  1414. MAC2STR(addr), MAC2STR(info->p2p_device_addr),
  1415. wps_dev_type_bin2str(info->pri_dev_type, devtype,
  1416. sizeof(devtype)),
  1417. info->device_name, info->config_methods,
  1418. info->dev_capab, info->group_capab,
  1419. wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
  1420. wfd_dev_info_hex ? wfd_dev_info_hex : "");
  1421. #endif /* CONFIG_NO_STDOUT_DEBUG */
  1422. os_free(wfd_dev_info_hex);
  1423. wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
  1424. }
  1425. static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
  1426. {
  1427. struct wpa_supplicant *wpa_s = ctx;
  1428. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
  1429. "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
  1430. wpas_notify_p2p_device_lost(wpa_s, dev_addr);
  1431. }
  1432. static void wpas_find_stopped(void *ctx)
  1433. {
  1434. struct wpa_supplicant *wpa_s = ctx;
  1435. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
  1436. }
  1437. struct wpas_p2p_listen_work {
  1438. unsigned int freq;
  1439. unsigned int duration;
  1440. struct wpabuf *probe_resp_ie;
  1441. };
  1442. static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
  1443. {
  1444. if (lwork == NULL)
  1445. return;
  1446. wpabuf_free(lwork->probe_resp_ie);
  1447. os_free(lwork);
  1448. }
  1449. static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
  1450. {
  1451. struct wpas_p2p_listen_work *lwork;
  1452. if (!wpa_s->p2p_listen_work)
  1453. return;
  1454. lwork = wpa_s->p2p_listen_work->ctx;
  1455. wpas_p2p_listen_work_free(lwork);
  1456. radio_work_done(wpa_s->p2p_listen_work);
  1457. wpa_s->p2p_listen_work = NULL;
  1458. }
  1459. static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
  1460. {
  1461. struct wpa_supplicant *wpa_s = work->wpa_s;
  1462. struct wpas_p2p_listen_work *lwork = work->ctx;
  1463. if (deinit) {
  1464. wpas_p2p_listen_work_free(lwork);
  1465. return;
  1466. }
  1467. wpa_s->p2p_listen_work = work;
  1468. wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
  1469. if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
  1470. wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
  1471. "report received Probe Request frames");
  1472. wpas_p2p_listen_work_done(wpa_s);
  1473. return;
  1474. }
  1475. wpa_s->pending_listen_freq = lwork->freq;
  1476. wpa_s->pending_listen_duration = lwork->duration;
  1477. if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, lwork->duration) < 0)
  1478. {
  1479. wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
  1480. "to remain on channel (%u MHz) for Listen "
  1481. "state", lwork->freq);
  1482. wpas_p2p_listen_work_done(wpa_s);
  1483. wpa_s->pending_listen_freq = 0;
  1484. return;
  1485. }
  1486. wpa_s->off_channel_freq = 0;
  1487. wpa_s->roc_waiting_drv_freq = lwork->freq;
  1488. }
  1489. static int wpas_start_listen(void *ctx, unsigned int freq,
  1490. unsigned int duration,
  1491. const struct wpabuf *probe_resp_ie)
  1492. {
  1493. struct wpa_supplicant *wpa_s = ctx;
  1494. struct wpas_p2p_listen_work *lwork;
  1495. if (wpa_s->p2p_listen_work) {
  1496. wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
  1497. return -1;
  1498. }
  1499. lwork = os_zalloc(sizeof(*lwork));
  1500. if (lwork == NULL)
  1501. return -1;
  1502. lwork->freq = freq;
  1503. lwork->duration = duration;
  1504. if (probe_resp_ie) {
  1505. lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
  1506. if (lwork->probe_resp_ie == NULL) {
  1507. wpas_p2p_listen_work_free(lwork);
  1508. return -1;
  1509. }
  1510. }
  1511. if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
  1512. lwork) < 0) {
  1513. wpas_p2p_listen_work_free(lwork);
  1514. return -1;
  1515. }
  1516. return 0;
  1517. }
  1518. static void wpas_stop_listen(void *ctx)
  1519. {
  1520. struct wpa_supplicant *wpa_s = ctx;
  1521. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  1522. wpa_drv_cancel_remain_on_channel(wpa_s);
  1523. wpa_s->off_channel_freq = 0;
  1524. wpa_s->roc_waiting_drv_freq = 0;
  1525. }
  1526. wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
  1527. wpa_drv_probe_req_report(wpa_s, 0);
  1528. wpas_p2p_listen_work_done(wpa_s);
  1529. }
  1530. static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
  1531. {
  1532. struct wpa_supplicant *wpa_s = ctx;
  1533. return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
  1534. }
  1535. /*
  1536. * DNS Header section is used only to calculate compression pointers, so the
  1537. * contents of this data does not matter, but the length needs to be reserved
  1538. * in the virtual packet.
  1539. */
  1540. #define DNS_HEADER_LEN 12
  1541. /*
  1542. * 27-octet in-memory packet from P2P specification containing two implied
  1543. * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
  1544. */
  1545. #define P2P_SD_IN_MEMORY_LEN 27
  1546. static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
  1547. u8 **spos, const u8 *end)
  1548. {
  1549. while (*spos < end) {
  1550. u8 val = ((*spos)[0] & 0xc0) >> 6;
  1551. int len;
  1552. if (val == 1 || val == 2) {
  1553. /* These are reserved values in RFC 1035 */
  1554. wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
  1555. "sequence starting with 0x%x", val);
  1556. return -1;
  1557. }
  1558. if (val == 3) {
  1559. u16 offset;
  1560. u8 *spos_tmp;
  1561. /* Offset */
  1562. if (*spos + 2 > end) {
  1563. wpa_printf(MSG_DEBUG, "P2P: No room for full "
  1564. "DNS offset field");
  1565. return -1;
  1566. }
  1567. offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
  1568. if (offset >= *spos - start) {
  1569. wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
  1570. "pointer offset %u", offset);
  1571. return -1;
  1572. }
  1573. (*spos) += 2;
  1574. spos_tmp = start + offset;
  1575. return p2p_sd_dns_uncompress_label(upos, uend, start,
  1576. &spos_tmp,
  1577. *spos - 2);
  1578. }
  1579. /* Label */
  1580. len = (*spos)[0] & 0x3f;
  1581. if (len == 0)
  1582. return 0;
  1583. (*spos)++;
  1584. if (*spos + len > end) {
  1585. wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
  1586. "sequence - no room for label with length "
  1587. "%u", len);
  1588. return -1;
  1589. }
  1590. if (*upos + len + 2 > uend)
  1591. return -2;
  1592. os_memcpy(*upos, *spos, len);
  1593. *spos += len;
  1594. *upos += len;
  1595. (*upos)[0] = '.';
  1596. (*upos)++;
  1597. (*upos)[0] = '\0';
  1598. }
  1599. return 0;
  1600. }
  1601. /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
  1602. * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
  1603. * not large enough */
  1604. static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
  1605. size_t msg_len, size_t offset)
  1606. {
  1607. /* 27-octet in-memory packet from P2P specification */
  1608. const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
  1609. "\x04_udp\xC0\x11\x00\x0C\x00\x01";
  1610. u8 *tmp, *end, *spos;
  1611. char *upos, *uend;
  1612. int ret = 0;
  1613. if (buf_len < 2)
  1614. return -1;
  1615. if (offset > msg_len)
  1616. return -1;
  1617. tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
  1618. if (tmp == NULL)
  1619. return -1;
  1620. spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
  1621. end = spos + msg_len;
  1622. spos += offset;
  1623. os_memset(tmp, 0, DNS_HEADER_LEN);
  1624. os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
  1625. os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
  1626. upos = buf;
  1627. uend = buf + buf_len;
  1628. ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
  1629. if (ret) {
  1630. os_free(tmp);
  1631. return ret;
  1632. }
  1633. if (upos == buf) {
  1634. upos[0] = '.';
  1635. upos[1] = '\0';
  1636. } else if (upos[-1] == '.')
  1637. upos[-1] = '\0';
  1638. os_free(tmp);
  1639. return 0;
  1640. }
  1641. static struct p2p_srv_bonjour *
  1642. wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
  1643. const struct wpabuf *query)
  1644. {
  1645. struct p2p_srv_bonjour *bsrv;
  1646. size_t len;
  1647. len = wpabuf_len(query);
  1648. dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
  1649. struct p2p_srv_bonjour, list) {
  1650. if (len == wpabuf_len(bsrv->query) &&
  1651. os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
  1652. len) == 0)
  1653. return bsrv;
  1654. }
  1655. return NULL;
  1656. }
  1657. static struct p2p_srv_upnp *
  1658. wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
  1659. const char *service)
  1660. {
  1661. struct p2p_srv_upnp *usrv;
  1662. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  1663. struct p2p_srv_upnp, list) {
  1664. if (version == usrv->version &&
  1665. os_strcmp(service, usrv->service) == 0)
  1666. return usrv;
  1667. }
  1668. return NULL;
  1669. }
  1670. static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
  1671. u8 srv_trans_id)
  1672. {
  1673. u8 *len_pos;
  1674. if (wpabuf_tailroom(resp) < 5)
  1675. return;
  1676. /* Length (to be filled) */
  1677. len_pos = wpabuf_put(resp, 2);
  1678. wpabuf_put_u8(resp, srv_proto);
  1679. wpabuf_put_u8(resp, srv_trans_id);
  1680. /* Status Code */
  1681. wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
  1682. /* Response Data: empty */
  1683. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1684. }
  1685. static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
  1686. struct wpabuf *resp, u8 srv_trans_id)
  1687. {
  1688. struct p2p_srv_bonjour *bsrv;
  1689. u8 *len_pos;
  1690. wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
  1691. if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  1692. wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
  1693. return;
  1694. }
  1695. dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
  1696. struct p2p_srv_bonjour, list) {
  1697. if (wpabuf_tailroom(resp) <
  1698. 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
  1699. return;
  1700. /* Length (to be filled) */
  1701. len_pos = wpabuf_put(resp, 2);
  1702. wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
  1703. wpabuf_put_u8(resp, srv_trans_id);
  1704. /* Status Code */
  1705. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1706. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
  1707. wpabuf_head(bsrv->resp),
  1708. wpabuf_len(bsrv->resp));
  1709. /* Response Data */
  1710. wpabuf_put_buf(resp, bsrv->query); /* Key */
  1711. wpabuf_put_buf(resp, bsrv->resp); /* Value */
  1712. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  1713. 2);
  1714. }
  1715. }
  1716. static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
  1717. size_t query_len)
  1718. {
  1719. char str_rx[256], str_srv[256];
  1720. if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
  1721. return 0; /* Too short to include DNS Type and Version */
  1722. if (os_memcmp(query + query_len - 3,
  1723. wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
  1724. 3) != 0)
  1725. return 0; /* Mismatch in DNS Type or Version */
  1726. if (query_len == wpabuf_len(bsrv->query) &&
  1727. os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
  1728. return 1; /* Binary match */
  1729. if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
  1730. 0))
  1731. return 0; /* Failed to uncompress query */
  1732. if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
  1733. wpabuf_head(bsrv->query),
  1734. wpabuf_len(bsrv->query) - 3, 0))
  1735. return 0; /* Failed to uncompress service */
  1736. return os_strcmp(str_rx, str_srv) == 0;
  1737. }
  1738. static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
  1739. struct wpabuf *resp, u8 srv_trans_id,
  1740. const u8 *query, size_t query_len)
  1741. {
  1742. struct p2p_srv_bonjour *bsrv;
  1743. u8 *len_pos;
  1744. int matches = 0;
  1745. wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
  1746. query, query_len);
  1747. if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  1748. wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
  1749. wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
  1750. srv_trans_id);
  1751. return;
  1752. }
  1753. if (query_len == 0) {
  1754. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  1755. return;
  1756. }
  1757. dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
  1758. struct p2p_srv_bonjour, list) {
  1759. if (!match_bonjour_query(bsrv, query, query_len))
  1760. continue;
  1761. if (wpabuf_tailroom(resp) <
  1762. 5 + query_len + wpabuf_len(bsrv->resp))
  1763. return;
  1764. matches++;
  1765. /* Length (to be filled) */
  1766. len_pos = wpabuf_put(resp, 2);
  1767. wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
  1768. wpabuf_put_u8(resp, srv_trans_id);
  1769. /* Status Code */
  1770. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1771. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
  1772. wpabuf_head(bsrv->resp),
  1773. wpabuf_len(bsrv->resp));
  1774. /* Response Data */
  1775. wpabuf_put_data(resp, query, query_len); /* Key */
  1776. wpabuf_put_buf(resp, bsrv->resp); /* Value */
  1777. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1778. }
  1779. if (matches == 0) {
  1780. wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
  1781. "available");
  1782. if (wpabuf_tailroom(resp) < 5)
  1783. return;
  1784. /* Length (to be filled) */
  1785. len_pos = wpabuf_put(resp, 2);
  1786. wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
  1787. wpabuf_put_u8(resp, srv_trans_id);
  1788. /* Status Code */
  1789. wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
  1790. /* Response Data: empty */
  1791. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  1792. 2);
  1793. }
  1794. }
  1795. static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
  1796. struct wpabuf *resp, u8 srv_trans_id)
  1797. {
  1798. struct p2p_srv_upnp *usrv;
  1799. u8 *len_pos;
  1800. wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
  1801. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
  1802. wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
  1803. return;
  1804. }
  1805. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  1806. struct p2p_srv_upnp, list) {
  1807. if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
  1808. return;
  1809. /* Length (to be filled) */
  1810. len_pos = wpabuf_put(resp, 2);
  1811. wpabuf_put_u8(resp, P2P_SERV_UPNP);
  1812. wpabuf_put_u8(resp, srv_trans_id);
  1813. /* Status Code */
  1814. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1815. /* Response Data */
  1816. wpabuf_put_u8(resp, usrv->version);
  1817. wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
  1818. usrv->service);
  1819. wpabuf_put_str(resp, usrv->service);
  1820. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  1821. 2);
  1822. }
  1823. }
  1824. static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
  1825. struct wpabuf *resp, u8 srv_trans_id,
  1826. const u8 *query, size_t query_len)
  1827. {
  1828. struct p2p_srv_upnp *usrv;
  1829. u8 *len_pos;
  1830. u8 version;
  1831. char *str;
  1832. int count = 0;
  1833. wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
  1834. query, query_len);
  1835. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
  1836. wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
  1837. wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
  1838. srv_trans_id);
  1839. return;
  1840. }
  1841. if (query_len == 0) {
  1842. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  1843. return;
  1844. }
  1845. if (wpabuf_tailroom(resp) < 5)
  1846. return;
  1847. /* Length (to be filled) */
  1848. len_pos = wpabuf_put(resp, 2);
  1849. wpabuf_put_u8(resp, P2P_SERV_UPNP);
  1850. wpabuf_put_u8(resp, srv_trans_id);
  1851. version = query[0];
  1852. str = os_malloc(query_len);
  1853. if (str == NULL)
  1854. return;
  1855. os_memcpy(str, query + 1, query_len - 1);
  1856. str[query_len - 1] = '\0';
  1857. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  1858. struct p2p_srv_upnp, list) {
  1859. if (version != usrv->version)
  1860. continue;
  1861. if (os_strcmp(str, "ssdp:all") != 0 &&
  1862. os_strstr(usrv->service, str) == NULL)
  1863. continue;
  1864. if (wpabuf_tailroom(resp) < 2)
  1865. break;
  1866. if (count == 0) {
  1867. /* Status Code */
  1868. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1869. /* Response Data */
  1870. wpabuf_put_u8(resp, version);
  1871. } else
  1872. wpabuf_put_u8(resp, ',');
  1873. count++;
  1874. wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
  1875. usrv->service);
  1876. if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
  1877. break;
  1878. wpabuf_put_str(resp, usrv->service);
  1879. }
  1880. os_free(str);
  1881. if (count == 0) {
  1882. wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
  1883. "available");
  1884. /* Status Code */
  1885. wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
  1886. /* Response Data: empty */
  1887. }
  1888. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1889. }
  1890. #ifdef CONFIG_WIFI_DISPLAY
  1891. static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
  1892. struct wpabuf *resp, u8 srv_trans_id,
  1893. const u8 *query, size_t query_len)
  1894. {
  1895. const u8 *pos;
  1896. u8 role;
  1897. u8 *len_pos;
  1898. wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
  1899. if (!wpa_s->global->wifi_display) {
  1900. wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
  1901. wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
  1902. srv_trans_id);
  1903. return;
  1904. }
  1905. if (query_len < 1) {
  1906. wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
  1907. "Role");
  1908. return;
  1909. }
  1910. if (wpabuf_tailroom(resp) < 5)
  1911. return;
  1912. pos = query;
  1913. role = *pos++;
  1914. wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
  1915. /* TODO: role specific handling */
  1916. /* Length (to be filled) */
  1917. len_pos = wpabuf_put(resp, 2);
  1918. wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
  1919. wpabuf_put_u8(resp, srv_trans_id);
  1920. wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
  1921. while (pos < query + query_len) {
  1922. if (*pos < MAX_WFD_SUBELEMS &&
  1923. wpa_s->global->wfd_subelem[*pos] &&
  1924. wpabuf_tailroom(resp) >=
  1925. wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
  1926. wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
  1927. "subelement %u", *pos);
  1928. wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
  1929. }
  1930. pos++;
  1931. }
  1932. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1933. }
  1934. #endif /* CONFIG_WIFI_DISPLAY */
  1935. static void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
  1936. u16 update_indic, const u8 *tlvs, size_t tlvs_len)
  1937. {
  1938. struct wpa_supplicant *wpa_s = ctx;
  1939. const u8 *pos = tlvs;
  1940. const u8 *end = tlvs + tlvs_len;
  1941. const u8 *tlv_end;
  1942. u16 slen;
  1943. struct wpabuf *resp;
  1944. u8 srv_proto, srv_trans_id;
  1945. size_t buf_len;
  1946. char *buf;
  1947. wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
  1948. tlvs, tlvs_len);
  1949. buf_len = 2 * tlvs_len + 1;
  1950. buf = os_malloc(buf_len);
  1951. if (buf) {
  1952. wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
  1953. wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
  1954. MACSTR " %u %u %s",
  1955. freq, MAC2STR(sa), dialog_token, update_indic,
  1956. buf);
  1957. os_free(buf);
  1958. }
  1959. if (wpa_s->p2p_sd_over_ctrl_iface) {
  1960. wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
  1961. update_indic, tlvs, tlvs_len);
  1962. return; /* to be processed by an external program */
  1963. }
  1964. resp = wpabuf_alloc(10000);
  1965. if (resp == NULL)
  1966. return;
  1967. while (pos + 1 < end) {
  1968. wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
  1969. slen = WPA_GET_LE16(pos);
  1970. pos += 2;
  1971. if (pos + slen > end || slen < 2) {
  1972. wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
  1973. "length");
  1974. wpabuf_free(resp);
  1975. return;
  1976. }
  1977. tlv_end = pos + slen;
  1978. srv_proto = *pos++;
  1979. wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
  1980. srv_proto);
  1981. srv_trans_id = *pos++;
  1982. wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
  1983. srv_trans_id);
  1984. wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
  1985. pos, tlv_end - pos);
  1986. if (wpa_s->force_long_sd) {
  1987. wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
  1988. "response");
  1989. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  1990. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  1991. goto done;
  1992. }
  1993. switch (srv_proto) {
  1994. case P2P_SERV_ALL_SERVICES:
  1995. wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
  1996. "for all services");
  1997. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
  1998. dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  1999. wpa_printf(MSG_DEBUG, "P2P: No service "
  2000. "discovery protocols available");
  2001. wpas_sd_add_proto_not_avail(
  2002. resp, P2P_SERV_ALL_SERVICES,
  2003. srv_trans_id);
  2004. break;
  2005. }
  2006. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  2007. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  2008. break;
  2009. case P2P_SERV_BONJOUR:
  2010. wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
  2011. pos, tlv_end - pos);
  2012. break;
  2013. case P2P_SERV_UPNP:
  2014. wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
  2015. pos, tlv_end - pos);
  2016. break;
  2017. #ifdef CONFIG_WIFI_DISPLAY
  2018. case P2P_SERV_WIFI_DISPLAY:
  2019. wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
  2020. pos, tlv_end - pos);
  2021. break;
  2022. #endif /* CONFIG_WIFI_DISPLAY */
  2023. default:
  2024. wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
  2025. "protocol %u", srv_proto);
  2026. wpas_sd_add_proto_not_avail(resp, srv_proto,
  2027. srv_trans_id);
  2028. break;
  2029. }
  2030. pos = tlv_end;
  2031. }
  2032. done:
  2033. wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
  2034. update_indic, tlvs, tlvs_len);
  2035. wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
  2036. wpabuf_free(resp);
  2037. }
  2038. static void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
  2039. const u8 *tlvs, size_t tlvs_len)
  2040. {
  2041. struct wpa_supplicant *wpa_s = ctx;
  2042. const u8 *pos = tlvs;
  2043. const u8 *end = tlvs + tlvs_len;
  2044. const u8 *tlv_end;
  2045. u16 slen;
  2046. size_t buf_len;
  2047. char *buf;
  2048. wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
  2049. tlvs, tlvs_len);
  2050. if (tlvs_len > 1500) {
  2051. /* TODO: better way for handling this */
  2052. wpa_msg_ctrl(wpa_s, MSG_INFO,
  2053. P2P_EVENT_SERV_DISC_RESP MACSTR
  2054. " %u <long response: %u bytes>",
  2055. MAC2STR(sa), update_indic,
  2056. (unsigned int) tlvs_len);
  2057. } else {
  2058. buf_len = 2 * tlvs_len + 1;
  2059. buf = os_malloc(buf_len);
  2060. if (buf) {
  2061. wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
  2062. wpa_msg_ctrl(wpa_s, MSG_INFO,
  2063. P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
  2064. MAC2STR(sa), update_indic, buf);
  2065. os_free(buf);
  2066. }
  2067. }
  2068. while (pos < end) {
  2069. u8 srv_proto, srv_trans_id, status;
  2070. wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
  2071. slen = WPA_GET_LE16(pos);
  2072. pos += 2;
  2073. if (pos + slen > end || slen < 3) {
  2074. wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
  2075. "length");
  2076. return;
  2077. }
  2078. tlv_end = pos + slen;
  2079. srv_proto = *pos++;
  2080. wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
  2081. srv_proto);
  2082. srv_trans_id = *pos++;
  2083. wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
  2084. srv_trans_id);
  2085. status = *pos++;
  2086. wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
  2087. status);
  2088. wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
  2089. pos, tlv_end - pos);
  2090. pos = tlv_end;
  2091. }
  2092. wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
  2093. }
  2094. u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
  2095. const struct wpabuf *tlvs)
  2096. {
  2097. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  2098. return 0;
  2099. return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
  2100. }
  2101. u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
  2102. u8 version, const char *query)
  2103. {
  2104. struct wpabuf *tlvs;
  2105. u64 ret;
  2106. tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
  2107. if (tlvs == NULL)
  2108. return 0;
  2109. wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
  2110. wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
  2111. wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
  2112. wpabuf_put_u8(tlvs, version);
  2113. wpabuf_put_str(tlvs, query);
  2114. ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
  2115. wpabuf_free(tlvs);
  2116. return ret;
  2117. }
  2118. #ifdef CONFIG_WIFI_DISPLAY
  2119. static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
  2120. const struct wpabuf *tlvs)
  2121. {
  2122. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  2123. return 0;
  2124. return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
  2125. }
  2126. #define MAX_WFD_SD_SUBELEMS 20
  2127. static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
  2128. const char *subelems)
  2129. {
  2130. u8 *len;
  2131. const char *pos;
  2132. int val;
  2133. int count = 0;
  2134. len = wpabuf_put(tlvs, 2);
  2135. wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
  2136. wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
  2137. wpabuf_put_u8(tlvs, role);
  2138. pos = subelems;
  2139. while (*pos) {
  2140. val = atoi(pos);
  2141. if (val >= 0 && val < 256) {
  2142. wpabuf_put_u8(tlvs, val);
  2143. count++;
  2144. if (count == MAX_WFD_SD_SUBELEMS)
  2145. break;
  2146. }
  2147. pos = os_strchr(pos + 1, ',');
  2148. if (pos == NULL)
  2149. break;
  2150. pos++;
  2151. }
  2152. WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
  2153. }
  2154. u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
  2155. const u8 *dst, const char *role)
  2156. {
  2157. struct wpabuf *tlvs;
  2158. u64 ret;
  2159. const char *subelems;
  2160. u8 id = 1;
  2161. subelems = os_strchr(role, ' ');
  2162. if (subelems == NULL)
  2163. return 0;
  2164. subelems++;
  2165. tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
  2166. if (tlvs == NULL)
  2167. return 0;
  2168. if (os_strstr(role, "[source]"))
  2169. wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
  2170. if (os_strstr(role, "[pri-sink]"))
  2171. wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
  2172. if (os_strstr(role, "[sec-sink]"))
  2173. wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
  2174. if (os_strstr(role, "[source+sink]"))
  2175. wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
  2176. ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
  2177. wpabuf_free(tlvs);
  2178. return ret;
  2179. }
  2180. #endif /* CONFIG_WIFI_DISPLAY */
  2181. int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
  2182. {
  2183. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  2184. return -1;
  2185. return p2p_sd_cancel_request(wpa_s->global->p2p,
  2186. (void *) (uintptr_t) req);
  2187. }
  2188. void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
  2189. const u8 *dst, u8 dialog_token,
  2190. const struct wpabuf *resp_tlvs)
  2191. {
  2192. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  2193. return;
  2194. p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
  2195. resp_tlvs);
  2196. }
  2197. void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
  2198. {
  2199. if (wpa_s->global->p2p)
  2200. p2p_sd_service_update(wpa_s->global->p2p);
  2201. }
  2202. static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
  2203. {
  2204. dl_list_del(&bsrv->list);
  2205. wpabuf_free(bsrv->query);
  2206. wpabuf_free(bsrv->resp);
  2207. os_free(bsrv);
  2208. }
  2209. static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
  2210. {
  2211. dl_list_del(&usrv->list);
  2212. os_free(usrv->service);
  2213. os_free(usrv);
  2214. }
  2215. void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
  2216. {
  2217. struct p2p_srv_bonjour *bsrv, *bn;
  2218. struct p2p_srv_upnp *usrv, *un;
  2219. dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
  2220. struct p2p_srv_bonjour, list)
  2221. wpas_p2p_srv_bonjour_free(bsrv);
  2222. dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
  2223. struct p2p_srv_upnp, list)
  2224. wpas_p2p_srv_upnp_free(usrv);
  2225. wpas_p2p_sd_service_update(wpa_s);
  2226. }
  2227. int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
  2228. struct wpabuf *query, struct wpabuf *resp)
  2229. {
  2230. struct p2p_srv_bonjour *bsrv;
  2231. bsrv = os_zalloc(sizeof(*bsrv));
  2232. if (bsrv == NULL)
  2233. return -1;
  2234. bsrv->query = query;
  2235. bsrv->resp = resp;
  2236. dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
  2237. wpas_p2p_sd_service_update(wpa_s);
  2238. return 0;
  2239. }
  2240. int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
  2241. const struct wpabuf *query)
  2242. {
  2243. struct p2p_srv_bonjour *bsrv;
  2244. bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
  2245. if (bsrv == NULL)
  2246. return -1;
  2247. wpas_p2p_srv_bonjour_free(bsrv);
  2248. wpas_p2p_sd_service_update(wpa_s);
  2249. return 0;
  2250. }
  2251. int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
  2252. const char *service)
  2253. {
  2254. struct p2p_srv_upnp *usrv;
  2255. if (wpas_p2p_service_get_upnp(wpa_s, version, service))
  2256. return 0; /* Already listed */
  2257. usrv = os_zalloc(sizeof(*usrv));
  2258. if (usrv == NULL)
  2259. return -1;
  2260. usrv->version = version;
  2261. usrv->service = os_strdup(service);
  2262. if (usrv->service == NULL) {
  2263. os_free(usrv);
  2264. return -1;
  2265. }
  2266. dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
  2267. wpas_p2p_sd_service_update(wpa_s);
  2268. return 0;
  2269. }
  2270. int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
  2271. const char *service)
  2272. {
  2273. struct p2p_srv_upnp *usrv;
  2274. usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
  2275. if (usrv == NULL)
  2276. return -1;
  2277. wpas_p2p_srv_upnp_free(usrv);
  2278. wpas_p2p_sd_service_update(wpa_s);
  2279. return 0;
  2280. }
  2281. static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
  2282. const u8 *peer, const char *params,
  2283. unsigned int generated_pin)
  2284. {
  2285. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
  2286. " %08d%s", MAC2STR(peer), generated_pin, params);
  2287. }
  2288. static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
  2289. const u8 *peer, const char *params)
  2290. {
  2291. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
  2292. "%s", MAC2STR(peer), params);
  2293. }
  2294. static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
  2295. const u8 *dev_addr, const u8 *pri_dev_type,
  2296. const char *dev_name, u16 supp_config_methods,
  2297. u8 dev_capab, u8 group_capab, const u8 *group_id,
  2298. size_t group_id_len)
  2299. {
  2300. struct wpa_supplicant *wpa_s = ctx;
  2301. char devtype[WPS_DEV_TYPE_BUFSIZE];
  2302. char params[300];
  2303. u8 empty_dev_type[8];
  2304. unsigned int generated_pin = 0;
  2305. struct wpa_supplicant *group = NULL;
  2306. if (group_id) {
  2307. for (group = wpa_s->global->ifaces; group; group = group->next)
  2308. {
  2309. struct wpa_ssid *s = group->current_ssid;
  2310. if (s != NULL &&
  2311. s->mode == WPAS_MODE_P2P_GO &&
  2312. group_id_len - ETH_ALEN == s->ssid_len &&
  2313. os_memcmp(group_id + ETH_ALEN, s->ssid,
  2314. s->ssid_len) == 0)
  2315. break;
  2316. }
  2317. }
  2318. if (pri_dev_type == NULL) {
  2319. os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
  2320. pri_dev_type = empty_dev_type;
  2321. }
  2322. os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
  2323. " pri_dev_type=%s name='%s' config_methods=0x%x "
  2324. "dev_capab=0x%x group_capab=0x%x%s%s",
  2325. MAC2STR(dev_addr),
  2326. wps_dev_type_bin2str(pri_dev_type, devtype,
  2327. sizeof(devtype)),
  2328. dev_name, supp_config_methods, dev_capab, group_capab,
  2329. group ? " group=" : "",
  2330. group ? group->ifname : "");
  2331. params[sizeof(params) - 1] = '\0';
  2332. if (config_methods & WPS_CONFIG_DISPLAY) {
  2333. generated_pin = wps_generate_pin();
  2334. wpas_prov_disc_local_display(wpa_s, peer, params,
  2335. generated_pin);
  2336. } else if (config_methods & WPS_CONFIG_KEYPAD)
  2337. wpas_prov_disc_local_keypad(wpa_s, peer, params);
  2338. else if (config_methods & WPS_CONFIG_PUSHBUTTON)
  2339. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
  2340. MACSTR "%s", MAC2STR(peer), params);
  2341. wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
  2342. P2P_PROV_DISC_SUCCESS,
  2343. config_methods, generated_pin);
  2344. }
  2345. static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
  2346. {
  2347. struct wpa_supplicant *wpa_s = ctx;
  2348. unsigned int generated_pin = 0;
  2349. char params[20];
  2350. if (wpa_s->pending_pd_before_join &&
  2351. (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
  2352. os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
  2353. wpa_s->pending_pd_before_join = 0;
  2354. wpa_printf(MSG_DEBUG, "P2P: Starting pending "
  2355. "join-existing-group operation");
  2356. wpas_p2p_join_start(wpa_s);
  2357. return;
  2358. }
  2359. if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
  2360. wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
  2361. os_snprintf(params, sizeof(params), " peer_go=%d",
  2362. wpa_s->pending_pd_use == AUTO_PD_JOIN);
  2363. else
  2364. params[0] = '\0';
  2365. if (config_methods & WPS_CONFIG_DISPLAY)
  2366. wpas_prov_disc_local_keypad(wpa_s, peer, params);
  2367. else if (config_methods & WPS_CONFIG_KEYPAD) {
  2368. generated_pin = wps_generate_pin();
  2369. wpas_prov_disc_local_display(wpa_s, peer, params,
  2370. generated_pin);
  2371. } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
  2372. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
  2373. MACSTR "%s", MAC2STR(peer), params);
  2374. wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
  2375. P2P_PROV_DISC_SUCCESS,
  2376. config_methods, generated_pin);
  2377. }
  2378. static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
  2379. enum p2p_prov_disc_status status)
  2380. {
  2381. struct wpa_supplicant *wpa_s = ctx;
  2382. if (wpa_s->p2p_fallback_to_go_neg) {
  2383. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
  2384. "failed - fall back to GO Negotiation");
  2385. wpas_p2p_fallback_to_go_neg(wpa_s, 0);
  2386. return;
  2387. }
  2388. if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
  2389. wpa_s->pending_pd_before_join = 0;
  2390. wpa_printf(MSG_DEBUG, "P2P: Starting pending "
  2391. "join-existing-group operation (no ACK for PD "
  2392. "Req attempts)");
  2393. wpas_p2p_join_start(wpa_s);
  2394. return;
  2395. }
  2396. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
  2397. " p2p_dev_addr=" MACSTR " status=%d",
  2398. MAC2STR(peer), status);
  2399. wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
  2400. status, 0, 0);
  2401. }
  2402. static int freq_included(const struct p2p_channels *channels, unsigned int freq)
  2403. {
  2404. if (channels == NULL)
  2405. return 1; /* Assume no restrictions */
  2406. return p2p_channels_includes_freq(channels, freq);
  2407. }
  2408. static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
  2409. const u8 *go_dev_addr, const u8 *ssid,
  2410. size_t ssid_len, int *go, u8 *group_bssid,
  2411. int *force_freq, int persistent_group,
  2412. const struct p2p_channels *channels)
  2413. {
  2414. struct wpa_supplicant *wpa_s = ctx;
  2415. struct wpa_ssid *s;
  2416. int res;
  2417. struct wpa_supplicant *grp;
  2418. if (!persistent_group) {
  2419. wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
  2420. " to join an active group (SSID: %s)",
  2421. MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
  2422. if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
  2423. (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
  2424. == 0 ||
  2425. os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
  2426. wpa_printf(MSG_DEBUG, "P2P: Accept previously "
  2427. "authorized invitation");
  2428. goto accept_inv;
  2429. }
  2430. /*
  2431. * Do not accept the invitation automatically; notify user and
  2432. * request approval.
  2433. */
  2434. return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  2435. }
  2436. grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
  2437. if (grp) {
  2438. wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
  2439. "running persistent group");
  2440. if (*go)
  2441. os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
  2442. goto accept_inv;
  2443. }
  2444. if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
  2445. os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
  2446. wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
  2447. "invitation to re-invoke a persistent group");
  2448. } else if (!wpa_s->conf->persistent_reconnect)
  2449. return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  2450. for (s = wpa_s->conf->ssid; s; s = s->next) {
  2451. if (s->disabled == 2 &&
  2452. os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
  2453. s->ssid_len == ssid_len &&
  2454. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  2455. break;
  2456. }
  2457. if (!s) {
  2458. wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
  2459. " requested reinvocation of an unknown group",
  2460. MAC2STR(sa));
  2461. return P2P_SC_FAIL_UNKNOWN_GROUP;
  2462. }
  2463. if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
  2464. *go = 1;
  2465. if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
  2466. wpa_printf(MSG_DEBUG, "P2P: The only available "
  2467. "interface is already in use - reject "
  2468. "invitation");
  2469. return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
  2470. }
  2471. os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
  2472. } else if (s->mode == WPAS_MODE_P2P_GO) {
  2473. *go = 1;
  2474. if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
  2475. {
  2476. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  2477. "interface address for the group");
  2478. return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
  2479. }
  2480. os_memcpy(group_bssid, wpa_s->pending_interface_addr,
  2481. ETH_ALEN);
  2482. }
  2483. accept_inv:
  2484. wpas_p2p_set_own_freq_preference(wpa_s, 0);
  2485. /* Get one of the frequencies currently in use */
  2486. if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) {
  2487. wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
  2488. wpas_p2p_set_own_freq_preference(wpa_s, res);
  2489. if (wpa_s->num_multichan_concurrent < 2 ||
  2490. wpas_p2p_num_unused_channels(wpa_s) < 1) {
  2491. wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
  2492. *force_freq = res;
  2493. }
  2494. }
  2495. if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
  2496. wpas_p2p_num_unused_channels(wpa_s) > 0) {
  2497. if (*go == 0) {
  2498. /* We are the client */
  2499. wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
  2500. "running a GO but we are capable of MCC, "
  2501. "figure out the best channel to use");
  2502. *force_freq = 0;
  2503. } else if (!freq_included(channels, *force_freq)) {
  2504. /* We are the GO, and *force_freq is not in the
  2505. * intersection */
  2506. wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
  2507. "in intersection but we are capable of MCC, "
  2508. "figure out the best channel to use",
  2509. *force_freq);
  2510. *force_freq = 0;
  2511. }
  2512. }
  2513. return P2P_SC_SUCCESS;
  2514. }
  2515. static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
  2516. const u8 *ssid, size_t ssid_len,
  2517. const u8 *go_dev_addr, u8 status,
  2518. int op_freq)
  2519. {
  2520. struct wpa_supplicant *wpa_s = ctx;
  2521. struct wpa_ssid *s;
  2522. for (s = wpa_s->conf->ssid; s; s = s->next) {
  2523. if (s->disabled == 2 &&
  2524. s->ssid_len == ssid_len &&
  2525. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  2526. break;
  2527. }
  2528. if (status == P2P_SC_SUCCESS) {
  2529. wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
  2530. " was accepted; op_freq=%d MHz, SSID=%s",
  2531. MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
  2532. if (s) {
  2533. int go = s->mode == WPAS_MODE_P2P_GO;
  2534. wpas_p2p_group_add_persistent(
  2535. wpa_s, s, go, go ? op_freq : 0, 0, 0, NULL,
  2536. go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
  2537. } else if (bssid) {
  2538. wpa_s->user_initiated_pd = 0;
  2539. wpas_p2p_join(wpa_s, bssid, go_dev_addr,
  2540. wpa_s->p2p_wps_method, 0,
  2541. ssid, ssid_len);
  2542. }
  2543. return;
  2544. }
  2545. if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
  2546. wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
  2547. " was rejected (status %u)", MAC2STR(sa), status);
  2548. return;
  2549. }
  2550. if (!s) {
  2551. if (bssid) {
  2552. wpa_msg_global(wpa_s, MSG_INFO,
  2553. P2P_EVENT_INVITATION_RECEIVED
  2554. "sa=" MACSTR " go_dev_addr=" MACSTR
  2555. " bssid=" MACSTR " unknown-network",
  2556. MAC2STR(sa), MAC2STR(go_dev_addr),
  2557. MAC2STR(bssid));
  2558. } else {
  2559. wpa_msg_global(wpa_s, MSG_INFO,
  2560. P2P_EVENT_INVITATION_RECEIVED
  2561. "sa=" MACSTR " go_dev_addr=" MACSTR
  2562. " unknown-network",
  2563. MAC2STR(sa), MAC2STR(go_dev_addr));
  2564. }
  2565. return;
  2566. }
  2567. if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
  2568. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
  2569. "sa=" MACSTR " persistent=%d freq=%d",
  2570. MAC2STR(sa), s->id, op_freq);
  2571. } else {
  2572. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
  2573. "sa=" MACSTR " persistent=%d",
  2574. MAC2STR(sa), s->id);
  2575. }
  2576. }
  2577. static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
  2578. struct wpa_ssid *ssid,
  2579. const u8 *peer, int inv)
  2580. {
  2581. size_t i;
  2582. if (ssid == NULL)
  2583. return;
  2584. for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
  2585. if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
  2586. ETH_ALEN) == 0)
  2587. break;
  2588. }
  2589. if (i >= ssid->num_p2p_clients) {
  2590. if (ssid->mode != WPAS_MODE_P2P_GO &&
  2591. os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
  2592. wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
  2593. "due to invitation result", ssid->id);
  2594. wpas_notify_network_removed(wpa_s, ssid);
  2595. wpa_config_remove_network(wpa_s->conf, ssid->id);
  2596. return;
  2597. }
  2598. return; /* Peer not found in client list */
  2599. }
  2600. wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
  2601. "group %d client list%s",
  2602. MAC2STR(peer), ssid->id,
  2603. inv ? " due to invitation result" : "");
  2604. os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
  2605. ssid->p2p_client_list + (i + 1) * ETH_ALEN,
  2606. (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
  2607. ssid->num_p2p_clients--;
  2608. #ifndef CONFIG_NO_CONFIG_WRITE
  2609. if (wpa_s->parent->conf->update_config &&
  2610. wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
  2611. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  2612. #endif /* CONFIG_NO_CONFIG_WRITE */
  2613. }
  2614. static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
  2615. const u8 *peer)
  2616. {
  2617. struct wpa_ssid *ssid;
  2618. wpa_s = wpa_s->global->p2p_invite_group;
  2619. if (wpa_s == NULL)
  2620. return; /* No known invitation group */
  2621. ssid = wpa_s->current_ssid;
  2622. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  2623. !ssid->p2p_persistent_group)
  2624. return; /* Not operating as a GO in persistent group */
  2625. ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
  2626. ssid->ssid, ssid->ssid_len);
  2627. wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
  2628. }
  2629. static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
  2630. const struct p2p_channels *channels,
  2631. const u8 *peer, int neg_freq)
  2632. {
  2633. struct wpa_supplicant *wpa_s = ctx;
  2634. struct wpa_ssid *ssid;
  2635. int freq;
  2636. if (bssid) {
  2637. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
  2638. "status=%d " MACSTR,
  2639. status, MAC2STR(bssid));
  2640. } else {
  2641. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
  2642. "status=%d ", status);
  2643. }
  2644. wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
  2645. wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
  2646. status, MAC2STR(peer));
  2647. if (wpa_s->pending_invite_ssid_id == -1) {
  2648. if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
  2649. wpas_remove_persistent_client(wpa_s, peer);
  2650. return; /* Invitation to active group */
  2651. }
  2652. if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
  2653. wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
  2654. "invitation exchange to indicate readiness for "
  2655. "re-invocation");
  2656. }
  2657. if (status != P2P_SC_SUCCESS) {
  2658. if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
  2659. ssid = wpa_config_get_network(
  2660. wpa_s->conf, wpa_s->pending_invite_ssid_id);
  2661. wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
  2662. }
  2663. wpas_p2p_remove_pending_group_interface(wpa_s);
  2664. return;
  2665. }
  2666. ssid = wpa_config_get_network(wpa_s->conf,
  2667. wpa_s->pending_invite_ssid_id);
  2668. if (ssid == NULL) {
  2669. wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
  2670. "data matching with invitation");
  2671. return;
  2672. }
  2673. /*
  2674. * The peer could have missed our ctrl::ack frame for Invitation
  2675. * Response and continue retransmitting the frame. To reduce the
  2676. * likelihood of the peer not getting successful TX status for the
  2677. * Invitation Response frame, wait a short time here before starting
  2678. * the persistent group so that we will remain on the current channel to
  2679. * acknowledge any possible retransmission from the peer.
  2680. */
  2681. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
  2682. "starting persistent group");
  2683. os_sleep(0, 50000);
  2684. freq = wpa_s->p2p_persistent_go_freq;
  2685. if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
  2686. freq_included(channels, neg_freq)) {
  2687. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use frequence %d MHz from invitation for GO mode",
  2688. neg_freq);
  2689. freq = neg_freq;
  2690. }
  2691. wpas_p2p_group_add_persistent(wpa_s, ssid,
  2692. ssid->mode == WPAS_MODE_P2P_GO,
  2693. freq,
  2694. wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
  2695. channels,
  2696. ssid->mode == WPAS_MODE_P2P_GO ?
  2697. P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
  2698. 0);
  2699. }
  2700. static int wpas_p2p_disallowed_freq(struct wpa_global *global,
  2701. unsigned int freq)
  2702. {
  2703. return freq_range_list_includes(&global->p2p_disallow_freq, freq);
  2704. }
  2705. static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
  2706. {
  2707. reg->channel[reg->channels] = chan;
  2708. reg->channels++;
  2709. }
  2710. static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
  2711. struct p2p_channels *chan,
  2712. struct p2p_channels *cli_chan)
  2713. {
  2714. int i, cla = 0;
  2715. os_memset(cli_chan, 0, sizeof(*cli_chan));
  2716. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
  2717. "band");
  2718. /* Operating class 81 - 2.4 GHz band channels 1..13 */
  2719. chan->reg_class[cla].reg_class = 81;
  2720. chan->reg_class[cla].channels = 0;
  2721. for (i = 0; i < 11; i++) {
  2722. if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
  2723. wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
  2724. }
  2725. if (chan->reg_class[cla].channels)
  2726. cla++;
  2727. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
  2728. "band");
  2729. /* Operating class 115 - 5 GHz, channels 36-48 */
  2730. chan->reg_class[cla].reg_class = 115;
  2731. chan->reg_class[cla].channels = 0;
  2732. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
  2733. wpas_p2p_add_chan(&chan->reg_class[cla], 36);
  2734. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
  2735. wpas_p2p_add_chan(&chan->reg_class[cla], 40);
  2736. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
  2737. wpas_p2p_add_chan(&chan->reg_class[cla], 44);
  2738. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
  2739. wpas_p2p_add_chan(&chan->reg_class[cla], 48);
  2740. if (chan->reg_class[cla].channels)
  2741. cla++;
  2742. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
  2743. "band");
  2744. /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
  2745. chan->reg_class[cla].reg_class = 124;
  2746. chan->reg_class[cla].channels = 0;
  2747. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
  2748. wpas_p2p_add_chan(&chan->reg_class[cla], 149);
  2749. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
  2750. wpas_p2p_add_chan(&chan->reg_class[cla], 153);
  2751. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
  2752. wpas_p2p_add_chan(&chan->reg_class[cla], 157);
  2753. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
  2754. wpas_p2p_add_chan(&chan->reg_class[cla], 161);
  2755. if (chan->reg_class[cla].channels)
  2756. cla++;
  2757. chan->reg_classes = cla;
  2758. return 0;
  2759. }
  2760. static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
  2761. u16 num_modes,
  2762. enum hostapd_hw_mode mode)
  2763. {
  2764. u16 i;
  2765. for (i = 0; i < num_modes; i++) {
  2766. if (modes[i].mode == mode)
  2767. return &modes[i];
  2768. }
  2769. return NULL;
  2770. }
  2771. enum chan_allowed {
  2772. NOT_ALLOWED, PASSIVE_ONLY, ALLOWED
  2773. };
  2774. static int has_channel(struct wpa_global *global,
  2775. struct hostapd_hw_modes *mode, u8 chan, int *flags)
  2776. {
  2777. int i;
  2778. unsigned int freq;
  2779. freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
  2780. chan * 5;
  2781. if (wpas_p2p_disallowed_freq(global, freq))
  2782. return NOT_ALLOWED;
  2783. for (i = 0; i < mode->num_channels; i++) {
  2784. if (mode->channels[i].chan == chan) {
  2785. if (flags)
  2786. *flags = mode->channels[i].flag;
  2787. if (mode->channels[i].flag &
  2788. (HOSTAPD_CHAN_DISABLED |
  2789. HOSTAPD_CHAN_RADAR))
  2790. return NOT_ALLOWED;
  2791. if (mode->channels[i].flag &
  2792. (HOSTAPD_CHAN_PASSIVE_SCAN |
  2793. HOSTAPD_CHAN_NO_IBSS))
  2794. return PASSIVE_ONLY;
  2795. return ALLOWED;
  2796. }
  2797. }
  2798. return NOT_ALLOWED;
  2799. }
  2800. struct p2p_oper_class_map {
  2801. enum hostapd_hw_mode mode;
  2802. u8 op_class;
  2803. u8 min_chan;
  2804. u8 max_chan;
  2805. u8 inc;
  2806. enum { BW20, BW40PLUS, BW40MINUS, BW80 } bw;
  2807. };
  2808. static struct p2p_oper_class_map op_class[] = {
  2809. { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
  2810. #if 0 /* Do not enable HT40 on 2 GHz for now */
  2811. { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
  2812. { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
  2813. #endif
  2814. { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
  2815. { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
  2816. { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
  2817. { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
  2818. { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
  2819. { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
  2820. /*
  2821. * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
  2822. * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
  2823. * 80 MHz, but currently use the following definition for simplicity
  2824. * (these center frequencies are not actual channels, which makes
  2825. * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
  2826. * removing invalid channels.
  2827. */
  2828. { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
  2829. { -1, 0, 0, 0, 0, BW20 }
  2830. };
  2831. static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
  2832. struct hostapd_hw_modes *mode,
  2833. u8 channel)
  2834. {
  2835. u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
  2836. unsigned int i;
  2837. if (mode->mode != HOSTAPD_MODE_IEEE80211A)
  2838. return 0;
  2839. for (i = 0; i < ARRAY_SIZE(center_channels); i++)
  2840. /*
  2841. * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
  2842. * so the center channel is 6 channels away from the start/end.
  2843. */
  2844. if (channel >= center_channels[i] - 6 &&
  2845. channel <= center_channels[i] + 6)
  2846. return center_channels[i];
  2847. return 0;
  2848. }
  2849. static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
  2850. struct hostapd_hw_modes *mode,
  2851. u8 channel, u8 bw)
  2852. {
  2853. u8 center_chan;
  2854. int i, flags;
  2855. enum chan_allowed res, ret = ALLOWED;
  2856. center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
  2857. if (!center_chan)
  2858. return NOT_ALLOWED;
  2859. if (center_chan >= 58 && center_chan <= 138)
  2860. return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
  2861. /* check all the channels are available */
  2862. for (i = 0; i < 4; i++) {
  2863. int adj_chan = center_chan - 6 + i * 4;
  2864. res = has_channel(wpa_s->global, mode, adj_chan, &flags);
  2865. if (res == NOT_ALLOWED)
  2866. return NOT_ALLOWED;
  2867. if (res == PASSIVE_ONLY)
  2868. ret = PASSIVE_ONLY;
  2869. if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
  2870. return NOT_ALLOWED;
  2871. if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
  2872. return NOT_ALLOWED;
  2873. if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
  2874. return NOT_ALLOWED;
  2875. if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
  2876. return NOT_ALLOWED;
  2877. }
  2878. return ret;
  2879. }
  2880. static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
  2881. struct hostapd_hw_modes *mode,
  2882. u8 channel, u8 bw)
  2883. {
  2884. int flag;
  2885. enum chan_allowed res, res2;
  2886. res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
  2887. if (bw == BW40MINUS) {
  2888. if (!(flag & HOSTAPD_CHAN_HT40MINUS))
  2889. return NOT_ALLOWED;
  2890. res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
  2891. } else if (bw == BW40PLUS) {
  2892. if (!(flag & HOSTAPD_CHAN_HT40PLUS))
  2893. return NOT_ALLOWED;
  2894. res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
  2895. } else if (bw == BW80) {
  2896. res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
  2897. }
  2898. if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
  2899. return NOT_ALLOWED;
  2900. if (res == PASSIVE_ONLY || res2 == PASSIVE_ONLY)
  2901. return PASSIVE_ONLY;
  2902. return res;
  2903. }
  2904. static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
  2905. struct p2p_channels *chan,
  2906. struct p2p_channels *cli_chan)
  2907. {
  2908. struct hostapd_hw_modes *mode;
  2909. int cla, op, cli_cla;
  2910. if (wpa_s->hw.modes == NULL) {
  2911. wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
  2912. "of all supported channels; assume dualband "
  2913. "support");
  2914. return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
  2915. }
  2916. cla = cli_cla = 0;
  2917. for (op = 0; op_class[op].op_class; op++) {
  2918. struct p2p_oper_class_map *o = &op_class[op];
  2919. u8 ch;
  2920. struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
  2921. mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
  2922. if (mode == NULL)
  2923. continue;
  2924. for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
  2925. enum chan_allowed res;
  2926. res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
  2927. if (res == ALLOWED) {
  2928. if (reg == NULL) {
  2929. wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
  2930. o->op_class);
  2931. reg = &chan->reg_class[cla];
  2932. cla++;
  2933. reg->reg_class = o->op_class;
  2934. }
  2935. reg->channel[reg->channels] = ch;
  2936. reg->channels++;
  2937. } else if (res == PASSIVE_ONLY &&
  2938. wpa_s->conf->p2p_add_cli_chan) {
  2939. if (cli_reg == NULL) {
  2940. wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
  2941. o->op_class);
  2942. cli_reg = &cli_chan->reg_class[cli_cla];
  2943. cli_cla++;
  2944. cli_reg->reg_class = o->op_class;
  2945. }
  2946. cli_reg->channel[cli_reg->channels] = ch;
  2947. cli_reg->channels++;
  2948. }
  2949. }
  2950. if (reg) {
  2951. wpa_hexdump(MSG_DEBUG, "P2P: Channels",
  2952. reg->channel, reg->channels);
  2953. }
  2954. if (cli_reg) {
  2955. wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
  2956. cli_reg->channel, cli_reg->channels);
  2957. }
  2958. }
  2959. chan->reg_classes = cla;
  2960. cli_chan->reg_classes = cli_cla;
  2961. return 0;
  2962. }
  2963. int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
  2964. struct hostapd_hw_modes *mode, u8 channel)
  2965. {
  2966. int op;
  2967. enum chan_allowed ret;
  2968. for (op = 0; op_class[op].op_class; op++) {
  2969. struct p2p_oper_class_map *o = &op_class[op];
  2970. u8 ch;
  2971. for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
  2972. if (o->mode != HOSTAPD_MODE_IEEE80211A ||
  2973. o->bw == BW20 || ch != channel)
  2974. continue;
  2975. ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
  2976. if (ret == ALLOWED)
  2977. return (o->bw == BW40MINUS) ? -1 : 1;
  2978. }
  2979. }
  2980. return 0;
  2981. }
  2982. int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
  2983. struct hostapd_hw_modes *mode, u8 channel)
  2984. {
  2985. if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
  2986. return 0;
  2987. return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
  2988. }
  2989. static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
  2990. size_t buf_len)
  2991. {
  2992. struct wpa_supplicant *wpa_s = ctx;
  2993. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2994. if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
  2995. break;
  2996. }
  2997. if (wpa_s == NULL)
  2998. return -1;
  2999. return wpa_drv_get_noa(wpa_s, buf, buf_len);
  3000. }
  3001. static int wpas_go_connected(void *ctx, const u8 *dev_addr)
  3002. {
  3003. struct wpa_supplicant *wpa_s = ctx;
  3004. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3005. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3006. if (ssid == NULL)
  3007. continue;
  3008. if (ssid->mode != WPAS_MODE_INFRA)
  3009. continue;
  3010. if (wpa_s->wpa_state != WPA_COMPLETED &&
  3011. wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
  3012. continue;
  3013. if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
  3014. return 1;
  3015. }
  3016. return 0;
  3017. }
  3018. static int wpas_is_concurrent_session_active(void *ctx)
  3019. {
  3020. struct wpa_supplicant *wpa_s = ctx;
  3021. struct wpa_supplicant *ifs;
  3022. for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
  3023. if (ifs == wpa_s)
  3024. continue;
  3025. if (ifs->wpa_state > WPA_ASSOCIATED)
  3026. return 1;
  3027. }
  3028. return 0;
  3029. }
  3030. static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
  3031. {
  3032. struct wpa_supplicant *wpa_s = ctx;
  3033. wpa_msg_global(wpa_s, level, "P2P: %s", msg);
  3034. }
  3035. int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
  3036. {
  3037. struct wpa_interface iface;
  3038. struct wpa_supplicant *p2pdev_wpa_s;
  3039. char ifname[100];
  3040. char force_name[100];
  3041. int ret;
  3042. os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
  3043. wpa_s->ifname);
  3044. force_name[0] = '\0';
  3045. wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
  3046. ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
  3047. force_name, wpa_s->pending_interface_addr, NULL);
  3048. if (ret < 0) {
  3049. wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
  3050. return ret;
  3051. }
  3052. os_strlcpy(wpa_s->pending_interface_name, ifname,
  3053. sizeof(wpa_s->pending_interface_name));
  3054. os_memset(&iface, 0, sizeof(iface));
  3055. iface.p2p_mgmt = 1;
  3056. iface.ifname = wpa_s->pending_interface_name;
  3057. iface.driver = wpa_s->driver->name;
  3058. iface.driver_param = wpa_s->conf->driver_param;
  3059. iface.confname = wpa_s->confname;
  3060. p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
  3061. if (!p2pdev_wpa_s) {
  3062. wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
  3063. return -1;
  3064. }
  3065. p2pdev_wpa_s->parent = wpa_s;
  3066. wpa_s->pending_interface_name[0] = '\0';
  3067. return 0;
  3068. }
  3069. static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
  3070. const u8 *noa, size_t noa_len)
  3071. {
  3072. struct wpa_supplicant *wpa_s, *intf = ctx;
  3073. char hex[100];
  3074. for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3075. if (wpa_s->waiting_presence_resp)
  3076. break;
  3077. }
  3078. if (!wpa_s) {
  3079. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
  3080. return;
  3081. }
  3082. wpa_s->waiting_presence_resp = 0;
  3083. wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
  3084. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
  3085. " status=%u noa=%s", MAC2STR(src), status, hex);
  3086. }
  3087. /**
  3088. * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
  3089. * @global: Pointer to global data from wpa_supplicant_init()
  3090. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3091. * Returns: 0 on success, -1 on failure
  3092. */
  3093. int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
  3094. {
  3095. struct p2p_config p2p;
  3096. unsigned int r;
  3097. int i;
  3098. if (wpa_s->conf->p2p_disabled)
  3099. return 0;
  3100. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
  3101. return 0;
  3102. if (global->p2p)
  3103. return 0;
  3104. os_memset(&p2p, 0, sizeof(p2p));
  3105. p2p.cb_ctx = wpa_s;
  3106. p2p.debug_print = wpas_p2p_debug_print;
  3107. p2p.p2p_scan = wpas_p2p_scan;
  3108. p2p.send_action = wpas_send_action;
  3109. p2p.send_action_done = wpas_send_action_done;
  3110. p2p.go_neg_completed = wpas_go_neg_completed;
  3111. p2p.go_neg_req_rx = wpas_go_neg_req_rx;
  3112. p2p.dev_found = wpas_dev_found;
  3113. p2p.dev_lost = wpas_dev_lost;
  3114. p2p.find_stopped = wpas_find_stopped;
  3115. p2p.start_listen = wpas_start_listen;
  3116. p2p.stop_listen = wpas_stop_listen;
  3117. p2p.send_probe_resp = wpas_send_probe_resp;
  3118. p2p.sd_request = wpas_sd_request;
  3119. p2p.sd_response = wpas_sd_response;
  3120. p2p.prov_disc_req = wpas_prov_disc_req;
  3121. p2p.prov_disc_resp = wpas_prov_disc_resp;
  3122. p2p.prov_disc_fail = wpas_prov_disc_fail;
  3123. p2p.invitation_process = wpas_invitation_process;
  3124. p2p.invitation_received = wpas_invitation_received;
  3125. p2p.invitation_result = wpas_invitation_result;
  3126. p2p.get_noa = wpas_get_noa;
  3127. p2p.go_connected = wpas_go_connected;
  3128. p2p.presence_resp = wpas_presence_resp;
  3129. p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
  3130. os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
  3131. os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
  3132. p2p.dev_name = wpa_s->conf->device_name;
  3133. p2p.manufacturer = wpa_s->conf->manufacturer;
  3134. p2p.model_name = wpa_s->conf->model_name;
  3135. p2p.model_number = wpa_s->conf->model_number;
  3136. p2p.serial_number = wpa_s->conf->serial_number;
  3137. if (wpa_s->wps) {
  3138. os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
  3139. p2p.config_methods = wpa_s->wps->config_methods;
  3140. }
  3141. if (wpa_s->conf->p2p_listen_reg_class &&
  3142. wpa_s->conf->p2p_listen_channel) {
  3143. p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
  3144. p2p.channel = wpa_s->conf->p2p_listen_channel;
  3145. } else {
  3146. p2p.reg_class = 81;
  3147. /*
  3148. * Pick one of the social channels randomly as the listen
  3149. * channel.
  3150. */
  3151. os_get_random((u8 *) &r, sizeof(r));
  3152. p2p.channel = 1 + (r % 3) * 5;
  3153. }
  3154. wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
  3155. if (wpa_s->conf->p2p_oper_reg_class &&
  3156. wpa_s->conf->p2p_oper_channel) {
  3157. p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
  3158. p2p.op_channel = wpa_s->conf->p2p_oper_channel;
  3159. p2p.cfg_op_channel = 1;
  3160. wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
  3161. "%d:%d", p2p.op_reg_class, p2p.op_channel);
  3162. } else {
  3163. p2p.op_reg_class = 81;
  3164. /*
  3165. * Use random operation channel from (1, 6, 11) if no other
  3166. * preference is indicated.
  3167. */
  3168. os_get_random((u8 *) &r, sizeof(r));
  3169. p2p.op_channel = 1 + (r % 3) * 5;
  3170. p2p.cfg_op_channel = 0;
  3171. wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
  3172. "%d:%d", p2p.op_reg_class, p2p.op_channel);
  3173. }
  3174. if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
  3175. p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
  3176. p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
  3177. }
  3178. if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  3179. os_memcpy(p2p.country, wpa_s->conf->country, 2);
  3180. p2p.country[2] = 0x04;
  3181. } else
  3182. os_memcpy(p2p.country, "XX\x04", 3);
  3183. if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
  3184. wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
  3185. "channel list");
  3186. return -1;
  3187. }
  3188. os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
  3189. WPS_DEV_TYPE_LEN);
  3190. p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
  3191. os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
  3192. p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
  3193. p2p.concurrent_operations = !!(wpa_s->drv_flags &
  3194. WPA_DRIVER_FLAGS_P2P_CONCURRENT);
  3195. p2p.max_peers = 100;
  3196. if (wpa_s->conf->p2p_ssid_postfix) {
  3197. p2p.ssid_postfix_len =
  3198. os_strlen(wpa_s->conf->p2p_ssid_postfix);
  3199. if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
  3200. p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
  3201. os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
  3202. p2p.ssid_postfix_len);
  3203. }
  3204. p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
  3205. p2p.max_listen = wpa_s->max_remain_on_chan;
  3206. global->p2p = p2p_init(&p2p);
  3207. if (global->p2p == NULL)
  3208. return -1;
  3209. global->p2p_init_wpa_s = wpa_s;
  3210. for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
  3211. if (wpa_s->conf->wps_vendor_ext[i] == NULL)
  3212. continue;
  3213. p2p_add_wps_vendor_extension(
  3214. global->p2p, wpa_s->conf->wps_vendor_ext[i]);
  3215. }
  3216. p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
  3217. return 0;
  3218. }
  3219. /**
  3220. * wpas_p2p_deinit - Deinitialize per-interface P2P data
  3221. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3222. *
  3223. * This function deinitialize per-interface P2P data.
  3224. */
  3225. void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
  3226. {
  3227. if (wpa_s->driver && wpa_s->drv_priv)
  3228. wpa_drv_probe_req_report(wpa_s, 0);
  3229. if (wpa_s->go_params) {
  3230. /* Clear any stored provisioning info */
  3231. p2p_clear_provisioning_info(
  3232. wpa_s->global->p2p,
  3233. wpa_s->go_params->peer_device_addr);
  3234. }
  3235. os_free(wpa_s->go_params);
  3236. wpa_s->go_params = NULL;
  3237. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  3238. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3239. wpa_s->p2p_long_listen = 0;
  3240. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  3241. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  3242. wpas_p2p_remove_pending_group_interface(wpa_s);
  3243. eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
  3244. wpas_p2p_listen_work_done(wpa_s);
  3245. if (wpa_s->p2p_send_action_work) {
  3246. os_free(wpa_s->p2p_send_action_work->ctx);
  3247. radio_work_done(wpa_s->p2p_send_action_work);
  3248. wpa_s->p2p_send_action_work = NULL;
  3249. }
  3250. eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
  3251. /* TODO: remove group interface from the driver if this wpa_s instance
  3252. * is on top of a P2P group interface */
  3253. }
  3254. /**
  3255. * wpas_p2p_deinit_global - Deinitialize global P2P module
  3256. * @global: Pointer to global data from wpa_supplicant_init()
  3257. *
  3258. * This function deinitializes the global (per device) P2P module.
  3259. */
  3260. void wpas_p2p_deinit_global(struct wpa_global *global)
  3261. {
  3262. struct wpa_supplicant *wpa_s, *tmp;
  3263. wpa_s = global->ifaces;
  3264. if (wpa_s)
  3265. wpas_p2p_service_flush(wpa_s);
  3266. if (global->p2p == NULL)
  3267. return;
  3268. /* Remove remaining P2P group interfaces */
  3269. while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
  3270. wpa_s = wpa_s->next;
  3271. while (wpa_s) {
  3272. tmp = global->ifaces;
  3273. while (tmp &&
  3274. (tmp == wpa_s ||
  3275. tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
  3276. tmp = tmp->next;
  3277. }
  3278. if (tmp == NULL)
  3279. break;
  3280. /* Disconnect from the P2P group and deinit the interface */
  3281. wpas_p2p_disconnect(tmp);
  3282. }
  3283. /*
  3284. * Deinit GO data on any possibly remaining interface (if main
  3285. * interface is used as GO).
  3286. */
  3287. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3288. if (wpa_s->ap_iface)
  3289. wpas_p2p_group_deinit(wpa_s);
  3290. }
  3291. p2p_deinit(global->p2p);
  3292. global->p2p = NULL;
  3293. global->p2p_init_wpa_s = NULL;
  3294. }
  3295. static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
  3296. {
  3297. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
  3298. wpa_s->conf->p2p_no_group_iface)
  3299. return 0; /* separate interface disabled per configuration */
  3300. if (wpa_s->drv_flags &
  3301. (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
  3302. WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
  3303. return 1; /* P2P group requires a new interface in every case
  3304. */
  3305. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
  3306. return 0; /* driver does not support concurrent operations */
  3307. if (wpa_s->global->ifaces->next)
  3308. return 1; /* more that one interface already in use */
  3309. if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
  3310. return 1; /* this interface is already in use */
  3311. return 0;
  3312. }
  3313. static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
  3314. const u8 *peer_addr,
  3315. enum p2p_wps_method wps_method,
  3316. int go_intent, const u8 *own_interface_addr,
  3317. unsigned int force_freq, int persistent_group,
  3318. struct wpa_ssid *ssid, unsigned int pref_freq)
  3319. {
  3320. if (persistent_group && wpa_s->conf->persistent_reconnect)
  3321. persistent_group = 2;
  3322. /*
  3323. * Increase GO config timeout if HT40 is used since it takes some time
  3324. * to scan channels for coex purposes before the BSS can be started.
  3325. */
  3326. p2p_set_config_timeout(wpa_s->global->p2p,
  3327. wpa_s->p2p_go_ht40 ? 255 : 100, 20);
  3328. return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
  3329. go_intent, own_interface_addr, force_freq,
  3330. persistent_group, ssid ? ssid->ssid : NULL,
  3331. ssid ? ssid->ssid_len : 0,
  3332. wpa_s->p2p_pd_before_go_neg, pref_freq);
  3333. }
  3334. static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
  3335. const u8 *peer_addr,
  3336. enum p2p_wps_method wps_method,
  3337. int go_intent, const u8 *own_interface_addr,
  3338. unsigned int force_freq, int persistent_group,
  3339. struct wpa_ssid *ssid, unsigned int pref_freq)
  3340. {
  3341. if (persistent_group && wpa_s->conf->persistent_reconnect)
  3342. persistent_group = 2;
  3343. return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
  3344. go_intent, own_interface_addr, force_freq,
  3345. persistent_group, ssid ? ssid->ssid : NULL,
  3346. ssid ? ssid->ssid_len : 0, pref_freq);
  3347. }
  3348. static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
  3349. {
  3350. wpa_s->p2p_join_scan_count++;
  3351. wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
  3352. wpa_s->p2p_join_scan_count);
  3353. if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
  3354. wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
  3355. " for join operationg - stop join attempt",
  3356. MAC2STR(wpa_s->pending_join_iface_addr));
  3357. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3358. if (wpa_s->p2p_auto_pd) {
  3359. wpa_s->p2p_auto_pd = 0;
  3360. wpa_msg_global(wpa_s, MSG_INFO,
  3361. P2P_EVENT_PROV_DISC_FAILURE
  3362. " p2p_dev_addr=" MACSTR " status=N/A",
  3363. MAC2STR(wpa_s->pending_join_dev_addr));
  3364. return;
  3365. }
  3366. wpa_msg_global(wpa_s->parent, MSG_INFO,
  3367. P2P_EVENT_GROUP_FORMATION_FAILURE);
  3368. }
  3369. }
  3370. static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
  3371. {
  3372. int *freqs, res, num, i;
  3373. if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
  3374. /* Multiple channels are supported and not all are in use */
  3375. return 0;
  3376. }
  3377. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  3378. if (!freqs)
  3379. return 1;
  3380. num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
  3381. wpa_s->num_multichan_concurrent);
  3382. if (num < 0) {
  3383. res = 1;
  3384. goto exit_free;
  3385. }
  3386. for (i = 0; i < num; i++) {
  3387. if (freqs[i] == freq) {
  3388. wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
  3389. freq);
  3390. res = 0;
  3391. goto exit_free;
  3392. }
  3393. }
  3394. res = 1;
  3395. exit_free:
  3396. os_free(freqs);
  3397. return res;
  3398. }
  3399. static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
  3400. const u8 *peer_dev_addr)
  3401. {
  3402. struct wpa_bss *bss;
  3403. int updated;
  3404. bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
  3405. if (bss == NULL)
  3406. return -1;
  3407. if (bss->last_update_idx < wpa_s->bss_update_idx) {
  3408. wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
  3409. "last scan");
  3410. return 0;
  3411. }
  3412. updated = os_reltime_before(&wpa_s->p2p_auto_started,
  3413. &bss->last_update);
  3414. wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
  3415. "%ld.%06ld (%supdated in last scan)",
  3416. bss->last_update.sec, bss->last_update.usec,
  3417. updated ? "": "not ");
  3418. return updated;
  3419. }
  3420. static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
  3421. struct wpa_scan_results *scan_res)
  3422. {
  3423. struct wpa_bss *bss = NULL;
  3424. int freq;
  3425. u8 iface_addr[ETH_ALEN];
  3426. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3427. if (wpa_s->global->p2p_disabled)
  3428. return;
  3429. wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
  3430. scan_res ? (int) scan_res->num : -1,
  3431. wpa_s->p2p_auto_join ? "auto_" : "");
  3432. if (scan_res)
  3433. wpas_p2p_scan_res_handler(wpa_s, scan_res);
  3434. if (wpa_s->p2p_auto_pd) {
  3435. int join = wpas_p2p_peer_go(wpa_s,
  3436. wpa_s->pending_join_dev_addr);
  3437. if (join == 0 &&
  3438. wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
  3439. wpa_s->auto_pd_scan_retry++;
  3440. bss = wpa_bss_get_bssid_latest(
  3441. wpa_s, wpa_s->pending_join_dev_addr);
  3442. if (bss) {
  3443. freq = bss->freq;
  3444. wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
  3445. "the peer " MACSTR " at %d MHz",
  3446. wpa_s->auto_pd_scan_retry,
  3447. MAC2STR(wpa_s->
  3448. pending_join_dev_addr),
  3449. freq);
  3450. wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
  3451. return;
  3452. }
  3453. }
  3454. if (join < 0)
  3455. join = 0;
  3456. wpa_s->p2p_auto_pd = 0;
  3457. wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
  3458. wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
  3459. MAC2STR(wpa_s->pending_join_dev_addr), join);
  3460. if (p2p_prov_disc_req(wpa_s->global->p2p,
  3461. wpa_s->pending_join_dev_addr,
  3462. wpa_s->pending_pd_config_methods, join,
  3463. 0, wpa_s->user_initiated_pd) < 0) {
  3464. wpa_s->p2p_auto_pd = 0;
  3465. wpa_msg_global(wpa_s, MSG_INFO,
  3466. P2P_EVENT_PROV_DISC_FAILURE
  3467. " p2p_dev_addr=" MACSTR " status=N/A",
  3468. MAC2STR(wpa_s->pending_join_dev_addr));
  3469. }
  3470. return;
  3471. }
  3472. if (wpa_s->p2p_auto_join) {
  3473. int join = wpas_p2p_peer_go(wpa_s,
  3474. wpa_s->pending_join_dev_addr);
  3475. if (join < 0) {
  3476. wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
  3477. "running a GO -> use GO Negotiation");
  3478. wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
  3479. wpa_s->p2p_pin, wpa_s->p2p_wps_method,
  3480. wpa_s->p2p_persistent_group, 0, 0, 0,
  3481. wpa_s->p2p_go_intent,
  3482. wpa_s->p2p_connect_freq,
  3483. wpa_s->p2p_persistent_id,
  3484. wpa_s->p2p_pd_before_go_neg,
  3485. wpa_s->p2p_go_ht40,
  3486. wpa_s->p2p_go_vht);
  3487. return;
  3488. }
  3489. wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
  3490. "try to join the group", join ? "" :
  3491. " in older scan");
  3492. if (!join)
  3493. wpa_s->p2p_fallback_to_go_neg = 1;
  3494. }
  3495. freq = p2p_get_oper_freq(wpa_s->global->p2p,
  3496. wpa_s->pending_join_iface_addr);
  3497. if (freq < 0 &&
  3498. p2p_get_interface_addr(wpa_s->global->p2p,
  3499. wpa_s->pending_join_dev_addr,
  3500. iface_addr) == 0 &&
  3501. os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
  3502. {
  3503. wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
  3504. "address for join from " MACSTR " to " MACSTR
  3505. " based on newly discovered P2P peer entry",
  3506. MAC2STR(wpa_s->pending_join_iface_addr),
  3507. MAC2STR(iface_addr));
  3508. os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
  3509. ETH_ALEN);
  3510. freq = p2p_get_oper_freq(wpa_s->global->p2p,
  3511. wpa_s->pending_join_iface_addr);
  3512. }
  3513. if (freq >= 0) {
  3514. wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
  3515. "from P2P peer table: %d MHz", freq);
  3516. }
  3517. if (wpa_s->p2p_join_ssid_len) {
  3518. wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
  3519. MACSTR " and SSID %s",
  3520. MAC2STR(wpa_s->pending_join_iface_addr),
  3521. wpa_ssid_txt(wpa_s->p2p_join_ssid,
  3522. wpa_s->p2p_join_ssid_len));
  3523. bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
  3524. wpa_s->p2p_join_ssid,
  3525. wpa_s->p2p_join_ssid_len);
  3526. }
  3527. if (!bss) {
  3528. wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
  3529. MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
  3530. bss = wpa_bss_get_bssid_latest(wpa_s,
  3531. wpa_s->pending_join_iface_addr);
  3532. }
  3533. if (bss) {
  3534. freq = bss->freq;
  3535. wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
  3536. "from BSS table: %d MHz (SSID %s)", freq,
  3537. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  3538. }
  3539. if (freq > 0) {
  3540. u16 method;
  3541. if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
  3542. wpa_msg_global(wpa_s->parent, MSG_INFO,
  3543. P2P_EVENT_GROUP_FORMATION_FAILURE
  3544. "reason=FREQ_CONFLICT");
  3545. return;
  3546. }
  3547. wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
  3548. "prior to joining an existing group (GO " MACSTR
  3549. " freq=%u MHz)",
  3550. MAC2STR(wpa_s->pending_join_dev_addr), freq);
  3551. wpa_s->pending_pd_before_join = 1;
  3552. switch (wpa_s->pending_join_wps_method) {
  3553. case WPS_PIN_DISPLAY:
  3554. method = WPS_CONFIG_KEYPAD;
  3555. break;
  3556. case WPS_PIN_KEYPAD:
  3557. method = WPS_CONFIG_DISPLAY;
  3558. break;
  3559. case WPS_PBC:
  3560. method = WPS_CONFIG_PUSHBUTTON;
  3561. break;
  3562. default:
  3563. method = 0;
  3564. break;
  3565. }
  3566. if ((p2p_get_provisioning_info(wpa_s->global->p2p,
  3567. wpa_s->pending_join_dev_addr) ==
  3568. method)) {
  3569. /*
  3570. * We have already performed provision discovery for
  3571. * joining the group. Proceed directly to join
  3572. * operation without duplicated provision discovery. */
  3573. wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
  3574. "with " MACSTR " already done - proceed to "
  3575. "join",
  3576. MAC2STR(wpa_s->pending_join_dev_addr));
  3577. wpa_s->pending_pd_before_join = 0;
  3578. goto start;
  3579. }
  3580. if (p2p_prov_disc_req(wpa_s->global->p2p,
  3581. wpa_s->pending_join_dev_addr, method, 1,
  3582. freq, wpa_s->user_initiated_pd) < 0) {
  3583. wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
  3584. "Discovery Request before joining an "
  3585. "existing group");
  3586. wpa_s->pending_pd_before_join = 0;
  3587. goto start;
  3588. }
  3589. return;
  3590. }
  3591. wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
  3592. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3593. eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
  3594. wpas_p2p_check_join_scan_limit(wpa_s);
  3595. return;
  3596. start:
  3597. /* Start join operation immediately */
  3598. wpas_p2p_join_start(wpa_s);
  3599. }
  3600. static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
  3601. const u8 *ssid, size_t ssid_len)
  3602. {
  3603. int ret;
  3604. struct wpa_driver_scan_params params;
  3605. struct wpabuf *wps_ie, *ies;
  3606. size_t ielen;
  3607. int freqs[2] = { 0, 0 };
  3608. os_memset(&params, 0, sizeof(params));
  3609. /* P2P Wildcard SSID */
  3610. params.num_ssids = 1;
  3611. if (ssid && ssid_len) {
  3612. params.ssids[0].ssid = ssid;
  3613. params.ssids[0].ssid_len = ssid_len;
  3614. os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
  3615. wpa_s->p2p_join_ssid_len = ssid_len;
  3616. } else {
  3617. params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
  3618. params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
  3619. wpa_s->p2p_join_ssid_len = 0;
  3620. }
  3621. wpa_s->wps->dev.p2p = 1;
  3622. wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
  3623. wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
  3624. NULL);
  3625. if (wps_ie == NULL) {
  3626. wpas_p2p_scan_res_join(wpa_s, NULL);
  3627. return;
  3628. }
  3629. ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
  3630. ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
  3631. if (ies == NULL) {
  3632. wpabuf_free(wps_ie);
  3633. wpas_p2p_scan_res_join(wpa_s, NULL);
  3634. return;
  3635. }
  3636. wpabuf_put_buf(ies, wps_ie);
  3637. wpabuf_free(wps_ie);
  3638. p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
  3639. params.p2p_probe = 1;
  3640. params.extra_ies = wpabuf_head(ies);
  3641. params.extra_ies_len = wpabuf_len(ies);
  3642. if (!freq) {
  3643. int oper_freq;
  3644. /*
  3645. * If freq is not provided, check the operating freq of the GO
  3646. * and use a single channel scan on if possible.
  3647. */
  3648. oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
  3649. wpa_s->pending_join_iface_addr);
  3650. if (oper_freq > 0)
  3651. freq = oper_freq;
  3652. }
  3653. if (freq > 0) {
  3654. freqs[0] = freq;
  3655. params.freqs = freqs;
  3656. }
  3657. /*
  3658. * Run a scan to update BSS table and start Provision Discovery once
  3659. * the new scan results become available.
  3660. */
  3661. ret = wpa_drv_scan(wpa_s, &params);
  3662. if (!ret) {
  3663. os_get_reltime(&wpa_s->scan_trigger_time);
  3664. wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
  3665. wpa_s->own_scan_requested = 1;
  3666. }
  3667. wpabuf_free(ies);
  3668. if (ret) {
  3669. wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
  3670. "try again later");
  3671. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3672. eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
  3673. wpas_p2p_check_join_scan_limit(wpa_s);
  3674. }
  3675. }
  3676. static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
  3677. {
  3678. struct wpa_supplicant *wpa_s = eloop_ctx;
  3679. wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
  3680. }
  3681. static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
  3682. const u8 *dev_addr, enum p2p_wps_method wps_method,
  3683. int auto_join, const u8 *ssid, size_t ssid_len)
  3684. {
  3685. wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
  3686. MACSTR " dev " MACSTR ")%s",
  3687. MAC2STR(iface_addr), MAC2STR(dev_addr),
  3688. auto_join ? " (auto_join)" : "");
  3689. if (ssid && ssid_len) {
  3690. wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
  3691. wpa_ssid_txt(ssid, ssid_len));
  3692. }
  3693. wpa_s->p2p_auto_pd = 0;
  3694. wpa_s->p2p_auto_join = !!auto_join;
  3695. os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
  3696. os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
  3697. wpa_s->pending_join_wps_method = wps_method;
  3698. /* Make sure we are not running find during connection establishment */
  3699. wpas_p2p_stop_find(wpa_s);
  3700. wpa_s->p2p_join_scan_count = 0;
  3701. wpas_p2p_join_scan_req(wpa_s, 0, ssid, ssid_len);
  3702. return 0;
  3703. }
  3704. static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
  3705. {
  3706. struct wpa_supplicant *group;
  3707. struct p2p_go_neg_results res;
  3708. struct wpa_bss *bss;
  3709. group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
  3710. if (group == NULL)
  3711. return -1;
  3712. if (group != wpa_s) {
  3713. os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
  3714. sizeof(group->p2p_pin));
  3715. group->p2p_wps_method = wpa_s->p2p_wps_method;
  3716. } else {
  3717. /*
  3718. * Need to mark the current interface for p2p_group_formation
  3719. * when a separate group interface is not used. This is needed
  3720. * to allow p2p_cancel stop a pending p2p_connect-join.
  3721. * wpas_p2p_init_group_interface() addresses this for the case
  3722. * where a separate group interface is used.
  3723. */
  3724. wpa_s->global->p2p_group_formation = wpa_s;
  3725. }
  3726. group->p2p_in_provisioning = 1;
  3727. group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
  3728. os_memset(&res, 0, sizeof(res));
  3729. os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
  3730. ETH_ALEN);
  3731. res.wps_method = wpa_s->pending_join_wps_method;
  3732. bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
  3733. if (bss) {
  3734. res.freq = bss->freq;
  3735. res.ssid_len = bss->ssid_len;
  3736. os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
  3737. wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency "
  3738. "from BSS table: %d MHz (SSID %s)", bss->freq,
  3739. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  3740. }
  3741. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  3742. wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
  3743. "starting client");
  3744. wpa_drv_cancel_remain_on_channel(wpa_s);
  3745. wpa_s->off_channel_freq = 0;
  3746. wpa_s->roc_waiting_drv_freq = 0;
  3747. }
  3748. wpas_start_wps_enrollee(group, &res);
  3749. /*
  3750. * Allow a longer timeout for join-a-running-group than normal 15
  3751. * second group formation timeout since the GO may not have authorized
  3752. * our connection yet.
  3753. */
  3754. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  3755. eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
  3756. wpa_s, NULL);
  3757. return 0;
  3758. }
  3759. static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
  3760. int *force_freq, int *pref_freq, int go)
  3761. {
  3762. int *freqs, res;
  3763. unsigned int freq_in_use = 0, num, i;
  3764. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  3765. if (!freqs)
  3766. return -1;
  3767. num = get_shared_radio_freqs(wpa_s, freqs,
  3768. wpa_s->num_multichan_concurrent);
  3769. wpa_printf(MSG_DEBUG,
  3770. "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
  3771. freq, wpa_s->num_multichan_concurrent, num);
  3772. if (freq > 0) {
  3773. int ret;
  3774. if (go)
  3775. ret = p2p_supported_freq(wpa_s->global->p2p, freq);
  3776. else
  3777. ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
  3778. if (!ret) {
  3779. wpa_printf(MSG_DEBUG, "P2P: The forced channel "
  3780. "(%u MHz) is not supported for P2P uses",
  3781. freq);
  3782. res = -3;
  3783. goto exit_free;
  3784. }
  3785. for (i = 0; i < num; i++) {
  3786. if (freqs[i] == freq)
  3787. freq_in_use = 1;
  3788. }
  3789. if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
  3790. wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
  3791. freq);
  3792. res = -2;
  3793. goto exit_free;
  3794. }
  3795. wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
  3796. "requested channel (%u MHz)", freq);
  3797. *force_freq = freq;
  3798. goto exit_ok;
  3799. }
  3800. for (i = 0; i < num; i++) {
  3801. if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
  3802. continue;
  3803. if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
  3804. wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
  3805. freqs[i]);
  3806. *pref_freq = freqs[i];
  3807. } else {
  3808. wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
  3809. freqs[i]);
  3810. *force_freq = freqs[i];
  3811. }
  3812. break;
  3813. }
  3814. if (i == num) {
  3815. if (num < wpa_s->num_multichan_concurrent && num > 0) {
  3816. wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
  3817. *force_freq = 0;
  3818. } else if (num < wpa_s->num_multichan_concurrent) {
  3819. wpa_printf(MSG_DEBUG, "P2P: No current operating channels - try to use a new channel");
  3820. *force_freq = 0;
  3821. } else {
  3822. wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
  3823. res = -2;
  3824. goto exit_free;
  3825. }
  3826. }
  3827. exit_ok:
  3828. res = 0;
  3829. exit_free:
  3830. os_free(freqs);
  3831. return res;
  3832. }
  3833. /**
  3834. * wpas_p2p_connect - Request P2P Group Formation to be started
  3835. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3836. * @peer_addr: Address of the peer P2P Device
  3837. * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
  3838. * @persistent_group: Whether to create a persistent group
  3839. * @auto_join: Whether to select join vs. GO Negotiation automatically
  3840. * @join: Whether to join an existing group (as a client) instead of starting
  3841. * Group Owner negotiation; @peer_addr is BSSID in that case
  3842. * @auth: Whether to only authorize the connection instead of doing that and
  3843. * initiating Group Owner negotiation
  3844. * @go_intent: GO Intent or -1 to use default
  3845. * @freq: Frequency for the group or 0 for auto-selection
  3846. * @persistent_id: Persistent group credentials to use for forcing GO
  3847. * parameters or -1 to generate new values (SSID/passphrase)
  3848. * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
  3849. * interoperability workaround when initiating group formation
  3850. * @ht40: Start GO with 40 MHz channel width
  3851. * @vht: Start GO with VHT support
  3852. * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
  3853. * failure, -2 on failure due to channel not currently available,
  3854. * -3 if forced channel is not supported
  3855. */
  3856. int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  3857. const char *pin, enum p2p_wps_method wps_method,
  3858. int persistent_group, int auto_join, int join, int auth,
  3859. int go_intent, int freq, int persistent_id, int pd,
  3860. int ht40, int vht)
  3861. {
  3862. int force_freq = 0, pref_freq = 0;
  3863. int ret = 0, res;
  3864. enum wpa_driver_if_type iftype;
  3865. const u8 *if_addr;
  3866. struct wpa_ssid *ssid = NULL;
  3867. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3868. return -1;
  3869. if (persistent_id >= 0) {
  3870. ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
  3871. if (ssid == NULL || ssid->disabled != 2 ||
  3872. ssid->mode != WPAS_MODE_P2P_GO)
  3873. return -1;
  3874. }
  3875. os_free(wpa_s->global->add_psk);
  3876. wpa_s->global->add_psk = NULL;
  3877. wpa_s->global->p2p_fail_on_wps_complete = 0;
  3878. if (go_intent < 0)
  3879. go_intent = wpa_s->conf->p2p_go_intent;
  3880. if (!auth)
  3881. wpa_s->p2p_long_listen = 0;
  3882. wpa_s->p2p_wps_method = wps_method;
  3883. wpa_s->p2p_persistent_group = !!persistent_group;
  3884. wpa_s->p2p_persistent_id = persistent_id;
  3885. wpa_s->p2p_go_intent = go_intent;
  3886. wpa_s->p2p_connect_freq = freq;
  3887. wpa_s->p2p_fallback_to_go_neg = 0;
  3888. wpa_s->p2p_pd_before_go_neg = !!pd;
  3889. wpa_s->p2p_go_ht40 = !!ht40;
  3890. wpa_s->p2p_go_vht = !!vht;
  3891. if (pin)
  3892. os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
  3893. else if (wps_method == WPS_PIN_DISPLAY) {
  3894. ret = wps_generate_pin();
  3895. os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
  3896. ret);
  3897. wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
  3898. wpa_s->p2p_pin);
  3899. } else
  3900. wpa_s->p2p_pin[0] = '\0';
  3901. if (join || auto_join) {
  3902. u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
  3903. if (auth) {
  3904. wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
  3905. "connect a running group from " MACSTR,
  3906. MAC2STR(peer_addr));
  3907. os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
  3908. return ret;
  3909. }
  3910. os_memcpy(dev_addr, peer_addr, ETH_ALEN);
  3911. if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
  3912. iface_addr) < 0) {
  3913. os_memcpy(iface_addr, peer_addr, ETH_ALEN);
  3914. p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
  3915. dev_addr);
  3916. }
  3917. if (auto_join) {
  3918. os_get_reltime(&wpa_s->p2p_auto_started);
  3919. wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
  3920. "%ld.%06ld",
  3921. wpa_s->p2p_auto_started.sec,
  3922. wpa_s->p2p_auto_started.usec);
  3923. }
  3924. wpa_s->user_initiated_pd = 1;
  3925. if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
  3926. auto_join, NULL, 0) < 0)
  3927. return -1;
  3928. return ret;
  3929. }
  3930. res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
  3931. go_intent == 15);
  3932. if (res)
  3933. return res;
  3934. wpas_p2p_set_own_freq_preference(wpa_s,
  3935. force_freq ? force_freq : pref_freq);
  3936. wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
  3937. if (wpa_s->create_p2p_iface) {
  3938. /* Prepare to add a new interface for the group */
  3939. iftype = WPA_IF_P2P_GROUP;
  3940. if (go_intent == 15)
  3941. iftype = WPA_IF_P2P_GO;
  3942. if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
  3943. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  3944. "interface for the group");
  3945. return -1;
  3946. }
  3947. if_addr = wpa_s->pending_interface_addr;
  3948. } else
  3949. if_addr = wpa_s->own_addr;
  3950. if (auth) {
  3951. if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
  3952. go_intent, if_addr,
  3953. force_freq, persistent_group, ssid,
  3954. pref_freq) < 0)
  3955. return -1;
  3956. return ret;
  3957. }
  3958. if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
  3959. go_intent, if_addr, force_freq,
  3960. persistent_group, ssid, pref_freq) < 0) {
  3961. if (wpa_s->create_p2p_iface)
  3962. wpas_p2p_remove_pending_group_interface(wpa_s);
  3963. return -1;
  3964. }
  3965. return ret;
  3966. }
  3967. /**
  3968. * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
  3969. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3970. * @freq: Frequency of the channel in MHz
  3971. * @duration: Duration of the stay on the channel in milliseconds
  3972. *
  3973. * This callback is called when the driver indicates that it has started the
  3974. * requested remain-on-channel duration.
  3975. */
  3976. void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  3977. unsigned int freq, unsigned int duration)
  3978. {
  3979. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3980. return;
  3981. if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
  3982. p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
  3983. wpa_s->pending_listen_duration);
  3984. wpa_s->pending_listen_freq = 0;
  3985. }
  3986. }
  3987. static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
  3988. unsigned int timeout)
  3989. {
  3990. /* Limit maximum Listen state time based on driver limitation. */
  3991. if (timeout > wpa_s->max_remain_on_chan)
  3992. timeout = wpa_s->max_remain_on_chan;
  3993. return p2p_listen(wpa_s->global->p2p, timeout);
  3994. }
  3995. /**
  3996. * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
  3997. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3998. * @freq: Frequency of the channel in MHz
  3999. *
  4000. * This callback is called when the driver indicates that a remain-on-channel
  4001. * operation has been completed, i.e., the duration on the requested channel
  4002. * has timed out.
  4003. */
  4004. void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  4005. unsigned int freq)
  4006. {
  4007. wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
  4008. "(p2p_long_listen=%d ms pending_action_tx=%p)",
  4009. wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
  4010. wpas_p2p_listen_work_done(wpa_s);
  4011. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4012. return;
  4013. if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
  4014. return; /* P2P module started a new operation */
  4015. if (offchannel_pending_action_tx(wpa_s))
  4016. return;
  4017. if (wpa_s->p2p_long_listen > 0)
  4018. wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
  4019. if (wpa_s->p2p_long_listen > 0) {
  4020. wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
  4021. wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
  4022. } else {
  4023. /*
  4024. * When listen duration is over, stop listen & update p2p_state
  4025. * to IDLE.
  4026. */
  4027. p2p_stop_listen(wpa_s->global->p2p);
  4028. }
  4029. }
  4030. /**
  4031. * wpas_p2p_group_remove - Remove a P2P group
  4032. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  4033. * @ifname: Network interface name of the group interface or "*" to remove all
  4034. * groups
  4035. * Returns: 0 on success, -1 on failure
  4036. *
  4037. * This function is used to remove a P2P group. This can be used to disconnect
  4038. * from a group in which the local end is a P2P Client or to end a P2P Group in
  4039. * case the local end is the Group Owner. If a virtual network interface was
  4040. * created for this group, that interface will be removed. Otherwise, only the
  4041. * configured P2P group network will be removed from the interface.
  4042. */
  4043. int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
  4044. {
  4045. struct wpa_global *global = wpa_s->global;
  4046. if (os_strcmp(ifname, "*") == 0) {
  4047. struct wpa_supplicant *prev;
  4048. wpa_s = global->ifaces;
  4049. while (wpa_s) {
  4050. prev = wpa_s;
  4051. wpa_s = wpa_s->next;
  4052. if (prev->p2p_group_interface !=
  4053. NOT_P2P_GROUP_INTERFACE ||
  4054. (prev->current_ssid &&
  4055. prev->current_ssid->p2p_group))
  4056. wpas_p2p_disconnect(prev);
  4057. }
  4058. return 0;
  4059. }
  4060. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  4061. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  4062. break;
  4063. }
  4064. return wpas_p2p_disconnect(wpa_s);
  4065. }
  4066. static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
  4067. {
  4068. unsigned int r;
  4069. if (freq == 2) {
  4070. wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
  4071. "band");
  4072. if (wpa_s->best_24_freq > 0 &&
  4073. p2p_supported_freq_go(wpa_s->global->p2p,
  4074. wpa_s->best_24_freq)) {
  4075. freq = wpa_s->best_24_freq;
  4076. wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
  4077. "channel: %d MHz", freq);
  4078. } else {
  4079. os_get_random((u8 *) &r, sizeof(r));
  4080. freq = 2412 + (r % 3) * 25;
  4081. wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
  4082. "channel: %d MHz", freq);
  4083. }
  4084. }
  4085. if (freq == 5) {
  4086. wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
  4087. "band");
  4088. if (wpa_s->best_5_freq > 0 &&
  4089. p2p_supported_freq_go(wpa_s->global->p2p,
  4090. wpa_s->best_5_freq)) {
  4091. freq = wpa_s->best_5_freq;
  4092. wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
  4093. "channel: %d MHz", freq);
  4094. } else {
  4095. os_get_random((u8 *) &r, sizeof(r));
  4096. freq = 5180 + (r % 4) * 20;
  4097. if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
  4098. wpa_printf(MSG_DEBUG, "P2P: Could not select "
  4099. "5 GHz channel for P2P group");
  4100. return -1;
  4101. }
  4102. wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
  4103. "channel: %d MHz", freq);
  4104. }
  4105. }
  4106. if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
  4107. wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
  4108. "(%u MHz) is not supported for P2P uses",
  4109. freq);
  4110. return -1;
  4111. }
  4112. return freq;
  4113. }
  4114. static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
  4115. struct p2p_go_neg_results *params,
  4116. int freq, int ht40, int vht,
  4117. const struct p2p_channels *channels)
  4118. {
  4119. int res, *freqs;
  4120. unsigned int pref_freq;
  4121. unsigned int num, i;
  4122. os_memset(params, 0, sizeof(*params));
  4123. params->role_go = 1;
  4124. params->ht40 = ht40;
  4125. params->vht = vht;
  4126. if (freq) {
  4127. if (!freq_included(channels, freq)) {
  4128. wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
  4129. "accepted", freq);
  4130. return -1;
  4131. }
  4132. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
  4133. "frequency %d MHz", freq);
  4134. params->freq = freq;
  4135. } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
  4136. wpa_s->conf->p2p_oper_channel >= 1 &&
  4137. wpa_s->conf->p2p_oper_channel <= 11 &&
  4138. freq_included(channels,
  4139. 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
  4140. params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
  4141. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
  4142. "frequency %d MHz", params->freq);
  4143. } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
  4144. wpa_s->conf->p2p_oper_reg_class == 116 ||
  4145. wpa_s->conf->p2p_oper_reg_class == 117 ||
  4146. wpa_s->conf->p2p_oper_reg_class == 124 ||
  4147. wpa_s->conf->p2p_oper_reg_class == 126 ||
  4148. wpa_s->conf->p2p_oper_reg_class == 127) &&
  4149. freq_included(channels,
  4150. 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
  4151. params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
  4152. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
  4153. "frequency %d MHz", params->freq);
  4154. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  4155. wpa_s->best_overall_freq > 0 &&
  4156. p2p_supported_freq_go(wpa_s->global->p2p,
  4157. wpa_s->best_overall_freq) &&
  4158. freq_included(channels, wpa_s->best_overall_freq)) {
  4159. params->freq = wpa_s->best_overall_freq;
  4160. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
  4161. "channel %d MHz", params->freq);
  4162. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  4163. wpa_s->best_24_freq > 0 &&
  4164. p2p_supported_freq_go(wpa_s->global->p2p,
  4165. wpa_s->best_24_freq) &&
  4166. freq_included(channels, wpa_s->best_24_freq)) {
  4167. params->freq = wpa_s->best_24_freq;
  4168. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
  4169. "channel %d MHz", params->freq);
  4170. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  4171. wpa_s->best_5_freq > 0 &&
  4172. p2p_supported_freq_go(wpa_s->global->p2p,
  4173. wpa_s->best_5_freq) &&
  4174. freq_included(channels, wpa_s->best_5_freq)) {
  4175. params->freq = wpa_s->best_5_freq;
  4176. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
  4177. "channel %d MHz", params->freq);
  4178. } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
  4179. channels))) {
  4180. params->freq = pref_freq;
  4181. wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
  4182. "channels", params->freq);
  4183. } else {
  4184. int chan;
  4185. for (chan = 0; chan < 11; chan++) {
  4186. params->freq = 2412 + chan * 5;
  4187. if (!wpas_p2p_disallowed_freq(wpa_s->global,
  4188. params->freq) &&
  4189. freq_included(channels, params->freq))
  4190. break;
  4191. }
  4192. if (chan == 11) {
  4193. wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
  4194. "allowed");
  4195. return -1;
  4196. }
  4197. wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
  4198. "known)", params->freq);
  4199. }
  4200. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  4201. if (!freqs)
  4202. return -1;
  4203. res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
  4204. wpa_s->num_multichan_concurrent);
  4205. if (res < 0) {
  4206. os_free(freqs);
  4207. return -1;
  4208. }
  4209. num = res;
  4210. for (i = 0; i < num; i++) {
  4211. if (freq && freqs[i] == freq)
  4212. break;
  4213. if (!freq && freq_included(channels, freqs[i])) {
  4214. wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
  4215. freqs[i]);
  4216. params->freq = freqs[i];
  4217. break;
  4218. }
  4219. }
  4220. if (i == num) {
  4221. if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
  4222. if (freq)
  4223. wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
  4224. else
  4225. wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
  4226. os_free(freqs);
  4227. return -1;
  4228. } else if (num == 0) {
  4229. wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
  4230. } else {
  4231. wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
  4232. }
  4233. }
  4234. os_free(freqs);
  4235. return 0;
  4236. }
  4237. static struct wpa_supplicant *
  4238. wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
  4239. int go)
  4240. {
  4241. struct wpa_supplicant *group_wpa_s;
  4242. if (!wpas_p2p_create_iface(wpa_s)) {
  4243. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
  4244. "operations");
  4245. wpa_s->p2p_first_connection_timeout = 0;
  4246. return wpa_s;
  4247. }
  4248. if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
  4249. WPA_IF_P2P_CLIENT) < 0) {
  4250. wpa_msg_global(wpa_s, MSG_ERROR,
  4251. "P2P: Failed to add group interface");
  4252. return NULL;
  4253. }
  4254. group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
  4255. if (group_wpa_s == NULL) {
  4256. wpa_msg_global(wpa_s, MSG_ERROR,
  4257. "P2P: Failed to initialize group interface");
  4258. wpas_p2p_remove_pending_group_interface(wpa_s);
  4259. return NULL;
  4260. }
  4261. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
  4262. group_wpa_s->ifname);
  4263. group_wpa_s->p2p_first_connection_timeout = 0;
  4264. return group_wpa_s;
  4265. }
  4266. /**
  4267. * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
  4268. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  4269. * @persistent_group: Whether to create a persistent group
  4270. * @freq: Frequency for the group or 0 to indicate no hardcoding
  4271. * @ht40: Start GO with 40 MHz channel width
  4272. * @vht: Start GO with VHT support
  4273. * Returns: 0 on success, -1 on failure
  4274. *
  4275. * This function creates a new P2P group with the local end as the Group Owner,
  4276. * i.e., without using Group Owner Negotiation.
  4277. */
  4278. int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
  4279. int freq, int ht40, int vht)
  4280. {
  4281. struct p2p_go_neg_results params;
  4282. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4283. return -1;
  4284. os_free(wpa_s->global->add_psk);
  4285. wpa_s->global->add_psk = NULL;
  4286. /* Make sure we are not running find during connection establishment */
  4287. wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
  4288. wpas_p2p_stop_find_oper(wpa_s);
  4289. freq = wpas_p2p_select_go_freq(wpa_s, freq);
  4290. if (freq < 0)
  4291. return -1;
  4292. if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, NULL))
  4293. return -1;
  4294. if (params.freq &&
  4295. !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
  4296. wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
  4297. "(%u MHz) is not supported for P2P uses",
  4298. params.freq);
  4299. return -1;
  4300. }
  4301. p2p_go_params(wpa_s->global->p2p, &params);
  4302. params.persistent_group = persistent_group;
  4303. wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
  4304. if (wpa_s == NULL)
  4305. return -1;
  4306. wpas_start_wps_go(wpa_s, &params, 0);
  4307. return 0;
  4308. }
  4309. static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
  4310. struct wpa_ssid *params, int addr_allocated)
  4311. {
  4312. struct wpa_ssid *ssid;
  4313. wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
  4314. if (wpa_s == NULL)
  4315. return -1;
  4316. wpa_s->p2p_last_4way_hs_fail = NULL;
  4317. wpa_supplicant_ap_deinit(wpa_s);
  4318. ssid = wpa_config_add_network(wpa_s->conf);
  4319. if (ssid == NULL)
  4320. return -1;
  4321. wpa_config_set_network_defaults(ssid);
  4322. ssid->temporary = 1;
  4323. ssid->proto = WPA_PROTO_RSN;
  4324. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  4325. ssid->group_cipher = WPA_CIPHER_CCMP;
  4326. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  4327. ssid->ssid = os_malloc(params->ssid_len);
  4328. if (ssid->ssid == NULL) {
  4329. wpa_config_remove_network(wpa_s->conf, ssid->id);
  4330. return -1;
  4331. }
  4332. os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
  4333. ssid->ssid_len = params->ssid_len;
  4334. ssid->p2p_group = 1;
  4335. ssid->export_keys = 1;
  4336. if (params->psk_set) {
  4337. os_memcpy(ssid->psk, params->psk, 32);
  4338. ssid->psk_set = 1;
  4339. }
  4340. if (params->passphrase)
  4341. ssid->passphrase = os_strdup(params->passphrase);
  4342. wpa_s->show_group_started = 1;
  4343. wpa_supplicant_select_network(wpa_s, ssid);
  4344. return 0;
  4345. }
  4346. int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
  4347. struct wpa_ssid *ssid, int addr_allocated,
  4348. int freq, int ht40, int vht,
  4349. const struct p2p_channels *channels,
  4350. int connection_timeout)
  4351. {
  4352. struct p2p_go_neg_results params;
  4353. int go = 0;
  4354. if (ssid->disabled != 2 || ssid->ssid == NULL)
  4355. return -1;
  4356. if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
  4357. go == (ssid->mode == WPAS_MODE_P2P_GO)) {
  4358. wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
  4359. "already running");
  4360. return 0;
  4361. }
  4362. os_free(wpa_s->global->add_psk);
  4363. wpa_s->global->add_psk = NULL;
  4364. /* Make sure we are not running find during connection establishment */
  4365. wpas_p2p_stop_find_oper(wpa_s);
  4366. wpa_s->p2p_fallback_to_go_neg = 0;
  4367. if (ssid->mode == WPAS_MODE_INFRA)
  4368. return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
  4369. if (ssid->mode != WPAS_MODE_P2P_GO)
  4370. return -1;
  4371. freq = wpas_p2p_select_go_freq(wpa_s, freq);
  4372. if (freq < 0)
  4373. return -1;
  4374. if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, channels))
  4375. return -1;
  4376. params.role_go = 1;
  4377. params.psk_set = ssid->psk_set;
  4378. if (params.psk_set)
  4379. os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
  4380. if (ssid->passphrase) {
  4381. if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
  4382. wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
  4383. "persistent group");
  4384. return -1;
  4385. }
  4386. os_strlcpy(params.passphrase, ssid->passphrase,
  4387. sizeof(params.passphrase));
  4388. }
  4389. os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
  4390. params.ssid_len = ssid->ssid_len;
  4391. params.persistent_group = 1;
  4392. wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
  4393. if (wpa_s == NULL)
  4394. return -1;
  4395. wpa_s->p2p_first_connection_timeout = connection_timeout;
  4396. wpas_start_wps_go(wpa_s, &params, 0);
  4397. return 0;
  4398. }
  4399. static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
  4400. struct wpabuf *proberesp_ies)
  4401. {
  4402. struct wpa_supplicant *wpa_s = ctx;
  4403. if (wpa_s->ap_iface) {
  4404. struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
  4405. if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
  4406. wpabuf_free(beacon_ies);
  4407. wpabuf_free(proberesp_ies);
  4408. return;
  4409. }
  4410. if (beacon_ies) {
  4411. wpabuf_free(hapd->p2p_beacon_ie);
  4412. hapd->p2p_beacon_ie = beacon_ies;
  4413. }
  4414. wpabuf_free(hapd->p2p_probe_resp_ie);
  4415. hapd->p2p_probe_resp_ie = proberesp_ies;
  4416. } else {
  4417. wpabuf_free(beacon_ies);
  4418. wpabuf_free(proberesp_ies);
  4419. }
  4420. wpa_supplicant_ap_update_beacon(wpa_s);
  4421. }
  4422. static void wpas_p2p_idle_update(void *ctx, int idle)
  4423. {
  4424. struct wpa_supplicant *wpa_s = ctx;
  4425. if (!wpa_s->ap_iface)
  4426. return;
  4427. wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
  4428. if (idle) {
  4429. if (wpa_s->global->p2p_fail_on_wps_complete &&
  4430. wpa_s->p2p_in_provisioning) {
  4431. wpas_p2p_grpform_fail_after_wps(wpa_s);
  4432. return;
  4433. }
  4434. wpas_p2p_set_group_idle_timeout(wpa_s);
  4435. } else
  4436. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  4437. }
  4438. struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
  4439. struct wpa_ssid *ssid)
  4440. {
  4441. struct p2p_group *group;
  4442. struct p2p_group_config *cfg;
  4443. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4444. return NULL;
  4445. cfg = os_zalloc(sizeof(*cfg));
  4446. if (cfg == NULL)
  4447. return NULL;
  4448. if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
  4449. cfg->persistent_group = 2;
  4450. else if (ssid->p2p_persistent_group)
  4451. cfg->persistent_group = 1;
  4452. os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
  4453. if (wpa_s->max_stations &&
  4454. wpa_s->max_stations < wpa_s->conf->max_num_sta)
  4455. cfg->max_clients = wpa_s->max_stations;
  4456. else
  4457. cfg->max_clients = wpa_s->conf->max_num_sta;
  4458. os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
  4459. cfg->ssid_len = ssid->ssid_len;
  4460. cfg->cb_ctx = wpa_s;
  4461. cfg->ie_update = wpas_p2p_ie_update;
  4462. cfg->idle_update = wpas_p2p_idle_update;
  4463. group = p2p_group_init(wpa_s->global->p2p, cfg);
  4464. if (group == NULL)
  4465. os_free(cfg);
  4466. if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
  4467. p2p_group_notif_formation_done(group);
  4468. wpa_s->p2p_group = group;
  4469. return group;
  4470. }
  4471. void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  4472. int registrar)
  4473. {
  4474. struct wpa_ssid *ssid = wpa_s->current_ssid;
  4475. if (!wpa_s->p2p_in_provisioning) {
  4476. wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
  4477. "provisioning not in progress");
  4478. return;
  4479. }
  4480. if (ssid && ssid->mode == WPAS_MODE_INFRA) {
  4481. u8 go_dev_addr[ETH_ALEN];
  4482. os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
  4483. wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
  4484. ssid->ssid_len);
  4485. /* Clear any stored provisioning info */
  4486. p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
  4487. }
  4488. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
  4489. NULL);
  4490. wpa_s->p2p_go_group_formation_completed = 1;
  4491. if (ssid && ssid->mode == WPAS_MODE_INFRA) {
  4492. /*
  4493. * Use a separate timeout for initial data connection to
  4494. * complete to allow the group to be removed automatically if
  4495. * something goes wrong in this step before the P2P group idle
  4496. * timeout mechanism is taken into use.
  4497. */
  4498. wpa_dbg(wpa_s, MSG_DEBUG,
  4499. "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
  4500. P2P_MAX_INITIAL_CONN_WAIT);
  4501. eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
  4502. wpas_p2p_group_formation_timeout,
  4503. wpa_s->parent, NULL);
  4504. } else if (ssid) {
  4505. /*
  4506. * Use a separate timeout for initial data connection to
  4507. * complete to allow the group to be removed automatically if
  4508. * the client does not complete data connection successfully.
  4509. */
  4510. wpa_dbg(wpa_s, MSG_DEBUG,
  4511. "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
  4512. P2P_MAX_INITIAL_CONN_WAIT_GO);
  4513. eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
  4514. wpas_p2p_group_formation_timeout,
  4515. wpa_s->parent, NULL);
  4516. /*
  4517. * Complete group formation on first successful data connection
  4518. */
  4519. wpa_s->p2p_go_group_formation_completed = 0;
  4520. }
  4521. if (wpa_s->global->p2p)
  4522. p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
  4523. wpas_group_formation_completed(wpa_s, 1);
  4524. }
  4525. void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
  4526. struct wps_event_fail *fail)
  4527. {
  4528. if (!wpa_s->p2p_in_provisioning) {
  4529. wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
  4530. "provisioning not in progress");
  4531. return;
  4532. }
  4533. if (wpa_s->go_params) {
  4534. p2p_clear_provisioning_info(
  4535. wpa_s->global->p2p,
  4536. wpa_s->go_params->peer_device_addr);
  4537. }
  4538. wpas_notify_p2p_wps_failed(wpa_s, fail);
  4539. if (wpa_s == wpa_s->global->p2p_group_formation) {
  4540. /*
  4541. * Allow some time for the failed WPS negotiation exchange to
  4542. * complete, but remove the group since group formation cannot
  4543. * succeed after provisioning failure.
  4544. */
  4545. wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
  4546. wpa_s->global->p2p_fail_on_wps_complete = 1;
  4547. eloop_deplete_timeout(0, 50000,
  4548. wpas_p2p_group_formation_timeout,
  4549. wpa_s->parent, NULL);
  4550. }
  4551. }
  4552. int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
  4553. {
  4554. if (!wpa_s->global->p2p_fail_on_wps_complete ||
  4555. !wpa_s->p2p_in_provisioning)
  4556. return 0;
  4557. wpas_p2p_grpform_fail_after_wps(wpa_s);
  4558. return 1;
  4559. }
  4560. int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  4561. const char *config_method,
  4562. enum wpas_p2p_prov_disc_use use)
  4563. {
  4564. u16 config_methods;
  4565. wpa_s->p2p_fallback_to_go_neg = 0;
  4566. wpa_s->pending_pd_use = NORMAL_PD;
  4567. if (os_strncmp(config_method, "display", 7) == 0)
  4568. config_methods = WPS_CONFIG_DISPLAY;
  4569. else if (os_strncmp(config_method, "keypad", 6) == 0)
  4570. config_methods = WPS_CONFIG_KEYPAD;
  4571. else if (os_strncmp(config_method, "pbc", 3) == 0 ||
  4572. os_strncmp(config_method, "pushbutton", 10) == 0)
  4573. config_methods = WPS_CONFIG_PUSHBUTTON;
  4574. else {
  4575. wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
  4576. return -1;
  4577. }
  4578. if (use == WPAS_P2P_PD_AUTO) {
  4579. os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
  4580. wpa_s->pending_pd_config_methods = config_methods;
  4581. wpa_s->p2p_auto_pd = 1;
  4582. wpa_s->p2p_auto_join = 0;
  4583. wpa_s->pending_pd_before_join = 0;
  4584. wpa_s->auto_pd_scan_retry = 0;
  4585. wpas_p2p_stop_find(wpa_s);
  4586. wpa_s->p2p_join_scan_count = 0;
  4587. os_get_reltime(&wpa_s->p2p_auto_started);
  4588. wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
  4589. wpa_s->p2p_auto_started.sec,
  4590. wpa_s->p2p_auto_started.usec);
  4591. wpas_p2p_join_scan(wpa_s, NULL);
  4592. return 0;
  4593. }
  4594. if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
  4595. return -1;
  4596. return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
  4597. config_methods, use == WPAS_P2P_PD_FOR_JOIN,
  4598. 0, 1);
  4599. }
  4600. int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
  4601. char *end)
  4602. {
  4603. return p2p_scan_result_text(ies, ies_len, buf, end);
  4604. }
  4605. static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
  4606. {
  4607. if (!offchannel_pending_action_tx(wpa_s))
  4608. return;
  4609. wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
  4610. "operation request");
  4611. offchannel_clear_pending_action_tx(wpa_s);
  4612. }
  4613. int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
  4614. enum p2p_discovery_type type,
  4615. unsigned int num_req_dev_types, const u8 *req_dev_types,
  4616. const u8 *dev_id, unsigned int search_delay)
  4617. {
  4618. wpas_p2p_clear_pending_action_tx(wpa_s);
  4619. wpa_s->p2p_long_listen = 0;
  4620. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
  4621. wpa_s->p2p_in_provisioning)
  4622. return -1;
  4623. wpa_supplicant_cancel_sched_scan(wpa_s);
  4624. return p2p_find(wpa_s->global->p2p, timeout, type,
  4625. num_req_dev_types, req_dev_types, dev_id,
  4626. search_delay);
  4627. }
  4628. static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
  4629. {
  4630. wpas_p2p_clear_pending_action_tx(wpa_s);
  4631. wpa_s->p2p_long_listen = 0;
  4632. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  4633. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  4634. if (wpa_s->global->p2p)
  4635. p2p_stop_find(wpa_s->global->p2p);
  4636. return 0;
  4637. }
  4638. void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
  4639. {
  4640. if (wpas_p2p_stop_find_oper(wpa_s) > 0)
  4641. return;
  4642. wpas_p2p_remove_pending_group_interface(wpa_s);
  4643. }
  4644. static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
  4645. {
  4646. struct wpa_supplicant *wpa_s = eloop_ctx;
  4647. wpa_s->p2p_long_listen = 0;
  4648. }
  4649. int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
  4650. {
  4651. int res;
  4652. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4653. return -1;
  4654. wpa_supplicant_cancel_sched_scan(wpa_s);
  4655. wpas_p2p_clear_pending_action_tx(wpa_s);
  4656. if (timeout == 0) {
  4657. /*
  4658. * This is a request for unlimited Listen state. However, at
  4659. * least for now, this is mapped to a Listen state for one
  4660. * hour.
  4661. */
  4662. timeout = 3600;
  4663. }
  4664. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  4665. wpa_s->p2p_long_listen = 0;
  4666. /*
  4667. * Stop previous find/listen operation to avoid trying to request a new
  4668. * remain-on-channel operation while the driver is still running the
  4669. * previous one.
  4670. */
  4671. if (wpa_s->global->p2p)
  4672. p2p_stop_find(wpa_s->global->p2p);
  4673. res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
  4674. if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
  4675. wpa_s->p2p_long_listen = timeout * 1000;
  4676. eloop_register_timeout(timeout, 0,
  4677. wpas_p2p_long_listen_timeout,
  4678. wpa_s, NULL);
  4679. }
  4680. return res;
  4681. }
  4682. int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  4683. u8 *buf, size_t len, int p2p_group)
  4684. {
  4685. struct wpabuf *p2p_ie;
  4686. int ret;
  4687. if (wpa_s->global->p2p_disabled)
  4688. return -1;
  4689. if (wpa_s->global->p2p == NULL)
  4690. return -1;
  4691. if (bss == NULL)
  4692. return -1;
  4693. p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  4694. ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
  4695. p2p_group, p2p_ie);
  4696. wpabuf_free(p2p_ie);
  4697. return ret;
  4698. }
  4699. int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
  4700. const u8 *dst, const u8 *bssid,
  4701. const u8 *ie, size_t ie_len, int ssi_signal)
  4702. {
  4703. if (wpa_s->global->p2p_disabled)
  4704. return 0;
  4705. if (wpa_s->global->p2p == NULL)
  4706. return 0;
  4707. switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
  4708. ie, ie_len)) {
  4709. case P2P_PREQ_NOT_P2P:
  4710. wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
  4711. ssi_signal);
  4712. /* fall through */
  4713. case P2P_PREQ_MALFORMED:
  4714. case P2P_PREQ_NOT_LISTEN:
  4715. case P2P_PREQ_NOT_PROCESSED:
  4716. default: /* make gcc happy */
  4717. return 0;
  4718. case P2P_PREQ_PROCESSED:
  4719. return 1;
  4720. }
  4721. }
  4722. void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
  4723. const u8 *sa, const u8 *bssid,
  4724. u8 category, const u8 *data, size_t len, int freq)
  4725. {
  4726. if (wpa_s->global->p2p_disabled)
  4727. return;
  4728. if (wpa_s->global->p2p == NULL)
  4729. return;
  4730. p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
  4731. freq);
  4732. }
  4733. void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
  4734. {
  4735. if (wpa_s->global->p2p_disabled)
  4736. return;
  4737. if (wpa_s->global->p2p == NULL)
  4738. return;
  4739. p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
  4740. }
  4741. void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
  4742. {
  4743. p2p_group_deinit(wpa_s->p2p_group);
  4744. wpa_s->p2p_group = NULL;
  4745. wpa_s->ap_configured_cb = NULL;
  4746. wpa_s->ap_configured_cb_ctx = NULL;
  4747. wpa_s->ap_configured_cb_data = NULL;
  4748. wpa_s->connect_without_scan = NULL;
  4749. }
  4750. int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
  4751. {
  4752. wpa_s->p2p_long_listen = 0;
  4753. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4754. return -1;
  4755. return p2p_reject(wpa_s->global->p2p, addr);
  4756. }
  4757. /* Invite to reinvoke a persistent group */
  4758. int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  4759. struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
  4760. int ht40, int vht, int pref_freq)
  4761. {
  4762. enum p2p_invite_role role;
  4763. u8 *bssid = NULL;
  4764. int force_freq = 0;
  4765. int res;
  4766. int no_pref_freq_given = pref_freq == 0;
  4767. wpa_s->global->p2p_invite_group = NULL;
  4768. if (peer_addr)
  4769. os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
  4770. else
  4771. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  4772. wpa_s->p2p_persistent_go_freq = freq;
  4773. wpa_s->p2p_go_ht40 = !!ht40;
  4774. if (ssid->mode == WPAS_MODE_P2P_GO) {
  4775. role = P2P_INVITE_ROLE_GO;
  4776. if (peer_addr == NULL) {
  4777. wpa_printf(MSG_DEBUG, "P2P: Missing peer "
  4778. "address in invitation command");
  4779. return -1;
  4780. }
  4781. if (wpas_p2p_create_iface(wpa_s)) {
  4782. if (wpas_p2p_add_group_interface(wpa_s,
  4783. WPA_IF_P2P_GO) < 0) {
  4784. wpa_printf(MSG_ERROR, "P2P: Failed to "
  4785. "allocate a new interface for the "
  4786. "group");
  4787. return -1;
  4788. }
  4789. bssid = wpa_s->pending_interface_addr;
  4790. } else
  4791. bssid = wpa_s->own_addr;
  4792. } else {
  4793. role = P2P_INVITE_ROLE_CLIENT;
  4794. peer_addr = ssid->bssid;
  4795. }
  4796. wpa_s->pending_invite_ssid_id = ssid->id;
  4797. res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
  4798. role == P2P_INVITE_ROLE_GO);
  4799. if (res)
  4800. return res;
  4801. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4802. return -1;
  4803. if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
  4804. no_pref_freq_given && pref_freq > 0 &&
  4805. wpa_s->num_multichan_concurrent > 1 &&
  4806. wpas_p2p_num_unused_channels(wpa_s) > 0) {
  4807. wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
  4808. pref_freq);
  4809. pref_freq = 0;
  4810. }
  4811. return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
  4812. ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
  4813. 1, pref_freq);
  4814. }
  4815. /* Invite to join an active group */
  4816. int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
  4817. const u8 *peer_addr, const u8 *go_dev_addr)
  4818. {
  4819. struct wpa_global *global = wpa_s->global;
  4820. enum p2p_invite_role role;
  4821. u8 *bssid = NULL;
  4822. struct wpa_ssid *ssid;
  4823. int persistent;
  4824. int freq = 0, force_freq = 0, pref_freq = 0;
  4825. int res;
  4826. wpa_s->p2p_persistent_go_freq = 0;
  4827. wpa_s->p2p_go_ht40 = 0;
  4828. wpa_s->p2p_go_vht = 0;
  4829. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  4830. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  4831. break;
  4832. }
  4833. if (wpa_s == NULL) {
  4834. wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
  4835. return -1;
  4836. }
  4837. ssid = wpa_s->current_ssid;
  4838. if (ssid == NULL) {
  4839. wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
  4840. "invitation");
  4841. return -1;
  4842. }
  4843. wpa_s->global->p2p_invite_group = wpa_s;
  4844. persistent = ssid->p2p_persistent_group &&
  4845. wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
  4846. ssid->ssid, ssid->ssid_len);
  4847. if (ssid->mode == WPAS_MODE_P2P_GO) {
  4848. role = P2P_INVITE_ROLE_ACTIVE_GO;
  4849. bssid = wpa_s->own_addr;
  4850. if (go_dev_addr == NULL)
  4851. go_dev_addr = wpa_s->global->p2p_dev_addr;
  4852. freq = ssid->frequency;
  4853. } else {
  4854. role = P2P_INVITE_ROLE_CLIENT;
  4855. if (wpa_s->wpa_state < WPA_ASSOCIATED) {
  4856. wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
  4857. "invite to current group");
  4858. return -1;
  4859. }
  4860. bssid = wpa_s->bssid;
  4861. if (go_dev_addr == NULL &&
  4862. !is_zero_ether_addr(wpa_s->go_dev_addr))
  4863. go_dev_addr = wpa_s->go_dev_addr;
  4864. freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
  4865. (int) wpa_s->assoc_freq;
  4866. }
  4867. wpa_s->parent->pending_invite_ssid_id = -1;
  4868. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4869. return -1;
  4870. res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
  4871. role == P2P_INVITE_ROLE_ACTIVE_GO);
  4872. if (res)
  4873. return res;
  4874. wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
  4875. return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
  4876. ssid->ssid, ssid->ssid_len, force_freq,
  4877. go_dev_addr, persistent, pref_freq);
  4878. }
  4879. void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
  4880. {
  4881. struct wpa_ssid *ssid = wpa_s->current_ssid;
  4882. const char *ssid_txt;
  4883. u8 go_dev_addr[ETH_ALEN];
  4884. int network_id = -1;
  4885. int persistent;
  4886. int freq;
  4887. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
  4888. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  4889. wpa_s->parent, NULL);
  4890. }
  4891. if (!wpa_s->show_group_started || !ssid)
  4892. return;
  4893. wpa_s->show_group_started = 0;
  4894. ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
  4895. os_memset(go_dev_addr, 0, ETH_ALEN);
  4896. if (ssid->bssid_set)
  4897. os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
  4898. persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
  4899. ssid->ssid_len);
  4900. os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
  4901. if (wpa_s->global->p2p_group_formation == wpa_s)
  4902. wpa_s->global->p2p_group_formation = NULL;
  4903. freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
  4904. (int) wpa_s->assoc_freq;
  4905. if (ssid->passphrase == NULL && ssid->psk_set) {
  4906. char psk[65];
  4907. wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
  4908. wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  4909. "%s client ssid=\"%s\" freq=%d psk=%s "
  4910. "go_dev_addr=" MACSTR "%s",
  4911. wpa_s->ifname, ssid_txt, freq, psk,
  4912. MAC2STR(go_dev_addr),
  4913. persistent ? " [PERSISTENT]" : "");
  4914. } else {
  4915. wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  4916. "%s client ssid=\"%s\" freq=%d "
  4917. "passphrase=\"%s\" go_dev_addr=" MACSTR "%s",
  4918. wpa_s->ifname, ssid_txt, freq,
  4919. ssid->passphrase ? ssid->passphrase : "",
  4920. MAC2STR(go_dev_addr),
  4921. persistent ? " [PERSISTENT]" : "");
  4922. }
  4923. if (persistent)
  4924. network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
  4925. ssid, go_dev_addr);
  4926. if (network_id < 0)
  4927. network_id = ssid->id;
  4928. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
  4929. }
  4930. int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
  4931. u32 interval1, u32 duration2, u32 interval2)
  4932. {
  4933. int ret;
  4934. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4935. return -1;
  4936. if (wpa_s->wpa_state < WPA_ASSOCIATED ||
  4937. wpa_s->current_ssid == NULL ||
  4938. wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
  4939. return -1;
  4940. ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
  4941. wpa_s->own_addr, wpa_s->assoc_freq,
  4942. duration1, interval1, duration2, interval2);
  4943. if (ret == 0)
  4944. wpa_s->waiting_presence_resp = 1;
  4945. return ret;
  4946. }
  4947. int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
  4948. unsigned int interval)
  4949. {
  4950. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4951. return -1;
  4952. return p2p_ext_listen(wpa_s->global->p2p, period, interval);
  4953. }
  4954. static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
  4955. {
  4956. if (wpa_s->current_ssid == NULL) {
  4957. /*
  4958. * current_ssid can be cleared when P2P client interface gets
  4959. * disconnected, so assume this interface was used as P2P
  4960. * client.
  4961. */
  4962. return 1;
  4963. }
  4964. return wpa_s->current_ssid->p2p_group &&
  4965. wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
  4966. }
  4967. static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
  4968. {
  4969. struct wpa_supplicant *wpa_s = eloop_ctx;
  4970. if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
  4971. wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
  4972. "disabled");
  4973. return;
  4974. }
  4975. wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
  4976. "group");
  4977. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
  4978. }
  4979. static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
  4980. {
  4981. int timeout;
  4982. if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
  4983. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
  4984. if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
  4985. return;
  4986. timeout = wpa_s->conf->p2p_group_idle;
  4987. if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
  4988. (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
  4989. timeout = P2P_MAX_CLIENT_IDLE;
  4990. if (timeout == 0)
  4991. return;
  4992. if (timeout < 0) {
  4993. if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
  4994. timeout = 0; /* special client mode no-timeout */
  4995. else
  4996. return;
  4997. }
  4998. if (wpa_s->p2p_in_provisioning) {
  4999. /*
  5000. * Use the normal group formation timeout during the
  5001. * provisioning phase to avoid terminating this process too
  5002. * early due to group idle timeout.
  5003. */
  5004. wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
  5005. "during provisioning");
  5006. return;
  5007. }
  5008. if (wpa_s->show_group_started) {
  5009. /*
  5010. * Use the normal group formation timeout between the end of
  5011. * the provisioning phase and completion of 4-way handshake to
  5012. * avoid terminating this process too early due to group idle
  5013. * timeout.
  5014. */
  5015. wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
  5016. "while waiting for initial 4-way handshake to "
  5017. "complete");
  5018. return;
  5019. }
  5020. wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
  5021. timeout);
  5022. eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
  5023. wpa_s, NULL);
  5024. }
  5025. /* Returns 1 if the interface was removed */
  5026. int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
  5027. u16 reason_code, const u8 *ie, size_t ie_len,
  5028. int locally_generated)
  5029. {
  5030. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5031. return 0;
  5032. if (!locally_generated)
  5033. p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
  5034. ie_len);
  5035. if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
  5036. wpa_s->current_ssid &&
  5037. wpa_s->current_ssid->p2p_group &&
  5038. wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
  5039. wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
  5040. "session is ending");
  5041. if (wpas_p2p_group_delete(wpa_s,
  5042. P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
  5043. > 0)
  5044. return 1;
  5045. }
  5046. return 0;
  5047. }
  5048. void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
  5049. u16 reason_code, const u8 *ie, size_t ie_len,
  5050. int locally_generated)
  5051. {
  5052. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5053. return;
  5054. if (!locally_generated)
  5055. p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
  5056. ie_len);
  5057. }
  5058. void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
  5059. {
  5060. struct p2p_data *p2p = wpa_s->global->p2p;
  5061. if (p2p == NULL)
  5062. return;
  5063. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
  5064. return;
  5065. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
  5066. p2p_set_dev_name(p2p, wpa_s->conf->device_name);
  5067. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
  5068. p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
  5069. if (wpa_s->wps &&
  5070. (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
  5071. p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
  5072. if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
  5073. p2p_set_uuid(p2p, wpa_s->wps->uuid);
  5074. if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
  5075. p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
  5076. p2p_set_model_name(p2p, wpa_s->conf->model_name);
  5077. p2p_set_model_number(p2p, wpa_s->conf->model_number);
  5078. p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
  5079. }
  5080. if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
  5081. p2p_set_sec_dev_types(p2p,
  5082. (void *) wpa_s->conf->sec_device_type,
  5083. wpa_s->conf->num_sec_device_types);
  5084. if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
  5085. int i;
  5086. p2p_remove_wps_vendor_extensions(p2p);
  5087. for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
  5088. if (wpa_s->conf->wps_vendor_ext[i] == NULL)
  5089. continue;
  5090. p2p_add_wps_vendor_extension(
  5091. p2p, wpa_s->conf->wps_vendor_ext[i]);
  5092. }
  5093. }
  5094. if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
  5095. wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  5096. char country[3];
  5097. country[0] = wpa_s->conf->country[0];
  5098. country[1] = wpa_s->conf->country[1];
  5099. country[2] = 0x04;
  5100. p2p_set_country(p2p, country);
  5101. }
  5102. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
  5103. p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
  5104. wpa_s->conf->p2p_ssid_postfix ?
  5105. os_strlen(wpa_s->conf->p2p_ssid_postfix) :
  5106. 0);
  5107. }
  5108. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
  5109. p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
  5110. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
  5111. u8 reg_class, channel;
  5112. int ret;
  5113. unsigned int r;
  5114. if (wpa_s->conf->p2p_listen_reg_class &&
  5115. wpa_s->conf->p2p_listen_channel) {
  5116. reg_class = wpa_s->conf->p2p_listen_reg_class;
  5117. channel = wpa_s->conf->p2p_listen_channel;
  5118. } else {
  5119. reg_class = 81;
  5120. /*
  5121. * Pick one of the social channels randomly as the
  5122. * listen channel.
  5123. */
  5124. os_get_random((u8 *) &r, sizeof(r));
  5125. channel = 1 + (r % 3) * 5;
  5126. }
  5127. ret = p2p_set_listen_channel(p2p, reg_class, channel);
  5128. if (ret)
  5129. wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
  5130. "failed: %d", ret);
  5131. }
  5132. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
  5133. u8 op_reg_class, op_channel, cfg_op_channel;
  5134. int ret = 0;
  5135. unsigned int r;
  5136. if (wpa_s->conf->p2p_oper_reg_class &&
  5137. wpa_s->conf->p2p_oper_channel) {
  5138. op_reg_class = wpa_s->conf->p2p_oper_reg_class;
  5139. op_channel = wpa_s->conf->p2p_oper_channel;
  5140. cfg_op_channel = 1;
  5141. } else {
  5142. op_reg_class = 81;
  5143. /*
  5144. * Use random operation channel from (1, 6, 11)
  5145. *if no other preference is indicated.
  5146. */
  5147. os_get_random((u8 *) &r, sizeof(r));
  5148. op_channel = 1 + (r % 3) * 5;
  5149. cfg_op_channel = 0;
  5150. }
  5151. ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
  5152. cfg_op_channel);
  5153. if (ret)
  5154. wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
  5155. "failed: %d", ret);
  5156. }
  5157. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
  5158. if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
  5159. wpa_s->conf->p2p_pref_chan) < 0) {
  5160. wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
  5161. "update failed");
  5162. }
  5163. if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
  5164. wpa_printf(MSG_ERROR, "P2P: No GO channel list "
  5165. "update failed");
  5166. }
  5167. }
  5168. }
  5169. int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
  5170. int duration)
  5171. {
  5172. if (!wpa_s->ap_iface)
  5173. return -1;
  5174. return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
  5175. duration);
  5176. }
  5177. int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
  5178. {
  5179. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5180. return -1;
  5181. wpa_s->global->cross_connection = enabled;
  5182. p2p_set_cross_connect(wpa_s->global->p2p, enabled);
  5183. if (!enabled) {
  5184. struct wpa_supplicant *iface;
  5185. for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
  5186. {
  5187. if (iface->cross_connect_enabled == 0)
  5188. continue;
  5189. iface->cross_connect_enabled = 0;
  5190. iface->cross_connect_in_use = 0;
  5191. wpa_msg_global(iface->parent, MSG_INFO,
  5192. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  5193. iface->ifname,
  5194. iface->cross_connect_uplink);
  5195. }
  5196. }
  5197. return 0;
  5198. }
  5199. static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
  5200. {
  5201. struct wpa_supplicant *iface;
  5202. if (!uplink->global->cross_connection)
  5203. return;
  5204. for (iface = uplink->global->ifaces; iface; iface = iface->next) {
  5205. if (!iface->cross_connect_enabled)
  5206. continue;
  5207. if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
  5208. 0)
  5209. continue;
  5210. if (iface->ap_iface == NULL)
  5211. continue;
  5212. if (iface->cross_connect_in_use)
  5213. continue;
  5214. iface->cross_connect_in_use = 1;
  5215. wpa_msg_global(iface->parent, MSG_INFO,
  5216. P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
  5217. iface->ifname, iface->cross_connect_uplink);
  5218. }
  5219. }
  5220. static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
  5221. {
  5222. struct wpa_supplicant *iface;
  5223. for (iface = uplink->global->ifaces; iface; iface = iface->next) {
  5224. if (!iface->cross_connect_enabled)
  5225. continue;
  5226. if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
  5227. 0)
  5228. continue;
  5229. if (!iface->cross_connect_in_use)
  5230. continue;
  5231. wpa_msg_global(iface->parent, MSG_INFO,
  5232. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  5233. iface->ifname, iface->cross_connect_uplink);
  5234. iface->cross_connect_in_use = 0;
  5235. }
  5236. }
  5237. void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
  5238. {
  5239. if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
  5240. wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
  5241. wpa_s->cross_connect_disallowed)
  5242. wpas_p2p_disable_cross_connect(wpa_s);
  5243. else
  5244. wpas_p2p_enable_cross_connect(wpa_s);
  5245. if (!wpa_s->ap_iface &&
  5246. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
  5247. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
  5248. }
  5249. void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
  5250. {
  5251. wpas_p2p_disable_cross_connect(wpa_s);
  5252. if (!wpa_s->ap_iface &&
  5253. !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
  5254. wpa_s, NULL))
  5255. wpas_p2p_set_group_idle_timeout(wpa_s);
  5256. }
  5257. static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
  5258. {
  5259. struct wpa_supplicant *iface;
  5260. if (!wpa_s->global->cross_connection)
  5261. return;
  5262. for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
  5263. if (iface == wpa_s)
  5264. continue;
  5265. if (iface->drv_flags &
  5266. WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
  5267. continue;
  5268. if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
  5269. continue;
  5270. wpa_s->cross_connect_enabled = 1;
  5271. os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
  5272. sizeof(wpa_s->cross_connect_uplink));
  5273. wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
  5274. "%s to %s whenever uplink is available",
  5275. wpa_s->ifname, wpa_s->cross_connect_uplink);
  5276. if (iface->ap_iface || iface->current_ssid == NULL ||
  5277. iface->current_ssid->mode != WPAS_MODE_INFRA ||
  5278. iface->cross_connect_disallowed ||
  5279. iface->wpa_state != WPA_COMPLETED)
  5280. break;
  5281. wpa_s->cross_connect_in_use = 1;
  5282. wpa_msg_global(wpa_s->parent, MSG_INFO,
  5283. P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
  5284. wpa_s->ifname, wpa_s->cross_connect_uplink);
  5285. break;
  5286. }
  5287. }
  5288. int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
  5289. {
  5290. if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
  5291. !wpa_s->p2p_in_provisioning)
  5292. return 0; /* not P2P client operation */
  5293. wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
  5294. "session overlap");
  5295. if (wpa_s != wpa_s->parent)
  5296. wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
  5297. wpas_p2p_group_formation_failed(wpa_s);
  5298. return 1;
  5299. }
  5300. void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
  5301. {
  5302. struct p2p_channels chan, cli_chan;
  5303. if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
  5304. return;
  5305. os_memset(&chan, 0, sizeof(chan));
  5306. os_memset(&cli_chan, 0, sizeof(cli_chan));
  5307. if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
  5308. wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
  5309. "channel list");
  5310. return;
  5311. }
  5312. p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
  5313. }
  5314. static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
  5315. struct wpa_scan_results *scan_res)
  5316. {
  5317. wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
  5318. }
  5319. int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
  5320. {
  5321. struct wpa_global *global = wpa_s->global;
  5322. int found = 0;
  5323. const u8 *peer;
  5324. if (global->p2p == NULL)
  5325. return -1;
  5326. wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
  5327. if (wpa_s->pending_interface_name[0] &&
  5328. !is_zero_ether_addr(wpa_s->pending_interface_addr))
  5329. found = 1;
  5330. peer = p2p_get_go_neg_peer(global->p2p);
  5331. if (peer) {
  5332. wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
  5333. MACSTR, MAC2STR(peer));
  5334. p2p_unauthorize(global->p2p, peer);
  5335. found = 1;
  5336. }
  5337. if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
  5338. wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
  5339. wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
  5340. found = 1;
  5341. }
  5342. if (wpa_s->pending_pd_before_join) {
  5343. wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
  5344. wpa_s->pending_pd_before_join = 0;
  5345. found = 1;
  5346. }
  5347. wpas_p2p_stop_find(wpa_s);
  5348. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  5349. if (wpa_s == global->p2p_group_formation &&
  5350. (wpa_s->p2p_in_provisioning ||
  5351. wpa_s->parent->pending_interface_type ==
  5352. WPA_IF_P2P_CLIENT)) {
  5353. wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
  5354. "formation found - cancelling",
  5355. wpa_s->ifname);
  5356. found = 1;
  5357. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  5358. wpa_s->parent, NULL);
  5359. if (wpa_s->p2p_in_provisioning) {
  5360. wpas_group_formation_completed(wpa_s, 0);
  5361. break;
  5362. }
  5363. wpas_p2p_group_delete(wpa_s,
  5364. P2P_GROUP_REMOVAL_REQUESTED);
  5365. break;
  5366. }
  5367. }
  5368. if (!found) {
  5369. wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
  5370. return -1;
  5371. }
  5372. return 0;
  5373. }
  5374. void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
  5375. {
  5376. if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
  5377. return;
  5378. wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
  5379. "being available anymore");
  5380. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
  5381. }
  5382. void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
  5383. int freq_24, int freq_5, int freq_overall)
  5384. {
  5385. struct p2p_data *p2p = wpa_s->global->p2p;
  5386. if (p2p == NULL)
  5387. return;
  5388. p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
  5389. }
  5390. int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
  5391. {
  5392. u8 peer[ETH_ALEN];
  5393. struct p2p_data *p2p = wpa_s->global->p2p;
  5394. if (p2p == NULL)
  5395. return -1;
  5396. if (hwaddr_aton(addr, peer))
  5397. return -1;
  5398. return p2p_unauthorize(p2p, peer);
  5399. }
  5400. /**
  5401. * wpas_p2p_disconnect - Disconnect from a P2P Group
  5402. * @wpa_s: Pointer to wpa_supplicant data
  5403. * Returns: 0 on success, -1 on failure
  5404. *
  5405. * This can be used to disconnect from a group in which the local end is a P2P
  5406. * Client or to end a P2P Group in case the local end is the Group Owner. If a
  5407. * virtual network interface was created for this group, that interface will be
  5408. * removed. Otherwise, only the configured P2P group network will be removed
  5409. * from the interface.
  5410. */
  5411. int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
  5412. {
  5413. if (wpa_s == NULL)
  5414. return -1;
  5415. return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
  5416. -1 : 0;
  5417. }
  5418. int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
  5419. {
  5420. int ret;
  5421. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5422. return 0;
  5423. ret = p2p_in_progress(wpa_s->global->p2p);
  5424. if (ret == 0) {
  5425. /*
  5426. * Check whether there is an ongoing WPS provisioning step (or
  5427. * other parts of group formation) on another interface since
  5428. * p2p_in_progress() does not report this to avoid issues for
  5429. * scans during such provisioning step.
  5430. */
  5431. if (wpa_s->global->p2p_group_formation &&
  5432. wpa_s->global->p2p_group_formation != wpa_s) {
  5433. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
  5434. "in group formation",
  5435. wpa_s->global->p2p_group_formation->ifname);
  5436. ret = 1;
  5437. }
  5438. }
  5439. if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
  5440. struct os_reltime now;
  5441. os_get_reltime(&now);
  5442. if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
  5443. P2P_MAX_INITIAL_CONN_WAIT_GO)) {
  5444. /* Wait for the first client has expired */
  5445. wpa_s->global->p2p_go_wait_client.sec = 0;
  5446. } else {
  5447. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
  5448. ret = 1;
  5449. }
  5450. }
  5451. return ret;
  5452. }
  5453. void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
  5454. struct wpa_ssid *ssid)
  5455. {
  5456. if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
  5457. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  5458. wpa_s->parent, NULL) > 0) {
  5459. /**
  5460. * Remove the network by scheduling the group formation
  5461. * timeout to happen immediately. The teardown code
  5462. * needs to be scheduled to run asynch later so that we
  5463. * don't delete data from under ourselves unexpectedly.
  5464. * Calling wpas_p2p_group_formation_timeout directly
  5465. * causes a series of crashes in WPS failure scenarios.
  5466. */
  5467. wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
  5468. "P2P group network getting removed");
  5469. eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
  5470. wpa_s->parent, NULL);
  5471. }
  5472. }
  5473. struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
  5474. const u8 *addr, const u8 *ssid,
  5475. size_t ssid_len)
  5476. {
  5477. struct wpa_ssid *s;
  5478. size_t i;
  5479. for (s = wpa_s->conf->ssid; s; s = s->next) {
  5480. if (s->disabled != 2)
  5481. continue;
  5482. if (ssid &&
  5483. (ssid_len != s->ssid_len ||
  5484. os_memcmp(ssid, s->ssid, ssid_len) != 0))
  5485. continue;
  5486. if (addr == NULL) {
  5487. if (s->mode == WPAS_MODE_P2P_GO)
  5488. return s;
  5489. continue;
  5490. }
  5491. if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
  5492. return s; /* peer is GO in the persistent group */
  5493. if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
  5494. continue;
  5495. for (i = 0; i < s->num_p2p_clients; i++) {
  5496. if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
  5497. addr, ETH_ALEN) == 0)
  5498. return s; /* peer is P2P client in persistent
  5499. * group */
  5500. }
  5501. }
  5502. return NULL;
  5503. }
  5504. void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
  5505. const u8 *addr)
  5506. {
  5507. if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  5508. wpa_s->parent, NULL) > 0) {
  5509. /*
  5510. * This can happen if WPS provisioning step is not terminated
  5511. * cleanly (e.g., P2P Client does not send WSC_Done). Since the
  5512. * peer was able to connect, there is no need to time out group
  5513. * formation after this, though. In addition, this is used with
  5514. * the initial connection wait on the GO as a separate formation
  5515. * timeout and as such, expected to be hit after the initial WPS
  5516. * provisioning step.
  5517. */
  5518. wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
  5519. }
  5520. if (!wpa_s->p2p_go_group_formation_completed) {
  5521. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
  5522. wpa_s->p2p_go_group_formation_completed = 1;
  5523. wpa_s->global->p2p_group_formation = NULL;
  5524. wpa_s->p2p_in_provisioning = 0;
  5525. }
  5526. wpa_s->global->p2p_go_wait_client.sec = 0;
  5527. if (addr == NULL)
  5528. return;
  5529. wpas_p2p_add_persistent_group_client(wpa_s, addr);
  5530. }
  5531. static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
  5532. int group_added)
  5533. {
  5534. struct wpa_supplicant *group = wpa_s;
  5535. if (wpa_s->global->p2p_group_formation)
  5536. group = wpa_s->global->p2p_group_formation;
  5537. wpa_s = wpa_s->parent;
  5538. offchannel_send_action_done(wpa_s);
  5539. if (group_added)
  5540. wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
  5541. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
  5542. wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
  5543. wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
  5544. 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
  5545. wpa_s->p2p_persistent_id,
  5546. wpa_s->p2p_pd_before_go_neg,
  5547. wpa_s->p2p_go_ht40,
  5548. wpa_s->p2p_go_vht);
  5549. }
  5550. int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
  5551. {
  5552. if (!wpa_s->p2p_fallback_to_go_neg ||
  5553. wpa_s->p2p_in_provisioning <= 5)
  5554. return 0;
  5555. if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
  5556. return 0; /* peer operating as a GO */
  5557. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
  5558. "fallback to GO Negotiation");
  5559. wpas_p2p_fallback_to_go_neg(wpa_s, 1);
  5560. return 1;
  5561. }
  5562. unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
  5563. {
  5564. struct wpa_supplicant *ifs;
  5565. if (wpa_s->wpa_state > WPA_SCANNING) {
  5566. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
  5567. "concurrent operation",
  5568. P2P_CONCURRENT_SEARCH_DELAY);
  5569. return P2P_CONCURRENT_SEARCH_DELAY;
  5570. }
  5571. dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
  5572. radio_list) {
  5573. if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
  5574. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
  5575. "delay due to concurrent operation on "
  5576. "interface %s",
  5577. P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
  5578. return P2P_CONCURRENT_SEARCH_DELAY;
  5579. }
  5580. }
  5581. return 0;
  5582. }
  5583. static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
  5584. struct wpa_ssid *s, const u8 *addr,
  5585. int iface_addr)
  5586. {
  5587. struct psk_list_entry *psk, *tmp;
  5588. int changed = 0;
  5589. dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
  5590. list) {
  5591. if ((iface_addr && !psk->p2p &&
  5592. os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
  5593. (!iface_addr && psk->p2p &&
  5594. os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
  5595. wpa_dbg(wpa_s, MSG_DEBUG,
  5596. "P2P: Remove persistent group PSK list entry for "
  5597. MACSTR " p2p=%u",
  5598. MAC2STR(psk->addr), psk->p2p);
  5599. dl_list_del(&psk->list);
  5600. os_free(psk);
  5601. changed++;
  5602. }
  5603. }
  5604. return changed;
  5605. }
  5606. void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
  5607. const u8 *p2p_dev_addr,
  5608. const u8 *psk, size_t psk_len)
  5609. {
  5610. struct wpa_ssid *ssid = wpa_s->current_ssid;
  5611. struct wpa_ssid *persistent;
  5612. struct psk_list_entry *p;
  5613. if (psk_len != sizeof(p->psk))
  5614. return;
  5615. if (p2p_dev_addr) {
  5616. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
  5617. " p2p_dev_addr=" MACSTR,
  5618. MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
  5619. if (is_zero_ether_addr(p2p_dev_addr))
  5620. p2p_dev_addr = NULL;
  5621. } else {
  5622. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
  5623. MAC2STR(mac_addr));
  5624. }
  5625. if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
  5626. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
  5627. /* To be added to persistent group once created */
  5628. if (wpa_s->global->add_psk == NULL) {
  5629. wpa_s->global->add_psk = os_zalloc(sizeof(*p));
  5630. if (wpa_s->global->add_psk == NULL)
  5631. return;
  5632. }
  5633. p = wpa_s->global->add_psk;
  5634. if (p2p_dev_addr) {
  5635. p->p2p = 1;
  5636. os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
  5637. } else {
  5638. p->p2p = 0;
  5639. os_memcpy(p->addr, mac_addr, ETH_ALEN);
  5640. }
  5641. os_memcpy(p->psk, psk, psk_len);
  5642. return;
  5643. }
  5644. if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
  5645. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
  5646. return;
  5647. }
  5648. persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
  5649. ssid->ssid_len);
  5650. if (!persistent) {
  5651. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
  5652. return;
  5653. }
  5654. p = os_zalloc(sizeof(*p));
  5655. if (p == NULL)
  5656. return;
  5657. if (p2p_dev_addr) {
  5658. p->p2p = 1;
  5659. os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
  5660. } else {
  5661. p->p2p = 0;
  5662. os_memcpy(p->addr, mac_addr, ETH_ALEN);
  5663. }
  5664. os_memcpy(p->psk, psk, psk_len);
  5665. if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS) {
  5666. struct psk_list_entry *last;
  5667. last = dl_list_last(&persistent->psk_list,
  5668. struct psk_list_entry, list);
  5669. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
  5670. MACSTR " (p2p=%u) to make room for a new one",
  5671. MAC2STR(last->addr), last->p2p);
  5672. dl_list_del(&last->list);
  5673. os_free(last);
  5674. }
  5675. wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
  5676. p2p_dev_addr ? p2p_dev_addr : mac_addr,
  5677. p2p_dev_addr == NULL);
  5678. if (p2p_dev_addr) {
  5679. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
  5680. MACSTR, MAC2STR(p2p_dev_addr));
  5681. } else {
  5682. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
  5683. MAC2STR(mac_addr));
  5684. }
  5685. dl_list_add(&persistent->psk_list, &p->list);
  5686. #ifndef CONFIG_NO_CONFIG_WRITE
  5687. if (wpa_s->parent->conf->update_config &&
  5688. wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
  5689. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  5690. #endif /* CONFIG_NO_CONFIG_WRITE */
  5691. }
  5692. static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
  5693. struct wpa_ssid *s, const u8 *addr,
  5694. int iface_addr)
  5695. {
  5696. int res;
  5697. res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
  5698. if (res > 0) {
  5699. #ifndef CONFIG_NO_CONFIG_WRITE
  5700. if (wpa_s->conf->update_config &&
  5701. wpa_config_write(wpa_s->confname, wpa_s->conf))
  5702. wpa_dbg(wpa_s, MSG_DEBUG,
  5703. "P2P: Failed to update configuration");
  5704. #endif /* CONFIG_NO_CONFIG_WRITE */
  5705. }
  5706. }
  5707. static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
  5708. const u8 *peer, int iface_addr)
  5709. {
  5710. struct hostapd_data *hapd;
  5711. struct hostapd_wpa_psk *psk, *prev, *rem;
  5712. struct sta_info *sta;
  5713. if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
  5714. wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
  5715. return;
  5716. /* Remove per-station PSK entry */
  5717. hapd = wpa_s->ap_iface->bss[0];
  5718. prev = NULL;
  5719. psk = hapd->conf->ssid.wpa_psk;
  5720. while (psk) {
  5721. if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
  5722. (!iface_addr &&
  5723. os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
  5724. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
  5725. MACSTR " iface_addr=%d",
  5726. MAC2STR(peer), iface_addr);
  5727. if (prev)
  5728. prev->next = psk->next;
  5729. else
  5730. hapd->conf->ssid.wpa_psk = psk->next;
  5731. rem = psk;
  5732. psk = psk->next;
  5733. os_free(rem);
  5734. } else {
  5735. prev = psk;
  5736. psk = psk->next;
  5737. }
  5738. }
  5739. /* Disconnect from group */
  5740. if (iface_addr)
  5741. sta = ap_get_sta(hapd, peer);
  5742. else
  5743. sta = ap_get_sta_p2p(hapd, peer);
  5744. if (sta) {
  5745. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
  5746. " (iface_addr=%d) from group",
  5747. MAC2STR(peer), iface_addr);
  5748. hostapd_drv_sta_deauth(hapd, sta->addr,
  5749. WLAN_REASON_DEAUTH_LEAVING);
  5750. ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
  5751. }
  5752. }
  5753. void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
  5754. int iface_addr)
  5755. {
  5756. struct wpa_ssid *s;
  5757. struct wpa_supplicant *w;
  5758. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
  5759. /* Remove from any persistent group */
  5760. for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
  5761. if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
  5762. continue;
  5763. if (!iface_addr)
  5764. wpas_remove_persistent_peer(wpa_s, s, peer, 0);
  5765. wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
  5766. }
  5767. /* Remove from any operating group */
  5768. for (w = wpa_s->global->ifaces; w; w = w->next)
  5769. wpas_p2p_remove_client_go(w, peer, iface_addr);
  5770. }
  5771. static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
  5772. {
  5773. struct wpa_supplicant *wpa_s = eloop_ctx;
  5774. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
  5775. }
  5776. static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
  5777. {
  5778. struct wpa_supplicant *wpa_s = eloop_ctx;
  5779. wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
  5780. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
  5781. }
  5782. int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
  5783. struct wpa_ssid *ssid)
  5784. {
  5785. struct wpa_supplicant *iface;
  5786. for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
  5787. if (!iface->current_ssid ||
  5788. iface->current_ssid->frequency == freq ||
  5789. (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
  5790. !iface->current_ssid->p2p_group))
  5791. continue;
  5792. /* Remove the connection with least priority */
  5793. if (!wpas_is_p2p_prioritized(iface)) {
  5794. /* STA connection has priority over existing
  5795. * P2P connection, so remove the interface. */
  5796. wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
  5797. eloop_register_timeout(0, 0,
  5798. wpas_p2p_group_freq_conflict,
  5799. iface, NULL);
  5800. /* If connection in progress is P2P connection, do not
  5801. * proceed for the connection. */
  5802. if (wpa_s == iface)
  5803. return -1;
  5804. else
  5805. return 0;
  5806. } else {
  5807. /* P2P connection has priority, disable the STA network
  5808. */
  5809. wpa_supplicant_disable_network(wpa_s->global->ifaces,
  5810. ssid);
  5811. wpa_msg(wpa_s->global->ifaces, MSG_INFO,
  5812. WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
  5813. os_memset(wpa_s->global->ifaces->pending_bssid, 0,
  5814. ETH_ALEN);
  5815. /* If P2P connection is in progress, continue
  5816. * connecting...*/
  5817. if (wpa_s == iface)
  5818. return 0;
  5819. else
  5820. return -1;
  5821. }
  5822. }
  5823. return 0;
  5824. }
  5825. int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
  5826. {
  5827. struct wpa_ssid *ssid = wpa_s->current_ssid;
  5828. if (ssid == NULL || !ssid->p2p_group)
  5829. return 0;
  5830. if (wpa_s->p2p_last_4way_hs_fail &&
  5831. wpa_s->p2p_last_4way_hs_fail == ssid) {
  5832. u8 go_dev_addr[ETH_ALEN];
  5833. struct wpa_ssid *persistent;
  5834. if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
  5835. ssid->ssid,
  5836. ssid->ssid_len) <= 0) {
  5837. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
  5838. goto disconnect;
  5839. }
  5840. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
  5841. MACSTR, MAC2STR(go_dev_addr));
  5842. persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
  5843. ssid->ssid,
  5844. ssid->ssid_len);
  5845. if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
  5846. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
  5847. goto disconnect;
  5848. }
  5849. wpa_msg_global(wpa_s->parent, MSG_INFO,
  5850. P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
  5851. persistent->id);
  5852. disconnect:
  5853. wpa_s->p2p_last_4way_hs_fail = NULL;
  5854. /*
  5855. * Remove the group from a timeout to avoid issues with caller
  5856. * continuing to use the interface if this is on a P2P group
  5857. * interface.
  5858. */
  5859. eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
  5860. wpa_s, NULL);
  5861. return 1;
  5862. }
  5863. wpa_s->p2p_last_4way_hs_fail = ssid;
  5864. return 0;
  5865. }