p2p_supplicant.c 255 KB

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