p2p_supplicant.c 177 KB

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