p2p_supplicant.c 186 KB

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