p2p_supplicant.c 227 KB

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