p2p_supplicant.c 229 KB

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