p2p_supplicant.c 211 KB

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