p2p_supplicant.c 150 KB

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