p2p_supplicant.c 189 KB

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