p2p_supplicant.c 178 KB

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