p2p_supplicant.c 177 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402
  1. /*
  2. * wpa_supplicant - P2P
  3. * Copyright (c) 2009-2010, Atheros Communications
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "eloop.h"
  11. #include "common/ieee802_11_common.h"
  12. #include "common/ieee802_11_defs.h"
  13. #include "common/wpa_ctrl.h"
  14. #include "wps/wps_i.h"
  15. #include "p2p/p2p.h"
  16. #include "ap/hostapd.h"
  17. #include "ap/ap_config.h"
  18. #include "ap/sta_info.h"
  19. #include "ap/ap_drv_ops.h"
  20. #include "ap/p2p_hostapd.h"
  21. #include "eapol_supp/eapol_supp_sm.h"
  22. #include "rsn_supp/wpa.h"
  23. #include "wpa_supplicant_i.h"
  24. #include "driver_i.h"
  25. #include "ap.h"
  26. #include "config_ssid.h"
  27. #include "config.h"
  28. #include "notify.h"
  29. #include "scan.h"
  30. #include "bss.h"
  31. #include "offchannel.h"
  32. #include "wps_supplicant.h"
  33. #include "p2p_supplicant.h"
  34. /*
  35. * How many times to try to scan to find the GO before giving up on join
  36. * request.
  37. */
  38. #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
  39. #define P2P_AUTO_PD_SCAN_ATTEMPTS 5
  40. #ifndef P2P_MAX_CLIENT_IDLE
  41. /*
  42. * How many seconds to try to reconnect to the GO when connection in P2P client
  43. * role has been lost.
  44. */
  45. #define P2P_MAX_CLIENT_IDLE 10
  46. #endif /* P2P_MAX_CLIENT_IDLE */
  47. #ifndef P2P_MAX_INITIAL_CONN_WAIT
  48. /*
  49. * How many seconds to wait for initial 4-way handshake to get completed after
  50. * WPS provisioning step.
  51. */
  52. #define P2P_MAX_INITIAL_CONN_WAIT 10
  53. #endif /* P2P_MAX_INITIAL_CONN_WAIT */
  54. #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
  55. /*
  56. * How many seconds to wait for initial 4-way handshake to get completed after
  57. * WPS provisioning step on the GO. This controls the extra time the P2P
  58. * operation is considered to be in progress (e.g., to delay other scans) after
  59. * WPS provisioning has been completed on the GO during group formation.
  60. */
  61. #define P2P_MAX_INITIAL_CONN_WAIT_GO 10
  62. #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
  63. #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
  64. /*
  65. * How many seconds to wait for initial 4-way handshake to get completed after
  66. * re-invocation of a persistent group on the GO when the client is expected
  67. * to connect automatically (no user interaction).
  68. */
  69. #define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
  70. #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
  71. #ifndef P2P_CONCURRENT_SEARCH_DELAY
  72. #define P2P_CONCURRENT_SEARCH_DELAY 500
  73. #endif /* P2P_CONCURRENT_SEARCH_DELAY */
  74. #define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
  75. enum p2p_group_removal_reason {
  76. P2P_GROUP_REMOVAL_UNKNOWN,
  77. P2P_GROUP_REMOVAL_SILENT,
  78. P2P_GROUP_REMOVAL_FORMATION_FAILED,
  79. P2P_GROUP_REMOVAL_REQUESTED,
  80. P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
  81. P2P_GROUP_REMOVAL_UNAVAILABLE,
  82. P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
  83. P2P_GROUP_REMOVAL_PSK_FAILURE
  84. };
  85. static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
  86. static struct wpa_supplicant *
  87. wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
  88. int go);
  89. static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
  90. static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
  91. static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
  92. static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
  93. const u8 *dev_addr, enum p2p_wps_method wps_method,
  94. int auto_join);
  95. static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
  96. static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
  97. static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
  98. static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
  99. static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
  100. void *timeout_ctx);
  101. static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
  102. int group_added);
  103. static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
  104. /*
  105. * Get the number of concurrent channels that the HW can operate, but that are
  106. * currently not in use by any of the wpa_supplicant interfaces.
  107. */
  108. static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
  109. {
  110. int *freqs;
  111. int num;
  112. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  113. if (!freqs)
  114. return -1;
  115. num = get_shared_radio_freqs(wpa_s, freqs,
  116. wpa_s->num_multichan_concurrent);
  117. os_free(freqs);
  118. return wpa_s->num_multichan_concurrent - num;
  119. }
  120. /*
  121. * Get the frequencies that are currently in use by one or more of the virtual
  122. * interfaces, and that are also valid for P2P operation.
  123. */
  124. static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
  125. int *p2p_freqs, unsigned int len)
  126. {
  127. int *freqs;
  128. unsigned int num, i, j;
  129. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  130. if (!freqs)
  131. return -1;
  132. num = get_shared_radio_freqs(wpa_s, freqs,
  133. wpa_s->num_multichan_concurrent);
  134. os_memset(p2p_freqs, 0, sizeof(int) * len);
  135. for (i = 0, j = 0; i < num && j < len; i++) {
  136. if (p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
  137. p2p_freqs[j++] = freqs[i];
  138. }
  139. os_free(freqs);
  140. return j;
  141. }
  142. static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
  143. int freq)
  144. {
  145. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  146. return;
  147. if (freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
  148. wpas_p2p_num_unused_channels(wpa_s) > 0 &&
  149. wpa_s->parent->conf->p2p_ignore_shared_freq)
  150. freq = 0;
  151. p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
  152. }
  153. static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
  154. struct wpa_scan_results *scan_res)
  155. {
  156. size_t i;
  157. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  158. return;
  159. wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
  160. (int) scan_res->num);
  161. for (i = 0; i < scan_res->num; i++) {
  162. struct wpa_scan_res *bss = scan_res->res[i];
  163. struct os_time time_tmp_age, entry_ts;
  164. time_tmp_age.sec = bss->age / 1000;
  165. time_tmp_age.usec = (bss->age % 1000) * 1000;
  166. os_time_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
  167. if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
  168. bss->freq, &entry_ts, bss->level,
  169. (const u8 *) (bss + 1),
  170. bss->ie_len) > 0)
  171. break;
  172. }
  173. p2p_scan_res_handled(wpa_s->global->p2p);
  174. }
  175. static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
  176. unsigned int num_req_dev_types,
  177. const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
  178. {
  179. struct wpa_supplicant *wpa_s = ctx;
  180. struct wpa_supplicant *ifs;
  181. struct wpa_driver_scan_params params;
  182. int ret;
  183. struct wpabuf *wps_ie, *ies;
  184. int social_channels[] = { 2412, 2437, 2462, 0, 0 };
  185. size_t ielen;
  186. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  187. return -1;
  188. for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
  189. if (ifs->sta_scan_pending &&
  190. (wpas_scan_scheduled(ifs) || ifs->scanning) &&
  191. wpas_p2p_in_progress(wpa_s) == 2) {
  192. wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
  193. "pending station mode scan to be "
  194. "completed on interface %s", ifs->ifname);
  195. wpa_s->global->p2p_cb_on_scan_complete = 1;
  196. wpa_supplicant_req_scan(ifs, 0, 0);
  197. return 1;
  198. }
  199. }
  200. os_memset(&params, 0, sizeof(params));
  201. /* P2P Wildcard SSID */
  202. params.num_ssids = 1;
  203. params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
  204. params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
  205. wpa_s->wps->dev.p2p = 1;
  206. wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
  207. wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
  208. num_req_dev_types, req_dev_types);
  209. if (wps_ie == NULL)
  210. return -1;
  211. ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
  212. ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
  213. if (ies == NULL) {
  214. wpabuf_free(wps_ie);
  215. return -1;
  216. }
  217. wpabuf_put_buf(ies, wps_ie);
  218. wpabuf_free(wps_ie);
  219. p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
  220. params.p2p_probe = 1;
  221. params.extra_ies = wpabuf_head(ies);
  222. params.extra_ies_len = wpabuf_len(ies);
  223. switch (type) {
  224. case P2P_SCAN_SOCIAL:
  225. params.freqs = social_channels;
  226. break;
  227. case P2P_SCAN_FULL:
  228. break;
  229. case P2P_SCAN_SOCIAL_PLUS_ONE:
  230. social_channels[3] = freq;
  231. params.freqs = social_channels;
  232. break;
  233. }
  234. ret = wpa_drv_scan(wpa_s, &params);
  235. wpabuf_free(ies);
  236. if (ret) {
  237. for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
  238. if (ifs->scanning ||
  239. ifs->scan_res_handler == wpas_p2p_scan_res_handler) {
  240. wpa_s->global->p2p_cb_on_scan_complete = 1;
  241. ret = 1;
  242. break;
  243. }
  244. }
  245. } else {
  246. os_get_time(&wpa_s->scan_trigger_time);
  247. wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
  248. }
  249. return ret;
  250. }
  251. static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
  252. {
  253. switch (p2p_group_interface) {
  254. case P2P_GROUP_INTERFACE_PENDING:
  255. return WPA_IF_P2P_GROUP;
  256. case P2P_GROUP_INTERFACE_GO:
  257. return WPA_IF_P2P_GO;
  258. case P2P_GROUP_INTERFACE_CLIENT:
  259. return WPA_IF_P2P_CLIENT;
  260. }
  261. return WPA_IF_P2P_GROUP;
  262. }
  263. static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
  264. const u8 *ssid,
  265. size_t ssid_len, int *go)
  266. {
  267. struct wpa_ssid *s;
  268. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  269. for (s = wpa_s->conf->ssid; s; s = s->next) {
  270. if (s->disabled != 0 || !s->p2p_group ||
  271. s->ssid_len != ssid_len ||
  272. os_memcmp(ssid, s->ssid, ssid_len) != 0)
  273. continue;
  274. if (s->mode == WPAS_MODE_P2P_GO &&
  275. s != wpa_s->current_ssid)
  276. continue;
  277. if (go)
  278. *go = s->mode == WPAS_MODE_P2P_GO;
  279. return wpa_s;
  280. }
  281. }
  282. return NULL;
  283. }
  284. static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
  285. enum p2p_group_removal_reason removal_reason)
  286. {
  287. struct wpa_ssid *ssid;
  288. char *gtype;
  289. const char *reason;
  290. ssid = wpa_s->current_ssid;
  291. if (ssid == NULL) {
  292. /*
  293. * The current SSID was not known, but there may still be a
  294. * pending P2P group interface waiting for provisioning or a
  295. * P2P group that is trying to reconnect.
  296. */
  297. ssid = wpa_s->conf->ssid;
  298. while (ssid) {
  299. if (ssid->p2p_group && ssid->disabled != 2)
  300. break;
  301. ssid = ssid->next;
  302. }
  303. if (ssid == NULL &&
  304. wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
  305. {
  306. wpa_printf(MSG_ERROR, "P2P: P2P group interface "
  307. "not found");
  308. return -1;
  309. }
  310. }
  311. if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
  312. gtype = "GO";
  313. else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
  314. (ssid && ssid->mode == WPAS_MODE_INFRA)) {
  315. wpa_s->reassociate = 0;
  316. wpa_s->disconnected = 1;
  317. wpa_supplicant_deauthenticate(wpa_s,
  318. WLAN_REASON_DEAUTH_LEAVING);
  319. gtype = "client";
  320. } else
  321. gtype = "GO";
  322. if (wpa_s->cross_connect_in_use) {
  323. wpa_s->cross_connect_in_use = 0;
  324. wpa_msg_global(wpa_s->parent, MSG_INFO,
  325. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  326. wpa_s->ifname, wpa_s->cross_connect_uplink);
  327. }
  328. switch (removal_reason) {
  329. case P2P_GROUP_REMOVAL_REQUESTED:
  330. reason = " reason=REQUESTED";
  331. break;
  332. case P2P_GROUP_REMOVAL_FORMATION_FAILED:
  333. reason = " reason=FORMATION_FAILED";
  334. break;
  335. case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
  336. reason = " reason=IDLE";
  337. break;
  338. case P2P_GROUP_REMOVAL_UNAVAILABLE:
  339. reason = " reason=UNAVAILABLE";
  340. break;
  341. case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
  342. reason = " reason=GO_ENDING_SESSION";
  343. break;
  344. case P2P_GROUP_REMOVAL_PSK_FAILURE:
  345. reason = " reason=PSK_FAILURE";
  346. break;
  347. default:
  348. reason = "";
  349. break;
  350. }
  351. if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
  352. wpa_msg_global(wpa_s->parent, MSG_INFO,
  353. P2P_EVENT_GROUP_REMOVED "%s %s%s",
  354. wpa_s->ifname, gtype, reason);
  355. }
  356. if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
  357. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
  358. if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  359. wpa_s->parent, NULL) > 0) {
  360. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
  361. "timeout");
  362. wpa_s->p2p_in_provisioning = 0;
  363. }
  364. if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
  365. wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
  366. if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
  367. struct wpa_global *global;
  368. char *ifname;
  369. enum wpa_driver_if_type type;
  370. wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
  371. wpa_s->ifname);
  372. global = wpa_s->global;
  373. ifname = os_strdup(wpa_s->ifname);
  374. type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
  375. wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
  376. wpa_s = global->ifaces;
  377. if (wpa_s && ifname)
  378. wpa_drv_if_remove(wpa_s, type, ifname);
  379. os_free(ifname);
  380. return 1;
  381. }
  382. if (!wpa_s->p2p_go_group_formation_completed) {
  383. wpa_s->global->p2p_group_formation = NULL;
  384. wpa_s->p2p_in_provisioning = 0;
  385. }
  386. wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
  387. if (ssid && (ssid->p2p_group ||
  388. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
  389. (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
  390. int id = ssid->id;
  391. if (ssid == wpa_s->current_ssid) {
  392. wpa_sm_set_config(wpa_s->wpa, NULL);
  393. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  394. wpa_s->current_ssid = NULL;
  395. }
  396. /*
  397. * Networks objects created during any P2P activities are not
  398. * exposed out as they might/will confuse certain non-P2P aware
  399. * applications since these network objects won't behave like
  400. * regular ones.
  401. *
  402. * Likewise, we don't send out network removed signals for such
  403. * network objects.
  404. */
  405. wpa_config_remove_network(wpa_s->conf, id);
  406. wpa_supplicant_clear_status(wpa_s);
  407. wpa_supplicant_cancel_sched_scan(wpa_s);
  408. wpa_s->sta_scan_pending = 0;
  409. } else {
  410. wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
  411. "found");
  412. }
  413. if (wpa_s->ap_iface)
  414. wpa_supplicant_ap_deinit(wpa_s);
  415. else
  416. wpa_drv_deinit_p2p_cli(wpa_s);
  417. return 0;
  418. }
  419. static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
  420. u8 *go_dev_addr,
  421. const u8 *ssid, size_t ssid_len)
  422. {
  423. struct wpa_bss *bss;
  424. const u8 *bssid;
  425. struct wpabuf *p2p;
  426. u8 group_capab;
  427. const u8 *addr;
  428. if (wpa_s->go_params)
  429. bssid = wpa_s->go_params->peer_interface_addr;
  430. else
  431. bssid = wpa_s->bssid;
  432. bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
  433. if (bss == NULL) {
  434. u8 iface_addr[ETH_ALEN];
  435. if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
  436. iface_addr) == 0)
  437. bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
  438. }
  439. if (bss == NULL) {
  440. wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
  441. "group is persistent - BSS " MACSTR " not found",
  442. MAC2STR(bssid));
  443. return 0;
  444. }
  445. p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  446. if (p2p == NULL) {
  447. wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
  448. "group is persistent - BSS " MACSTR
  449. " did not include P2P IE", MAC2STR(bssid));
  450. wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
  451. (u8 *) (bss + 1), bss->ie_len);
  452. wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
  453. ((u8 *) bss + 1) + bss->ie_len,
  454. bss->beacon_ie_len);
  455. return 0;
  456. }
  457. group_capab = p2p_get_group_capab(p2p);
  458. addr = p2p_get_go_dev_addr(p2p);
  459. wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
  460. "group_capab=0x%x", group_capab);
  461. if (addr) {
  462. os_memcpy(go_dev_addr, addr, ETH_ALEN);
  463. wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
  464. MAC2STR(addr));
  465. } else
  466. os_memset(go_dev_addr, 0, ETH_ALEN);
  467. wpabuf_free(p2p);
  468. wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
  469. "go_dev_addr=" MACSTR,
  470. MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
  471. return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
  472. }
  473. static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
  474. struct wpa_ssid *ssid,
  475. const u8 *go_dev_addr)
  476. {
  477. struct wpa_ssid *s;
  478. int changed = 0;
  479. wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
  480. "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
  481. for (s = wpa_s->conf->ssid; s; s = s->next) {
  482. if (s->disabled == 2 &&
  483. os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
  484. s->ssid_len == ssid->ssid_len &&
  485. os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
  486. break;
  487. }
  488. if (s) {
  489. wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
  490. "entry");
  491. if (ssid->passphrase && !s->passphrase)
  492. changed = 1;
  493. else if (ssid->passphrase && s->passphrase &&
  494. os_strcmp(ssid->passphrase, s->passphrase) != 0)
  495. changed = 1;
  496. } else {
  497. wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
  498. "entry");
  499. changed = 1;
  500. s = wpa_config_add_network(wpa_s->conf);
  501. if (s == NULL)
  502. return -1;
  503. /*
  504. * Instead of network_added we emit persistent_group_added
  505. * notification. Also to keep the defense checks in
  506. * persistent_group obj registration method, we set the
  507. * relevant flags in s to designate it as a persistent group.
  508. */
  509. s->p2p_group = 1;
  510. s->p2p_persistent_group = 1;
  511. wpas_notify_persistent_group_added(wpa_s, s);
  512. wpa_config_set_network_defaults(s);
  513. }
  514. s->p2p_group = 1;
  515. s->p2p_persistent_group = 1;
  516. s->disabled = 2;
  517. s->bssid_set = 1;
  518. os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
  519. s->mode = ssid->mode;
  520. s->auth_alg = WPA_AUTH_ALG_OPEN;
  521. s->key_mgmt = WPA_KEY_MGMT_PSK;
  522. s->proto = WPA_PROTO_RSN;
  523. s->pairwise_cipher = WPA_CIPHER_CCMP;
  524. s->export_keys = 1;
  525. if (ssid->passphrase) {
  526. os_free(s->passphrase);
  527. s->passphrase = os_strdup(ssid->passphrase);
  528. }
  529. if (ssid->psk_set) {
  530. s->psk_set = 1;
  531. os_memcpy(s->psk, ssid->psk, 32);
  532. }
  533. if (s->passphrase && !s->psk_set)
  534. wpa_config_update_psk(s);
  535. if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
  536. os_free(s->ssid);
  537. s->ssid = os_malloc(ssid->ssid_len);
  538. }
  539. if (s->ssid) {
  540. s->ssid_len = ssid->ssid_len;
  541. os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
  542. }
  543. if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
  544. dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
  545. wpa_s->global->add_psk = NULL;
  546. changed = 1;
  547. }
  548. #ifndef CONFIG_NO_CONFIG_WRITE
  549. if (changed && wpa_s->conf->update_config &&
  550. wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  551. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  552. }
  553. #endif /* CONFIG_NO_CONFIG_WRITE */
  554. return s->id;
  555. }
  556. static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
  557. const u8 *addr)
  558. {
  559. struct wpa_ssid *ssid, *s;
  560. u8 *n;
  561. size_t i;
  562. int found = 0;
  563. ssid = wpa_s->current_ssid;
  564. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  565. !ssid->p2p_persistent_group)
  566. return;
  567. for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
  568. if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
  569. continue;
  570. if (s->ssid_len == ssid->ssid_len &&
  571. os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
  572. break;
  573. }
  574. if (s == NULL)
  575. return;
  576. for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
  577. if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
  578. ETH_ALEN) != 0)
  579. continue;
  580. if (i == s->num_p2p_clients - 1)
  581. return; /* already the most recent entry */
  582. /* move the entry to mark it most recent */
  583. os_memmove(s->p2p_client_list + i * ETH_ALEN,
  584. s->p2p_client_list + (i + 1) * ETH_ALEN,
  585. (s->num_p2p_clients - i - 1) * ETH_ALEN);
  586. os_memcpy(s->p2p_client_list +
  587. (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
  588. found = 1;
  589. break;
  590. }
  591. if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
  592. n = os_realloc_array(s->p2p_client_list,
  593. s->num_p2p_clients + 1, ETH_ALEN);
  594. if (n == NULL)
  595. return;
  596. os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
  597. s->p2p_client_list = n;
  598. s->num_p2p_clients++;
  599. } else if (!found) {
  600. /* Not enough room for an additional entry - drop the oldest
  601. * entry */
  602. os_memmove(s->p2p_client_list,
  603. s->p2p_client_list + ETH_ALEN,
  604. (s->num_p2p_clients - 1) * ETH_ALEN);
  605. os_memcpy(s->p2p_client_list +
  606. (s->num_p2p_clients - 1) * ETH_ALEN,
  607. addr, ETH_ALEN);
  608. }
  609. #ifndef CONFIG_NO_CONFIG_WRITE
  610. if (wpa_s->parent->conf->update_config &&
  611. wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
  612. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  613. #endif /* CONFIG_NO_CONFIG_WRITE */
  614. }
  615. static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
  616. int success)
  617. {
  618. struct wpa_ssid *ssid;
  619. const char *ssid_txt;
  620. int client;
  621. int persistent;
  622. u8 go_dev_addr[ETH_ALEN];
  623. int network_id = -1;
  624. /*
  625. * This callback is likely called for the main interface. Update wpa_s
  626. * to use the group interface if a new interface was created for the
  627. * group.
  628. */
  629. if (wpa_s->global->p2p_group_formation)
  630. wpa_s = wpa_s->global->p2p_group_formation;
  631. if (wpa_s->p2p_go_group_formation_completed) {
  632. wpa_s->global->p2p_group_formation = NULL;
  633. wpa_s->p2p_in_provisioning = 0;
  634. }
  635. if (!success) {
  636. wpa_msg_global(wpa_s->parent, MSG_INFO,
  637. P2P_EVENT_GROUP_FORMATION_FAILURE);
  638. wpas_p2p_group_delete(wpa_s,
  639. P2P_GROUP_REMOVAL_FORMATION_FAILED);
  640. return;
  641. }
  642. wpa_msg_global(wpa_s->parent, MSG_INFO,
  643. P2P_EVENT_GROUP_FORMATION_SUCCESS);
  644. ssid = wpa_s->current_ssid;
  645. if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
  646. ssid->mode = WPAS_MODE_P2P_GO;
  647. p2p_group_notif_formation_done(wpa_s->p2p_group);
  648. wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
  649. }
  650. persistent = 0;
  651. if (ssid) {
  652. ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
  653. client = ssid->mode == WPAS_MODE_INFRA;
  654. if (ssid->mode == WPAS_MODE_P2P_GO) {
  655. persistent = ssid->p2p_persistent_group;
  656. os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
  657. ETH_ALEN);
  658. } else
  659. persistent = wpas_p2p_persistent_group(wpa_s,
  660. go_dev_addr,
  661. ssid->ssid,
  662. ssid->ssid_len);
  663. } else {
  664. ssid_txt = "";
  665. client = wpa_s->p2p_group_interface ==
  666. P2P_GROUP_INTERFACE_CLIENT;
  667. os_memset(go_dev_addr, 0, ETH_ALEN);
  668. }
  669. wpa_s->show_group_started = 0;
  670. if (client) {
  671. /*
  672. * Indicate event only after successfully completed 4-way
  673. * handshake, i.e., when the interface is ready for data
  674. * packets.
  675. */
  676. wpa_s->show_group_started = 1;
  677. } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
  678. char psk[65];
  679. wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
  680. wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  681. "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
  682. MACSTR "%s",
  683. wpa_s->ifname, ssid_txt, ssid->frequency, psk,
  684. MAC2STR(go_dev_addr),
  685. persistent ? " [PERSISTENT]" : "");
  686. wpas_p2p_cross_connect_setup(wpa_s);
  687. wpas_p2p_set_group_idle_timeout(wpa_s);
  688. } else {
  689. wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  690. "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
  691. "go_dev_addr=" MACSTR "%s",
  692. wpa_s->ifname, ssid_txt,
  693. ssid ? ssid->frequency : 0,
  694. ssid && ssid->passphrase ? ssid->passphrase : "",
  695. MAC2STR(go_dev_addr),
  696. persistent ? " [PERSISTENT]" : "");
  697. wpas_p2p_cross_connect_setup(wpa_s);
  698. wpas_p2p_set_group_idle_timeout(wpa_s);
  699. }
  700. if (persistent)
  701. network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
  702. ssid, go_dev_addr);
  703. else {
  704. os_free(wpa_s->global->add_psk);
  705. wpa_s->global->add_psk = NULL;
  706. }
  707. if (network_id < 0 && ssid)
  708. network_id = ssid->id;
  709. if (!client) {
  710. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
  711. os_get_time(&wpa_s->global->p2p_go_wait_client);
  712. }
  713. }
  714. static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
  715. unsigned int freq,
  716. const u8 *dst, const u8 *src,
  717. const u8 *bssid,
  718. const u8 *data, size_t data_len,
  719. enum offchannel_send_action_result
  720. result)
  721. {
  722. enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
  723. if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
  724. return;
  725. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  726. return;
  727. switch (result) {
  728. case OFFCHANNEL_SEND_ACTION_SUCCESS:
  729. res = P2P_SEND_ACTION_SUCCESS;
  730. break;
  731. case OFFCHANNEL_SEND_ACTION_NO_ACK:
  732. res = P2P_SEND_ACTION_NO_ACK;
  733. break;
  734. case OFFCHANNEL_SEND_ACTION_FAILED:
  735. res = P2P_SEND_ACTION_FAILED;
  736. break;
  737. }
  738. p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
  739. if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
  740. wpa_s->pending_pd_before_join &&
  741. (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
  742. os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
  743. wpa_s->p2p_fallback_to_go_neg) {
  744. wpa_s->pending_pd_before_join = 0;
  745. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
  746. "during p2p_connect-auto");
  747. wpas_p2p_fallback_to_go_neg(wpa_s, 0);
  748. return;
  749. }
  750. }
  751. static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
  752. const u8 *src, const u8 *bssid, const u8 *buf,
  753. size_t len, unsigned int wait_time)
  754. {
  755. struct wpa_supplicant *wpa_s = ctx;
  756. return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
  757. wait_time,
  758. wpas_p2p_send_action_tx_status, 1);
  759. }
  760. static void wpas_send_action_done(void *ctx)
  761. {
  762. struct wpa_supplicant *wpa_s = ctx;
  763. offchannel_send_action_done(wpa_s);
  764. }
  765. static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
  766. struct p2p_go_neg_results *params)
  767. {
  768. if (wpa_s->go_params == NULL) {
  769. wpa_s->go_params = os_malloc(sizeof(*params));
  770. if (wpa_s->go_params == NULL)
  771. return -1;
  772. }
  773. os_memcpy(wpa_s->go_params, params, sizeof(*params));
  774. return 0;
  775. }
  776. static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
  777. struct p2p_go_neg_results *res)
  778. {
  779. wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
  780. MAC2STR(res->peer_interface_addr));
  781. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
  782. res->ssid, res->ssid_len);
  783. wpa_supplicant_ap_deinit(wpa_s);
  784. wpas_copy_go_neg_results(wpa_s, res);
  785. if (res->wps_method == WPS_PBC)
  786. wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
  787. else {
  788. u16 dev_pw_id = DEV_PW_DEFAULT;
  789. if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
  790. dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
  791. wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
  792. wpa_s->p2p_pin, 1, dev_pw_id);
  793. }
  794. }
  795. static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
  796. struct wpa_ssid *ssid)
  797. {
  798. struct wpa_ssid *persistent;
  799. struct psk_list_entry *psk;
  800. struct hostapd_data *hapd;
  801. if (!wpa_s->ap_iface)
  802. return;
  803. persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
  804. ssid->ssid_len);
  805. if (persistent == NULL)
  806. return;
  807. hapd = wpa_s->ap_iface->bss[0];
  808. dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
  809. list) {
  810. struct hostapd_wpa_psk *hpsk;
  811. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
  812. MACSTR " psk=%d",
  813. MAC2STR(psk->addr), psk->p2p);
  814. hpsk = os_zalloc(sizeof(*hpsk));
  815. if (hpsk == NULL)
  816. break;
  817. os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
  818. if (psk->p2p)
  819. os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
  820. else
  821. os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
  822. hpsk->next = hapd->conf->ssid.wpa_psk;
  823. hapd->conf->ssid.wpa_psk = hpsk;
  824. }
  825. }
  826. static void p2p_go_configured(void *ctx, void *data)
  827. {
  828. struct wpa_supplicant *wpa_s = ctx;
  829. struct p2p_go_neg_results *params = data;
  830. struct wpa_ssid *ssid;
  831. int network_id = -1;
  832. ssid = wpa_s->current_ssid;
  833. if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
  834. wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
  835. if (wpa_s->global->p2p_group_formation == wpa_s)
  836. wpa_s->global->p2p_group_formation = NULL;
  837. if (os_strlen(params->passphrase) > 0) {
  838. wpa_msg_global(wpa_s->parent, MSG_INFO,
  839. P2P_EVENT_GROUP_STARTED
  840. "%s GO ssid=\"%s\" freq=%d "
  841. "passphrase=\"%s\" go_dev_addr=" MACSTR
  842. "%s", wpa_s->ifname,
  843. wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
  844. ssid->frequency, params->passphrase,
  845. MAC2STR(wpa_s->global->p2p_dev_addr),
  846. params->persistent_group ?
  847. " [PERSISTENT]" : "");
  848. } else {
  849. char psk[65];
  850. wpa_snprintf_hex(psk, sizeof(psk), params->psk,
  851. sizeof(params->psk));
  852. wpa_msg_global(wpa_s->parent, MSG_INFO,
  853. P2P_EVENT_GROUP_STARTED
  854. "%s GO ssid=\"%s\" freq=%d psk=%s "
  855. "go_dev_addr=" MACSTR "%s",
  856. wpa_s->ifname,
  857. wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
  858. ssid->frequency, psk,
  859. MAC2STR(wpa_s->global->p2p_dev_addr),
  860. params->persistent_group ?
  861. " [PERSISTENT]" : "");
  862. }
  863. os_get_time(&wpa_s->global->p2p_go_wait_client);
  864. if (params->persistent_group) {
  865. network_id = wpas_p2p_store_persistent_group(
  866. wpa_s->parent, ssid,
  867. wpa_s->global->p2p_dev_addr);
  868. wpas_p2p_add_psk_list(wpa_s, ssid);
  869. }
  870. if (network_id < 0)
  871. network_id = ssid->id;
  872. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
  873. wpas_p2p_cross_connect_setup(wpa_s);
  874. wpas_p2p_set_group_idle_timeout(wpa_s);
  875. if (wpa_s->p2p_first_connection_timeout) {
  876. wpa_dbg(wpa_s, MSG_DEBUG,
  877. "P2P: Start group formation timeout of %d seconds until first data connection on GO",
  878. wpa_s->p2p_first_connection_timeout);
  879. wpa_s->p2p_go_group_formation_completed = 0;
  880. wpa_s->global->p2p_group_formation = wpa_s;
  881. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  882. wpa_s->parent, NULL);
  883. eloop_register_timeout(
  884. wpa_s->p2p_first_connection_timeout, 0,
  885. wpas_p2p_group_formation_timeout,
  886. wpa_s->parent, NULL);
  887. }
  888. return;
  889. }
  890. wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
  891. if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
  892. params->peer_interface_addr)) {
  893. wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
  894. "filtering");
  895. return;
  896. }
  897. if (params->wps_method == WPS_PBC)
  898. wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
  899. params->peer_device_addr);
  900. else if (wpa_s->p2p_pin[0])
  901. wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
  902. wpa_s->p2p_pin, NULL, 0, 0);
  903. os_free(wpa_s->go_params);
  904. wpa_s->go_params = NULL;
  905. }
  906. static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
  907. struct p2p_go_neg_results *params,
  908. int group_formation)
  909. {
  910. struct wpa_ssid *ssid;
  911. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
  912. if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
  913. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
  914. "results");
  915. return;
  916. }
  917. ssid = wpa_config_add_network(wpa_s->conf);
  918. if (ssid == NULL) {
  919. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
  920. return;
  921. }
  922. wpa_s->show_group_started = 0;
  923. wpa_config_set_network_defaults(ssid);
  924. ssid->temporary = 1;
  925. ssid->p2p_group = 1;
  926. ssid->p2p_persistent_group = params->persistent_group;
  927. ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
  928. WPAS_MODE_P2P_GO;
  929. ssid->frequency = params->freq;
  930. ssid->ht40 = params->ht40;
  931. ssid->ssid = os_zalloc(params->ssid_len + 1);
  932. if (ssid->ssid) {
  933. os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
  934. ssid->ssid_len = params->ssid_len;
  935. }
  936. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  937. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  938. ssid->proto = WPA_PROTO_RSN;
  939. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  940. if (os_strlen(params->passphrase) > 0) {
  941. ssid->passphrase = os_strdup(params->passphrase);
  942. if (ssid->passphrase == NULL) {
  943. wpa_msg_global(wpa_s, MSG_ERROR,
  944. "P2P: Failed to copy passphrase for GO");
  945. wpa_config_remove_network(wpa_s->conf, ssid->id);
  946. return;
  947. }
  948. } else
  949. ssid->passphrase = NULL;
  950. ssid->psk_set = params->psk_set;
  951. if (ssid->psk_set)
  952. os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
  953. else if (ssid->passphrase)
  954. wpa_config_update_psk(ssid);
  955. ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
  956. wpa_s->ap_configured_cb = p2p_go_configured;
  957. wpa_s->ap_configured_cb_ctx = wpa_s;
  958. wpa_s->ap_configured_cb_data = wpa_s->go_params;
  959. wpa_s->connect_without_scan = ssid;
  960. wpa_s->reassociate = 1;
  961. wpa_s->disconnected = 0;
  962. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
  963. "start GO)");
  964. wpa_supplicant_req_scan(wpa_s, 0, 0);
  965. }
  966. static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
  967. const struct wpa_supplicant *src)
  968. {
  969. struct wpa_config *d;
  970. const struct wpa_config *s;
  971. d = dst->conf;
  972. s = src->conf;
  973. #define C(n) if (s->n) d->n = os_strdup(s->n)
  974. C(device_name);
  975. C(manufacturer);
  976. C(model_name);
  977. C(model_number);
  978. C(serial_number);
  979. C(config_methods);
  980. #undef C
  981. os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
  982. os_memcpy(d->sec_device_type, s->sec_device_type,
  983. sizeof(d->sec_device_type));
  984. d->num_sec_device_types = s->num_sec_device_types;
  985. d->p2p_group_idle = s->p2p_group_idle;
  986. d->p2p_intra_bss = s->p2p_intra_bss;
  987. d->persistent_reconnect = s->persistent_reconnect;
  988. d->max_num_sta = s->max_num_sta;
  989. d->pbc_in_m1 = s->pbc_in_m1;
  990. d->ignore_old_scan_res = s->ignore_old_scan_res;
  991. d->beacon_int = s->beacon_int;
  992. d->disassoc_low_ack = s->disassoc_low_ack;
  993. d->disable_scan_offload = s->disable_scan_offload;
  994. }
  995. static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
  996. char *ifname, size_t len)
  997. {
  998. char *ifname_ptr = wpa_s->ifname;
  999. if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
  1000. os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
  1001. ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
  1002. }
  1003. os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
  1004. if (os_strlen(ifname) >= IFNAMSIZ &&
  1005. os_strlen(wpa_s->ifname) < IFNAMSIZ) {
  1006. /* Try to avoid going over the IFNAMSIZ length limit */
  1007. os_snprintf(ifname, sizeof(ifname), "p2p-%d",
  1008. wpa_s->p2p_group_idx);
  1009. }
  1010. }
  1011. static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
  1012. enum wpa_driver_if_type type)
  1013. {
  1014. char ifname[120], force_ifname[120];
  1015. if (wpa_s->pending_interface_name[0]) {
  1016. wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
  1017. "- skip creation of a new one");
  1018. if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
  1019. wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
  1020. "unknown?! ifname='%s'",
  1021. wpa_s->pending_interface_name);
  1022. return -1;
  1023. }
  1024. return 0;
  1025. }
  1026. wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
  1027. force_ifname[0] = '\0';
  1028. wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
  1029. ifname);
  1030. wpa_s->p2p_group_idx++;
  1031. wpa_s->pending_interface_type = type;
  1032. if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
  1033. wpa_s->pending_interface_addr, NULL) < 0) {
  1034. wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
  1035. "interface");
  1036. return -1;
  1037. }
  1038. if (force_ifname[0]) {
  1039. wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
  1040. force_ifname);
  1041. os_strlcpy(wpa_s->pending_interface_name, force_ifname,
  1042. sizeof(wpa_s->pending_interface_name));
  1043. } else
  1044. os_strlcpy(wpa_s->pending_interface_name, ifname,
  1045. sizeof(wpa_s->pending_interface_name));
  1046. wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
  1047. MACSTR, wpa_s->pending_interface_name,
  1048. MAC2STR(wpa_s->pending_interface_addr));
  1049. return 0;
  1050. }
  1051. static void wpas_p2p_remove_pending_group_interface(
  1052. struct wpa_supplicant *wpa_s)
  1053. {
  1054. if (!wpa_s->pending_interface_name[0] ||
  1055. is_zero_ether_addr(wpa_s->pending_interface_addr))
  1056. return; /* No pending virtual interface */
  1057. wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
  1058. wpa_s->pending_interface_name);
  1059. wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
  1060. wpa_s->pending_interface_name);
  1061. os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
  1062. wpa_s->pending_interface_name[0] = '\0';
  1063. }
  1064. static struct wpa_supplicant *
  1065. wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
  1066. {
  1067. struct wpa_interface iface;
  1068. struct wpa_supplicant *group_wpa_s;
  1069. if (!wpa_s->pending_interface_name[0]) {
  1070. wpa_printf(MSG_ERROR, "P2P: No pending group interface");
  1071. if (!wpas_p2p_create_iface(wpa_s))
  1072. return NULL;
  1073. /*
  1074. * Something has forced us to remove the pending interface; try
  1075. * to create a new one and hope for the best that we will get
  1076. * the same local address.
  1077. */
  1078. if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
  1079. WPA_IF_P2P_CLIENT) < 0)
  1080. return NULL;
  1081. }
  1082. os_memset(&iface, 0, sizeof(iface));
  1083. iface.ifname = wpa_s->pending_interface_name;
  1084. iface.driver = wpa_s->driver->name;
  1085. if (wpa_s->conf->ctrl_interface == NULL &&
  1086. wpa_s->parent != wpa_s &&
  1087. wpa_s->p2p_mgmt &&
  1088. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
  1089. iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
  1090. else
  1091. iface.ctrl_interface = wpa_s->conf->ctrl_interface;
  1092. iface.driver_param = wpa_s->conf->driver_param;
  1093. group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
  1094. if (group_wpa_s == NULL) {
  1095. wpa_printf(MSG_ERROR, "P2P: Failed to create new "
  1096. "wpa_supplicant interface");
  1097. return NULL;
  1098. }
  1099. wpa_s->pending_interface_name[0] = '\0';
  1100. group_wpa_s->parent = wpa_s;
  1101. group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
  1102. P2P_GROUP_INTERFACE_CLIENT;
  1103. wpa_s->global->p2p_group_formation = group_wpa_s;
  1104. wpas_p2p_clone_config(group_wpa_s, wpa_s);
  1105. return group_wpa_s;
  1106. }
  1107. static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
  1108. void *timeout_ctx)
  1109. {
  1110. struct wpa_supplicant *wpa_s = eloop_ctx;
  1111. wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
  1112. wpas_p2p_group_formation_failed(wpa_s);
  1113. }
  1114. void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
  1115. {
  1116. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  1117. wpa_s->parent, NULL);
  1118. if (wpa_s->global->p2p)
  1119. p2p_group_formation_failed(wpa_s->global->p2p);
  1120. else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  1121. wpa_drv_p2p_group_formation_failed(wpa_s);
  1122. wpas_group_formation_completed(wpa_s, 0);
  1123. }
  1124. void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
  1125. {
  1126. if (wpa_s->global->p2p_group_formation != wpa_s)
  1127. return;
  1128. /* Speed up group formation timeout since this cannot succeed */
  1129. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  1130. wpa_s->parent, NULL);
  1131. eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
  1132. wpa_s->parent, NULL);
  1133. }
  1134. void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
  1135. {
  1136. struct wpa_supplicant *wpa_s = ctx;
  1137. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  1138. wpa_drv_cancel_remain_on_channel(wpa_s);
  1139. wpa_s->off_channel_freq = 0;
  1140. wpa_s->roc_waiting_drv_freq = 0;
  1141. }
  1142. if (res->status) {
  1143. wpa_msg_global(wpa_s, MSG_INFO,
  1144. P2P_EVENT_GO_NEG_FAILURE "status=%d",
  1145. res->status);
  1146. wpas_notify_p2p_go_neg_completed(wpa_s, res);
  1147. wpas_p2p_remove_pending_group_interface(wpa_s);
  1148. return;
  1149. }
  1150. if (wpa_s->p2p_go_ht40)
  1151. res->ht40 = 1;
  1152. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
  1153. "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
  1154. " wps_method=%s",
  1155. res->role_go ? "GO" : "client", res->freq, res->ht40,
  1156. MAC2STR(res->peer_device_addr),
  1157. MAC2STR(res->peer_interface_addr),
  1158. p2p_wps_method_text(res->wps_method));
  1159. wpas_notify_p2p_go_neg_completed(wpa_s, res);
  1160. if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
  1161. struct wpa_ssid *ssid;
  1162. ssid = wpa_config_get_network(wpa_s->conf,
  1163. wpa_s->p2p_persistent_id);
  1164. if (ssid && ssid->disabled == 2 &&
  1165. ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
  1166. size_t len = os_strlen(ssid->passphrase);
  1167. wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
  1168. "on requested persistent group");
  1169. os_memcpy(res->passphrase, ssid->passphrase, len);
  1170. res->passphrase[len] = '\0';
  1171. }
  1172. }
  1173. if (wpa_s->create_p2p_iface) {
  1174. struct wpa_supplicant *group_wpa_s =
  1175. wpas_p2p_init_group_interface(wpa_s, res->role_go);
  1176. if (group_wpa_s == NULL) {
  1177. wpas_p2p_remove_pending_group_interface(wpa_s);
  1178. return;
  1179. }
  1180. if (group_wpa_s != wpa_s) {
  1181. os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
  1182. sizeof(group_wpa_s->p2p_pin));
  1183. group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
  1184. }
  1185. os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
  1186. wpa_s->pending_interface_name[0] = '\0';
  1187. group_wpa_s->p2p_in_provisioning = 1;
  1188. if (res->role_go)
  1189. wpas_start_wps_go(group_wpa_s, res, 1);
  1190. else
  1191. wpas_start_wps_enrollee(group_wpa_s, res);
  1192. } else {
  1193. wpa_s->p2p_in_provisioning = 1;
  1194. wpa_s->global->p2p_group_formation = wpa_s;
  1195. if (res->role_go)
  1196. wpas_start_wps_go(wpa_s, res, 1);
  1197. else
  1198. wpas_start_wps_enrollee(ctx, res);
  1199. }
  1200. wpa_s->p2p_long_listen = 0;
  1201. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  1202. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  1203. eloop_register_timeout(15 + res->peer_config_timeout / 100,
  1204. (res->peer_config_timeout % 100) * 10000,
  1205. wpas_p2p_group_formation_timeout, wpa_s, NULL);
  1206. }
  1207. void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
  1208. {
  1209. struct wpa_supplicant *wpa_s = ctx;
  1210. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
  1211. " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
  1212. wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
  1213. }
  1214. void wpas_dev_found(void *ctx, const u8 *addr,
  1215. const struct p2p_peer_info *info,
  1216. int new_device)
  1217. {
  1218. #ifndef CONFIG_NO_STDOUT_DEBUG
  1219. struct wpa_supplicant *wpa_s = ctx;
  1220. char devtype[WPS_DEV_TYPE_BUFSIZE];
  1221. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
  1222. " p2p_dev_addr=" MACSTR
  1223. " pri_dev_type=%s name='%s' config_methods=0x%x "
  1224. "dev_capab=0x%x group_capab=0x%x",
  1225. MAC2STR(addr), MAC2STR(info->p2p_device_addr),
  1226. wps_dev_type_bin2str(info->pri_dev_type, devtype,
  1227. sizeof(devtype)),
  1228. info->device_name, info->config_methods,
  1229. info->dev_capab, info->group_capab);
  1230. #endif /* CONFIG_NO_STDOUT_DEBUG */
  1231. wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
  1232. }
  1233. static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
  1234. {
  1235. struct wpa_supplicant *wpa_s = ctx;
  1236. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
  1237. "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
  1238. wpas_notify_p2p_device_lost(wpa_s, dev_addr);
  1239. }
  1240. static void wpas_find_stopped(void *ctx)
  1241. {
  1242. struct wpa_supplicant *wpa_s = ctx;
  1243. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
  1244. }
  1245. static int wpas_start_listen(void *ctx, unsigned int freq,
  1246. unsigned int duration,
  1247. const struct wpabuf *probe_resp_ie)
  1248. {
  1249. struct wpa_supplicant *wpa_s = ctx;
  1250. wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
  1251. if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
  1252. wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
  1253. "report received Probe Request frames");
  1254. return -1;
  1255. }
  1256. wpa_s->pending_listen_freq = freq;
  1257. wpa_s->pending_listen_duration = duration;
  1258. if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
  1259. wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
  1260. "to remain on channel (%u MHz) for Listen "
  1261. "state", freq);
  1262. wpa_s->pending_listen_freq = 0;
  1263. return -1;
  1264. }
  1265. wpa_s->off_channel_freq = 0;
  1266. wpa_s->roc_waiting_drv_freq = freq;
  1267. return 0;
  1268. }
  1269. static void wpas_stop_listen(void *ctx)
  1270. {
  1271. struct wpa_supplicant *wpa_s = ctx;
  1272. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  1273. wpa_drv_cancel_remain_on_channel(wpa_s);
  1274. wpa_s->off_channel_freq = 0;
  1275. wpa_s->roc_waiting_drv_freq = 0;
  1276. }
  1277. wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
  1278. wpa_drv_probe_req_report(wpa_s, 0);
  1279. }
  1280. static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
  1281. {
  1282. struct wpa_supplicant *wpa_s = ctx;
  1283. return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
  1284. }
  1285. /*
  1286. * DNS Header section is used only to calculate compression pointers, so the
  1287. * contents of this data does not matter, but the length needs to be reserved
  1288. * in the virtual packet.
  1289. */
  1290. #define DNS_HEADER_LEN 12
  1291. /*
  1292. * 27-octet in-memory packet from P2P specification containing two implied
  1293. * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
  1294. */
  1295. #define P2P_SD_IN_MEMORY_LEN 27
  1296. static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
  1297. u8 **spos, const u8 *end)
  1298. {
  1299. while (*spos < end) {
  1300. u8 val = ((*spos)[0] & 0xc0) >> 6;
  1301. int len;
  1302. if (val == 1 || val == 2) {
  1303. /* These are reserved values in RFC 1035 */
  1304. wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
  1305. "sequence starting with 0x%x", val);
  1306. return -1;
  1307. }
  1308. if (val == 3) {
  1309. u16 offset;
  1310. u8 *spos_tmp;
  1311. /* Offset */
  1312. if (*spos + 2 > end) {
  1313. wpa_printf(MSG_DEBUG, "P2P: No room for full "
  1314. "DNS offset field");
  1315. return -1;
  1316. }
  1317. offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
  1318. if (offset >= *spos - start) {
  1319. wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
  1320. "pointer offset %u", offset);
  1321. return -1;
  1322. }
  1323. (*spos) += 2;
  1324. spos_tmp = start + offset;
  1325. return p2p_sd_dns_uncompress_label(upos, uend, start,
  1326. &spos_tmp,
  1327. *spos - 2);
  1328. }
  1329. /* Label */
  1330. len = (*spos)[0] & 0x3f;
  1331. if (len == 0)
  1332. return 0;
  1333. (*spos)++;
  1334. if (*spos + len > end) {
  1335. wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
  1336. "sequence - no room for label with length "
  1337. "%u", len);
  1338. return -1;
  1339. }
  1340. if (*upos + len + 2 > uend)
  1341. return -2;
  1342. os_memcpy(*upos, *spos, len);
  1343. *spos += len;
  1344. *upos += len;
  1345. (*upos)[0] = '.';
  1346. (*upos)++;
  1347. (*upos)[0] = '\0';
  1348. }
  1349. return 0;
  1350. }
  1351. /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
  1352. * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
  1353. * not large enough */
  1354. static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
  1355. size_t msg_len, size_t offset)
  1356. {
  1357. /* 27-octet in-memory packet from P2P specification */
  1358. const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
  1359. "\x04_udp\xC0\x11\x00\x0C\x00\x01";
  1360. u8 *tmp, *end, *spos;
  1361. char *upos, *uend;
  1362. int ret = 0;
  1363. if (buf_len < 2)
  1364. return -1;
  1365. if (offset > msg_len)
  1366. return -1;
  1367. tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
  1368. if (tmp == NULL)
  1369. return -1;
  1370. spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
  1371. end = spos + msg_len;
  1372. spos += offset;
  1373. os_memset(tmp, 0, DNS_HEADER_LEN);
  1374. os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
  1375. os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
  1376. upos = buf;
  1377. uend = buf + buf_len;
  1378. ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
  1379. if (ret) {
  1380. os_free(tmp);
  1381. return ret;
  1382. }
  1383. if (upos == buf) {
  1384. upos[0] = '.';
  1385. upos[1] = '\0';
  1386. } else if (upos[-1] == '.')
  1387. upos[-1] = '\0';
  1388. os_free(tmp);
  1389. return 0;
  1390. }
  1391. static struct p2p_srv_bonjour *
  1392. wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
  1393. const struct wpabuf *query)
  1394. {
  1395. struct p2p_srv_bonjour *bsrv;
  1396. size_t len;
  1397. len = wpabuf_len(query);
  1398. dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
  1399. struct p2p_srv_bonjour, list) {
  1400. if (len == wpabuf_len(bsrv->query) &&
  1401. os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
  1402. len) == 0)
  1403. return bsrv;
  1404. }
  1405. return NULL;
  1406. }
  1407. static struct p2p_srv_upnp *
  1408. wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
  1409. const char *service)
  1410. {
  1411. struct p2p_srv_upnp *usrv;
  1412. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  1413. struct p2p_srv_upnp, list) {
  1414. if (version == usrv->version &&
  1415. os_strcmp(service, usrv->service) == 0)
  1416. return usrv;
  1417. }
  1418. return NULL;
  1419. }
  1420. static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
  1421. u8 srv_trans_id)
  1422. {
  1423. u8 *len_pos;
  1424. if (wpabuf_tailroom(resp) < 5)
  1425. return;
  1426. /* Length (to be filled) */
  1427. len_pos = wpabuf_put(resp, 2);
  1428. wpabuf_put_u8(resp, srv_proto);
  1429. wpabuf_put_u8(resp, srv_trans_id);
  1430. /* Status Code */
  1431. wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
  1432. /* Response Data: empty */
  1433. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1434. }
  1435. static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
  1436. struct wpabuf *resp, u8 srv_trans_id)
  1437. {
  1438. struct p2p_srv_bonjour *bsrv;
  1439. u8 *len_pos;
  1440. wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
  1441. if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  1442. wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
  1443. return;
  1444. }
  1445. dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
  1446. struct p2p_srv_bonjour, list) {
  1447. if (wpabuf_tailroom(resp) <
  1448. 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
  1449. return;
  1450. /* Length (to be filled) */
  1451. len_pos = wpabuf_put(resp, 2);
  1452. wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
  1453. wpabuf_put_u8(resp, srv_trans_id);
  1454. /* Status Code */
  1455. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1456. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
  1457. wpabuf_head(bsrv->resp),
  1458. wpabuf_len(bsrv->resp));
  1459. /* Response Data */
  1460. wpabuf_put_buf(resp, bsrv->query); /* Key */
  1461. wpabuf_put_buf(resp, bsrv->resp); /* Value */
  1462. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  1463. 2);
  1464. }
  1465. }
  1466. static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
  1467. size_t query_len)
  1468. {
  1469. char str_rx[256], str_srv[256];
  1470. if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
  1471. return 0; /* Too short to include DNS Type and Version */
  1472. if (os_memcmp(query + query_len - 3,
  1473. wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
  1474. 3) != 0)
  1475. return 0; /* Mismatch in DNS Type or Version */
  1476. if (query_len == wpabuf_len(bsrv->query) &&
  1477. os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
  1478. return 1; /* Binary match */
  1479. if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
  1480. 0))
  1481. return 0; /* Failed to uncompress query */
  1482. if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
  1483. wpabuf_head(bsrv->query),
  1484. wpabuf_len(bsrv->query) - 3, 0))
  1485. return 0; /* Failed to uncompress service */
  1486. return os_strcmp(str_rx, str_srv) == 0;
  1487. }
  1488. static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
  1489. struct wpabuf *resp, u8 srv_trans_id,
  1490. const u8 *query, size_t query_len)
  1491. {
  1492. struct p2p_srv_bonjour *bsrv;
  1493. u8 *len_pos;
  1494. int matches = 0;
  1495. wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
  1496. query, query_len);
  1497. if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  1498. wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
  1499. wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
  1500. srv_trans_id);
  1501. return;
  1502. }
  1503. if (query_len == 0) {
  1504. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  1505. return;
  1506. }
  1507. dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
  1508. struct p2p_srv_bonjour, list) {
  1509. if (!match_bonjour_query(bsrv, query, query_len))
  1510. continue;
  1511. if (wpabuf_tailroom(resp) <
  1512. 5 + query_len + wpabuf_len(bsrv->resp))
  1513. return;
  1514. matches++;
  1515. /* Length (to be filled) */
  1516. len_pos = wpabuf_put(resp, 2);
  1517. wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
  1518. wpabuf_put_u8(resp, srv_trans_id);
  1519. /* Status Code */
  1520. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1521. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
  1522. wpabuf_head(bsrv->resp),
  1523. wpabuf_len(bsrv->resp));
  1524. /* Response Data */
  1525. wpabuf_put_data(resp, query, query_len); /* Key */
  1526. wpabuf_put_buf(resp, bsrv->resp); /* Value */
  1527. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1528. }
  1529. if (matches == 0) {
  1530. wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
  1531. "available");
  1532. if (wpabuf_tailroom(resp) < 5)
  1533. return;
  1534. /* Length (to be filled) */
  1535. len_pos = wpabuf_put(resp, 2);
  1536. wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
  1537. wpabuf_put_u8(resp, srv_trans_id);
  1538. /* Status Code */
  1539. wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
  1540. /* Response Data: empty */
  1541. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  1542. 2);
  1543. }
  1544. }
  1545. static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
  1546. struct wpabuf *resp, u8 srv_trans_id)
  1547. {
  1548. struct p2p_srv_upnp *usrv;
  1549. u8 *len_pos;
  1550. wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
  1551. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
  1552. wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
  1553. return;
  1554. }
  1555. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  1556. struct p2p_srv_upnp, list) {
  1557. if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
  1558. return;
  1559. /* Length (to be filled) */
  1560. len_pos = wpabuf_put(resp, 2);
  1561. wpabuf_put_u8(resp, P2P_SERV_UPNP);
  1562. wpabuf_put_u8(resp, srv_trans_id);
  1563. /* Status Code */
  1564. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1565. /* Response Data */
  1566. wpabuf_put_u8(resp, usrv->version);
  1567. wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
  1568. usrv->service);
  1569. wpabuf_put_str(resp, usrv->service);
  1570. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  1571. 2);
  1572. }
  1573. }
  1574. static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
  1575. struct wpabuf *resp, u8 srv_trans_id,
  1576. const u8 *query, size_t query_len)
  1577. {
  1578. struct p2p_srv_upnp *usrv;
  1579. u8 *len_pos;
  1580. u8 version;
  1581. char *str;
  1582. int count = 0;
  1583. wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
  1584. query, query_len);
  1585. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
  1586. wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
  1587. wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
  1588. srv_trans_id);
  1589. return;
  1590. }
  1591. if (query_len == 0) {
  1592. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  1593. return;
  1594. }
  1595. if (wpabuf_tailroom(resp) < 5)
  1596. return;
  1597. /* Length (to be filled) */
  1598. len_pos = wpabuf_put(resp, 2);
  1599. wpabuf_put_u8(resp, P2P_SERV_UPNP);
  1600. wpabuf_put_u8(resp, srv_trans_id);
  1601. version = query[0];
  1602. str = os_malloc(query_len);
  1603. if (str == NULL)
  1604. return;
  1605. os_memcpy(str, query + 1, query_len - 1);
  1606. str[query_len - 1] = '\0';
  1607. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  1608. struct p2p_srv_upnp, list) {
  1609. if (version != usrv->version)
  1610. continue;
  1611. if (os_strcmp(str, "ssdp:all") != 0 &&
  1612. os_strstr(usrv->service, str) == NULL)
  1613. continue;
  1614. if (wpabuf_tailroom(resp) < 2)
  1615. break;
  1616. if (count == 0) {
  1617. /* Status Code */
  1618. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1619. /* Response Data */
  1620. wpabuf_put_u8(resp, version);
  1621. } else
  1622. wpabuf_put_u8(resp, ',');
  1623. count++;
  1624. wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
  1625. usrv->service);
  1626. if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
  1627. break;
  1628. wpabuf_put_str(resp, usrv->service);
  1629. }
  1630. os_free(str);
  1631. if (count == 0) {
  1632. wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
  1633. "available");
  1634. /* Status Code */
  1635. wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
  1636. /* Response Data: empty */
  1637. }
  1638. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1639. }
  1640. #ifdef CONFIG_WIFI_DISPLAY
  1641. static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
  1642. struct wpabuf *resp, u8 srv_trans_id,
  1643. const u8 *query, size_t query_len)
  1644. {
  1645. const u8 *pos;
  1646. u8 role;
  1647. u8 *len_pos;
  1648. wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
  1649. if (!wpa_s->global->wifi_display) {
  1650. wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
  1651. wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
  1652. srv_trans_id);
  1653. return;
  1654. }
  1655. if (query_len < 1) {
  1656. wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
  1657. "Role");
  1658. return;
  1659. }
  1660. if (wpabuf_tailroom(resp) < 5)
  1661. return;
  1662. pos = query;
  1663. role = *pos++;
  1664. wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
  1665. /* TODO: role specific handling */
  1666. /* Length (to be filled) */
  1667. len_pos = wpabuf_put(resp, 2);
  1668. wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
  1669. wpabuf_put_u8(resp, srv_trans_id);
  1670. wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
  1671. while (pos < query + query_len) {
  1672. if (*pos < MAX_WFD_SUBELEMS &&
  1673. wpa_s->global->wfd_subelem[*pos] &&
  1674. wpabuf_tailroom(resp) >=
  1675. wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
  1676. wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
  1677. "subelement %u", *pos);
  1678. wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
  1679. }
  1680. pos++;
  1681. }
  1682. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1683. }
  1684. #endif /* CONFIG_WIFI_DISPLAY */
  1685. void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
  1686. u16 update_indic, const u8 *tlvs, size_t tlvs_len)
  1687. {
  1688. struct wpa_supplicant *wpa_s = ctx;
  1689. const u8 *pos = tlvs;
  1690. const u8 *end = tlvs + tlvs_len;
  1691. const u8 *tlv_end;
  1692. u16 slen;
  1693. struct wpabuf *resp;
  1694. u8 srv_proto, srv_trans_id;
  1695. size_t buf_len;
  1696. char *buf;
  1697. wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
  1698. tlvs, tlvs_len);
  1699. buf_len = 2 * tlvs_len + 1;
  1700. buf = os_malloc(buf_len);
  1701. if (buf) {
  1702. wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
  1703. wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
  1704. MACSTR " %u %u %s",
  1705. freq, MAC2STR(sa), dialog_token, update_indic,
  1706. buf);
  1707. os_free(buf);
  1708. }
  1709. if (wpa_s->p2p_sd_over_ctrl_iface) {
  1710. wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
  1711. update_indic, tlvs, tlvs_len);
  1712. return; /* to be processed by an external program */
  1713. }
  1714. resp = wpabuf_alloc(10000);
  1715. if (resp == NULL)
  1716. return;
  1717. while (pos + 1 < end) {
  1718. wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
  1719. slen = WPA_GET_LE16(pos);
  1720. pos += 2;
  1721. if (pos + slen > end || slen < 2) {
  1722. wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
  1723. "length");
  1724. wpabuf_free(resp);
  1725. return;
  1726. }
  1727. tlv_end = pos + slen;
  1728. srv_proto = *pos++;
  1729. wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
  1730. srv_proto);
  1731. srv_trans_id = *pos++;
  1732. wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
  1733. srv_trans_id);
  1734. wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
  1735. pos, tlv_end - pos);
  1736. if (wpa_s->force_long_sd) {
  1737. wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
  1738. "response");
  1739. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  1740. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  1741. goto done;
  1742. }
  1743. switch (srv_proto) {
  1744. case P2P_SERV_ALL_SERVICES:
  1745. wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
  1746. "for all services");
  1747. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
  1748. dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  1749. wpa_printf(MSG_DEBUG, "P2P: No service "
  1750. "discovery protocols available");
  1751. wpas_sd_add_proto_not_avail(
  1752. resp, P2P_SERV_ALL_SERVICES,
  1753. srv_trans_id);
  1754. break;
  1755. }
  1756. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  1757. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  1758. break;
  1759. case P2P_SERV_BONJOUR:
  1760. wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
  1761. pos, tlv_end - pos);
  1762. break;
  1763. case P2P_SERV_UPNP:
  1764. wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
  1765. pos, tlv_end - pos);
  1766. break;
  1767. #ifdef CONFIG_WIFI_DISPLAY
  1768. case P2P_SERV_WIFI_DISPLAY:
  1769. wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
  1770. pos, tlv_end - pos);
  1771. break;
  1772. #endif /* CONFIG_WIFI_DISPLAY */
  1773. default:
  1774. wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
  1775. "protocol %u", srv_proto);
  1776. wpas_sd_add_proto_not_avail(resp, srv_proto,
  1777. srv_trans_id);
  1778. break;
  1779. }
  1780. pos = tlv_end;
  1781. }
  1782. done:
  1783. wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
  1784. update_indic, tlvs, tlvs_len);
  1785. wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
  1786. wpabuf_free(resp);
  1787. }
  1788. void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
  1789. const u8 *tlvs, size_t tlvs_len)
  1790. {
  1791. struct wpa_supplicant *wpa_s = ctx;
  1792. const u8 *pos = tlvs;
  1793. const u8 *end = tlvs + tlvs_len;
  1794. const u8 *tlv_end;
  1795. u16 slen;
  1796. size_t buf_len;
  1797. char *buf;
  1798. wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
  1799. tlvs, tlvs_len);
  1800. if (tlvs_len > 1500) {
  1801. /* TODO: better way for handling this */
  1802. wpa_msg_ctrl(wpa_s, MSG_INFO,
  1803. P2P_EVENT_SERV_DISC_RESP MACSTR
  1804. " %u <long response: %u bytes>",
  1805. MAC2STR(sa), update_indic,
  1806. (unsigned int) tlvs_len);
  1807. } else {
  1808. buf_len = 2 * tlvs_len + 1;
  1809. buf = os_malloc(buf_len);
  1810. if (buf) {
  1811. wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
  1812. wpa_msg_ctrl(wpa_s, MSG_INFO,
  1813. P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
  1814. MAC2STR(sa), update_indic, buf);
  1815. os_free(buf);
  1816. }
  1817. }
  1818. while (pos < end) {
  1819. u8 srv_proto, srv_trans_id, status;
  1820. wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
  1821. slen = WPA_GET_LE16(pos);
  1822. pos += 2;
  1823. if (pos + slen > end || slen < 3) {
  1824. wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
  1825. "length");
  1826. return;
  1827. }
  1828. tlv_end = pos + slen;
  1829. srv_proto = *pos++;
  1830. wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
  1831. srv_proto);
  1832. srv_trans_id = *pos++;
  1833. wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
  1834. srv_trans_id);
  1835. status = *pos++;
  1836. wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
  1837. status);
  1838. wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
  1839. pos, tlv_end - pos);
  1840. pos = tlv_end;
  1841. }
  1842. wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
  1843. }
  1844. u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
  1845. const struct wpabuf *tlvs)
  1846. {
  1847. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  1848. return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
  1849. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  1850. return 0;
  1851. return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
  1852. }
  1853. u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
  1854. u8 version, const char *query)
  1855. {
  1856. struct wpabuf *tlvs;
  1857. u64 ret;
  1858. tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
  1859. if (tlvs == NULL)
  1860. return 0;
  1861. wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
  1862. wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
  1863. wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
  1864. wpabuf_put_u8(tlvs, version);
  1865. wpabuf_put_str(tlvs, query);
  1866. ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
  1867. wpabuf_free(tlvs);
  1868. return ret;
  1869. }
  1870. #ifdef CONFIG_WIFI_DISPLAY
  1871. static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
  1872. const struct wpabuf *tlvs)
  1873. {
  1874. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  1875. return 0;
  1876. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  1877. return 0;
  1878. return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
  1879. }
  1880. #define MAX_WFD_SD_SUBELEMS 20
  1881. static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
  1882. const char *subelems)
  1883. {
  1884. u8 *len;
  1885. const char *pos;
  1886. int val;
  1887. int count = 0;
  1888. len = wpabuf_put(tlvs, 2);
  1889. wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
  1890. wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
  1891. wpabuf_put_u8(tlvs, role);
  1892. pos = subelems;
  1893. while (*pos) {
  1894. val = atoi(pos);
  1895. if (val >= 0 && val < 256) {
  1896. wpabuf_put_u8(tlvs, val);
  1897. count++;
  1898. if (count == MAX_WFD_SD_SUBELEMS)
  1899. break;
  1900. }
  1901. pos = os_strchr(pos + 1, ',');
  1902. if (pos == NULL)
  1903. break;
  1904. pos++;
  1905. }
  1906. WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
  1907. }
  1908. u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
  1909. const u8 *dst, const char *role)
  1910. {
  1911. struct wpabuf *tlvs;
  1912. u64 ret;
  1913. const char *subelems;
  1914. u8 id = 1;
  1915. subelems = os_strchr(role, ' ');
  1916. if (subelems == NULL)
  1917. return 0;
  1918. subelems++;
  1919. tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
  1920. if (tlvs == NULL)
  1921. return 0;
  1922. if (os_strstr(role, "[source]"))
  1923. wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
  1924. if (os_strstr(role, "[pri-sink]"))
  1925. wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
  1926. if (os_strstr(role, "[sec-sink]"))
  1927. wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
  1928. if (os_strstr(role, "[source+sink]"))
  1929. wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
  1930. ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
  1931. wpabuf_free(tlvs);
  1932. return ret;
  1933. }
  1934. #endif /* CONFIG_WIFI_DISPLAY */
  1935. int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
  1936. {
  1937. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  1938. return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
  1939. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  1940. return -1;
  1941. return p2p_sd_cancel_request(wpa_s->global->p2p,
  1942. (void *) (uintptr_t) req);
  1943. }
  1944. void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
  1945. const u8 *dst, u8 dialog_token,
  1946. const struct wpabuf *resp_tlvs)
  1947. {
  1948. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  1949. wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
  1950. resp_tlvs);
  1951. return;
  1952. }
  1953. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  1954. return;
  1955. p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
  1956. resp_tlvs);
  1957. }
  1958. void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
  1959. {
  1960. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  1961. wpa_drv_p2p_service_update(wpa_s);
  1962. return;
  1963. }
  1964. if (wpa_s->global->p2p)
  1965. p2p_sd_service_update(wpa_s->global->p2p);
  1966. }
  1967. static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
  1968. {
  1969. dl_list_del(&bsrv->list);
  1970. wpabuf_free(bsrv->query);
  1971. wpabuf_free(bsrv->resp);
  1972. os_free(bsrv);
  1973. }
  1974. static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
  1975. {
  1976. dl_list_del(&usrv->list);
  1977. os_free(usrv->service);
  1978. os_free(usrv);
  1979. }
  1980. void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
  1981. {
  1982. struct p2p_srv_bonjour *bsrv, *bn;
  1983. struct p2p_srv_upnp *usrv, *un;
  1984. dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
  1985. struct p2p_srv_bonjour, list)
  1986. wpas_p2p_srv_bonjour_free(bsrv);
  1987. dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
  1988. struct p2p_srv_upnp, list)
  1989. wpas_p2p_srv_upnp_free(usrv);
  1990. wpas_p2p_sd_service_update(wpa_s);
  1991. }
  1992. int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
  1993. struct wpabuf *query, struct wpabuf *resp)
  1994. {
  1995. struct p2p_srv_bonjour *bsrv;
  1996. bsrv = os_zalloc(sizeof(*bsrv));
  1997. if (bsrv == NULL)
  1998. return -1;
  1999. bsrv->query = query;
  2000. bsrv->resp = resp;
  2001. dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
  2002. wpas_p2p_sd_service_update(wpa_s);
  2003. return 0;
  2004. }
  2005. int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
  2006. const struct wpabuf *query)
  2007. {
  2008. struct p2p_srv_bonjour *bsrv;
  2009. bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
  2010. if (bsrv == NULL)
  2011. return -1;
  2012. wpas_p2p_srv_bonjour_free(bsrv);
  2013. wpas_p2p_sd_service_update(wpa_s);
  2014. return 0;
  2015. }
  2016. int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
  2017. const char *service)
  2018. {
  2019. struct p2p_srv_upnp *usrv;
  2020. if (wpas_p2p_service_get_upnp(wpa_s, version, service))
  2021. return 0; /* Already listed */
  2022. usrv = os_zalloc(sizeof(*usrv));
  2023. if (usrv == NULL)
  2024. return -1;
  2025. usrv->version = version;
  2026. usrv->service = os_strdup(service);
  2027. if (usrv->service == NULL) {
  2028. os_free(usrv);
  2029. return -1;
  2030. }
  2031. dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
  2032. wpas_p2p_sd_service_update(wpa_s);
  2033. return 0;
  2034. }
  2035. int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
  2036. const char *service)
  2037. {
  2038. struct p2p_srv_upnp *usrv;
  2039. usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
  2040. if (usrv == NULL)
  2041. return -1;
  2042. wpas_p2p_srv_upnp_free(usrv);
  2043. wpas_p2p_sd_service_update(wpa_s);
  2044. return 0;
  2045. }
  2046. static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
  2047. const u8 *peer, const char *params,
  2048. unsigned int generated_pin)
  2049. {
  2050. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
  2051. " %08d%s", MAC2STR(peer), generated_pin, params);
  2052. }
  2053. static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
  2054. const u8 *peer, const char *params)
  2055. {
  2056. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
  2057. "%s", MAC2STR(peer), params);
  2058. }
  2059. void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
  2060. const u8 *dev_addr, const u8 *pri_dev_type,
  2061. const char *dev_name, u16 supp_config_methods,
  2062. u8 dev_capab, u8 group_capab, const u8 *group_id,
  2063. size_t group_id_len)
  2064. {
  2065. struct wpa_supplicant *wpa_s = ctx;
  2066. char devtype[WPS_DEV_TYPE_BUFSIZE];
  2067. char params[300];
  2068. u8 empty_dev_type[8];
  2069. unsigned int generated_pin = 0;
  2070. struct wpa_supplicant *group = NULL;
  2071. if (group_id) {
  2072. for (group = wpa_s->global->ifaces; group; group = group->next)
  2073. {
  2074. struct wpa_ssid *s = group->current_ssid;
  2075. if (s != NULL &&
  2076. s->mode == WPAS_MODE_P2P_GO &&
  2077. group_id_len - ETH_ALEN == s->ssid_len &&
  2078. os_memcmp(group_id + ETH_ALEN, s->ssid,
  2079. s->ssid_len) == 0)
  2080. break;
  2081. }
  2082. }
  2083. if (pri_dev_type == NULL) {
  2084. os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
  2085. pri_dev_type = empty_dev_type;
  2086. }
  2087. os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
  2088. " pri_dev_type=%s name='%s' config_methods=0x%x "
  2089. "dev_capab=0x%x group_capab=0x%x%s%s",
  2090. MAC2STR(dev_addr),
  2091. wps_dev_type_bin2str(pri_dev_type, devtype,
  2092. sizeof(devtype)),
  2093. dev_name, supp_config_methods, dev_capab, group_capab,
  2094. group ? " group=" : "",
  2095. group ? group->ifname : "");
  2096. params[sizeof(params) - 1] = '\0';
  2097. if (config_methods & WPS_CONFIG_DISPLAY) {
  2098. generated_pin = wps_generate_pin();
  2099. wpas_prov_disc_local_display(wpa_s, peer, params,
  2100. generated_pin);
  2101. } else if (config_methods & WPS_CONFIG_KEYPAD)
  2102. wpas_prov_disc_local_keypad(wpa_s, peer, params);
  2103. else if (config_methods & WPS_CONFIG_PUSHBUTTON)
  2104. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
  2105. MACSTR "%s", MAC2STR(peer), params);
  2106. wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
  2107. P2P_PROV_DISC_SUCCESS,
  2108. config_methods, generated_pin);
  2109. }
  2110. void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
  2111. {
  2112. struct wpa_supplicant *wpa_s = ctx;
  2113. unsigned int generated_pin = 0;
  2114. char params[20];
  2115. if (wpa_s->pending_pd_before_join &&
  2116. (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
  2117. os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
  2118. wpa_s->pending_pd_before_join = 0;
  2119. wpa_printf(MSG_DEBUG, "P2P: Starting pending "
  2120. "join-existing-group operation");
  2121. wpas_p2p_join_start(wpa_s);
  2122. return;
  2123. }
  2124. if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
  2125. wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
  2126. os_snprintf(params, sizeof(params), " peer_go=%d",
  2127. wpa_s->pending_pd_use == AUTO_PD_JOIN);
  2128. else
  2129. params[0] = '\0';
  2130. if (config_methods & WPS_CONFIG_DISPLAY)
  2131. wpas_prov_disc_local_keypad(wpa_s, peer, params);
  2132. else if (config_methods & WPS_CONFIG_KEYPAD) {
  2133. generated_pin = wps_generate_pin();
  2134. wpas_prov_disc_local_display(wpa_s, peer, params,
  2135. generated_pin);
  2136. } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
  2137. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
  2138. MACSTR "%s", MAC2STR(peer), params);
  2139. wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
  2140. P2P_PROV_DISC_SUCCESS,
  2141. config_methods, generated_pin);
  2142. }
  2143. static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
  2144. enum p2p_prov_disc_status status)
  2145. {
  2146. struct wpa_supplicant *wpa_s = ctx;
  2147. if (wpa_s->p2p_fallback_to_go_neg) {
  2148. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
  2149. "failed - fall back to GO Negotiation");
  2150. wpas_p2p_fallback_to_go_neg(wpa_s, 0);
  2151. return;
  2152. }
  2153. if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
  2154. wpa_s->pending_pd_before_join = 0;
  2155. wpa_printf(MSG_DEBUG, "P2P: Starting pending "
  2156. "join-existing-group operation (no ACK for PD "
  2157. "Req attempts)");
  2158. wpas_p2p_join_start(wpa_s);
  2159. return;
  2160. }
  2161. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
  2162. " p2p_dev_addr=" MACSTR " status=%d",
  2163. MAC2STR(peer), status);
  2164. wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
  2165. status, 0, 0);
  2166. }
  2167. static int freq_included(const struct p2p_channels *channels, unsigned int freq)
  2168. {
  2169. if (channels == NULL)
  2170. return 1; /* Assume no restrictions */
  2171. return p2p_channels_includes_freq(channels, freq);
  2172. }
  2173. static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
  2174. const u8 *go_dev_addr, const u8 *ssid,
  2175. size_t ssid_len, int *go, u8 *group_bssid,
  2176. int *force_freq, int persistent_group,
  2177. const struct p2p_channels *channels)
  2178. {
  2179. struct wpa_supplicant *wpa_s = ctx;
  2180. struct wpa_ssid *s;
  2181. int res;
  2182. struct wpa_supplicant *grp;
  2183. if (!persistent_group) {
  2184. wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
  2185. " to join an active group", MAC2STR(sa));
  2186. if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
  2187. (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
  2188. == 0 ||
  2189. os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
  2190. wpa_printf(MSG_DEBUG, "P2P: Accept previously "
  2191. "authorized invitation");
  2192. goto accept_inv;
  2193. }
  2194. /*
  2195. * Do not accept the invitation automatically; notify user and
  2196. * request approval.
  2197. */
  2198. return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  2199. }
  2200. grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
  2201. if (grp) {
  2202. wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
  2203. "running persistent group");
  2204. if (*go)
  2205. os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
  2206. goto accept_inv;
  2207. }
  2208. if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
  2209. os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
  2210. wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
  2211. "invitation to re-invoke a persistent group");
  2212. } else if (!wpa_s->conf->persistent_reconnect)
  2213. return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  2214. for (s = wpa_s->conf->ssid; s; s = s->next) {
  2215. if (s->disabled == 2 &&
  2216. os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
  2217. s->ssid_len == ssid_len &&
  2218. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  2219. break;
  2220. }
  2221. if (!s) {
  2222. wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
  2223. " requested reinvocation of an unknown group",
  2224. MAC2STR(sa));
  2225. return P2P_SC_FAIL_UNKNOWN_GROUP;
  2226. }
  2227. if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
  2228. *go = 1;
  2229. if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
  2230. wpa_printf(MSG_DEBUG, "P2P: The only available "
  2231. "interface is already in use - reject "
  2232. "invitation");
  2233. return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
  2234. }
  2235. os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
  2236. } else if (s->mode == WPAS_MODE_P2P_GO) {
  2237. *go = 1;
  2238. if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
  2239. {
  2240. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  2241. "interface address for the group");
  2242. return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
  2243. }
  2244. os_memcpy(group_bssid, wpa_s->pending_interface_addr,
  2245. ETH_ALEN);
  2246. }
  2247. accept_inv:
  2248. wpas_p2p_set_own_freq_preference(wpa_s, 0);
  2249. /* Get one of the frequencies currently in use */
  2250. if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) {
  2251. wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match a channel already used by one of the interfaces");
  2252. *force_freq = res;
  2253. wpas_p2p_set_own_freq_preference(wpa_s, res);
  2254. }
  2255. if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
  2256. wpas_p2p_num_unused_channels(wpa_s) > 0) {
  2257. if (*go == 0) {
  2258. /* We are the client */
  2259. wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
  2260. "running a GO but we are capable of MCC, "
  2261. "figure out the best channel to use");
  2262. *force_freq = 0;
  2263. } else if (!freq_included(channels, *force_freq)) {
  2264. /* We are the GO, and *force_freq is not in the
  2265. * intersection */
  2266. wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
  2267. "in intersection but we are capable of MCC, "
  2268. "figure out the best channel to use",
  2269. *force_freq);
  2270. *force_freq = 0;
  2271. }
  2272. }
  2273. return P2P_SC_SUCCESS;
  2274. }
  2275. static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
  2276. const u8 *ssid, size_t ssid_len,
  2277. const u8 *go_dev_addr, u8 status,
  2278. int op_freq)
  2279. {
  2280. struct wpa_supplicant *wpa_s = ctx;
  2281. struct wpa_ssid *s;
  2282. for (s = wpa_s->conf->ssid; s; s = s->next) {
  2283. if (s->disabled == 2 &&
  2284. s->ssid_len == ssid_len &&
  2285. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  2286. break;
  2287. }
  2288. if (status == P2P_SC_SUCCESS) {
  2289. wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
  2290. " was accepted; op_freq=%d MHz",
  2291. MAC2STR(sa), op_freq);
  2292. if (s) {
  2293. int go = s->mode == WPAS_MODE_P2P_GO;
  2294. wpas_p2p_group_add_persistent(
  2295. wpa_s, s, go, go ? op_freq : 0, 0, NULL,
  2296. go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
  2297. } else if (bssid) {
  2298. wpa_s->user_initiated_pd = 0;
  2299. wpas_p2p_join(wpa_s, bssid, go_dev_addr,
  2300. wpa_s->p2p_wps_method, 0);
  2301. }
  2302. return;
  2303. }
  2304. if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
  2305. wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
  2306. " was rejected (status %u)", MAC2STR(sa), status);
  2307. return;
  2308. }
  2309. if (!s) {
  2310. if (bssid) {
  2311. wpa_msg_global(wpa_s, MSG_INFO,
  2312. P2P_EVENT_INVITATION_RECEIVED
  2313. "sa=" MACSTR " go_dev_addr=" MACSTR
  2314. " bssid=" MACSTR " unknown-network",
  2315. MAC2STR(sa), MAC2STR(go_dev_addr),
  2316. MAC2STR(bssid));
  2317. } else {
  2318. wpa_msg_global(wpa_s, MSG_INFO,
  2319. P2P_EVENT_INVITATION_RECEIVED
  2320. "sa=" MACSTR " go_dev_addr=" MACSTR
  2321. " unknown-network",
  2322. MAC2STR(sa), MAC2STR(go_dev_addr));
  2323. }
  2324. return;
  2325. }
  2326. if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
  2327. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
  2328. "sa=" MACSTR " persistent=%d freq=%d",
  2329. MAC2STR(sa), s->id, op_freq);
  2330. } else {
  2331. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
  2332. "sa=" MACSTR " persistent=%d",
  2333. MAC2STR(sa), s->id);
  2334. }
  2335. }
  2336. static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
  2337. struct wpa_ssid *ssid,
  2338. const u8 *peer, int inv)
  2339. {
  2340. size_t i;
  2341. if (ssid == NULL)
  2342. return;
  2343. for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
  2344. if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
  2345. ETH_ALEN) == 0)
  2346. break;
  2347. }
  2348. if (i >= ssid->num_p2p_clients) {
  2349. if (ssid->mode != WPAS_MODE_P2P_GO &&
  2350. os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
  2351. wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
  2352. "due to invitation result", ssid->id);
  2353. wpas_notify_network_removed(wpa_s, ssid);
  2354. wpa_config_remove_network(wpa_s->conf, ssid->id);
  2355. return;
  2356. }
  2357. return; /* Peer not found in client list */
  2358. }
  2359. wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
  2360. "group %d client list%s",
  2361. MAC2STR(peer), ssid->id,
  2362. inv ? " due to invitation result" : "");
  2363. os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
  2364. ssid->p2p_client_list + (i + 1) * ETH_ALEN,
  2365. (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
  2366. ssid->num_p2p_clients--;
  2367. #ifndef CONFIG_NO_CONFIG_WRITE
  2368. if (wpa_s->parent->conf->update_config &&
  2369. wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
  2370. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  2371. #endif /* CONFIG_NO_CONFIG_WRITE */
  2372. }
  2373. static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
  2374. const u8 *peer)
  2375. {
  2376. struct wpa_ssid *ssid;
  2377. wpa_s = wpa_s->global->p2p_invite_group;
  2378. if (wpa_s == NULL)
  2379. return; /* No known invitation group */
  2380. ssid = wpa_s->current_ssid;
  2381. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  2382. !ssid->p2p_persistent_group)
  2383. return; /* Not operating as a GO in persistent group */
  2384. ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
  2385. ssid->ssid, ssid->ssid_len);
  2386. wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
  2387. }
  2388. static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
  2389. const struct p2p_channels *channels,
  2390. const u8 *peer)
  2391. {
  2392. struct wpa_supplicant *wpa_s = ctx;
  2393. struct wpa_ssid *ssid;
  2394. if (bssid) {
  2395. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
  2396. "status=%d " MACSTR,
  2397. status, MAC2STR(bssid));
  2398. } else {
  2399. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
  2400. "status=%d ", status);
  2401. }
  2402. wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
  2403. wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
  2404. status, MAC2STR(peer));
  2405. if (wpa_s->pending_invite_ssid_id == -1) {
  2406. if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
  2407. wpas_remove_persistent_client(wpa_s, peer);
  2408. return; /* Invitation to active group */
  2409. }
  2410. if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
  2411. wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
  2412. "invitation exchange to indicate readiness for "
  2413. "re-invocation");
  2414. }
  2415. if (status != P2P_SC_SUCCESS) {
  2416. if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
  2417. ssid = wpa_config_get_network(
  2418. wpa_s->conf, wpa_s->pending_invite_ssid_id);
  2419. wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
  2420. }
  2421. wpas_p2p_remove_pending_group_interface(wpa_s);
  2422. return;
  2423. }
  2424. ssid = wpa_config_get_network(wpa_s->conf,
  2425. wpa_s->pending_invite_ssid_id);
  2426. if (ssid == NULL) {
  2427. wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
  2428. "data matching with invitation");
  2429. return;
  2430. }
  2431. /*
  2432. * The peer could have missed our ctrl::ack frame for Invitation
  2433. * Response and continue retransmitting the frame. To reduce the
  2434. * likelihood of the peer not getting successful TX status for the
  2435. * Invitation Response frame, wait a short time here before starting
  2436. * the persistent group so that we will remain on the current channel to
  2437. * acknowledge any possible retransmission from the peer.
  2438. */
  2439. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
  2440. "starting persistent group");
  2441. os_sleep(0, 50000);
  2442. wpas_p2p_group_add_persistent(wpa_s, ssid,
  2443. ssid->mode == WPAS_MODE_P2P_GO,
  2444. wpa_s->p2p_persistent_go_freq,
  2445. wpa_s->p2p_go_ht40, channels,
  2446. ssid->mode == WPAS_MODE_P2P_GO ?
  2447. P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
  2448. 0);
  2449. }
  2450. static int wpas_p2p_disallowed_freq(struct wpa_global *global,
  2451. unsigned int freq)
  2452. {
  2453. unsigned int i;
  2454. if (global->p2p_disallow_freq == NULL)
  2455. return 0;
  2456. for (i = 0; i < global->num_p2p_disallow_freq; i++) {
  2457. if (freq >= global->p2p_disallow_freq[i].min &&
  2458. freq <= global->p2p_disallow_freq[i].max)
  2459. return 1;
  2460. }
  2461. return 0;
  2462. }
  2463. static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
  2464. {
  2465. reg->channel[reg->channels] = chan;
  2466. reg->channels++;
  2467. }
  2468. static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
  2469. struct p2p_channels *chan)
  2470. {
  2471. int i, cla = 0;
  2472. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
  2473. "band");
  2474. /* Operating class 81 - 2.4 GHz band channels 1..13 */
  2475. chan->reg_class[cla].reg_class = 81;
  2476. chan->reg_class[cla].channels = 0;
  2477. for (i = 0; i < 11; i++) {
  2478. if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
  2479. wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
  2480. }
  2481. if (chan->reg_class[cla].channels)
  2482. cla++;
  2483. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
  2484. "band");
  2485. /* Operating class 115 - 5 GHz, channels 36-48 */
  2486. chan->reg_class[cla].reg_class = 115;
  2487. chan->reg_class[cla].channels = 0;
  2488. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
  2489. wpas_p2p_add_chan(&chan->reg_class[cla], 36);
  2490. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
  2491. wpas_p2p_add_chan(&chan->reg_class[cla], 40);
  2492. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
  2493. wpas_p2p_add_chan(&chan->reg_class[cla], 44);
  2494. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
  2495. wpas_p2p_add_chan(&chan->reg_class[cla], 48);
  2496. if (chan->reg_class[cla].channels)
  2497. cla++;
  2498. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
  2499. "band");
  2500. /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
  2501. chan->reg_class[cla].reg_class = 124;
  2502. chan->reg_class[cla].channels = 0;
  2503. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
  2504. wpas_p2p_add_chan(&chan->reg_class[cla], 149);
  2505. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
  2506. wpas_p2p_add_chan(&chan->reg_class[cla], 153);
  2507. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
  2508. wpas_p2p_add_chan(&chan->reg_class[cla], 157);
  2509. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
  2510. wpas_p2p_add_chan(&chan->reg_class[cla], 161);
  2511. if (chan->reg_class[cla].channels)
  2512. cla++;
  2513. chan->reg_classes = cla;
  2514. return 0;
  2515. }
  2516. static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
  2517. u16 num_modes,
  2518. enum hostapd_hw_mode mode)
  2519. {
  2520. u16 i;
  2521. for (i = 0; i < num_modes; i++) {
  2522. if (modes[i].mode == mode)
  2523. return &modes[i];
  2524. }
  2525. return NULL;
  2526. }
  2527. static int has_channel(struct wpa_global *global,
  2528. struct hostapd_hw_modes *mode, u8 chan, int *flags)
  2529. {
  2530. int i;
  2531. unsigned int freq;
  2532. freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
  2533. chan * 5;
  2534. if (wpas_p2p_disallowed_freq(global, freq))
  2535. return 0;
  2536. for (i = 0; i < mode->num_channels; i++) {
  2537. if (mode->channels[i].chan == chan) {
  2538. if (flags)
  2539. *flags = mode->channels[i].flag;
  2540. return !(mode->channels[i].flag &
  2541. (HOSTAPD_CHAN_DISABLED |
  2542. HOSTAPD_CHAN_PASSIVE_SCAN |
  2543. HOSTAPD_CHAN_NO_IBSS |
  2544. HOSTAPD_CHAN_RADAR));
  2545. }
  2546. }
  2547. return 0;
  2548. }
  2549. struct p2p_oper_class_map {
  2550. enum hostapd_hw_mode mode;
  2551. u8 op_class;
  2552. u8 min_chan;
  2553. u8 max_chan;
  2554. u8 inc;
  2555. enum { BW20, BW40PLUS, BW40MINUS } bw;
  2556. };
  2557. static struct p2p_oper_class_map op_class[] = {
  2558. { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
  2559. #if 0 /* Do not enable HT40 on 2 GHz for now */
  2560. { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
  2561. { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
  2562. #endif
  2563. { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
  2564. { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
  2565. { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
  2566. { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
  2567. { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
  2568. { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
  2569. { -1, 0, 0, 0, 0, BW20 }
  2570. };
  2571. static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
  2572. struct hostapd_hw_modes *mode,
  2573. u8 channel, u8 bw)
  2574. {
  2575. int flag;
  2576. if (!has_channel(wpa_s->global, mode, channel, &flag))
  2577. return -1;
  2578. if (bw == BW40MINUS &&
  2579. (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
  2580. !has_channel(wpa_s->global, mode, channel - 4, NULL)))
  2581. return 0;
  2582. if (bw == BW40PLUS &&
  2583. (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
  2584. !has_channel(wpa_s->global, mode, channel + 4, NULL)))
  2585. return 0;
  2586. return 1;
  2587. }
  2588. static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
  2589. struct p2p_channels *chan)
  2590. {
  2591. struct hostapd_hw_modes *mode;
  2592. int cla, op;
  2593. if (wpa_s->hw.modes == NULL) {
  2594. wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
  2595. "of all supported channels; assume dualband "
  2596. "support");
  2597. return wpas_p2p_default_channels(wpa_s, chan);
  2598. }
  2599. cla = 0;
  2600. for (op = 0; op_class[op].op_class; op++) {
  2601. struct p2p_oper_class_map *o = &op_class[op];
  2602. u8 ch;
  2603. struct p2p_reg_class *reg = NULL;
  2604. mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
  2605. if (mode == NULL)
  2606. continue;
  2607. for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
  2608. if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
  2609. continue;
  2610. if (reg == NULL) {
  2611. wpa_printf(MSG_DEBUG, "P2P: Add operating "
  2612. "class %u", o->op_class);
  2613. reg = &chan->reg_class[cla];
  2614. cla++;
  2615. reg->reg_class = o->op_class;
  2616. }
  2617. reg->channel[reg->channels] = ch;
  2618. reg->channels++;
  2619. }
  2620. if (reg) {
  2621. wpa_hexdump(MSG_DEBUG, "P2P: Channels",
  2622. reg->channel, reg->channels);
  2623. }
  2624. }
  2625. chan->reg_classes = cla;
  2626. return 0;
  2627. }
  2628. int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
  2629. struct hostapd_hw_modes *mode, u8 channel)
  2630. {
  2631. int op, ret;
  2632. for (op = 0; op_class[op].op_class; op++) {
  2633. struct p2p_oper_class_map *o = &op_class[op];
  2634. u8 ch;
  2635. for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
  2636. if (o->mode != HOSTAPD_MODE_IEEE80211A ||
  2637. o->bw == BW20 || ch != channel)
  2638. continue;
  2639. ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
  2640. if (ret < 0)
  2641. continue;
  2642. else if (ret > 0)
  2643. return (o->bw == BW40MINUS) ? -1 : 1;
  2644. else
  2645. return 0;
  2646. }
  2647. }
  2648. return 0;
  2649. }
  2650. static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
  2651. size_t buf_len)
  2652. {
  2653. struct wpa_supplicant *wpa_s = ctx;
  2654. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2655. if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
  2656. break;
  2657. }
  2658. if (wpa_s == NULL)
  2659. return -1;
  2660. return wpa_drv_get_noa(wpa_s, buf, buf_len);
  2661. }
  2662. static int wpas_go_connected(void *ctx, const u8 *dev_addr)
  2663. {
  2664. struct wpa_supplicant *wpa_s = ctx;
  2665. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2666. struct wpa_ssid *ssid = wpa_s->current_ssid;
  2667. if (ssid == NULL)
  2668. continue;
  2669. if (ssid->mode != WPAS_MODE_INFRA)
  2670. continue;
  2671. if (wpa_s->wpa_state != WPA_COMPLETED &&
  2672. wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
  2673. continue;
  2674. if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
  2675. return 1;
  2676. }
  2677. return 0;
  2678. }
  2679. static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
  2680. {
  2681. struct wpa_supplicant *wpa_s = ctx;
  2682. wpa_msg_global(wpa_s, level, "P2P: %s", msg);
  2683. }
  2684. int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
  2685. {
  2686. struct wpa_interface iface;
  2687. struct wpa_supplicant *p2pdev_wpa_s;
  2688. char ifname[100];
  2689. char force_name[100];
  2690. int ret;
  2691. os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
  2692. wpa_s->ifname);
  2693. force_name[0] = '\0';
  2694. wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
  2695. ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
  2696. force_name, wpa_s->pending_interface_addr, NULL);
  2697. if (ret < 0) {
  2698. wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
  2699. return ret;
  2700. }
  2701. os_strlcpy(wpa_s->pending_interface_name, ifname,
  2702. sizeof(wpa_s->pending_interface_name));
  2703. os_memset(&iface, 0, sizeof(iface));
  2704. iface.p2p_mgmt = 1;
  2705. iface.ifname = wpa_s->pending_interface_name;
  2706. iface.driver = wpa_s->driver->name;
  2707. iface.driver_param = wpa_s->conf->driver_param;
  2708. iface.confname = wpa_s->confname;
  2709. p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
  2710. if (!p2pdev_wpa_s) {
  2711. wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
  2712. return -1;
  2713. }
  2714. p2pdev_wpa_s->parent = wpa_s;
  2715. wpa_s->pending_interface_name[0] = '\0';
  2716. return 0;
  2717. }
  2718. /**
  2719. * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
  2720. * @global: Pointer to global data from wpa_supplicant_init()
  2721. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2722. * Returns: 0 on success, -1 on failure
  2723. */
  2724. int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
  2725. {
  2726. struct p2p_config p2p;
  2727. unsigned int r;
  2728. int i;
  2729. if (wpa_s->conf->p2p_disabled)
  2730. return 0;
  2731. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
  2732. return 0;
  2733. if (global->p2p)
  2734. return 0;
  2735. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  2736. struct p2p_params params;
  2737. wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
  2738. os_memset(&params, 0, sizeof(params));
  2739. params.dev_name = wpa_s->conf->device_name;
  2740. os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
  2741. WPS_DEV_TYPE_LEN);
  2742. params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
  2743. os_memcpy(params.sec_dev_type,
  2744. wpa_s->conf->sec_device_type,
  2745. params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
  2746. if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
  2747. return -1;
  2748. return 0;
  2749. }
  2750. os_memset(&p2p, 0, sizeof(p2p));
  2751. p2p.cb_ctx = wpa_s;
  2752. p2p.debug_print = wpas_p2p_debug_print;
  2753. p2p.p2p_scan = wpas_p2p_scan;
  2754. p2p.send_action = wpas_send_action;
  2755. p2p.send_action_done = wpas_send_action_done;
  2756. p2p.go_neg_completed = wpas_go_neg_completed;
  2757. p2p.go_neg_req_rx = wpas_go_neg_req_rx;
  2758. p2p.dev_found = wpas_dev_found;
  2759. p2p.dev_lost = wpas_dev_lost;
  2760. p2p.find_stopped = wpas_find_stopped;
  2761. p2p.start_listen = wpas_start_listen;
  2762. p2p.stop_listen = wpas_stop_listen;
  2763. p2p.send_probe_resp = wpas_send_probe_resp;
  2764. p2p.sd_request = wpas_sd_request;
  2765. p2p.sd_response = wpas_sd_response;
  2766. p2p.prov_disc_req = wpas_prov_disc_req;
  2767. p2p.prov_disc_resp = wpas_prov_disc_resp;
  2768. p2p.prov_disc_fail = wpas_prov_disc_fail;
  2769. p2p.invitation_process = wpas_invitation_process;
  2770. p2p.invitation_received = wpas_invitation_received;
  2771. p2p.invitation_result = wpas_invitation_result;
  2772. p2p.get_noa = wpas_get_noa;
  2773. p2p.go_connected = wpas_go_connected;
  2774. os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
  2775. os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
  2776. p2p.dev_name = wpa_s->conf->device_name;
  2777. p2p.manufacturer = wpa_s->conf->manufacturer;
  2778. p2p.model_name = wpa_s->conf->model_name;
  2779. p2p.model_number = wpa_s->conf->model_number;
  2780. p2p.serial_number = wpa_s->conf->serial_number;
  2781. if (wpa_s->wps) {
  2782. os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
  2783. p2p.config_methods = wpa_s->wps->config_methods;
  2784. }
  2785. if (wpa_s->conf->p2p_listen_reg_class &&
  2786. wpa_s->conf->p2p_listen_channel) {
  2787. p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
  2788. p2p.channel = wpa_s->conf->p2p_listen_channel;
  2789. } else {
  2790. p2p.reg_class = 81;
  2791. /*
  2792. * Pick one of the social channels randomly as the listen
  2793. * channel.
  2794. */
  2795. os_get_random((u8 *) &r, sizeof(r));
  2796. p2p.channel = 1 + (r % 3) * 5;
  2797. }
  2798. wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
  2799. if (wpa_s->conf->p2p_oper_reg_class &&
  2800. wpa_s->conf->p2p_oper_channel) {
  2801. p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
  2802. p2p.op_channel = wpa_s->conf->p2p_oper_channel;
  2803. p2p.cfg_op_channel = 1;
  2804. wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
  2805. "%d:%d", p2p.op_reg_class, p2p.op_channel);
  2806. } else {
  2807. p2p.op_reg_class = 81;
  2808. /*
  2809. * Use random operation channel from (1, 6, 11) if no other
  2810. * preference is indicated.
  2811. */
  2812. os_get_random((u8 *) &r, sizeof(r));
  2813. p2p.op_channel = 1 + (r % 3) * 5;
  2814. p2p.cfg_op_channel = 0;
  2815. wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
  2816. "%d:%d", p2p.op_reg_class, p2p.op_channel);
  2817. }
  2818. if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
  2819. p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
  2820. p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
  2821. }
  2822. if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  2823. os_memcpy(p2p.country, wpa_s->conf->country, 2);
  2824. p2p.country[2] = 0x04;
  2825. } else
  2826. os_memcpy(p2p.country, "XX\x04", 3);
  2827. if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
  2828. wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
  2829. "channel list");
  2830. return -1;
  2831. }
  2832. os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
  2833. WPS_DEV_TYPE_LEN);
  2834. p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
  2835. os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
  2836. p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
  2837. p2p.concurrent_operations = !!(wpa_s->drv_flags &
  2838. WPA_DRIVER_FLAGS_P2P_CONCURRENT);
  2839. p2p.max_peers = 100;
  2840. if (wpa_s->conf->p2p_ssid_postfix) {
  2841. p2p.ssid_postfix_len =
  2842. os_strlen(wpa_s->conf->p2p_ssid_postfix);
  2843. if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
  2844. p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
  2845. os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
  2846. p2p.ssid_postfix_len);
  2847. }
  2848. p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
  2849. p2p.max_listen = wpa_s->max_remain_on_chan;
  2850. global->p2p = p2p_init(&p2p);
  2851. if (global->p2p == NULL)
  2852. return -1;
  2853. global->p2p_init_wpa_s = wpa_s;
  2854. for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
  2855. if (wpa_s->conf->wps_vendor_ext[i] == NULL)
  2856. continue;
  2857. p2p_add_wps_vendor_extension(
  2858. global->p2p, wpa_s->conf->wps_vendor_ext[i]);
  2859. }
  2860. return 0;
  2861. }
  2862. /**
  2863. * wpas_p2p_deinit - Deinitialize per-interface P2P data
  2864. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2865. *
  2866. * This function deinitialize per-interface P2P data.
  2867. */
  2868. void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
  2869. {
  2870. if (wpa_s->driver && wpa_s->drv_priv)
  2871. wpa_drv_probe_req_report(wpa_s, 0);
  2872. if (wpa_s->go_params) {
  2873. /* Clear any stored provisioning info */
  2874. p2p_clear_provisioning_info(
  2875. wpa_s->global->p2p,
  2876. wpa_s->go_params->peer_device_addr);
  2877. }
  2878. os_free(wpa_s->go_params);
  2879. wpa_s->go_params = NULL;
  2880. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  2881. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2882. wpa_s->p2p_long_listen = 0;
  2883. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  2884. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  2885. wpas_p2p_remove_pending_group_interface(wpa_s);
  2886. /* TODO: remove group interface from the driver if this wpa_s instance
  2887. * is on top of a P2P group interface */
  2888. }
  2889. /**
  2890. * wpas_p2p_deinit_global - Deinitialize global P2P module
  2891. * @global: Pointer to global data from wpa_supplicant_init()
  2892. *
  2893. * This function deinitializes the global (per device) P2P module.
  2894. */
  2895. void wpas_p2p_deinit_global(struct wpa_global *global)
  2896. {
  2897. struct wpa_supplicant *wpa_s, *tmp;
  2898. wpa_s = global->ifaces;
  2899. if (wpa_s)
  2900. wpas_p2p_service_flush(wpa_s);
  2901. if (global->p2p == NULL)
  2902. return;
  2903. /* Remove remaining P2P group interfaces */
  2904. while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
  2905. wpa_s = wpa_s->next;
  2906. while (wpa_s) {
  2907. tmp = global->ifaces;
  2908. while (tmp &&
  2909. (tmp == wpa_s ||
  2910. tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
  2911. tmp = tmp->next;
  2912. }
  2913. if (tmp == NULL)
  2914. break;
  2915. /* Disconnect from the P2P group and deinit the interface */
  2916. wpas_p2p_disconnect(tmp);
  2917. }
  2918. /*
  2919. * Deinit GO data on any possibly remaining interface (if main
  2920. * interface is used as GO).
  2921. */
  2922. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2923. if (wpa_s->ap_iface)
  2924. wpas_p2p_group_deinit(wpa_s);
  2925. }
  2926. p2p_deinit(global->p2p);
  2927. global->p2p = NULL;
  2928. global->p2p_init_wpa_s = NULL;
  2929. }
  2930. static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
  2931. {
  2932. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
  2933. wpa_s->conf->p2p_no_group_iface)
  2934. return 0; /* separate interface disabled per configuration */
  2935. if (wpa_s->drv_flags &
  2936. (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
  2937. WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
  2938. return 1; /* P2P group requires a new interface in every case
  2939. */
  2940. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
  2941. return 0; /* driver does not support concurrent operations */
  2942. if (wpa_s->global->ifaces->next)
  2943. return 1; /* more that one interface already in use */
  2944. if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
  2945. return 1; /* this interface is already in use */
  2946. return 0;
  2947. }
  2948. static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
  2949. const u8 *peer_addr,
  2950. enum p2p_wps_method wps_method,
  2951. int go_intent, const u8 *own_interface_addr,
  2952. unsigned int force_freq, int persistent_group,
  2953. struct wpa_ssid *ssid, unsigned int pref_freq)
  2954. {
  2955. if (persistent_group && wpa_s->conf->persistent_reconnect)
  2956. persistent_group = 2;
  2957. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  2958. return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
  2959. go_intent, own_interface_addr,
  2960. force_freq, persistent_group);
  2961. }
  2962. /*
  2963. * Increase GO config timeout if HT40 is used since it takes some time
  2964. * to scan channels for coex purposes before the BSS can be started.
  2965. */
  2966. p2p_set_config_timeout(wpa_s->global->p2p,
  2967. wpa_s->p2p_go_ht40 ? 255 : 100, 20);
  2968. return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
  2969. go_intent, own_interface_addr, force_freq,
  2970. persistent_group, ssid ? ssid->ssid : NULL,
  2971. ssid ? ssid->ssid_len : 0,
  2972. wpa_s->p2p_pd_before_go_neg, pref_freq);
  2973. }
  2974. static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
  2975. const u8 *peer_addr,
  2976. enum p2p_wps_method wps_method,
  2977. int go_intent, const u8 *own_interface_addr,
  2978. unsigned int force_freq, int persistent_group,
  2979. struct wpa_ssid *ssid, unsigned int pref_freq)
  2980. {
  2981. if (persistent_group && wpa_s->conf->persistent_reconnect)
  2982. persistent_group = 2;
  2983. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  2984. return -1;
  2985. return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
  2986. go_intent, own_interface_addr, force_freq,
  2987. persistent_group, ssid ? ssid->ssid : NULL,
  2988. ssid ? ssid->ssid_len : 0, pref_freq);
  2989. }
  2990. static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
  2991. {
  2992. wpa_s->p2p_join_scan_count++;
  2993. wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
  2994. wpa_s->p2p_join_scan_count);
  2995. if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
  2996. wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
  2997. " for join operationg - stop join attempt",
  2998. MAC2STR(wpa_s->pending_join_iface_addr));
  2999. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3000. if (wpa_s->p2p_auto_pd) {
  3001. wpa_s->p2p_auto_pd = 0;
  3002. wpa_msg_global(wpa_s, MSG_INFO,
  3003. P2P_EVENT_PROV_DISC_FAILURE
  3004. " p2p_dev_addr=" MACSTR " status=N/A",
  3005. MAC2STR(wpa_s->pending_join_dev_addr));
  3006. return;
  3007. }
  3008. wpa_msg_global(wpa_s->parent, MSG_INFO,
  3009. P2P_EVENT_GROUP_FORMATION_FAILURE);
  3010. }
  3011. }
  3012. static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
  3013. {
  3014. int *freqs, res, num, i;
  3015. if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
  3016. /* Multiple channels are supported and not all are in use */
  3017. return 0;
  3018. }
  3019. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  3020. if (!freqs)
  3021. return 1;
  3022. num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
  3023. wpa_s->num_multichan_concurrent);
  3024. if (num < 0) {
  3025. res = 1;
  3026. goto exit_free;
  3027. }
  3028. for (i = 0; i < num; i++) {
  3029. if (freqs[i] == freq) {
  3030. wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
  3031. freq);
  3032. res = 0;
  3033. goto exit_free;
  3034. }
  3035. }
  3036. res = 1;
  3037. exit_free:
  3038. os_free(freqs);
  3039. return res;
  3040. }
  3041. static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
  3042. const u8 *peer_dev_addr)
  3043. {
  3044. struct wpa_bss *bss;
  3045. int updated;
  3046. bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
  3047. if (bss == NULL)
  3048. return -1;
  3049. if (bss->last_update_idx < wpa_s->bss_update_idx) {
  3050. wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
  3051. "last scan");
  3052. return 0;
  3053. }
  3054. updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
  3055. wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
  3056. "%ld.%06ld (%supdated in last scan)",
  3057. bss->last_update.sec, bss->last_update.usec,
  3058. updated ? "": "not ");
  3059. return updated;
  3060. }
  3061. static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
  3062. struct wpa_scan_results *scan_res)
  3063. {
  3064. struct wpa_bss *bss;
  3065. int freq;
  3066. u8 iface_addr[ETH_ALEN];
  3067. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3068. if (wpa_s->global->p2p_disabled)
  3069. return;
  3070. wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
  3071. scan_res ? (int) scan_res->num : -1,
  3072. wpa_s->p2p_auto_join ? "auto_" : "");
  3073. if (scan_res)
  3074. wpas_p2p_scan_res_handler(wpa_s, scan_res);
  3075. if (wpa_s->p2p_auto_pd) {
  3076. int join = wpas_p2p_peer_go(wpa_s,
  3077. wpa_s->pending_join_dev_addr);
  3078. if (join == 0 &&
  3079. wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
  3080. wpa_s->auto_pd_scan_retry++;
  3081. bss = wpa_bss_get_bssid_latest(
  3082. wpa_s, wpa_s->pending_join_dev_addr);
  3083. if (bss) {
  3084. freq = bss->freq;
  3085. wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
  3086. "the peer " MACSTR " at %d MHz",
  3087. wpa_s->auto_pd_scan_retry,
  3088. MAC2STR(wpa_s->
  3089. pending_join_dev_addr),
  3090. freq);
  3091. wpas_p2p_join_scan_req(wpa_s, freq);
  3092. return;
  3093. }
  3094. }
  3095. if (join < 0)
  3096. join = 0;
  3097. wpa_s->p2p_auto_pd = 0;
  3098. wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
  3099. wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
  3100. MAC2STR(wpa_s->pending_join_dev_addr), join);
  3101. if (p2p_prov_disc_req(wpa_s->global->p2p,
  3102. wpa_s->pending_join_dev_addr,
  3103. wpa_s->pending_pd_config_methods, join,
  3104. 0, wpa_s->user_initiated_pd) < 0) {
  3105. wpa_s->p2p_auto_pd = 0;
  3106. wpa_msg_global(wpa_s, MSG_INFO,
  3107. P2P_EVENT_PROV_DISC_FAILURE
  3108. " p2p_dev_addr=" MACSTR " status=N/A",
  3109. MAC2STR(wpa_s->pending_join_dev_addr));
  3110. }
  3111. return;
  3112. }
  3113. if (wpa_s->p2p_auto_join) {
  3114. int join = wpas_p2p_peer_go(wpa_s,
  3115. wpa_s->pending_join_dev_addr);
  3116. if (join < 0) {
  3117. wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
  3118. "running a GO -> use GO Negotiation");
  3119. wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
  3120. wpa_s->p2p_pin, wpa_s->p2p_wps_method,
  3121. wpa_s->p2p_persistent_group, 0, 0, 0,
  3122. wpa_s->p2p_go_intent,
  3123. wpa_s->p2p_connect_freq,
  3124. wpa_s->p2p_persistent_id,
  3125. wpa_s->p2p_pd_before_go_neg,
  3126. wpa_s->p2p_go_ht40);
  3127. return;
  3128. }
  3129. wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
  3130. "try to join the group", join ? "" :
  3131. " in older scan");
  3132. if (!join)
  3133. wpa_s->p2p_fallback_to_go_neg = 1;
  3134. }
  3135. freq = p2p_get_oper_freq(wpa_s->global->p2p,
  3136. wpa_s->pending_join_iface_addr);
  3137. if (freq < 0 &&
  3138. p2p_get_interface_addr(wpa_s->global->p2p,
  3139. wpa_s->pending_join_dev_addr,
  3140. iface_addr) == 0 &&
  3141. os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
  3142. {
  3143. wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
  3144. "address for join from " MACSTR " to " MACSTR
  3145. " based on newly discovered P2P peer entry",
  3146. MAC2STR(wpa_s->pending_join_iface_addr),
  3147. MAC2STR(iface_addr));
  3148. os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
  3149. ETH_ALEN);
  3150. freq = p2p_get_oper_freq(wpa_s->global->p2p,
  3151. wpa_s->pending_join_iface_addr);
  3152. }
  3153. if (freq >= 0) {
  3154. wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
  3155. "from P2P peer table: %d MHz", freq);
  3156. }
  3157. bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
  3158. if (bss) {
  3159. freq = bss->freq;
  3160. wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
  3161. "from BSS table: %d MHz", freq);
  3162. }
  3163. if (freq > 0) {
  3164. u16 method;
  3165. if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
  3166. wpa_msg_global(wpa_s->parent, MSG_INFO,
  3167. P2P_EVENT_GROUP_FORMATION_FAILURE
  3168. "reason=FREQ_CONFLICT");
  3169. return;
  3170. }
  3171. wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
  3172. "prior to joining an existing group (GO " MACSTR
  3173. " freq=%u MHz)",
  3174. MAC2STR(wpa_s->pending_join_dev_addr), freq);
  3175. wpa_s->pending_pd_before_join = 1;
  3176. switch (wpa_s->pending_join_wps_method) {
  3177. case WPS_PIN_DISPLAY:
  3178. method = WPS_CONFIG_KEYPAD;
  3179. break;
  3180. case WPS_PIN_KEYPAD:
  3181. method = WPS_CONFIG_DISPLAY;
  3182. break;
  3183. case WPS_PBC:
  3184. method = WPS_CONFIG_PUSHBUTTON;
  3185. break;
  3186. default:
  3187. method = 0;
  3188. break;
  3189. }
  3190. if ((p2p_get_provisioning_info(wpa_s->global->p2p,
  3191. wpa_s->pending_join_dev_addr) ==
  3192. method)) {
  3193. /*
  3194. * We have already performed provision discovery for
  3195. * joining the group. Proceed directly to join
  3196. * operation without duplicated provision discovery. */
  3197. wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
  3198. "with " MACSTR " already done - proceed to "
  3199. "join",
  3200. MAC2STR(wpa_s->pending_join_dev_addr));
  3201. wpa_s->pending_pd_before_join = 0;
  3202. goto start;
  3203. }
  3204. if (p2p_prov_disc_req(wpa_s->global->p2p,
  3205. wpa_s->pending_join_dev_addr, method, 1,
  3206. freq, wpa_s->user_initiated_pd) < 0) {
  3207. wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
  3208. "Discovery Request before joining an "
  3209. "existing group");
  3210. wpa_s->pending_pd_before_join = 0;
  3211. goto start;
  3212. }
  3213. return;
  3214. }
  3215. wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
  3216. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3217. eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
  3218. wpas_p2p_check_join_scan_limit(wpa_s);
  3219. return;
  3220. start:
  3221. /* Start join operation immediately */
  3222. wpas_p2p_join_start(wpa_s);
  3223. }
  3224. static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
  3225. {
  3226. int ret;
  3227. struct wpa_driver_scan_params params;
  3228. struct wpabuf *wps_ie, *ies;
  3229. size_t ielen;
  3230. int freqs[2] = { 0, 0 };
  3231. os_memset(&params, 0, sizeof(params));
  3232. /* P2P Wildcard SSID */
  3233. params.num_ssids = 1;
  3234. params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
  3235. params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
  3236. wpa_s->wps->dev.p2p = 1;
  3237. wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
  3238. wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
  3239. NULL);
  3240. if (wps_ie == NULL) {
  3241. wpas_p2p_scan_res_join(wpa_s, NULL);
  3242. return;
  3243. }
  3244. ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
  3245. ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
  3246. if (ies == NULL) {
  3247. wpabuf_free(wps_ie);
  3248. wpas_p2p_scan_res_join(wpa_s, NULL);
  3249. return;
  3250. }
  3251. wpabuf_put_buf(ies, wps_ie);
  3252. wpabuf_free(wps_ie);
  3253. p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
  3254. params.p2p_probe = 1;
  3255. params.extra_ies = wpabuf_head(ies);
  3256. params.extra_ies_len = wpabuf_len(ies);
  3257. if (freq > 0) {
  3258. freqs[0] = freq;
  3259. params.freqs = freqs;
  3260. }
  3261. /*
  3262. * Run a scan to update BSS table and start Provision Discovery once
  3263. * the new scan results become available.
  3264. */
  3265. ret = wpa_drv_scan(wpa_s, &params);
  3266. if (!ret) {
  3267. os_get_time(&wpa_s->scan_trigger_time);
  3268. wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
  3269. }
  3270. wpabuf_free(ies);
  3271. if (ret) {
  3272. wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
  3273. "try again later");
  3274. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3275. eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
  3276. wpas_p2p_check_join_scan_limit(wpa_s);
  3277. }
  3278. }
  3279. static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
  3280. {
  3281. struct wpa_supplicant *wpa_s = eloop_ctx;
  3282. wpas_p2p_join_scan_req(wpa_s, 0);
  3283. }
  3284. static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
  3285. const u8 *dev_addr, enum p2p_wps_method wps_method,
  3286. int auto_join)
  3287. {
  3288. wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
  3289. MACSTR " dev " MACSTR ")%s",
  3290. MAC2STR(iface_addr), MAC2STR(dev_addr),
  3291. auto_join ? " (auto_join)" : "");
  3292. wpa_s->p2p_auto_pd = 0;
  3293. wpa_s->p2p_auto_join = !!auto_join;
  3294. os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
  3295. os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
  3296. wpa_s->pending_join_wps_method = wps_method;
  3297. /* Make sure we are not running find during connection establishment */
  3298. wpas_p2p_stop_find(wpa_s);
  3299. wpa_s->p2p_join_scan_count = 0;
  3300. wpas_p2p_join_scan(wpa_s, NULL);
  3301. return 0;
  3302. }
  3303. static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
  3304. {
  3305. struct wpa_supplicant *group;
  3306. struct p2p_go_neg_results res;
  3307. struct wpa_bss *bss;
  3308. group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
  3309. if (group == NULL)
  3310. return -1;
  3311. if (group != wpa_s) {
  3312. os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
  3313. sizeof(group->p2p_pin));
  3314. group->p2p_wps_method = wpa_s->p2p_wps_method;
  3315. } else {
  3316. /*
  3317. * Need to mark the current interface for p2p_group_formation
  3318. * when a separate group interface is not used. This is needed
  3319. * to allow p2p_cancel stop a pending p2p_connect-join.
  3320. * wpas_p2p_init_group_interface() addresses this for the case
  3321. * where a separate group interface is used.
  3322. */
  3323. wpa_s->global->p2p_group_formation = wpa_s;
  3324. }
  3325. group->p2p_in_provisioning = 1;
  3326. group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
  3327. os_memset(&res, 0, sizeof(res));
  3328. os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
  3329. ETH_ALEN);
  3330. res.wps_method = wpa_s->pending_join_wps_method;
  3331. bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
  3332. if (bss) {
  3333. res.freq = bss->freq;
  3334. res.ssid_len = bss->ssid_len;
  3335. os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
  3336. }
  3337. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  3338. wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
  3339. "starting client");
  3340. wpa_drv_cancel_remain_on_channel(wpa_s);
  3341. wpa_s->off_channel_freq = 0;
  3342. wpa_s->roc_waiting_drv_freq = 0;
  3343. }
  3344. wpas_start_wps_enrollee(group, &res);
  3345. /*
  3346. * Allow a longer timeout for join-a-running-group than normal 15
  3347. * second group formation timeout since the GO may not have authorized
  3348. * our connection yet.
  3349. */
  3350. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  3351. eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
  3352. wpa_s, NULL);
  3353. return 0;
  3354. }
  3355. static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
  3356. int *force_freq, int *pref_freq)
  3357. {
  3358. int *freqs, res;
  3359. unsigned int freq_in_use = 0, num, i;
  3360. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  3361. if (!freqs)
  3362. return -1;
  3363. num = get_shared_radio_freqs(wpa_s, freqs,
  3364. wpa_s->num_multichan_concurrent);
  3365. wpa_printf(MSG_DEBUG,
  3366. "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
  3367. freq, wpa_s->num_multichan_concurrent, num);
  3368. if (freq > 0) {
  3369. if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
  3370. wpa_printf(MSG_DEBUG, "P2P: The forced channel "
  3371. "(%u MHz) is not supported for P2P uses",
  3372. freq);
  3373. res = -3;
  3374. goto exit_free;
  3375. }
  3376. for (i = 0; i < num; i++) {
  3377. if (freqs[i] == freq)
  3378. freq_in_use = 1;
  3379. }
  3380. if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
  3381. wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
  3382. freq);
  3383. res = -2;
  3384. goto exit_free;
  3385. }
  3386. wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
  3387. "requested channel (%u MHz)", freq);
  3388. *force_freq = freq;
  3389. goto exit_ok;
  3390. }
  3391. for (i = 0; i < num; i++) {
  3392. if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
  3393. continue;
  3394. wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
  3395. *force_freq);
  3396. *force_freq = freqs[i];
  3397. if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
  3398. wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency we are already using");
  3399. *pref_freq = *force_freq;
  3400. }
  3401. break;
  3402. }
  3403. if (i == num) {
  3404. if (num < wpa_s->num_multichan_concurrent) {
  3405. wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
  3406. *force_freq = 0;
  3407. } else {
  3408. wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
  3409. res = -2;
  3410. goto exit_free;
  3411. }
  3412. }
  3413. exit_ok:
  3414. res = 0;
  3415. exit_free:
  3416. os_free(freqs);
  3417. return res;
  3418. }
  3419. /**
  3420. * wpas_p2p_connect - Request P2P Group Formation to be started
  3421. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3422. * @peer_addr: Address of the peer P2P Device
  3423. * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
  3424. * @persistent_group: Whether to create a persistent group
  3425. * @auto_join: Whether to select join vs. GO Negotiation automatically
  3426. * @join: Whether to join an existing group (as a client) instead of starting
  3427. * Group Owner negotiation; @peer_addr is BSSID in that case
  3428. * @auth: Whether to only authorize the connection instead of doing that and
  3429. * initiating Group Owner negotiation
  3430. * @go_intent: GO Intent or -1 to use default
  3431. * @freq: Frequency for the group or 0 for auto-selection
  3432. * @persistent_id: Persistent group credentials to use for forcing GO
  3433. * parameters or -1 to generate new values (SSID/passphrase)
  3434. * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
  3435. * interoperability workaround when initiating group formation
  3436. * @ht40: Start GO with 40 MHz channel width
  3437. * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
  3438. * failure, -2 on failure due to channel not currently available,
  3439. * -3 if forced channel is not supported
  3440. */
  3441. int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  3442. const char *pin, enum p2p_wps_method wps_method,
  3443. int persistent_group, int auto_join, int join, int auth,
  3444. int go_intent, int freq, int persistent_id, int pd,
  3445. int ht40)
  3446. {
  3447. int force_freq = 0, pref_freq = 0;
  3448. int ret = 0, res;
  3449. enum wpa_driver_if_type iftype;
  3450. const u8 *if_addr;
  3451. struct wpa_ssid *ssid = NULL;
  3452. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3453. return -1;
  3454. if (persistent_id >= 0) {
  3455. ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
  3456. if (ssid == NULL || ssid->disabled != 2 ||
  3457. ssid->mode != WPAS_MODE_P2P_GO)
  3458. return -1;
  3459. }
  3460. os_free(wpa_s->global->add_psk);
  3461. wpa_s->global->add_psk = NULL;
  3462. if (go_intent < 0)
  3463. go_intent = wpa_s->conf->p2p_go_intent;
  3464. if (!auth)
  3465. wpa_s->p2p_long_listen = 0;
  3466. wpa_s->p2p_wps_method = wps_method;
  3467. wpa_s->p2p_persistent_group = !!persistent_group;
  3468. wpa_s->p2p_persistent_id = persistent_id;
  3469. wpa_s->p2p_go_intent = go_intent;
  3470. wpa_s->p2p_connect_freq = freq;
  3471. wpa_s->p2p_fallback_to_go_neg = 0;
  3472. wpa_s->p2p_pd_before_go_neg = !!pd;
  3473. wpa_s->p2p_go_ht40 = !!ht40;
  3474. if (pin)
  3475. os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
  3476. else if (wps_method == WPS_PIN_DISPLAY) {
  3477. ret = wps_generate_pin();
  3478. os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
  3479. ret);
  3480. wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
  3481. wpa_s->p2p_pin);
  3482. } else
  3483. wpa_s->p2p_pin[0] = '\0';
  3484. if (join || auto_join) {
  3485. u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
  3486. if (auth) {
  3487. wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
  3488. "connect a running group from " MACSTR,
  3489. MAC2STR(peer_addr));
  3490. os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
  3491. return ret;
  3492. }
  3493. os_memcpy(dev_addr, peer_addr, ETH_ALEN);
  3494. if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
  3495. iface_addr) < 0) {
  3496. os_memcpy(iface_addr, peer_addr, ETH_ALEN);
  3497. p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
  3498. dev_addr);
  3499. }
  3500. if (auto_join) {
  3501. os_get_time(&wpa_s->p2p_auto_started);
  3502. wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
  3503. "%ld.%06ld",
  3504. wpa_s->p2p_auto_started.sec,
  3505. wpa_s->p2p_auto_started.usec);
  3506. }
  3507. wpa_s->user_initiated_pd = 1;
  3508. if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
  3509. auto_join) < 0)
  3510. return -1;
  3511. return ret;
  3512. }
  3513. res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq);
  3514. if (res)
  3515. return res;
  3516. wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
  3517. wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
  3518. if (wpa_s->create_p2p_iface) {
  3519. /* Prepare to add a new interface for the group */
  3520. iftype = WPA_IF_P2P_GROUP;
  3521. if (go_intent == 15)
  3522. iftype = WPA_IF_P2P_GO;
  3523. if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
  3524. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  3525. "interface for the group");
  3526. return -1;
  3527. }
  3528. if_addr = wpa_s->pending_interface_addr;
  3529. } else
  3530. if_addr = wpa_s->own_addr;
  3531. if (auth) {
  3532. if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
  3533. go_intent, if_addr,
  3534. force_freq, persistent_group, ssid,
  3535. pref_freq) < 0)
  3536. return -1;
  3537. return ret;
  3538. }
  3539. if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
  3540. go_intent, if_addr, force_freq,
  3541. persistent_group, ssid, pref_freq) < 0) {
  3542. if (wpa_s->create_p2p_iface)
  3543. wpas_p2p_remove_pending_group_interface(wpa_s);
  3544. return -1;
  3545. }
  3546. return ret;
  3547. }
  3548. /**
  3549. * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
  3550. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3551. * @freq: Frequency of the channel in MHz
  3552. * @duration: Duration of the stay on the channel in milliseconds
  3553. *
  3554. * This callback is called when the driver indicates that it has started the
  3555. * requested remain-on-channel duration.
  3556. */
  3557. void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  3558. unsigned int freq, unsigned int duration)
  3559. {
  3560. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3561. return;
  3562. if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
  3563. p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
  3564. wpa_s->pending_listen_duration);
  3565. wpa_s->pending_listen_freq = 0;
  3566. }
  3567. }
  3568. static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
  3569. unsigned int timeout)
  3570. {
  3571. /* Limit maximum Listen state time based on driver limitation. */
  3572. if (timeout > wpa_s->max_remain_on_chan)
  3573. timeout = wpa_s->max_remain_on_chan;
  3574. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3575. return wpa_drv_p2p_listen(wpa_s, timeout);
  3576. return p2p_listen(wpa_s->global->p2p, timeout);
  3577. }
  3578. /**
  3579. * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
  3580. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3581. * @freq: Frequency of the channel in MHz
  3582. *
  3583. * This callback is called when the driver indicates that a remain-on-channel
  3584. * operation has been completed, i.e., the duration on the requested channel
  3585. * has timed out.
  3586. */
  3587. void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  3588. unsigned int freq)
  3589. {
  3590. wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
  3591. "(p2p_long_listen=%d ms pending_action_tx=%p)",
  3592. wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
  3593. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3594. return;
  3595. if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
  3596. return; /* P2P module started a new operation */
  3597. if (offchannel_pending_action_tx(wpa_s))
  3598. return;
  3599. if (wpa_s->p2p_long_listen > 0)
  3600. wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
  3601. if (wpa_s->p2p_long_listen > 0) {
  3602. wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
  3603. wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
  3604. } else {
  3605. /*
  3606. * When listen duration is over, stop listen & update p2p_state
  3607. * to IDLE.
  3608. */
  3609. p2p_stop_listen(wpa_s->global->p2p);
  3610. }
  3611. }
  3612. /**
  3613. * wpas_p2p_group_remove - Remove a P2P group
  3614. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3615. * @ifname: Network interface name of the group interface or "*" to remove all
  3616. * groups
  3617. * Returns: 0 on success, -1 on failure
  3618. *
  3619. * This function is used to remove a P2P group. This can be used to disconnect
  3620. * from a group in which the local end is a P2P Client or to end a P2P Group in
  3621. * case the local end is the Group Owner. If a virtual network interface was
  3622. * created for this group, that interface will be removed. Otherwise, only the
  3623. * configured P2P group network will be removed from the interface.
  3624. */
  3625. int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
  3626. {
  3627. struct wpa_global *global = wpa_s->global;
  3628. if (os_strcmp(ifname, "*") == 0) {
  3629. struct wpa_supplicant *prev;
  3630. wpa_s = global->ifaces;
  3631. while (wpa_s) {
  3632. prev = wpa_s;
  3633. wpa_s = wpa_s->next;
  3634. if (prev->p2p_group_interface !=
  3635. NOT_P2P_GROUP_INTERFACE ||
  3636. (prev->current_ssid &&
  3637. prev->current_ssid->p2p_group))
  3638. wpas_p2p_disconnect(prev);
  3639. }
  3640. return 0;
  3641. }
  3642. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3643. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  3644. break;
  3645. }
  3646. return wpas_p2p_disconnect(wpa_s);
  3647. }
  3648. static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
  3649. {
  3650. unsigned int r;
  3651. if (freq == 2) {
  3652. wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
  3653. "band");
  3654. if (wpa_s->best_24_freq > 0 &&
  3655. p2p_supported_freq(wpa_s->global->p2p,
  3656. wpa_s->best_24_freq)) {
  3657. freq = wpa_s->best_24_freq;
  3658. wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
  3659. "channel: %d MHz", freq);
  3660. } else {
  3661. os_get_random((u8 *) &r, sizeof(r));
  3662. freq = 2412 + (r % 3) * 25;
  3663. wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
  3664. "channel: %d MHz", freq);
  3665. }
  3666. }
  3667. if (freq == 5) {
  3668. wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
  3669. "band");
  3670. if (wpa_s->best_5_freq > 0 &&
  3671. p2p_supported_freq(wpa_s->global->p2p,
  3672. wpa_s->best_5_freq)) {
  3673. freq = wpa_s->best_5_freq;
  3674. wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
  3675. "channel: %d MHz", freq);
  3676. } else {
  3677. os_get_random((u8 *) &r, sizeof(r));
  3678. freq = 5180 + (r % 4) * 20;
  3679. if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
  3680. wpa_printf(MSG_DEBUG, "P2P: Could not select "
  3681. "5 GHz channel for P2P group");
  3682. return -1;
  3683. }
  3684. wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
  3685. "channel: %d MHz", freq);
  3686. }
  3687. }
  3688. if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
  3689. wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
  3690. "(%u MHz) is not supported for P2P uses",
  3691. freq);
  3692. return -1;
  3693. }
  3694. return freq;
  3695. }
  3696. static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
  3697. struct p2p_go_neg_results *params,
  3698. int freq, int ht40,
  3699. const struct p2p_channels *channels)
  3700. {
  3701. int res, *freqs;
  3702. unsigned int pref_freq;
  3703. unsigned int num, i;
  3704. os_memset(params, 0, sizeof(*params));
  3705. params->role_go = 1;
  3706. params->ht40 = ht40;
  3707. if (freq) {
  3708. if (!freq_included(channels, freq)) {
  3709. wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
  3710. "accepted", freq);
  3711. return -1;
  3712. }
  3713. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
  3714. "frequency %d MHz", freq);
  3715. params->freq = freq;
  3716. } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
  3717. wpa_s->conf->p2p_oper_channel >= 1 &&
  3718. wpa_s->conf->p2p_oper_channel <= 11 &&
  3719. freq_included(channels,
  3720. 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
  3721. params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
  3722. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
  3723. "frequency %d MHz", params->freq);
  3724. } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
  3725. wpa_s->conf->p2p_oper_reg_class == 116 ||
  3726. wpa_s->conf->p2p_oper_reg_class == 117 ||
  3727. wpa_s->conf->p2p_oper_reg_class == 124 ||
  3728. wpa_s->conf->p2p_oper_reg_class == 126 ||
  3729. wpa_s->conf->p2p_oper_reg_class == 127) &&
  3730. freq_included(channels,
  3731. 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
  3732. params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
  3733. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
  3734. "frequency %d MHz", params->freq);
  3735. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  3736. wpa_s->best_overall_freq > 0 &&
  3737. p2p_supported_freq(wpa_s->global->p2p,
  3738. wpa_s->best_overall_freq) &&
  3739. freq_included(channels, wpa_s->best_overall_freq)) {
  3740. params->freq = wpa_s->best_overall_freq;
  3741. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
  3742. "channel %d MHz", params->freq);
  3743. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  3744. wpa_s->best_24_freq > 0 &&
  3745. p2p_supported_freq(wpa_s->global->p2p,
  3746. wpa_s->best_24_freq) &&
  3747. freq_included(channels, wpa_s->best_24_freq)) {
  3748. params->freq = wpa_s->best_24_freq;
  3749. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
  3750. "channel %d MHz", params->freq);
  3751. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  3752. wpa_s->best_5_freq > 0 &&
  3753. p2p_supported_freq(wpa_s->global->p2p,
  3754. wpa_s->best_5_freq) &&
  3755. freq_included(channels, wpa_s->best_5_freq)) {
  3756. params->freq = wpa_s->best_5_freq;
  3757. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
  3758. "channel %d MHz", params->freq);
  3759. } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
  3760. channels))) {
  3761. params->freq = pref_freq;
  3762. wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
  3763. "channels", params->freq);
  3764. } else {
  3765. int chan;
  3766. for (chan = 0; chan < 11; chan++) {
  3767. params->freq = 2412 + chan * 5;
  3768. if (!wpas_p2p_disallowed_freq(wpa_s->global,
  3769. params->freq) &&
  3770. freq_included(channels, params->freq))
  3771. break;
  3772. }
  3773. if (chan == 11) {
  3774. wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
  3775. "allowed");
  3776. return -1;
  3777. }
  3778. wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
  3779. "known)", params->freq);
  3780. }
  3781. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  3782. if (!freqs)
  3783. return -1;
  3784. res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
  3785. wpa_s->num_multichan_concurrent);
  3786. if (res < 0) {
  3787. os_free(freqs);
  3788. return -1;
  3789. }
  3790. num = res;
  3791. if (!freq) {
  3792. for (i = 0; i < num; i++) {
  3793. if (freq_included(channels, freqs[i])) {
  3794. wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
  3795. freqs[i]);
  3796. params->freq = freqs[i];
  3797. break;
  3798. }
  3799. }
  3800. if (i == num) {
  3801. if (num == wpa_s->num_multichan_concurrent) {
  3802. wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
  3803. os_free(freqs);
  3804. return -1;
  3805. } else {
  3806. wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
  3807. }
  3808. }
  3809. } else {
  3810. for (i = 0; i < num; i++) {
  3811. if (freqs[i] == freq)
  3812. break;
  3813. }
  3814. if (i == num) {
  3815. if (num == wpa_s->num_multichan_concurrent) {
  3816. wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
  3817. os_free(freqs);
  3818. return -1;
  3819. } else {
  3820. wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
  3821. }
  3822. }
  3823. }
  3824. os_free(freqs);
  3825. return 0;
  3826. }
  3827. static struct wpa_supplicant *
  3828. wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
  3829. int go)
  3830. {
  3831. struct wpa_supplicant *group_wpa_s;
  3832. if (!wpas_p2p_create_iface(wpa_s)) {
  3833. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
  3834. "operations");
  3835. wpa_s->p2p_first_connection_timeout = 0;
  3836. return wpa_s;
  3837. }
  3838. if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
  3839. WPA_IF_P2P_CLIENT) < 0) {
  3840. wpa_msg_global(wpa_s, MSG_ERROR,
  3841. "P2P: Failed to add group interface");
  3842. return NULL;
  3843. }
  3844. group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
  3845. if (group_wpa_s == NULL) {
  3846. wpa_msg_global(wpa_s, MSG_ERROR,
  3847. "P2P: Failed to initialize group interface");
  3848. wpas_p2p_remove_pending_group_interface(wpa_s);
  3849. return NULL;
  3850. }
  3851. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
  3852. group_wpa_s->ifname);
  3853. group_wpa_s->p2p_first_connection_timeout = 0;
  3854. return group_wpa_s;
  3855. }
  3856. /**
  3857. * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
  3858. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3859. * @persistent_group: Whether to create a persistent group
  3860. * @freq: Frequency for the group or 0 to indicate no hardcoding
  3861. * Returns: 0 on success, -1 on failure
  3862. *
  3863. * This function creates a new P2P group with the local end as the Group Owner,
  3864. * i.e., without using Group Owner Negotiation.
  3865. */
  3866. int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
  3867. int freq, int ht40)
  3868. {
  3869. struct p2p_go_neg_results params;
  3870. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3871. return -1;
  3872. os_free(wpa_s->global->add_psk);
  3873. wpa_s->global->add_psk = NULL;
  3874. /* Make sure we are not running find during connection establishment */
  3875. wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
  3876. wpas_p2p_stop_find_oper(wpa_s);
  3877. freq = wpas_p2p_select_go_freq(wpa_s, freq);
  3878. if (freq < 0)
  3879. return -1;
  3880. if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, NULL))
  3881. return -1;
  3882. if (params.freq &&
  3883. !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
  3884. wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
  3885. "(%u MHz) is not supported for P2P uses",
  3886. params.freq);
  3887. return -1;
  3888. }
  3889. p2p_go_params(wpa_s->global->p2p, &params);
  3890. params.persistent_group = persistent_group;
  3891. wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
  3892. if (wpa_s == NULL)
  3893. return -1;
  3894. wpas_start_wps_go(wpa_s, &params, 0);
  3895. return 0;
  3896. }
  3897. static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
  3898. struct wpa_ssid *params, int addr_allocated)
  3899. {
  3900. struct wpa_ssid *ssid;
  3901. wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
  3902. if (wpa_s == NULL)
  3903. return -1;
  3904. wpa_s->p2p_last_4way_hs_fail = NULL;
  3905. wpa_supplicant_ap_deinit(wpa_s);
  3906. ssid = wpa_config_add_network(wpa_s->conf);
  3907. if (ssid == NULL)
  3908. return -1;
  3909. wpa_config_set_network_defaults(ssid);
  3910. ssid->temporary = 1;
  3911. ssid->proto = WPA_PROTO_RSN;
  3912. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  3913. ssid->group_cipher = WPA_CIPHER_CCMP;
  3914. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  3915. ssid->ssid = os_malloc(params->ssid_len);
  3916. if (ssid->ssid == NULL) {
  3917. wpa_config_remove_network(wpa_s->conf, ssid->id);
  3918. return -1;
  3919. }
  3920. os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
  3921. ssid->ssid_len = params->ssid_len;
  3922. ssid->p2p_group = 1;
  3923. ssid->export_keys = 1;
  3924. if (params->psk_set) {
  3925. os_memcpy(ssid->psk, params->psk, 32);
  3926. ssid->psk_set = 1;
  3927. }
  3928. if (params->passphrase)
  3929. ssid->passphrase = os_strdup(params->passphrase);
  3930. wpa_supplicant_select_network(wpa_s, ssid);
  3931. wpa_s->show_group_started = 1;
  3932. return 0;
  3933. }
  3934. int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
  3935. struct wpa_ssid *ssid, int addr_allocated,
  3936. int freq, int ht40,
  3937. const struct p2p_channels *channels,
  3938. int connection_timeout)
  3939. {
  3940. struct p2p_go_neg_results params;
  3941. int go = 0;
  3942. if (ssid->disabled != 2 || ssid->ssid == NULL)
  3943. return -1;
  3944. if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
  3945. go == (ssid->mode == WPAS_MODE_P2P_GO)) {
  3946. wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
  3947. "already running");
  3948. return 0;
  3949. }
  3950. os_free(wpa_s->global->add_psk);
  3951. wpa_s->global->add_psk = NULL;
  3952. /* Make sure we are not running find during connection establishment */
  3953. wpas_p2p_stop_find_oper(wpa_s);
  3954. wpa_s->p2p_fallback_to_go_neg = 0;
  3955. if (ssid->mode == WPAS_MODE_INFRA)
  3956. return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
  3957. if (ssid->mode != WPAS_MODE_P2P_GO)
  3958. return -1;
  3959. freq = wpas_p2p_select_go_freq(wpa_s, freq);
  3960. if (freq < 0)
  3961. return -1;
  3962. if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, channels))
  3963. return -1;
  3964. params.role_go = 1;
  3965. params.psk_set = ssid->psk_set;
  3966. if (params.psk_set)
  3967. os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
  3968. if (ssid->passphrase) {
  3969. if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
  3970. wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
  3971. "persistent group");
  3972. return -1;
  3973. }
  3974. os_strlcpy(params.passphrase, ssid->passphrase,
  3975. sizeof(params.passphrase));
  3976. }
  3977. os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
  3978. params.ssid_len = ssid->ssid_len;
  3979. params.persistent_group = 1;
  3980. wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
  3981. if (wpa_s == NULL)
  3982. return -1;
  3983. wpa_s->p2p_first_connection_timeout = connection_timeout;
  3984. wpas_start_wps_go(wpa_s, &params, 0);
  3985. return 0;
  3986. }
  3987. static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
  3988. struct wpabuf *proberesp_ies)
  3989. {
  3990. struct wpa_supplicant *wpa_s = ctx;
  3991. if (wpa_s->ap_iface) {
  3992. struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
  3993. if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
  3994. wpabuf_free(beacon_ies);
  3995. wpabuf_free(proberesp_ies);
  3996. return;
  3997. }
  3998. if (beacon_ies) {
  3999. wpabuf_free(hapd->p2p_beacon_ie);
  4000. hapd->p2p_beacon_ie = beacon_ies;
  4001. }
  4002. wpabuf_free(hapd->p2p_probe_resp_ie);
  4003. hapd->p2p_probe_resp_ie = proberesp_ies;
  4004. } else {
  4005. wpabuf_free(beacon_ies);
  4006. wpabuf_free(proberesp_ies);
  4007. }
  4008. wpa_supplicant_ap_update_beacon(wpa_s);
  4009. }
  4010. static void wpas_p2p_idle_update(void *ctx, int idle)
  4011. {
  4012. struct wpa_supplicant *wpa_s = ctx;
  4013. if (!wpa_s->ap_iface)
  4014. return;
  4015. wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
  4016. if (idle)
  4017. wpas_p2p_set_group_idle_timeout(wpa_s);
  4018. else
  4019. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  4020. }
  4021. struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
  4022. struct wpa_ssid *ssid)
  4023. {
  4024. struct p2p_group *group;
  4025. struct p2p_group_config *cfg;
  4026. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4027. return NULL;
  4028. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4029. return NULL;
  4030. cfg = os_zalloc(sizeof(*cfg));
  4031. if (cfg == NULL)
  4032. return NULL;
  4033. if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
  4034. cfg->persistent_group = 2;
  4035. else if (ssid->p2p_persistent_group)
  4036. cfg->persistent_group = 1;
  4037. os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
  4038. if (wpa_s->max_stations &&
  4039. wpa_s->max_stations < wpa_s->conf->max_num_sta)
  4040. cfg->max_clients = wpa_s->max_stations;
  4041. else
  4042. cfg->max_clients = wpa_s->conf->max_num_sta;
  4043. os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
  4044. cfg->ssid_len = ssid->ssid_len;
  4045. cfg->cb_ctx = wpa_s;
  4046. cfg->ie_update = wpas_p2p_ie_update;
  4047. cfg->idle_update = wpas_p2p_idle_update;
  4048. group = p2p_group_init(wpa_s->global->p2p, cfg);
  4049. if (group == NULL)
  4050. os_free(cfg);
  4051. if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
  4052. p2p_group_notif_formation_done(group);
  4053. wpa_s->p2p_group = group;
  4054. return group;
  4055. }
  4056. void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  4057. int registrar)
  4058. {
  4059. struct wpa_ssid *ssid = wpa_s->current_ssid;
  4060. if (!wpa_s->p2p_in_provisioning) {
  4061. wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
  4062. "provisioning not in progress");
  4063. return;
  4064. }
  4065. if (ssid && ssid->mode == WPAS_MODE_INFRA) {
  4066. u8 go_dev_addr[ETH_ALEN];
  4067. os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
  4068. wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
  4069. ssid->ssid_len);
  4070. /* Clear any stored provisioning info */
  4071. p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
  4072. }
  4073. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
  4074. NULL);
  4075. wpa_s->p2p_go_group_formation_completed = 1;
  4076. if (ssid && ssid->mode == WPAS_MODE_INFRA) {
  4077. /*
  4078. * Use a separate timeout for initial data connection to
  4079. * complete to allow the group to be removed automatically if
  4080. * something goes wrong in this step before the P2P group idle
  4081. * timeout mechanism is taken into use.
  4082. */
  4083. wpa_dbg(wpa_s, MSG_DEBUG,
  4084. "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
  4085. P2P_MAX_INITIAL_CONN_WAIT);
  4086. eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
  4087. wpas_p2p_group_formation_timeout,
  4088. wpa_s->parent, NULL);
  4089. } else if (ssid) {
  4090. /*
  4091. * Use a separate timeout for initial data connection to
  4092. * complete to allow the group to be removed automatically if
  4093. * the client does not complete data connection successfully.
  4094. */
  4095. wpa_dbg(wpa_s, MSG_DEBUG,
  4096. "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
  4097. P2P_MAX_INITIAL_CONN_WAIT_GO);
  4098. eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
  4099. wpas_p2p_group_formation_timeout,
  4100. wpa_s->parent, NULL);
  4101. /*
  4102. * Complete group formation on first successful data connection
  4103. */
  4104. wpa_s->p2p_go_group_formation_completed = 0;
  4105. }
  4106. if (wpa_s->global->p2p)
  4107. p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
  4108. else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4109. wpa_drv_wps_success_cb(wpa_s, peer_addr);
  4110. wpas_group_formation_completed(wpa_s, 1);
  4111. }
  4112. void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
  4113. struct wps_event_fail *fail)
  4114. {
  4115. if (!wpa_s->p2p_in_provisioning) {
  4116. wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
  4117. "provisioning not in progress");
  4118. return;
  4119. }
  4120. if (wpa_s->go_params) {
  4121. p2p_clear_provisioning_info(
  4122. wpa_s->global->p2p,
  4123. wpa_s->go_params->peer_device_addr);
  4124. }
  4125. wpas_notify_p2p_wps_failed(wpa_s, fail);
  4126. }
  4127. int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  4128. const char *config_method,
  4129. enum wpas_p2p_prov_disc_use use)
  4130. {
  4131. u16 config_methods;
  4132. wpa_s->p2p_fallback_to_go_neg = 0;
  4133. wpa_s->pending_pd_use = NORMAL_PD;
  4134. if (os_strncmp(config_method, "display", 7) == 0)
  4135. config_methods = WPS_CONFIG_DISPLAY;
  4136. else if (os_strncmp(config_method, "keypad", 6) == 0)
  4137. config_methods = WPS_CONFIG_KEYPAD;
  4138. else if (os_strncmp(config_method, "pbc", 3) == 0 ||
  4139. os_strncmp(config_method, "pushbutton", 10) == 0)
  4140. config_methods = WPS_CONFIG_PUSHBUTTON;
  4141. else {
  4142. wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
  4143. return -1;
  4144. }
  4145. if (use == WPAS_P2P_PD_AUTO) {
  4146. os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
  4147. wpa_s->pending_pd_config_methods = config_methods;
  4148. wpa_s->p2p_auto_pd = 1;
  4149. wpa_s->p2p_auto_join = 0;
  4150. wpa_s->pending_pd_before_join = 0;
  4151. wpa_s->auto_pd_scan_retry = 0;
  4152. wpas_p2p_stop_find(wpa_s);
  4153. wpa_s->p2p_join_scan_count = 0;
  4154. os_get_time(&wpa_s->p2p_auto_started);
  4155. wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
  4156. wpa_s->p2p_auto_started.sec,
  4157. wpa_s->p2p_auto_started.usec);
  4158. wpas_p2p_join_scan(wpa_s, NULL);
  4159. return 0;
  4160. }
  4161. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  4162. return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
  4163. config_methods,
  4164. use == WPAS_P2P_PD_FOR_JOIN);
  4165. }
  4166. if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
  4167. return -1;
  4168. return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
  4169. config_methods, use == WPAS_P2P_PD_FOR_JOIN,
  4170. 0, 1);
  4171. }
  4172. int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
  4173. char *end)
  4174. {
  4175. return p2p_scan_result_text(ies, ies_len, buf, end);
  4176. }
  4177. static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
  4178. {
  4179. if (!offchannel_pending_action_tx(wpa_s))
  4180. return;
  4181. wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
  4182. "operation request");
  4183. offchannel_clear_pending_action_tx(wpa_s);
  4184. }
  4185. int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
  4186. enum p2p_discovery_type type,
  4187. unsigned int num_req_dev_types, const u8 *req_dev_types,
  4188. const u8 *dev_id, unsigned int search_delay)
  4189. {
  4190. wpas_p2p_clear_pending_action_tx(wpa_s);
  4191. wpa_s->p2p_long_listen = 0;
  4192. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4193. return wpa_drv_p2p_find(wpa_s, timeout, type);
  4194. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
  4195. wpa_s->p2p_in_provisioning)
  4196. return -1;
  4197. wpa_supplicant_cancel_sched_scan(wpa_s);
  4198. return p2p_find(wpa_s->global->p2p, timeout, type,
  4199. num_req_dev_types, req_dev_types, dev_id,
  4200. search_delay);
  4201. }
  4202. static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
  4203. {
  4204. wpas_p2p_clear_pending_action_tx(wpa_s);
  4205. wpa_s->p2p_long_listen = 0;
  4206. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  4207. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  4208. wpa_s->global->p2p_cb_on_scan_complete = 0;
  4209. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  4210. wpa_drv_p2p_stop_find(wpa_s);
  4211. return 1;
  4212. }
  4213. if (wpa_s->global->p2p)
  4214. p2p_stop_find(wpa_s->global->p2p);
  4215. return 0;
  4216. }
  4217. void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
  4218. {
  4219. if (wpas_p2p_stop_find_oper(wpa_s) > 0)
  4220. return;
  4221. wpas_p2p_remove_pending_group_interface(wpa_s);
  4222. }
  4223. static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
  4224. {
  4225. struct wpa_supplicant *wpa_s = eloop_ctx;
  4226. wpa_s->p2p_long_listen = 0;
  4227. }
  4228. int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
  4229. {
  4230. int res;
  4231. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4232. return -1;
  4233. wpa_supplicant_cancel_sched_scan(wpa_s);
  4234. wpas_p2p_clear_pending_action_tx(wpa_s);
  4235. if (timeout == 0) {
  4236. /*
  4237. * This is a request for unlimited Listen state. However, at
  4238. * least for now, this is mapped to a Listen state for one
  4239. * hour.
  4240. */
  4241. timeout = 3600;
  4242. }
  4243. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  4244. wpa_s->p2p_long_listen = 0;
  4245. /*
  4246. * Stop previous find/listen operation to avoid trying to request a new
  4247. * remain-on-channel operation while the driver is still running the
  4248. * previous one.
  4249. */
  4250. if (wpa_s->global->p2p)
  4251. p2p_stop_find(wpa_s->global->p2p);
  4252. res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
  4253. if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
  4254. wpa_s->p2p_long_listen = timeout * 1000;
  4255. eloop_register_timeout(timeout, 0,
  4256. wpas_p2p_long_listen_timeout,
  4257. wpa_s, NULL);
  4258. }
  4259. return res;
  4260. }
  4261. int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  4262. u8 *buf, size_t len, int p2p_group)
  4263. {
  4264. struct wpabuf *p2p_ie;
  4265. int ret;
  4266. if (wpa_s->global->p2p_disabled)
  4267. return -1;
  4268. if (wpa_s->global->p2p == NULL)
  4269. return -1;
  4270. if (bss == NULL)
  4271. return -1;
  4272. p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  4273. ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
  4274. p2p_group, p2p_ie);
  4275. wpabuf_free(p2p_ie);
  4276. return ret;
  4277. }
  4278. int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
  4279. const u8 *dst, const u8 *bssid,
  4280. const u8 *ie, size_t ie_len, int ssi_signal)
  4281. {
  4282. if (wpa_s->global->p2p_disabled)
  4283. return 0;
  4284. if (wpa_s->global->p2p == NULL)
  4285. return 0;
  4286. switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
  4287. ie, ie_len)) {
  4288. case P2P_PREQ_NOT_P2P:
  4289. wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
  4290. ssi_signal);
  4291. /* fall through */
  4292. case P2P_PREQ_MALFORMED:
  4293. case P2P_PREQ_NOT_LISTEN:
  4294. case P2P_PREQ_NOT_PROCESSED:
  4295. default: /* make gcc happy */
  4296. return 0;
  4297. case P2P_PREQ_PROCESSED:
  4298. return 1;
  4299. }
  4300. }
  4301. void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
  4302. const u8 *sa, const u8 *bssid,
  4303. u8 category, const u8 *data, size_t len, int freq)
  4304. {
  4305. if (wpa_s->global->p2p_disabled)
  4306. return;
  4307. if (wpa_s->global->p2p == NULL)
  4308. return;
  4309. p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
  4310. freq);
  4311. }
  4312. void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
  4313. {
  4314. if (wpa_s->global->p2p_disabled)
  4315. return;
  4316. if (wpa_s->global->p2p == NULL)
  4317. return;
  4318. p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
  4319. }
  4320. void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
  4321. {
  4322. p2p_group_deinit(wpa_s->p2p_group);
  4323. wpa_s->p2p_group = NULL;
  4324. wpa_s->ap_configured_cb = NULL;
  4325. wpa_s->ap_configured_cb_ctx = NULL;
  4326. wpa_s->ap_configured_cb_data = NULL;
  4327. wpa_s->connect_without_scan = NULL;
  4328. }
  4329. int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
  4330. {
  4331. wpa_s->p2p_long_listen = 0;
  4332. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4333. return wpa_drv_p2p_reject(wpa_s, addr);
  4334. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4335. return -1;
  4336. return p2p_reject(wpa_s->global->p2p, addr);
  4337. }
  4338. /* Invite to reinvoke a persistent group */
  4339. int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  4340. struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
  4341. int ht40, int pref_freq)
  4342. {
  4343. enum p2p_invite_role role;
  4344. u8 *bssid = NULL;
  4345. int force_freq = 0;
  4346. int res;
  4347. wpa_s->global->p2p_invite_group = NULL;
  4348. if (peer_addr)
  4349. os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
  4350. else
  4351. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  4352. wpa_s->p2p_persistent_go_freq = freq;
  4353. wpa_s->p2p_go_ht40 = !!ht40;
  4354. if (ssid->mode == WPAS_MODE_P2P_GO) {
  4355. role = P2P_INVITE_ROLE_GO;
  4356. if (peer_addr == NULL) {
  4357. wpa_printf(MSG_DEBUG, "P2P: Missing peer "
  4358. "address in invitation command");
  4359. return -1;
  4360. }
  4361. if (wpas_p2p_create_iface(wpa_s)) {
  4362. if (wpas_p2p_add_group_interface(wpa_s,
  4363. WPA_IF_P2P_GO) < 0) {
  4364. wpa_printf(MSG_ERROR, "P2P: Failed to "
  4365. "allocate a new interface for the "
  4366. "group");
  4367. return -1;
  4368. }
  4369. bssid = wpa_s->pending_interface_addr;
  4370. } else
  4371. bssid = wpa_s->own_addr;
  4372. } else {
  4373. role = P2P_INVITE_ROLE_CLIENT;
  4374. peer_addr = ssid->bssid;
  4375. }
  4376. wpa_s->pending_invite_ssid_id = ssid->id;
  4377. res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq);
  4378. if (res)
  4379. return res;
  4380. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4381. return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
  4382. ssid->ssid, ssid->ssid_len,
  4383. go_dev_addr, 1);
  4384. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4385. return -1;
  4386. return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
  4387. ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
  4388. 1, pref_freq);
  4389. }
  4390. /* Invite to join an active group */
  4391. int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
  4392. const u8 *peer_addr, const u8 *go_dev_addr)
  4393. {
  4394. struct wpa_global *global = wpa_s->global;
  4395. enum p2p_invite_role role;
  4396. u8 *bssid = NULL;
  4397. struct wpa_ssid *ssid;
  4398. int persistent;
  4399. int force_freq = 0, pref_freq = 0;
  4400. int res;
  4401. wpa_s->p2p_persistent_go_freq = 0;
  4402. wpa_s->p2p_go_ht40 = 0;
  4403. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  4404. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  4405. break;
  4406. }
  4407. if (wpa_s == NULL) {
  4408. wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
  4409. return -1;
  4410. }
  4411. ssid = wpa_s->current_ssid;
  4412. if (ssid == NULL) {
  4413. wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
  4414. "invitation");
  4415. return -1;
  4416. }
  4417. wpa_s->global->p2p_invite_group = wpa_s;
  4418. persistent = ssid->p2p_persistent_group &&
  4419. wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
  4420. ssid->ssid, ssid->ssid_len);
  4421. if (ssid->mode == WPAS_MODE_P2P_GO) {
  4422. role = P2P_INVITE_ROLE_ACTIVE_GO;
  4423. bssid = wpa_s->own_addr;
  4424. if (go_dev_addr == NULL)
  4425. go_dev_addr = wpa_s->global->p2p_dev_addr;
  4426. } else {
  4427. role = P2P_INVITE_ROLE_CLIENT;
  4428. if (wpa_s->wpa_state < WPA_ASSOCIATED) {
  4429. wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
  4430. "invite to current group");
  4431. return -1;
  4432. }
  4433. bssid = wpa_s->bssid;
  4434. if (go_dev_addr == NULL &&
  4435. !is_zero_ether_addr(wpa_s->go_dev_addr))
  4436. go_dev_addr = wpa_s->go_dev_addr;
  4437. }
  4438. wpa_s->parent->pending_invite_ssid_id = -1;
  4439. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4440. return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
  4441. ssid->ssid, ssid->ssid_len,
  4442. go_dev_addr, persistent);
  4443. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4444. return -1;
  4445. res = wpas_p2p_setup_freqs(wpa_s, 0, &force_freq, &pref_freq);
  4446. if (res)
  4447. return res;
  4448. wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
  4449. return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
  4450. ssid->ssid, ssid->ssid_len, force_freq,
  4451. go_dev_addr, persistent, pref_freq);
  4452. }
  4453. void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
  4454. {
  4455. struct wpa_ssid *ssid = wpa_s->current_ssid;
  4456. const char *ssid_txt;
  4457. u8 go_dev_addr[ETH_ALEN];
  4458. int network_id = -1;
  4459. int persistent;
  4460. int freq;
  4461. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
  4462. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  4463. wpa_s->parent, NULL);
  4464. }
  4465. if (!wpa_s->show_group_started || !ssid)
  4466. goto done;
  4467. wpa_s->show_group_started = 0;
  4468. ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
  4469. os_memset(go_dev_addr, 0, ETH_ALEN);
  4470. if (ssid->bssid_set)
  4471. os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
  4472. persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
  4473. ssid->ssid_len);
  4474. os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
  4475. if (wpa_s->global->p2p_group_formation == wpa_s)
  4476. wpa_s->global->p2p_group_formation = NULL;
  4477. freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
  4478. (int) wpa_s->assoc_freq;
  4479. if (ssid->passphrase == NULL && ssid->psk_set) {
  4480. char psk[65];
  4481. wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
  4482. wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  4483. "%s client ssid=\"%s\" freq=%d psk=%s "
  4484. "go_dev_addr=" MACSTR "%s",
  4485. wpa_s->ifname, ssid_txt, freq, psk,
  4486. MAC2STR(go_dev_addr),
  4487. persistent ? " [PERSISTENT]" : "");
  4488. } else {
  4489. wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  4490. "%s client ssid=\"%s\" freq=%d "
  4491. "passphrase=\"%s\" go_dev_addr=" MACSTR "%s",
  4492. wpa_s->ifname, ssid_txt, freq,
  4493. ssid->passphrase ? ssid->passphrase : "",
  4494. MAC2STR(go_dev_addr),
  4495. persistent ? " [PERSISTENT]" : "");
  4496. }
  4497. if (persistent)
  4498. network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
  4499. ssid, go_dev_addr);
  4500. if (network_id < 0)
  4501. network_id = ssid->id;
  4502. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
  4503. done:
  4504. wpas_p2p_continue_after_scan(wpa_s);
  4505. }
  4506. int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
  4507. u32 interval1, u32 duration2, u32 interval2)
  4508. {
  4509. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4510. return -1;
  4511. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4512. return -1;
  4513. if (wpa_s->wpa_state < WPA_ASSOCIATED ||
  4514. wpa_s->current_ssid == NULL ||
  4515. wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
  4516. return -1;
  4517. return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
  4518. wpa_s->own_addr, wpa_s->assoc_freq,
  4519. duration1, interval1, duration2, interval2);
  4520. }
  4521. int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
  4522. unsigned int interval)
  4523. {
  4524. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4525. return -1;
  4526. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4527. return -1;
  4528. return p2p_ext_listen(wpa_s->global->p2p, period, interval);
  4529. }
  4530. static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
  4531. {
  4532. if (wpa_s->current_ssid == NULL) {
  4533. /*
  4534. * current_ssid can be cleared when P2P client interface gets
  4535. * disconnected, so assume this interface was used as P2P
  4536. * client.
  4537. */
  4538. return 1;
  4539. }
  4540. return wpa_s->current_ssid->p2p_group &&
  4541. wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
  4542. }
  4543. static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
  4544. {
  4545. struct wpa_supplicant *wpa_s = eloop_ctx;
  4546. if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
  4547. wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
  4548. "disabled");
  4549. return;
  4550. }
  4551. wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
  4552. "group");
  4553. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
  4554. }
  4555. static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
  4556. {
  4557. int timeout;
  4558. if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
  4559. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
  4560. if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
  4561. return;
  4562. timeout = wpa_s->conf->p2p_group_idle;
  4563. if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
  4564. (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
  4565. timeout = P2P_MAX_CLIENT_IDLE;
  4566. if (timeout == 0)
  4567. return;
  4568. if (timeout < 0) {
  4569. if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
  4570. timeout = 0; /* special client mode no-timeout */
  4571. else
  4572. return;
  4573. }
  4574. if (wpa_s->p2p_in_provisioning) {
  4575. /*
  4576. * Use the normal group formation timeout during the
  4577. * provisioning phase to avoid terminating this process too
  4578. * early due to group idle timeout.
  4579. */
  4580. wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
  4581. "during provisioning");
  4582. return;
  4583. }
  4584. if (wpa_s->show_group_started) {
  4585. /*
  4586. * Use the normal group formation timeout between the end of
  4587. * the provisioning phase and completion of 4-way handshake to
  4588. * avoid terminating this process too early due to group idle
  4589. * timeout.
  4590. */
  4591. wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
  4592. "while waiting for initial 4-way handshake to "
  4593. "complete");
  4594. return;
  4595. }
  4596. wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
  4597. timeout);
  4598. eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
  4599. wpa_s, NULL);
  4600. }
  4601. /* Returns 1 if the interface was removed */
  4602. int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
  4603. u16 reason_code, const u8 *ie, size_t ie_len,
  4604. int locally_generated)
  4605. {
  4606. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4607. return 0;
  4608. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4609. return 0;
  4610. if (!locally_generated)
  4611. p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
  4612. ie_len);
  4613. if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
  4614. wpa_s->current_ssid &&
  4615. wpa_s->current_ssid->p2p_group &&
  4616. wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
  4617. wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
  4618. "session is ending");
  4619. if (wpas_p2p_group_delete(wpa_s,
  4620. P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
  4621. > 0)
  4622. return 1;
  4623. }
  4624. return 0;
  4625. }
  4626. void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
  4627. u16 reason_code, const u8 *ie, size_t ie_len,
  4628. int locally_generated)
  4629. {
  4630. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4631. return;
  4632. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4633. return;
  4634. if (!locally_generated)
  4635. p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
  4636. ie_len);
  4637. }
  4638. void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
  4639. {
  4640. struct p2p_data *p2p = wpa_s->global->p2p;
  4641. if (p2p == NULL)
  4642. return;
  4643. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
  4644. return;
  4645. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
  4646. p2p_set_dev_name(p2p, wpa_s->conf->device_name);
  4647. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
  4648. p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
  4649. if (wpa_s->wps &&
  4650. (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
  4651. p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
  4652. if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
  4653. p2p_set_uuid(p2p, wpa_s->wps->uuid);
  4654. if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
  4655. p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
  4656. p2p_set_model_name(p2p, wpa_s->conf->model_name);
  4657. p2p_set_model_number(p2p, wpa_s->conf->model_number);
  4658. p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
  4659. }
  4660. if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
  4661. p2p_set_sec_dev_types(p2p,
  4662. (void *) wpa_s->conf->sec_device_type,
  4663. wpa_s->conf->num_sec_device_types);
  4664. if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
  4665. int i;
  4666. p2p_remove_wps_vendor_extensions(p2p);
  4667. for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
  4668. if (wpa_s->conf->wps_vendor_ext[i] == NULL)
  4669. continue;
  4670. p2p_add_wps_vendor_extension(
  4671. p2p, wpa_s->conf->wps_vendor_ext[i]);
  4672. }
  4673. }
  4674. if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
  4675. wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  4676. char country[3];
  4677. country[0] = wpa_s->conf->country[0];
  4678. country[1] = wpa_s->conf->country[1];
  4679. country[2] = 0x04;
  4680. p2p_set_country(p2p, country);
  4681. }
  4682. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
  4683. p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
  4684. wpa_s->conf->p2p_ssid_postfix ?
  4685. os_strlen(wpa_s->conf->p2p_ssid_postfix) :
  4686. 0);
  4687. }
  4688. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
  4689. p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
  4690. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
  4691. u8 reg_class, channel;
  4692. int ret;
  4693. unsigned int r;
  4694. if (wpa_s->conf->p2p_listen_reg_class &&
  4695. wpa_s->conf->p2p_listen_channel) {
  4696. reg_class = wpa_s->conf->p2p_listen_reg_class;
  4697. channel = wpa_s->conf->p2p_listen_channel;
  4698. } else {
  4699. reg_class = 81;
  4700. /*
  4701. * Pick one of the social channels randomly as the
  4702. * listen channel.
  4703. */
  4704. os_get_random((u8 *) &r, sizeof(r));
  4705. channel = 1 + (r % 3) * 5;
  4706. }
  4707. ret = p2p_set_listen_channel(p2p, reg_class, channel);
  4708. if (ret)
  4709. wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
  4710. "failed: %d", ret);
  4711. }
  4712. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
  4713. u8 op_reg_class, op_channel, cfg_op_channel;
  4714. int ret = 0;
  4715. unsigned int r;
  4716. if (wpa_s->conf->p2p_oper_reg_class &&
  4717. wpa_s->conf->p2p_oper_channel) {
  4718. op_reg_class = wpa_s->conf->p2p_oper_reg_class;
  4719. op_channel = wpa_s->conf->p2p_oper_channel;
  4720. cfg_op_channel = 1;
  4721. } else {
  4722. op_reg_class = 81;
  4723. /*
  4724. * Use random operation channel from (1, 6, 11)
  4725. *if no other preference is indicated.
  4726. */
  4727. os_get_random((u8 *) &r, sizeof(r));
  4728. op_channel = 1 + (r % 3) * 5;
  4729. cfg_op_channel = 0;
  4730. }
  4731. ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
  4732. cfg_op_channel);
  4733. if (ret)
  4734. wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
  4735. "failed: %d", ret);
  4736. }
  4737. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
  4738. if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
  4739. wpa_s->conf->p2p_pref_chan) < 0) {
  4740. wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
  4741. "update failed");
  4742. }
  4743. }
  4744. }
  4745. int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
  4746. int duration)
  4747. {
  4748. if (!wpa_s->ap_iface)
  4749. return -1;
  4750. return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
  4751. duration);
  4752. }
  4753. int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
  4754. {
  4755. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4756. return -1;
  4757. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4758. return -1;
  4759. wpa_s->global->cross_connection = enabled;
  4760. p2p_set_cross_connect(wpa_s->global->p2p, enabled);
  4761. if (!enabled) {
  4762. struct wpa_supplicant *iface;
  4763. for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
  4764. {
  4765. if (iface->cross_connect_enabled == 0)
  4766. continue;
  4767. iface->cross_connect_enabled = 0;
  4768. iface->cross_connect_in_use = 0;
  4769. wpa_msg_global(iface->parent, MSG_INFO,
  4770. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  4771. iface->ifname,
  4772. iface->cross_connect_uplink);
  4773. }
  4774. }
  4775. return 0;
  4776. }
  4777. static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
  4778. {
  4779. struct wpa_supplicant *iface;
  4780. if (!uplink->global->cross_connection)
  4781. return;
  4782. for (iface = uplink->global->ifaces; iface; iface = iface->next) {
  4783. if (!iface->cross_connect_enabled)
  4784. continue;
  4785. if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
  4786. 0)
  4787. continue;
  4788. if (iface->ap_iface == NULL)
  4789. continue;
  4790. if (iface->cross_connect_in_use)
  4791. continue;
  4792. iface->cross_connect_in_use = 1;
  4793. wpa_msg_global(iface->parent, MSG_INFO,
  4794. P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
  4795. iface->ifname, iface->cross_connect_uplink);
  4796. }
  4797. }
  4798. static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
  4799. {
  4800. struct wpa_supplicant *iface;
  4801. for (iface = uplink->global->ifaces; iface; iface = iface->next) {
  4802. if (!iface->cross_connect_enabled)
  4803. continue;
  4804. if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
  4805. 0)
  4806. continue;
  4807. if (!iface->cross_connect_in_use)
  4808. continue;
  4809. wpa_msg_global(iface->parent, MSG_INFO,
  4810. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  4811. iface->ifname, iface->cross_connect_uplink);
  4812. iface->cross_connect_in_use = 0;
  4813. }
  4814. }
  4815. void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
  4816. {
  4817. if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
  4818. wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
  4819. wpa_s->cross_connect_disallowed)
  4820. wpas_p2p_disable_cross_connect(wpa_s);
  4821. else
  4822. wpas_p2p_enable_cross_connect(wpa_s);
  4823. if (!wpa_s->ap_iface &&
  4824. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
  4825. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
  4826. }
  4827. void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
  4828. {
  4829. wpas_p2p_disable_cross_connect(wpa_s);
  4830. if (!wpa_s->ap_iface &&
  4831. !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
  4832. wpa_s, NULL))
  4833. wpas_p2p_set_group_idle_timeout(wpa_s);
  4834. }
  4835. static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
  4836. {
  4837. struct wpa_supplicant *iface;
  4838. if (!wpa_s->global->cross_connection)
  4839. return;
  4840. for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
  4841. if (iface == wpa_s)
  4842. continue;
  4843. if (iface->drv_flags &
  4844. WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
  4845. continue;
  4846. if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
  4847. continue;
  4848. wpa_s->cross_connect_enabled = 1;
  4849. os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
  4850. sizeof(wpa_s->cross_connect_uplink));
  4851. wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
  4852. "%s to %s whenever uplink is available",
  4853. wpa_s->ifname, wpa_s->cross_connect_uplink);
  4854. if (iface->ap_iface || iface->current_ssid == NULL ||
  4855. iface->current_ssid->mode != WPAS_MODE_INFRA ||
  4856. iface->cross_connect_disallowed ||
  4857. iface->wpa_state != WPA_COMPLETED)
  4858. break;
  4859. wpa_s->cross_connect_in_use = 1;
  4860. wpa_msg_global(wpa_s->parent, MSG_INFO,
  4861. P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
  4862. wpa_s->ifname, wpa_s->cross_connect_uplink);
  4863. break;
  4864. }
  4865. }
  4866. int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
  4867. {
  4868. if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
  4869. !wpa_s->p2p_in_provisioning)
  4870. return 0; /* not P2P client operation */
  4871. wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
  4872. "session overlap");
  4873. if (wpa_s != wpa_s->parent)
  4874. wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
  4875. wpas_p2p_group_formation_failed(wpa_s);
  4876. return 1;
  4877. }
  4878. void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
  4879. {
  4880. struct p2p_channels chan;
  4881. if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
  4882. return;
  4883. os_memset(&chan, 0, sizeof(chan));
  4884. if (wpas_p2p_setup_channels(wpa_s, &chan)) {
  4885. wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
  4886. "channel list");
  4887. return;
  4888. }
  4889. p2p_update_channel_list(wpa_s->global->p2p, &chan);
  4890. }
  4891. static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
  4892. struct wpa_scan_results *scan_res)
  4893. {
  4894. wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
  4895. }
  4896. int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
  4897. {
  4898. struct wpa_global *global = wpa_s->global;
  4899. int found = 0;
  4900. const u8 *peer;
  4901. if (global->p2p == NULL)
  4902. return -1;
  4903. wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
  4904. if (wpa_s->pending_interface_name[0] &&
  4905. !is_zero_ether_addr(wpa_s->pending_interface_addr))
  4906. found = 1;
  4907. peer = p2p_get_go_neg_peer(global->p2p);
  4908. if (peer) {
  4909. wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
  4910. MACSTR, MAC2STR(peer));
  4911. p2p_unauthorize(global->p2p, peer);
  4912. found = 1;
  4913. }
  4914. if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
  4915. wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
  4916. wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
  4917. found = 1;
  4918. }
  4919. if (wpa_s->pending_pd_before_join) {
  4920. wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
  4921. wpa_s->pending_pd_before_join = 0;
  4922. found = 1;
  4923. }
  4924. wpas_p2p_stop_find(wpa_s);
  4925. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  4926. if (wpa_s == global->p2p_group_formation &&
  4927. (wpa_s->p2p_in_provisioning ||
  4928. wpa_s->parent->pending_interface_type ==
  4929. WPA_IF_P2P_CLIENT)) {
  4930. wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
  4931. "formation found - cancelling",
  4932. wpa_s->ifname);
  4933. found = 1;
  4934. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  4935. wpa_s->parent, NULL);
  4936. if (wpa_s->p2p_in_provisioning) {
  4937. wpas_group_formation_completed(wpa_s, 0);
  4938. break;
  4939. }
  4940. wpas_p2p_group_delete(wpa_s,
  4941. P2P_GROUP_REMOVAL_REQUESTED);
  4942. break;
  4943. }
  4944. }
  4945. if (!found) {
  4946. wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
  4947. return -1;
  4948. }
  4949. return 0;
  4950. }
  4951. void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
  4952. {
  4953. if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
  4954. return;
  4955. wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
  4956. "being available anymore");
  4957. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
  4958. }
  4959. void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
  4960. int freq_24, int freq_5, int freq_overall)
  4961. {
  4962. struct p2p_data *p2p = wpa_s->global->p2p;
  4963. if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
  4964. return;
  4965. p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
  4966. }
  4967. int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
  4968. {
  4969. u8 peer[ETH_ALEN];
  4970. struct p2p_data *p2p = wpa_s->global->p2p;
  4971. if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
  4972. return -1;
  4973. if (hwaddr_aton(addr, peer))
  4974. return -1;
  4975. return p2p_unauthorize(p2p, peer);
  4976. }
  4977. /**
  4978. * wpas_p2p_disconnect - Disconnect from a P2P Group
  4979. * @wpa_s: Pointer to wpa_supplicant data
  4980. * Returns: 0 on success, -1 on failure
  4981. *
  4982. * This can be used to disconnect from a group in which the local end is a P2P
  4983. * Client or to end a P2P Group in case the local end is the Group Owner. If a
  4984. * virtual network interface was created for this group, that interface will be
  4985. * removed. Otherwise, only the configured P2P group network will be removed
  4986. * from the interface.
  4987. */
  4988. int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
  4989. {
  4990. if (wpa_s == NULL)
  4991. return -1;
  4992. return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
  4993. -1 : 0;
  4994. }
  4995. int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
  4996. {
  4997. int ret;
  4998. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4999. return 0;
  5000. ret = p2p_in_progress(wpa_s->global->p2p);
  5001. if (ret == 0) {
  5002. /*
  5003. * Check whether there is an ongoing WPS provisioning step (or
  5004. * other parts of group formation) on another interface since
  5005. * p2p_in_progress() does not report this to avoid issues for
  5006. * scans during such provisioning step.
  5007. */
  5008. if (wpa_s->global->p2p_group_formation &&
  5009. wpa_s->global->p2p_group_formation != wpa_s) {
  5010. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
  5011. "in group formation",
  5012. wpa_s->global->p2p_group_formation->ifname);
  5013. ret = 1;
  5014. }
  5015. }
  5016. if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
  5017. struct os_time now;
  5018. os_get_time(&now);
  5019. if (now.sec > wpa_s->global->p2p_go_wait_client.sec +
  5020. P2P_MAX_INITIAL_CONN_WAIT_GO) {
  5021. /* Wait for the first client has expired */
  5022. wpa_s->global->p2p_go_wait_client.sec = 0;
  5023. } else {
  5024. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
  5025. ret = 1;
  5026. }
  5027. }
  5028. return ret;
  5029. }
  5030. void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
  5031. struct wpa_ssid *ssid)
  5032. {
  5033. if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
  5034. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  5035. wpa_s->parent, NULL) > 0) {
  5036. /**
  5037. * Remove the network by scheduling the group formation
  5038. * timeout to happen immediately. The teardown code
  5039. * needs to be scheduled to run asynch later so that we
  5040. * don't delete data from under ourselves unexpectedly.
  5041. * Calling wpas_p2p_group_formation_timeout directly
  5042. * causes a series of crashes in WPS failure scenarios.
  5043. */
  5044. wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
  5045. "P2P group network getting removed");
  5046. eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
  5047. wpa_s->parent, NULL);
  5048. }
  5049. }
  5050. struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
  5051. const u8 *addr, const u8 *ssid,
  5052. size_t ssid_len)
  5053. {
  5054. struct wpa_ssid *s;
  5055. size_t i;
  5056. for (s = wpa_s->conf->ssid; s; s = s->next) {
  5057. if (s->disabled != 2)
  5058. continue;
  5059. if (ssid &&
  5060. (ssid_len != s->ssid_len ||
  5061. os_memcmp(ssid, s->ssid, ssid_len) != 0))
  5062. continue;
  5063. if (addr == NULL) {
  5064. if (s->mode == WPAS_MODE_P2P_GO)
  5065. return s;
  5066. continue;
  5067. }
  5068. if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
  5069. return s; /* peer is GO in the persistent group */
  5070. if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
  5071. continue;
  5072. for (i = 0; i < s->num_p2p_clients; i++) {
  5073. if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
  5074. addr, ETH_ALEN) == 0)
  5075. return s; /* peer is P2P client in persistent
  5076. * group */
  5077. }
  5078. }
  5079. return NULL;
  5080. }
  5081. void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
  5082. const u8 *addr)
  5083. {
  5084. if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  5085. wpa_s->parent, NULL) > 0) {
  5086. /*
  5087. * This can happen if WPS provisioning step is not terminated
  5088. * cleanly (e.g., P2P Client does not send WSC_Done). Since the
  5089. * peer was able to connect, there is no need to time out group
  5090. * formation after this, though. In addition, this is used with
  5091. * the initial connection wait on the GO as a separate formation
  5092. * timeout and as such, expected to be hit after the initial WPS
  5093. * provisioning step.
  5094. */
  5095. wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
  5096. }
  5097. if (!wpa_s->p2p_go_group_formation_completed) {
  5098. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
  5099. wpa_s->p2p_go_group_formation_completed = 1;
  5100. wpa_s->global->p2p_group_formation = NULL;
  5101. wpa_s->p2p_in_provisioning = 0;
  5102. }
  5103. wpa_s->global->p2p_go_wait_client.sec = 0;
  5104. if (addr == NULL)
  5105. return;
  5106. wpas_p2p_add_persistent_group_client(wpa_s, addr);
  5107. }
  5108. static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
  5109. int group_added)
  5110. {
  5111. struct wpa_supplicant *group = wpa_s;
  5112. if (wpa_s->global->p2p_group_formation)
  5113. group = wpa_s->global->p2p_group_formation;
  5114. wpa_s = wpa_s->parent;
  5115. offchannel_send_action_done(wpa_s);
  5116. if (group_added)
  5117. wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
  5118. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
  5119. wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
  5120. wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
  5121. 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
  5122. wpa_s->p2p_persistent_id,
  5123. wpa_s->p2p_pd_before_go_neg,
  5124. wpa_s->p2p_go_ht40);
  5125. }
  5126. int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
  5127. {
  5128. if (!wpa_s->p2p_fallback_to_go_neg ||
  5129. wpa_s->p2p_in_provisioning <= 5)
  5130. return 0;
  5131. if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
  5132. return 0; /* peer operating as a GO */
  5133. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
  5134. "fallback to GO Negotiation");
  5135. wpas_p2p_fallback_to_go_neg(wpa_s, 1);
  5136. return 1;
  5137. }
  5138. unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
  5139. {
  5140. const char *rn, *rn2;
  5141. struct wpa_supplicant *ifs;
  5142. if (wpa_s->wpa_state > WPA_SCANNING) {
  5143. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
  5144. "concurrent operation",
  5145. P2P_CONCURRENT_SEARCH_DELAY);
  5146. return P2P_CONCURRENT_SEARCH_DELAY;
  5147. }
  5148. if (!wpa_s->driver->get_radio_name)
  5149. return 0;
  5150. rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
  5151. if (rn == NULL || rn[0] == '\0')
  5152. return 0;
  5153. for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
  5154. if (ifs == wpa_s || !ifs->driver->get_radio_name)
  5155. continue;
  5156. rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
  5157. if (!rn2 || os_strcmp(rn, rn2) != 0)
  5158. continue;
  5159. if (ifs->wpa_state > WPA_SCANNING) {
  5160. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
  5161. "delay due to concurrent operation on "
  5162. "interface %s",
  5163. P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
  5164. return P2P_CONCURRENT_SEARCH_DELAY;
  5165. }
  5166. }
  5167. return 0;
  5168. }
  5169. void wpas_p2p_continue_after_scan(struct wpa_supplicant *wpa_s)
  5170. {
  5171. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Station mode scan operation not "
  5172. "pending anymore (sta_scan_pending=%d "
  5173. "p2p_cb_on_scan_complete=%d)", wpa_s->sta_scan_pending,
  5174. wpa_s->global->p2p_cb_on_scan_complete);
  5175. wpa_s->sta_scan_pending = 0;
  5176. if (!wpa_s->global->p2p_cb_on_scan_complete)
  5177. return;
  5178. wpa_s->global->p2p_cb_on_scan_complete = 0;
  5179. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5180. return;
  5181. if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
  5182. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
  5183. "continued after successful connection");
  5184. p2p_increase_search_delay(wpa_s->global->p2p,
  5185. wpas_p2p_search_delay(wpa_s));
  5186. }
  5187. }
  5188. static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
  5189. struct wpa_ssid *s, const u8 *addr,
  5190. int iface_addr)
  5191. {
  5192. struct psk_list_entry *psk, *tmp;
  5193. int changed = 0;
  5194. dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
  5195. list) {
  5196. if ((iface_addr && !psk->p2p &&
  5197. os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
  5198. (!iface_addr && psk->p2p &&
  5199. os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
  5200. wpa_dbg(wpa_s, MSG_DEBUG,
  5201. "P2P: Remove persistent group PSK list entry for "
  5202. MACSTR " p2p=%u",
  5203. MAC2STR(psk->addr), psk->p2p);
  5204. dl_list_del(&psk->list);
  5205. os_free(psk);
  5206. changed++;
  5207. }
  5208. }
  5209. return changed;
  5210. }
  5211. void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
  5212. const u8 *p2p_dev_addr,
  5213. const u8 *psk, size_t psk_len)
  5214. {
  5215. struct wpa_ssid *ssid = wpa_s->current_ssid;
  5216. struct wpa_ssid *persistent;
  5217. struct psk_list_entry *p;
  5218. if (psk_len != sizeof(p->psk))
  5219. return;
  5220. if (p2p_dev_addr) {
  5221. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
  5222. " p2p_dev_addr=" MACSTR,
  5223. MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
  5224. if (is_zero_ether_addr(p2p_dev_addr))
  5225. p2p_dev_addr = NULL;
  5226. } else {
  5227. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
  5228. MAC2STR(mac_addr));
  5229. }
  5230. if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
  5231. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
  5232. /* To be added to persistent group once created */
  5233. if (wpa_s->global->add_psk == NULL) {
  5234. wpa_s->global->add_psk = os_zalloc(sizeof(*p));
  5235. if (wpa_s->global->add_psk == NULL)
  5236. return;
  5237. }
  5238. p = wpa_s->global->add_psk;
  5239. if (p2p_dev_addr) {
  5240. p->p2p = 1;
  5241. os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
  5242. } else {
  5243. p->p2p = 0;
  5244. os_memcpy(p->addr, mac_addr, ETH_ALEN);
  5245. }
  5246. os_memcpy(p->psk, psk, psk_len);
  5247. return;
  5248. }
  5249. if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
  5250. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
  5251. return;
  5252. }
  5253. persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
  5254. ssid->ssid_len);
  5255. if (!persistent) {
  5256. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
  5257. return;
  5258. }
  5259. p = os_zalloc(sizeof(*p));
  5260. if (p == NULL)
  5261. return;
  5262. if (p2p_dev_addr) {
  5263. p->p2p = 1;
  5264. os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
  5265. } else {
  5266. p->p2p = 0;
  5267. os_memcpy(p->addr, mac_addr, ETH_ALEN);
  5268. }
  5269. os_memcpy(p->psk, psk, psk_len);
  5270. if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS) {
  5271. struct psk_list_entry *last;
  5272. last = dl_list_last(&persistent->psk_list,
  5273. struct psk_list_entry, list);
  5274. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
  5275. MACSTR " (p2p=%u) to make room for a new one",
  5276. MAC2STR(last->addr), last->p2p);
  5277. dl_list_del(&last->list);
  5278. os_free(last);
  5279. }
  5280. wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
  5281. p2p_dev_addr ? p2p_dev_addr : mac_addr,
  5282. p2p_dev_addr == NULL);
  5283. if (p2p_dev_addr) {
  5284. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
  5285. MACSTR, MAC2STR(p2p_dev_addr));
  5286. } else {
  5287. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
  5288. MAC2STR(mac_addr));
  5289. }
  5290. dl_list_add(&persistent->psk_list, &p->list);
  5291. #ifndef CONFIG_NO_CONFIG_WRITE
  5292. if (wpa_s->parent->conf->update_config &&
  5293. wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
  5294. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  5295. #endif /* CONFIG_NO_CONFIG_WRITE */
  5296. }
  5297. static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
  5298. struct wpa_ssid *s, const u8 *addr,
  5299. int iface_addr)
  5300. {
  5301. int res;
  5302. res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
  5303. if (res > 0) {
  5304. #ifndef CONFIG_NO_CONFIG_WRITE
  5305. if (wpa_s->conf->update_config &&
  5306. wpa_config_write(wpa_s->confname, wpa_s->conf))
  5307. wpa_dbg(wpa_s, MSG_DEBUG,
  5308. "P2P: Failed to update configuration");
  5309. #endif /* CONFIG_NO_CONFIG_WRITE */
  5310. }
  5311. }
  5312. static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
  5313. const u8 *peer, int iface_addr)
  5314. {
  5315. struct hostapd_data *hapd;
  5316. struct hostapd_wpa_psk *psk, *prev, *rem;
  5317. struct sta_info *sta;
  5318. if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
  5319. wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
  5320. return;
  5321. /* Remove per-station PSK entry */
  5322. hapd = wpa_s->ap_iface->bss[0];
  5323. prev = NULL;
  5324. psk = hapd->conf->ssid.wpa_psk;
  5325. while (psk) {
  5326. if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
  5327. (!iface_addr &&
  5328. os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
  5329. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
  5330. MACSTR " iface_addr=%d",
  5331. MAC2STR(peer), iface_addr);
  5332. if (prev)
  5333. prev->next = psk->next;
  5334. else
  5335. hapd->conf->ssid.wpa_psk = psk->next;
  5336. rem = psk;
  5337. psk = psk->next;
  5338. os_free(rem);
  5339. } else {
  5340. prev = psk;
  5341. psk = psk->next;
  5342. }
  5343. }
  5344. /* Disconnect from group */
  5345. if (iface_addr)
  5346. sta = ap_get_sta(hapd, peer);
  5347. else
  5348. sta = ap_get_sta_p2p(hapd, peer);
  5349. if (sta) {
  5350. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
  5351. " (iface_addr=%d) from group",
  5352. MAC2STR(peer), iface_addr);
  5353. hostapd_drv_sta_deauth(hapd, sta->addr,
  5354. WLAN_REASON_DEAUTH_LEAVING);
  5355. ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
  5356. }
  5357. }
  5358. void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
  5359. int iface_addr)
  5360. {
  5361. struct wpa_ssid *s;
  5362. struct wpa_supplicant *w;
  5363. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
  5364. /* Remove from any persistent group */
  5365. for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
  5366. if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
  5367. continue;
  5368. if (!iface_addr)
  5369. wpas_remove_persistent_peer(wpa_s, s, peer, 0);
  5370. wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
  5371. }
  5372. /* Remove from any operating group */
  5373. for (w = wpa_s->global->ifaces; w; w = w->next)
  5374. wpas_p2p_remove_client_go(w, peer, iface_addr);
  5375. }
  5376. static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
  5377. {
  5378. struct wpa_supplicant *wpa_s = eloop_ctx;
  5379. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
  5380. }
  5381. int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
  5382. {
  5383. struct wpa_ssid *ssid = wpa_s->current_ssid;
  5384. if (ssid == NULL || !ssid->p2p_group)
  5385. return 0;
  5386. if (wpa_s->p2p_last_4way_hs_fail &&
  5387. wpa_s->p2p_last_4way_hs_fail == ssid) {
  5388. u8 go_dev_addr[ETH_ALEN];
  5389. struct wpa_ssid *persistent;
  5390. if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
  5391. ssid->ssid,
  5392. ssid->ssid_len) <= 0) {
  5393. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
  5394. goto disconnect;
  5395. }
  5396. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
  5397. MACSTR, MAC2STR(go_dev_addr));
  5398. persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
  5399. ssid->ssid,
  5400. ssid->ssid_len);
  5401. if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
  5402. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
  5403. goto disconnect;
  5404. }
  5405. wpa_msg_global(wpa_s->parent, MSG_INFO,
  5406. P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
  5407. persistent->id);
  5408. disconnect:
  5409. wpa_s->p2p_last_4way_hs_fail = NULL;
  5410. /*
  5411. * Remove the group from a timeout to avoid issues with caller
  5412. * continuing to use the interface if this is on a P2P group
  5413. * interface.
  5414. */
  5415. eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
  5416. wpa_s, NULL);
  5417. return 1;
  5418. }
  5419. wpa_s->p2p_last_4way_hs_fail = ssid;
  5420. return 0;
  5421. }