p2p_supplicant.c 213 KB

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