p2p.c 128 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917
  1. /*
  2. * Wi-Fi Direct - P2P module
  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_defs.h"
  12. #include "common/ieee802_11_common.h"
  13. #include "common/wpa_ctrl.h"
  14. #include "wps/wps_i.h"
  15. #include "p2p_i.h"
  16. #include "p2p.h"
  17. static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx);
  18. static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev);
  19. static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
  20. const u8 *sa, const u8 *data, size_t len,
  21. int rx_freq);
  22. static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
  23. const u8 *sa, const u8 *data,
  24. size_t len);
  25. static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx);
  26. static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx);
  27. /*
  28. * p2p_scan recovery timeout
  29. *
  30. * Many drivers are using 30 second timeout on scan results. Allow a bit larger
  31. * timeout for this to avoid hitting P2P timeout unnecessarily.
  32. */
  33. #define P2P_SCAN_TIMEOUT 35
  34. /**
  35. * P2P_PEER_EXPIRATION_AGE - Number of seconds after which inactive peer
  36. * entries will be removed
  37. */
  38. #ifndef P2P_PEER_EXPIRATION_AGE
  39. #define P2P_PEER_EXPIRATION_AGE 60
  40. #endif /* P2P_PEER_EXPIRATION_AGE */
  41. #define P2P_PEER_EXPIRATION_INTERVAL (P2P_PEER_EXPIRATION_AGE / 2)
  42. static void p2p_expire_peers(struct p2p_data *p2p)
  43. {
  44. struct p2p_device *dev, *n;
  45. struct os_reltime now;
  46. size_t i;
  47. os_get_reltime(&now);
  48. dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) {
  49. if (dev->last_seen.sec + P2P_PEER_EXPIRATION_AGE >= now.sec)
  50. continue;
  51. if (dev == p2p->go_neg_peer) {
  52. /*
  53. * GO Negotiation is in progress with the peer, so
  54. * don't expire the peer entry until GO Negotiation
  55. * fails or times out.
  56. */
  57. continue;
  58. }
  59. if (p2p->cfg->go_connected &&
  60. p2p->cfg->go_connected(p2p->cfg->cb_ctx,
  61. dev->info.p2p_device_addr)) {
  62. /*
  63. * We are connected as a client to a group in which the
  64. * peer is the GO, so do not expire the peer entry.
  65. */
  66. os_get_reltime(&dev->last_seen);
  67. continue;
  68. }
  69. for (i = 0; i < p2p->num_groups; i++) {
  70. if (p2p_group_is_client_connected(
  71. p2p->groups[i], dev->info.p2p_device_addr))
  72. break;
  73. }
  74. if (i < p2p->num_groups) {
  75. /*
  76. * The peer is connected as a client in a group where
  77. * we are the GO, so do not expire the peer entry.
  78. */
  79. os_get_reltime(&dev->last_seen);
  80. continue;
  81. }
  82. p2p_dbg(p2p, "Expiring old peer entry " MACSTR,
  83. MAC2STR(dev->info.p2p_device_addr));
  84. dl_list_del(&dev->list);
  85. p2p_device_free(p2p, dev);
  86. }
  87. }
  88. static void p2p_expiration_timeout(void *eloop_ctx, void *timeout_ctx)
  89. {
  90. struct p2p_data *p2p = eloop_ctx;
  91. p2p_expire_peers(p2p);
  92. eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
  93. p2p_expiration_timeout, p2p, NULL);
  94. }
  95. static const char * p2p_state_txt(int state)
  96. {
  97. switch (state) {
  98. case P2P_IDLE:
  99. return "IDLE";
  100. case P2P_SEARCH:
  101. return "SEARCH";
  102. case P2P_CONNECT:
  103. return "CONNECT";
  104. case P2P_CONNECT_LISTEN:
  105. return "CONNECT_LISTEN";
  106. case P2P_GO_NEG:
  107. return "GO_NEG";
  108. case P2P_LISTEN_ONLY:
  109. return "LISTEN_ONLY";
  110. case P2P_WAIT_PEER_CONNECT:
  111. return "WAIT_PEER_CONNECT";
  112. case P2P_WAIT_PEER_IDLE:
  113. return "WAIT_PEER_IDLE";
  114. case P2P_SD_DURING_FIND:
  115. return "SD_DURING_FIND";
  116. case P2P_PROVISIONING:
  117. return "PROVISIONING";
  118. case P2P_PD_DURING_FIND:
  119. return "PD_DURING_FIND";
  120. case P2P_INVITE:
  121. return "INVITE";
  122. case P2P_INVITE_LISTEN:
  123. return "INVITE_LISTEN";
  124. default:
  125. return "?";
  126. }
  127. }
  128. const char * p2p_get_state_txt(struct p2p_data *p2p)
  129. {
  130. return p2p_state_txt(p2p->state);
  131. }
  132. u16 p2p_get_provisioning_info(struct p2p_data *p2p, const u8 *addr)
  133. {
  134. struct p2p_device *dev = NULL;
  135. if (!addr || !p2p)
  136. return 0;
  137. dev = p2p_get_device(p2p, addr);
  138. if (dev)
  139. return dev->wps_prov_info;
  140. else
  141. return 0;
  142. }
  143. void p2p_clear_provisioning_info(struct p2p_data *p2p, const u8 *addr)
  144. {
  145. struct p2p_device *dev = NULL;
  146. if (!addr || !p2p)
  147. return;
  148. dev = p2p_get_device(p2p, addr);
  149. if (dev)
  150. dev->wps_prov_info = 0;
  151. }
  152. void p2p_set_state(struct p2p_data *p2p, int new_state)
  153. {
  154. p2p_dbg(p2p, "State %s -> %s",
  155. p2p_state_txt(p2p->state), p2p_state_txt(new_state));
  156. p2p->state = new_state;
  157. if (new_state == P2P_IDLE && p2p->pending_channel) {
  158. p2p_dbg(p2p, "Apply change in listen channel");
  159. p2p->cfg->reg_class = p2p->pending_reg_class;
  160. p2p->cfg->channel = p2p->pending_channel;
  161. p2p->pending_reg_class = 0;
  162. p2p->pending_channel = 0;
  163. }
  164. }
  165. void p2p_set_timeout(struct p2p_data *p2p, unsigned int sec, unsigned int usec)
  166. {
  167. p2p_dbg(p2p, "Set timeout (state=%s): %u.%06u sec",
  168. p2p_state_txt(p2p->state), sec, usec);
  169. eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
  170. eloop_register_timeout(sec, usec, p2p_state_timeout, p2p, NULL);
  171. }
  172. void p2p_clear_timeout(struct p2p_data *p2p)
  173. {
  174. p2p_dbg(p2p, "Clear timeout (state=%s)", p2p_state_txt(p2p->state));
  175. eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
  176. }
  177. void p2p_go_neg_failed(struct p2p_data *p2p, int status)
  178. {
  179. struct p2p_go_neg_results res;
  180. struct p2p_device *peer = p2p->go_neg_peer;
  181. if (!peer)
  182. return;
  183. eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
  184. if (p2p->state != P2P_SEARCH) {
  185. /*
  186. * Clear timeouts related to GO Negotiation if no new p2p_find
  187. * has been started.
  188. */
  189. p2p_clear_timeout(p2p);
  190. p2p_set_state(p2p, P2P_IDLE);
  191. }
  192. peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
  193. peer->wps_method = WPS_NOT_READY;
  194. peer->oob_pw_id = 0;
  195. wpabuf_free(peer->go_neg_conf);
  196. peer->go_neg_conf = NULL;
  197. p2p->go_neg_peer = NULL;
  198. os_memset(&res, 0, sizeof(res));
  199. res.status = status;
  200. os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
  201. os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
  202. p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
  203. }
  204. static void p2p_listen_in_find(struct p2p_data *p2p, int dev_disc)
  205. {
  206. unsigned int r, tu;
  207. int freq;
  208. struct wpabuf *ies;
  209. p2p_dbg(p2p, "Starting short listen state (state=%s)",
  210. p2p_state_txt(p2p->state));
  211. if (p2p->pending_listen_freq) {
  212. /* We have a pending p2p_listen request */
  213. p2p_dbg(p2p, "p2p_listen command pending already");
  214. return;
  215. }
  216. freq = p2p_channel_to_freq(p2p->cfg->reg_class, p2p->cfg->channel);
  217. if (freq < 0) {
  218. p2p_dbg(p2p, "Unknown regulatory class/channel");
  219. return;
  220. }
  221. if (os_get_random((u8 *) &r, sizeof(r)) < 0)
  222. r = 0;
  223. tu = (r % ((p2p->max_disc_int - p2p->min_disc_int) + 1) +
  224. p2p->min_disc_int) * 100;
  225. if (p2p->max_disc_tu >= 0 && tu > (unsigned int) p2p->max_disc_tu)
  226. tu = p2p->max_disc_tu;
  227. if (!dev_disc && tu < 100)
  228. tu = 100; /* Need to wait in non-device discovery use cases */
  229. if (p2p->cfg->max_listen && 1024 * tu / 1000 > p2p->cfg->max_listen)
  230. tu = p2p->cfg->max_listen * 1000 / 1024;
  231. if (tu == 0) {
  232. p2p_dbg(p2p, "Skip listen state since duration was 0 TU");
  233. p2p_set_timeout(p2p, 0, 0);
  234. return;
  235. }
  236. ies = p2p_build_probe_resp_ies(p2p);
  237. if (ies == NULL)
  238. return;
  239. p2p->pending_listen_freq = freq;
  240. p2p->pending_listen_sec = 0;
  241. p2p->pending_listen_usec = 1024 * tu;
  242. if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, 1024 * tu / 1000,
  243. ies) < 0) {
  244. p2p_dbg(p2p, "Failed to start listen mode");
  245. p2p->pending_listen_freq = 0;
  246. }
  247. wpabuf_free(ies);
  248. }
  249. int p2p_listen(struct p2p_data *p2p, unsigned int timeout)
  250. {
  251. int freq;
  252. struct wpabuf *ies;
  253. p2p_dbg(p2p, "Going to listen(only) state");
  254. if (p2p->pending_listen_freq) {
  255. /* We have a pending p2p_listen request */
  256. p2p_dbg(p2p, "p2p_listen command pending already");
  257. return -1;
  258. }
  259. freq = p2p_channel_to_freq(p2p->cfg->reg_class, p2p->cfg->channel);
  260. if (freq < 0) {
  261. p2p_dbg(p2p, "Unknown regulatory class/channel");
  262. return -1;
  263. }
  264. p2p->pending_listen_sec = timeout / 1000;
  265. p2p->pending_listen_usec = (timeout % 1000) * 1000;
  266. if (p2p->p2p_scan_running) {
  267. if (p2p->start_after_scan == P2P_AFTER_SCAN_CONNECT) {
  268. p2p_dbg(p2p, "p2p_scan running - connect is already pending - skip listen");
  269. return 0;
  270. }
  271. p2p_dbg(p2p, "p2p_scan running - delay start of listen state");
  272. p2p->start_after_scan = P2P_AFTER_SCAN_LISTEN;
  273. return 0;
  274. }
  275. ies = p2p_build_probe_resp_ies(p2p);
  276. if (ies == NULL)
  277. return -1;
  278. p2p->pending_listen_freq = freq;
  279. if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, timeout, ies) < 0) {
  280. p2p_dbg(p2p, "Failed to start listen mode");
  281. p2p->pending_listen_freq = 0;
  282. wpabuf_free(ies);
  283. return -1;
  284. }
  285. wpabuf_free(ies);
  286. p2p_set_state(p2p, P2P_LISTEN_ONLY);
  287. return 0;
  288. }
  289. static void p2p_device_clear_reported(struct p2p_data *p2p)
  290. {
  291. struct p2p_device *dev;
  292. dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
  293. dev->flags &= ~P2P_DEV_REPORTED;
  294. dev->sd_reqs = 0;
  295. }
  296. }
  297. /**
  298. * p2p_get_device - Fetch a peer entry
  299. * @p2p: P2P module context from p2p_init()
  300. * @addr: P2P Device Address of the peer
  301. * Returns: Pointer to the device entry or %NULL if not found
  302. */
  303. struct p2p_device * p2p_get_device(struct p2p_data *p2p, const u8 *addr)
  304. {
  305. struct p2p_device *dev;
  306. dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
  307. if (os_memcmp(dev->info.p2p_device_addr, addr, ETH_ALEN) == 0)
  308. return dev;
  309. }
  310. return NULL;
  311. }
  312. /**
  313. * p2p_get_device_interface - Fetch a peer entry based on P2P Interface Address
  314. * @p2p: P2P module context from p2p_init()
  315. * @addr: P2P Interface Address of the peer
  316. * Returns: Pointer to the device entry or %NULL if not found
  317. */
  318. struct p2p_device * p2p_get_device_interface(struct p2p_data *p2p,
  319. const u8 *addr)
  320. {
  321. struct p2p_device *dev;
  322. dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
  323. if (os_memcmp(dev->interface_addr, addr, ETH_ALEN) == 0)
  324. return dev;
  325. }
  326. return NULL;
  327. }
  328. /**
  329. * p2p_create_device - Create a peer entry
  330. * @p2p: P2P module context from p2p_init()
  331. * @addr: P2P Device Address of the peer
  332. * Returns: Pointer to the device entry or %NULL on failure
  333. *
  334. * If there is already an entry for the peer, it will be returned instead of
  335. * creating a new one.
  336. */
  337. static struct p2p_device * p2p_create_device(struct p2p_data *p2p,
  338. const u8 *addr)
  339. {
  340. struct p2p_device *dev, *oldest = NULL;
  341. size_t count = 0;
  342. dev = p2p_get_device(p2p, addr);
  343. if (dev)
  344. return dev;
  345. dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
  346. count++;
  347. if (oldest == NULL ||
  348. os_reltime_before(&dev->last_seen, &oldest->last_seen))
  349. oldest = dev;
  350. }
  351. if (count + 1 > p2p->cfg->max_peers && oldest) {
  352. p2p_dbg(p2p, "Remove oldest peer entry to make room for a new peer");
  353. dl_list_del(&oldest->list);
  354. p2p_device_free(p2p, oldest);
  355. }
  356. dev = os_zalloc(sizeof(*dev));
  357. if (dev == NULL)
  358. return NULL;
  359. dl_list_add(&p2p->devices, &dev->list);
  360. os_memcpy(dev->info.p2p_device_addr, addr, ETH_ALEN);
  361. return dev;
  362. }
  363. static void p2p_copy_client_info(struct p2p_device *dev,
  364. struct p2p_client_info *cli)
  365. {
  366. os_memcpy(dev->info.device_name, cli->dev_name, cli->dev_name_len);
  367. dev->info.device_name[cli->dev_name_len] = '\0';
  368. dev->info.dev_capab = cli->dev_capab;
  369. dev->info.config_methods = cli->config_methods;
  370. os_memcpy(dev->info.pri_dev_type, cli->pri_dev_type, 8);
  371. dev->info.wps_sec_dev_type_list_len = 8 * cli->num_sec_dev_types;
  372. os_memcpy(dev->info.wps_sec_dev_type_list, cli->sec_dev_types,
  373. dev->info.wps_sec_dev_type_list_len);
  374. }
  375. static int p2p_add_group_clients(struct p2p_data *p2p, const u8 *go_dev_addr,
  376. const u8 *go_interface_addr, int freq,
  377. const u8 *gi, size_t gi_len)
  378. {
  379. struct p2p_group_info info;
  380. size_t c;
  381. struct p2p_device *dev;
  382. if (gi == NULL)
  383. return 0;
  384. if (p2p_group_info_parse(gi, gi_len, &info) < 0)
  385. return -1;
  386. /*
  387. * Clear old data for this group; if the devices are still in the
  388. * group, the information will be restored in the loop following this.
  389. */
  390. dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
  391. if (os_memcmp(dev->member_in_go_iface, go_interface_addr,
  392. ETH_ALEN) == 0) {
  393. os_memset(dev->member_in_go_iface, 0, ETH_ALEN);
  394. os_memset(dev->member_in_go_dev, 0, ETH_ALEN);
  395. }
  396. }
  397. for (c = 0; c < info.num_clients; c++) {
  398. struct p2p_client_info *cli = &info.client[c];
  399. if (os_memcmp(cli->p2p_device_addr, p2p->cfg->dev_addr,
  400. ETH_ALEN) == 0)
  401. continue; /* ignore our own entry */
  402. dev = p2p_get_device(p2p, cli->p2p_device_addr);
  403. if (dev) {
  404. if (dev->flags & (P2P_DEV_GROUP_CLIENT_ONLY |
  405. P2P_DEV_PROBE_REQ_ONLY)) {
  406. /*
  407. * Update information since we have not
  408. * received this directly from the client.
  409. */
  410. p2p_copy_client_info(dev, cli);
  411. } else {
  412. /*
  413. * Need to update P2P Client Discoverability
  414. * flag since it is valid only in P2P Group
  415. * Info attribute.
  416. */
  417. dev->info.dev_capab &=
  418. ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
  419. dev->info.dev_capab |=
  420. cli->dev_capab &
  421. P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
  422. }
  423. if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
  424. dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
  425. }
  426. } else {
  427. dev = p2p_create_device(p2p, cli->p2p_device_addr);
  428. if (dev == NULL)
  429. continue;
  430. dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
  431. p2p_copy_client_info(dev, cli);
  432. dev->oper_freq = freq;
  433. p2p->cfg->dev_found(p2p->cfg->cb_ctx,
  434. dev->info.p2p_device_addr,
  435. &dev->info, 1);
  436. dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
  437. }
  438. os_memcpy(dev->interface_addr, cli->p2p_interface_addr,
  439. ETH_ALEN);
  440. os_get_reltime(&dev->last_seen);
  441. os_memcpy(dev->member_in_go_dev, go_dev_addr, ETH_ALEN);
  442. os_memcpy(dev->member_in_go_iface, go_interface_addr,
  443. ETH_ALEN);
  444. }
  445. return 0;
  446. }
  447. static void p2p_copy_wps_info(struct p2p_data *p2p, struct p2p_device *dev,
  448. int probe_req, const struct p2p_message *msg)
  449. {
  450. os_memcpy(dev->info.device_name, msg->device_name,
  451. sizeof(dev->info.device_name));
  452. if (msg->manufacturer &&
  453. msg->manufacturer_len < sizeof(dev->info.manufacturer)) {
  454. os_memset(dev->info.manufacturer, 0,
  455. sizeof(dev->info.manufacturer));
  456. os_memcpy(dev->info.manufacturer, msg->manufacturer,
  457. msg->manufacturer_len);
  458. }
  459. if (msg->model_name &&
  460. msg->model_name_len < sizeof(dev->info.model_name)) {
  461. os_memset(dev->info.model_name, 0,
  462. sizeof(dev->info.model_name));
  463. os_memcpy(dev->info.model_name, msg->model_name,
  464. msg->model_name_len);
  465. }
  466. if (msg->model_number &&
  467. msg->model_number_len < sizeof(dev->info.model_number)) {
  468. os_memset(dev->info.model_number, 0,
  469. sizeof(dev->info.model_number));
  470. os_memcpy(dev->info.model_number, msg->model_number,
  471. msg->model_number_len);
  472. }
  473. if (msg->serial_number &&
  474. msg->serial_number_len < sizeof(dev->info.serial_number)) {
  475. os_memset(dev->info.serial_number, 0,
  476. sizeof(dev->info.serial_number));
  477. os_memcpy(dev->info.serial_number, msg->serial_number,
  478. msg->serial_number_len);
  479. }
  480. if (msg->pri_dev_type)
  481. os_memcpy(dev->info.pri_dev_type, msg->pri_dev_type,
  482. sizeof(dev->info.pri_dev_type));
  483. else if (msg->wps_pri_dev_type)
  484. os_memcpy(dev->info.pri_dev_type, msg->wps_pri_dev_type,
  485. sizeof(dev->info.pri_dev_type));
  486. if (msg->wps_sec_dev_type_list) {
  487. os_memcpy(dev->info.wps_sec_dev_type_list,
  488. msg->wps_sec_dev_type_list,
  489. msg->wps_sec_dev_type_list_len);
  490. dev->info.wps_sec_dev_type_list_len =
  491. msg->wps_sec_dev_type_list_len;
  492. }
  493. if (msg->capability) {
  494. /*
  495. * P2P Client Discoverability bit is reserved in all frames
  496. * that use this function, so do not change its value here.
  497. */
  498. dev->info.dev_capab &= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
  499. dev->info.dev_capab |= msg->capability[0] &
  500. ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
  501. dev->info.group_capab = msg->capability[1];
  502. }
  503. if (msg->ext_listen_timing) {
  504. dev->ext_listen_period = WPA_GET_LE16(msg->ext_listen_timing);
  505. dev->ext_listen_interval =
  506. WPA_GET_LE16(msg->ext_listen_timing + 2);
  507. }
  508. if (!probe_req) {
  509. u16 new_config_methods;
  510. new_config_methods = msg->config_methods ?
  511. msg->config_methods : msg->wps_config_methods;
  512. if (new_config_methods &&
  513. dev->info.config_methods != new_config_methods) {
  514. p2p_dbg(p2p, "Update peer " MACSTR
  515. " config_methods 0x%x -> 0x%x",
  516. MAC2STR(dev->info.p2p_device_addr),
  517. dev->info.config_methods,
  518. new_config_methods);
  519. dev->info.config_methods = new_config_methods;
  520. }
  521. }
  522. }
  523. static void p2p_update_peer_vendor_elems(struct p2p_device *dev, const u8 *ies,
  524. size_t ies_len)
  525. {
  526. const u8 *pos, *end;
  527. u8 id, len;
  528. wpabuf_free(dev->info.vendor_elems);
  529. dev->info.vendor_elems = NULL;
  530. end = ies + ies_len;
  531. for (pos = ies; pos + 1 < end; pos += len) {
  532. id = *pos++;
  533. len = *pos++;
  534. if (pos + len > end)
  535. break;
  536. if (id != WLAN_EID_VENDOR_SPECIFIC || len < 3)
  537. continue;
  538. if (len >= 4) {
  539. u32 type = WPA_GET_BE32(pos);
  540. if (type == WPA_IE_VENDOR_TYPE ||
  541. type == WMM_IE_VENDOR_TYPE ||
  542. type == WPS_IE_VENDOR_TYPE ||
  543. type == P2P_IE_VENDOR_TYPE ||
  544. type == WFD_IE_VENDOR_TYPE)
  545. continue;
  546. }
  547. /* Unknown vendor element - make raw IE data available */
  548. if (wpabuf_resize(&dev->info.vendor_elems, 2 + len) < 0)
  549. break;
  550. wpabuf_put_data(dev->info.vendor_elems, pos - 2, 2 + len);
  551. }
  552. }
  553. static int p2p_compare_wfd_info(struct p2p_device *dev,
  554. const struct p2p_message *msg)
  555. {
  556. if (dev->info.wfd_subelems && msg->wfd_subelems) {
  557. if (dev->info.wfd_subelems->used != msg->wfd_subelems->used)
  558. return 1;
  559. return os_memcmp(dev->info.wfd_subelems->buf,
  560. msg->wfd_subelems->buf,
  561. dev->info.wfd_subelems->used);
  562. }
  563. if (dev->info.wfd_subelems || msg->wfd_subelems)
  564. return 1;
  565. return 0;
  566. }
  567. /**
  568. * p2p_add_device - Add peer entries based on scan results or P2P frames
  569. * @p2p: P2P module context from p2p_init()
  570. * @addr: Source address of Beacon or Probe Response frame (may be either
  571. * P2P Device Address or P2P Interface Address)
  572. * @level: Signal level (signal strength of the received frame from the peer)
  573. * @freq: Frequency on which the Beacon or Probe Response frame was received
  574. * @rx_time: Time when the result was received
  575. * @ies: IEs from the Beacon or Probe Response frame
  576. * @ies_len: Length of ies buffer in octets
  577. * @scan_res: Whether this was based on scan results
  578. * Returns: 0 on success, -1 on failure
  579. *
  580. * If the scan result is for a GO, the clients in the group will also be added
  581. * to the peer table. This function can also be used with some other frames
  582. * like Provision Discovery Request that contains P2P Capability and P2P Device
  583. * Info attributes.
  584. */
  585. int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq,
  586. struct os_reltime *rx_time, int level, const u8 *ies,
  587. size_t ies_len, int scan_res)
  588. {
  589. struct p2p_device *dev;
  590. struct p2p_message msg;
  591. const u8 *p2p_dev_addr;
  592. int wfd_changed;
  593. int i;
  594. struct os_reltime time_now;
  595. os_memset(&msg, 0, sizeof(msg));
  596. if (p2p_parse_ies(ies, ies_len, &msg)) {
  597. p2p_dbg(p2p, "Failed to parse P2P IE for a device entry");
  598. p2p_parse_free(&msg);
  599. return -1;
  600. }
  601. if (msg.p2p_device_addr)
  602. p2p_dev_addr = msg.p2p_device_addr;
  603. else if (msg.device_id)
  604. p2p_dev_addr = msg.device_id;
  605. else {
  606. p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id");
  607. p2p_parse_free(&msg);
  608. return -1;
  609. }
  610. if (!is_zero_ether_addr(p2p->peer_filter) &&
  611. os_memcmp(p2p_dev_addr, p2p->peer_filter, ETH_ALEN) != 0) {
  612. p2p_dbg(p2p, "Do not add peer filter for " MACSTR
  613. " due to peer filter", MAC2STR(p2p_dev_addr));
  614. p2p_parse_free(&msg);
  615. return 0;
  616. }
  617. dev = p2p_create_device(p2p, p2p_dev_addr);
  618. if (dev == NULL) {
  619. p2p_parse_free(&msg);
  620. return -1;
  621. }
  622. if (rx_time == NULL) {
  623. os_get_reltime(&time_now);
  624. rx_time = &time_now;
  625. }
  626. /*
  627. * Update the device entry only if the new peer
  628. * entry is newer than the one previously stored.
  629. */
  630. if (dev->last_seen.sec > 0 &&
  631. os_reltime_before(rx_time, &dev->last_seen)) {
  632. p2p_dbg(p2p, "Do not update peer entry based on old frame (rx_time=%u.%06u last_seen=%u.%06u)",
  633. (unsigned int) rx_time->sec,
  634. (unsigned int) rx_time->usec,
  635. (unsigned int) dev->last_seen.sec,
  636. (unsigned int) dev->last_seen.usec);
  637. p2p_parse_free(&msg);
  638. return -1;
  639. }
  640. os_memcpy(&dev->last_seen, rx_time, sizeof(struct os_reltime));
  641. dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY);
  642. if (os_memcmp(addr, p2p_dev_addr, ETH_ALEN) != 0)
  643. os_memcpy(dev->interface_addr, addr, ETH_ALEN);
  644. if (msg.ssid &&
  645. (msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
  646. os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
  647. != 0)) {
  648. os_memcpy(dev->oper_ssid, msg.ssid + 2, msg.ssid[1]);
  649. dev->oper_ssid_len = msg.ssid[1];
  650. }
  651. if (freq >= 2412 && freq <= 2484 && msg.ds_params &&
  652. *msg.ds_params >= 1 && *msg.ds_params <= 14) {
  653. int ds_freq;
  654. if (*msg.ds_params == 14)
  655. ds_freq = 2484;
  656. else
  657. ds_freq = 2407 + *msg.ds_params * 5;
  658. if (freq != ds_freq) {
  659. p2p_dbg(p2p, "Update Listen frequency based on DS Parameter Set IE: %d -> %d MHz",
  660. freq, ds_freq);
  661. freq = ds_freq;
  662. }
  663. }
  664. if (dev->listen_freq && dev->listen_freq != freq && scan_res) {
  665. p2p_dbg(p2p, "Update Listen frequency based on scan results ("
  666. MACSTR " %d -> %d MHz (DS param %d)",
  667. MAC2STR(dev->info.p2p_device_addr), dev->listen_freq,
  668. freq, msg.ds_params ? *msg.ds_params : -1);
  669. }
  670. if (scan_res) {
  671. dev->listen_freq = freq;
  672. if (msg.group_info)
  673. dev->oper_freq = freq;
  674. }
  675. dev->info.level = level;
  676. p2p_copy_wps_info(p2p, dev, 0, &msg);
  677. for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
  678. wpabuf_free(dev->info.wps_vendor_ext[i]);
  679. dev->info.wps_vendor_ext[i] = NULL;
  680. }
  681. for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
  682. if (msg.wps_vendor_ext[i] == NULL)
  683. break;
  684. dev->info.wps_vendor_ext[i] = wpabuf_alloc_copy(
  685. msg.wps_vendor_ext[i], msg.wps_vendor_ext_len[i]);
  686. if (dev->info.wps_vendor_ext[i] == NULL)
  687. break;
  688. }
  689. wfd_changed = p2p_compare_wfd_info(dev, &msg);
  690. if (msg.wfd_subelems) {
  691. wpabuf_free(dev->info.wfd_subelems);
  692. dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
  693. }
  694. if (scan_res) {
  695. p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq,
  696. msg.group_info, msg.group_info_len);
  697. }
  698. p2p_parse_free(&msg);
  699. p2p_update_peer_vendor_elems(dev, ies, ies_len);
  700. if (dev->flags & P2P_DEV_REPORTED && !wfd_changed)
  701. return 0;
  702. p2p_dbg(p2p, "Peer found with Listen frequency %d MHz (rx_time=%u.%06u)",
  703. freq, (unsigned int) rx_time->sec,
  704. (unsigned int) rx_time->usec);
  705. if (dev->flags & P2P_DEV_USER_REJECTED) {
  706. p2p_dbg(p2p, "Do not report rejected device");
  707. return 0;
  708. }
  709. if (dev->info.config_methods == 0 &&
  710. (freq == 2412 || freq == 2437 || freq == 2462)) {
  711. /*
  712. * If we have only seen a Beacon frame from a GO, we do not yet
  713. * know what WPS config methods it supports. Since some
  714. * applications use config_methods value from P2P-DEVICE-FOUND
  715. * events, postpone reporting this peer until we've fully
  716. * discovered its capabilities.
  717. *
  718. * At least for now, do this only if the peer was detected on
  719. * one of the social channels since that peer can be easily be
  720. * found again and there are no limitations of having to use
  721. * passive scan on this channels, so this can be done through
  722. * Probe Response frame that includes the config_methods
  723. * information.
  724. */
  725. p2p_dbg(p2p, "Do not report peer " MACSTR
  726. " with unknown config methods", MAC2STR(addr));
  727. return 0;
  728. }
  729. p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
  730. !(dev->flags & P2P_DEV_REPORTED_ONCE));
  731. dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
  732. return 0;
  733. }
  734. static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev)
  735. {
  736. int i;
  737. if (p2p->go_neg_peer == dev) {
  738. /*
  739. * If GO Negotiation is in progress, report that it has failed.
  740. */
  741. p2p_go_neg_failed(p2p, -1);
  742. }
  743. if (p2p->invite_peer == dev)
  744. p2p->invite_peer = NULL;
  745. if (p2p->sd_peer == dev)
  746. p2p->sd_peer = NULL;
  747. if (p2p->pending_client_disc_go == dev)
  748. p2p->pending_client_disc_go = NULL;
  749. /* dev_lost() device, but only if it was previously dev_found() */
  750. if (dev->flags & P2P_DEV_REPORTED_ONCE)
  751. p2p->cfg->dev_lost(p2p->cfg->cb_ctx,
  752. dev->info.p2p_device_addr);
  753. for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
  754. wpabuf_free(dev->info.wps_vendor_ext[i]);
  755. dev->info.wps_vendor_ext[i] = NULL;
  756. }
  757. wpabuf_free(dev->info.wfd_subelems);
  758. wpabuf_free(dev->info.vendor_elems);
  759. wpabuf_free(dev->go_neg_conf);
  760. os_free(dev);
  761. }
  762. static int p2p_get_next_prog_freq(struct p2p_data *p2p)
  763. {
  764. struct p2p_channels *c;
  765. struct p2p_reg_class *cla;
  766. size_t cl, ch;
  767. int found = 0;
  768. u8 reg_class;
  769. u8 channel;
  770. int freq;
  771. c = &p2p->cfg->channels;
  772. for (cl = 0; cl < c->reg_classes; cl++) {
  773. cla = &c->reg_class[cl];
  774. if (cla->reg_class != p2p->last_prog_scan_class)
  775. continue;
  776. for (ch = 0; ch < cla->channels; ch++) {
  777. if (cla->channel[ch] == p2p->last_prog_scan_chan) {
  778. found = 1;
  779. break;
  780. }
  781. }
  782. if (found)
  783. break;
  784. }
  785. if (!found) {
  786. /* Start from beginning */
  787. reg_class = c->reg_class[0].reg_class;
  788. channel = c->reg_class[0].channel[0];
  789. } else {
  790. /* Pick the next channel */
  791. ch++;
  792. if (ch == cla->channels) {
  793. cl++;
  794. if (cl == c->reg_classes)
  795. cl = 0;
  796. ch = 0;
  797. }
  798. reg_class = c->reg_class[cl].reg_class;
  799. channel = c->reg_class[cl].channel[ch];
  800. }
  801. freq = p2p_channel_to_freq(reg_class, channel);
  802. p2p_dbg(p2p, "Next progressive search channel: reg_class %u channel %u -> %d MHz",
  803. reg_class, channel, freq);
  804. p2p->last_prog_scan_class = reg_class;
  805. p2p->last_prog_scan_chan = channel;
  806. if (freq == 2412 || freq == 2437 || freq == 2462)
  807. return 0; /* No need to add social channels */
  808. return freq;
  809. }
  810. static void p2p_search(struct p2p_data *p2p)
  811. {
  812. int freq = 0;
  813. enum p2p_scan_type type;
  814. u16 pw_id = DEV_PW_DEFAULT;
  815. int res;
  816. if (p2p->drv_in_listen) {
  817. p2p_dbg(p2p, "Driver is still in Listen state - wait for it to end before continuing");
  818. return;
  819. }
  820. p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
  821. if (p2p->find_type == P2P_FIND_PROGRESSIVE &&
  822. (freq = p2p_get_next_prog_freq(p2p)) > 0) {
  823. type = P2P_SCAN_SOCIAL_PLUS_ONE;
  824. p2p_dbg(p2p, "Starting search (+ freq %u)", freq);
  825. } else {
  826. type = P2P_SCAN_SOCIAL;
  827. p2p_dbg(p2p, "Starting search");
  828. }
  829. res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq,
  830. p2p->num_req_dev_types, p2p->req_dev_types,
  831. p2p->find_dev_id, pw_id);
  832. if (res < 0) {
  833. p2p_dbg(p2p, "Scan request schedule failed");
  834. p2p_continue_find(p2p);
  835. }
  836. }
  837. static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx)
  838. {
  839. struct p2p_data *p2p = eloop_ctx;
  840. p2p_dbg(p2p, "Find timeout -> stop");
  841. p2p_stop_find(p2p);
  842. }
  843. void p2p_notify_scan_trigger_status(struct p2p_data *p2p, int status)
  844. {
  845. if (status != 0) {
  846. p2p_dbg(p2p, "Scan request failed");
  847. /* Do continue find even for the first p2p_find_scan */
  848. p2p_continue_find(p2p);
  849. } else {
  850. p2p_dbg(p2p, "Running p2p_scan");
  851. p2p->p2p_scan_running = 1;
  852. eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
  853. eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
  854. p2p, NULL);
  855. }
  856. }
  857. static int p2p_run_after_scan(struct p2p_data *p2p)
  858. {
  859. struct p2p_device *dev;
  860. enum p2p_after_scan op;
  861. if (p2p->after_scan_tx) {
  862. p2p->after_scan_tx_in_progress = 1;
  863. p2p_dbg(p2p, "Send pending Action frame at p2p_scan completion");
  864. p2p->cfg->send_action(p2p->cfg->cb_ctx,
  865. p2p->after_scan_tx->freq,
  866. p2p->after_scan_tx->dst,
  867. p2p->after_scan_tx->src,
  868. p2p->after_scan_tx->bssid,
  869. (u8 *) (p2p->after_scan_tx + 1),
  870. p2p->after_scan_tx->len,
  871. p2p->after_scan_tx->wait_time);
  872. os_free(p2p->after_scan_tx);
  873. p2p->after_scan_tx = NULL;
  874. return 1;
  875. }
  876. op = p2p->start_after_scan;
  877. p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
  878. switch (op) {
  879. case P2P_AFTER_SCAN_NOTHING:
  880. break;
  881. case P2P_AFTER_SCAN_LISTEN:
  882. p2p_dbg(p2p, "Start previously requested Listen state");
  883. p2p_listen(p2p, p2p->pending_listen_sec * 1000 +
  884. p2p->pending_listen_usec / 1000);
  885. return 1;
  886. case P2P_AFTER_SCAN_CONNECT:
  887. p2p_dbg(p2p, "Start previously requested connect with " MACSTR,
  888. MAC2STR(p2p->after_scan_peer));
  889. dev = p2p_get_device(p2p, p2p->after_scan_peer);
  890. if (dev == NULL) {
  891. p2p_dbg(p2p, "Peer not known anymore");
  892. break;
  893. }
  894. p2p_connect_send(p2p, dev);
  895. return 1;
  896. }
  897. return 0;
  898. }
  899. static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx)
  900. {
  901. struct p2p_data *p2p = eloop_ctx;
  902. int running;
  903. p2p_dbg(p2p, "p2p_scan timeout (running=%d)", p2p->p2p_scan_running);
  904. running = p2p->p2p_scan_running;
  905. /* Make sure we recover from missed scan results callback */
  906. p2p->p2p_scan_running = 0;
  907. if (running)
  908. p2p_run_after_scan(p2p);
  909. }
  910. static void p2p_free_req_dev_types(struct p2p_data *p2p)
  911. {
  912. p2p->num_req_dev_types = 0;
  913. os_free(p2p->req_dev_types);
  914. p2p->req_dev_types = NULL;
  915. }
  916. int p2p_find(struct p2p_data *p2p, unsigned int timeout,
  917. enum p2p_discovery_type type,
  918. unsigned int num_req_dev_types, const u8 *req_dev_types,
  919. const u8 *dev_id, unsigned int search_delay)
  920. {
  921. int res;
  922. p2p_dbg(p2p, "Starting find (type=%d)", type);
  923. os_get_reltime(&p2p->find_start);
  924. if (p2p->p2p_scan_running) {
  925. p2p_dbg(p2p, "p2p_scan is already running");
  926. }
  927. p2p_free_req_dev_types(p2p);
  928. if (req_dev_types && num_req_dev_types) {
  929. p2p->req_dev_types = os_malloc(num_req_dev_types *
  930. WPS_DEV_TYPE_LEN);
  931. if (p2p->req_dev_types == NULL)
  932. return -1;
  933. os_memcpy(p2p->req_dev_types, req_dev_types,
  934. num_req_dev_types * WPS_DEV_TYPE_LEN);
  935. p2p->num_req_dev_types = num_req_dev_types;
  936. }
  937. if (dev_id) {
  938. os_memcpy(p2p->find_dev_id_buf, dev_id, ETH_ALEN);
  939. p2p->find_dev_id = p2p->find_dev_id_buf;
  940. } else
  941. p2p->find_dev_id = NULL;
  942. p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
  943. p2p_clear_timeout(p2p);
  944. p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
  945. p2p->find_type = type;
  946. p2p_device_clear_reported(p2p);
  947. p2p_set_state(p2p, P2P_SEARCH);
  948. p2p->search_delay = search_delay;
  949. p2p->in_search_delay = 0;
  950. eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
  951. p2p->last_p2p_find_timeout = timeout;
  952. if (timeout)
  953. eloop_register_timeout(timeout, 0, p2p_find_timeout,
  954. p2p, NULL);
  955. switch (type) {
  956. case P2P_FIND_START_WITH_FULL:
  957. case P2P_FIND_PROGRESSIVE:
  958. res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0,
  959. p2p->num_req_dev_types,
  960. p2p->req_dev_types, dev_id,
  961. DEV_PW_DEFAULT);
  962. break;
  963. case P2P_FIND_ONLY_SOCIAL:
  964. res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0,
  965. p2p->num_req_dev_types,
  966. p2p->req_dev_types, dev_id,
  967. DEV_PW_DEFAULT);
  968. break;
  969. default:
  970. return -1;
  971. }
  972. if (res != 0 && p2p->p2p_scan_running) {
  973. p2p_dbg(p2p, "Failed to start p2p_scan - another p2p_scan was already running");
  974. /* wait for the previous p2p_scan to complete */
  975. res = 0; /* do not report failure */
  976. } else if (res != 0) {
  977. p2p_dbg(p2p, "Failed to start p2p_scan");
  978. p2p_set_state(p2p, P2P_IDLE);
  979. eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
  980. }
  981. return res;
  982. }
  983. void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq)
  984. {
  985. p2p_dbg(p2p, "Stopping find");
  986. eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
  987. p2p_clear_timeout(p2p);
  988. if (p2p->state == P2P_SEARCH)
  989. p2p->cfg->find_stopped(p2p->cfg->cb_ctx);
  990. p2p_set_state(p2p, P2P_IDLE);
  991. p2p_free_req_dev_types(p2p);
  992. p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
  993. if (p2p->go_neg_peer)
  994. p2p->go_neg_peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
  995. p2p->go_neg_peer = NULL;
  996. p2p->sd_peer = NULL;
  997. p2p->invite_peer = NULL;
  998. p2p_stop_listen_for_freq(p2p, freq);
  999. }
  1000. void p2p_stop_listen_for_freq(struct p2p_data *p2p, int freq)
  1001. {
  1002. if (freq > 0 && p2p->drv_in_listen == freq && p2p->in_listen) {
  1003. p2p_dbg(p2p, "Skip stop_listen since we are on correct channel for response");
  1004. return;
  1005. }
  1006. if (p2p->in_listen) {
  1007. p2p->in_listen = 0;
  1008. p2p_clear_timeout(p2p);
  1009. }
  1010. if (p2p->drv_in_listen) {
  1011. /*
  1012. * The driver may not deliver callback to p2p_listen_end()
  1013. * when the operation gets canceled, so clear the internal
  1014. * variable that is tracking driver state.
  1015. */
  1016. p2p_dbg(p2p, "Clear drv_in_listen (%d)", p2p->drv_in_listen);
  1017. p2p->drv_in_listen = 0;
  1018. }
  1019. p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
  1020. }
  1021. void p2p_stop_listen(struct p2p_data *p2p)
  1022. {
  1023. if (p2p->state != P2P_LISTEN_ONLY) {
  1024. p2p_dbg(p2p, "Skip stop_listen since not in listen_only state.");
  1025. return;
  1026. }
  1027. p2p_stop_listen_for_freq(p2p, 0);
  1028. p2p_set_state(p2p, P2P_IDLE);
  1029. }
  1030. void p2p_stop_find(struct p2p_data *p2p)
  1031. {
  1032. p2p->pending_listen_freq = 0;
  1033. p2p_stop_find_for_freq(p2p, 0);
  1034. }
  1035. static int p2p_prepare_channel_pref(struct p2p_data *p2p,
  1036. unsigned int force_freq,
  1037. unsigned int pref_freq, int go)
  1038. {
  1039. u8 op_class, op_channel;
  1040. unsigned int freq = force_freq ? force_freq : pref_freq;
  1041. p2p_dbg(p2p, "Prepare channel pref - force_freq=%u pref_freq=%u go=%d",
  1042. force_freq, pref_freq, go);
  1043. if (p2p_freq_to_channel(freq, &op_class, &op_channel) < 0) {
  1044. p2p_dbg(p2p, "Unsupported frequency %u MHz", freq);
  1045. return -1;
  1046. }
  1047. if (!p2p_channels_includes(&p2p->cfg->channels, op_class, op_channel) &&
  1048. (go || !p2p_channels_includes(&p2p->cfg->cli_channels, op_class,
  1049. op_channel))) {
  1050. p2p_dbg(p2p, "Frequency %u MHz (oper_class %u channel %u) not allowed for P2P",
  1051. freq, op_class, op_channel);
  1052. return -1;
  1053. }
  1054. p2p->op_reg_class = op_class;
  1055. p2p->op_channel = op_channel;
  1056. if (force_freq) {
  1057. p2p->channels.reg_classes = 1;
  1058. p2p->channels.reg_class[0].channels = 1;
  1059. p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
  1060. p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
  1061. } else {
  1062. os_memcpy(&p2p->channels, &p2p->cfg->channels,
  1063. sizeof(struct p2p_channels));
  1064. }
  1065. return 0;
  1066. }
  1067. static void p2p_prepare_channel_best(struct p2p_data *p2p)
  1068. {
  1069. u8 op_class, op_channel;
  1070. const int op_classes_5ghz[] = { 124, 115, 0 };
  1071. const int op_classes_ht40[] = { 126, 127, 116, 117, 0 };
  1072. const int op_classes_vht[] = { 128, 0 };
  1073. p2p_dbg(p2p, "Prepare channel best");
  1074. if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 &&
  1075. p2p_supported_freq(p2p, p2p->best_freq_overall) &&
  1076. p2p_freq_to_channel(p2p->best_freq_overall, &op_class, &op_channel)
  1077. == 0) {
  1078. p2p_dbg(p2p, "Select best overall channel as operating channel preference");
  1079. p2p->op_reg_class = op_class;
  1080. p2p->op_channel = op_channel;
  1081. } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 &&
  1082. p2p_supported_freq(p2p, p2p->best_freq_5) &&
  1083. p2p_freq_to_channel(p2p->best_freq_5, &op_class, &op_channel)
  1084. == 0) {
  1085. p2p_dbg(p2p, "Select best 5 GHz channel as operating channel preference");
  1086. p2p->op_reg_class = op_class;
  1087. p2p->op_channel = op_channel;
  1088. } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_24 > 0 &&
  1089. p2p_supported_freq(p2p, p2p->best_freq_24) &&
  1090. p2p_freq_to_channel(p2p->best_freq_24, &op_class,
  1091. &op_channel) == 0) {
  1092. p2p_dbg(p2p, "Select best 2.4 GHz channel as operating channel preference");
  1093. p2p->op_reg_class = op_class;
  1094. p2p->op_channel = op_channel;
  1095. } else if (p2p->cfg->num_pref_chan > 0 &&
  1096. p2p_channels_includes(&p2p->cfg->channels,
  1097. p2p->cfg->pref_chan[0].op_class,
  1098. p2p->cfg->pref_chan[0].chan)) {
  1099. p2p_dbg(p2p, "Select first pref_chan entry as operating channel preference");
  1100. p2p->op_reg_class = p2p->cfg->pref_chan[0].op_class;
  1101. p2p->op_channel = p2p->cfg->pref_chan[0].chan;
  1102. } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_vht,
  1103. &p2p->op_reg_class, &p2p->op_channel) ==
  1104. 0) {
  1105. p2p_dbg(p2p, "Select possible VHT channel (op_class %u channel %u) as operating channel preference",
  1106. p2p->op_reg_class, p2p->op_channel);
  1107. } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_ht40,
  1108. &p2p->op_reg_class, &p2p->op_channel) ==
  1109. 0) {
  1110. p2p_dbg(p2p, "Select possible HT40 channel (op_class %u channel %u) as operating channel preference",
  1111. p2p->op_reg_class, p2p->op_channel);
  1112. } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_5ghz,
  1113. &p2p->op_reg_class, &p2p->op_channel) ==
  1114. 0) {
  1115. p2p_dbg(p2p, "Select possible 5 GHz channel (op_class %u channel %u) as operating channel preference",
  1116. p2p->op_reg_class, p2p->op_channel);
  1117. } else if (p2p_channels_includes(&p2p->cfg->channels,
  1118. p2p->cfg->op_reg_class,
  1119. p2p->cfg->op_channel)) {
  1120. p2p_dbg(p2p, "Select pre-configured channel as operating channel preference");
  1121. p2p->op_reg_class = p2p->cfg->op_reg_class;
  1122. p2p->op_channel = p2p->cfg->op_channel;
  1123. } else if (p2p_channel_random_social(&p2p->cfg->channels,
  1124. &p2p->op_reg_class,
  1125. &p2p->op_channel) == 0) {
  1126. p2p_dbg(p2p, "Select random available social channel (op_class %u channel %u) as operating channel preference",
  1127. p2p->op_reg_class, p2p->op_channel);
  1128. } else {
  1129. /* Select any random available channel from the first available
  1130. * operating class */
  1131. p2p_channel_select(&p2p->cfg->channels, NULL,
  1132. &p2p->op_reg_class,
  1133. &p2p->op_channel);
  1134. p2p_dbg(p2p, "Select random available channel %d from operating class %d as operating channel preference",
  1135. p2p->op_channel, p2p->op_reg_class);
  1136. }
  1137. os_memcpy(&p2p->channels, &p2p->cfg->channels,
  1138. sizeof(struct p2p_channels));
  1139. }
  1140. /**
  1141. * p2p_prepare_channel - Select operating channel for GO Negotiation
  1142. * @p2p: P2P module context from p2p_init()
  1143. * @dev: Selected peer device
  1144. * @force_freq: Forced frequency in MHz or 0 if not forced
  1145. * @pref_freq: Preferred frequency in MHz or 0 if no preference
  1146. * @go: Whether the local end will be forced to be GO
  1147. * Returns: 0 on success, -1 on failure (channel not supported for P2P)
  1148. *
  1149. * This function is used to do initial operating channel selection for GO
  1150. * Negotiation prior to having received peer information. The selected channel
  1151. * may be further optimized in p2p_reselect_channel() once the peer information
  1152. * is available.
  1153. */
  1154. int p2p_prepare_channel(struct p2p_data *p2p, struct p2p_device *dev,
  1155. unsigned int force_freq, unsigned int pref_freq, int go)
  1156. {
  1157. p2p_dbg(p2p, "Prepare channel - force_freq=%u pref_freq=%u go=%d",
  1158. force_freq, pref_freq, go);
  1159. if (force_freq || pref_freq) {
  1160. if (p2p_prepare_channel_pref(p2p, force_freq, pref_freq, go) <
  1161. 0)
  1162. return -1;
  1163. } else {
  1164. p2p_prepare_channel_best(p2p);
  1165. }
  1166. p2p_channels_dump(p2p, "prepared channels", &p2p->channels);
  1167. if (go)
  1168. p2p_channels_remove_freqs(&p2p->channels, &p2p->no_go_freq);
  1169. else if (!force_freq)
  1170. p2p_channels_union_inplace(&p2p->channels,
  1171. &p2p->cfg->cli_channels);
  1172. p2p_channels_dump(p2p, "after go/cli filter/add", &p2p->channels);
  1173. p2p_dbg(p2p, "Own preference for operation channel: Operating Class %u Channel %u%s",
  1174. p2p->op_reg_class, p2p->op_channel,
  1175. force_freq ? " (forced)" : "");
  1176. if (force_freq)
  1177. dev->flags |= P2P_DEV_FORCE_FREQ;
  1178. else
  1179. dev->flags &= ~P2P_DEV_FORCE_FREQ;
  1180. return 0;
  1181. }
  1182. static void p2p_set_dev_persistent(struct p2p_device *dev,
  1183. int persistent_group)
  1184. {
  1185. switch (persistent_group) {
  1186. case 0:
  1187. dev->flags &= ~(P2P_DEV_PREFER_PERSISTENT_GROUP |
  1188. P2P_DEV_PREFER_PERSISTENT_RECONN);
  1189. break;
  1190. case 1:
  1191. dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
  1192. dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_RECONN;
  1193. break;
  1194. case 2:
  1195. dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP |
  1196. P2P_DEV_PREFER_PERSISTENT_RECONN;
  1197. break;
  1198. }
  1199. }
  1200. int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
  1201. enum p2p_wps_method wps_method,
  1202. int go_intent, const u8 *own_interface_addr,
  1203. unsigned int force_freq, int persistent_group,
  1204. const u8 *force_ssid, size_t force_ssid_len,
  1205. int pd_before_go_neg, unsigned int pref_freq, u16 oob_pw_id)
  1206. {
  1207. struct p2p_device *dev;
  1208. p2p_dbg(p2p, "Request to start group negotiation - peer=" MACSTR
  1209. " GO Intent=%d Intended Interface Address=" MACSTR
  1210. " wps_method=%d persistent_group=%d pd_before_go_neg=%d "
  1211. "oob_pw_id=%u",
  1212. MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
  1213. wps_method, persistent_group, pd_before_go_neg, oob_pw_id);
  1214. dev = p2p_get_device(p2p, peer_addr);
  1215. if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
  1216. p2p_dbg(p2p, "Cannot connect to unknown P2P Device " MACSTR,
  1217. MAC2STR(peer_addr));
  1218. return -1;
  1219. }
  1220. if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq,
  1221. go_intent == 15) < 0)
  1222. return -1;
  1223. if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
  1224. if (!(dev->info.dev_capab &
  1225. P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
  1226. p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR
  1227. " that is in a group and is not discoverable",
  1228. MAC2STR(peer_addr));
  1229. return -1;
  1230. }
  1231. if (dev->oper_freq <= 0) {
  1232. p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR
  1233. " with incomplete information",
  1234. MAC2STR(peer_addr));
  1235. return -1;
  1236. }
  1237. /*
  1238. * First, try to connect directly. If the peer does not
  1239. * acknowledge frames, assume it is sleeping and use device
  1240. * discoverability via the GO at that point.
  1241. */
  1242. }
  1243. p2p->ssid_set = 0;
  1244. if (force_ssid) {
  1245. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
  1246. force_ssid, force_ssid_len);
  1247. os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
  1248. p2p->ssid_len = force_ssid_len;
  1249. p2p->ssid_set = 1;
  1250. }
  1251. dev->flags &= ~P2P_DEV_NOT_YET_READY;
  1252. dev->flags &= ~P2P_DEV_USER_REJECTED;
  1253. dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
  1254. dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
  1255. if (pd_before_go_neg)
  1256. dev->flags |= P2P_DEV_PD_BEFORE_GO_NEG;
  1257. else {
  1258. dev->flags &= ~P2P_DEV_PD_BEFORE_GO_NEG;
  1259. /*
  1260. * Assign dialog token and tie breaker here to use the same
  1261. * values in each retry within the same GO Negotiation exchange.
  1262. */
  1263. dev->dialog_token++;
  1264. if (dev->dialog_token == 0)
  1265. dev->dialog_token = 1;
  1266. dev->tie_breaker = p2p->next_tie_breaker;
  1267. p2p->next_tie_breaker = !p2p->next_tie_breaker;
  1268. }
  1269. dev->connect_reqs = 0;
  1270. dev->go_neg_req_sent = 0;
  1271. dev->go_state = UNKNOWN_GO;
  1272. p2p_set_dev_persistent(dev, persistent_group);
  1273. p2p->go_intent = go_intent;
  1274. os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
  1275. if (p2p->state != P2P_IDLE)
  1276. p2p_stop_find(p2p);
  1277. if (p2p->after_scan_tx) {
  1278. /*
  1279. * We need to drop the pending frame to avoid issues with the
  1280. * new GO Negotiation, e.g., when the pending frame was from a
  1281. * previous attempt at starting a GO Negotiation.
  1282. */
  1283. p2p_dbg(p2p, "Dropped previous pending Action frame TX that was waiting for p2p_scan completion");
  1284. os_free(p2p->after_scan_tx);
  1285. p2p->after_scan_tx = NULL;
  1286. }
  1287. dev->wps_method = wps_method;
  1288. dev->oob_pw_id = oob_pw_id;
  1289. dev->status = P2P_SC_SUCCESS;
  1290. if (p2p->p2p_scan_running) {
  1291. p2p_dbg(p2p, "p2p_scan running - delay connect send");
  1292. p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT;
  1293. os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN);
  1294. return 0;
  1295. }
  1296. p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
  1297. return p2p_connect_send(p2p, dev);
  1298. }
  1299. int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
  1300. enum p2p_wps_method wps_method,
  1301. int go_intent, const u8 *own_interface_addr,
  1302. unsigned int force_freq, int persistent_group,
  1303. const u8 *force_ssid, size_t force_ssid_len,
  1304. unsigned int pref_freq, u16 oob_pw_id)
  1305. {
  1306. struct p2p_device *dev;
  1307. p2p_dbg(p2p, "Request to authorize group negotiation - peer=" MACSTR
  1308. " GO Intent=%d Intended Interface Address=" MACSTR
  1309. " wps_method=%d persistent_group=%d oob_pw_id=%u",
  1310. MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
  1311. wps_method, persistent_group, oob_pw_id);
  1312. dev = p2p_get_device(p2p, peer_addr);
  1313. if (dev == NULL) {
  1314. p2p_dbg(p2p, "Cannot authorize unknown P2P Device " MACSTR,
  1315. MAC2STR(peer_addr));
  1316. return -1;
  1317. }
  1318. if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq, go_intent ==
  1319. 15) < 0)
  1320. return -1;
  1321. p2p->ssid_set = 0;
  1322. if (force_ssid) {
  1323. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
  1324. force_ssid, force_ssid_len);
  1325. os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
  1326. p2p->ssid_len = force_ssid_len;
  1327. p2p->ssid_set = 1;
  1328. }
  1329. dev->flags &= ~P2P_DEV_NOT_YET_READY;
  1330. dev->flags &= ~P2P_DEV_USER_REJECTED;
  1331. dev->go_neg_req_sent = 0;
  1332. dev->go_state = UNKNOWN_GO;
  1333. p2p_set_dev_persistent(dev, persistent_group);
  1334. p2p->go_intent = go_intent;
  1335. os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
  1336. dev->wps_method = wps_method;
  1337. dev->oob_pw_id = oob_pw_id;
  1338. dev->status = P2P_SC_SUCCESS;
  1339. return 0;
  1340. }
  1341. void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
  1342. struct p2p_device *dev, struct p2p_message *msg)
  1343. {
  1344. os_get_reltime(&dev->last_seen);
  1345. p2p_copy_wps_info(p2p, dev, 0, msg);
  1346. if (msg->listen_channel) {
  1347. int freq;
  1348. freq = p2p_channel_to_freq(msg->listen_channel[3],
  1349. msg->listen_channel[4]);
  1350. if (freq < 0) {
  1351. p2p_dbg(p2p, "Unknown peer Listen channel: "
  1352. "country=%c%c(0x%02x) reg_class=%u channel=%u",
  1353. msg->listen_channel[0],
  1354. msg->listen_channel[1],
  1355. msg->listen_channel[2],
  1356. msg->listen_channel[3],
  1357. msg->listen_channel[4]);
  1358. } else {
  1359. p2p_dbg(p2p, "Update peer " MACSTR
  1360. " Listen channel: %u -> %u MHz",
  1361. MAC2STR(dev->info.p2p_device_addr),
  1362. dev->listen_freq, freq);
  1363. dev->listen_freq = freq;
  1364. }
  1365. }
  1366. if (msg->wfd_subelems) {
  1367. wpabuf_free(dev->info.wfd_subelems);
  1368. dev->info.wfd_subelems = wpabuf_dup(msg->wfd_subelems);
  1369. }
  1370. if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
  1371. dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
  1372. p2p_dbg(p2p, "Completed device entry based on data from GO Negotiation Request");
  1373. } else {
  1374. p2p_dbg(p2p, "Created device entry based on GO Neg Req: "
  1375. MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' "
  1376. "listen_freq=%d",
  1377. MAC2STR(dev->info.p2p_device_addr),
  1378. dev->info.dev_capab, dev->info.group_capab,
  1379. dev->info.device_name, dev->listen_freq);
  1380. }
  1381. dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY;
  1382. if (dev->flags & P2P_DEV_USER_REJECTED) {
  1383. p2p_dbg(p2p, "Do not report rejected device");
  1384. return;
  1385. }
  1386. p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
  1387. !(dev->flags & P2P_DEV_REPORTED_ONCE));
  1388. dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
  1389. }
  1390. void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len)
  1391. {
  1392. os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
  1393. p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2);
  1394. os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2],
  1395. p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len);
  1396. *ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len;
  1397. }
  1398. int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params)
  1399. {
  1400. p2p_build_ssid(p2p, params->ssid, &params->ssid_len);
  1401. p2p_random(params->passphrase, p2p->cfg->passphrase_len);
  1402. return 0;
  1403. }
  1404. void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
  1405. {
  1406. struct p2p_go_neg_results res;
  1407. int go = peer->go_state == LOCAL_GO;
  1408. struct p2p_channels intersection;
  1409. p2p_dbg(p2p, "GO Negotiation with " MACSTR " completed (%s will be GO)",
  1410. MAC2STR(peer->info.p2p_device_addr), go ? "local end" : "peer");
  1411. os_memset(&res, 0, sizeof(res));
  1412. res.role_go = go;
  1413. os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
  1414. os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
  1415. res.wps_method = peer->wps_method;
  1416. if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
  1417. if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
  1418. res.persistent_group = 2;
  1419. else
  1420. res.persistent_group = 1;
  1421. }
  1422. if (go) {
  1423. /* Setup AP mode for WPS provisioning */
  1424. res.freq = p2p_channel_to_freq(p2p->op_reg_class,
  1425. p2p->op_channel);
  1426. os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
  1427. res.ssid_len = p2p->ssid_len;
  1428. p2p_random(res.passphrase, p2p->cfg->passphrase_len);
  1429. } else {
  1430. res.freq = peer->oper_freq;
  1431. if (p2p->ssid_len) {
  1432. os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
  1433. res.ssid_len = p2p->ssid_len;
  1434. }
  1435. }
  1436. p2p_channels_dump(p2p, "own channels", &p2p->channels);
  1437. p2p_channels_dump(p2p, "peer channels", &peer->channels);
  1438. p2p_channels_intersect(&p2p->channels, &peer->channels,
  1439. &intersection);
  1440. if (go) {
  1441. p2p_channels_remove_freqs(&intersection, &p2p->no_go_freq);
  1442. p2p_channels_dump(p2p, "intersection after no-GO removal",
  1443. &intersection);
  1444. }
  1445. p2p_channels_to_freqs(&intersection, res.freq_list,
  1446. P2P_MAX_CHANNELS);
  1447. res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout;
  1448. p2p_clear_timeout(p2p);
  1449. p2p->ssid_set = 0;
  1450. peer->go_neg_req_sent = 0;
  1451. peer->wps_method = WPS_NOT_READY;
  1452. peer->oob_pw_id = 0;
  1453. wpabuf_free(peer->go_neg_conf);
  1454. peer->go_neg_conf = NULL;
  1455. p2p_set_state(p2p, P2P_PROVISIONING);
  1456. p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
  1457. }
  1458. static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
  1459. const u8 *data, size_t len, int rx_freq)
  1460. {
  1461. p2p_dbg(p2p, "RX P2P Public Action from " MACSTR, MAC2STR(sa));
  1462. wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len);
  1463. if (len < 1)
  1464. return;
  1465. switch (data[0]) {
  1466. case P2P_GO_NEG_REQ:
  1467. p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq);
  1468. break;
  1469. case P2P_GO_NEG_RESP:
  1470. p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq);
  1471. break;
  1472. case P2P_GO_NEG_CONF:
  1473. p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1);
  1474. break;
  1475. case P2P_INVITATION_REQ:
  1476. p2p_process_invitation_req(p2p, sa, data + 1, len - 1,
  1477. rx_freq);
  1478. break;
  1479. case P2P_INVITATION_RESP:
  1480. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  1481. p2p_process_invitation_resp(p2p, sa, data + 1, len - 1);
  1482. break;
  1483. case P2P_PROV_DISC_REQ:
  1484. p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
  1485. break;
  1486. case P2P_PROV_DISC_RESP:
  1487. p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1);
  1488. break;
  1489. case P2P_DEV_DISC_REQ:
  1490. p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
  1491. break;
  1492. case P2P_DEV_DISC_RESP:
  1493. p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1);
  1494. break;
  1495. default:
  1496. p2p_dbg(p2p, "Unsupported P2P Public Action frame type %d",
  1497. data[0]);
  1498. break;
  1499. }
  1500. }
  1501. static void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da,
  1502. const u8 *sa, const u8 *bssid, const u8 *data,
  1503. size_t len, int freq)
  1504. {
  1505. if (len < 1)
  1506. return;
  1507. switch (data[0]) {
  1508. case WLAN_PA_VENDOR_SPECIFIC:
  1509. data++;
  1510. len--;
  1511. if (len < 4)
  1512. return;
  1513. if (WPA_GET_BE32(data) != P2P_IE_VENDOR_TYPE)
  1514. return;
  1515. data += 4;
  1516. len -= 4;
  1517. p2p_rx_p2p_action(p2p, sa, data, len, freq);
  1518. break;
  1519. case WLAN_PA_GAS_INITIAL_REQ:
  1520. p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq);
  1521. break;
  1522. case WLAN_PA_GAS_INITIAL_RESP:
  1523. p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq);
  1524. break;
  1525. case WLAN_PA_GAS_COMEBACK_REQ:
  1526. p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq);
  1527. break;
  1528. case WLAN_PA_GAS_COMEBACK_RESP:
  1529. p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq);
  1530. break;
  1531. }
  1532. }
  1533. void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
  1534. const u8 *bssid, u8 category,
  1535. const u8 *data, size_t len, int freq)
  1536. {
  1537. if (category == WLAN_ACTION_PUBLIC) {
  1538. p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq);
  1539. return;
  1540. }
  1541. if (category != WLAN_ACTION_VENDOR_SPECIFIC)
  1542. return;
  1543. if (len < 4)
  1544. return;
  1545. if (WPA_GET_BE32(data) != P2P_IE_VENDOR_TYPE)
  1546. return;
  1547. data += 4;
  1548. len -= 4;
  1549. /* P2P action frame */
  1550. p2p_dbg(p2p, "RX P2P Action from " MACSTR, MAC2STR(sa));
  1551. wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len);
  1552. if (len < 1)
  1553. return;
  1554. switch (data[0]) {
  1555. case P2P_NOA:
  1556. p2p_dbg(p2p, "Received P2P Action - Notice of Absence");
  1557. /* TODO */
  1558. break;
  1559. case P2P_PRESENCE_REQ:
  1560. p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq);
  1561. break;
  1562. case P2P_PRESENCE_RESP:
  1563. p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1);
  1564. break;
  1565. case P2P_GO_DISC_REQ:
  1566. p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq);
  1567. break;
  1568. default:
  1569. p2p_dbg(p2p, "Received P2P Action - unknown type %u", data[0]);
  1570. break;
  1571. }
  1572. }
  1573. static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx)
  1574. {
  1575. struct p2p_data *p2p = eloop_ctx;
  1576. if (p2p->go_neg_peer == NULL)
  1577. return;
  1578. if (p2p->pending_listen_freq) {
  1579. p2p_dbg(p2p, "Clear pending_listen_freq for p2p_go_neg_start");
  1580. p2p->pending_listen_freq = 0;
  1581. }
  1582. p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
  1583. p2p->go_neg_peer->status = P2P_SC_SUCCESS;
  1584. /*
  1585. * Set new timeout to make sure a previously set one does not expire
  1586. * too quickly while waiting for the GO Negotiation to complete.
  1587. */
  1588. p2p_set_timeout(p2p, 0, 500000);
  1589. p2p_connect_send(p2p, p2p->go_neg_peer);
  1590. }
  1591. static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx)
  1592. {
  1593. struct p2p_data *p2p = eloop_ctx;
  1594. if (p2p->invite_peer == NULL)
  1595. return;
  1596. if (p2p->pending_listen_freq) {
  1597. p2p_dbg(p2p, "Clear pending_listen_freq for p2p_invite_start");
  1598. p2p->pending_listen_freq = 0;
  1599. }
  1600. p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
  1601. p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr,
  1602. p2p->invite_dev_pw_id);
  1603. }
  1604. static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr,
  1605. const u8 *ie, size_t ie_len)
  1606. {
  1607. struct p2p_message msg;
  1608. struct p2p_device *dev;
  1609. os_memset(&msg, 0, sizeof(msg));
  1610. if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL)
  1611. {
  1612. p2p_parse_free(&msg);
  1613. return; /* not a P2P probe */
  1614. }
  1615. if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
  1616. os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
  1617. != 0) {
  1618. /* The Probe Request is not part of P2P Device Discovery. It is
  1619. * not known whether the source address of the frame is the P2P
  1620. * Device Address or P2P Interface Address. Do not add a new
  1621. * peer entry based on this frames.
  1622. */
  1623. p2p_parse_free(&msg);
  1624. return;
  1625. }
  1626. dev = p2p_get_device(p2p, addr);
  1627. if (dev) {
  1628. if (dev->country[0] == 0 && msg.listen_channel)
  1629. os_memcpy(dev->country, msg.listen_channel, 3);
  1630. os_get_reltime(&dev->last_seen);
  1631. p2p_parse_free(&msg);
  1632. return; /* already known */
  1633. }
  1634. dev = p2p_create_device(p2p, addr);
  1635. if (dev == NULL) {
  1636. p2p_parse_free(&msg);
  1637. return;
  1638. }
  1639. os_get_reltime(&dev->last_seen);
  1640. dev->flags |= P2P_DEV_PROBE_REQ_ONLY;
  1641. if (msg.listen_channel) {
  1642. os_memcpy(dev->country, msg.listen_channel, 3);
  1643. dev->listen_freq = p2p_channel_to_freq(msg.listen_channel[3],
  1644. msg.listen_channel[4]);
  1645. }
  1646. p2p_copy_wps_info(p2p, dev, 1, &msg);
  1647. if (msg.wfd_subelems) {
  1648. wpabuf_free(dev->info.wfd_subelems);
  1649. dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
  1650. }
  1651. p2p_parse_free(&msg);
  1652. p2p_dbg(p2p, "Created device entry based on Probe Req: " MACSTR
  1653. " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
  1654. MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab,
  1655. dev->info.group_capab, dev->info.device_name,
  1656. dev->listen_freq);
  1657. }
  1658. struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p,
  1659. const u8 *addr,
  1660. struct p2p_message *msg)
  1661. {
  1662. struct p2p_device *dev;
  1663. dev = p2p_get_device(p2p, addr);
  1664. if (dev) {
  1665. os_get_reltime(&dev->last_seen);
  1666. return dev; /* already known */
  1667. }
  1668. dev = p2p_create_device(p2p, addr);
  1669. if (dev == NULL)
  1670. return NULL;
  1671. p2p_add_dev_info(p2p, addr, dev, msg);
  1672. return dev;
  1673. }
  1674. static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type)
  1675. {
  1676. if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0)
  1677. return 1;
  1678. if (os_memcmp(dev_type, req_dev_type, 2) == 0 &&
  1679. WPA_GET_BE32(&req_dev_type[2]) == 0 &&
  1680. WPA_GET_BE16(&req_dev_type[6]) == 0)
  1681. return 1; /* Category match with wildcard OUI/sub-category */
  1682. return 0;
  1683. }
  1684. int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[],
  1685. size_t num_req_dev_type)
  1686. {
  1687. size_t i;
  1688. for (i = 0; i < num_req_dev_type; i++) {
  1689. if (dev_type_match(dev_type, req_dev_type[i]))
  1690. return 1;
  1691. }
  1692. return 0;
  1693. }
  1694. /**
  1695. * p2p_match_dev_type - Match local device type with requested type
  1696. * @p2p: P2P module context from p2p_init()
  1697. * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
  1698. * Returns: 1 on match, 0 on mismatch
  1699. *
  1700. * This function can be used to match the Requested Device Type attribute in
  1701. * WPS IE with the local device types for deciding whether to reply to a Probe
  1702. * Request frame.
  1703. */
  1704. int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps)
  1705. {
  1706. struct wps_parse_attr attr;
  1707. size_t i;
  1708. if (wps_parse_msg(wps, &attr))
  1709. return 1; /* assume no Requested Device Type attributes */
  1710. if (attr.num_req_dev_type == 0)
  1711. return 1; /* no Requested Device Type attributes -> match */
  1712. if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type,
  1713. attr.num_req_dev_type))
  1714. return 1; /* Own Primary Device Type matches */
  1715. for (i = 0; i < p2p->cfg->num_sec_dev_types; i++) {
  1716. if (dev_type_list_match(p2p->cfg->sec_dev_type[i],
  1717. attr.req_dev_type,
  1718. attr.num_req_dev_type))
  1719. return 1; /* Own Secondary Device Type matches */
  1720. }
  1721. /* No matching device type found */
  1722. return 0;
  1723. }
  1724. struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p)
  1725. {
  1726. struct wpabuf *buf;
  1727. u8 *len;
  1728. int pw_id = -1;
  1729. size_t extra = 0;
  1730. #ifdef CONFIG_WIFI_DISPLAY
  1731. if (p2p->wfd_ie_probe_resp)
  1732. extra = wpabuf_len(p2p->wfd_ie_probe_resp);
  1733. #endif /* CONFIG_WIFI_DISPLAY */
  1734. if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P])
  1735. extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]);
  1736. buf = wpabuf_alloc(1000 + extra);
  1737. if (buf == NULL)
  1738. return NULL;
  1739. if (p2p->go_neg_peer) {
  1740. /* Advertise immediate availability of WPS credential */
  1741. pw_id = p2p_wps_method_pw_id(p2p->go_neg_peer->wps_method);
  1742. }
  1743. if (p2p_build_wps_ie(p2p, buf, pw_id, 1) < 0) {
  1744. p2p_dbg(p2p, "Failed to build WPS IE for Probe Response");
  1745. wpabuf_free(buf);
  1746. return NULL;
  1747. }
  1748. #ifdef CONFIG_WIFI_DISPLAY
  1749. if (p2p->wfd_ie_probe_resp)
  1750. wpabuf_put_buf(buf, p2p->wfd_ie_probe_resp);
  1751. #endif /* CONFIG_WIFI_DISPLAY */
  1752. if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P])
  1753. wpabuf_put_buf(buf,
  1754. p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]);
  1755. /* P2P IE */
  1756. len = p2p_buf_add_ie_hdr(buf);
  1757. p2p_buf_add_capability(buf, p2p->dev_capab &
  1758. ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
  1759. if (p2p->ext_listen_interval)
  1760. p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
  1761. p2p->ext_listen_interval);
  1762. p2p_buf_add_device_info(buf, p2p, NULL);
  1763. p2p_buf_update_ie_hdr(buf, len);
  1764. return buf;
  1765. }
  1766. static enum p2p_probe_req_status
  1767. p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
  1768. const u8 *bssid, const u8 *ie, size_t ie_len)
  1769. {
  1770. struct ieee802_11_elems elems;
  1771. struct wpabuf *buf;
  1772. struct ieee80211_mgmt *resp;
  1773. struct p2p_message msg;
  1774. struct wpabuf *ies;
  1775. if (!p2p->in_listen || !p2p->drv_in_listen) {
  1776. /* not in Listen state - ignore Probe Request */
  1777. p2p_dbg(p2p, "Not in Listen state (in_listen=%d drv_in_listen=%d) - ignore Probe Request",
  1778. p2p->in_listen, p2p->drv_in_listen);
  1779. return P2P_PREQ_NOT_LISTEN;
  1780. }
  1781. if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) ==
  1782. ParseFailed) {
  1783. /* Ignore invalid Probe Request frames */
  1784. p2p_dbg(p2p, "Could not parse Probe Request frame - ignore it");
  1785. return P2P_PREQ_MALFORMED;
  1786. }
  1787. if (elems.p2p == NULL) {
  1788. /* not a P2P probe - ignore it */
  1789. p2p_dbg(p2p, "Not a P2P probe - ignore it");
  1790. return P2P_PREQ_NOT_P2P;
  1791. }
  1792. if (dst && !is_broadcast_ether_addr(dst) &&
  1793. os_memcmp(dst, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
  1794. /* Not sent to the broadcast address or our P2P Device Address
  1795. */
  1796. p2p_dbg(p2p, "Probe Req DA " MACSTR " not ours - ignore it",
  1797. MAC2STR(dst));
  1798. return P2P_PREQ_NOT_PROCESSED;
  1799. }
  1800. if (bssid && !is_broadcast_ether_addr(bssid)) {
  1801. /* Not sent to the Wildcard BSSID */
  1802. p2p_dbg(p2p, "Probe Req BSSID " MACSTR " not wildcard - ignore it",
  1803. MAC2STR(bssid));
  1804. return P2P_PREQ_NOT_PROCESSED;
  1805. }
  1806. if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN ||
  1807. os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) !=
  1808. 0) {
  1809. /* not using P2P Wildcard SSID - ignore */
  1810. p2p_dbg(p2p, "Probe Req not using P2P Wildcard SSID - ignore it");
  1811. return P2P_PREQ_NOT_PROCESSED;
  1812. }
  1813. if (supp_rates_11b_only(&elems)) {
  1814. /* Indicates support for 11b rates only */
  1815. p2p_dbg(p2p, "Probe Req with 11b rates only supported - ignore it");
  1816. return P2P_PREQ_NOT_P2P;
  1817. }
  1818. os_memset(&msg, 0, sizeof(msg));
  1819. if (p2p_parse_ies(ie, ie_len, &msg) < 0) {
  1820. /* Could not parse P2P attributes */
  1821. p2p_dbg(p2p, "Could not parse P2P attributes in Probe Req - ignore it");
  1822. return P2P_PREQ_NOT_P2P;
  1823. }
  1824. if (msg.device_id &&
  1825. os_memcmp(msg.device_id, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
  1826. /* Device ID did not match */
  1827. p2p_dbg(p2p, "Probe Req requested Device ID " MACSTR " did not match - ignore it",
  1828. MAC2STR(msg.device_id));
  1829. p2p_parse_free(&msg);
  1830. return P2P_PREQ_NOT_PROCESSED;
  1831. }
  1832. /* Check Requested Device Type match */
  1833. if (msg.wps_attributes &&
  1834. !p2p_match_dev_type(p2p, msg.wps_attributes)) {
  1835. /* No match with Requested Device Type */
  1836. p2p_dbg(p2p, "Probe Req requestred Device Type did not match - ignore it");
  1837. p2p_parse_free(&msg);
  1838. return P2P_PREQ_NOT_PROCESSED;
  1839. }
  1840. p2p_parse_free(&msg);
  1841. if (!p2p->cfg->send_probe_resp) {
  1842. /* Response generated elsewhere */
  1843. p2p_dbg(p2p, "Probe Resp generated elsewhere - do not generate additional response");
  1844. return P2P_PREQ_NOT_PROCESSED;
  1845. }
  1846. p2p_dbg(p2p, "Reply to P2P Probe Request in Listen state");
  1847. /*
  1848. * We do not really have a specific BSS that this frame is advertising,
  1849. * so build a frame that has some information in valid format. This is
  1850. * really only used for discovery purposes, not to learn exact BSS
  1851. * parameters.
  1852. */
  1853. ies = p2p_build_probe_resp_ies(p2p);
  1854. if (ies == NULL)
  1855. return P2P_PREQ_NOT_PROCESSED;
  1856. buf = wpabuf_alloc(200 + wpabuf_len(ies));
  1857. if (buf == NULL) {
  1858. wpabuf_free(ies);
  1859. return P2P_PREQ_NOT_PROCESSED;
  1860. }
  1861. resp = NULL;
  1862. resp = wpabuf_put(buf, resp->u.probe_resp.variable - (u8 *) resp);
  1863. resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
  1864. (WLAN_FC_STYPE_PROBE_RESP << 4));
  1865. os_memcpy(resp->da, addr, ETH_ALEN);
  1866. os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN);
  1867. os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN);
  1868. resp->u.probe_resp.beacon_int = host_to_le16(100);
  1869. /* hardware or low-level driver will setup seq_ctrl and timestamp */
  1870. resp->u.probe_resp.capab_info =
  1871. host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE |
  1872. WLAN_CAPABILITY_PRIVACY |
  1873. WLAN_CAPABILITY_SHORT_SLOT_TIME);
  1874. wpabuf_put_u8(buf, WLAN_EID_SSID);
  1875. wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN);
  1876. wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
  1877. wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES);
  1878. wpabuf_put_u8(buf, 8);
  1879. wpabuf_put_u8(buf, (60 / 5) | 0x80);
  1880. wpabuf_put_u8(buf, 90 / 5);
  1881. wpabuf_put_u8(buf, (120 / 5) | 0x80);
  1882. wpabuf_put_u8(buf, 180 / 5);
  1883. wpabuf_put_u8(buf, (240 / 5) | 0x80);
  1884. wpabuf_put_u8(buf, 360 / 5);
  1885. wpabuf_put_u8(buf, 480 / 5);
  1886. wpabuf_put_u8(buf, 540 / 5);
  1887. wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS);
  1888. wpabuf_put_u8(buf, 1);
  1889. wpabuf_put_u8(buf, p2p->cfg->channel);
  1890. wpabuf_put_buf(buf, ies);
  1891. wpabuf_free(ies);
  1892. p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf);
  1893. wpabuf_free(buf);
  1894. return P2P_PREQ_NOT_PROCESSED;
  1895. }
  1896. enum p2p_probe_req_status
  1897. p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
  1898. const u8 *bssid, const u8 *ie, size_t ie_len)
  1899. {
  1900. enum p2p_probe_req_status res;
  1901. p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len);
  1902. res = p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len);
  1903. if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) &&
  1904. p2p->go_neg_peer &&
  1905. os_memcmp(addr, p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN)
  1906. == 0 &&
  1907. !(p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
  1908. /* Received a Probe Request from GO Negotiation peer */
  1909. p2p_dbg(p2p, "Found GO Negotiation peer - try to start GO negotiation from timeout");
  1910. eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
  1911. eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL);
  1912. return P2P_PREQ_PROCESSED;
  1913. }
  1914. if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) &&
  1915. p2p->invite_peer &&
  1916. (p2p->invite_peer->flags & P2P_DEV_WAIT_INV_REQ_ACK) &&
  1917. os_memcmp(addr, p2p->invite_peer->info.p2p_device_addr, ETH_ALEN)
  1918. == 0) {
  1919. /* Received a Probe Request from Invite peer */
  1920. p2p_dbg(p2p, "Found Invite peer - try to start Invite from timeout");
  1921. eloop_cancel_timeout(p2p_invite_start, p2p, NULL);
  1922. eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL);
  1923. return P2P_PREQ_PROCESSED;
  1924. }
  1925. return res;
  1926. }
  1927. static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid,
  1928. u8 *buf, size_t len, struct wpabuf *p2p_ie)
  1929. {
  1930. struct wpabuf *tmp;
  1931. u8 *lpos;
  1932. size_t tmplen;
  1933. int res;
  1934. u8 group_capab;
  1935. if (p2p_ie == NULL)
  1936. return 0; /* WLAN AP is not a P2P manager */
  1937. /*
  1938. * (Re)Association Request - P2P IE
  1939. * P2P Capability attribute (shall be present)
  1940. * P2P Interface attribute (present if concurrent device and
  1941. * P2P Management is enabled)
  1942. */
  1943. tmp = wpabuf_alloc(200);
  1944. if (tmp == NULL)
  1945. return -1;
  1946. lpos = p2p_buf_add_ie_hdr(tmp);
  1947. group_capab = 0;
  1948. if (p2p->num_groups > 0) {
  1949. group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
  1950. if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
  1951. (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) &&
  1952. p2p->cross_connect)
  1953. group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
  1954. }
  1955. p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab);
  1956. if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
  1957. (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED))
  1958. p2p_buf_add_p2p_interface(tmp, p2p);
  1959. p2p_buf_update_ie_hdr(tmp, lpos);
  1960. tmplen = wpabuf_len(tmp);
  1961. if (tmplen > len)
  1962. res = -1;
  1963. else {
  1964. os_memcpy(buf, wpabuf_head(tmp), tmplen);
  1965. res = tmplen;
  1966. }
  1967. wpabuf_free(tmp);
  1968. return res;
  1969. }
  1970. int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf,
  1971. size_t len, int p2p_group, struct wpabuf *p2p_ie)
  1972. {
  1973. struct wpabuf *tmp;
  1974. u8 *lpos;
  1975. struct p2p_device *peer;
  1976. size_t tmplen;
  1977. int res;
  1978. size_t extra = 0;
  1979. if (!p2p_group)
  1980. return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie);
  1981. #ifdef CONFIG_WIFI_DISPLAY
  1982. if (p2p->wfd_ie_assoc_req)
  1983. extra = wpabuf_len(p2p->wfd_ie_assoc_req);
  1984. #endif /* CONFIG_WIFI_DISPLAY */
  1985. if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ])
  1986. extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]);
  1987. /*
  1988. * (Re)Association Request - P2P IE
  1989. * P2P Capability attribute (shall be present)
  1990. * Extended Listen Timing (may be present)
  1991. * P2P Device Info attribute (shall be present)
  1992. */
  1993. tmp = wpabuf_alloc(200 + extra);
  1994. if (tmp == NULL)
  1995. return -1;
  1996. #ifdef CONFIG_WIFI_DISPLAY
  1997. if (p2p->wfd_ie_assoc_req)
  1998. wpabuf_put_buf(tmp, p2p->wfd_ie_assoc_req);
  1999. #endif /* CONFIG_WIFI_DISPLAY */
  2000. if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ])
  2001. wpabuf_put_buf(tmp,
  2002. p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]);
  2003. peer = bssid ? p2p_get_device(p2p, bssid) : NULL;
  2004. lpos = p2p_buf_add_ie_hdr(tmp);
  2005. p2p_buf_add_capability(tmp, p2p->dev_capab, 0);
  2006. if (p2p->ext_listen_interval)
  2007. p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period,
  2008. p2p->ext_listen_interval);
  2009. p2p_buf_add_device_info(tmp, p2p, peer);
  2010. p2p_buf_update_ie_hdr(tmp, lpos);
  2011. tmplen = wpabuf_len(tmp);
  2012. if (tmplen > len)
  2013. res = -1;
  2014. else {
  2015. os_memcpy(buf, wpabuf_head(tmp), tmplen);
  2016. res = tmplen;
  2017. }
  2018. wpabuf_free(tmp);
  2019. return res;
  2020. }
  2021. int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end)
  2022. {
  2023. struct wpabuf *p2p_ie;
  2024. int ret;
  2025. p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE);
  2026. if (p2p_ie == NULL)
  2027. return 0;
  2028. ret = p2p_attr_text(p2p_ie, buf, end);
  2029. wpabuf_free(p2p_ie);
  2030. return ret;
  2031. }
  2032. int p2p_parse_dev_addr_in_p2p_ie(struct wpabuf *p2p_ie, u8 *dev_addr)
  2033. {
  2034. struct p2p_message msg;
  2035. os_memset(&msg, 0, sizeof(msg));
  2036. if (p2p_parse_p2p_ie(p2p_ie, &msg))
  2037. return -1;
  2038. if (msg.p2p_device_addr) {
  2039. os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN);
  2040. return 0;
  2041. } else if (msg.device_id) {
  2042. os_memcpy(dev_addr, msg.device_id, ETH_ALEN);
  2043. return 0;
  2044. }
  2045. return -1;
  2046. }
  2047. int p2p_parse_dev_addr(const u8 *ies, size_t ies_len, u8 *dev_addr)
  2048. {
  2049. struct wpabuf *p2p_ie;
  2050. int ret;
  2051. p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
  2052. P2P_IE_VENDOR_TYPE);
  2053. if (p2p_ie == NULL)
  2054. return -1;
  2055. ret = p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr);
  2056. wpabuf_free(p2p_ie);
  2057. return ret;
  2058. }
  2059. static void p2p_clear_go_neg(struct p2p_data *p2p)
  2060. {
  2061. p2p->go_neg_peer = NULL;
  2062. p2p_clear_timeout(p2p);
  2063. p2p_set_state(p2p, P2P_IDLE);
  2064. }
  2065. void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
  2066. {
  2067. if (p2p->go_neg_peer == NULL) {
  2068. p2p_dbg(p2p, "No pending Group Formation - ignore WPS registration success notification");
  2069. return; /* No pending Group Formation */
  2070. }
  2071. if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) !=
  2072. 0) {
  2073. p2p_dbg(p2p, "Ignore WPS registration success notification for "
  2074. MACSTR " (GO Negotiation peer " MACSTR ")",
  2075. MAC2STR(mac_addr),
  2076. MAC2STR(p2p->go_neg_peer->intended_addr));
  2077. return; /* Ignore unexpected peer address */
  2078. }
  2079. p2p_dbg(p2p, "Group Formation completed successfully with " MACSTR,
  2080. MAC2STR(mac_addr));
  2081. p2p_clear_go_neg(p2p);
  2082. }
  2083. void p2p_group_formation_failed(struct p2p_data *p2p)
  2084. {
  2085. if (p2p->go_neg_peer == NULL) {
  2086. p2p_dbg(p2p, "No pending Group Formation - ignore group formation failure notification");
  2087. return; /* No pending Group Formation */
  2088. }
  2089. p2p_dbg(p2p, "Group Formation failed with " MACSTR,
  2090. MAC2STR(p2p->go_neg_peer->intended_addr));
  2091. p2p_clear_go_neg(p2p);
  2092. }
  2093. struct p2p_data * p2p_init(const struct p2p_config *cfg)
  2094. {
  2095. struct p2p_data *p2p;
  2096. if (cfg->max_peers < 1 ||
  2097. cfg->passphrase_len < 8 || cfg->passphrase_len > 63)
  2098. return NULL;
  2099. p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg));
  2100. if (p2p == NULL)
  2101. return NULL;
  2102. p2p->cfg = (struct p2p_config *) (p2p + 1);
  2103. os_memcpy(p2p->cfg, cfg, sizeof(*cfg));
  2104. if (cfg->dev_name)
  2105. p2p->cfg->dev_name = os_strdup(cfg->dev_name);
  2106. if (cfg->manufacturer)
  2107. p2p->cfg->manufacturer = os_strdup(cfg->manufacturer);
  2108. if (cfg->model_name)
  2109. p2p->cfg->model_name = os_strdup(cfg->model_name);
  2110. if (cfg->model_number)
  2111. p2p->cfg->model_number = os_strdup(cfg->model_number);
  2112. if (cfg->serial_number)
  2113. p2p->cfg->serial_number = os_strdup(cfg->serial_number);
  2114. if (cfg->pref_chan) {
  2115. p2p->cfg->pref_chan = os_malloc(cfg->num_pref_chan *
  2116. sizeof(struct p2p_channel));
  2117. if (p2p->cfg->pref_chan) {
  2118. os_memcpy(p2p->cfg->pref_chan, cfg->pref_chan,
  2119. cfg->num_pref_chan *
  2120. sizeof(struct p2p_channel));
  2121. } else
  2122. p2p->cfg->num_pref_chan = 0;
  2123. }
  2124. p2p->min_disc_int = 1;
  2125. p2p->max_disc_int = 3;
  2126. p2p->max_disc_tu = -1;
  2127. if (os_get_random(&p2p->next_tie_breaker, 1) < 0)
  2128. p2p->next_tie_breaker = 0;
  2129. p2p->next_tie_breaker &= 0x01;
  2130. if (cfg->sd_request)
  2131. p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
  2132. p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
  2133. if (cfg->concurrent_operations)
  2134. p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER;
  2135. p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
  2136. dl_list_init(&p2p->devices);
  2137. eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
  2138. p2p_expiration_timeout, p2p, NULL);
  2139. p2p->go_timeout = 100;
  2140. p2p->client_timeout = 20;
  2141. p2p->num_p2p_sd_queries = 0;
  2142. p2p_dbg(p2p, "initialized");
  2143. p2p_channels_dump(p2p, "channels", &p2p->cfg->channels);
  2144. p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels);
  2145. return p2p;
  2146. }
  2147. void p2p_deinit(struct p2p_data *p2p)
  2148. {
  2149. #ifdef CONFIG_WIFI_DISPLAY
  2150. wpabuf_free(p2p->wfd_ie_beacon);
  2151. wpabuf_free(p2p->wfd_ie_probe_req);
  2152. wpabuf_free(p2p->wfd_ie_probe_resp);
  2153. wpabuf_free(p2p->wfd_ie_assoc_req);
  2154. wpabuf_free(p2p->wfd_ie_invitation);
  2155. wpabuf_free(p2p->wfd_ie_prov_disc_req);
  2156. wpabuf_free(p2p->wfd_ie_prov_disc_resp);
  2157. wpabuf_free(p2p->wfd_ie_go_neg);
  2158. wpabuf_free(p2p->wfd_dev_info);
  2159. wpabuf_free(p2p->wfd_assoc_bssid);
  2160. wpabuf_free(p2p->wfd_coupled_sink_info);
  2161. #endif /* CONFIG_WIFI_DISPLAY */
  2162. eloop_cancel_timeout(p2p_expiration_timeout, p2p, NULL);
  2163. eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
  2164. eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
  2165. eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
  2166. eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
  2167. p2p_flush(p2p);
  2168. p2p_free_req_dev_types(p2p);
  2169. os_free(p2p->cfg->dev_name);
  2170. os_free(p2p->cfg->manufacturer);
  2171. os_free(p2p->cfg->model_name);
  2172. os_free(p2p->cfg->model_number);
  2173. os_free(p2p->cfg->serial_number);
  2174. os_free(p2p->cfg->pref_chan);
  2175. os_free(p2p->groups);
  2176. wpabuf_free(p2p->sd_resp);
  2177. os_free(p2p->after_scan_tx);
  2178. p2p_remove_wps_vendor_extensions(p2p);
  2179. os_free(p2p->no_go_freq.range);
  2180. os_free(p2p);
  2181. }
  2182. void p2p_flush(struct p2p_data *p2p)
  2183. {
  2184. struct p2p_device *dev, *prev;
  2185. p2p_stop_find(p2p);
  2186. dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device,
  2187. list) {
  2188. dl_list_del(&dev->list);
  2189. p2p_device_free(p2p, dev);
  2190. }
  2191. p2p_free_sd_queries(p2p);
  2192. os_free(p2p->after_scan_tx);
  2193. p2p->after_scan_tx = NULL;
  2194. }
  2195. int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr)
  2196. {
  2197. struct p2p_device *dev;
  2198. dev = p2p_get_device(p2p, addr);
  2199. if (dev == NULL)
  2200. return -1;
  2201. p2p_dbg(p2p, "Unauthorizing " MACSTR, MAC2STR(addr));
  2202. if (p2p->go_neg_peer == dev) {
  2203. eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
  2204. p2p->go_neg_peer = NULL;
  2205. }
  2206. dev->wps_method = WPS_NOT_READY;
  2207. dev->oob_pw_id = 0;
  2208. dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
  2209. dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
  2210. /* Check if after_scan_tx is for this peer. If so free it */
  2211. if (p2p->after_scan_tx &&
  2212. os_memcmp(addr, p2p->after_scan_tx->dst, ETH_ALEN) == 0) {
  2213. os_free(p2p->after_scan_tx);
  2214. p2p->after_scan_tx = NULL;
  2215. }
  2216. return 0;
  2217. }
  2218. int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name)
  2219. {
  2220. os_free(p2p->cfg->dev_name);
  2221. if (dev_name) {
  2222. p2p->cfg->dev_name = os_strdup(dev_name);
  2223. if (p2p->cfg->dev_name == NULL)
  2224. return -1;
  2225. } else
  2226. p2p->cfg->dev_name = NULL;
  2227. return 0;
  2228. }
  2229. int p2p_set_manufacturer(struct p2p_data *p2p, const char *manufacturer)
  2230. {
  2231. os_free(p2p->cfg->manufacturer);
  2232. p2p->cfg->manufacturer = NULL;
  2233. if (manufacturer) {
  2234. p2p->cfg->manufacturer = os_strdup(manufacturer);
  2235. if (p2p->cfg->manufacturer == NULL)
  2236. return -1;
  2237. }
  2238. return 0;
  2239. }
  2240. int p2p_set_model_name(struct p2p_data *p2p, const char *model_name)
  2241. {
  2242. os_free(p2p->cfg->model_name);
  2243. p2p->cfg->model_name = NULL;
  2244. if (model_name) {
  2245. p2p->cfg->model_name = os_strdup(model_name);
  2246. if (p2p->cfg->model_name == NULL)
  2247. return -1;
  2248. }
  2249. return 0;
  2250. }
  2251. int p2p_set_model_number(struct p2p_data *p2p, const char *model_number)
  2252. {
  2253. os_free(p2p->cfg->model_number);
  2254. p2p->cfg->model_number = NULL;
  2255. if (model_number) {
  2256. p2p->cfg->model_number = os_strdup(model_number);
  2257. if (p2p->cfg->model_number == NULL)
  2258. return -1;
  2259. }
  2260. return 0;
  2261. }
  2262. int p2p_set_serial_number(struct p2p_data *p2p, const char *serial_number)
  2263. {
  2264. os_free(p2p->cfg->serial_number);
  2265. p2p->cfg->serial_number = NULL;
  2266. if (serial_number) {
  2267. p2p->cfg->serial_number = os_strdup(serial_number);
  2268. if (p2p->cfg->serial_number == NULL)
  2269. return -1;
  2270. }
  2271. return 0;
  2272. }
  2273. void p2p_set_config_methods(struct p2p_data *p2p, u16 config_methods)
  2274. {
  2275. p2p->cfg->config_methods = config_methods;
  2276. }
  2277. void p2p_set_uuid(struct p2p_data *p2p, const u8 *uuid)
  2278. {
  2279. os_memcpy(p2p->cfg->uuid, uuid, 16);
  2280. }
  2281. int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type)
  2282. {
  2283. os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8);
  2284. return 0;
  2285. }
  2286. int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8],
  2287. size_t num_dev_types)
  2288. {
  2289. if (num_dev_types > P2P_SEC_DEVICE_TYPES)
  2290. num_dev_types = P2P_SEC_DEVICE_TYPES;
  2291. p2p->cfg->num_sec_dev_types = num_dev_types;
  2292. os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8);
  2293. return 0;
  2294. }
  2295. void p2p_remove_wps_vendor_extensions(struct p2p_data *p2p)
  2296. {
  2297. int i;
  2298. for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
  2299. wpabuf_free(p2p->wps_vendor_ext[i]);
  2300. p2p->wps_vendor_ext[i] = NULL;
  2301. }
  2302. }
  2303. int p2p_add_wps_vendor_extension(struct p2p_data *p2p,
  2304. const struct wpabuf *vendor_ext)
  2305. {
  2306. int i;
  2307. if (vendor_ext == NULL)
  2308. return -1;
  2309. for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
  2310. if (p2p->wps_vendor_ext[i] == NULL)
  2311. break;
  2312. }
  2313. if (i >= P2P_MAX_WPS_VENDOR_EXT)
  2314. return -1;
  2315. p2p->wps_vendor_ext[i] = wpabuf_dup(vendor_ext);
  2316. if (p2p->wps_vendor_ext[i] == NULL)
  2317. return -1;
  2318. return 0;
  2319. }
  2320. int p2p_set_country(struct p2p_data *p2p, const char *country)
  2321. {
  2322. os_memcpy(p2p->cfg->country, country, 3);
  2323. return 0;
  2324. }
  2325. static int p2p_pre_find_operation(struct p2p_data *p2p, struct p2p_device *dev)
  2326. {
  2327. if (dev->sd_pending_bcast_queries == 0) {
  2328. /* Initialize with total number of registered broadcast
  2329. * SD queries. */
  2330. dev->sd_pending_bcast_queries = p2p->num_p2p_sd_queries;
  2331. }
  2332. if (p2p_start_sd(p2p, dev) == 0)
  2333. return 1;
  2334. if (dev->req_config_methods &&
  2335. !(dev->flags & P2P_DEV_PD_FOR_JOIN)) {
  2336. p2p_dbg(p2p, "Send pending Provision Discovery Request to "
  2337. MACSTR " (config methods 0x%x)",
  2338. MAC2STR(dev->info.p2p_device_addr),
  2339. dev->req_config_methods);
  2340. if (p2p_send_prov_disc_req(p2p, dev, 0, 0) == 0)
  2341. return 1;
  2342. }
  2343. return 0;
  2344. }
  2345. void p2p_continue_find(struct p2p_data *p2p)
  2346. {
  2347. struct p2p_device *dev;
  2348. int found;
  2349. p2p_set_state(p2p, P2P_SEARCH);
  2350. /* Continue from the device following the last iteration */
  2351. found = 0;
  2352. dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
  2353. if (dev == p2p->last_p2p_find_oper) {
  2354. found = 1;
  2355. continue;
  2356. }
  2357. if (!found)
  2358. continue;
  2359. if (p2p_pre_find_operation(p2p, dev) > 0) {
  2360. p2p->last_p2p_find_oper = dev;
  2361. return;
  2362. }
  2363. }
  2364. /*
  2365. * Wrap around to the beginning of the list and continue until the last
  2366. * iteration device.
  2367. */
  2368. dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
  2369. if (p2p_pre_find_operation(p2p, dev) > 0) {
  2370. p2p->last_p2p_find_oper = dev;
  2371. return;
  2372. }
  2373. if (dev == p2p->last_p2p_find_oper)
  2374. break;
  2375. }
  2376. p2p_listen_in_find(p2p, 1);
  2377. }
  2378. static void p2p_sd_cb(struct p2p_data *p2p, int success)
  2379. {
  2380. p2p_dbg(p2p, "Service Discovery Query TX callback: success=%d",
  2381. success);
  2382. p2p->pending_action_state = P2P_NO_PENDING_ACTION;
  2383. if (!success) {
  2384. if (p2p->sd_peer)
  2385. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  2386. p2p->sd_peer = NULL;
  2387. p2p_continue_find(p2p);
  2388. return;
  2389. }
  2390. if (p2p->sd_peer == NULL) {
  2391. p2p_dbg(p2p, "No SD peer entry known");
  2392. p2p_continue_find(p2p);
  2393. return;
  2394. }
  2395. if (p2p->sd_query->for_all_peers) {
  2396. /* Update the pending broadcast SD query count for this device
  2397. */
  2398. p2p->sd_peer->sd_pending_bcast_queries--;
  2399. /*
  2400. * If there are no pending broadcast queries for this device,
  2401. * mark it as done (-1).
  2402. */
  2403. if (p2p->sd_peer->sd_pending_bcast_queries == 0)
  2404. p2p->sd_peer->sd_pending_bcast_queries = -1;
  2405. }
  2406. /* Wait for response from the peer */
  2407. p2p_set_state(p2p, P2P_SD_DURING_FIND);
  2408. p2p_set_timeout(p2p, 0, 200000);
  2409. }
  2410. /**
  2411. * p2p_retry_pd - Retry any pending provision disc requests in IDLE state
  2412. * @p2p: P2P module context from p2p_init()
  2413. */
  2414. static void p2p_retry_pd(struct p2p_data *p2p)
  2415. {
  2416. struct p2p_device *dev;
  2417. if (p2p->state != P2P_IDLE)
  2418. return;
  2419. /*
  2420. * Retry the prov disc req attempt only for the peer that the user had
  2421. * requested.
  2422. */
  2423. dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
  2424. if (os_memcmp(p2p->pending_pd_devaddr,
  2425. dev->info.p2p_device_addr, ETH_ALEN) != 0)
  2426. continue;
  2427. if (!dev->req_config_methods)
  2428. continue;
  2429. p2p_dbg(p2p, "Send pending Provision Discovery Request to "
  2430. MACSTR " (config methods 0x%x)",
  2431. MAC2STR(dev->info.p2p_device_addr),
  2432. dev->req_config_methods);
  2433. p2p_send_prov_disc_req(p2p, dev,
  2434. dev->flags & P2P_DEV_PD_FOR_JOIN,
  2435. p2p->pd_force_freq);
  2436. return;
  2437. }
  2438. }
  2439. static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
  2440. {
  2441. p2p_dbg(p2p, "Provision Discovery Request TX callback: success=%d",
  2442. success);
  2443. /*
  2444. * Postpone resetting the pending action state till after we actually
  2445. * time out. This allows us to take some action like notifying any
  2446. * interested parties about no response to the request.
  2447. *
  2448. * When the timer (below) goes off we check in IDLE, SEARCH, or
  2449. * LISTEN_ONLY state, which are the only allowed states to issue a PD
  2450. * requests in, if this was still pending and then raise notification.
  2451. */
  2452. if (!success) {
  2453. p2p->pending_action_state = P2P_NO_PENDING_ACTION;
  2454. if (p2p->user_initiated_pd &&
  2455. (p2p->state == P2P_SEARCH || p2p->state == P2P_LISTEN_ONLY))
  2456. {
  2457. /* Retry request from timeout to avoid busy loops */
  2458. p2p->pending_action_state = P2P_PENDING_PD;
  2459. p2p_set_timeout(p2p, 0, 50000);
  2460. } else if (p2p->state != P2P_IDLE)
  2461. p2p_continue_find(p2p);
  2462. else if (p2p->user_initiated_pd) {
  2463. p2p->pending_action_state = P2P_PENDING_PD;
  2464. p2p_set_timeout(p2p, 0, 300000);
  2465. }
  2466. return;
  2467. }
  2468. /*
  2469. * This postponing, of resetting pending_action_state, needs to be
  2470. * done only for user initiated PD requests and not internal ones.
  2471. */
  2472. if (p2p->user_initiated_pd)
  2473. p2p->pending_action_state = P2P_PENDING_PD;
  2474. else
  2475. p2p->pending_action_state = P2P_NO_PENDING_ACTION;
  2476. /* Wait for response from the peer */
  2477. if (p2p->state == P2P_SEARCH)
  2478. p2p_set_state(p2p, P2P_PD_DURING_FIND);
  2479. p2p_set_timeout(p2p, 0, 200000);
  2480. }
  2481. int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
  2482. struct os_reltime *rx_time, int level, const u8 *ies,
  2483. size_t ies_len)
  2484. {
  2485. if (os_reltime_before(rx_time, &p2p->find_start)) {
  2486. /*
  2487. * The driver may have cached (e.g., in cfg80211 BSS table) the
  2488. * scan results for relatively long time. To avoid reporting
  2489. * stale information, update P2P peers only based on results
  2490. * that have based on frames received after the last p2p_find
  2491. * operation was started.
  2492. */
  2493. p2p_dbg(p2p, "Ignore old scan result for " MACSTR
  2494. " (rx_time=%u.%06u)",
  2495. MAC2STR(bssid), (unsigned int) rx_time->sec,
  2496. (unsigned int) rx_time->usec);
  2497. return 0;
  2498. }
  2499. p2p_add_device(p2p, bssid, freq, rx_time, level, ies, ies_len, 1);
  2500. return 0;
  2501. }
  2502. void p2p_scan_res_handled(struct p2p_data *p2p)
  2503. {
  2504. if (!p2p->p2p_scan_running) {
  2505. p2p_dbg(p2p, "p2p_scan was not running, but scan results received");
  2506. }
  2507. p2p->p2p_scan_running = 0;
  2508. eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
  2509. if (p2p_run_after_scan(p2p))
  2510. return;
  2511. if (p2p->state == P2P_SEARCH)
  2512. p2p_continue_find(p2p);
  2513. }
  2514. void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies, const u8 *dev_id)
  2515. {
  2516. u8 *len;
  2517. #ifdef CONFIG_WIFI_DISPLAY
  2518. if (p2p->wfd_ie_probe_req)
  2519. wpabuf_put_buf(ies, p2p->wfd_ie_probe_req);
  2520. #endif /* CONFIG_WIFI_DISPLAY */
  2521. if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P])
  2522. wpabuf_put_buf(ies,
  2523. p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]);
  2524. len = p2p_buf_add_ie_hdr(ies);
  2525. p2p_buf_add_capability(ies, p2p->dev_capab &
  2526. ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
  2527. if (dev_id)
  2528. p2p_buf_add_device_id(ies, dev_id);
  2529. if (p2p->cfg->reg_class && p2p->cfg->channel)
  2530. p2p_buf_add_listen_channel(ies, p2p->cfg->country,
  2531. p2p->cfg->reg_class,
  2532. p2p->cfg->channel);
  2533. if (p2p->ext_listen_interval)
  2534. p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period,
  2535. p2p->ext_listen_interval);
  2536. /* TODO: p2p_buf_add_operating_channel() if GO */
  2537. p2p_buf_update_ie_hdr(ies, len);
  2538. }
  2539. size_t p2p_scan_ie_buf_len(struct p2p_data *p2p)
  2540. {
  2541. size_t len = 100;
  2542. #ifdef CONFIG_WIFI_DISPLAY
  2543. if (p2p && p2p->wfd_ie_probe_req)
  2544. len += wpabuf_len(p2p->wfd_ie_probe_req);
  2545. #endif /* CONFIG_WIFI_DISPLAY */
  2546. if (p2p && p2p->vendor_elem &&
  2547. p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P])
  2548. len += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]);
  2549. return len;
  2550. }
  2551. int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end)
  2552. {
  2553. return p2p_attr_text(p2p_ie, buf, end);
  2554. }
  2555. static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success)
  2556. {
  2557. struct p2p_device *dev = p2p->go_neg_peer;
  2558. int timeout;
  2559. p2p_dbg(p2p, "GO Negotiation Request TX callback: success=%d", success);
  2560. if (dev == NULL) {
  2561. p2p_dbg(p2p, "No pending GO Negotiation");
  2562. return;
  2563. }
  2564. if (success) {
  2565. if (dev->flags & P2P_DEV_USER_REJECTED) {
  2566. p2p_set_state(p2p, P2P_IDLE);
  2567. return;
  2568. }
  2569. } else if (dev->go_neg_req_sent) {
  2570. /* Cancel the increment from p2p_connect_send() on failure */
  2571. dev->go_neg_req_sent--;
  2572. }
  2573. if (!success &&
  2574. (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) &&
  2575. !is_zero_ether_addr(dev->member_in_go_dev)) {
  2576. p2p_dbg(p2p, "Peer " MACSTR " did not acknowledge request - try to use device discoverability through its GO",
  2577. MAC2STR(dev->info.p2p_device_addr));
  2578. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  2579. p2p_send_dev_disc_req(p2p, dev);
  2580. return;
  2581. }
  2582. /*
  2583. * Use P2P find, if needed, to find the other device from its listen
  2584. * channel.
  2585. */
  2586. p2p_set_state(p2p, P2P_CONNECT);
  2587. timeout = success ? 500000 : 100000;
  2588. if (!success && p2p->go_neg_peer &&
  2589. (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE)) {
  2590. unsigned int r;
  2591. /*
  2592. * Peer is expected to wait our response and we will skip the
  2593. * listen phase. Add some randomness to the wait time here to
  2594. * make it less likely to hit cases where we could end up in
  2595. * sync with peer not listening.
  2596. */
  2597. if (os_get_random((u8 *) &r, sizeof(r)) < 0)
  2598. r = 0;
  2599. timeout += r % 100000;
  2600. }
  2601. p2p_set_timeout(p2p, 0, timeout);
  2602. }
  2603. static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success)
  2604. {
  2605. p2p_dbg(p2p, "GO Negotiation Response TX callback: success=%d",
  2606. success);
  2607. if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) {
  2608. p2p_dbg(p2p, "Ignore TX callback event - GO Negotiation is not running anymore");
  2609. return;
  2610. }
  2611. p2p_set_state(p2p, P2P_CONNECT);
  2612. p2p_set_timeout(p2p, 0, 500000);
  2613. }
  2614. static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success,
  2615. const u8 *addr)
  2616. {
  2617. p2p_dbg(p2p, "GO Negotiation Response (failure) TX callback: success=%d", success);
  2618. if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) {
  2619. p2p_go_neg_failed(p2p, p2p->go_neg_peer->status);
  2620. } else if (success) {
  2621. struct p2p_device *dev;
  2622. dev = p2p_get_device(p2p, addr);
  2623. if (dev &&
  2624. dev->status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE)
  2625. dev->flags |= P2P_DEV_PEER_WAITING_RESPONSE;
  2626. }
  2627. }
  2628. static void p2p_go_neg_conf_cb(struct p2p_data *p2p,
  2629. enum p2p_send_action_result result)
  2630. {
  2631. struct p2p_device *dev;
  2632. p2p_dbg(p2p, "GO Negotiation Confirm TX callback: result=%d", result);
  2633. if (result == P2P_SEND_ACTION_FAILED) {
  2634. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  2635. p2p_go_neg_failed(p2p, -1);
  2636. return;
  2637. }
  2638. dev = p2p->go_neg_peer;
  2639. if (result == P2P_SEND_ACTION_NO_ACK) {
  2640. /*
  2641. * Retry GO Negotiation Confirmation
  2642. * P2P_GO_NEG_CNF_MAX_RETRY_COUNT times if we did not receive
  2643. * ACK for confirmation.
  2644. */
  2645. if (dev && dev->go_neg_conf &&
  2646. dev->go_neg_conf_sent <= P2P_GO_NEG_CNF_MAX_RETRY_COUNT) {
  2647. p2p_dbg(p2p, "GO Negotiation Confirm retry %d",
  2648. dev->go_neg_conf_sent);
  2649. p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM;
  2650. if (p2p_send_action(p2p, dev->go_neg_conf_freq,
  2651. dev->info.p2p_device_addr,
  2652. p2p->cfg->dev_addr,
  2653. dev->info.p2p_device_addr,
  2654. wpabuf_head(dev->go_neg_conf),
  2655. wpabuf_len(dev->go_neg_conf), 0) >=
  2656. 0) {
  2657. dev->go_neg_conf_sent++;
  2658. return;
  2659. }
  2660. p2p_dbg(p2p, "Failed to re-send Action frame");
  2661. /*
  2662. * Continue with the assumption that the first attempt
  2663. * went through and just the ACK frame was lost.
  2664. */
  2665. }
  2666. /*
  2667. * It looks like the TX status for GO Negotiation Confirm is
  2668. * often showing failure even when the peer has actually
  2669. * received the frame. Since the peer may change channels
  2670. * immediately after having received the frame, we may not see
  2671. * an Ack for retries, so just dropping a single frame may
  2672. * trigger this. To allow the group formation to succeed if the
  2673. * peer did indeed receive the frame, continue regardless of
  2674. * the TX status.
  2675. */
  2676. p2p_dbg(p2p, "Assume GO Negotiation Confirm TX was actually received by the peer even though Ack was not reported");
  2677. }
  2678. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  2679. if (dev == NULL)
  2680. return;
  2681. p2p_go_complete(p2p, dev);
  2682. }
  2683. void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
  2684. const u8 *src, const u8 *bssid,
  2685. enum p2p_send_action_result result)
  2686. {
  2687. enum p2p_pending_action_state state;
  2688. int success;
  2689. p2p_dbg(p2p, "Action frame TX callback (state=%d freq=%u dst=" MACSTR
  2690. " src=" MACSTR " bssid=" MACSTR " result=%d",
  2691. p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src),
  2692. MAC2STR(bssid), result);
  2693. success = result == P2P_SEND_ACTION_SUCCESS;
  2694. state = p2p->pending_action_state;
  2695. p2p->pending_action_state = P2P_NO_PENDING_ACTION;
  2696. switch (state) {
  2697. case P2P_NO_PENDING_ACTION:
  2698. if (p2p->send_action_in_progress) {
  2699. p2p->send_action_in_progress = 0;
  2700. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  2701. }
  2702. if (p2p->after_scan_tx_in_progress) {
  2703. p2p->after_scan_tx_in_progress = 0;
  2704. if (p2p->start_after_scan != P2P_AFTER_SCAN_NOTHING &&
  2705. p2p_run_after_scan(p2p))
  2706. break;
  2707. if (p2p->state == P2P_SEARCH) {
  2708. p2p_dbg(p2p, "Continue find after after_scan_tx completion");
  2709. p2p_continue_find(p2p);
  2710. }
  2711. }
  2712. break;
  2713. case P2P_PENDING_GO_NEG_REQUEST:
  2714. p2p_go_neg_req_cb(p2p, success);
  2715. break;
  2716. case P2P_PENDING_GO_NEG_RESPONSE:
  2717. p2p_go_neg_resp_cb(p2p, success);
  2718. break;
  2719. case P2P_PENDING_GO_NEG_RESPONSE_FAILURE:
  2720. p2p_go_neg_resp_failure_cb(p2p, success, dst);
  2721. break;
  2722. case P2P_PENDING_GO_NEG_CONFIRM:
  2723. p2p_go_neg_conf_cb(p2p, result);
  2724. break;
  2725. case P2P_PENDING_SD:
  2726. p2p_sd_cb(p2p, success);
  2727. break;
  2728. case P2P_PENDING_PD:
  2729. p2p_prov_disc_cb(p2p, success);
  2730. break;
  2731. case P2P_PENDING_INVITATION_REQUEST:
  2732. p2p_invitation_req_cb(p2p, success);
  2733. break;
  2734. case P2P_PENDING_INVITATION_RESPONSE:
  2735. p2p_invitation_resp_cb(p2p, success);
  2736. break;
  2737. case P2P_PENDING_DEV_DISC_REQUEST:
  2738. p2p_dev_disc_req_cb(p2p, success);
  2739. break;
  2740. case P2P_PENDING_DEV_DISC_RESPONSE:
  2741. p2p_dev_disc_resp_cb(p2p, success);
  2742. break;
  2743. case P2P_PENDING_GO_DISC_REQ:
  2744. p2p_go_disc_req_cb(p2p, success);
  2745. break;
  2746. }
  2747. p2p->after_scan_tx_in_progress = 0;
  2748. }
  2749. void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
  2750. unsigned int duration)
  2751. {
  2752. if (freq == p2p->pending_client_disc_freq) {
  2753. p2p_dbg(p2p, "Client discoverability remain-awake completed");
  2754. p2p->pending_client_disc_freq = 0;
  2755. return;
  2756. }
  2757. if (freq != p2p->pending_listen_freq) {
  2758. p2p_dbg(p2p, "Unexpected listen callback for freq=%u duration=%u (pending_listen_freq=%u)",
  2759. freq, duration, p2p->pending_listen_freq);
  2760. return;
  2761. }
  2762. p2p_dbg(p2p, "Starting Listen timeout(%u,%u) on freq=%u based on callback",
  2763. p2p->pending_listen_sec, p2p->pending_listen_usec,
  2764. p2p->pending_listen_freq);
  2765. p2p->in_listen = 1;
  2766. p2p->drv_in_listen = freq;
  2767. if (p2p->pending_listen_sec || p2p->pending_listen_usec) {
  2768. /*
  2769. * Add 20 msec extra wait to avoid race condition with driver
  2770. * remain-on-channel end event, i.e., give driver more time to
  2771. * complete the operation before our timeout expires.
  2772. */
  2773. p2p_set_timeout(p2p, p2p->pending_listen_sec,
  2774. p2p->pending_listen_usec + 20000);
  2775. }
  2776. p2p->pending_listen_freq = 0;
  2777. }
  2778. int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
  2779. {
  2780. p2p_dbg(p2p, "Driver ended Listen state (freq=%u)", freq);
  2781. p2p->drv_in_listen = 0;
  2782. if (p2p->in_listen)
  2783. return 0; /* Internal timeout will trigger the next step */
  2784. if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) {
  2785. if (p2p->go_neg_peer->connect_reqs >= 120) {
  2786. p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response");
  2787. p2p_go_neg_failed(p2p, -1);
  2788. return 0;
  2789. }
  2790. p2p_set_state(p2p, P2P_CONNECT);
  2791. p2p_connect_send(p2p, p2p->go_neg_peer);
  2792. return 1;
  2793. } else if (p2p->state == P2P_SEARCH) {
  2794. if (p2p->p2p_scan_running) {
  2795. /*
  2796. * Search is already in progress. This can happen if
  2797. * an Action frame RX is reported immediately after
  2798. * the end of a remain-on-channel operation and the
  2799. * response frame to that is sent using an offchannel
  2800. * operation while in p2p_find. Avoid an attempt to
  2801. * restart a scan here.
  2802. */
  2803. p2p_dbg(p2p, "p2p_scan already in progress - do not try to start a new one");
  2804. return 1;
  2805. }
  2806. if (p2p->pending_listen_freq) {
  2807. /*
  2808. * Better wait a bit if the driver is unable to start
  2809. * offchannel operation for some reason. p2p_search()
  2810. * will be started from internal timeout.
  2811. */
  2812. p2p_dbg(p2p, "Listen operation did not seem to start - delay search phase to avoid busy loop");
  2813. p2p_set_timeout(p2p, 0, 100000);
  2814. return 1;
  2815. }
  2816. if (p2p->search_delay) {
  2817. p2p_dbg(p2p, "Delay search operation by %u ms",
  2818. p2p->search_delay);
  2819. p2p_set_timeout(p2p, p2p->search_delay / 1000,
  2820. (p2p->search_delay % 1000) * 1000);
  2821. return 1;
  2822. }
  2823. p2p_search(p2p);
  2824. return 1;
  2825. }
  2826. return 0;
  2827. }
  2828. static void p2p_timeout_connect(struct p2p_data *p2p)
  2829. {
  2830. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  2831. if (p2p->go_neg_peer &&
  2832. (p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
  2833. p2p_dbg(p2p, "Wait for GO Negotiation Confirm timed out - assume GO Negotiation failed");
  2834. p2p_go_neg_failed(p2p, -1);
  2835. return;
  2836. }
  2837. if (p2p->go_neg_peer &&
  2838. (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE) &&
  2839. p2p->go_neg_peer->connect_reqs < 120) {
  2840. p2p_dbg(p2p, "Peer expected to wait our response - skip listen");
  2841. p2p_connect_send(p2p, p2p->go_neg_peer);
  2842. return;
  2843. }
  2844. if (p2p->go_neg_peer && p2p->go_neg_peer->oob_go_neg_freq > 0) {
  2845. p2p_dbg(p2p, "Skip connect-listen since GO Neg channel known (OOB)");
  2846. p2p_set_state(p2p, P2P_CONNECT_LISTEN);
  2847. p2p_set_timeout(p2p, 0, 30000);
  2848. return;
  2849. }
  2850. p2p_set_state(p2p, P2P_CONNECT_LISTEN);
  2851. p2p_listen_in_find(p2p, 0);
  2852. }
  2853. static void p2p_timeout_connect_listen(struct p2p_data *p2p)
  2854. {
  2855. if (p2p->go_neg_peer) {
  2856. if (p2p->drv_in_listen) {
  2857. p2p_dbg(p2p, "Driver is still in Listen state; wait for it to complete");
  2858. return;
  2859. }
  2860. if (p2p->go_neg_peer->connect_reqs >= 120) {
  2861. p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response");
  2862. p2p_go_neg_failed(p2p, -1);
  2863. return;
  2864. }
  2865. p2p_set_state(p2p, P2P_CONNECT);
  2866. p2p_connect_send(p2p, p2p->go_neg_peer);
  2867. } else
  2868. p2p_set_state(p2p, P2P_IDLE);
  2869. }
  2870. static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p)
  2871. {
  2872. p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
  2873. if (p2p->cfg->is_concurrent_session_active &&
  2874. p2p->cfg->is_concurrent_session_active(p2p->cfg->cb_ctx))
  2875. p2p_set_timeout(p2p, 0, 500000);
  2876. else
  2877. p2p_set_timeout(p2p, 0, 200000);
  2878. }
  2879. static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p)
  2880. {
  2881. struct p2p_device *dev = p2p->go_neg_peer;
  2882. if (dev == NULL) {
  2883. p2p_dbg(p2p, "Unknown GO Neg peer - stop GO Neg wait");
  2884. return;
  2885. }
  2886. p2p_dbg(p2p, "Go to Listen state while waiting for the peer to become ready for GO Negotiation");
  2887. p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
  2888. p2p_listen_in_find(p2p, 0);
  2889. }
  2890. static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
  2891. {
  2892. p2p_dbg(p2p, "Service Discovery Query timeout");
  2893. if (p2p->sd_peer) {
  2894. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  2895. p2p->sd_peer = NULL;
  2896. }
  2897. p2p_continue_find(p2p);
  2898. }
  2899. static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p)
  2900. {
  2901. p2p_dbg(p2p, "Provision Discovery Request timeout");
  2902. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  2903. p2p_continue_find(p2p);
  2904. }
  2905. static void p2p_timeout_prov_disc_req(struct p2p_data *p2p)
  2906. {
  2907. p2p->pending_action_state = P2P_NO_PENDING_ACTION;
  2908. /*
  2909. * For user initiated PD requests that we have not gotten any responses
  2910. * for while in IDLE state, we retry them a couple of times before
  2911. * giving up.
  2912. */
  2913. if (!p2p->user_initiated_pd)
  2914. return;
  2915. p2p_dbg(p2p, "User initiated Provision Discovery Request timeout");
  2916. if (p2p->pd_retries) {
  2917. p2p->pd_retries--;
  2918. p2p_retry_pd(p2p);
  2919. } else {
  2920. struct p2p_device *dev;
  2921. int for_join = 0;
  2922. dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
  2923. if (os_memcmp(p2p->pending_pd_devaddr,
  2924. dev->info.p2p_device_addr, ETH_ALEN) != 0)
  2925. continue;
  2926. if (dev->req_config_methods &&
  2927. (dev->flags & P2P_DEV_PD_FOR_JOIN))
  2928. for_join = 1;
  2929. }
  2930. if (p2p->cfg->prov_disc_fail)
  2931. p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx,
  2932. p2p->pending_pd_devaddr,
  2933. for_join ?
  2934. P2P_PROV_DISC_TIMEOUT_JOIN :
  2935. P2P_PROV_DISC_TIMEOUT);
  2936. p2p_reset_pending_pd(p2p);
  2937. }
  2938. }
  2939. static void p2p_timeout_invite(struct p2p_data *p2p)
  2940. {
  2941. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  2942. p2p_set_state(p2p, P2P_INVITE_LISTEN);
  2943. if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
  2944. /*
  2945. * Better remain on operating channel instead of listen channel
  2946. * when running a group.
  2947. */
  2948. p2p_dbg(p2p, "Inviting in active GO role - wait on operating channel");
  2949. p2p_set_timeout(p2p, 0, 100000);
  2950. return;
  2951. }
  2952. p2p_listen_in_find(p2p, 0);
  2953. }
  2954. static void p2p_timeout_invite_listen(struct p2p_data *p2p)
  2955. {
  2956. if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) {
  2957. p2p_set_state(p2p, P2P_INVITE);
  2958. p2p_invite_send(p2p, p2p->invite_peer,
  2959. p2p->invite_go_dev_addr, p2p->invite_dev_pw_id);
  2960. } else {
  2961. if (p2p->invite_peer) {
  2962. p2p_dbg(p2p, "Invitation Request retry limit reached");
  2963. if (p2p->cfg->invitation_result)
  2964. p2p->cfg->invitation_result(
  2965. p2p->cfg->cb_ctx, -1, NULL, NULL,
  2966. p2p->invite_peer->info.p2p_device_addr,
  2967. 0, 0);
  2968. }
  2969. p2p_set_state(p2p, P2P_IDLE);
  2970. }
  2971. }
  2972. static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
  2973. {
  2974. struct p2p_data *p2p = eloop_ctx;
  2975. p2p_dbg(p2p, "Timeout (state=%s)", p2p_state_txt(p2p->state));
  2976. p2p->in_listen = 0;
  2977. if (p2p->drv_in_listen) {
  2978. p2p_dbg(p2p, "Driver is still in listen state - stop it");
  2979. p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
  2980. }
  2981. switch (p2p->state) {
  2982. case P2P_IDLE:
  2983. /* Check if we timed out waiting for PD req */
  2984. if (p2p->pending_action_state == P2P_PENDING_PD)
  2985. p2p_timeout_prov_disc_req(p2p);
  2986. break;
  2987. case P2P_SEARCH:
  2988. /* Check if we timed out waiting for PD req */
  2989. if (p2p->pending_action_state == P2P_PENDING_PD)
  2990. p2p_timeout_prov_disc_req(p2p);
  2991. if (p2p->search_delay && !p2p->in_search_delay) {
  2992. p2p_dbg(p2p, "Delay search operation by %u ms",
  2993. p2p->search_delay);
  2994. p2p->in_search_delay = 1;
  2995. p2p_set_timeout(p2p, p2p->search_delay / 1000,
  2996. (p2p->search_delay % 1000) * 1000);
  2997. break;
  2998. }
  2999. p2p->in_search_delay = 0;
  3000. p2p_search(p2p);
  3001. break;
  3002. case P2P_CONNECT:
  3003. p2p_timeout_connect(p2p);
  3004. break;
  3005. case P2P_CONNECT_LISTEN:
  3006. p2p_timeout_connect_listen(p2p);
  3007. break;
  3008. case P2P_GO_NEG:
  3009. break;
  3010. case P2P_LISTEN_ONLY:
  3011. /* Check if we timed out waiting for PD req */
  3012. if (p2p->pending_action_state == P2P_PENDING_PD)
  3013. p2p_timeout_prov_disc_req(p2p);
  3014. if (p2p->ext_listen_only) {
  3015. p2p_dbg(p2p, "Extended Listen Timing - Listen State completed");
  3016. p2p->ext_listen_only = 0;
  3017. p2p_set_state(p2p, P2P_IDLE);
  3018. }
  3019. break;
  3020. case P2P_WAIT_PEER_CONNECT:
  3021. p2p_timeout_wait_peer_connect(p2p);
  3022. break;
  3023. case P2P_WAIT_PEER_IDLE:
  3024. p2p_timeout_wait_peer_idle(p2p);
  3025. break;
  3026. case P2P_SD_DURING_FIND:
  3027. p2p_timeout_sd_during_find(p2p);
  3028. break;
  3029. case P2P_PROVISIONING:
  3030. break;
  3031. case P2P_PD_DURING_FIND:
  3032. p2p_timeout_prov_disc_during_find(p2p);
  3033. break;
  3034. case P2P_INVITE:
  3035. p2p_timeout_invite(p2p);
  3036. break;
  3037. case P2P_INVITE_LISTEN:
  3038. p2p_timeout_invite_listen(p2p);
  3039. break;
  3040. }
  3041. }
  3042. int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr)
  3043. {
  3044. struct p2p_device *dev;
  3045. dev = p2p_get_device(p2p, peer_addr);
  3046. p2p_dbg(p2p, "Local request to reject connection attempts by peer "
  3047. MACSTR, MAC2STR(peer_addr));
  3048. if (dev == NULL) {
  3049. p2p_dbg(p2p, "Peer " MACSTR " unknown", MAC2STR(peer_addr));
  3050. return -1;
  3051. }
  3052. dev->status = P2P_SC_FAIL_REJECTED_BY_USER;
  3053. dev->flags |= P2P_DEV_USER_REJECTED;
  3054. return 0;
  3055. }
  3056. const char * p2p_wps_method_text(enum p2p_wps_method method)
  3057. {
  3058. switch (method) {
  3059. case WPS_NOT_READY:
  3060. return "not-ready";
  3061. case WPS_PIN_DISPLAY:
  3062. return "Display";
  3063. case WPS_PIN_KEYPAD:
  3064. return "Keypad";
  3065. case WPS_PBC:
  3066. return "PBC";
  3067. case WPS_NFC:
  3068. return "NFC";
  3069. }
  3070. return "??";
  3071. }
  3072. static const char * p2p_go_state_text(enum p2p_go_state go_state)
  3073. {
  3074. switch (go_state) {
  3075. case UNKNOWN_GO:
  3076. return "unknown";
  3077. case LOCAL_GO:
  3078. return "local";
  3079. case REMOTE_GO:
  3080. return "remote";
  3081. }
  3082. return "??";
  3083. }
  3084. const struct p2p_peer_info * p2p_get_peer_info(struct p2p_data *p2p,
  3085. const u8 *addr, int next)
  3086. {
  3087. struct p2p_device *dev;
  3088. if (addr)
  3089. dev = p2p_get_device(p2p, addr);
  3090. else
  3091. dev = dl_list_first(&p2p->devices, struct p2p_device, list);
  3092. if (dev && next) {
  3093. dev = dl_list_first(&dev->list, struct p2p_device, list);
  3094. if (&dev->list == &p2p->devices)
  3095. dev = NULL;
  3096. }
  3097. if (dev == NULL)
  3098. return NULL;
  3099. return &dev->info;
  3100. }
  3101. int p2p_get_peer_info_txt(const struct p2p_peer_info *info,
  3102. char *buf, size_t buflen)
  3103. {
  3104. struct p2p_device *dev;
  3105. int res;
  3106. char *pos, *end;
  3107. struct os_reltime now;
  3108. if (info == NULL)
  3109. return -1;
  3110. dev = (struct p2p_device *) (((u8 *) info) -
  3111. offsetof(struct p2p_device, info));
  3112. pos = buf;
  3113. end = buf + buflen;
  3114. os_get_reltime(&now);
  3115. res = os_snprintf(pos, end - pos,
  3116. "age=%d\n"
  3117. "listen_freq=%d\n"
  3118. "wps_method=%s\n"
  3119. "interface_addr=" MACSTR "\n"
  3120. "member_in_go_dev=" MACSTR "\n"
  3121. "member_in_go_iface=" MACSTR "\n"
  3122. "go_neg_req_sent=%d\n"
  3123. "go_state=%s\n"
  3124. "dialog_token=%u\n"
  3125. "intended_addr=" MACSTR "\n"
  3126. "country=%c%c\n"
  3127. "oper_freq=%d\n"
  3128. "req_config_methods=0x%x\n"
  3129. "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
  3130. "status=%d\n"
  3131. "invitation_reqs=%u\n",
  3132. (int) (now.sec - dev->last_seen.sec),
  3133. dev->listen_freq,
  3134. p2p_wps_method_text(dev->wps_method),
  3135. MAC2STR(dev->interface_addr),
  3136. MAC2STR(dev->member_in_go_dev),
  3137. MAC2STR(dev->member_in_go_iface),
  3138. dev->go_neg_req_sent,
  3139. p2p_go_state_text(dev->go_state),
  3140. dev->dialog_token,
  3141. MAC2STR(dev->intended_addr),
  3142. dev->country[0] ? dev->country[0] : '_',
  3143. dev->country[1] ? dev->country[1] : '_',
  3144. dev->oper_freq,
  3145. dev->req_config_methods,
  3146. dev->flags & P2P_DEV_PROBE_REQ_ONLY ?
  3147. "[PROBE_REQ_ONLY]" : "",
  3148. dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "",
  3149. dev->flags & P2P_DEV_NOT_YET_READY ?
  3150. "[NOT_YET_READY]" : "",
  3151. dev->flags & P2P_DEV_PD_PEER_DISPLAY ?
  3152. "[PD_PEER_DISPLAY]" : "",
  3153. dev->flags & P2P_DEV_PD_PEER_KEYPAD ?
  3154. "[PD_PEER_KEYPAD]" : "",
  3155. dev->flags & P2P_DEV_USER_REJECTED ?
  3156. "[USER_REJECTED]" : "",
  3157. dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ?
  3158. "[PEER_WAITING_RESPONSE]" : "",
  3159. dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ?
  3160. "[PREFER_PERSISTENT_GROUP]" : "",
  3161. dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ?
  3162. "[WAIT_GO_NEG_RESPONSE]" : "",
  3163. dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ?
  3164. "[WAIT_GO_NEG_CONFIRM]" : "",
  3165. dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ?
  3166. "[GROUP_CLIENT_ONLY]" : "",
  3167. dev->flags & P2P_DEV_FORCE_FREQ ?
  3168. "[FORCE_FREQ]" : "",
  3169. dev->flags & P2P_DEV_PD_FOR_JOIN ?
  3170. "[PD_FOR_JOIN]" : "",
  3171. dev->status,
  3172. dev->invitation_reqs);
  3173. if (os_snprintf_error(end - pos, res))
  3174. return pos - buf;
  3175. pos += res;
  3176. if (dev->ext_listen_period) {
  3177. res = os_snprintf(pos, end - pos,
  3178. "ext_listen_period=%u\n"
  3179. "ext_listen_interval=%u\n",
  3180. dev->ext_listen_period,
  3181. dev->ext_listen_interval);
  3182. if (os_snprintf_error(end - pos, res))
  3183. return pos - buf;
  3184. pos += res;
  3185. }
  3186. if (dev->oper_ssid_len) {
  3187. res = os_snprintf(pos, end - pos,
  3188. "oper_ssid=%s\n",
  3189. wpa_ssid_txt(dev->oper_ssid,
  3190. dev->oper_ssid_len));
  3191. if (os_snprintf_error(end - pos, res))
  3192. return pos - buf;
  3193. pos += res;
  3194. }
  3195. #ifdef CONFIG_WIFI_DISPLAY
  3196. if (dev->info.wfd_subelems) {
  3197. res = os_snprintf(pos, end - pos, "wfd_subelems=");
  3198. if (os_snprintf_error(end - pos, res))
  3199. return pos - buf;
  3200. pos += res;
  3201. pos += wpa_snprintf_hex(pos, end - pos,
  3202. wpabuf_head(dev->info.wfd_subelems),
  3203. wpabuf_len(dev->info.wfd_subelems));
  3204. res = os_snprintf(pos, end - pos, "\n");
  3205. if (os_snprintf_error(end - pos, res))
  3206. return pos - buf;
  3207. pos += res;
  3208. }
  3209. #endif /* CONFIG_WIFI_DISPLAY */
  3210. return pos - buf;
  3211. }
  3212. int p2p_peer_known(struct p2p_data *p2p, const u8 *addr)
  3213. {
  3214. return p2p_get_device(p2p, addr) != NULL;
  3215. }
  3216. void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled)
  3217. {
  3218. if (enabled) {
  3219. p2p_dbg(p2p, "Client discoverability enabled");
  3220. p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
  3221. } else {
  3222. p2p_dbg(p2p, "Client discoverability disabled");
  3223. p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
  3224. }
  3225. }
  3226. static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1,
  3227. u32 duration2, u32 interval2)
  3228. {
  3229. struct wpabuf *req;
  3230. struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL;
  3231. u8 *len;
  3232. req = wpabuf_alloc(100);
  3233. if (req == NULL)
  3234. return NULL;
  3235. if (duration1 || interval1) {
  3236. os_memset(&desc1, 0, sizeof(desc1));
  3237. desc1.count_type = 1;
  3238. desc1.duration = duration1;
  3239. desc1.interval = interval1;
  3240. ptr1 = &desc1;
  3241. if (duration2 || interval2) {
  3242. os_memset(&desc2, 0, sizeof(desc2));
  3243. desc2.count_type = 2;
  3244. desc2.duration = duration2;
  3245. desc2.interval = interval2;
  3246. ptr2 = &desc2;
  3247. }
  3248. }
  3249. p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1);
  3250. len = p2p_buf_add_ie_hdr(req);
  3251. p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2);
  3252. p2p_buf_update_ie_hdr(req, len);
  3253. return req;
  3254. }
  3255. int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
  3256. const u8 *own_interface_addr, unsigned int freq,
  3257. u32 duration1, u32 interval1, u32 duration2,
  3258. u32 interval2)
  3259. {
  3260. struct wpabuf *req;
  3261. p2p_dbg(p2p, "Send Presence Request to GO " MACSTR
  3262. " (own interface " MACSTR ") freq=%u dur1=%u int1=%u "
  3263. "dur2=%u int2=%u",
  3264. MAC2STR(go_interface_addr), MAC2STR(own_interface_addr),
  3265. freq, duration1, interval1, duration2, interval2);
  3266. req = p2p_build_presence_req(duration1, interval1, duration2,
  3267. interval2);
  3268. if (req == NULL)
  3269. return -1;
  3270. p2p->pending_action_state = P2P_NO_PENDING_ACTION;
  3271. if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr,
  3272. go_interface_addr,
  3273. wpabuf_head(req), wpabuf_len(req), 200) < 0) {
  3274. p2p_dbg(p2p, "Failed to send Action frame");
  3275. }
  3276. wpabuf_free(req);
  3277. return 0;
  3278. }
  3279. static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa,
  3280. size_t noa_len, u8 dialog_token)
  3281. {
  3282. struct wpabuf *resp;
  3283. u8 *len;
  3284. resp = wpabuf_alloc(100 + noa_len);
  3285. if (resp == NULL)
  3286. return NULL;
  3287. p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token);
  3288. len = p2p_buf_add_ie_hdr(resp);
  3289. p2p_buf_add_status(resp, status);
  3290. if (noa) {
  3291. wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE);
  3292. wpabuf_put_le16(resp, noa_len);
  3293. wpabuf_put_data(resp, noa, noa_len);
  3294. } else
  3295. p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL);
  3296. p2p_buf_update_ie_hdr(resp, len);
  3297. return resp;
  3298. }
  3299. static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
  3300. const u8 *sa, const u8 *data, size_t len,
  3301. int rx_freq)
  3302. {
  3303. struct p2p_message msg;
  3304. u8 status;
  3305. struct wpabuf *resp;
  3306. size_t g;
  3307. struct p2p_group *group = NULL;
  3308. int parsed = 0;
  3309. u8 noa[50];
  3310. int noa_len;
  3311. p2p_dbg(p2p, "Received P2P Action - P2P Presence Request");
  3312. for (g = 0; g < p2p->num_groups; g++) {
  3313. if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]),
  3314. ETH_ALEN) == 0) {
  3315. group = p2p->groups[g];
  3316. break;
  3317. }
  3318. }
  3319. if (group == NULL) {
  3320. p2p_dbg(p2p, "Ignore P2P Presence Request for unknown group "
  3321. MACSTR, MAC2STR(da));
  3322. return;
  3323. }
  3324. if (p2p_parse(data, len, &msg) < 0) {
  3325. p2p_dbg(p2p, "Failed to parse P2P Presence Request");
  3326. status = P2P_SC_FAIL_INVALID_PARAMS;
  3327. goto fail;
  3328. }
  3329. parsed = 1;
  3330. if (msg.noa == NULL) {
  3331. p2p_dbg(p2p, "No NoA attribute in P2P Presence Request");
  3332. status = P2P_SC_FAIL_INVALID_PARAMS;
  3333. goto fail;
  3334. }
  3335. status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len);
  3336. fail:
  3337. if (p2p->cfg->get_noa)
  3338. noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa,
  3339. sizeof(noa));
  3340. else
  3341. noa_len = -1;
  3342. resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL,
  3343. noa_len > 0 ? noa_len : 0,
  3344. msg.dialog_token);
  3345. if (parsed)
  3346. p2p_parse_free(&msg);
  3347. if (resp == NULL)
  3348. return;
  3349. p2p->pending_action_state = P2P_NO_PENDING_ACTION;
  3350. if (p2p_send_action(p2p, rx_freq, sa, da, da,
  3351. wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
  3352. p2p_dbg(p2p, "Failed to send Action frame");
  3353. }
  3354. wpabuf_free(resp);
  3355. }
  3356. static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
  3357. const u8 *sa, const u8 *data, size_t len)
  3358. {
  3359. struct p2p_message msg;
  3360. p2p_dbg(p2p, "Received P2P Action - P2P Presence Response");
  3361. if (p2p_parse(data, len, &msg) < 0) {
  3362. p2p_dbg(p2p, "Failed to parse P2P Presence Response");
  3363. return;
  3364. }
  3365. if (msg.status == NULL || msg.noa == NULL) {
  3366. p2p_dbg(p2p, "No Status or NoA attribute in P2P Presence Response");
  3367. p2p_parse_free(&msg);
  3368. return;
  3369. }
  3370. if (p2p->cfg->presence_resp) {
  3371. p2p->cfg->presence_resp(p2p->cfg->cb_ctx, sa, *msg.status,
  3372. msg.noa, msg.noa_len);
  3373. }
  3374. if (*msg.status) {
  3375. p2p_dbg(p2p, "P2P Presence Request was rejected: status %u",
  3376. *msg.status);
  3377. p2p_parse_free(&msg);
  3378. return;
  3379. }
  3380. p2p_dbg(p2p, "P2P Presence Request was accepted");
  3381. wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA",
  3382. msg.noa, msg.noa_len);
  3383. /* TODO: process NoA */
  3384. p2p_parse_free(&msg);
  3385. }
  3386. static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx)
  3387. {
  3388. struct p2p_data *p2p = eloop_ctx;
  3389. if (p2p->ext_listen_interval) {
  3390. /* Schedule next extended listen timeout */
  3391. eloop_register_timeout(p2p->ext_listen_interval_sec,
  3392. p2p->ext_listen_interval_usec,
  3393. p2p_ext_listen_timeout, p2p, NULL);
  3394. }
  3395. if ((p2p->cfg->is_p2p_in_progress &&
  3396. p2p->cfg->is_p2p_in_progress(p2p->cfg->cb_ctx)) ||
  3397. (p2p->pending_action_state == P2P_PENDING_PD &&
  3398. p2p->pd_retries > 0)) {
  3399. p2p_dbg(p2p, "Operation in progress - skip Extended Listen timeout (%s)",
  3400. p2p_state_txt(p2p->state));
  3401. return;
  3402. }
  3403. if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) {
  3404. /*
  3405. * This should not really happen, but it looks like the Listen
  3406. * command may fail is something else (e.g., a scan) was
  3407. * running at an inconvenient time. As a workaround, allow new
  3408. * Extended Listen operation to be started.
  3409. */
  3410. p2p_dbg(p2p, "Previous Extended Listen operation had not been completed - try again");
  3411. p2p->ext_listen_only = 0;
  3412. p2p_set_state(p2p, P2P_IDLE);
  3413. }
  3414. if (p2p->state != P2P_IDLE) {
  3415. p2p_dbg(p2p, "Skip Extended Listen timeout in active state (%s)", p2p_state_txt(p2p->state));
  3416. return;
  3417. }
  3418. p2p_dbg(p2p, "Extended Listen timeout");
  3419. p2p->ext_listen_only = 1;
  3420. if (p2p_listen(p2p, p2p->ext_listen_period) < 0) {
  3421. p2p_dbg(p2p, "Failed to start Listen state for Extended Listen Timing");
  3422. p2p->ext_listen_only = 0;
  3423. }
  3424. }
  3425. int p2p_ext_listen(struct p2p_data *p2p, unsigned int period,
  3426. unsigned int interval)
  3427. {
  3428. if (period > 65535 || interval > 65535 || period > interval ||
  3429. (period == 0 && interval > 0) || (period > 0 && interval == 0)) {
  3430. p2p_dbg(p2p, "Invalid Extended Listen Timing request: period=%u interval=%u",
  3431. period, interval);
  3432. return -1;
  3433. }
  3434. eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
  3435. if (interval == 0) {
  3436. p2p_dbg(p2p, "Disabling Extended Listen Timing");
  3437. p2p->ext_listen_period = 0;
  3438. p2p->ext_listen_interval = 0;
  3439. return 0;
  3440. }
  3441. p2p_dbg(p2p, "Enabling Extended Listen Timing: period %u msec, interval %u msec",
  3442. period, interval);
  3443. p2p->ext_listen_period = period;
  3444. p2p->ext_listen_interval = interval;
  3445. p2p->ext_listen_interval_sec = interval / 1000;
  3446. p2p->ext_listen_interval_usec = (interval % 1000) * 1000;
  3447. eloop_register_timeout(p2p->ext_listen_interval_sec,
  3448. p2p->ext_listen_interval_usec,
  3449. p2p_ext_listen_timeout, p2p, NULL);
  3450. return 0;
  3451. }
  3452. void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
  3453. const u8 *ie, size_t ie_len)
  3454. {
  3455. struct p2p_message msg;
  3456. if (bssid == NULL || ie == NULL)
  3457. return;
  3458. os_memset(&msg, 0, sizeof(msg));
  3459. if (p2p_parse_ies(ie, ie_len, &msg))
  3460. return;
  3461. if (msg.minor_reason_code == NULL) {
  3462. p2p_parse_free(&msg);
  3463. return;
  3464. }
  3465. p2p_dbg(p2p, "Deauthentication notification BSSID " MACSTR
  3466. " reason_code=%u minor_reason_code=%u",
  3467. MAC2STR(bssid), reason_code, *msg.minor_reason_code);
  3468. p2p_parse_free(&msg);
  3469. }
  3470. void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
  3471. const u8 *ie, size_t ie_len)
  3472. {
  3473. struct p2p_message msg;
  3474. if (bssid == NULL || ie == NULL)
  3475. return;
  3476. os_memset(&msg, 0, sizeof(msg));
  3477. if (p2p_parse_ies(ie, ie_len, &msg))
  3478. return;
  3479. if (msg.minor_reason_code == NULL) {
  3480. p2p_parse_free(&msg);
  3481. return;
  3482. }
  3483. p2p_dbg(p2p, "Disassociation notification BSSID " MACSTR
  3484. " reason_code=%u minor_reason_code=%u",
  3485. MAC2STR(bssid), reason_code, *msg.minor_reason_code);
  3486. p2p_parse_free(&msg);
  3487. }
  3488. void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
  3489. {
  3490. if (enabled) {
  3491. p2p_dbg(p2p, "Managed P2P Device operations enabled");
  3492. p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED;
  3493. } else {
  3494. p2p_dbg(p2p, "Managed P2P Device operations disabled");
  3495. p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED;
  3496. }
  3497. }
  3498. int p2p_config_get_random_social(struct p2p_config *p2p, u8 *op_class,
  3499. u8 *op_channel)
  3500. {
  3501. return p2p_channel_random_social(&p2p->channels, op_class, op_channel);
  3502. }
  3503. int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel,
  3504. u8 forced)
  3505. {
  3506. if (p2p_channel_to_freq(reg_class, channel) < 0)
  3507. return -1;
  3508. p2p_dbg(p2p, "Set Listen channel: reg_class %u channel %u",
  3509. reg_class, channel);
  3510. /*
  3511. * Listen channel was set in configuration or set by control interface;
  3512. * cannot override it.
  3513. */
  3514. if (p2p->cfg->channel_forced && forced == 0)
  3515. return -1;
  3516. if (p2p->state == P2P_IDLE) {
  3517. p2p->cfg->reg_class = reg_class;
  3518. p2p->cfg->channel = channel;
  3519. p2p->cfg->channel_forced = forced;
  3520. } else {
  3521. p2p_dbg(p2p, "Defer setting listen channel");
  3522. p2p->pending_reg_class = reg_class;
  3523. p2p->pending_channel = channel;
  3524. p2p->pending_channel_forced = forced;
  3525. }
  3526. return 0;
  3527. }
  3528. u8 p2p_get_listen_channel(struct p2p_data *p2p)
  3529. {
  3530. return p2p->cfg->channel;
  3531. }
  3532. int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
  3533. {
  3534. p2p_dbg(p2p, "New SSID postfix: %s", wpa_ssid_txt(postfix, len));
  3535. if (postfix == NULL) {
  3536. p2p->cfg->ssid_postfix_len = 0;
  3537. return 0;
  3538. }
  3539. if (len > sizeof(p2p->cfg->ssid_postfix))
  3540. return -1;
  3541. os_memcpy(p2p->cfg->ssid_postfix, postfix, len);
  3542. p2p->cfg->ssid_postfix_len = len;
  3543. return 0;
  3544. }
  3545. int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel,
  3546. int cfg_op_channel)
  3547. {
  3548. if (p2p_channel_to_freq(op_reg_class, op_channel) < 0)
  3549. return -1;
  3550. p2p_dbg(p2p, "Set Operating channel: reg_class %u channel %u",
  3551. op_reg_class, op_channel);
  3552. p2p->cfg->op_reg_class = op_reg_class;
  3553. p2p->cfg->op_channel = op_channel;
  3554. p2p->cfg->cfg_op_channel = cfg_op_channel;
  3555. return 0;
  3556. }
  3557. int p2p_set_pref_chan(struct p2p_data *p2p, unsigned int num_pref_chan,
  3558. const struct p2p_channel *pref_chan)
  3559. {
  3560. struct p2p_channel *n;
  3561. if (pref_chan) {
  3562. n = os_malloc(num_pref_chan * sizeof(struct p2p_channel));
  3563. if (n == NULL)
  3564. return -1;
  3565. os_memcpy(n, pref_chan,
  3566. num_pref_chan * sizeof(struct p2p_channel));
  3567. } else
  3568. n = NULL;
  3569. os_free(p2p->cfg->pref_chan);
  3570. p2p->cfg->pref_chan = n;
  3571. p2p->cfg->num_pref_chan = num_pref_chan;
  3572. return 0;
  3573. }
  3574. int p2p_set_no_go_freq(struct p2p_data *p2p,
  3575. const struct wpa_freq_range_list *list)
  3576. {
  3577. struct wpa_freq_range *tmp;
  3578. if (list == NULL || list->num == 0) {
  3579. os_free(p2p->no_go_freq.range);
  3580. p2p->no_go_freq.range = NULL;
  3581. p2p->no_go_freq.num = 0;
  3582. return 0;
  3583. }
  3584. tmp = os_calloc(list->num, sizeof(struct wpa_freq_range));
  3585. if (tmp == NULL)
  3586. return -1;
  3587. os_memcpy(tmp, list->range, list->num * sizeof(struct wpa_freq_range));
  3588. os_free(p2p->no_go_freq.range);
  3589. p2p->no_go_freq.range = tmp;
  3590. p2p->no_go_freq.num = list->num;
  3591. p2p_dbg(p2p, "Updated no GO chan list");
  3592. return 0;
  3593. }
  3594. int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr,
  3595. u8 *iface_addr)
  3596. {
  3597. struct p2p_device *dev = p2p_get_device(p2p, dev_addr);
  3598. if (dev == NULL || is_zero_ether_addr(dev->interface_addr))
  3599. return -1;
  3600. os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN);
  3601. return 0;
  3602. }
  3603. int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr,
  3604. u8 *dev_addr)
  3605. {
  3606. struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
  3607. if (dev == NULL)
  3608. return -1;
  3609. os_memcpy(dev_addr, dev->info.p2p_device_addr, ETH_ALEN);
  3610. return 0;
  3611. }
  3612. void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr)
  3613. {
  3614. os_memcpy(p2p->peer_filter, addr, ETH_ALEN);
  3615. if (is_zero_ether_addr(p2p->peer_filter))
  3616. p2p_dbg(p2p, "Disable peer filter");
  3617. else
  3618. p2p_dbg(p2p, "Enable peer filter for " MACSTR,
  3619. MAC2STR(p2p->peer_filter));
  3620. }
  3621. void p2p_set_cross_connect(struct p2p_data *p2p, int enabled)
  3622. {
  3623. p2p_dbg(p2p, "Cross connection %s", enabled ? "enabled" : "disabled");
  3624. if (p2p->cross_connect == enabled)
  3625. return;
  3626. p2p->cross_connect = enabled;
  3627. /* TODO: may need to tear down any action group where we are GO(?) */
  3628. }
  3629. int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr)
  3630. {
  3631. struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
  3632. if (dev == NULL)
  3633. return -1;
  3634. if (dev->oper_freq <= 0)
  3635. return -1;
  3636. return dev->oper_freq;
  3637. }
  3638. void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled)
  3639. {
  3640. p2p_dbg(p2p, "Intra BSS distribution %s",
  3641. enabled ? "enabled" : "disabled");
  3642. p2p->cfg->p2p_intra_bss = enabled;
  3643. }
  3644. void p2p_update_channel_list(struct p2p_data *p2p,
  3645. const struct p2p_channels *chan,
  3646. const struct p2p_channels *cli_chan)
  3647. {
  3648. p2p_dbg(p2p, "Update channel list");
  3649. os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels));
  3650. p2p_channels_dump(p2p, "channels", &p2p->cfg->channels);
  3651. os_memcpy(&p2p->cfg->cli_channels, cli_chan,
  3652. sizeof(struct p2p_channels));
  3653. p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels);
  3654. }
  3655. int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
  3656. const u8 *src, const u8 *bssid, const u8 *buf,
  3657. size_t len, unsigned int wait_time)
  3658. {
  3659. if (p2p->p2p_scan_running) {
  3660. p2p_dbg(p2p, "Delay Action frame TX until p2p_scan completes");
  3661. if (p2p->after_scan_tx) {
  3662. p2p_dbg(p2p, "Dropped previous pending Action frame TX");
  3663. os_free(p2p->after_scan_tx);
  3664. }
  3665. p2p->after_scan_tx = os_malloc(sizeof(*p2p->after_scan_tx) +
  3666. len);
  3667. if (p2p->after_scan_tx == NULL)
  3668. return -1;
  3669. p2p->after_scan_tx->freq = freq;
  3670. os_memcpy(p2p->after_scan_tx->dst, dst, ETH_ALEN);
  3671. os_memcpy(p2p->after_scan_tx->src, src, ETH_ALEN);
  3672. os_memcpy(p2p->after_scan_tx->bssid, bssid, ETH_ALEN);
  3673. p2p->after_scan_tx->len = len;
  3674. p2p->after_scan_tx->wait_time = wait_time;
  3675. os_memcpy(p2p->after_scan_tx + 1, buf, len);
  3676. return 0;
  3677. }
  3678. return p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid,
  3679. buf, len, wait_time);
  3680. }
  3681. void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5,
  3682. int freq_overall)
  3683. {
  3684. p2p_dbg(p2p, "Best channel: 2.4 GHz: %d, 5 GHz: %d, overall: %d",
  3685. freq_24, freq_5, freq_overall);
  3686. p2p->best_freq_24 = freq_24;
  3687. p2p->best_freq_5 = freq_5;
  3688. p2p->best_freq_overall = freq_overall;
  3689. }
  3690. void p2p_set_own_freq_preference(struct p2p_data *p2p, int freq)
  3691. {
  3692. p2p_dbg(p2p, "Own frequency preference: %d MHz", freq);
  3693. p2p->own_freq_preference = freq;
  3694. }
  3695. const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p)
  3696. {
  3697. if (p2p == NULL || p2p->go_neg_peer == NULL)
  3698. return NULL;
  3699. return p2p->go_neg_peer->info.p2p_device_addr;
  3700. }
  3701. const struct p2p_peer_info *
  3702. p2p_get_peer_found(struct p2p_data *p2p, const u8 *addr, int next)
  3703. {
  3704. struct p2p_device *dev;
  3705. if (addr) {
  3706. dev = p2p_get_device(p2p, addr);
  3707. if (!dev)
  3708. return NULL;
  3709. if (!next) {
  3710. if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
  3711. return NULL;
  3712. return &dev->info;
  3713. } else {
  3714. do {
  3715. dev = dl_list_first(&dev->list,
  3716. struct p2p_device,
  3717. list);
  3718. if (!dev || &dev->list == &p2p->devices)
  3719. return NULL;
  3720. } while (dev->flags & P2P_DEV_PROBE_REQ_ONLY);
  3721. }
  3722. } else {
  3723. dev = dl_list_first(&p2p->devices, struct p2p_device, list);
  3724. if (!dev)
  3725. return NULL;
  3726. while (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
  3727. dev = dl_list_first(&dev->list,
  3728. struct p2p_device,
  3729. list);
  3730. if (!dev || &dev->list == &p2p->devices)
  3731. return NULL;
  3732. }
  3733. }
  3734. return &dev->info;
  3735. }
  3736. int p2p_in_progress(struct p2p_data *p2p)
  3737. {
  3738. if (p2p == NULL)
  3739. return 0;
  3740. if (p2p->state == P2P_SEARCH)
  3741. return 2;
  3742. return p2p->state != P2P_IDLE && p2p->state != P2P_PROVISIONING;
  3743. }
  3744. void p2p_set_config_timeout(struct p2p_data *p2p, u8 go_timeout,
  3745. u8 client_timeout)
  3746. {
  3747. if (p2p) {
  3748. p2p->go_timeout = go_timeout;
  3749. p2p->client_timeout = client_timeout;
  3750. }
  3751. }
  3752. #ifdef CONFIG_WIFI_DISPLAY
  3753. static void p2p_update_wfd_ie_groups(struct p2p_data *p2p)
  3754. {
  3755. size_t g;
  3756. struct p2p_group *group;
  3757. for (g = 0; g < p2p->num_groups; g++) {
  3758. group = p2p->groups[g];
  3759. p2p_group_force_beacon_update_ies(group);
  3760. }
  3761. }
  3762. int p2p_set_wfd_ie_beacon(struct p2p_data *p2p, struct wpabuf *ie)
  3763. {
  3764. wpabuf_free(p2p->wfd_ie_beacon);
  3765. p2p->wfd_ie_beacon = ie;
  3766. p2p_update_wfd_ie_groups(p2p);
  3767. return 0;
  3768. }
  3769. int p2p_set_wfd_ie_probe_req(struct p2p_data *p2p, struct wpabuf *ie)
  3770. {
  3771. wpabuf_free(p2p->wfd_ie_probe_req);
  3772. p2p->wfd_ie_probe_req = ie;
  3773. return 0;
  3774. }
  3775. int p2p_set_wfd_ie_probe_resp(struct p2p_data *p2p, struct wpabuf *ie)
  3776. {
  3777. wpabuf_free(p2p->wfd_ie_probe_resp);
  3778. p2p->wfd_ie_probe_resp = ie;
  3779. p2p_update_wfd_ie_groups(p2p);
  3780. return 0;
  3781. }
  3782. int p2p_set_wfd_ie_assoc_req(struct p2p_data *p2p, struct wpabuf *ie)
  3783. {
  3784. wpabuf_free(p2p->wfd_ie_assoc_req);
  3785. p2p->wfd_ie_assoc_req = ie;
  3786. return 0;
  3787. }
  3788. int p2p_set_wfd_ie_invitation(struct p2p_data *p2p, struct wpabuf *ie)
  3789. {
  3790. wpabuf_free(p2p->wfd_ie_invitation);
  3791. p2p->wfd_ie_invitation = ie;
  3792. return 0;
  3793. }
  3794. int p2p_set_wfd_ie_prov_disc_req(struct p2p_data *p2p, struct wpabuf *ie)
  3795. {
  3796. wpabuf_free(p2p->wfd_ie_prov_disc_req);
  3797. p2p->wfd_ie_prov_disc_req = ie;
  3798. return 0;
  3799. }
  3800. int p2p_set_wfd_ie_prov_disc_resp(struct p2p_data *p2p, struct wpabuf *ie)
  3801. {
  3802. wpabuf_free(p2p->wfd_ie_prov_disc_resp);
  3803. p2p->wfd_ie_prov_disc_resp = ie;
  3804. return 0;
  3805. }
  3806. int p2p_set_wfd_ie_go_neg(struct p2p_data *p2p, struct wpabuf *ie)
  3807. {
  3808. wpabuf_free(p2p->wfd_ie_go_neg);
  3809. p2p->wfd_ie_go_neg = ie;
  3810. return 0;
  3811. }
  3812. int p2p_set_wfd_dev_info(struct p2p_data *p2p, const struct wpabuf *elem)
  3813. {
  3814. wpabuf_free(p2p->wfd_dev_info);
  3815. if (elem) {
  3816. p2p->wfd_dev_info = wpabuf_dup(elem);
  3817. if (p2p->wfd_dev_info == NULL)
  3818. return -1;
  3819. } else
  3820. p2p->wfd_dev_info = NULL;
  3821. return 0;
  3822. }
  3823. int p2p_set_wfd_assoc_bssid(struct p2p_data *p2p, const struct wpabuf *elem)
  3824. {
  3825. wpabuf_free(p2p->wfd_assoc_bssid);
  3826. if (elem) {
  3827. p2p->wfd_assoc_bssid = wpabuf_dup(elem);
  3828. if (p2p->wfd_assoc_bssid == NULL)
  3829. return -1;
  3830. } else
  3831. p2p->wfd_assoc_bssid = NULL;
  3832. return 0;
  3833. }
  3834. int p2p_set_wfd_coupled_sink_info(struct p2p_data *p2p,
  3835. const struct wpabuf *elem)
  3836. {
  3837. wpabuf_free(p2p->wfd_coupled_sink_info);
  3838. if (elem) {
  3839. p2p->wfd_coupled_sink_info = wpabuf_dup(elem);
  3840. if (p2p->wfd_coupled_sink_info == NULL)
  3841. return -1;
  3842. } else
  3843. p2p->wfd_coupled_sink_info = NULL;
  3844. return 0;
  3845. }
  3846. #endif /* CONFIG_WIFI_DISPLAY */
  3847. int p2p_set_disc_int(struct p2p_data *p2p, int min_disc_int, int max_disc_int,
  3848. int max_disc_tu)
  3849. {
  3850. if (min_disc_int > max_disc_int || min_disc_int < 0 || max_disc_int < 0)
  3851. return -1;
  3852. p2p->min_disc_int = min_disc_int;
  3853. p2p->max_disc_int = max_disc_int;
  3854. p2p->max_disc_tu = max_disc_tu;
  3855. p2p_dbg(p2p, "Set discoverable interval: min=%d max=%d max_tu=%d",
  3856. min_disc_int, max_disc_int, max_disc_tu);
  3857. return 0;
  3858. }
  3859. void p2p_dbg(struct p2p_data *p2p, const char *fmt, ...)
  3860. {
  3861. va_list ap;
  3862. char buf[500];
  3863. if (!p2p->cfg->debug_print)
  3864. return;
  3865. va_start(ap, fmt);
  3866. vsnprintf(buf, sizeof(buf), fmt, ap);
  3867. buf[sizeof(buf) - 1] = '\0';
  3868. va_end(ap);
  3869. p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_DEBUG, buf);
  3870. }
  3871. void p2p_info(struct p2p_data *p2p, const char *fmt, ...)
  3872. {
  3873. va_list ap;
  3874. char buf[500];
  3875. if (!p2p->cfg->debug_print)
  3876. return;
  3877. va_start(ap, fmt);
  3878. vsnprintf(buf, sizeof(buf), fmt, ap);
  3879. buf[sizeof(buf) - 1] = '\0';
  3880. va_end(ap);
  3881. p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_INFO, buf);
  3882. }
  3883. void p2p_err(struct p2p_data *p2p, const char *fmt, ...)
  3884. {
  3885. va_list ap;
  3886. char buf[500];
  3887. if (!p2p->cfg->debug_print)
  3888. return;
  3889. va_start(ap, fmt);
  3890. vsnprintf(buf, sizeof(buf), fmt, ap);
  3891. buf[sizeof(buf) - 1] = '\0';
  3892. va_end(ap);
  3893. p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_ERROR, buf);
  3894. }
  3895. void p2p_loop_on_known_peers(struct p2p_data *p2p,
  3896. void (*peer_callback)(struct p2p_peer_info *peer,
  3897. void *user_data),
  3898. void *user_data)
  3899. {
  3900. struct p2p_device *dev, *n;
  3901. dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) {
  3902. peer_callback(&dev->info, user_data);
  3903. }
  3904. }
  3905. #ifdef CONFIG_WPS_NFC
  3906. static struct wpabuf * p2p_build_nfc_handover(struct p2p_data *p2p,
  3907. int client_freq,
  3908. const u8 *go_dev_addr,
  3909. const u8 *ssid, size_t ssid_len)
  3910. {
  3911. struct wpabuf *buf;
  3912. u8 op_class, channel;
  3913. enum p2p_role_indication role = P2P_DEVICE_NOT_IN_GROUP;
  3914. buf = wpabuf_alloc(1000);
  3915. if (buf == NULL)
  3916. return NULL;
  3917. op_class = p2p->cfg->reg_class;
  3918. channel = p2p->cfg->channel;
  3919. p2p_buf_add_capability(buf, p2p->dev_capab &
  3920. ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
  3921. p2p_buf_add_device_info(buf, p2p, NULL);
  3922. if (p2p->num_groups > 0) {
  3923. int freq = p2p_group_get_freq(p2p->groups[0]);
  3924. role = P2P_GO_IN_A_GROUP;
  3925. if (p2p_freq_to_channel(freq, &op_class, &channel) < 0) {
  3926. p2p_dbg(p2p,
  3927. "Unknown GO operating frequency %d MHz for NFC handover",
  3928. freq);
  3929. wpabuf_free(buf);
  3930. return NULL;
  3931. }
  3932. } else if (client_freq > 0) {
  3933. role = P2P_CLIENT_IN_A_GROUP;
  3934. if (p2p_freq_to_channel(client_freq, &op_class, &channel) < 0) {
  3935. p2p_dbg(p2p,
  3936. "Unknown client operating frequency %d MHz for NFC handover",
  3937. client_freq);
  3938. wpabuf_free(buf);
  3939. return NULL;
  3940. }
  3941. }
  3942. p2p_buf_add_oob_go_neg_channel(buf, p2p->cfg->country, op_class,
  3943. channel, role);
  3944. if (p2p->num_groups > 0) {
  3945. /* Limit number of clients to avoid very long message */
  3946. p2p_buf_add_group_info(p2p->groups[0], buf, 5);
  3947. p2p_group_buf_add_id(p2p->groups[0], buf);
  3948. } else if (client_freq > 0 &&
  3949. go_dev_addr && !is_zero_ether_addr(go_dev_addr) &&
  3950. ssid && ssid_len > 0) {
  3951. /*
  3952. * Add the optional P2P Group ID to indicate in which group this
  3953. * device is a P2P Client.
  3954. */
  3955. p2p_buf_add_group_id(buf, go_dev_addr, ssid, ssid_len);
  3956. }
  3957. return buf;
  3958. }
  3959. struct wpabuf * p2p_build_nfc_handover_req(struct p2p_data *p2p,
  3960. int client_freq,
  3961. const u8 *go_dev_addr,
  3962. const u8 *ssid, size_t ssid_len)
  3963. {
  3964. return p2p_build_nfc_handover(p2p, client_freq, go_dev_addr, ssid,
  3965. ssid_len);
  3966. }
  3967. struct wpabuf * p2p_build_nfc_handover_sel(struct p2p_data *p2p,
  3968. int client_freq,
  3969. const u8 *go_dev_addr,
  3970. const u8 *ssid, size_t ssid_len)
  3971. {
  3972. return p2p_build_nfc_handover(p2p, client_freq, go_dev_addr, ssid,
  3973. ssid_len);
  3974. }
  3975. int p2p_process_nfc_connection_handover(struct p2p_data *p2p,
  3976. struct p2p_nfc_params *params)
  3977. {
  3978. struct p2p_message msg;
  3979. struct p2p_device *dev;
  3980. const u8 *p2p_dev_addr;
  3981. int freq;
  3982. enum p2p_role_indication role;
  3983. params->next_step = NO_ACTION;
  3984. if (p2p_parse_ies_separate(params->wsc_attr, params->wsc_len,
  3985. params->p2p_attr, params->p2p_len, &msg)) {
  3986. p2p_dbg(p2p, "Failed to parse WSC/P2P attributes from NFC");
  3987. p2p_parse_free(&msg);
  3988. return -1;
  3989. }
  3990. if (msg.p2p_device_addr)
  3991. p2p_dev_addr = msg.p2p_device_addr;
  3992. else if (msg.device_id)
  3993. p2p_dev_addr = msg.device_id;
  3994. else {
  3995. p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id");
  3996. p2p_parse_free(&msg);
  3997. return -1;
  3998. }
  3999. if (msg.oob_dev_password) {
  4000. os_memcpy(params->oob_dev_pw, msg.oob_dev_password,
  4001. msg.oob_dev_password_len);
  4002. params->oob_dev_pw_len = msg.oob_dev_password_len;
  4003. }
  4004. dev = p2p_create_device(p2p, p2p_dev_addr);
  4005. if (dev == NULL) {
  4006. p2p_parse_free(&msg);
  4007. return -1;
  4008. }
  4009. params->peer = &dev->info;
  4010. os_get_reltime(&dev->last_seen);
  4011. dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY);
  4012. p2p_copy_wps_info(p2p, dev, 0, &msg);
  4013. if (!msg.oob_go_neg_channel) {
  4014. p2p_dbg(p2p, "OOB GO Negotiation Channel attribute not included");
  4015. return -1;
  4016. }
  4017. if (msg.oob_go_neg_channel[3] == 0 &&
  4018. msg.oob_go_neg_channel[4] == 0)
  4019. freq = 0;
  4020. else
  4021. freq = p2p_channel_to_freq(msg.oob_go_neg_channel[3],
  4022. msg.oob_go_neg_channel[4]);
  4023. if (freq < 0) {
  4024. p2p_dbg(p2p, "Unknown peer OOB GO Neg channel");
  4025. return -1;
  4026. }
  4027. role = msg.oob_go_neg_channel[5];
  4028. if (role == P2P_GO_IN_A_GROUP) {
  4029. p2p_dbg(p2p, "Peer OOB GO operating channel: %u MHz", freq);
  4030. params->go_freq = freq;
  4031. } else if (role == P2P_CLIENT_IN_A_GROUP) {
  4032. p2p_dbg(p2p, "Peer (client) OOB GO operating channel: %u MHz",
  4033. freq);
  4034. params->go_freq = freq;
  4035. } else
  4036. p2p_dbg(p2p, "Peer OOB GO Neg channel: %u MHz", freq);
  4037. dev->oob_go_neg_freq = freq;
  4038. if (!params->sel && role != P2P_GO_IN_A_GROUP) {
  4039. freq = p2p_channel_to_freq(p2p->cfg->reg_class,
  4040. p2p->cfg->channel);
  4041. if (freq < 0) {
  4042. p2p_dbg(p2p, "Own listen channel not known");
  4043. return -1;
  4044. }
  4045. p2p_dbg(p2p, "Use own Listen channel as OOB GO Neg channel: %u MHz", freq);
  4046. dev->oob_go_neg_freq = freq;
  4047. }
  4048. if (msg.group_id) {
  4049. os_memcpy(params->go_dev_addr, msg.group_id, ETH_ALEN);
  4050. params->go_ssid_len = msg.group_id_len - ETH_ALEN;
  4051. os_memcpy(params->go_ssid, msg.group_id + ETH_ALEN,
  4052. params->go_ssid_len);
  4053. }
  4054. if (dev->flags & P2P_DEV_USER_REJECTED) {
  4055. p2p_dbg(p2p, "Do not report rejected device");
  4056. p2p_parse_free(&msg);
  4057. return 0;
  4058. }
  4059. if (!(dev->flags & P2P_DEV_REPORTED)) {
  4060. p2p->cfg->dev_found(p2p->cfg->cb_ctx, p2p_dev_addr, &dev->info,
  4061. !(dev->flags & P2P_DEV_REPORTED_ONCE));
  4062. dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
  4063. }
  4064. p2p_parse_free(&msg);
  4065. if (role == P2P_GO_IN_A_GROUP && p2p->num_groups > 0)
  4066. params->next_step = BOTH_GO;
  4067. else if (role == P2P_GO_IN_A_GROUP)
  4068. params->next_step = JOIN_GROUP;
  4069. else if (role == P2P_CLIENT_IN_A_GROUP) {
  4070. dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
  4071. params->next_step = PEER_CLIENT;
  4072. } else if (p2p->num_groups > 0)
  4073. params->next_step = AUTH_JOIN;
  4074. else if (params->sel)
  4075. params->next_step = INIT_GO_NEG;
  4076. else
  4077. params->next_step = RESP_GO_NEG;
  4078. return 0;
  4079. }
  4080. void p2p_set_authorized_oob_dev_pw_id(struct p2p_data *p2p, u16 dev_pw_id,
  4081. int go_intent,
  4082. const u8 *own_interface_addr)
  4083. {
  4084. p2p->authorized_oob_dev_pw_id = dev_pw_id;
  4085. if (dev_pw_id == 0) {
  4086. p2p_dbg(p2p, "NFC OOB Password unauthorized for static handover");
  4087. return;
  4088. }
  4089. p2p_dbg(p2p, "NFC OOB Password (id=%u) authorized for static handover",
  4090. dev_pw_id);
  4091. p2p->go_intent = go_intent;
  4092. os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
  4093. }
  4094. #endif /* CONFIG_WPS_NFC */
  4095. int p2p_set_passphrase_len(struct p2p_data *p2p, unsigned int len)
  4096. {
  4097. if (len < 8 || len > 63)
  4098. return -1;
  4099. p2p->cfg->passphrase_len = len;
  4100. return 0;
  4101. }
  4102. void p2p_set_vendor_elems(struct p2p_data *p2p, struct wpabuf **vendor_elem)
  4103. {
  4104. p2p->vendor_elem = vendor_elem;
  4105. }
  4106. void p2p_go_neg_wait_timeout(void *eloop_ctx, void *timeout_ctx)
  4107. {
  4108. struct p2p_data *p2p = eloop_ctx;
  4109. p2p_dbg(p2p,
  4110. "Timeout on waiting peer to become ready for GO Negotiation");
  4111. p2p_go_neg_failed(p2p, -1);
  4112. }