p2p.c 115 KB

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