p2p_supplicant.c 221 KB

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