p2p_supplicant.c 235 KB

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