p2p_supplicant.c 227 KB

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