p2p_supplicant.c 207 KB

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