p2p_supplicant.c 212 KB

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