p2p_supplicant.c 254 KB

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