p2p_supplicant.c 183 KB

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