p2p_supplicant.c 213 KB

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