p2p_supplicant.c 227 KB

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