p2p_supplicant.c 163 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919
  1. /*
  2. * wpa_supplicant - P2P
  3. * Copyright (c) 2009-2010, Atheros Communications
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "eloop.h"
  11. #include "common/ieee802_11_common.h"
  12. #include "common/ieee802_11_defs.h"
  13. #include "common/wpa_ctrl.h"
  14. #include "wps/wps_i.h"
  15. #include "p2p/p2p.h"
  16. #include "ap/hostapd.h"
  17. #include "ap/ap_config.h"
  18. #include "ap/p2p_hostapd.h"
  19. #include "eapol_supp/eapol_supp_sm.h"
  20. #include "rsn_supp/wpa.h"
  21. #include "wpa_supplicant_i.h"
  22. #include "driver_i.h"
  23. #include "ap.h"
  24. #include "config_ssid.h"
  25. #include "config.h"
  26. #include "notify.h"
  27. #include "scan.h"
  28. #include "bss.h"
  29. #include "offchannel.h"
  30. #include "wps_supplicant.h"
  31. #include "p2p_supplicant.h"
  32. /*
  33. * How many times to try to scan to find the GO before giving up on join
  34. * request.
  35. */
  36. #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
  37. #define P2P_AUTO_PD_SCAN_ATTEMPTS 5
  38. #ifndef P2P_MAX_CLIENT_IDLE
  39. /*
  40. * How many seconds to try to reconnect to the GO when connection in P2P client
  41. * role has been lost.
  42. */
  43. #define P2P_MAX_CLIENT_IDLE 10
  44. #endif /* P2P_MAX_CLIENT_IDLE */
  45. #ifndef P2P_MAX_INITIAL_CONN_WAIT
  46. /*
  47. * How many seconds to wait for initial 4-way handshake to get completed after
  48. * WPS provisioning step.
  49. */
  50. #define P2P_MAX_INITIAL_CONN_WAIT 10
  51. #endif /* P2P_MAX_INITIAL_CONN_WAIT */
  52. #ifndef P2P_CONCURRENT_SEARCH_DELAY
  53. #define P2P_CONCURRENT_SEARCH_DELAY 500
  54. #endif /* P2P_CONCURRENT_SEARCH_DELAY */
  55. #define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
  56. enum p2p_group_removal_reason {
  57. P2P_GROUP_REMOVAL_UNKNOWN,
  58. P2P_GROUP_REMOVAL_SILENT,
  59. P2P_GROUP_REMOVAL_FORMATION_FAILED,
  60. P2P_GROUP_REMOVAL_REQUESTED,
  61. P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
  62. P2P_GROUP_REMOVAL_UNAVAILABLE,
  63. P2P_GROUP_REMOVAL_GO_ENDING_SESSION
  64. };
  65. static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
  66. static struct wpa_supplicant *
  67. wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
  68. int go);
  69. static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
  70. static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
  71. static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
  72. static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
  73. const u8 *dev_addr, enum p2p_wps_method wps_method,
  74. int auto_join);
  75. static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
  76. static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
  77. static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
  78. static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
  79. static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
  80. void *timeout_ctx);
  81. static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
  82. int group_added);
  83. static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
  84. /*
  85. * Get the number of concurrent channels that the HW can operate, but that are
  86. * currently not in use by any of the wpa_supplicant interfaces.
  87. */
  88. static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
  89. {
  90. int *freqs;
  91. int num;
  92. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  93. if (!freqs)
  94. return -1;
  95. num = get_shared_radio_freqs(wpa_s, freqs,
  96. wpa_s->num_multichan_concurrent);
  97. os_free(freqs);
  98. return wpa_s->num_multichan_concurrent - num;
  99. }
  100. /*
  101. * Get the frequencies that are currently in use by one or more of the virtual
  102. * interfaces, and that are also valid for P2P operation.
  103. */
  104. static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
  105. int *p2p_freqs, unsigned int len)
  106. {
  107. int *freqs;
  108. unsigned int num, i, j;
  109. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  110. if (!freqs)
  111. return -1;
  112. num = get_shared_radio_freqs(wpa_s, freqs,
  113. wpa_s->num_multichan_concurrent);
  114. os_memset(p2p_freqs, 0, sizeof(int) * len);
  115. for (i = 0, j = 0; i < num && j < len; i++) {
  116. if (p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
  117. p2p_freqs[j++] = freqs[i];
  118. }
  119. os_free(freqs);
  120. return j;
  121. }
  122. static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
  123. int freq)
  124. {
  125. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  126. return;
  127. if (freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
  128. wpas_p2p_num_unused_channels(wpa_s) > 0 &&
  129. wpa_s->parent->conf->p2p_ignore_shared_freq)
  130. freq = 0;
  131. p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
  132. }
  133. static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
  134. struct wpa_scan_results *scan_res)
  135. {
  136. size_t i;
  137. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  138. return;
  139. wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
  140. (int) scan_res->num);
  141. for (i = 0; i < scan_res->num; i++) {
  142. struct wpa_scan_res *bss = scan_res->res[i];
  143. struct os_time time_tmp_age, entry_ts;
  144. time_tmp_age.sec = bss->age / 1000;
  145. time_tmp_age.usec = (bss->age % 1000) * 1000;
  146. os_time_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
  147. if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
  148. bss->freq, &entry_ts, bss->level,
  149. (const u8 *) (bss + 1),
  150. bss->ie_len) > 0)
  151. break;
  152. }
  153. p2p_scan_res_handled(wpa_s->global->p2p);
  154. }
  155. static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
  156. unsigned int num_req_dev_types,
  157. const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
  158. {
  159. struct wpa_supplicant *wpa_s = ctx;
  160. struct wpa_supplicant *ifs;
  161. struct wpa_driver_scan_params params;
  162. int ret;
  163. struct wpabuf *wps_ie, *ies;
  164. int social_channels[] = { 2412, 2437, 2462, 0, 0 };
  165. size_t ielen;
  166. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  167. return -1;
  168. for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
  169. if (ifs->sta_scan_pending &&
  170. (wpas_scan_scheduled(ifs) || ifs->scanning) &&
  171. wpas_p2p_in_progress(wpa_s) == 2) {
  172. wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
  173. "pending station mode scan to be "
  174. "completed on interface %s", ifs->ifname);
  175. wpa_s->global->p2p_cb_on_scan_complete = 1;
  176. wpa_supplicant_req_scan(ifs, 0, 0);
  177. return 1;
  178. }
  179. }
  180. os_memset(&params, 0, sizeof(params));
  181. /* P2P Wildcard SSID */
  182. params.num_ssids = 1;
  183. params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
  184. params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
  185. wpa_s->wps->dev.p2p = 1;
  186. wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
  187. wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
  188. num_req_dev_types, req_dev_types);
  189. if (wps_ie == NULL)
  190. return -1;
  191. ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
  192. ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
  193. if (ies == NULL) {
  194. wpabuf_free(wps_ie);
  195. return -1;
  196. }
  197. wpabuf_put_buf(ies, wps_ie);
  198. wpabuf_free(wps_ie);
  199. p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
  200. params.p2p_probe = 1;
  201. params.extra_ies = wpabuf_head(ies);
  202. params.extra_ies_len = wpabuf_len(ies);
  203. switch (type) {
  204. case P2P_SCAN_SOCIAL:
  205. params.freqs = social_channels;
  206. break;
  207. case P2P_SCAN_FULL:
  208. break;
  209. case P2P_SCAN_SOCIAL_PLUS_ONE:
  210. social_channels[3] = freq;
  211. params.freqs = social_channels;
  212. break;
  213. }
  214. ret = wpa_drv_scan(wpa_s, &params);
  215. wpabuf_free(ies);
  216. if (ret) {
  217. for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
  218. if (ifs->scanning ||
  219. ifs->scan_res_handler == wpas_p2p_scan_res_handler) {
  220. wpa_s->global->p2p_cb_on_scan_complete = 1;
  221. ret = 1;
  222. break;
  223. }
  224. }
  225. } else {
  226. os_get_time(&wpa_s->scan_trigger_time);
  227. wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
  228. }
  229. return ret;
  230. }
  231. static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
  232. {
  233. switch (p2p_group_interface) {
  234. case P2P_GROUP_INTERFACE_PENDING:
  235. return WPA_IF_P2P_GROUP;
  236. case P2P_GROUP_INTERFACE_GO:
  237. return WPA_IF_P2P_GO;
  238. case P2P_GROUP_INTERFACE_CLIENT:
  239. return WPA_IF_P2P_CLIENT;
  240. }
  241. return WPA_IF_P2P_GROUP;
  242. }
  243. static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
  244. const u8 *ssid,
  245. size_t ssid_len, int *go)
  246. {
  247. struct wpa_ssid *s;
  248. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  249. for (s = wpa_s->conf->ssid; s; s = s->next) {
  250. if (s->disabled != 0 || !s->p2p_group ||
  251. s->ssid_len != ssid_len ||
  252. os_memcmp(ssid, s->ssid, ssid_len) != 0)
  253. continue;
  254. if (s->mode == WPAS_MODE_P2P_GO &&
  255. s != wpa_s->current_ssid)
  256. continue;
  257. if (go)
  258. *go = s->mode == WPAS_MODE_P2P_GO;
  259. return wpa_s;
  260. }
  261. }
  262. return NULL;
  263. }
  264. static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
  265. enum p2p_group_removal_reason removal_reason)
  266. {
  267. struct wpa_ssid *ssid;
  268. char *gtype;
  269. const char *reason;
  270. ssid = wpa_s->current_ssid;
  271. if (ssid == NULL) {
  272. /*
  273. * The current SSID was not known, but there may still be a
  274. * pending P2P group interface waiting for provisioning or a
  275. * P2P group that is trying to reconnect.
  276. */
  277. ssid = wpa_s->conf->ssid;
  278. while (ssid) {
  279. if (ssid->p2p_group && ssid->disabled != 2)
  280. break;
  281. ssid = ssid->next;
  282. }
  283. if (ssid == NULL &&
  284. wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
  285. {
  286. wpa_printf(MSG_ERROR, "P2P: P2P group interface "
  287. "not found");
  288. return -1;
  289. }
  290. }
  291. if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
  292. gtype = "GO";
  293. else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
  294. (ssid && ssid->mode == WPAS_MODE_INFRA)) {
  295. wpa_s->reassociate = 0;
  296. wpa_s->disconnected = 1;
  297. wpa_supplicant_deauthenticate(wpa_s,
  298. WLAN_REASON_DEAUTH_LEAVING);
  299. gtype = "client";
  300. } else
  301. gtype = "GO";
  302. if (wpa_s->cross_connect_in_use) {
  303. wpa_s->cross_connect_in_use = 0;
  304. wpa_msg_global(wpa_s->parent, MSG_INFO,
  305. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  306. wpa_s->ifname, wpa_s->cross_connect_uplink);
  307. }
  308. switch (removal_reason) {
  309. case P2P_GROUP_REMOVAL_REQUESTED:
  310. reason = " reason=REQUESTED";
  311. break;
  312. case P2P_GROUP_REMOVAL_FORMATION_FAILED:
  313. reason = " reason=FORMATION_FAILED";
  314. break;
  315. case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
  316. reason = " reason=IDLE";
  317. break;
  318. case P2P_GROUP_REMOVAL_UNAVAILABLE:
  319. reason = " reason=UNAVAILABLE";
  320. break;
  321. case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
  322. reason = " reason=GO_ENDING_SESSION";
  323. break;
  324. default:
  325. reason = "";
  326. break;
  327. }
  328. if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
  329. wpa_msg_global(wpa_s->parent, MSG_INFO,
  330. P2P_EVENT_GROUP_REMOVED "%s %s%s",
  331. wpa_s->ifname, gtype, reason);
  332. }
  333. if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
  334. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
  335. if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  336. wpa_s->parent, NULL) > 0) {
  337. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
  338. "timeout");
  339. wpa_s->p2p_in_provisioning = 0;
  340. }
  341. if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
  342. wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
  343. if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
  344. struct wpa_global *global;
  345. char *ifname;
  346. enum wpa_driver_if_type type;
  347. wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
  348. wpa_s->ifname);
  349. global = wpa_s->global;
  350. ifname = os_strdup(wpa_s->ifname);
  351. type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
  352. wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
  353. wpa_s = global->ifaces;
  354. if (wpa_s && ifname)
  355. wpa_drv_if_remove(wpa_s, type, ifname);
  356. os_free(ifname);
  357. return 1;
  358. }
  359. wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
  360. if (ssid && (ssid->p2p_group ||
  361. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
  362. (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
  363. int id = ssid->id;
  364. if (ssid == wpa_s->current_ssid) {
  365. wpa_sm_set_config(wpa_s->wpa, NULL);
  366. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  367. wpa_s->current_ssid = NULL;
  368. }
  369. /*
  370. * Networks objects created during any P2P activities are not
  371. * exposed out as they might/will confuse certain non-P2P aware
  372. * applications since these network objects won't behave like
  373. * regular ones.
  374. *
  375. * Likewise, we don't send out network removed signals for such
  376. * network objects.
  377. */
  378. wpa_config_remove_network(wpa_s->conf, id);
  379. wpa_supplicant_clear_status(wpa_s);
  380. wpa_supplicant_cancel_sched_scan(wpa_s);
  381. wpa_s->sta_scan_pending = 0;
  382. } else {
  383. wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
  384. "found");
  385. }
  386. if (wpa_s->ap_iface)
  387. wpa_supplicant_ap_deinit(wpa_s);
  388. else
  389. wpa_drv_deinit_p2p_cli(wpa_s);
  390. return 0;
  391. }
  392. static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
  393. u8 *go_dev_addr,
  394. const u8 *ssid, size_t ssid_len)
  395. {
  396. struct wpa_bss *bss;
  397. const u8 *bssid;
  398. struct wpabuf *p2p;
  399. u8 group_capab;
  400. const u8 *addr;
  401. if (wpa_s->go_params)
  402. bssid = wpa_s->go_params->peer_interface_addr;
  403. else
  404. bssid = wpa_s->bssid;
  405. bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
  406. if (bss == NULL) {
  407. u8 iface_addr[ETH_ALEN];
  408. if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
  409. iface_addr) == 0)
  410. bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
  411. }
  412. if (bss == NULL) {
  413. wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
  414. "group is persistent - BSS " MACSTR " not found",
  415. MAC2STR(bssid));
  416. return 0;
  417. }
  418. p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  419. if (p2p == NULL) {
  420. wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
  421. "group is persistent - BSS " MACSTR
  422. " did not include P2P IE", MAC2STR(bssid));
  423. wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
  424. (u8 *) (bss + 1), bss->ie_len);
  425. wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
  426. ((u8 *) bss + 1) + bss->ie_len,
  427. bss->beacon_ie_len);
  428. return 0;
  429. }
  430. group_capab = p2p_get_group_capab(p2p);
  431. addr = p2p_get_go_dev_addr(p2p);
  432. wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
  433. "group_capab=0x%x", group_capab);
  434. if (addr) {
  435. os_memcpy(go_dev_addr, addr, ETH_ALEN);
  436. wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
  437. MAC2STR(addr));
  438. } else
  439. os_memset(go_dev_addr, 0, ETH_ALEN);
  440. wpabuf_free(p2p);
  441. wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
  442. "go_dev_addr=" MACSTR,
  443. MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
  444. return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
  445. }
  446. static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
  447. struct wpa_ssid *ssid,
  448. const u8 *go_dev_addr)
  449. {
  450. struct wpa_ssid *s;
  451. int changed = 0;
  452. wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
  453. "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
  454. for (s = wpa_s->conf->ssid; s; s = s->next) {
  455. if (s->disabled == 2 &&
  456. os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
  457. s->ssid_len == ssid->ssid_len &&
  458. os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
  459. break;
  460. }
  461. if (s) {
  462. wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
  463. "entry");
  464. if (ssid->passphrase && !s->passphrase)
  465. changed = 1;
  466. else if (ssid->passphrase && s->passphrase &&
  467. os_strcmp(ssid->passphrase, s->passphrase) != 0)
  468. changed = 1;
  469. } else {
  470. wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
  471. "entry");
  472. changed = 1;
  473. s = wpa_config_add_network(wpa_s->conf);
  474. if (s == NULL)
  475. return -1;
  476. /*
  477. * Instead of network_added we emit persistent_group_added
  478. * notification. Also to keep the defense checks in
  479. * persistent_group obj registration method, we set the
  480. * relevant flags in s to designate it as a persistent group.
  481. */
  482. s->p2p_group = 1;
  483. s->p2p_persistent_group = 1;
  484. wpas_notify_persistent_group_added(wpa_s, s);
  485. wpa_config_set_network_defaults(s);
  486. }
  487. s->p2p_group = 1;
  488. s->p2p_persistent_group = 1;
  489. s->disabled = 2;
  490. s->bssid_set = 1;
  491. os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
  492. s->mode = ssid->mode;
  493. s->auth_alg = WPA_AUTH_ALG_OPEN;
  494. s->key_mgmt = WPA_KEY_MGMT_PSK;
  495. s->proto = WPA_PROTO_RSN;
  496. s->pairwise_cipher = WPA_CIPHER_CCMP;
  497. s->export_keys = 1;
  498. if (ssid->passphrase) {
  499. os_free(s->passphrase);
  500. s->passphrase = os_strdup(ssid->passphrase);
  501. }
  502. if (ssid->psk_set) {
  503. s->psk_set = 1;
  504. os_memcpy(s->psk, ssid->psk, 32);
  505. }
  506. if (s->passphrase && !s->psk_set)
  507. wpa_config_update_psk(s);
  508. if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
  509. os_free(s->ssid);
  510. s->ssid = os_malloc(ssid->ssid_len);
  511. }
  512. if (s->ssid) {
  513. s->ssid_len = ssid->ssid_len;
  514. os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
  515. }
  516. #ifndef CONFIG_NO_CONFIG_WRITE
  517. if (changed && wpa_s->conf->update_config &&
  518. wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  519. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  520. }
  521. #endif /* CONFIG_NO_CONFIG_WRITE */
  522. return s->id;
  523. }
  524. static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
  525. const u8 *addr)
  526. {
  527. struct wpa_ssid *ssid, *s;
  528. u8 *n;
  529. size_t i;
  530. int found = 0;
  531. ssid = wpa_s->current_ssid;
  532. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  533. !ssid->p2p_persistent_group)
  534. return;
  535. for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
  536. if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
  537. continue;
  538. if (s->ssid_len == ssid->ssid_len &&
  539. os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
  540. break;
  541. }
  542. if (s == NULL)
  543. return;
  544. for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
  545. if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
  546. ETH_ALEN) != 0)
  547. continue;
  548. if (i == s->num_p2p_clients - 1)
  549. return; /* already the most recent entry */
  550. /* move the entry to mark it most recent */
  551. os_memmove(s->p2p_client_list + i * ETH_ALEN,
  552. s->p2p_client_list + (i + 1) * ETH_ALEN,
  553. (s->num_p2p_clients - i - 1) * ETH_ALEN);
  554. os_memcpy(s->p2p_client_list +
  555. (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
  556. found = 1;
  557. break;
  558. }
  559. if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
  560. n = os_realloc_array(s->p2p_client_list,
  561. s->num_p2p_clients + 1, ETH_ALEN);
  562. if (n == NULL)
  563. return;
  564. os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
  565. s->p2p_client_list = n;
  566. s->num_p2p_clients++;
  567. } else if (!found) {
  568. /* Not enough room for an additional entry - drop the oldest
  569. * entry */
  570. os_memmove(s->p2p_client_list,
  571. s->p2p_client_list + ETH_ALEN,
  572. (s->num_p2p_clients - 1) * ETH_ALEN);
  573. os_memcpy(s->p2p_client_list +
  574. (s->num_p2p_clients - 1) * ETH_ALEN,
  575. addr, ETH_ALEN);
  576. }
  577. #ifndef CONFIG_NO_CONFIG_WRITE
  578. if (wpa_s->parent->conf->update_config &&
  579. wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
  580. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  581. #endif /* CONFIG_NO_CONFIG_WRITE */
  582. }
  583. static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
  584. int success)
  585. {
  586. struct wpa_ssid *ssid;
  587. const char *ssid_txt;
  588. int client;
  589. int persistent;
  590. u8 go_dev_addr[ETH_ALEN];
  591. int network_id = -1;
  592. /*
  593. * This callback is likely called for the main interface. Update wpa_s
  594. * to use the group interface if a new interface was created for the
  595. * group.
  596. */
  597. if (wpa_s->global->p2p_group_formation)
  598. wpa_s = wpa_s->global->p2p_group_formation;
  599. wpa_s->global->p2p_group_formation = NULL;
  600. wpa_s->p2p_in_provisioning = 0;
  601. if (!success) {
  602. wpa_msg_global(wpa_s->parent, MSG_INFO,
  603. P2P_EVENT_GROUP_FORMATION_FAILURE);
  604. wpas_p2p_group_delete(wpa_s,
  605. P2P_GROUP_REMOVAL_FORMATION_FAILED);
  606. return;
  607. }
  608. wpa_msg_global(wpa_s->parent, MSG_INFO,
  609. P2P_EVENT_GROUP_FORMATION_SUCCESS);
  610. ssid = wpa_s->current_ssid;
  611. if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
  612. ssid->mode = WPAS_MODE_P2P_GO;
  613. p2p_group_notif_formation_done(wpa_s->p2p_group);
  614. wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
  615. }
  616. persistent = 0;
  617. if (ssid) {
  618. ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
  619. client = ssid->mode == WPAS_MODE_INFRA;
  620. if (ssid->mode == WPAS_MODE_P2P_GO) {
  621. persistent = ssid->p2p_persistent_group;
  622. os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
  623. ETH_ALEN);
  624. } else
  625. persistent = wpas_p2p_persistent_group(wpa_s,
  626. go_dev_addr,
  627. ssid->ssid,
  628. ssid->ssid_len);
  629. } else {
  630. ssid_txt = "";
  631. client = wpa_s->p2p_group_interface ==
  632. P2P_GROUP_INTERFACE_CLIENT;
  633. os_memset(go_dev_addr, 0, ETH_ALEN);
  634. }
  635. wpa_s->show_group_started = 0;
  636. if (client) {
  637. /*
  638. * Indicate event only after successfully completed 4-way
  639. * handshake, i.e., when the interface is ready for data
  640. * packets.
  641. */
  642. wpa_s->show_group_started = 1;
  643. } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
  644. char psk[65];
  645. wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
  646. wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  647. "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
  648. MACSTR "%s",
  649. wpa_s->ifname, ssid_txt, ssid->frequency, psk,
  650. MAC2STR(go_dev_addr),
  651. persistent ? " [PERSISTENT]" : "");
  652. wpas_p2p_cross_connect_setup(wpa_s);
  653. wpas_p2p_set_group_idle_timeout(wpa_s);
  654. } else {
  655. wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  656. "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
  657. "go_dev_addr=" MACSTR "%s",
  658. wpa_s->ifname, ssid_txt,
  659. ssid ? ssid->frequency : 0,
  660. ssid && ssid->passphrase ? ssid->passphrase : "",
  661. MAC2STR(go_dev_addr),
  662. persistent ? " [PERSISTENT]" : "");
  663. wpas_p2p_cross_connect_setup(wpa_s);
  664. wpas_p2p_set_group_idle_timeout(wpa_s);
  665. }
  666. if (persistent)
  667. network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
  668. ssid, go_dev_addr);
  669. if (network_id < 0 && ssid)
  670. network_id = ssid->id;
  671. if (!client)
  672. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
  673. }
  674. static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
  675. unsigned int freq,
  676. const u8 *dst, const u8 *src,
  677. const u8 *bssid,
  678. const u8 *data, size_t data_len,
  679. enum offchannel_send_action_result
  680. result)
  681. {
  682. enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
  683. if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
  684. return;
  685. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  686. return;
  687. switch (result) {
  688. case OFFCHANNEL_SEND_ACTION_SUCCESS:
  689. res = P2P_SEND_ACTION_SUCCESS;
  690. break;
  691. case OFFCHANNEL_SEND_ACTION_NO_ACK:
  692. res = P2P_SEND_ACTION_NO_ACK;
  693. break;
  694. case OFFCHANNEL_SEND_ACTION_FAILED:
  695. res = P2P_SEND_ACTION_FAILED;
  696. break;
  697. }
  698. p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
  699. if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
  700. wpa_s->pending_pd_before_join &&
  701. (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
  702. os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
  703. wpa_s->p2p_fallback_to_go_neg) {
  704. wpa_s->pending_pd_before_join = 0;
  705. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
  706. "during p2p_connect-auto");
  707. wpas_p2p_fallback_to_go_neg(wpa_s, 0);
  708. return;
  709. }
  710. }
  711. static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
  712. const u8 *src, const u8 *bssid, const u8 *buf,
  713. size_t len, unsigned int wait_time)
  714. {
  715. struct wpa_supplicant *wpa_s = ctx;
  716. return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
  717. wait_time,
  718. wpas_p2p_send_action_tx_status, 1);
  719. }
  720. static void wpas_send_action_done(void *ctx)
  721. {
  722. struct wpa_supplicant *wpa_s = ctx;
  723. offchannel_send_action_done(wpa_s);
  724. }
  725. static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
  726. struct p2p_go_neg_results *params)
  727. {
  728. if (wpa_s->go_params == NULL) {
  729. wpa_s->go_params = os_malloc(sizeof(*params));
  730. if (wpa_s->go_params == NULL)
  731. return -1;
  732. }
  733. os_memcpy(wpa_s->go_params, params, sizeof(*params));
  734. return 0;
  735. }
  736. static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
  737. struct p2p_go_neg_results *res)
  738. {
  739. wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
  740. MAC2STR(res->peer_interface_addr));
  741. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
  742. res->ssid, res->ssid_len);
  743. wpa_supplicant_ap_deinit(wpa_s);
  744. wpas_copy_go_neg_results(wpa_s, res);
  745. if (res->wps_method == WPS_PBC)
  746. wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
  747. else {
  748. u16 dev_pw_id = DEV_PW_DEFAULT;
  749. if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
  750. dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
  751. wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
  752. wpa_s->p2p_pin, 1, dev_pw_id);
  753. }
  754. }
  755. static void p2p_go_configured(void *ctx, void *data)
  756. {
  757. struct wpa_supplicant *wpa_s = ctx;
  758. struct p2p_go_neg_results *params = data;
  759. struct wpa_ssid *ssid;
  760. int network_id = -1;
  761. ssid = wpa_s->current_ssid;
  762. if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
  763. wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
  764. if (wpa_s->global->p2p_group_formation == wpa_s)
  765. wpa_s->global->p2p_group_formation = NULL;
  766. if (os_strlen(params->passphrase) > 0) {
  767. wpa_msg_global(wpa_s->parent, MSG_INFO,
  768. P2P_EVENT_GROUP_STARTED
  769. "%s GO ssid=\"%s\" freq=%d "
  770. "passphrase=\"%s\" go_dev_addr=" MACSTR
  771. "%s", wpa_s->ifname,
  772. wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
  773. ssid->frequency, params->passphrase,
  774. MAC2STR(wpa_s->global->p2p_dev_addr),
  775. params->persistent_group ?
  776. " [PERSISTENT]" : "");
  777. } else {
  778. char psk[65];
  779. wpa_snprintf_hex(psk, sizeof(psk), params->psk,
  780. sizeof(params->psk));
  781. wpa_msg_global(wpa_s->parent, MSG_INFO,
  782. P2P_EVENT_GROUP_STARTED
  783. "%s GO ssid=\"%s\" freq=%d psk=%s "
  784. "go_dev_addr=" MACSTR "%s",
  785. wpa_s->ifname,
  786. wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
  787. ssid->frequency, psk,
  788. MAC2STR(wpa_s->global->p2p_dev_addr),
  789. params->persistent_group ?
  790. " [PERSISTENT]" : "");
  791. }
  792. if (params->persistent_group)
  793. network_id = wpas_p2p_store_persistent_group(
  794. wpa_s->parent, ssid,
  795. wpa_s->global->p2p_dev_addr);
  796. if (network_id < 0)
  797. network_id = ssid->id;
  798. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
  799. wpas_p2p_cross_connect_setup(wpa_s);
  800. wpas_p2p_set_group_idle_timeout(wpa_s);
  801. return;
  802. }
  803. wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
  804. if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
  805. params->peer_interface_addr)) {
  806. wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
  807. "filtering");
  808. return;
  809. }
  810. if (params->wps_method == WPS_PBC)
  811. wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
  812. params->peer_device_addr);
  813. else if (wpa_s->p2p_pin[0])
  814. wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
  815. wpa_s->p2p_pin, NULL, 0, 0);
  816. os_free(wpa_s->go_params);
  817. wpa_s->go_params = NULL;
  818. }
  819. static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
  820. struct p2p_go_neg_results *params,
  821. int group_formation)
  822. {
  823. struct wpa_ssid *ssid;
  824. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
  825. if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
  826. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
  827. "results");
  828. return;
  829. }
  830. ssid = wpa_config_add_network(wpa_s->conf);
  831. if (ssid == NULL) {
  832. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
  833. return;
  834. }
  835. wpa_s->show_group_started = 0;
  836. wpa_config_set_network_defaults(ssid);
  837. ssid->temporary = 1;
  838. ssid->p2p_group = 1;
  839. ssid->p2p_persistent_group = params->persistent_group;
  840. ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
  841. WPAS_MODE_P2P_GO;
  842. ssid->frequency = params->freq;
  843. ssid->ht40 = params->ht40;
  844. ssid->ssid = os_zalloc(params->ssid_len + 1);
  845. if (ssid->ssid) {
  846. os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
  847. ssid->ssid_len = params->ssid_len;
  848. }
  849. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  850. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  851. ssid->proto = WPA_PROTO_RSN;
  852. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  853. if (os_strlen(params->passphrase) > 0) {
  854. ssid->passphrase = os_strdup(params->passphrase);
  855. if (ssid->passphrase == NULL) {
  856. wpa_msg_global(wpa_s, MSG_ERROR,
  857. "P2P: Failed to copy passphrase for GO");
  858. wpa_config_remove_network(wpa_s->conf, ssid->id);
  859. return;
  860. }
  861. } else
  862. ssid->passphrase = NULL;
  863. ssid->psk_set = params->psk_set;
  864. if (ssid->psk_set)
  865. os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
  866. else if (ssid->passphrase)
  867. wpa_config_update_psk(ssid);
  868. ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
  869. wpa_s->ap_configured_cb = p2p_go_configured;
  870. wpa_s->ap_configured_cb_ctx = wpa_s;
  871. wpa_s->ap_configured_cb_data = wpa_s->go_params;
  872. wpa_s->connect_without_scan = ssid;
  873. wpa_s->reassociate = 1;
  874. wpa_s->disconnected = 0;
  875. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
  876. "start GO)");
  877. wpa_supplicant_req_scan(wpa_s, 0, 0);
  878. }
  879. static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
  880. const struct wpa_supplicant *src)
  881. {
  882. struct wpa_config *d;
  883. const struct wpa_config *s;
  884. d = dst->conf;
  885. s = src->conf;
  886. #define C(n) if (s->n) d->n = os_strdup(s->n)
  887. C(device_name);
  888. C(manufacturer);
  889. C(model_name);
  890. C(model_number);
  891. C(serial_number);
  892. C(config_methods);
  893. #undef C
  894. os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
  895. os_memcpy(d->sec_device_type, s->sec_device_type,
  896. sizeof(d->sec_device_type));
  897. d->num_sec_device_types = s->num_sec_device_types;
  898. d->p2p_group_idle = s->p2p_group_idle;
  899. d->p2p_intra_bss = s->p2p_intra_bss;
  900. d->persistent_reconnect = s->persistent_reconnect;
  901. d->max_num_sta = s->max_num_sta;
  902. d->pbc_in_m1 = s->pbc_in_m1;
  903. d->ignore_old_scan_res = s->ignore_old_scan_res;
  904. d->beacon_int = s->beacon_int;
  905. }
  906. static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
  907. char *ifname, size_t len)
  908. {
  909. char *ifname_ptr = wpa_s->ifname;
  910. if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
  911. os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
  912. ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
  913. }
  914. os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
  915. if (os_strlen(ifname) >= IFNAMSIZ &&
  916. os_strlen(wpa_s->ifname) < IFNAMSIZ) {
  917. /* Try to avoid going over the IFNAMSIZ length limit */
  918. os_snprintf(ifname, sizeof(ifname), "p2p-%d",
  919. wpa_s->p2p_group_idx);
  920. }
  921. }
  922. static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
  923. enum wpa_driver_if_type type)
  924. {
  925. char ifname[120], force_ifname[120];
  926. if (wpa_s->pending_interface_name[0]) {
  927. wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
  928. "- skip creation of a new one");
  929. if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
  930. wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
  931. "unknown?! ifname='%s'",
  932. wpa_s->pending_interface_name);
  933. return -1;
  934. }
  935. return 0;
  936. }
  937. wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
  938. force_ifname[0] = '\0';
  939. wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
  940. ifname);
  941. wpa_s->p2p_group_idx++;
  942. wpa_s->pending_interface_type = type;
  943. if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
  944. wpa_s->pending_interface_addr, NULL) < 0) {
  945. wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
  946. "interface");
  947. return -1;
  948. }
  949. if (force_ifname[0]) {
  950. wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
  951. force_ifname);
  952. os_strlcpy(wpa_s->pending_interface_name, force_ifname,
  953. sizeof(wpa_s->pending_interface_name));
  954. } else
  955. os_strlcpy(wpa_s->pending_interface_name, ifname,
  956. sizeof(wpa_s->pending_interface_name));
  957. wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
  958. MACSTR, wpa_s->pending_interface_name,
  959. MAC2STR(wpa_s->pending_interface_addr));
  960. return 0;
  961. }
  962. static void wpas_p2p_remove_pending_group_interface(
  963. struct wpa_supplicant *wpa_s)
  964. {
  965. if (!wpa_s->pending_interface_name[0] ||
  966. is_zero_ether_addr(wpa_s->pending_interface_addr))
  967. return; /* No pending virtual interface */
  968. wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
  969. wpa_s->pending_interface_name);
  970. wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
  971. wpa_s->pending_interface_name);
  972. os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
  973. wpa_s->pending_interface_name[0] = '\0';
  974. }
  975. static struct wpa_supplicant *
  976. wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
  977. {
  978. struct wpa_interface iface;
  979. struct wpa_supplicant *group_wpa_s;
  980. if (!wpa_s->pending_interface_name[0]) {
  981. wpa_printf(MSG_ERROR, "P2P: No pending group interface");
  982. if (!wpas_p2p_create_iface(wpa_s))
  983. return NULL;
  984. /*
  985. * Something has forced us to remove the pending interface; try
  986. * to create a new one and hope for the best that we will get
  987. * the same local address.
  988. */
  989. if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
  990. WPA_IF_P2P_CLIENT) < 0)
  991. return NULL;
  992. }
  993. os_memset(&iface, 0, sizeof(iface));
  994. iface.ifname = wpa_s->pending_interface_name;
  995. iface.driver = wpa_s->driver->name;
  996. if (wpa_s->conf->ctrl_interface == NULL &&
  997. wpa_s->parent != wpa_s &&
  998. wpa_s->p2p_mgmt &&
  999. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
  1000. iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
  1001. else
  1002. iface.ctrl_interface = wpa_s->conf->ctrl_interface;
  1003. iface.driver_param = wpa_s->conf->driver_param;
  1004. group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
  1005. if (group_wpa_s == NULL) {
  1006. wpa_printf(MSG_ERROR, "P2P: Failed to create new "
  1007. "wpa_supplicant interface");
  1008. return NULL;
  1009. }
  1010. wpa_s->pending_interface_name[0] = '\0';
  1011. group_wpa_s->parent = wpa_s;
  1012. group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
  1013. P2P_GROUP_INTERFACE_CLIENT;
  1014. wpa_s->global->p2p_group_formation = group_wpa_s;
  1015. wpas_p2p_clone_config(group_wpa_s, wpa_s);
  1016. return group_wpa_s;
  1017. }
  1018. static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
  1019. void *timeout_ctx)
  1020. {
  1021. struct wpa_supplicant *wpa_s = eloop_ctx;
  1022. wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
  1023. wpas_p2p_group_formation_failed(wpa_s);
  1024. }
  1025. void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
  1026. {
  1027. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  1028. wpa_s->parent, NULL);
  1029. if (wpa_s->global->p2p)
  1030. p2p_group_formation_failed(wpa_s->global->p2p);
  1031. else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  1032. wpa_drv_p2p_group_formation_failed(wpa_s);
  1033. wpas_group_formation_completed(wpa_s, 0);
  1034. }
  1035. void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
  1036. {
  1037. struct wpa_supplicant *wpa_s = ctx;
  1038. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  1039. wpa_drv_cancel_remain_on_channel(wpa_s);
  1040. wpa_s->off_channel_freq = 0;
  1041. wpa_s->roc_waiting_drv_freq = 0;
  1042. }
  1043. if (res->status) {
  1044. wpa_msg_global(wpa_s, MSG_INFO,
  1045. P2P_EVENT_GO_NEG_FAILURE "status=%d",
  1046. res->status);
  1047. wpas_notify_p2p_go_neg_completed(wpa_s, res);
  1048. wpas_p2p_remove_pending_group_interface(wpa_s);
  1049. return;
  1050. }
  1051. if (wpa_s->p2p_go_ht40)
  1052. res->ht40 = 1;
  1053. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
  1054. wpas_notify_p2p_go_neg_completed(wpa_s, res);
  1055. if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
  1056. struct wpa_ssid *ssid;
  1057. ssid = wpa_config_get_network(wpa_s->conf,
  1058. wpa_s->p2p_persistent_id);
  1059. if (ssid && ssid->disabled == 2 &&
  1060. ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
  1061. size_t len = os_strlen(ssid->passphrase);
  1062. wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
  1063. "on requested persistent group");
  1064. os_memcpy(res->passphrase, ssid->passphrase, len);
  1065. res->passphrase[len] = '\0';
  1066. }
  1067. }
  1068. if (wpa_s->create_p2p_iface) {
  1069. struct wpa_supplicant *group_wpa_s =
  1070. wpas_p2p_init_group_interface(wpa_s, res->role_go);
  1071. if (group_wpa_s == NULL) {
  1072. wpas_p2p_remove_pending_group_interface(wpa_s);
  1073. return;
  1074. }
  1075. if (group_wpa_s != wpa_s) {
  1076. os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
  1077. sizeof(group_wpa_s->p2p_pin));
  1078. group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
  1079. }
  1080. os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
  1081. wpa_s->pending_interface_name[0] = '\0';
  1082. group_wpa_s->p2p_in_provisioning = 1;
  1083. if (res->role_go)
  1084. wpas_start_wps_go(group_wpa_s, res, 1);
  1085. else
  1086. wpas_start_wps_enrollee(group_wpa_s, res);
  1087. } else {
  1088. wpa_s->p2p_in_provisioning = 1;
  1089. wpa_s->global->p2p_group_formation = wpa_s;
  1090. if (res->role_go)
  1091. wpas_start_wps_go(wpa_s, res, 1);
  1092. else
  1093. wpas_start_wps_enrollee(ctx, res);
  1094. }
  1095. wpa_s->p2p_long_listen = 0;
  1096. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  1097. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  1098. eloop_register_timeout(15 + res->peer_config_timeout / 100,
  1099. (res->peer_config_timeout % 100) * 10000,
  1100. wpas_p2p_group_formation_timeout, wpa_s, NULL);
  1101. }
  1102. void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
  1103. {
  1104. struct wpa_supplicant *wpa_s = ctx;
  1105. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
  1106. " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
  1107. wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
  1108. }
  1109. void wpas_dev_found(void *ctx, const u8 *addr,
  1110. const struct p2p_peer_info *info,
  1111. int new_device)
  1112. {
  1113. #ifndef CONFIG_NO_STDOUT_DEBUG
  1114. struct wpa_supplicant *wpa_s = ctx;
  1115. char devtype[WPS_DEV_TYPE_BUFSIZE];
  1116. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
  1117. " p2p_dev_addr=" MACSTR
  1118. " pri_dev_type=%s name='%s' config_methods=0x%x "
  1119. "dev_capab=0x%x group_capab=0x%x",
  1120. MAC2STR(addr), MAC2STR(info->p2p_device_addr),
  1121. wps_dev_type_bin2str(info->pri_dev_type, devtype,
  1122. sizeof(devtype)),
  1123. info->device_name, info->config_methods,
  1124. info->dev_capab, info->group_capab);
  1125. #endif /* CONFIG_NO_STDOUT_DEBUG */
  1126. wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
  1127. }
  1128. static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
  1129. {
  1130. struct wpa_supplicant *wpa_s = ctx;
  1131. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
  1132. "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
  1133. wpas_notify_p2p_device_lost(wpa_s, dev_addr);
  1134. }
  1135. static void wpas_find_stopped(void *ctx)
  1136. {
  1137. struct wpa_supplicant *wpa_s = ctx;
  1138. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
  1139. }
  1140. static int wpas_start_listen(void *ctx, unsigned int freq,
  1141. unsigned int duration,
  1142. const struct wpabuf *probe_resp_ie)
  1143. {
  1144. struct wpa_supplicant *wpa_s = ctx;
  1145. wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
  1146. if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
  1147. wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
  1148. "report received Probe Request frames");
  1149. return -1;
  1150. }
  1151. wpa_s->pending_listen_freq = freq;
  1152. wpa_s->pending_listen_duration = duration;
  1153. if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
  1154. wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
  1155. "to remain on channel (%u MHz) for Listen "
  1156. "state", freq);
  1157. wpa_s->pending_listen_freq = 0;
  1158. return -1;
  1159. }
  1160. wpa_s->off_channel_freq = 0;
  1161. wpa_s->roc_waiting_drv_freq = freq;
  1162. return 0;
  1163. }
  1164. static void wpas_stop_listen(void *ctx)
  1165. {
  1166. struct wpa_supplicant *wpa_s = ctx;
  1167. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  1168. wpa_drv_cancel_remain_on_channel(wpa_s);
  1169. wpa_s->off_channel_freq = 0;
  1170. wpa_s->roc_waiting_drv_freq = 0;
  1171. }
  1172. wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
  1173. wpa_drv_probe_req_report(wpa_s, 0);
  1174. }
  1175. static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
  1176. {
  1177. struct wpa_supplicant *wpa_s = ctx;
  1178. return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
  1179. }
  1180. /*
  1181. * DNS Header section is used only to calculate compression pointers, so the
  1182. * contents of this data does not matter, but the length needs to be reserved
  1183. * in the virtual packet.
  1184. */
  1185. #define DNS_HEADER_LEN 12
  1186. /*
  1187. * 27-octet in-memory packet from P2P specification containing two implied
  1188. * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
  1189. */
  1190. #define P2P_SD_IN_MEMORY_LEN 27
  1191. static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
  1192. u8 **spos, const u8 *end)
  1193. {
  1194. while (*spos < end) {
  1195. u8 val = ((*spos)[0] & 0xc0) >> 6;
  1196. int len;
  1197. if (val == 1 || val == 2) {
  1198. /* These are reserved values in RFC 1035 */
  1199. wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
  1200. "sequence starting with 0x%x", val);
  1201. return -1;
  1202. }
  1203. if (val == 3) {
  1204. u16 offset;
  1205. u8 *spos_tmp;
  1206. /* Offset */
  1207. if (*spos + 2 > end) {
  1208. wpa_printf(MSG_DEBUG, "P2P: No room for full "
  1209. "DNS offset field");
  1210. return -1;
  1211. }
  1212. offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
  1213. if (offset >= *spos - start) {
  1214. wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
  1215. "pointer offset %u", offset);
  1216. return -1;
  1217. }
  1218. (*spos) += 2;
  1219. spos_tmp = start + offset;
  1220. return p2p_sd_dns_uncompress_label(upos, uend, start,
  1221. &spos_tmp,
  1222. *spos - 2);
  1223. }
  1224. /* Label */
  1225. len = (*spos)[0] & 0x3f;
  1226. if (len == 0)
  1227. return 0;
  1228. (*spos)++;
  1229. if (*spos + len > end) {
  1230. wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
  1231. "sequence - no room for label with length "
  1232. "%u", len);
  1233. return -1;
  1234. }
  1235. if (*upos + len + 2 > uend)
  1236. return -2;
  1237. os_memcpy(*upos, *spos, len);
  1238. *spos += len;
  1239. *upos += len;
  1240. (*upos)[0] = '.';
  1241. (*upos)++;
  1242. (*upos)[0] = '\0';
  1243. }
  1244. return 0;
  1245. }
  1246. /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
  1247. * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
  1248. * not large enough */
  1249. static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
  1250. size_t msg_len, size_t offset)
  1251. {
  1252. /* 27-octet in-memory packet from P2P specification */
  1253. const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
  1254. "\x04_udp\xC0\x11\x00\x0C\x00\x01";
  1255. u8 *tmp, *end, *spos;
  1256. char *upos, *uend;
  1257. int ret = 0;
  1258. if (buf_len < 2)
  1259. return -1;
  1260. if (offset > msg_len)
  1261. return -1;
  1262. tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
  1263. if (tmp == NULL)
  1264. return -1;
  1265. spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
  1266. end = spos + msg_len;
  1267. spos += offset;
  1268. os_memset(tmp, 0, DNS_HEADER_LEN);
  1269. os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
  1270. os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
  1271. upos = buf;
  1272. uend = buf + buf_len;
  1273. ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
  1274. if (ret) {
  1275. os_free(tmp);
  1276. return ret;
  1277. }
  1278. if (upos == buf) {
  1279. upos[0] = '.';
  1280. upos[1] = '\0';
  1281. } else if (upos[-1] == '.')
  1282. upos[-1] = '\0';
  1283. os_free(tmp);
  1284. return 0;
  1285. }
  1286. static struct p2p_srv_bonjour *
  1287. wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
  1288. const struct wpabuf *query)
  1289. {
  1290. struct p2p_srv_bonjour *bsrv;
  1291. size_t len;
  1292. len = wpabuf_len(query);
  1293. dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
  1294. struct p2p_srv_bonjour, list) {
  1295. if (len == wpabuf_len(bsrv->query) &&
  1296. os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
  1297. len) == 0)
  1298. return bsrv;
  1299. }
  1300. return NULL;
  1301. }
  1302. static struct p2p_srv_upnp *
  1303. wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
  1304. const char *service)
  1305. {
  1306. struct p2p_srv_upnp *usrv;
  1307. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  1308. struct p2p_srv_upnp, list) {
  1309. if (version == usrv->version &&
  1310. os_strcmp(service, usrv->service) == 0)
  1311. return usrv;
  1312. }
  1313. return NULL;
  1314. }
  1315. static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
  1316. u8 srv_trans_id)
  1317. {
  1318. u8 *len_pos;
  1319. if (wpabuf_tailroom(resp) < 5)
  1320. return;
  1321. /* Length (to be filled) */
  1322. len_pos = wpabuf_put(resp, 2);
  1323. wpabuf_put_u8(resp, srv_proto);
  1324. wpabuf_put_u8(resp, srv_trans_id);
  1325. /* Status Code */
  1326. wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
  1327. /* Response Data: empty */
  1328. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1329. }
  1330. static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
  1331. struct wpabuf *resp, u8 srv_trans_id)
  1332. {
  1333. struct p2p_srv_bonjour *bsrv;
  1334. u8 *len_pos;
  1335. wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
  1336. if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  1337. wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
  1338. return;
  1339. }
  1340. dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
  1341. struct p2p_srv_bonjour, list) {
  1342. if (wpabuf_tailroom(resp) <
  1343. 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
  1344. return;
  1345. /* Length (to be filled) */
  1346. len_pos = wpabuf_put(resp, 2);
  1347. wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
  1348. wpabuf_put_u8(resp, srv_trans_id);
  1349. /* Status Code */
  1350. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1351. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
  1352. wpabuf_head(bsrv->resp),
  1353. wpabuf_len(bsrv->resp));
  1354. /* Response Data */
  1355. wpabuf_put_buf(resp, bsrv->query); /* Key */
  1356. wpabuf_put_buf(resp, bsrv->resp); /* Value */
  1357. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  1358. 2);
  1359. }
  1360. }
  1361. static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
  1362. size_t query_len)
  1363. {
  1364. char str_rx[256], str_srv[256];
  1365. if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
  1366. return 0; /* Too short to include DNS Type and Version */
  1367. if (os_memcmp(query + query_len - 3,
  1368. wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
  1369. 3) != 0)
  1370. return 0; /* Mismatch in DNS Type or Version */
  1371. if (query_len == wpabuf_len(bsrv->query) &&
  1372. os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
  1373. return 1; /* Binary match */
  1374. if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
  1375. 0))
  1376. return 0; /* Failed to uncompress query */
  1377. if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
  1378. wpabuf_head(bsrv->query),
  1379. wpabuf_len(bsrv->query) - 3, 0))
  1380. return 0; /* Failed to uncompress service */
  1381. return os_strcmp(str_rx, str_srv) == 0;
  1382. }
  1383. static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
  1384. struct wpabuf *resp, u8 srv_trans_id,
  1385. const u8 *query, size_t query_len)
  1386. {
  1387. struct p2p_srv_bonjour *bsrv;
  1388. u8 *len_pos;
  1389. int matches = 0;
  1390. wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
  1391. query, query_len);
  1392. if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  1393. wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
  1394. wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
  1395. srv_trans_id);
  1396. return;
  1397. }
  1398. if (query_len == 0) {
  1399. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  1400. return;
  1401. }
  1402. dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
  1403. struct p2p_srv_bonjour, list) {
  1404. if (!match_bonjour_query(bsrv, query, query_len))
  1405. continue;
  1406. if (wpabuf_tailroom(resp) <
  1407. 5 + query_len + wpabuf_len(bsrv->resp))
  1408. return;
  1409. matches++;
  1410. /* Length (to be filled) */
  1411. len_pos = wpabuf_put(resp, 2);
  1412. wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
  1413. wpabuf_put_u8(resp, srv_trans_id);
  1414. /* Status Code */
  1415. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1416. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
  1417. wpabuf_head(bsrv->resp),
  1418. wpabuf_len(bsrv->resp));
  1419. /* Response Data */
  1420. wpabuf_put_data(resp, query, query_len); /* Key */
  1421. wpabuf_put_buf(resp, bsrv->resp); /* Value */
  1422. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1423. }
  1424. if (matches == 0) {
  1425. wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
  1426. "available");
  1427. if (wpabuf_tailroom(resp) < 5)
  1428. return;
  1429. /* Length (to be filled) */
  1430. len_pos = wpabuf_put(resp, 2);
  1431. wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
  1432. wpabuf_put_u8(resp, srv_trans_id);
  1433. /* Status Code */
  1434. wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
  1435. /* Response Data: empty */
  1436. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  1437. 2);
  1438. }
  1439. }
  1440. static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
  1441. struct wpabuf *resp, u8 srv_trans_id)
  1442. {
  1443. struct p2p_srv_upnp *usrv;
  1444. u8 *len_pos;
  1445. wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
  1446. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
  1447. wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
  1448. return;
  1449. }
  1450. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  1451. struct p2p_srv_upnp, list) {
  1452. if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
  1453. return;
  1454. /* Length (to be filled) */
  1455. len_pos = wpabuf_put(resp, 2);
  1456. wpabuf_put_u8(resp, P2P_SERV_UPNP);
  1457. wpabuf_put_u8(resp, srv_trans_id);
  1458. /* Status Code */
  1459. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1460. /* Response Data */
  1461. wpabuf_put_u8(resp, usrv->version);
  1462. wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
  1463. usrv->service);
  1464. wpabuf_put_str(resp, usrv->service);
  1465. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  1466. 2);
  1467. }
  1468. }
  1469. static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
  1470. struct wpabuf *resp, u8 srv_trans_id,
  1471. const u8 *query, size_t query_len)
  1472. {
  1473. struct p2p_srv_upnp *usrv;
  1474. u8 *len_pos;
  1475. u8 version;
  1476. char *str;
  1477. int count = 0;
  1478. wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
  1479. query, query_len);
  1480. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
  1481. wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
  1482. wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
  1483. srv_trans_id);
  1484. return;
  1485. }
  1486. if (query_len == 0) {
  1487. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  1488. return;
  1489. }
  1490. if (wpabuf_tailroom(resp) < 5)
  1491. return;
  1492. /* Length (to be filled) */
  1493. len_pos = wpabuf_put(resp, 2);
  1494. wpabuf_put_u8(resp, P2P_SERV_UPNP);
  1495. wpabuf_put_u8(resp, srv_trans_id);
  1496. version = query[0];
  1497. str = os_malloc(query_len);
  1498. if (str == NULL)
  1499. return;
  1500. os_memcpy(str, query + 1, query_len - 1);
  1501. str[query_len - 1] = '\0';
  1502. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  1503. struct p2p_srv_upnp, list) {
  1504. if (version != usrv->version)
  1505. continue;
  1506. if (os_strcmp(str, "ssdp:all") != 0 &&
  1507. os_strstr(usrv->service, str) == NULL)
  1508. continue;
  1509. if (wpabuf_tailroom(resp) < 2)
  1510. break;
  1511. if (count == 0) {
  1512. /* Status Code */
  1513. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1514. /* Response Data */
  1515. wpabuf_put_u8(resp, version);
  1516. } else
  1517. wpabuf_put_u8(resp, ',');
  1518. count++;
  1519. wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
  1520. usrv->service);
  1521. if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
  1522. break;
  1523. wpabuf_put_str(resp, usrv->service);
  1524. }
  1525. os_free(str);
  1526. if (count == 0) {
  1527. wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
  1528. "available");
  1529. /* Status Code */
  1530. wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
  1531. /* Response Data: empty */
  1532. }
  1533. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1534. }
  1535. #ifdef CONFIG_WIFI_DISPLAY
  1536. static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
  1537. struct wpabuf *resp, u8 srv_trans_id,
  1538. const u8 *query, size_t query_len)
  1539. {
  1540. const u8 *pos;
  1541. u8 role;
  1542. u8 *len_pos;
  1543. wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
  1544. if (!wpa_s->global->wifi_display) {
  1545. wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
  1546. wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
  1547. srv_trans_id);
  1548. return;
  1549. }
  1550. if (query_len < 1) {
  1551. wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
  1552. "Role");
  1553. return;
  1554. }
  1555. if (wpabuf_tailroom(resp) < 5)
  1556. return;
  1557. pos = query;
  1558. role = *pos++;
  1559. wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
  1560. /* TODO: role specific handling */
  1561. /* Length (to be filled) */
  1562. len_pos = wpabuf_put(resp, 2);
  1563. wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
  1564. wpabuf_put_u8(resp, srv_trans_id);
  1565. wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
  1566. while (pos < query + query_len) {
  1567. if (*pos < MAX_WFD_SUBELEMS &&
  1568. wpa_s->global->wfd_subelem[*pos] &&
  1569. wpabuf_tailroom(resp) >=
  1570. wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
  1571. wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
  1572. "subelement %u", *pos);
  1573. wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
  1574. }
  1575. pos++;
  1576. }
  1577. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1578. }
  1579. #endif /* CONFIG_WIFI_DISPLAY */
  1580. void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
  1581. u16 update_indic, const u8 *tlvs, size_t tlvs_len)
  1582. {
  1583. struct wpa_supplicant *wpa_s = ctx;
  1584. const u8 *pos = tlvs;
  1585. const u8 *end = tlvs + tlvs_len;
  1586. const u8 *tlv_end;
  1587. u16 slen;
  1588. struct wpabuf *resp;
  1589. u8 srv_proto, srv_trans_id;
  1590. size_t buf_len;
  1591. char *buf;
  1592. wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
  1593. tlvs, tlvs_len);
  1594. buf_len = 2 * tlvs_len + 1;
  1595. buf = os_malloc(buf_len);
  1596. if (buf) {
  1597. wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
  1598. wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
  1599. MACSTR " %u %u %s",
  1600. freq, MAC2STR(sa), dialog_token, update_indic,
  1601. buf);
  1602. os_free(buf);
  1603. }
  1604. if (wpa_s->p2p_sd_over_ctrl_iface) {
  1605. wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
  1606. update_indic, tlvs, tlvs_len);
  1607. return; /* to be processed by an external program */
  1608. }
  1609. resp = wpabuf_alloc(10000);
  1610. if (resp == NULL)
  1611. return;
  1612. while (pos + 1 < end) {
  1613. wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
  1614. slen = WPA_GET_LE16(pos);
  1615. pos += 2;
  1616. if (pos + slen > end || slen < 2) {
  1617. wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
  1618. "length");
  1619. wpabuf_free(resp);
  1620. return;
  1621. }
  1622. tlv_end = pos + slen;
  1623. srv_proto = *pos++;
  1624. wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
  1625. srv_proto);
  1626. srv_trans_id = *pos++;
  1627. wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
  1628. srv_trans_id);
  1629. wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
  1630. pos, tlv_end - pos);
  1631. if (wpa_s->force_long_sd) {
  1632. wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
  1633. "response");
  1634. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  1635. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  1636. goto done;
  1637. }
  1638. switch (srv_proto) {
  1639. case P2P_SERV_ALL_SERVICES:
  1640. wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
  1641. "for all services");
  1642. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
  1643. dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  1644. wpa_printf(MSG_DEBUG, "P2P: No service "
  1645. "discovery protocols available");
  1646. wpas_sd_add_proto_not_avail(
  1647. resp, P2P_SERV_ALL_SERVICES,
  1648. srv_trans_id);
  1649. break;
  1650. }
  1651. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  1652. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  1653. break;
  1654. case P2P_SERV_BONJOUR:
  1655. wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
  1656. pos, tlv_end - pos);
  1657. break;
  1658. case P2P_SERV_UPNP:
  1659. wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
  1660. pos, tlv_end - pos);
  1661. break;
  1662. #ifdef CONFIG_WIFI_DISPLAY
  1663. case P2P_SERV_WIFI_DISPLAY:
  1664. wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
  1665. pos, tlv_end - pos);
  1666. break;
  1667. #endif /* CONFIG_WIFI_DISPLAY */
  1668. default:
  1669. wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
  1670. "protocol %u", srv_proto);
  1671. wpas_sd_add_proto_not_avail(resp, srv_proto,
  1672. srv_trans_id);
  1673. break;
  1674. }
  1675. pos = tlv_end;
  1676. }
  1677. done:
  1678. wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
  1679. update_indic, tlvs, tlvs_len);
  1680. wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
  1681. wpabuf_free(resp);
  1682. }
  1683. void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
  1684. const u8 *tlvs, size_t tlvs_len)
  1685. {
  1686. struct wpa_supplicant *wpa_s = ctx;
  1687. const u8 *pos = tlvs;
  1688. const u8 *end = tlvs + tlvs_len;
  1689. const u8 *tlv_end;
  1690. u16 slen;
  1691. size_t buf_len;
  1692. char *buf;
  1693. wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
  1694. tlvs, tlvs_len);
  1695. if (tlvs_len > 1500) {
  1696. /* TODO: better way for handling this */
  1697. wpa_msg_ctrl(wpa_s, MSG_INFO,
  1698. P2P_EVENT_SERV_DISC_RESP MACSTR
  1699. " %u <long response: %u bytes>",
  1700. MAC2STR(sa), update_indic,
  1701. (unsigned int) tlvs_len);
  1702. } else {
  1703. buf_len = 2 * tlvs_len + 1;
  1704. buf = os_malloc(buf_len);
  1705. if (buf) {
  1706. wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
  1707. wpa_msg_ctrl(wpa_s, MSG_INFO,
  1708. P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
  1709. MAC2STR(sa), update_indic, buf);
  1710. os_free(buf);
  1711. }
  1712. }
  1713. while (pos < end) {
  1714. u8 srv_proto, srv_trans_id, status;
  1715. wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
  1716. slen = WPA_GET_LE16(pos);
  1717. pos += 2;
  1718. if (pos + slen > end || slen < 3) {
  1719. wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
  1720. "length");
  1721. return;
  1722. }
  1723. tlv_end = pos + slen;
  1724. srv_proto = *pos++;
  1725. wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
  1726. srv_proto);
  1727. srv_trans_id = *pos++;
  1728. wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
  1729. srv_trans_id);
  1730. status = *pos++;
  1731. wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
  1732. status);
  1733. wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
  1734. pos, tlv_end - pos);
  1735. pos = tlv_end;
  1736. }
  1737. wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
  1738. }
  1739. u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
  1740. const struct wpabuf *tlvs)
  1741. {
  1742. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  1743. return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
  1744. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  1745. return 0;
  1746. return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
  1747. }
  1748. u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
  1749. u8 version, const char *query)
  1750. {
  1751. struct wpabuf *tlvs;
  1752. u64 ret;
  1753. tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
  1754. if (tlvs == NULL)
  1755. return 0;
  1756. wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
  1757. wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
  1758. wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
  1759. wpabuf_put_u8(tlvs, version);
  1760. wpabuf_put_str(tlvs, query);
  1761. ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
  1762. wpabuf_free(tlvs);
  1763. return ret;
  1764. }
  1765. #ifdef CONFIG_WIFI_DISPLAY
  1766. static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
  1767. const struct wpabuf *tlvs)
  1768. {
  1769. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  1770. return 0;
  1771. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  1772. return 0;
  1773. return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
  1774. }
  1775. #define MAX_WFD_SD_SUBELEMS 20
  1776. static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
  1777. const char *subelems)
  1778. {
  1779. u8 *len;
  1780. const char *pos;
  1781. int val;
  1782. int count = 0;
  1783. len = wpabuf_put(tlvs, 2);
  1784. wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
  1785. wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
  1786. wpabuf_put_u8(tlvs, role);
  1787. pos = subelems;
  1788. while (*pos) {
  1789. val = atoi(pos);
  1790. if (val >= 0 && val < 256) {
  1791. wpabuf_put_u8(tlvs, val);
  1792. count++;
  1793. if (count == MAX_WFD_SD_SUBELEMS)
  1794. break;
  1795. }
  1796. pos = os_strchr(pos + 1, ',');
  1797. if (pos == NULL)
  1798. break;
  1799. pos++;
  1800. }
  1801. WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
  1802. }
  1803. u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
  1804. const u8 *dst, const char *role)
  1805. {
  1806. struct wpabuf *tlvs;
  1807. u64 ret;
  1808. const char *subelems;
  1809. u8 id = 1;
  1810. subelems = os_strchr(role, ' ');
  1811. if (subelems == NULL)
  1812. return 0;
  1813. subelems++;
  1814. tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
  1815. if (tlvs == NULL)
  1816. return 0;
  1817. if (os_strstr(role, "[source]"))
  1818. wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
  1819. if (os_strstr(role, "[pri-sink]"))
  1820. wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
  1821. if (os_strstr(role, "[sec-sink]"))
  1822. wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
  1823. if (os_strstr(role, "[source+sink]"))
  1824. wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
  1825. ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
  1826. wpabuf_free(tlvs);
  1827. return ret;
  1828. }
  1829. #endif /* CONFIG_WIFI_DISPLAY */
  1830. int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
  1831. {
  1832. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  1833. return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
  1834. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  1835. return -1;
  1836. return p2p_sd_cancel_request(wpa_s->global->p2p,
  1837. (void *) (uintptr_t) req);
  1838. }
  1839. void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
  1840. const u8 *dst, u8 dialog_token,
  1841. const struct wpabuf *resp_tlvs)
  1842. {
  1843. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  1844. wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
  1845. resp_tlvs);
  1846. return;
  1847. }
  1848. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  1849. return;
  1850. p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
  1851. resp_tlvs);
  1852. }
  1853. void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
  1854. {
  1855. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  1856. wpa_drv_p2p_service_update(wpa_s);
  1857. return;
  1858. }
  1859. if (wpa_s->global->p2p)
  1860. p2p_sd_service_update(wpa_s->global->p2p);
  1861. }
  1862. static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
  1863. {
  1864. dl_list_del(&bsrv->list);
  1865. wpabuf_free(bsrv->query);
  1866. wpabuf_free(bsrv->resp);
  1867. os_free(bsrv);
  1868. }
  1869. static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
  1870. {
  1871. dl_list_del(&usrv->list);
  1872. os_free(usrv->service);
  1873. os_free(usrv);
  1874. }
  1875. void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
  1876. {
  1877. struct p2p_srv_bonjour *bsrv, *bn;
  1878. struct p2p_srv_upnp *usrv, *un;
  1879. dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
  1880. struct p2p_srv_bonjour, list)
  1881. wpas_p2p_srv_bonjour_free(bsrv);
  1882. dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
  1883. struct p2p_srv_upnp, list)
  1884. wpas_p2p_srv_upnp_free(usrv);
  1885. wpas_p2p_sd_service_update(wpa_s);
  1886. }
  1887. int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
  1888. struct wpabuf *query, struct wpabuf *resp)
  1889. {
  1890. struct p2p_srv_bonjour *bsrv;
  1891. bsrv = os_zalloc(sizeof(*bsrv));
  1892. if (bsrv == NULL)
  1893. return -1;
  1894. bsrv->query = query;
  1895. bsrv->resp = resp;
  1896. dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
  1897. wpas_p2p_sd_service_update(wpa_s);
  1898. return 0;
  1899. }
  1900. int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
  1901. const struct wpabuf *query)
  1902. {
  1903. struct p2p_srv_bonjour *bsrv;
  1904. bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
  1905. if (bsrv == NULL)
  1906. return -1;
  1907. wpas_p2p_srv_bonjour_free(bsrv);
  1908. wpas_p2p_sd_service_update(wpa_s);
  1909. return 0;
  1910. }
  1911. int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
  1912. const char *service)
  1913. {
  1914. struct p2p_srv_upnp *usrv;
  1915. if (wpas_p2p_service_get_upnp(wpa_s, version, service))
  1916. return 0; /* Already listed */
  1917. usrv = os_zalloc(sizeof(*usrv));
  1918. if (usrv == NULL)
  1919. return -1;
  1920. usrv->version = version;
  1921. usrv->service = os_strdup(service);
  1922. if (usrv->service == NULL) {
  1923. os_free(usrv);
  1924. return -1;
  1925. }
  1926. dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
  1927. wpas_p2p_sd_service_update(wpa_s);
  1928. return 0;
  1929. }
  1930. int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
  1931. const char *service)
  1932. {
  1933. struct p2p_srv_upnp *usrv;
  1934. usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
  1935. if (usrv == NULL)
  1936. return -1;
  1937. wpas_p2p_srv_upnp_free(usrv);
  1938. wpas_p2p_sd_service_update(wpa_s);
  1939. return 0;
  1940. }
  1941. static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
  1942. const u8 *peer, const char *params,
  1943. unsigned int generated_pin)
  1944. {
  1945. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
  1946. " %08d%s", MAC2STR(peer), generated_pin, params);
  1947. }
  1948. static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
  1949. const u8 *peer, const char *params)
  1950. {
  1951. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
  1952. "%s", MAC2STR(peer), params);
  1953. }
  1954. void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
  1955. const u8 *dev_addr, const u8 *pri_dev_type,
  1956. const char *dev_name, u16 supp_config_methods,
  1957. u8 dev_capab, u8 group_capab, const u8 *group_id,
  1958. size_t group_id_len)
  1959. {
  1960. struct wpa_supplicant *wpa_s = ctx;
  1961. char devtype[WPS_DEV_TYPE_BUFSIZE];
  1962. char params[300];
  1963. u8 empty_dev_type[8];
  1964. unsigned int generated_pin = 0;
  1965. struct wpa_supplicant *group = NULL;
  1966. if (group_id) {
  1967. for (group = wpa_s->global->ifaces; group; group = group->next)
  1968. {
  1969. struct wpa_ssid *s = group->current_ssid;
  1970. if (s != NULL &&
  1971. s->mode == WPAS_MODE_P2P_GO &&
  1972. group_id_len - ETH_ALEN == s->ssid_len &&
  1973. os_memcmp(group_id + ETH_ALEN, s->ssid,
  1974. s->ssid_len) == 0)
  1975. break;
  1976. }
  1977. }
  1978. if (pri_dev_type == NULL) {
  1979. os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
  1980. pri_dev_type = empty_dev_type;
  1981. }
  1982. os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
  1983. " pri_dev_type=%s name='%s' config_methods=0x%x "
  1984. "dev_capab=0x%x group_capab=0x%x%s%s",
  1985. MAC2STR(dev_addr),
  1986. wps_dev_type_bin2str(pri_dev_type, devtype,
  1987. sizeof(devtype)),
  1988. dev_name, supp_config_methods, dev_capab, group_capab,
  1989. group ? " group=" : "",
  1990. group ? group->ifname : "");
  1991. params[sizeof(params) - 1] = '\0';
  1992. if (config_methods & WPS_CONFIG_DISPLAY) {
  1993. generated_pin = wps_generate_pin();
  1994. wpas_prov_disc_local_display(wpa_s, peer, params,
  1995. generated_pin);
  1996. } else if (config_methods & WPS_CONFIG_KEYPAD)
  1997. wpas_prov_disc_local_keypad(wpa_s, peer, params);
  1998. else if (config_methods & WPS_CONFIG_PUSHBUTTON)
  1999. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
  2000. MACSTR "%s", MAC2STR(peer), params);
  2001. wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
  2002. P2P_PROV_DISC_SUCCESS,
  2003. config_methods, generated_pin);
  2004. }
  2005. void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
  2006. {
  2007. struct wpa_supplicant *wpa_s = ctx;
  2008. unsigned int generated_pin = 0;
  2009. char params[20];
  2010. if (wpa_s->pending_pd_before_join &&
  2011. (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
  2012. os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
  2013. wpa_s->pending_pd_before_join = 0;
  2014. wpa_printf(MSG_DEBUG, "P2P: Starting pending "
  2015. "join-existing-group operation");
  2016. wpas_p2p_join_start(wpa_s);
  2017. return;
  2018. }
  2019. if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
  2020. wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
  2021. os_snprintf(params, sizeof(params), " peer_go=%d",
  2022. wpa_s->pending_pd_use == AUTO_PD_JOIN);
  2023. else
  2024. params[0] = '\0';
  2025. if (config_methods & WPS_CONFIG_DISPLAY)
  2026. wpas_prov_disc_local_keypad(wpa_s, peer, params);
  2027. else if (config_methods & WPS_CONFIG_KEYPAD) {
  2028. generated_pin = wps_generate_pin();
  2029. wpas_prov_disc_local_display(wpa_s, peer, params,
  2030. generated_pin);
  2031. } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
  2032. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
  2033. MACSTR "%s", MAC2STR(peer), params);
  2034. wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
  2035. P2P_PROV_DISC_SUCCESS,
  2036. config_methods, generated_pin);
  2037. }
  2038. static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
  2039. enum p2p_prov_disc_status status)
  2040. {
  2041. struct wpa_supplicant *wpa_s = ctx;
  2042. if (wpa_s->p2p_fallback_to_go_neg) {
  2043. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
  2044. "failed - fall back to GO Negotiation");
  2045. wpas_p2p_fallback_to_go_neg(wpa_s, 0);
  2046. return;
  2047. }
  2048. if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
  2049. wpa_s->pending_pd_before_join = 0;
  2050. wpa_printf(MSG_DEBUG, "P2P: Starting pending "
  2051. "join-existing-group operation (no ACK for PD "
  2052. "Req attempts)");
  2053. wpas_p2p_join_start(wpa_s);
  2054. return;
  2055. }
  2056. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
  2057. " p2p_dev_addr=" MACSTR " status=%d",
  2058. MAC2STR(peer), status);
  2059. wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
  2060. status, 0, 0);
  2061. }
  2062. static int freq_included(const struct p2p_channels *channels, unsigned int freq)
  2063. {
  2064. if (channels == NULL)
  2065. return 1; /* Assume no restrictions */
  2066. return p2p_channels_includes_freq(channels, freq);
  2067. }
  2068. static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
  2069. const u8 *go_dev_addr, const u8 *ssid,
  2070. size_t ssid_len, int *go, u8 *group_bssid,
  2071. int *force_freq, int persistent_group,
  2072. const struct p2p_channels *channels)
  2073. {
  2074. struct wpa_supplicant *wpa_s = ctx;
  2075. struct wpa_ssid *s;
  2076. int res;
  2077. struct wpa_supplicant *grp;
  2078. if (!persistent_group) {
  2079. wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
  2080. " to join an active group", MAC2STR(sa));
  2081. if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
  2082. (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
  2083. == 0 ||
  2084. os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
  2085. wpa_printf(MSG_DEBUG, "P2P: Accept previously "
  2086. "authorized invitation");
  2087. goto accept_inv;
  2088. }
  2089. /*
  2090. * Do not accept the invitation automatically; notify user and
  2091. * request approval.
  2092. */
  2093. return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  2094. }
  2095. grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
  2096. if (grp) {
  2097. wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
  2098. "running persistent group");
  2099. if (*go)
  2100. os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
  2101. goto accept_inv;
  2102. }
  2103. if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
  2104. os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
  2105. wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
  2106. "invitation to re-invoke a persistent group");
  2107. } else if (!wpa_s->conf->persistent_reconnect)
  2108. return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  2109. for (s = wpa_s->conf->ssid; s; s = s->next) {
  2110. if (s->disabled == 2 &&
  2111. os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
  2112. s->ssid_len == ssid_len &&
  2113. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  2114. break;
  2115. }
  2116. if (!s) {
  2117. wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
  2118. " requested reinvocation of an unknown group",
  2119. MAC2STR(sa));
  2120. return P2P_SC_FAIL_UNKNOWN_GROUP;
  2121. }
  2122. if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
  2123. *go = 1;
  2124. if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
  2125. wpa_printf(MSG_DEBUG, "P2P: The only available "
  2126. "interface is already in use - reject "
  2127. "invitation");
  2128. return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
  2129. }
  2130. os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
  2131. } else if (s->mode == WPAS_MODE_P2P_GO) {
  2132. *go = 1;
  2133. if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
  2134. {
  2135. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  2136. "interface address for the group");
  2137. return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
  2138. }
  2139. os_memcpy(group_bssid, wpa_s->pending_interface_addr,
  2140. ETH_ALEN);
  2141. }
  2142. accept_inv:
  2143. wpas_p2p_set_own_freq_preference(wpa_s, 0);
  2144. /* Get one of the frequencies currently in use */
  2145. if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) {
  2146. wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match a channel already used by one of the interfaces");
  2147. *force_freq = res;
  2148. wpas_p2p_set_own_freq_preference(wpa_s, res);
  2149. }
  2150. if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
  2151. wpas_p2p_num_unused_channels(wpa_s) > 0) {
  2152. if (*go == 0) {
  2153. /* We are the client */
  2154. wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
  2155. "running a GO but we are capable of MCC, "
  2156. "figure out the best channel to use");
  2157. *force_freq = 0;
  2158. } else if (!freq_included(channels, *force_freq)) {
  2159. /* We are the GO, and *force_freq is not in the
  2160. * intersection */
  2161. wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
  2162. "in intersection but we are capable of MCC, "
  2163. "figure out the best channel to use",
  2164. *force_freq);
  2165. *force_freq = 0;
  2166. }
  2167. }
  2168. return P2P_SC_SUCCESS;
  2169. }
  2170. static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
  2171. const u8 *ssid, size_t ssid_len,
  2172. const u8 *go_dev_addr, u8 status,
  2173. int op_freq)
  2174. {
  2175. struct wpa_supplicant *wpa_s = ctx;
  2176. struct wpa_ssid *s;
  2177. for (s = wpa_s->conf->ssid; s; s = s->next) {
  2178. if (s->disabled == 2 &&
  2179. s->ssid_len == ssid_len &&
  2180. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  2181. break;
  2182. }
  2183. if (status == P2P_SC_SUCCESS) {
  2184. wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
  2185. " was accepted; op_freq=%d MHz",
  2186. MAC2STR(sa), op_freq);
  2187. if (s) {
  2188. int go = s->mode == WPAS_MODE_P2P_GO;
  2189. wpas_p2p_group_add_persistent(
  2190. wpa_s, s, go, go ? op_freq : 0, 0, NULL);
  2191. } else if (bssid) {
  2192. wpa_s->user_initiated_pd = 0;
  2193. wpas_p2p_join(wpa_s, bssid, go_dev_addr,
  2194. wpa_s->p2p_wps_method, 0);
  2195. }
  2196. return;
  2197. }
  2198. if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
  2199. wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
  2200. " was rejected (status %u)", MAC2STR(sa), status);
  2201. return;
  2202. }
  2203. if (!s) {
  2204. if (bssid) {
  2205. wpa_msg_global(wpa_s, MSG_INFO,
  2206. P2P_EVENT_INVITATION_RECEIVED
  2207. "sa=" MACSTR " go_dev_addr=" MACSTR
  2208. " bssid=" MACSTR " unknown-network",
  2209. MAC2STR(sa), MAC2STR(go_dev_addr),
  2210. MAC2STR(bssid));
  2211. } else {
  2212. wpa_msg_global(wpa_s, MSG_INFO,
  2213. P2P_EVENT_INVITATION_RECEIVED
  2214. "sa=" MACSTR " go_dev_addr=" MACSTR
  2215. " unknown-network",
  2216. MAC2STR(sa), MAC2STR(go_dev_addr));
  2217. }
  2218. return;
  2219. }
  2220. if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
  2221. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
  2222. "sa=" MACSTR " persistent=%d freq=%d",
  2223. MAC2STR(sa), s->id, op_freq);
  2224. } else {
  2225. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
  2226. "sa=" MACSTR " persistent=%d",
  2227. MAC2STR(sa), s->id);
  2228. }
  2229. }
  2230. static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
  2231. struct wpa_ssid *ssid,
  2232. const u8 *peer)
  2233. {
  2234. size_t i;
  2235. if (ssid == NULL)
  2236. return;
  2237. for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
  2238. if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
  2239. ETH_ALEN) == 0)
  2240. break;
  2241. }
  2242. if (i >= ssid->num_p2p_clients) {
  2243. if (ssid->mode != WPAS_MODE_P2P_GO &&
  2244. os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
  2245. wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
  2246. "due to invitation result", ssid->id);
  2247. wpas_notify_network_removed(wpa_s, ssid);
  2248. wpa_config_remove_network(wpa_s->conf, ssid->id);
  2249. return;
  2250. }
  2251. return; /* Peer not found in client list */
  2252. }
  2253. wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
  2254. "group %d client list due to invitation result",
  2255. MAC2STR(peer), ssid->id);
  2256. os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
  2257. ssid->p2p_client_list + (i + 1) * ETH_ALEN,
  2258. (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
  2259. ssid->num_p2p_clients--;
  2260. #ifndef CONFIG_NO_CONFIG_WRITE
  2261. if (wpa_s->parent->conf->update_config &&
  2262. wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
  2263. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  2264. #endif /* CONFIG_NO_CONFIG_WRITE */
  2265. }
  2266. static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
  2267. const u8 *peer)
  2268. {
  2269. struct wpa_ssid *ssid;
  2270. wpa_s = wpa_s->global->p2p_invite_group;
  2271. if (wpa_s == NULL)
  2272. return; /* No known invitation group */
  2273. ssid = wpa_s->current_ssid;
  2274. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  2275. !ssid->p2p_persistent_group)
  2276. return; /* Not operating as a GO in persistent group */
  2277. ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
  2278. ssid->ssid, ssid->ssid_len);
  2279. wpas_remove_persistent_peer(wpa_s, ssid, peer);
  2280. }
  2281. static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
  2282. const struct p2p_channels *channels,
  2283. const u8 *peer)
  2284. {
  2285. struct wpa_supplicant *wpa_s = ctx;
  2286. struct wpa_ssid *ssid;
  2287. if (bssid) {
  2288. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
  2289. "status=%d " MACSTR,
  2290. status, MAC2STR(bssid));
  2291. } else {
  2292. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
  2293. "status=%d ", status);
  2294. }
  2295. wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
  2296. wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
  2297. status, MAC2STR(peer));
  2298. if (wpa_s->pending_invite_ssid_id == -1) {
  2299. if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
  2300. wpas_remove_persistent_client(wpa_s, peer);
  2301. return; /* Invitation to active group */
  2302. }
  2303. if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
  2304. wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
  2305. "invitation exchange to indicate readiness for "
  2306. "re-invocation");
  2307. }
  2308. if (status != P2P_SC_SUCCESS) {
  2309. if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
  2310. ssid = wpa_config_get_network(
  2311. wpa_s->conf, wpa_s->pending_invite_ssid_id);
  2312. wpas_remove_persistent_peer(wpa_s, ssid, peer);
  2313. }
  2314. wpas_p2p_remove_pending_group_interface(wpa_s);
  2315. return;
  2316. }
  2317. ssid = wpa_config_get_network(wpa_s->conf,
  2318. wpa_s->pending_invite_ssid_id);
  2319. if (ssid == NULL) {
  2320. wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
  2321. "data matching with invitation");
  2322. return;
  2323. }
  2324. /*
  2325. * The peer could have missed our ctrl::ack frame for Invitation
  2326. * Response and continue retransmitting the frame. To reduce the
  2327. * likelihood of the peer not getting successful TX status for the
  2328. * Invitation Response frame, wait a short time here before starting
  2329. * the persistent group so that we will remain on the current channel to
  2330. * acknowledge any possible retransmission from the peer.
  2331. */
  2332. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
  2333. "starting persistent group");
  2334. os_sleep(0, 50000);
  2335. wpas_p2p_group_add_persistent(wpa_s, ssid,
  2336. ssid->mode == WPAS_MODE_P2P_GO,
  2337. wpa_s->p2p_persistent_go_freq,
  2338. wpa_s->p2p_go_ht40, channels);
  2339. }
  2340. static int wpas_p2p_disallowed_freq(struct wpa_global *global,
  2341. unsigned int freq)
  2342. {
  2343. unsigned int i;
  2344. if (global->p2p_disallow_freq == NULL)
  2345. return 0;
  2346. for (i = 0; i < global->num_p2p_disallow_freq; i++) {
  2347. if (freq >= global->p2p_disallow_freq[i].min &&
  2348. freq <= global->p2p_disallow_freq[i].max)
  2349. return 1;
  2350. }
  2351. return 0;
  2352. }
  2353. static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
  2354. {
  2355. reg->channel[reg->channels] = chan;
  2356. reg->channels++;
  2357. }
  2358. static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
  2359. struct p2p_channels *chan)
  2360. {
  2361. int i, cla = 0;
  2362. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
  2363. "band");
  2364. /* Operating class 81 - 2.4 GHz band channels 1..13 */
  2365. chan->reg_class[cla].reg_class = 81;
  2366. chan->reg_class[cla].channels = 0;
  2367. for (i = 0; i < 11; i++) {
  2368. if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
  2369. wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
  2370. }
  2371. if (chan->reg_class[cla].channels)
  2372. cla++;
  2373. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
  2374. "band");
  2375. /* Operating class 115 - 5 GHz, channels 36-48 */
  2376. chan->reg_class[cla].reg_class = 115;
  2377. chan->reg_class[cla].channels = 0;
  2378. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
  2379. wpas_p2p_add_chan(&chan->reg_class[cla], 36);
  2380. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
  2381. wpas_p2p_add_chan(&chan->reg_class[cla], 40);
  2382. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
  2383. wpas_p2p_add_chan(&chan->reg_class[cla], 44);
  2384. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
  2385. wpas_p2p_add_chan(&chan->reg_class[cla], 48);
  2386. if (chan->reg_class[cla].channels)
  2387. cla++;
  2388. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
  2389. "band");
  2390. /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
  2391. chan->reg_class[cla].reg_class = 124;
  2392. chan->reg_class[cla].channels = 0;
  2393. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
  2394. wpas_p2p_add_chan(&chan->reg_class[cla], 149);
  2395. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
  2396. wpas_p2p_add_chan(&chan->reg_class[cla], 153);
  2397. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
  2398. wpas_p2p_add_chan(&chan->reg_class[cla], 157);
  2399. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
  2400. wpas_p2p_add_chan(&chan->reg_class[cla], 161);
  2401. if (chan->reg_class[cla].channels)
  2402. cla++;
  2403. chan->reg_classes = cla;
  2404. return 0;
  2405. }
  2406. static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
  2407. u16 num_modes,
  2408. enum hostapd_hw_mode mode)
  2409. {
  2410. u16 i;
  2411. for (i = 0; i < num_modes; i++) {
  2412. if (modes[i].mode == mode)
  2413. return &modes[i];
  2414. }
  2415. return NULL;
  2416. }
  2417. static int has_channel(struct wpa_global *global,
  2418. struct hostapd_hw_modes *mode, u8 chan, int *flags)
  2419. {
  2420. int i;
  2421. unsigned int freq;
  2422. freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
  2423. chan * 5;
  2424. if (wpas_p2p_disallowed_freq(global, freq))
  2425. return 0;
  2426. for (i = 0; i < mode->num_channels; i++) {
  2427. if (mode->channels[i].chan == chan) {
  2428. if (flags)
  2429. *flags = mode->channels[i].flag;
  2430. return !(mode->channels[i].flag &
  2431. (HOSTAPD_CHAN_DISABLED |
  2432. HOSTAPD_CHAN_PASSIVE_SCAN |
  2433. HOSTAPD_CHAN_NO_IBSS |
  2434. HOSTAPD_CHAN_RADAR));
  2435. }
  2436. }
  2437. return 0;
  2438. }
  2439. struct p2p_oper_class_map {
  2440. enum hostapd_hw_mode mode;
  2441. u8 op_class;
  2442. u8 min_chan;
  2443. u8 max_chan;
  2444. u8 inc;
  2445. enum { BW20, BW40PLUS, BW40MINUS } bw;
  2446. };
  2447. static struct p2p_oper_class_map op_class[] = {
  2448. { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
  2449. #if 0 /* Do not enable HT40 on 2 GHz for now */
  2450. { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
  2451. { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
  2452. #endif
  2453. { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
  2454. { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
  2455. { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
  2456. { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
  2457. { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
  2458. { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
  2459. { -1, 0, 0, 0, 0, BW20 }
  2460. };
  2461. static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
  2462. struct hostapd_hw_modes *mode,
  2463. u8 channel, u8 bw)
  2464. {
  2465. int flag;
  2466. if (!has_channel(wpa_s->global, mode, channel, &flag))
  2467. return -1;
  2468. if (bw == BW40MINUS &&
  2469. (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
  2470. !has_channel(wpa_s->global, mode, channel - 4, NULL)))
  2471. return 0;
  2472. if (bw == BW40PLUS &&
  2473. (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
  2474. !has_channel(wpa_s->global, mode, channel + 4, NULL)))
  2475. return 0;
  2476. return 1;
  2477. }
  2478. static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
  2479. struct p2p_channels *chan)
  2480. {
  2481. struct hostapd_hw_modes *mode;
  2482. int cla, op;
  2483. if (wpa_s->hw.modes == NULL) {
  2484. wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
  2485. "of all supported channels; assume dualband "
  2486. "support");
  2487. return wpas_p2p_default_channels(wpa_s, chan);
  2488. }
  2489. cla = 0;
  2490. for (op = 0; op_class[op].op_class; op++) {
  2491. struct p2p_oper_class_map *o = &op_class[op];
  2492. u8 ch;
  2493. struct p2p_reg_class *reg = NULL;
  2494. mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
  2495. if (mode == NULL)
  2496. continue;
  2497. for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
  2498. if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
  2499. continue;
  2500. if (reg == NULL) {
  2501. wpa_printf(MSG_DEBUG, "P2P: Add operating "
  2502. "class %u", o->op_class);
  2503. reg = &chan->reg_class[cla];
  2504. cla++;
  2505. reg->reg_class = o->op_class;
  2506. }
  2507. reg->channel[reg->channels] = ch;
  2508. reg->channels++;
  2509. }
  2510. if (reg) {
  2511. wpa_hexdump(MSG_DEBUG, "P2P: Channels",
  2512. reg->channel, reg->channels);
  2513. }
  2514. }
  2515. chan->reg_classes = cla;
  2516. return 0;
  2517. }
  2518. int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
  2519. struct hostapd_hw_modes *mode, u8 channel)
  2520. {
  2521. int op, ret;
  2522. for (op = 0; op_class[op].op_class; op++) {
  2523. struct p2p_oper_class_map *o = &op_class[op];
  2524. u8 ch;
  2525. for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
  2526. if (o->mode != HOSTAPD_MODE_IEEE80211A ||
  2527. o->bw == BW20 || ch != channel)
  2528. continue;
  2529. ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
  2530. if (ret < 0)
  2531. continue;
  2532. else if (ret > 0)
  2533. return (o->bw == BW40MINUS) ? -1 : 1;
  2534. else
  2535. return 0;
  2536. }
  2537. }
  2538. return 0;
  2539. }
  2540. static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
  2541. size_t buf_len)
  2542. {
  2543. struct wpa_supplicant *wpa_s = ctx;
  2544. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2545. if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
  2546. break;
  2547. }
  2548. if (wpa_s == NULL)
  2549. return -1;
  2550. return wpa_drv_get_noa(wpa_s, buf, buf_len);
  2551. }
  2552. static int wpas_go_connected(void *ctx, const u8 *dev_addr)
  2553. {
  2554. struct wpa_supplicant *wpa_s = ctx;
  2555. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2556. struct wpa_ssid *ssid = wpa_s->current_ssid;
  2557. if (ssid == NULL)
  2558. continue;
  2559. if (ssid->mode != WPAS_MODE_INFRA)
  2560. continue;
  2561. if (wpa_s->wpa_state != WPA_COMPLETED &&
  2562. wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
  2563. continue;
  2564. if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
  2565. return 1;
  2566. }
  2567. return 0;
  2568. }
  2569. static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
  2570. {
  2571. struct wpa_supplicant *wpa_s = ctx;
  2572. wpa_msg_global(wpa_s, level, "P2P: %s", msg);
  2573. }
  2574. int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
  2575. {
  2576. struct wpa_interface iface;
  2577. struct wpa_supplicant *p2pdev_wpa_s;
  2578. char ifname[100];
  2579. char force_name[100];
  2580. int ret;
  2581. os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
  2582. wpa_s->ifname);
  2583. force_name[0] = '\0';
  2584. wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
  2585. ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
  2586. force_name, wpa_s->pending_interface_addr, NULL);
  2587. if (ret < 0) {
  2588. wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
  2589. return ret;
  2590. }
  2591. os_strlcpy(wpa_s->pending_interface_name, ifname,
  2592. sizeof(wpa_s->pending_interface_name));
  2593. os_memset(&iface, 0, sizeof(iface));
  2594. iface.p2p_mgmt = 1;
  2595. iface.ifname = wpa_s->pending_interface_name;
  2596. iface.driver = wpa_s->driver->name;
  2597. iface.driver_param = wpa_s->conf->driver_param;
  2598. iface.confname = wpa_s->confname;
  2599. p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
  2600. if (!p2pdev_wpa_s) {
  2601. wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
  2602. return -1;
  2603. }
  2604. p2pdev_wpa_s->parent = wpa_s;
  2605. wpa_s->pending_interface_name[0] = '\0';
  2606. return 0;
  2607. }
  2608. /**
  2609. * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
  2610. * @global: Pointer to global data from wpa_supplicant_init()
  2611. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2612. * Returns: 0 on success, -1 on failure
  2613. */
  2614. int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
  2615. {
  2616. struct p2p_config p2p;
  2617. unsigned int r;
  2618. int i;
  2619. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
  2620. return 0;
  2621. if (global->p2p)
  2622. return 0;
  2623. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  2624. struct p2p_params params;
  2625. wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
  2626. os_memset(&params, 0, sizeof(params));
  2627. params.dev_name = wpa_s->conf->device_name;
  2628. os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
  2629. WPS_DEV_TYPE_LEN);
  2630. params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
  2631. os_memcpy(params.sec_dev_type,
  2632. wpa_s->conf->sec_device_type,
  2633. params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
  2634. if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
  2635. return -1;
  2636. return 0;
  2637. }
  2638. os_memset(&p2p, 0, sizeof(p2p));
  2639. p2p.cb_ctx = wpa_s;
  2640. p2p.debug_print = wpas_p2p_debug_print;
  2641. p2p.p2p_scan = wpas_p2p_scan;
  2642. p2p.send_action = wpas_send_action;
  2643. p2p.send_action_done = wpas_send_action_done;
  2644. p2p.go_neg_completed = wpas_go_neg_completed;
  2645. p2p.go_neg_req_rx = wpas_go_neg_req_rx;
  2646. p2p.dev_found = wpas_dev_found;
  2647. p2p.dev_lost = wpas_dev_lost;
  2648. p2p.find_stopped = wpas_find_stopped;
  2649. p2p.start_listen = wpas_start_listen;
  2650. p2p.stop_listen = wpas_stop_listen;
  2651. p2p.send_probe_resp = wpas_send_probe_resp;
  2652. p2p.sd_request = wpas_sd_request;
  2653. p2p.sd_response = wpas_sd_response;
  2654. p2p.prov_disc_req = wpas_prov_disc_req;
  2655. p2p.prov_disc_resp = wpas_prov_disc_resp;
  2656. p2p.prov_disc_fail = wpas_prov_disc_fail;
  2657. p2p.invitation_process = wpas_invitation_process;
  2658. p2p.invitation_received = wpas_invitation_received;
  2659. p2p.invitation_result = wpas_invitation_result;
  2660. p2p.get_noa = wpas_get_noa;
  2661. p2p.go_connected = wpas_go_connected;
  2662. os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
  2663. os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
  2664. p2p.dev_name = wpa_s->conf->device_name;
  2665. p2p.manufacturer = wpa_s->conf->manufacturer;
  2666. p2p.model_name = wpa_s->conf->model_name;
  2667. p2p.model_number = wpa_s->conf->model_number;
  2668. p2p.serial_number = wpa_s->conf->serial_number;
  2669. if (wpa_s->wps) {
  2670. os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
  2671. p2p.config_methods = wpa_s->wps->config_methods;
  2672. }
  2673. if (wpa_s->conf->p2p_listen_reg_class &&
  2674. wpa_s->conf->p2p_listen_channel) {
  2675. p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
  2676. p2p.channel = wpa_s->conf->p2p_listen_channel;
  2677. } else {
  2678. p2p.reg_class = 81;
  2679. /*
  2680. * Pick one of the social channels randomly as the listen
  2681. * channel.
  2682. */
  2683. os_get_random((u8 *) &r, sizeof(r));
  2684. p2p.channel = 1 + (r % 3) * 5;
  2685. }
  2686. wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
  2687. if (wpa_s->conf->p2p_oper_reg_class &&
  2688. wpa_s->conf->p2p_oper_channel) {
  2689. p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
  2690. p2p.op_channel = wpa_s->conf->p2p_oper_channel;
  2691. p2p.cfg_op_channel = 1;
  2692. wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
  2693. "%d:%d", p2p.op_reg_class, p2p.op_channel);
  2694. } else {
  2695. p2p.op_reg_class = 81;
  2696. /*
  2697. * Use random operation channel from (1, 6, 11) if no other
  2698. * preference is indicated.
  2699. */
  2700. os_get_random((u8 *) &r, sizeof(r));
  2701. p2p.op_channel = 1 + (r % 3) * 5;
  2702. p2p.cfg_op_channel = 0;
  2703. wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
  2704. "%d:%d", p2p.op_reg_class, p2p.op_channel);
  2705. }
  2706. if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
  2707. p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
  2708. p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
  2709. }
  2710. if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  2711. os_memcpy(p2p.country, wpa_s->conf->country, 2);
  2712. p2p.country[2] = 0x04;
  2713. } else
  2714. os_memcpy(p2p.country, "XX\x04", 3);
  2715. if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
  2716. wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
  2717. "channel list");
  2718. return -1;
  2719. }
  2720. os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
  2721. WPS_DEV_TYPE_LEN);
  2722. p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
  2723. os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
  2724. p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
  2725. p2p.concurrent_operations = !!(wpa_s->drv_flags &
  2726. WPA_DRIVER_FLAGS_P2P_CONCURRENT);
  2727. p2p.max_peers = 100;
  2728. if (wpa_s->conf->p2p_ssid_postfix) {
  2729. p2p.ssid_postfix_len =
  2730. os_strlen(wpa_s->conf->p2p_ssid_postfix);
  2731. if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
  2732. p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
  2733. os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
  2734. p2p.ssid_postfix_len);
  2735. }
  2736. p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
  2737. p2p.max_listen = wpa_s->max_remain_on_chan;
  2738. global->p2p = p2p_init(&p2p);
  2739. if (global->p2p == NULL)
  2740. return -1;
  2741. global->p2p_init_wpa_s = wpa_s;
  2742. for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
  2743. if (wpa_s->conf->wps_vendor_ext[i] == NULL)
  2744. continue;
  2745. p2p_add_wps_vendor_extension(
  2746. global->p2p, wpa_s->conf->wps_vendor_ext[i]);
  2747. }
  2748. return 0;
  2749. }
  2750. /**
  2751. * wpas_p2p_deinit - Deinitialize per-interface P2P data
  2752. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2753. *
  2754. * This function deinitialize per-interface P2P data.
  2755. */
  2756. void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
  2757. {
  2758. if (wpa_s->driver && wpa_s->drv_priv)
  2759. wpa_drv_probe_req_report(wpa_s, 0);
  2760. if (wpa_s->go_params) {
  2761. /* Clear any stored provisioning info */
  2762. p2p_clear_provisioning_info(
  2763. wpa_s->global->p2p,
  2764. wpa_s->go_params->peer_device_addr);
  2765. }
  2766. os_free(wpa_s->go_params);
  2767. wpa_s->go_params = NULL;
  2768. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  2769. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2770. wpa_s->p2p_long_listen = 0;
  2771. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  2772. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  2773. wpas_p2p_remove_pending_group_interface(wpa_s);
  2774. /* TODO: remove group interface from the driver if this wpa_s instance
  2775. * is on top of a P2P group interface */
  2776. }
  2777. /**
  2778. * wpas_p2p_deinit_global - Deinitialize global P2P module
  2779. * @global: Pointer to global data from wpa_supplicant_init()
  2780. *
  2781. * This function deinitializes the global (per device) P2P module.
  2782. */
  2783. void wpas_p2p_deinit_global(struct wpa_global *global)
  2784. {
  2785. struct wpa_supplicant *wpa_s, *tmp;
  2786. wpa_s = global->ifaces;
  2787. if (wpa_s)
  2788. wpas_p2p_service_flush(wpa_s);
  2789. if (global->p2p == NULL)
  2790. return;
  2791. /* Remove remaining P2P group interfaces */
  2792. while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
  2793. wpa_s = wpa_s->next;
  2794. while (wpa_s) {
  2795. tmp = global->ifaces;
  2796. while (tmp &&
  2797. (tmp == wpa_s ||
  2798. tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
  2799. tmp = tmp->next;
  2800. }
  2801. if (tmp == NULL)
  2802. break;
  2803. /* Disconnect from the P2P group and deinit the interface */
  2804. wpas_p2p_disconnect(tmp);
  2805. }
  2806. /*
  2807. * Deinit GO data on any possibly remaining interface (if main
  2808. * interface is used as GO).
  2809. */
  2810. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2811. if (wpa_s->ap_iface)
  2812. wpas_p2p_group_deinit(wpa_s);
  2813. }
  2814. p2p_deinit(global->p2p);
  2815. global->p2p = NULL;
  2816. global->p2p_init_wpa_s = NULL;
  2817. }
  2818. static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
  2819. {
  2820. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
  2821. wpa_s->conf->p2p_no_group_iface)
  2822. return 0; /* separate interface disabled per configuration */
  2823. if (wpa_s->drv_flags &
  2824. (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
  2825. WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
  2826. return 1; /* P2P group requires a new interface in every case
  2827. */
  2828. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
  2829. return 0; /* driver does not support concurrent operations */
  2830. if (wpa_s->global->ifaces->next)
  2831. return 1; /* more that one interface already in use */
  2832. if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
  2833. return 1; /* this interface is already in use */
  2834. return 0;
  2835. }
  2836. static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
  2837. const u8 *peer_addr,
  2838. enum p2p_wps_method wps_method,
  2839. int go_intent, const u8 *own_interface_addr,
  2840. unsigned int force_freq, int persistent_group,
  2841. struct wpa_ssid *ssid, unsigned int pref_freq)
  2842. {
  2843. if (persistent_group && wpa_s->conf->persistent_reconnect)
  2844. persistent_group = 2;
  2845. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  2846. return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
  2847. go_intent, own_interface_addr,
  2848. force_freq, persistent_group);
  2849. }
  2850. /*
  2851. * Increase GO config timeout if HT40 is used since it takes some time
  2852. * to scan channels for coex purposes before the BSS can be started.
  2853. */
  2854. p2p_set_config_timeout(wpa_s->global->p2p,
  2855. wpa_s->p2p_go_ht40 ? 255 : 100, 20);
  2856. return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
  2857. go_intent, own_interface_addr, force_freq,
  2858. persistent_group, ssid ? ssid->ssid : NULL,
  2859. ssid ? ssid->ssid_len : 0,
  2860. wpa_s->p2p_pd_before_go_neg, pref_freq);
  2861. }
  2862. static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
  2863. const u8 *peer_addr,
  2864. enum p2p_wps_method wps_method,
  2865. int go_intent, const u8 *own_interface_addr,
  2866. unsigned int force_freq, int persistent_group,
  2867. struct wpa_ssid *ssid, unsigned int pref_freq)
  2868. {
  2869. if (persistent_group && wpa_s->conf->persistent_reconnect)
  2870. persistent_group = 2;
  2871. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  2872. return -1;
  2873. return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
  2874. go_intent, own_interface_addr, force_freq,
  2875. persistent_group, ssid ? ssid->ssid : NULL,
  2876. ssid ? ssid->ssid_len : 0, pref_freq);
  2877. }
  2878. static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
  2879. {
  2880. wpa_s->p2p_join_scan_count++;
  2881. wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
  2882. wpa_s->p2p_join_scan_count);
  2883. if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
  2884. wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
  2885. " for join operationg - stop join attempt",
  2886. MAC2STR(wpa_s->pending_join_iface_addr));
  2887. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2888. if (wpa_s->p2p_auto_pd) {
  2889. wpa_s->p2p_auto_pd = 0;
  2890. wpa_msg_global(wpa_s, MSG_INFO,
  2891. P2P_EVENT_PROV_DISC_FAILURE
  2892. " p2p_dev_addr=" MACSTR " status=N/A",
  2893. MAC2STR(wpa_s->pending_join_dev_addr));
  2894. return;
  2895. }
  2896. wpa_msg_global(wpa_s->parent, MSG_INFO,
  2897. P2P_EVENT_GROUP_FORMATION_FAILURE);
  2898. }
  2899. }
  2900. static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
  2901. {
  2902. int *freqs, res, num, i;
  2903. if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
  2904. /* Multiple channels are supported and not all are in use */
  2905. return 0;
  2906. }
  2907. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  2908. if (!freqs)
  2909. return 1;
  2910. num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
  2911. wpa_s->num_multichan_concurrent);
  2912. if (num < 0) {
  2913. res = 1;
  2914. goto exit_free;
  2915. }
  2916. for (i = 0; i < num; i++) {
  2917. if (freqs[i] == freq) {
  2918. wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
  2919. freq);
  2920. res = 0;
  2921. goto exit_free;
  2922. }
  2923. }
  2924. res = 1;
  2925. exit_free:
  2926. os_free(freqs);
  2927. return res;
  2928. }
  2929. static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
  2930. const u8 *peer_dev_addr)
  2931. {
  2932. struct wpa_bss *bss;
  2933. int updated;
  2934. bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
  2935. if (bss == NULL)
  2936. return -1;
  2937. if (bss->last_update_idx < wpa_s->bss_update_idx) {
  2938. wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
  2939. "last scan");
  2940. return 0;
  2941. }
  2942. updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
  2943. wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
  2944. "%ld.%06ld (%supdated in last scan)",
  2945. bss->last_update.sec, bss->last_update.usec,
  2946. updated ? "": "not ");
  2947. return updated;
  2948. }
  2949. static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
  2950. struct wpa_scan_results *scan_res)
  2951. {
  2952. struct wpa_bss *bss;
  2953. int freq;
  2954. u8 iface_addr[ETH_ALEN];
  2955. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2956. if (wpa_s->global->p2p_disabled)
  2957. return;
  2958. wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
  2959. scan_res ? (int) scan_res->num : -1,
  2960. wpa_s->p2p_auto_join ? "auto_" : "");
  2961. if (scan_res)
  2962. wpas_p2p_scan_res_handler(wpa_s, scan_res);
  2963. if (wpa_s->p2p_auto_pd) {
  2964. int join = wpas_p2p_peer_go(wpa_s,
  2965. wpa_s->pending_join_dev_addr);
  2966. if (join == 0 &&
  2967. wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
  2968. wpa_s->auto_pd_scan_retry++;
  2969. bss = wpa_bss_get_bssid_latest(
  2970. wpa_s, wpa_s->pending_join_dev_addr);
  2971. if (bss) {
  2972. freq = bss->freq;
  2973. wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
  2974. "the peer " MACSTR " at %d MHz",
  2975. wpa_s->auto_pd_scan_retry,
  2976. MAC2STR(wpa_s->
  2977. pending_join_dev_addr),
  2978. freq);
  2979. wpas_p2p_join_scan_req(wpa_s, freq);
  2980. return;
  2981. }
  2982. }
  2983. if (join < 0)
  2984. join = 0;
  2985. wpa_s->p2p_auto_pd = 0;
  2986. wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
  2987. wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
  2988. MAC2STR(wpa_s->pending_join_dev_addr), join);
  2989. if (p2p_prov_disc_req(wpa_s->global->p2p,
  2990. wpa_s->pending_join_dev_addr,
  2991. wpa_s->pending_pd_config_methods, join,
  2992. 0, wpa_s->user_initiated_pd) < 0) {
  2993. wpa_s->p2p_auto_pd = 0;
  2994. wpa_msg_global(wpa_s, MSG_INFO,
  2995. P2P_EVENT_PROV_DISC_FAILURE
  2996. " p2p_dev_addr=" MACSTR " status=N/A",
  2997. MAC2STR(wpa_s->pending_join_dev_addr));
  2998. }
  2999. return;
  3000. }
  3001. if (wpa_s->p2p_auto_join) {
  3002. int join = wpas_p2p_peer_go(wpa_s,
  3003. wpa_s->pending_join_dev_addr);
  3004. if (join < 0) {
  3005. wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
  3006. "running a GO -> use GO Negotiation");
  3007. wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
  3008. wpa_s->p2p_pin, wpa_s->p2p_wps_method,
  3009. wpa_s->p2p_persistent_group, 0, 0, 0,
  3010. wpa_s->p2p_go_intent,
  3011. wpa_s->p2p_connect_freq,
  3012. wpa_s->p2p_persistent_id,
  3013. wpa_s->p2p_pd_before_go_neg,
  3014. wpa_s->p2p_go_ht40);
  3015. return;
  3016. }
  3017. wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
  3018. "try to join the group", join ? "" :
  3019. " in older scan");
  3020. if (!join)
  3021. wpa_s->p2p_fallback_to_go_neg = 1;
  3022. }
  3023. freq = p2p_get_oper_freq(wpa_s->global->p2p,
  3024. wpa_s->pending_join_iface_addr);
  3025. if (freq < 0 &&
  3026. p2p_get_interface_addr(wpa_s->global->p2p,
  3027. wpa_s->pending_join_dev_addr,
  3028. iface_addr) == 0 &&
  3029. os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
  3030. {
  3031. wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
  3032. "address for join from " MACSTR " to " MACSTR
  3033. " based on newly discovered P2P peer entry",
  3034. MAC2STR(wpa_s->pending_join_iface_addr),
  3035. MAC2STR(iface_addr));
  3036. os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
  3037. ETH_ALEN);
  3038. freq = p2p_get_oper_freq(wpa_s->global->p2p,
  3039. wpa_s->pending_join_iface_addr);
  3040. }
  3041. if (freq >= 0) {
  3042. wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
  3043. "from P2P peer table: %d MHz", freq);
  3044. }
  3045. bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
  3046. if (bss) {
  3047. freq = bss->freq;
  3048. wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
  3049. "from BSS table: %d MHz", freq);
  3050. }
  3051. if (freq > 0) {
  3052. u16 method;
  3053. if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
  3054. wpa_msg_global(wpa_s->parent, MSG_INFO,
  3055. P2P_EVENT_GROUP_FORMATION_FAILURE
  3056. "reason=FREQ_CONFLICT");
  3057. return;
  3058. }
  3059. wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
  3060. "prior to joining an existing group (GO " MACSTR
  3061. " freq=%u MHz)",
  3062. MAC2STR(wpa_s->pending_join_dev_addr), freq);
  3063. wpa_s->pending_pd_before_join = 1;
  3064. switch (wpa_s->pending_join_wps_method) {
  3065. case WPS_PIN_DISPLAY:
  3066. method = WPS_CONFIG_KEYPAD;
  3067. break;
  3068. case WPS_PIN_KEYPAD:
  3069. method = WPS_CONFIG_DISPLAY;
  3070. break;
  3071. case WPS_PBC:
  3072. method = WPS_CONFIG_PUSHBUTTON;
  3073. break;
  3074. default:
  3075. method = 0;
  3076. break;
  3077. }
  3078. if ((p2p_get_provisioning_info(wpa_s->global->p2p,
  3079. wpa_s->pending_join_dev_addr) ==
  3080. method)) {
  3081. /*
  3082. * We have already performed provision discovery for
  3083. * joining the group. Proceed directly to join
  3084. * operation without duplicated provision discovery. */
  3085. wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
  3086. "with " MACSTR " already done - proceed to "
  3087. "join",
  3088. MAC2STR(wpa_s->pending_join_dev_addr));
  3089. wpa_s->pending_pd_before_join = 0;
  3090. goto start;
  3091. }
  3092. if (p2p_prov_disc_req(wpa_s->global->p2p,
  3093. wpa_s->pending_join_dev_addr, method, 1,
  3094. freq, wpa_s->user_initiated_pd) < 0) {
  3095. wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
  3096. "Discovery Request before joining an "
  3097. "existing group");
  3098. wpa_s->pending_pd_before_join = 0;
  3099. goto start;
  3100. }
  3101. return;
  3102. }
  3103. wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
  3104. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3105. eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
  3106. wpas_p2p_check_join_scan_limit(wpa_s);
  3107. return;
  3108. start:
  3109. /* Start join operation immediately */
  3110. wpas_p2p_join_start(wpa_s);
  3111. }
  3112. static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
  3113. {
  3114. int ret;
  3115. struct wpa_driver_scan_params params;
  3116. struct wpabuf *wps_ie, *ies;
  3117. size_t ielen;
  3118. int freqs[2] = { 0, 0 };
  3119. os_memset(&params, 0, sizeof(params));
  3120. /* P2P Wildcard SSID */
  3121. params.num_ssids = 1;
  3122. params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
  3123. params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
  3124. wpa_s->wps->dev.p2p = 1;
  3125. wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
  3126. wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
  3127. NULL);
  3128. if (wps_ie == NULL) {
  3129. wpas_p2p_scan_res_join(wpa_s, NULL);
  3130. return;
  3131. }
  3132. ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
  3133. ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
  3134. if (ies == NULL) {
  3135. wpabuf_free(wps_ie);
  3136. wpas_p2p_scan_res_join(wpa_s, NULL);
  3137. return;
  3138. }
  3139. wpabuf_put_buf(ies, wps_ie);
  3140. wpabuf_free(wps_ie);
  3141. p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
  3142. params.p2p_probe = 1;
  3143. params.extra_ies = wpabuf_head(ies);
  3144. params.extra_ies_len = wpabuf_len(ies);
  3145. if (freq > 0) {
  3146. freqs[0] = freq;
  3147. params.freqs = freqs;
  3148. }
  3149. /*
  3150. * Run a scan to update BSS table and start Provision Discovery once
  3151. * the new scan results become available.
  3152. */
  3153. ret = wpa_drv_scan(wpa_s, &params);
  3154. if (!ret) {
  3155. os_get_time(&wpa_s->scan_trigger_time);
  3156. wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
  3157. }
  3158. wpabuf_free(ies);
  3159. if (ret) {
  3160. wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
  3161. "try again later");
  3162. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3163. eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
  3164. wpas_p2p_check_join_scan_limit(wpa_s);
  3165. }
  3166. }
  3167. static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
  3168. {
  3169. struct wpa_supplicant *wpa_s = eloop_ctx;
  3170. wpas_p2p_join_scan_req(wpa_s, 0);
  3171. }
  3172. static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
  3173. const u8 *dev_addr, enum p2p_wps_method wps_method,
  3174. int auto_join)
  3175. {
  3176. wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
  3177. MACSTR " dev " MACSTR ")%s",
  3178. MAC2STR(iface_addr), MAC2STR(dev_addr),
  3179. auto_join ? " (auto_join)" : "");
  3180. wpa_s->p2p_auto_pd = 0;
  3181. wpa_s->p2p_auto_join = !!auto_join;
  3182. os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
  3183. os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
  3184. wpa_s->pending_join_wps_method = wps_method;
  3185. /* Make sure we are not running find during connection establishment */
  3186. wpas_p2p_stop_find(wpa_s);
  3187. wpa_s->p2p_join_scan_count = 0;
  3188. wpas_p2p_join_scan(wpa_s, NULL);
  3189. return 0;
  3190. }
  3191. static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
  3192. {
  3193. struct wpa_supplicant *group;
  3194. struct p2p_go_neg_results res;
  3195. struct wpa_bss *bss;
  3196. group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
  3197. if (group == NULL)
  3198. return -1;
  3199. if (group != wpa_s) {
  3200. os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
  3201. sizeof(group->p2p_pin));
  3202. group->p2p_wps_method = wpa_s->p2p_wps_method;
  3203. } else {
  3204. /*
  3205. * Need to mark the current interface for p2p_group_formation
  3206. * when a separate group interface is not used. This is needed
  3207. * to allow p2p_cancel stop a pending p2p_connect-join.
  3208. * wpas_p2p_init_group_interface() addresses this for the case
  3209. * where a separate group interface is used.
  3210. */
  3211. wpa_s->global->p2p_group_formation = wpa_s;
  3212. }
  3213. group->p2p_in_provisioning = 1;
  3214. group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
  3215. os_memset(&res, 0, sizeof(res));
  3216. os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
  3217. ETH_ALEN);
  3218. res.wps_method = wpa_s->pending_join_wps_method;
  3219. bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
  3220. if (bss) {
  3221. res.freq = bss->freq;
  3222. res.ssid_len = bss->ssid_len;
  3223. os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
  3224. }
  3225. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  3226. wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
  3227. "starting client");
  3228. wpa_drv_cancel_remain_on_channel(wpa_s);
  3229. wpa_s->off_channel_freq = 0;
  3230. wpa_s->roc_waiting_drv_freq = 0;
  3231. }
  3232. wpas_start_wps_enrollee(group, &res);
  3233. /*
  3234. * Allow a longer timeout for join-a-running-group than normal 15
  3235. * second group formation timeout since the GO may not have authorized
  3236. * our connection yet.
  3237. */
  3238. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  3239. eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
  3240. wpa_s, NULL);
  3241. return 0;
  3242. }
  3243. static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
  3244. int *force_freq, int *pref_freq)
  3245. {
  3246. int *freqs, res;
  3247. unsigned int freq_in_use = 0, num, i;
  3248. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  3249. if (!freqs)
  3250. return -1;
  3251. num = get_shared_radio_freqs(wpa_s, freqs,
  3252. wpa_s->num_multichan_concurrent);
  3253. if (freq > 0) {
  3254. if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
  3255. wpa_printf(MSG_DEBUG, "P2P: The forced channel "
  3256. "(%u MHz) is not supported for P2P uses",
  3257. freq);
  3258. res = -3;
  3259. goto exit_free;
  3260. }
  3261. for (i = 0; i < num; i++) {
  3262. if (freqs[i] == freq)
  3263. freq_in_use = 1;
  3264. }
  3265. if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
  3266. wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
  3267. freq);
  3268. res = -2;
  3269. goto exit_free;
  3270. }
  3271. wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
  3272. "requested channel (%u MHz)", freq);
  3273. *force_freq = freq;
  3274. goto exit_ok;
  3275. }
  3276. for (i = 0; i < num; i++) {
  3277. if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
  3278. continue;
  3279. wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
  3280. *force_freq);
  3281. *force_freq = freqs[i];
  3282. if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
  3283. wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency we are already using");
  3284. *pref_freq = *force_freq;
  3285. }
  3286. break;
  3287. }
  3288. if (i == num) {
  3289. if (num < wpa_s->num_multichan_concurrent) {
  3290. wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
  3291. *force_freq = 0;
  3292. } else {
  3293. wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
  3294. res = -2;
  3295. goto exit_free;
  3296. }
  3297. }
  3298. exit_ok:
  3299. res = 0;
  3300. exit_free:
  3301. os_free(freqs);
  3302. return res;
  3303. }
  3304. /**
  3305. * wpas_p2p_connect - Request P2P Group Formation to be started
  3306. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3307. * @peer_addr: Address of the peer P2P Device
  3308. * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
  3309. * @persistent_group: Whether to create a persistent group
  3310. * @auto_join: Whether to select join vs. GO Negotiation automatically
  3311. * @join: Whether to join an existing group (as a client) instead of starting
  3312. * Group Owner negotiation; @peer_addr is BSSID in that case
  3313. * @auth: Whether to only authorize the connection instead of doing that and
  3314. * initiating Group Owner negotiation
  3315. * @go_intent: GO Intent or -1 to use default
  3316. * @freq: Frequency for the group or 0 for auto-selection
  3317. * @persistent_id: Persistent group credentials to use for forcing GO
  3318. * parameters or -1 to generate new values (SSID/passphrase)
  3319. * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
  3320. * interoperability workaround when initiating group formation
  3321. * @ht40: Start GO with 40 MHz channel width
  3322. * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
  3323. * failure, -2 on failure due to channel not currently available,
  3324. * -3 if forced channel is not supported
  3325. */
  3326. int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  3327. const char *pin, enum p2p_wps_method wps_method,
  3328. int persistent_group, int auto_join, int join, int auth,
  3329. int go_intent, int freq, int persistent_id, int pd,
  3330. int ht40)
  3331. {
  3332. int force_freq = 0, pref_freq = 0;
  3333. int ret = 0, res;
  3334. enum wpa_driver_if_type iftype;
  3335. const u8 *if_addr;
  3336. struct wpa_ssid *ssid = NULL;
  3337. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3338. return -1;
  3339. if (persistent_id >= 0) {
  3340. ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
  3341. if (ssid == NULL || ssid->disabled != 2 ||
  3342. ssid->mode != WPAS_MODE_P2P_GO)
  3343. return -1;
  3344. }
  3345. if (go_intent < 0)
  3346. go_intent = wpa_s->conf->p2p_go_intent;
  3347. if (!auth)
  3348. wpa_s->p2p_long_listen = 0;
  3349. wpa_s->p2p_wps_method = wps_method;
  3350. wpa_s->p2p_persistent_group = !!persistent_group;
  3351. wpa_s->p2p_persistent_id = persistent_id;
  3352. wpa_s->p2p_go_intent = go_intent;
  3353. wpa_s->p2p_connect_freq = freq;
  3354. wpa_s->p2p_fallback_to_go_neg = 0;
  3355. wpa_s->p2p_pd_before_go_neg = !!pd;
  3356. wpa_s->p2p_go_ht40 = !!ht40;
  3357. if (pin)
  3358. os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
  3359. else if (wps_method == WPS_PIN_DISPLAY) {
  3360. ret = wps_generate_pin();
  3361. os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
  3362. ret);
  3363. wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
  3364. wpa_s->p2p_pin);
  3365. } else
  3366. wpa_s->p2p_pin[0] = '\0';
  3367. if (join || auto_join) {
  3368. u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
  3369. if (auth) {
  3370. wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
  3371. "connect a running group from " MACSTR,
  3372. MAC2STR(peer_addr));
  3373. os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
  3374. return ret;
  3375. }
  3376. os_memcpy(dev_addr, peer_addr, ETH_ALEN);
  3377. if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
  3378. iface_addr) < 0) {
  3379. os_memcpy(iface_addr, peer_addr, ETH_ALEN);
  3380. p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
  3381. dev_addr);
  3382. }
  3383. if (auto_join) {
  3384. os_get_time(&wpa_s->p2p_auto_started);
  3385. wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
  3386. "%ld.%06ld",
  3387. wpa_s->p2p_auto_started.sec,
  3388. wpa_s->p2p_auto_started.usec);
  3389. }
  3390. wpa_s->user_initiated_pd = 1;
  3391. if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
  3392. auto_join) < 0)
  3393. return -1;
  3394. return ret;
  3395. }
  3396. res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq);
  3397. if (res)
  3398. return res;
  3399. wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
  3400. wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
  3401. if (wpa_s->create_p2p_iface) {
  3402. /* Prepare to add a new interface for the group */
  3403. iftype = WPA_IF_P2P_GROUP;
  3404. if (go_intent == 15)
  3405. iftype = WPA_IF_P2P_GO;
  3406. if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
  3407. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  3408. "interface for the group");
  3409. return -1;
  3410. }
  3411. if_addr = wpa_s->pending_interface_addr;
  3412. } else
  3413. if_addr = wpa_s->own_addr;
  3414. if (auth) {
  3415. if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
  3416. go_intent, if_addr,
  3417. force_freq, persistent_group, ssid,
  3418. pref_freq) < 0)
  3419. return -1;
  3420. return ret;
  3421. }
  3422. if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
  3423. go_intent, if_addr, force_freq,
  3424. persistent_group, ssid, pref_freq) < 0) {
  3425. if (wpa_s->create_p2p_iface)
  3426. wpas_p2p_remove_pending_group_interface(wpa_s);
  3427. return -1;
  3428. }
  3429. return ret;
  3430. }
  3431. /**
  3432. * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
  3433. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3434. * @freq: Frequency of the channel in MHz
  3435. * @duration: Duration of the stay on the channel in milliseconds
  3436. *
  3437. * This callback is called when the driver indicates that it has started the
  3438. * requested remain-on-channel duration.
  3439. */
  3440. void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  3441. unsigned int freq, unsigned int duration)
  3442. {
  3443. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3444. return;
  3445. if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
  3446. p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
  3447. wpa_s->pending_listen_duration);
  3448. wpa_s->pending_listen_freq = 0;
  3449. }
  3450. }
  3451. static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
  3452. unsigned int timeout)
  3453. {
  3454. /* Limit maximum Listen state time based on driver limitation. */
  3455. if (timeout > wpa_s->max_remain_on_chan)
  3456. timeout = wpa_s->max_remain_on_chan;
  3457. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3458. return wpa_drv_p2p_listen(wpa_s, timeout);
  3459. return p2p_listen(wpa_s->global->p2p, timeout);
  3460. }
  3461. /**
  3462. * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
  3463. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3464. * @freq: Frequency of the channel in MHz
  3465. *
  3466. * This callback is called when the driver indicates that a remain-on-channel
  3467. * operation has been completed, i.e., the duration on the requested channel
  3468. * has timed out.
  3469. */
  3470. void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  3471. unsigned int freq)
  3472. {
  3473. wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
  3474. "(p2p_long_listen=%d ms pending_action_tx=%p)",
  3475. wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
  3476. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3477. return;
  3478. if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
  3479. return; /* P2P module started a new operation */
  3480. if (offchannel_pending_action_tx(wpa_s))
  3481. return;
  3482. if (wpa_s->p2p_long_listen > 0)
  3483. wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
  3484. if (wpa_s->p2p_long_listen > 0) {
  3485. wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
  3486. wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
  3487. }
  3488. }
  3489. /**
  3490. * wpas_p2p_group_remove - Remove a P2P group
  3491. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3492. * @ifname: Network interface name of the group interface or "*" to remove all
  3493. * groups
  3494. * Returns: 0 on success, -1 on failure
  3495. *
  3496. * This function is used to remove a P2P group. This can be used to disconnect
  3497. * from a group in which the local end is a P2P Client or to end a P2P Group in
  3498. * case the local end is the Group Owner. If a virtual network interface was
  3499. * created for this group, that interface will be removed. Otherwise, only the
  3500. * configured P2P group network will be removed from the interface.
  3501. */
  3502. int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
  3503. {
  3504. struct wpa_global *global = wpa_s->global;
  3505. if (os_strcmp(ifname, "*") == 0) {
  3506. struct wpa_supplicant *prev;
  3507. wpa_s = global->ifaces;
  3508. while (wpa_s) {
  3509. prev = wpa_s;
  3510. wpa_s = wpa_s->next;
  3511. wpas_p2p_disconnect(prev);
  3512. }
  3513. return 0;
  3514. }
  3515. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3516. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  3517. break;
  3518. }
  3519. return wpas_p2p_disconnect(wpa_s);
  3520. }
  3521. static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
  3522. {
  3523. unsigned int r;
  3524. if (freq == 2) {
  3525. wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
  3526. "band");
  3527. if (wpa_s->best_24_freq > 0 &&
  3528. p2p_supported_freq(wpa_s->global->p2p,
  3529. wpa_s->best_24_freq)) {
  3530. freq = wpa_s->best_24_freq;
  3531. wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
  3532. "channel: %d MHz", freq);
  3533. } else {
  3534. os_get_random((u8 *) &r, sizeof(r));
  3535. freq = 2412 + (r % 3) * 25;
  3536. wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
  3537. "channel: %d MHz", freq);
  3538. }
  3539. }
  3540. if (freq == 5) {
  3541. wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
  3542. "band");
  3543. if (wpa_s->best_5_freq > 0 &&
  3544. p2p_supported_freq(wpa_s->global->p2p,
  3545. wpa_s->best_5_freq)) {
  3546. freq = wpa_s->best_5_freq;
  3547. wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
  3548. "channel: %d MHz", freq);
  3549. } else {
  3550. os_get_random((u8 *) &r, sizeof(r));
  3551. freq = 5180 + (r % 4) * 20;
  3552. if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
  3553. wpa_printf(MSG_DEBUG, "P2P: Could not select "
  3554. "5 GHz channel for P2P group");
  3555. return -1;
  3556. }
  3557. wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
  3558. "channel: %d MHz", freq);
  3559. }
  3560. }
  3561. if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
  3562. wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
  3563. "(%u MHz) is not supported for P2P uses",
  3564. freq);
  3565. return -1;
  3566. }
  3567. return freq;
  3568. }
  3569. static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
  3570. struct p2p_go_neg_results *params,
  3571. int freq, int ht40,
  3572. const struct p2p_channels *channels)
  3573. {
  3574. int res, *freqs;
  3575. unsigned int pref_freq;
  3576. unsigned int num, i;
  3577. os_memset(params, 0, sizeof(*params));
  3578. params->role_go = 1;
  3579. params->ht40 = ht40;
  3580. if (freq) {
  3581. if (!freq_included(channels, freq)) {
  3582. wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
  3583. "accepted", freq);
  3584. return -1;
  3585. }
  3586. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
  3587. "frequency %d MHz", freq);
  3588. params->freq = freq;
  3589. } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
  3590. wpa_s->conf->p2p_oper_channel >= 1 &&
  3591. wpa_s->conf->p2p_oper_channel <= 11 &&
  3592. freq_included(channels,
  3593. 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
  3594. params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
  3595. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
  3596. "frequency %d MHz", params->freq);
  3597. } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
  3598. wpa_s->conf->p2p_oper_reg_class == 116 ||
  3599. wpa_s->conf->p2p_oper_reg_class == 117 ||
  3600. wpa_s->conf->p2p_oper_reg_class == 124 ||
  3601. wpa_s->conf->p2p_oper_reg_class == 126 ||
  3602. wpa_s->conf->p2p_oper_reg_class == 127) &&
  3603. freq_included(channels,
  3604. 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
  3605. params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
  3606. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
  3607. "frequency %d MHz", params->freq);
  3608. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  3609. wpa_s->best_overall_freq > 0 &&
  3610. p2p_supported_freq(wpa_s->global->p2p,
  3611. wpa_s->best_overall_freq) &&
  3612. freq_included(channels, wpa_s->best_overall_freq)) {
  3613. params->freq = wpa_s->best_overall_freq;
  3614. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
  3615. "channel %d MHz", params->freq);
  3616. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  3617. wpa_s->best_24_freq > 0 &&
  3618. p2p_supported_freq(wpa_s->global->p2p,
  3619. wpa_s->best_24_freq) &&
  3620. freq_included(channels, wpa_s->best_24_freq)) {
  3621. params->freq = wpa_s->best_24_freq;
  3622. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
  3623. "channel %d MHz", params->freq);
  3624. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  3625. wpa_s->best_5_freq > 0 &&
  3626. p2p_supported_freq(wpa_s->global->p2p,
  3627. wpa_s->best_5_freq) &&
  3628. freq_included(channels, wpa_s->best_5_freq)) {
  3629. params->freq = wpa_s->best_5_freq;
  3630. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
  3631. "channel %d MHz", params->freq);
  3632. } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
  3633. channels))) {
  3634. params->freq = pref_freq;
  3635. wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
  3636. "channels", params->freq);
  3637. } else {
  3638. int chan;
  3639. for (chan = 0; chan < 11; chan++) {
  3640. params->freq = 2412 + chan * 5;
  3641. if (!wpas_p2p_disallowed_freq(wpa_s->global,
  3642. params->freq) &&
  3643. freq_included(channels, params->freq))
  3644. break;
  3645. }
  3646. if (chan == 11) {
  3647. wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
  3648. "allowed");
  3649. return -1;
  3650. }
  3651. wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
  3652. "known)", params->freq);
  3653. }
  3654. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  3655. if (!freqs)
  3656. return -1;
  3657. res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
  3658. wpa_s->num_multichan_concurrent);
  3659. if (res < 0) {
  3660. os_free(freqs);
  3661. return -1;
  3662. }
  3663. num = res;
  3664. if (!freq) {
  3665. for (i = 0; i < num; i++) {
  3666. if (freq_included(channels, freqs[i])) {
  3667. wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
  3668. freqs[i]);
  3669. params->freq = freqs[i];
  3670. break;
  3671. }
  3672. }
  3673. if (i == num) {
  3674. if (num == wpa_s->num_multichan_concurrent) {
  3675. wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
  3676. os_free(freqs);
  3677. return -1;
  3678. } else {
  3679. wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
  3680. }
  3681. }
  3682. } else {
  3683. for (i = 0; i < num; i++) {
  3684. if (freqs[i] == freq)
  3685. break;
  3686. }
  3687. if (i == num) {
  3688. if (num == wpa_s->num_multichan_concurrent) {
  3689. wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
  3690. os_free(freqs);
  3691. return -1;
  3692. } else {
  3693. wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
  3694. }
  3695. }
  3696. }
  3697. os_free(freqs);
  3698. return 0;
  3699. }
  3700. static struct wpa_supplicant *
  3701. wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
  3702. int go)
  3703. {
  3704. struct wpa_supplicant *group_wpa_s;
  3705. if (!wpas_p2p_create_iface(wpa_s)) {
  3706. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
  3707. "operations");
  3708. return wpa_s;
  3709. }
  3710. if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
  3711. WPA_IF_P2P_CLIENT) < 0) {
  3712. wpa_msg_global(wpa_s, MSG_ERROR,
  3713. "P2P: Failed to add group interface");
  3714. return NULL;
  3715. }
  3716. group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
  3717. if (group_wpa_s == NULL) {
  3718. wpa_msg_global(wpa_s, MSG_ERROR,
  3719. "P2P: Failed to initialize group interface");
  3720. wpas_p2p_remove_pending_group_interface(wpa_s);
  3721. return NULL;
  3722. }
  3723. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
  3724. group_wpa_s->ifname);
  3725. return group_wpa_s;
  3726. }
  3727. /**
  3728. * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
  3729. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3730. * @persistent_group: Whether to create a persistent group
  3731. * @freq: Frequency for the group or 0 to indicate no hardcoding
  3732. * Returns: 0 on success, -1 on failure
  3733. *
  3734. * This function creates a new P2P group with the local end as the Group Owner,
  3735. * i.e., without using Group Owner Negotiation.
  3736. */
  3737. int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
  3738. int freq, int ht40)
  3739. {
  3740. struct p2p_go_neg_results params;
  3741. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3742. return -1;
  3743. /* Make sure we are not running find during connection establishment */
  3744. wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
  3745. wpas_p2p_stop_find_oper(wpa_s);
  3746. freq = wpas_p2p_select_go_freq(wpa_s, freq);
  3747. if (freq < 0)
  3748. return -1;
  3749. if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, NULL))
  3750. return -1;
  3751. if (params.freq &&
  3752. !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
  3753. wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
  3754. "(%u MHz) is not supported for P2P uses",
  3755. params.freq);
  3756. return -1;
  3757. }
  3758. p2p_go_params(wpa_s->global->p2p, &params);
  3759. params.persistent_group = persistent_group;
  3760. wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
  3761. if (wpa_s == NULL)
  3762. return -1;
  3763. wpas_start_wps_go(wpa_s, &params, 0);
  3764. return 0;
  3765. }
  3766. static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
  3767. struct wpa_ssid *params, int addr_allocated)
  3768. {
  3769. struct wpa_ssid *ssid;
  3770. wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
  3771. if (wpa_s == NULL)
  3772. return -1;
  3773. wpa_supplicant_ap_deinit(wpa_s);
  3774. ssid = wpa_config_add_network(wpa_s->conf);
  3775. if (ssid == NULL)
  3776. return -1;
  3777. wpa_config_set_network_defaults(ssid);
  3778. ssid->temporary = 1;
  3779. ssid->proto = WPA_PROTO_RSN;
  3780. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  3781. ssid->group_cipher = WPA_CIPHER_CCMP;
  3782. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  3783. ssid->ssid = os_malloc(params->ssid_len);
  3784. if (ssid->ssid == NULL) {
  3785. wpa_config_remove_network(wpa_s->conf, ssid->id);
  3786. return -1;
  3787. }
  3788. os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
  3789. ssid->ssid_len = params->ssid_len;
  3790. ssid->p2p_group = 1;
  3791. ssid->export_keys = 1;
  3792. if (params->psk_set) {
  3793. os_memcpy(ssid->psk, params->psk, 32);
  3794. ssid->psk_set = 1;
  3795. }
  3796. if (params->passphrase)
  3797. ssid->passphrase = os_strdup(params->passphrase);
  3798. wpa_supplicant_select_network(wpa_s, ssid);
  3799. wpa_s->show_group_started = 1;
  3800. return 0;
  3801. }
  3802. int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
  3803. struct wpa_ssid *ssid, int addr_allocated,
  3804. int freq, int ht40,
  3805. const struct p2p_channels *channels)
  3806. {
  3807. struct p2p_go_neg_results params;
  3808. int go = 0;
  3809. if (ssid->disabled != 2 || ssid->ssid == NULL)
  3810. return -1;
  3811. if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
  3812. go == (ssid->mode == WPAS_MODE_P2P_GO)) {
  3813. wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
  3814. "already running");
  3815. return 0;
  3816. }
  3817. /* Make sure we are not running find during connection establishment */
  3818. wpas_p2p_stop_find_oper(wpa_s);
  3819. wpa_s->p2p_fallback_to_go_neg = 0;
  3820. if (ssid->mode == WPAS_MODE_INFRA)
  3821. return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
  3822. if (ssid->mode != WPAS_MODE_P2P_GO)
  3823. return -1;
  3824. freq = wpas_p2p_select_go_freq(wpa_s, freq);
  3825. if (freq < 0)
  3826. return -1;
  3827. if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, channels))
  3828. return -1;
  3829. params.role_go = 1;
  3830. params.psk_set = ssid->psk_set;
  3831. if (params.psk_set)
  3832. os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
  3833. if (ssid->passphrase) {
  3834. if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
  3835. wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
  3836. "persistent group");
  3837. return -1;
  3838. }
  3839. os_strlcpy(params.passphrase, ssid->passphrase,
  3840. sizeof(params.passphrase));
  3841. }
  3842. os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
  3843. params.ssid_len = ssid->ssid_len;
  3844. params.persistent_group = 1;
  3845. wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
  3846. if (wpa_s == NULL)
  3847. return -1;
  3848. wpas_start_wps_go(wpa_s, &params, 0);
  3849. return 0;
  3850. }
  3851. static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
  3852. struct wpabuf *proberesp_ies)
  3853. {
  3854. struct wpa_supplicant *wpa_s = ctx;
  3855. if (wpa_s->ap_iface) {
  3856. struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
  3857. if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
  3858. wpabuf_free(beacon_ies);
  3859. wpabuf_free(proberesp_ies);
  3860. return;
  3861. }
  3862. if (beacon_ies) {
  3863. wpabuf_free(hapd->p2p_beacon_ie);
  3864. hapd->p2p_beacon_ie = beacon_ies;
  3865. }
  3866. wpabuf_free(hapd->p2p_probe_resp_ie);
  3867. hapd->p2p_probe_resp_ie = proberesp_ies;
  3868. } else {
  3869. wpabuf_free(beacon_ies);
  3870. wpabuf_free(proberesp_ies);
  3871. }
  3872. wpa_supplicant_ap_update_beacon(wpa_s);
  3873. }
  3874. static void wpas_p2p_idle_update(void *ctx, int idle)
  3875. {
  3876. struct wpa_supplicant *wpa_s = ctx;
  3877. if (!wpa_s->ap_iface)
  3878. return;
  3879. wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
  3880. if (idle)
  3881. wpas_p2p_set_group_idle_timeout(wpa_s);
  3882. else
  3883. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  3884. }
  3885. struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
  3886. struct wpa_ssid *ssid)
  3887. {
  3888. struct p2p_group *group;
  3889. struct p2p_group_config *cfg;
  3890. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3891. return NULL;
  3892. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3893. return NULL;
  3894. cfg = os_zalloc(sizeof(*cfg));
  3895. if (cfg == NULL)
  3896. return NULL;
  3897. if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
  3898. cfg->persistent_group = 2;
  3899. else if (ssid->p2p_persistent_group)
  3900. cfg->persistent_group = 1;
  3901. os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
  3902. if (wpa_s->max_stations &&
  3903. wpa_s->max_stations < wpa_s->conf->max_num_sta)
  3904. cfg->max_clients = wpa_s->max_stations;
  3905. else
  3906. cfg->max_clients = wpa_s->conf->max_num_sta;
  3907. os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
  3908. cfg->ssid_len = ssid->ssid_len;
  3909. cfg->cb_ctx = wpa_s;
  3910. cfg->ie_update = wpas_p2p_ie_update;
  3911. cfg->idle_update = wpas_p2p_idle_update;
  3912. group = p2p_group_init(wpa_s->global->p2p, cfg);
  3913. if (group == NULL)
  3914. os_free(cfg);
  3915. if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
  3916. p2p_group_notif_formation_done(group);
  3917. wpa_s->p2p_group = group;
  3918. return group;
  3919. }
  3920. void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  3921. int registrar)
  3922. {
  3923. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3924. if (!wpa_s->p2p_in_provisioning) {
  3925. wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
  3926. "provisioning not in progress");
  3927. return;
  3928. }
  3929. if (ssid && ssid->mode == WPAS_MODE_INFRA) {
  3930. u8 go_dev_addr[ETH_ALEN];
  3931. os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
  3932. wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
  3933. ssid->ssid_len);
  3934. /* Clear any stored provisioning info */
  3935. p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
  3936. }
  3937. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
  3938. NULL);
  3939. if (ssid && ssid->mode == WPAS_MODE_INFRA) {
  3940. /*
  3941. * Use a separate timeout for initial data connection to
  3942. * complete to allow the group to be removed automatically if
  3943. * something goes wrong in this step before the P2P group idle
  3944. * timeout mechanism is taken into use.
  3945. */
  3946. eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
  3947. wpas_p2p_group_formation_timeout,
  3948. wpa_s->parent, NULL);
  3949. }
  3950. if (wpa_s->global->p2p)
  3951. p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
  3952. else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3953. wpa_drv_wps_success_cb(wpa_s, peer_addr);
  3954. wpas_group_formation_completed(wpa_s, 1);
  3955. }
  3956. void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
  3957. struct wps_event_fail *fail)
  3958. {
  3959. if (!wpa_s->p2p_in_provisioning) {
  3960. wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
  3961. "provisioning not in progress");
  3962. return;
  3963. }
  3964. if (wpa_s->go_params) {
  3965. p2p_clear_provisioning_info(
  3966. wpa_s->global->p2p,
  3967. wpa_s->go_params->peer_device_addr);
  3968. }
  3969. wpas_notify_p2p_wps_failed(wpa_s, fail);
  3970. }
  3971. int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  3972. const char *config_method,
  3973. enum wpas_p2p_prov_disc_use use)
  3974. {
  3975. u16 config_methods;
  3976. wpa_s->p2p_fallback_to_go_neg = 0;
  3977. wpa_s->pending_pd_use = NORMAL_PD;
  3978. if (os_strncmp(config_method, "display", 7) == 0)
  3979. config_methods = WPS_CONFIG_DISPLAY;
  3980. else if (os_strncmp(config_method, "keypad", 6) == 0)
  3981. config_methods = WPS_CONFIG_KEYPAD;
  3982. else if (os_strncmp(config_method, "pbc", 3) == 0 ||
  3983. os_strncmp(config_method, "pushbutton", 10) == 0)
  3984. config_methods = WPS_CONFIG_PUSHBUTTON;
  3985. else {
  3986. wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
  3987. return -1;
  3988. }
  3989. if (use == WPAS_P2P_PD_AUTO) {
  3990. os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
  3991. wpa_s->pending_pd_config_methods = config_methods;
  3992. wpa_s->p2p_auto_pd = 1;
  3993. wpa_s->p2p_auto_join = 0;
  3994. wpa_s->pending_pd_before_join = 0;
  3995. wpa_s->auto_pd_scan_retry = 0;
  3996. wpas_p2p_stop_find(wpa_s);
  3997. wpa_s->p2p_join_scan_count = 0;
  3998. os_get_time(&wpa_s->p2p_auto_started);
  3999. wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
  4000. wpa_s->p2p_auto_started.sec,
  4001. wpa_s->p2p_auto_started.usec);
  4002. wpas_p2p_join_scan(wpa_s, NULL);
  4003. return 0;
  4004. }
  4005. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  4006. return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
  4007. config_methods,
  4008. use == WPAS_P2P_PD_FOR_JOIN);
  4009. }
  4010. if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
  4011. return -1;
  4012. return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
  4013. config_methods, use == WPAS_P2P_PD_FOR_JOIN,
  4014. 0, 1);
  4015. }
  4016. int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
  4017. char *end)
  4018. {
  4019. return p2p_scan_result_text(ies, ies_len, buf, end);
  4020. }
  4021. static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
  4022. {
  4023. if (!offchannel_pending_action_tx(wpa_s))
  4024. return;
  4025. wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
  4026. "operation request");
  4027. offchannel_clear_pending_action_tx(wpa_s);
  4028. }
  4029. int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
  4030. enum p2p_discovery_type type,
  4031. unsigned int num_req_dev_types, const u8 *req_dev_types,
  4032. const u8 *dev_id, unsigned int search_delay)
  4033. {
  4034. wpas_p2p_clear_pending_action_tx(wpa_s);
  4035. wpa_s->p2p_long_listen = 0;
  4036. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4037. return wpa_drv_p2p_find(wpa_s, timeout, type);
  4038. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
  4039. wpa_s->p2p_in_provisioning)
  4040. return -1;
  4041. wpa_supplicant_cancel_sched_scan(wpa_s);
  4042. return p2p_find(wpa_s->global->p2p, timeout, type,
  4043. num_req_dev_types, req_dev_types, dev_id,
  4044. search_delay);
  4045. }
  4046. static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
  4047. {
  4048. wpas_p2p_clear_pending_action_tx(wpa_s);
  4049. wpa_s->p2p_long_listen = 0;
  4050. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  4051. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  4052. wpa_s->global->p2p_cb_on_scan_complete = 0;
  4053. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  4054. wpa_drv_p2p_stop_find(wpa_s);
  4055. return 1;
  4056. }
  4057. if (wpa_s->global->p2p)
  4058. p2p_stop_find(wpa_s->global->p2p);
  4059. return 0;
  4060. }
  4061. void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
  4062. {
  4063. if (wpas_p2p_stop_find_oper(wpa_s) > 0)
  4064. return;
  4065. wpas_p2p_remove_pending_group_interface(wpa_s);
  4066. }
  4067. static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
  4068. {
  4069. struct wpa_supplicant *wpa_s = eloop_ctx;
  4070. wpa_s->p2p_long_listen = 0;
  4071. }
  4072. int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
  4073. {
  4074. int res;
  4075. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4076. return -1;
  4077. wpa_supplicant_cancel_sched_scan(wpa_s);
  4078. wpas_p2p_clear_pending_action_tx(wpa_s);
  4079. if (timeout == 0) {
  4080. /*
  4081. * This is a request for unlimited Listen state. However, at
  4082. * least for now, this is mapped to a Listen state for one
  4083. * hour.
  4084. */
  4085. timeout = 3600;
  4086. }
  4087. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  4088. wpa_s->p2p_long_listen = 0;
  4089. /*
  4090. * Stop previous find/listen operation to avoid trying to request a new
  4091. * remain-on-channel operation while the driver is still running the
  4092. * previous one.
  4093. */
  4094. if (wpa_s->global->p2p)
  4095. p2p_stop_find(wpa_s->global->p2p);
  4096. res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
  4097. if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
  4098. wpa_s->p2p_long_listen = timeout * 1000;
  4099. eloop_register_timeout(timeout, 0,
  4100. wpas_p2p_long_listen_timeout,
  4101. wpa_s, NULL);
  4102. }
  4103. return res;
  4104. }
  4105. int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  4106. u8 *buf, size_t len, int p2p_group)
  4107. {
  4108. struct wpabuf *p2p_ie;
  4109. int ret;
  4110. if (wpa_s->global->p2p_disabled)
  4111. return -1;
  4112. if (wpa_s->global->p2p == NULL)
  4113. return -1;
  4114. if (bss == NULL)
  4115. return -1;
  4116. p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  4117. ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
  4118. p2p_group, p2p_ie);
  4119. wpabuf_free(p2p_ie);
  4120. return ret;
  4121. }
  4122. int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
  4123. const u8 *dst, const u8 *bssid,
  4124. const u8 *ie, size_t ie_len, int ssi_signal)
  4125. {
  4126. if (wpa_s->global->p2p_disabled)
  4127. return 0;
  4128. if (wpa_s->global->p2p == NULL)
  4129. return 0;
  4130. switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
  4131. ie, ie_len)) {
  4132. case P2P_PREQ_NOT_P2P:
  4133. wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
  4134. ssi_signal);
  4135. /* fall through */
  4136. case P2P_PREQ_MALFORMED:
  4137. case P2P_PREQ_NOT_LISTEN:
  4138. case P2P_PREQ_NOT_PROCESSED:
  4139. default: /* make gcc happy */
  4140. return 0;
  4141. case P2P_PREQ_PROCESSED:
  4142. return 1;
  4143. }
  4144. }
  4145. void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
  4146. const u8 *sa, const u8 *bssid,
  4147. u8 category, const u8 *data, size_t len, int freq)
  4148. {
  4149. if (wpa_s->global->p2p_disabled)
  4150. return;
  4151. if (wpa_s->global->p2p == NULL)
  4152. return;
  4153. p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
  4154. freq);
  4155. }
  4156. void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
  4157. {
  4158. if (wpa_s->global->p2p_disabled)
  4159. return;
  4160. if (wpa_s->global->p2p == NULL)
  4161. return;
  4162. p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
  4163. }
  4164. void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
  4165. {
  4166. p2p_group_deinit(wpa_s->p2p_group);
  4167. wpa_s->p2p_group = NULL;
  4168. wpa_s->ap_configured_cb = NULL;
  4169. wpa_s->ap_configured_cb_ctx = NULL;
  4170. wpa_s->ap_configured_cb_data = NULL;
  4171. wpa_s->connect_without_scan = NULL;
  4172. }
  4173. int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
  4174. {
  4175. wpa_s->p2p_long_listen = 0;
  4176. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4177. return wpa_drv_p2p_reject(wpa_s, addr);
  4178. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4179. return -1;
  4180. return p2p_reject(wpa_s->global->p2p, addr);
  4181. }
  4182. /* Invite to reinvoke a persistent group */
  4183. int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  4184. struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
  4185. int ht40, int pref_freq)
  4186. {
  4187. enum p2p_invite_role role;
  4188. u8 *bssid = NULL;
  4189. int force_freq = 0;
  4190. int res;
  4191. wpa_s->global->p2p_invite_group = NULL;
  4192. if (peer_addr)
  4193. os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
  4194. else
  4195. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  4196. wpa_s->p2p_persistent_go_freq = freq;
  4197. wpa_s->p2p_go_ht40 = !!ht40;
  4198. if (ssid->mode == WPAS_MODE_P2P_GO) {
  4199. role = P2P_INVITE_ROLE_GO;
  4200. if (peer_addr == NULL) {
  4201. wpa_printf(MSG_DEBUG, "P2P: Missing peer "
  4202. "address in invitation command");
  4203. return -1;
  4204. }
  4205. if (wpas_p2p_create_iface(wpa_s)) {
  4206. if (wpas_p2p_add_group_interface(wpa_s,
  4207. WPA_IF_P2P_GO) < 0) {
  4208. wpa_printf(MSG_ERROR, "P2P: Failed to "
  4209. "allocate a new interface for the "
  4210. "group");
  4211. return -1;
  4212. }
  4213. bssid = wpa_s->pending_interface_addr;
  4214. } else
  4215. bssid = wpa_s->own_addr;
  4216. } else {
  4217. role = P2P_INVITE_ROLE_CLIENT;
  4218. peer_addr = ssid->bssid;
  4219. }
  4220. wpa_s->pending_invite_ssid_id = ssid->id;
  4221. res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq);
  4222. if (res)
  4223. return res;
  4224. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4225. return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
  4226. ssid->ssid, ssid->ssid_len,
  4227. go_dev_addr, 1);
  4228. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4229. return -1;
  4230. return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
  4231. ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
  4232. 1, pref_freq);
  4233. }
  4234. /* Invite to join an active group */
  4235. int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
  4236. const u8 *peer_addr, const u8 *go_dev_addr)
  4237. {
  4238. struct wpa_global *global = wpa_s->global;
  4239. enum p2p_invite_role role;
  4240. u8 *bssid = NULL;
  4241. struct wpa_ssid *ssid;
  4242. int persistent;
  4243. int force_freq = 0, pref_freq = 0;
  4244. int res;
  4245. wpa_s->p2p_persistent_go_freq = 0;
  4246. wpa_s->p2p_go_ht40 = 0;
  4247. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  4248. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  4249. break;
  4250. }
  4251. if (wpa_s == NULL) {
  4252. wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
  4253. return -1;
  4254. }
  4255. ssid = wpa_s->current_ssid;
  4256. if (ssid == NULL) {
  4257. wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
  4258. "invitation");
  4259. return -1;
  4260. }
  4261. wpa_s->global->p2p_invite_group = wpa_s;
  4262. persistent = ssid->p2p_persistent_group &&
  4263. wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
  4264. ssid->ssid, ssid->ssid_len);
  4265. if (ssid->mode == WPAS_MODE_P2P_GO) {
  4266. role = P2P_INVITE_ROLE_ACTIVE_GO;
  4267. bssid = wpa_s->own_addr;
  4268. if (go_dev_addr == NULL)
  4269. go_dev_addr = wpa_s->global->p2p_dev_addr;
  4270. } else {
  4271. role = P2P_INVITE_ROLE_CLIENT;
  4272. if (wpa_s->wpa_state < WPA_ASSOCIATED) {
  4273. wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
  4274. "invite to current group");
  4275. return -1;
  4276. }
  4277. bssid = wpa_s->bssid;
  4278. if (go_dev_addr == NULL &&
  4279. !is_zero_ether_addr(wpa_s->go_dev_addr))
  4280. go_dev_addr = wpa_s->go_dev_addr;
  4281. }
  4282. wpa_s->parent->pending_invite_ssid_id = -1;
  4283. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4284. return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
  4285. ssid->ssid, ssid->ssid_len,
  4286. go_dev_addr, persistent);
  4287. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4288. return -1;
  4289. res = wpas_p2p_setup_freqs(wpa_s, 0, &force_freq, &pref_freq);
  4290. if (res)
  4291. return res;
  4292. wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
  4293. return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
  4294. ssid->ssid, ssid->ssid_len, force_freq,
  4295. go_dev_addr, persistent, pref_freq);
  4296. }
  4297. void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
  4298. {
  4299. struct wpa_ssid *ssid = wpa_s->current_ssid;
  4300. const char *ssid_txt;
  4301. u8 go_dev_addr[ETH_ALEN];
  4302. int network_id = -1;
  4303. int persistent;
  4304. int freq;
  4305. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
  4306. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  4307. wpa_s->parent, NULL);
  4308. }
  4309. if (!wpa_s->show_group_started || !ssid)
  4310. goto done;
  4311. wpa_s->show_group_started = 0;
  4312. ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
  4313. os_memset(go_dev_addr, 0, ETH_ALEN);
  4314. if (ssid->bssid_set)
  4315. os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
  4316. persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
  4317. ssid->ssid_len);
  4318. os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
  4319. if (wpa_s->global->p2p_group_formation == wpa_s)
  4320. wpa_s->global->p2p_group_formation = NULL;
  4321. freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
  4322. (int) wpa_s->assoc_freq;
  4323. if (ssid->passphrase == NULL && ssid->psk_set) {
  4324. char psk[65];
  4325. wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
  4326. wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  4327. "%s client ssid=\"%s\" freq=%d psk=%s "
  4328. "go_dev_addr=" MACSTR "%s",
  4329. wpa_s->ifname, ssid_txt, freq, psk,
  4330. MAC2STR(go_dev_addr),
  4331. persistent ? " [PERSISTENT]" : "");
  4332. } else {
  4333. wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  4334. "%s client ssid=\"%s\" freq=%d "
  4335. "passphrase=\"%s\" go_dev_addr=" MACSTR "%s",
  4336. wpa_s->ifname, ssid_txt, freq,
  4337. ssid->passphrase ? ssid->passphrase : "",
  4338. MAC2STR(go_dev_addr),
  4339. persistent ? " [PERSISTENT]" : "");
  4340. }
  4341. if (persistent)
  4342. network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
  4343. ssid, go_dev_addr);
  4344. if (network_id < 0)
  4345. network_id = ssid->id;
  4346. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
  4347. done:
  4348. wpas_p2p_continue_after_scan(wpa_s);
  4349. }
  4350. int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
  4351. u32 interval1, u32 duration2, u32 interval2)
  4352. {
  4353. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4354. return -1;
  4355. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4356. return -1;
  4357. if (wpa_s->wpa_state < WPA_ASSOCIATED ||
  4358. wpa_s->current_ssid == NULL ||
  4359. wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
  4360. return -1;
  4361. return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
  4362. wpa_s->own_addr, wpa_s->assoc_freq,
  4363. duration1, interval1, duration2, interval2);
  4364. }
  4365. int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
  4366. unsigned int interval)
  4367. {
  4368. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4369. return -1;
  4370. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4371. return -1;
  4372. return p2p_ext_listen(wpa_s->global->p2p, period, interval);
  4373. }
  4374. static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
  4375. {
  4376. if (wpa_s->current_ssid == NULL) {
  4377. /*
  4378. * current_ssid can be cleared when P2P client interface gets
  4379. * disconnected, so assume this interface was used as P2P
  4380. * client.
  4381. */
  4382. return 1;
  4383. }
  4384. return wpa_s->current_ssid->p2p_group &&
  4385. wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
  4386. }
  4387. static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
  4388. {
  4389. struct wpa_supplicant *wpa_s = eloop_ctx;
  4390. if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
  4391. wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
  4392. "disabled");
  4393. return;
  4394. }
  4395. wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
  4396. "group");
  4397. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
  4398. }
  4399. static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
  4400. {
  4401. int timeout;
  4402. if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
  4403. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
  4404. if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
  4405. return;
  4406. timeout = wpa_s->conf->p2p_group_idle;
  4407. if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
  4408. (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
  4409. timeout = P2P_MAX_CLIENT_IDLE;
  4410. if (timeout == 0)
  4411. return;
  4412. if (timeout < 0) {
  4413. if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
  4414. timeout = 0; /* special client mode no-timeout */
  4415. else
  4416. return;
  4417. }
  4418. if (wpa_s->p2p_in_provisioning) {
  4419. /*
  4420. * Use the normal group formation timeout during the
  4421. * provisioning phase to avoid terminating this process too
  4422. * early due to group idle timeout.
  4423. */
  4424. wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
  4425. "during provisioning");
  4426. return;
  4427. }
  4428. if (wpa_s->show_group_started) {
  4429. /*
  4430. * Use the normal group formation timeout between the end of
  4431. * the provisioning phase and completion of 4-way handshake to
  4432. * avoid terminating this process too early due to group idle
  4433. * timeout.
  4434. */
  4435. wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
  4436. "while waiting for initial 4-way handshake to "
  4437. "complete");
  4438. return;
  4439. }
  4440. wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
  4441. timeout);
  4442. eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
  4443. wpa_s, NULL);
  4444. }
  4445. /* Returns 1 if the interface was removed */
  4446. int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
  4447. u16 reason_code, const u8 *ie, size_t ie_len,
  4448. int locally_generated)
  4449. {
  4450. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4451. return 0;
  4452. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4453. return 0;
  4454. if (!locally_generated)
  4455. p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
  4456. ie_len);
  4457. if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
  4458. wpa_s->current_ssid &&
  4459. wpa_s->current_ssid->p2p_group &&
  4460. wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
  4461. wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
  4462. "session is ending");
  4463. if (wpas_p2p_group_delete(wpa_s,
  4464. P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
  4465. > 0)
  4466. return 1;
  4467. }
  4468. return 0;
  4469. }
  4470. void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
  4471. u16 reason_code, const u8 *ie, size_t ie_len,
  4472. int locally_generated)
  4473. {
  4474. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4475. return;
  4476. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4477. return;
  4478. if (!locally_generated)
  4479. p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
  4480. ie_len);
  4481. }
  4482. void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
  4483. {
  4484. struct p2p_data *p2p = wpa_s->global->p2p;
  4485. if (p2p == NULL)
  4486. return;
  4487. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
  4488. return;
  4489. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
  4490. p2p_set_dev_name(p2p, wpa_s->conf->device_name);
  4491. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
  4492. p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
  4493. if (wpa_s->wps &&
  4494. (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
  4495. p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
  4496. if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
  4497. p2p_set_uuid(p2p, wpa_s->wps->uuid);
  4498. if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
  4499. p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
  4500. p2p_set_model_name(p2p, wpa_s->conf->model_name);
  4501. p2p_set_model_number(p2p, wpa_s->conf->model_number);
  4502. p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
  4503. }
  4504. if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
  4505. p2p_set_sec_dev_types(p2p,
  4506. (void *) wpa_s->conf->sec_device_type,
  4507. wpa_s->conf->num_sec_device_types);
  4508. if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
  4509. int i;
  4510. p2p_remove_wps_vendor_extensions(p2p);
  4511. for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
  4512. if (wpa_s->conf->wps_vendor_ext[i] == NULL)
  4513. continue;
  4514. p2p_add_wps_vendor_extension(
  4515. p2p, wpa_s->conf->wps_vendor_ext[i]);
  4516. }
  4517. }
  4518. if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
  4519. wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  4520. char country[3];
  4521. country[0] = wpa_s->conf->country[0];
  4522. country[1] = wpa_s->conf->country[1];
  4523. country[2] = 0x04;
  4524. p2p_set_country(p2p, country);
  4525. }
  4526. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
  4527. p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
  4528. wpa_s->conf->p2p_ssid_postfix ?
  4529. os_strlen(wpa_s->conf->p2p_ssid_postfix) :
  4530. 0);
  4531. }
  4532. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
  4533. p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
  4534. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
  4535. u8 reg_class, channel;
  4536. int ret;
  4537. unsigned int r;
  4538. if (wpa_s->conf->p2p_listen_reg_class &&
  4539. wpa_s->conf->p2p_listen_channel) {
  4540. reg_class = wpa_s->conf->p2p_listen_reg_class;
  4541. channel = wpa_s->conf->p2p_listen_channel;
  4542. } else {
  4543. reg_class = 81;
  4544. /*
  4545. * Pick one of the social channels randomly as the
  4546. * listen channel.
  4547. */
  4548. os_get_random((u8 *) &r, sizeof(r));
  4549. channel = 1 + (r % 3) * 5;
  4550. }
  4551. ret = p2p_set_listen_channel(p2p, reg_class, channel);
  4552. if (ret)
  4553. wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
  4554. "failed: %d", ret);
  4555. }
  4556. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
  4557. u8 op_reg_class, op_channel, cfg_op_channel;
  4558. int ret = 0;
  4559. unsigned int r;
  4560. if (wpa_s->conf->p2p_oper_reg_class &&
  4561. wpa_s->conf->p2p_oper_channel) {
  4562. op_reg_class = wpa_s->conf->p2p_oper_reg_class;
  4563. op_channel = wpa_s->conf->p2p_oper_channel;
  4564. cfg_op_channel = 1;
  4565. } else {
  4566. op_reg_class = 81;
  4567. /*
  4568. * Use random operation channel from (1, 6, 11)
  4569. *if no other preference is indicated.
  4570. */
  4571. os_get_random((u8 *) &r, sizeof(r));
  4572. op_channel = 1 + (r % 3) * 5;
  4573. cfg_op_channel = 0;
  4574. }
  4575. ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
  4576. cfg_op_channel);
  4577. if (ret)
  4578. wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
  4579. "failed: %d", ret);
  4580. }
  4581. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
  4582. if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
  4583. wpa_s->conf->p2p_pref_chan) < 0) {
  4584. wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
  4585. "update failed");
  4586. }
  4587. }
  4588. }
  4589. int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
  4590. int duration)
  4591. {
  4592. if (!wpa_s->ap_iface)
  4593. return -1;
  4594. return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
  4595. duration);
  4596. }
  4597. int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
  4598. {
  4599. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4600. return -1;
  4601. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4602. return -1;
  4603. wpa_s->global->cross_connection = enabled;
  4604. p2p_set_cross_connect(wpa_s->global->p2p, enabled);
  4605. if (!enabled) {
  4606. struct wpa_supplicant *iface;
  4607. for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
  4608. {
  4609. if (iface->cross_connect_enabled == 0)
  4610. continue;
  4611. iface->cross_connect_enabled = 0;
  4612. iface->cross_connect_in_use = 0;
  4613. wpa_msg_global(iface->parent, MSG_INFO,
  4614. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  4615. iface->ifname,
  4616. iface->cross_connect_uplink);
  4617. }
  4618. }
  4619. return 0;
  4620. }
  4621. static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
  4622. {
  4623. struct wpa_supplicant *iface;
  4624. if (!uplink->global->cross_connection)
  4625. return;
  4626. for (iface = uplink->global->ifaces; iface; iface = iface->next) {
  4627. if (!iface->cross_connect_enabled)
  4628. continue;
  4629. if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
  4630. 0)
  4631. continue;
  4632. if (iface->ap_iface == NULL)
  4633. continue;
  4634. if (iface->cross_connect_in_use)
  4635. continue;
  4636. iface->cross_connect_in_use = 1;
  4637. wpa_msg_global(iface->parent, MSG_INFO,
  4638. P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
  4639. iface->ifname, iface->cross_connect_uplink);
  4640. }
  4641. }
  4642. static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
  4643. {
  4644. struct wpa_supplicant *iface;
  4645. for (iface = uplink->global->ifaces; iface; iface = iface->next) {
  4646. if (!iface->cross_connect_enabled)
  4647. continue;
  4648. if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
  4649. 0)
  4650. continue;
  4651. if (!iface->cross_connect_in_use)
  4652. continue;
  4653. wpa_msg_global(iface->parent, MSG_INFO,
  4654. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  4655. iface->ifname, iface->cross_connect_uplink);
  4656. iface->cross_connect_in_use = 0;
  4657. }
  4658. }
  4659. void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
  4660. {
  4661. if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
  4662. wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
  4663. wpa_s->cross_connect_disallowed)
  4664. wpas_p2p_disable_cross_connect(wpa_s);
  4665. else
  4666. wpas_p2p_enable_cross_connect(wpa_s);
  4667. if (!wpa_s->ap_iface &&
  4668. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
  4669. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
  4670. }
  4671. void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
  4672. {
  4673. wpas_p2p_disable_cross_connect(wpa_s);
  4674. if (!wpa_s->ap_iface &&
  4675. !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
  4676. wpa_s, NULL))
  4677. wpas_p2p_set_group_idle_timeout(wpa_s);
  4678. }
  4679. static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
  4680. {
  4681. struct wpa_supplicant *iface;
  4682. if (!wpa_s->global->cross_connection)
  4683. return;
  4684. for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
  4685. if (iface == wpa_s)
  4686. continue;
  4687. if (iface->drv_flags &
  4688. WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
  4689. continue;
  4690. if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
  4691. continue;
  4692. wpa_s->cross_connect_enabled = 1;
  4693. os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
  4694. sizeof(wpa_s->cross_connect_uplink));
  4695. wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
  4696. "%s to %s whenever uplink is available",
  4697. wpa_s->ifname, wpa_s->cross_connect_uplink);
  4698. if (iface->ap_iface || iface->current_ssid == NULL ||
  4699. iface->current_ssid->mode != WPAS_MODE_INFRA ||
  4700. iface->cross_connect_disallowed ||
  4701. iface->wpa_state != WPA_COMPLETED)
  4702. break;
  4703. wpa_s->cross_connect_in_use = 1;
  4704. wpa_msg_global(wpa_s->parent, MSG_INFO,
  4705. P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
  4706. wpa_s->ifname, wpa_s->cross_connect_uplink);
  4707. break;
  4708. }
  4709. }
  4710. int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
  4711. {
  4712. if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
  4713. !wpa_s->p2p_in_provisioning)
  4714. return 0; /* not P2P client operation */
  4715. wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
  4716. "session overlap");
  4717. if (wpa_s != wpa_s->parent)
  4718. wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
  4719. wpas_p2p_group_formation_failed(wpa_s);
  4720. return 1;
  4721. }
  4722. void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
  4723. {
  4724. struct p2p_channels chan;
  4725. if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
  4726. return;
  4727. os_memset(&chan, 0, sizeof(chan));
  4728. if (wpas_p2p_setup_channels(wpa_s, &chan)) {
  4729. wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
  4730. "channel list");
  4731. return;
  4732. }
  4733. p2p_update_channel_list(wpa_s->global->p2p, &chan);
  4734. }
  4735. static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
  4736. struct wpa_scan_results *scan_res)
  4737. {
  4738. wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
  4739. }
  4740. int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
  4741. {
  4742. struct wpa_global *global = wpa_s->global;
  4743. int found = 0;
  4744. const u8 *peer;
  4745. if (global->p2p == NULL)
  4746. return -1;
  4747. wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
  4748. if (wpa_s->pending_interface_name[0] &&
  4749. !is_zero_ether_addr(wpa_s->pending_interface_addr))
  4750. found = 1;
  4751. peer = p2p_get_go_neg_peer(global->p2p);
  4752. if (peer) {
  4753. wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
  4754. MACSTR, MAC2STR(peer));
  4755. p2p_unauthorize(global->p2p, peer);
  4756. found = 1;
  4757. }
  4758. if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
  4759. wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
  4760. wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
  4761. found = 1;
  4762. }
  4763. if (wpa_s->pending_pd_before_join) {
  4764. wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
  4765. wpa_s->pending_pd_before_join = 0;
  4766. found = 1;
  4767. }
  4768. wpas_p2p_stop_find(wpa_s);
  4769. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  4770. if (wpa_s == global->p2p_group_formation &&
  4771. (wpa_s->p2p_in_provisioning ||
  4772. wpa_s->parent->pending_interface_type ==
  4773. WPA_IF_P2P_CLIENT)) {
  4774. wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
  4775. "formation found - cancelling",
  4776. wpa_s->ifname);
  4777. found = 1;
  4778. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  4779. wpa_s->parent, NULL);
  4780. if (wpa_s->p2p_in_provisioning) {
  4781. wpas_group_formation_completed(wpa_s, 0);
  4782. break;
  4783. }
  4784. wpas_p2p_group_delete(wpa_s,
  4785. P2P_GROUP_REMOVAL_REQUESTED);
  4786. break;
  4787. }
  4788. }
  4789. if (!found) {
  4790. wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
  4791. return -1;
  4792. }
  4793. return 0;
  4794. }
  4795. void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
  4796. {
  4797. if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
  4798. return;
  4799. wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
  4800. "being available anymore");
  4801. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
  4802. }
  4803. void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
  4804. int freq_24, int freq_5, int freq_overall)
  4805. {
  4806. struct p2p_data *p2p = wpa_s->global->p2p;
  4807. if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
  4808. return;
  4809. p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
  4810. }
  4811. int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
  4812. {
  4813. u8 peer[ETH_ALEN];
  4814. struct p2p_data *p2p = wpa_s->global->p2p;
  4815. if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
  4816. return -1;
  4817. if (hwaddr_aton(addr, peer))
  4818. return -1;
  4819. return p2p_unauthorize(p2p, peer);
  4820. }
  4821. /**
  4822. * wpas_p2p_disconnect - Disconnect from a P2P Group
  4823. * @wpa_s: Pointer to wpa_supplicant data
  4824. * Returns: 0 on success, -1 on failure
  4825. *
  4826. * This can be used to disconnect from a group in which the local end is a P2P
  4827. * Client or to end a P2P Group in case the local end is the Group Owner. If a
  4828. * virtual network interface was created for this group, that interface will be
  4829. * removed. Otherwise, only the configured P2P group network will be removed
  4830. * from the interface.
  4831. */
  4832. int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
  4833. {
  4834. if (wpa_s == NULL)
  4835. return -1;
  4836. return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
  4837. -1 : 0;
  4838. }
  4839. int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
  4840. {
  4841. int ret;
  4842. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4843. return 0;
  4844. ret = p2p_in_progress(wpa_s->global->p2p);
  4845. if (ret == 0) {
  4846. /*
  4847. * Check whether there is an ongoing WPS provisioning step (or
  4848. * other parts of group formation) on another interface since
  4849. * p2p_in_progress() does not report this to avoid issues for
  4850. * scans during such provisioning step.
  4851. */
  4852. if (wpa_s->global->p2p_group_formation &&
  4853. wpa_s->global->p2p_group_formation != wpa_s) {
  4854. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
  4855. "in group formation",
  4856. wpa_s->global->p2p_group_formation->ifname);
  4857. ret = 1;
  4858. }
  4859. }
  4860. return ret;
  4861. }
  4862. void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
  4863. struct wpa_ssid *ssid)
  4864. {
  4865. if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
  4866. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  4867. wpa_s->parent, NULL) > 0) {
  4868. /**
  4869. * Remove the network by scheduling the group formation
  4870. * timeout to happen immediately. The teardown code
  4871. * needs to be scheduled to run asynch later so that we
  4872. * don't delete data from under ourselves unexpectedly.
  4873. * Calling wpas_p2p_group_formation_timeout directly
  4874. * causes a series of crashes in WPS failure scenarios.
  4875. */
  4876. wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
  4877. "P2P group network getting removed");
  4878. eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
  4879. wpa_s->parent, NULL);
  4880. }
  4881. }
  4882. struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
  4883. const u8 *addr, const u8 *ssid,
  4884. size_t ssid_len)
  4885. {
  4886. struct wpa_ssid *s;
  4887. size_t i;
  4888. for (s = wpa_s->conf->ssid; s; s = s->next) {
  4889. if (s->disabled != 2)
  4890. continue;
  4891. if (ssid &&
  4892. (ssid_len != s->ssid_len ||
  4893. os_memcmp(ssid, s->ssid, ssid_len) != 0))
  4894. continue;
  4895. if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
  4896. return s; /* peer is GO in the persistent group */
  4897. if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
  4898. continue;
  4899. for (i = 0; i < s->num_p2p_clients; i++) {
  4900. if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
  4901. addr, ETH_ALEN) == 0)
  4902. return s; /* peer is P2P client in persistent
  4903. * group */
  4904. }
  4905. }
  4906. return NULL;
  4907. }
  4908. void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
  4909. const u8 *addr)
  4910. {
  4911. if (addr == NULL)
  4912. return;
  4913. wpas_p2p_add_persistent_group_client(wpa_s, addr);
  4914. }
  4915. static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
  4916. int group_added)
  4917. {
  4918. struct wpa_supplicant *group = wpa_s;
  4919. if (wpa_s->global->p2p_group_formation)
  4920. group = wpa_s->global->p2p_group_formation;
  4921. wpa_s = wpa_s->parent;
  4922. offchannel_send_action_done(wpa_s);
  4923. if (group_added)
  4924. wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
  4925. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
  4926. wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
  4927. wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
  4928. 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
  4929. wpa_s->p2p_persistent_id,
  4930. wpa_s->p2p_pd_before_go_neg,
  4931. wpa_s->p2p_go_ht40);
  4932. }
  4933. int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
  4934. {
  4935. if (!wpa_s->p2p_fallback_to_go_neg ||
  4936. wpa_s->p2p_in_provisioning <= 5)
  4937. return 0;
  4938. if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
  4939. return 0; /* peer operating as a GO */
  4940. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
  4941. "fallback to GO Negotiation");
  4942. wpas_p2p_fallback_to_go_neg(wpa_s, 1);
  4943. return 1;
  4944. }
  4945. unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
  4946. {
  4947. const char *rn, *rn2;
  4948. struct wpa_supplicant *ifs;
  4949. if (wpa_s->wpa_state > WPA_SCANNING) {
  4950. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
  4951. "concurrent operation",
  4952. P2P_CONCURRENT_SEARCH_DELAY);
  4953. return P2P_CONCURRENT_SEARCH_DELAY;
  4954. }
  4955. if (!wpa_s->driver->get_radio_name)
  4956. return 0;
  4957. rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
  4958. if (rn == NULL || rn[0] == '\0')
  4959. return 0;
  4960. for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
  4961. if (ifs == wpa_s || !ifs->driver->get_radio_name)
  4962. continue;
  4963. rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
  4964. if (!rn2 || os_strcmp(rn, rn2) != 0)
  4965. continue;
  4966. if (ifs->wpa_state > WPA_SCANNING) {
  4967. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
  4968. "delay due to concurrent operation on "
  4969. "interface %s",
  4970. P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
  4971. return P2P_CONCURRENT_SEARCH_DELAY;
  4972. }
  4973. }
  4974. return 0;
  4975. }
  4976. void wpas_p2p_continue_after_scan(struct wpa_supplicant *wpa_s)
  4977. {
  4978. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Station mode scan operation not "
  4979. "pending anymore (sta_scan_pending=%d "
  4980. "p2p_cb_on_scan_complete=%d)", wpa_s->sta_scan_pending,
  4981. wpa_s->global->p2p_cb_on_scan_complete);
  4982. wpa_s->sta_scan_pending = 0;
  4983. if (!wpa_s->global->p2p_cb_on_scan_complete)
  4984. return;
  4985. wpa_s->global->p2p_cb_on_scan_complete = 0;
  4986. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4987. return;
  4988. if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
  4989. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
  4990. "continued after successful connection");
  4991. p2p_increase_search_delay(wpa_s->global->p2p,
  4992. wpas_p2p_search_delay(wpa_s));
  4993. }
  4994. }