p2p_supplicant.c 218 KB

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