p2p_supplicant.c 244 KB

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