p2p_supplicant.c 164 KB

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