p2p_supplicant.c 230 KB

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