p2p_supplicant.c 178 KB

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