p2p_supplicant.c 220 KB

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