p2p_supplicant.c 224 KB

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