p2p_supplicant.c 223 KB

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