p2p_supplicant.c 177 KB

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