p2p_supplicant.c 234 KB

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