p2p_supplicant.c 211 KB

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