p2p_supplicant.c 173 KB

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