p2p.c 114 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394
  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->wps_method = WPS_NOT_READY;
  170. p2p->go_neg_peer = NULL;
  171. os_memset(&res, 0, sizeof(res));
  172. res.status = status;
  173. if (peer) {
  174. os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr,
  175. ETH_ALEN);
  176. os_memcpy(res.peer_interface_addr, peer->intended_addr,
  177. ETH_ALEN);
  178. }
  179. p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
  180. }
  181. static void p2p_listen_in_find(struct p2p_data *p2p, int dev_disc)
  182. {
  183. unsigned int r, tu;
  184. int freq;
  185. struct wpabuf *ies;
  186. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  187. "P2P: Starting short listen state (state=%s)",
  188. p2p_state_txt(p2p->state));
  189. freq = p2p_channel_to_freq(p2p->cfg->country, p2p->cfg->reg_class,
  190. p2p->cfg->channel);
  191. if (freq < 0) {
  192. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  193. "P2P: Unknown regulatory class/channel");
  194. return;
  195. }
  196. os_get_random((u8 *) &r, sizeof(r));
  197. tu = (r % ((p2p->max_disc_int - p2p->min_disc_int) + 1) +
  198. p2p->min_disc_int) * 100;
  199. if (p2p->max_disc_tu >= 0 && tu > (unsigned int) p2p->max_disc_tu)
  200. tu = p2p->max_disc_tu;
  201. if (!dev_disc && tu < 100)
  202. tu = 100; /* Need to wait in non-device discovery use cases */
  203. if (p2p->cfg->max_listen && 1024 * tu / 1000 > p2p->cfg->max_listen)
  204. tu = p2p->cfg->max_listen * 1000 / 1024;
  205. if (tu == 0) {
  206. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip listen state "
  207. "since duration was 0 TU");
  208. p2p_set_timeout(p2p, 0, 0);
  209. return;
  210. }
  211. p2p->pending_listen_freq = freq;
  212. p2p->pending_listen_sec = 0;
  213. p2p->pending_listen_usec = 1024 * tu;
  214. ies = p2p_build_probe_resp_ies(p2p);
  215. if (ies == NULL)
  216. return;
  217. if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, 1024 * tu / 1000,
  218. ies) < 0) {
  219. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  220. "P2P: Failed to start listen mode");
  221. p2p->pending_listen_freq = 0;
  222. }
  223. wpabuf_free(ies);
  224. }
  225. int p2p_listen(struct p2p_data *p2p, unsigned int timeout)
  226. {
  227. int freq;
  228. struct wpabuf *ies;
  229. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  230. "P2P: Going to listen(only) state");
  231. freq = p2p_channel_to_freq(p2p->cfg->country, p2p->cfg->reg_class,
  232. 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(p2p->cfg->country, 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. /* TODO: schedule p2p_run_after_scan to be called from TX
  809. * status callback(?) */
  810. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send pending "
  811. "Action frame at p2p_scan completion");
  812. p2p->cfg->send_action(p2p->cfg->cb_ctx,
  813. p2p->after_scan_tx->freq,
  814. p2p->after_scan_tx->dst,
  815. p2p->after_scan_tx->src,
  816. p2p->after_scan_tx->bssid,
  817. (u8 *) (p2p->after_scan_tx + 1),
  818. p2p->after_scan_tx->len,
  819. p2p->after_scan_tx->wait_time);
  820. os_free(p2p->after_scan_tx);
  821. p2p->after_scan_tx = NULL;
  822. return 1;
  823. }
  824. op = p2p->start_after_scan;
  825. p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
  826. switch (op) {
  827. case P2P_AFTER_SCAN_NOTHING:
  828. break;
  829. case P2P_AFTER_SCAN_LISTEN:
  830. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
  831. "requested Listen state");
  832. p2p_listen(p2p, p2p->pending_listen_sec * 1000 +
  833. p2p->pending_listen_usec / 1000);
  834. return 1;
  835. case P2P_AFTER_SCAN_CONNECT:
  836. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
  837. "requested connect with " MACSTR,
  838. MAC2STR(p2p->after_scan_peer));
  839. dev = p2p_get_device(p2p, p2p->after_scan_peer);
  840. if (dev == NULL) {
  841. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer not "
  842. "known anymore");
  843. break;
  844. }
  845. p2p_connect_send(p2p, dev);
  846. return 1;
  847. }
  848. return 0;
  849. }
  850. static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx)
  851. {
  852. struct p2p_data *p2p = eloop_ctx;
  853. int running;
  854. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan timeout "
  855. "(running=%d)", p2p->p2p_scan_running);
  856. running = p2p->p2p_scan_running;
  857. /* Make sure we recover from missed scan results callback */
  858. p2p->p2p_scan_running = 0;
  859. if (running)
  860. p2p_run_after_scan(p2p);
  861. }
  862. static void p2p_free_req_dev_types(struct p2p_data *p2p)
  863. {
  864. p2p->num_req_dev_types = 0;
  865. os_free(p2p->req_dev_types);
  866. p2p->req_dev_types = NULL;
  867. }
  868. int p2p_find(struct p2p_data *p2p, unsigned int timeout,
  869. enum p2p_discovery_type type,
  870. unsigned int num_req_dev_types, const u8 *req_dev_types,
  871. const u8 *dev_id, unsigned int search_delay)
  872. {
  873. int res;
  874. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting find (type=%d)",
  875. type);
  876. os_get_time(&p2p->find_start);
  877. if (p2p->p2p_scan_running) {
  878. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan is "
  879. "already running");
  880. }
  881. p2p_free_req_dev_types(p2p);
  882. if (req_dev_types && num_req_dev_types) {
  883. p2p->req_dev_types = os_malloc(num_req_dev_types *
  884. WPS_DEV_TYPE_LEN);
  885. if (p2p->req_dev_types == NULL)
  886. return -1;
  887. os_memcpy(p2p->req_dev_types, req_dev_types,
  888. num_req_dev_types * WPS_DEV_TYPE_LEN);
  889. p2p->num_req_dev_types = num_req_dev_types;
  890. }
  891. if (dev_id) {
  892. os_memcpy(p2p->find_dev_id_buf, dev_id, ETH_ALEN);
  893. p2p->find_dev_id = p2p->find_dev_id_buf;
  894. } else
  895. p2p->find_dev_id = NULL;
  896. p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
  897. p2p_clear_timeout(p2p);
  898. p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
  899. p2p->find_type = type;
  900. p2p_device_clear_reported(p2p);
  901. p2p_set_state(p2p, P2P_SEARCH);
  902. p2p->search_delay = search_delay;
  903. p2p->in_search_delay = 0;
  904. eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
  905. p2p->last_p2p_find_timeout = timeout;
  906. if (timeout)
  907. eloop_register_timeout(timeout, 0, p2p_find_timeout,
  908. p2p, NULL);
  909. switch (type) {
  910. case P2P_FIND_START_WITH_FULL:
  911. case P2P_FIND_PROGRESSIVE:
  912. res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0,
  913. p2p->num_req_dev_types,
  914. p2p->req_dev_types, dev_id,
  915. DEV_PW_DEFAULT);
  916. break;
  917. case P2P_FIND_ONLY_SOCIAL:
  918. res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0,
  919. p2p->num_req_dev_types,
  920. p2p->req_dev_types, dev_id,
  921. DEV_PW_DEFAULT);
  922. break;
  923. default:
  924. return -1;
  925. }
  926. if (res == 0) {
  927. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan");
  928. p2p->p2p_scan_running = 1;
  929. eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
  930. eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
  931. p2p, NULL);
  932. } else if (res == 1) {
  933. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Could not start "
  934. "p2p_scan at this point - will try again after "
  935. "previous scan completes");
  936. res = 0;
  937. p2p_set_state(p2p, P2P_SEARCH_WHEN_READY);
  938. eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
  939. } else {
  940. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
  941. "p2p_scan");
  942. p2p_set_state(p2p, P2P_IDLE);
  943. eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
  944. }
  945. return res;
  946. }
  947. int p2p_other_scan_completed(struct p2p_data *p2p)
  948. {
  949. if (p2p->state == P2P_CONTINUE_SEARCH_WHEN_READY) {
  950. p2p_set_state(p2p, P2P_SEARCH);
  951. p2p_search(p2p);
  952. return 1;
  953. }
  954. if (p2p->state != P2P_SEARCH_WHEN_READY)
  955. return 0;
  956. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting pending P2P find "
  957. "now that previous scan was completed");
  958. if (p2p_find(p2p, p2p->last_p2p_find_timeout, p2p->find_type,
  959. p2p->num_req_dev_types, p2p->req_dev_types,
  960. p2p->find_dev_id, p2p->search_delay) < 0) {
  961. wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, P2P_EVENT_FIND_STOPPED);
  962. return 0;
  963. }
  964. return 1;
  965. }
  966. void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq)
  967. {
  968. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Stopping find");
  969. eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
  970. p2p_clear_timeout(p2p);
  971. if (p2p->state == P2P_SEARCH ||
  972. p2p->state == P2P_CONTINUE_SEARCH_WHEN_READY ||
  973. p2p->state == P2P_SEARCH_WHEN_READY)
  974. wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, P2P_EVENT_FIND_STOPPED);
  975. p2p_set_state(p2p, P2P_IDLE);
  976. p2p_free_req_dev_types(p2p);
  977. p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
  978. p2p->go_neg_peer = NULL;
  979. p2p->sd_peer = NULL;
  980. p2p->invite_peer = NULL;
  981. p2p_stop_listen_for_freq(p2p, freq);
  982. }
  983. void p2p_stop_listen_for_freq(struct p2p_data *p2p, int freq)
  984. {
  985. if (freq > 0 && p2p->drv_in_listen == freq && p2p->in_listen) {
  986. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip stop_listen "
  987. "since we are on correct channel for response");
  988. return;
  989. }
  990. if (p2p->in_listen) {
  991. p2p->in_listen = 0;
  992. p2p_clear_timeout(p2p);
  993. }
  994. if (p2p->drv_in_listen) {
  995. /*
  996. * The driver may not deliver callback to p2p_listen_end()
  997. * when the operation gets canceled, so clear the internal
  998. * variable that is tracking driver state.
  999. */
  1000. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Clear "
  1001. "drv_in_listen (%d)", p2p->drv_in_listen);
  1002. p2p->drv_in_listen = 0;
  1003. }
  1004. p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
  1005. }
  1006. void p2p_stop_find(struct p2p_data *p2p)
  1007. {
  1008. p2p_stop_find_for_freq(p2p, 0);
  1009. }
  1010. static int p2p_prepare_channel_pref(struct p2p_data *p2p,
  1011. unsigned int force_freq,
  1012. unsigned int pref_freq)
  1013. {
  1014. u8 op_class, op_channel;
  1015. unsigned int freq = force_freq ? force_freq : pref_freq;
  1016. if (p2p_freq_to_channel(p2p->cfg->country, freq,
  1017. &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->cfg->country, p2p->best_freq_overall,
  1047. &op_class, &op_channel) == 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->cfg->country, p2p->best_freq_5,
  1055. &op_class, &op_channel) == 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->cfg->country, p2p->best_freq_24,
  1063. &op_class, &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. static 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((char *) msg->listen_channel,
  1279. msg->listen_channel[3],
  1280. msg->listen_channel[4]);
  1281. if (freq < 0) {
  1282. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  1283. "P2P: Unknown peer Listen channel: "
  1284. "country=%c%c(0x%02x) reg_class=%u channel=%u",
  1285. msg->listen_channel[0],
  1286. msg->listen_channel[1],
  1287. msg->listen_channel[2],
  1288. msg->listen_channel[3],
  1289. msg->listen_channel[4]);
  1290. } else {
  1291. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update "
  1292. "peer " MACSTR " Listen channel: %u -> %u MHz",
  1293. MAC2STR(dev->info.p2p_device_addr),
  1294. dev->listen_freq, freq);
  1295. dev->listen_freq = freq;
  1296. }
  1297. }
  1298. if (msg->wfd_subelems) {
  1299. wpabuf_free(dev->info.wfd_subelems);
  1300. dev->info.wfd_subelems = wpabuf_dup(msg->wfd_subelems);
  1301. }
  1302. if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
  1303. dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
  1304. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  1305. "P2P: Completed device entry based on data from "
  1306. "GO Negotiation Request");
  1307. } else {
  1308. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  1309. "P2P: Created device entry based on GO Neg Req: "
  1310. MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' "
  1311. "listen_freq=%d",
  1312. MAC2STR(dev->info.p2p_device_addr),
  1313. dev->info.dev_capab, dev->info.group_capab,
  1314. dev->info.device_name, dev->listen_freq);
  1315. }
  1316. dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY;
  1317. if (dev->flags & P2P_DEV_USER_REJECTED) {
  1318. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  1319. "P2P: Do not report rejected device");
  1320. return;
  1321. }
  1322. p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
  1323. !(dev->flags & P2P_DEV_REPORTED_ONCE));
  1324. dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
  1325. }
  1326. void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len)
  1327. {
  1328. os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
  1329. p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2);
  1330. os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2],
  1331. p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len);
  1332. *ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len;
  1333. }
  1334. int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params)
  1335. {
  1336. p2p_build_ssid(p2p, params->ssid, &params->ssid_len);
  1337. p2p_random(params->passphrase, 8);
  1338. return 0;
  1339. }
  1340. void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
  1341. {
  1342. struct p2p_go_neg_results res;
  1343. int go = peer->go_state == LOCAL_GO;
  1344. struct p2p_channels intersection;
  1345. int freqs;
  1346. size_t i, j;
  1347. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  1348. "P2P: GO Negotiation with " MACSTR " completed (%s will be "
  1349. "GO)", MAC2STR(peer->info.p2p_device_addr),
  1350. go ? "local end" : "peer");
  1351. os_memset(&res, 0, sizeof(res));
  1352. res.role_go = go;
  1353. os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
  1354. os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
  1355. res.wps_method = peer->wps_method;
  1356. if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
  1357. if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
  1358. res.persistent_group = 2;
  1359. else
  1360. res.persistent_group = 1;
  1361. }
  1362. if (go) {
  1363. /* Setup AP mode for WPS provisioning */
  1364. res.freq = p2p_channel_to_freq(p2p->cfg->country,
  1365. p2p->op_reg_class,
  1366. p2p->op_channel);
  1367. os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
  1368. res.ssid_len = p2p->ssid_len;
  1369. p2p_random(res.passphrase, 8);
  1370. } else {
  1371. res.freq = peer->oper_freq;
  1372. if (p2p->ssid_len) {
  1373. os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
  1374. res.ssid_len = p2p->ssid_len;
  1375. }
  1376. }
  1377. p2p_channels_intersect(&p2p->channels, &peer->channels,
  1378. &intersection);
  1379. freqs = 0;
  1380. for (i = 0; i < intersection.reg_classes; i++) {
  1381. struct p2p_reg_class *c = &intersection.reg_class[i];
  1382. if (freqs + 1 == P2P_MAX_CHANNELS)
  1383. break;
  1384. for (j = 0; j < c->channels; j++) {
  1385. int freq;
  1386. if (freqs + 1 == P2P_MAX_CHANNELS)
  1387. break;
  1388. freq = p2p_channel_to_freq(peer->country, c->reg_class,
  1389. c->channel[j]);
  1390. if (freq < 0)
  1391. continue;
  1392. res.freq_list[freqs++] = freq;
  1393. }
  1394. }
  1395. res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout;
  1396. p2p_clear_timeout(p2p);
  1397. p2p->ssid_set = 0;
  1398. peer->go_neg_req_sent = 0;
  1399. peer->wps_method = WPS_NOT_READY;
  1400. p2p_set_state(p2p, P2P_PROVISIONING);
  1401. p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
  1402. }
  1403. static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
  1404. const u8 *data, size_t len, int rx_freq)
  1405. {
  1406. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  1407. "P2P: RX P2P Public Action from " MACSTR, MAC2STR(sa));
  1408. wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len);
  1409. if (len < 1)
  1410. return;
  1411. switch (data[0]) {
  1412. case P2P_GO_NEG_REQ:
  1413. p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq);
  1414. break;
  1415. case P2P_GO_NEG_RESP:
  1416. p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq);
  1417. break;
  1418. case P2P_GO_NEG_CONF:
  1419. p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1);
  1420. break;
  1421. case P2P_INVITATION_REQ:
  1422. p2p_process_invitation_req(p2p, sa, data + 1, len - 1,
  1423. rx_freq);
  1424. break;
  1425. case P2P_INVITATION_RESP:
  1426. p2p_process_invitation_resp(p2p, sa, data + 1, len - 1);
  1427. break;
  1428. case P2P_PROV_DISC_REQ:
  1429. p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
  1430. break;
  1431. case P2P_PROV_DISC_RESP:
  1432. p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1);
  1433. break;
  1434. case P2P_DEV_DISC_REQ:
  1435. p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
  1436. break;
  1437. case P2P_DEV_DISC_RESP:
  1438. p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1);
  1439. break;
  1440. default:
  1441. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  1442. "P2P: Unsupported P2P Public Action frame type %d",
  1443. data[0]);
  1444. break;
  1445. }
  1446. }
  1447. static void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da,
  1448. const u8 *sa, const u8 *bssid, const u8 *data,
  1449. size_t len, int freq)
  1450. {
  1451. if (len < 1)
  1452. return;
  1453. switch (data[0]) {
  1454. case WLAN_PA_VENDOR_SPECIFIC:
  1455. data++;
  1456. len--;
  1457. if (len < 3)
  1458. return;
  1459. if (WPA_GET_BE24(data) != OUI_WFA)
  1460. return;
  1461. data += 3;
  1462. len -= 3;
  1463. if (len < 1)
  1464. return;
  1465. if (*data != P2P_OUI_TYPE)
  1466. return;
  1467. p2p_rx_p2p_action(p2p, sa, data + 1, len - 1, freq);
  1468. break;
  1469. case WLAN_PA_GAS_INITIAL_REQ:
  1470. p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq);
  1471. break;
  1472. case WLAN_PA_GAS_INITIAL_RESP:
  1473. p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq);
  1474. break;
  1475. case WLAN_PA_GAS_COMEBACK_REQ:
  1476. p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq);
  1477. break;
  1478. case WLAN_PA_GAS_COMEBACK_RESP:
  1479. p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq);
  1480. break;
  1481. }
  1482. }
  1483. void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
  1484. const u8 *bssid, u8 category,
  1485. const u8 *data, size_t len, int freq)
  1486. {
  1487. if (category == WLAN_ACTION_PUBLIC) {
  1488. p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq);
  1489. return;
  1490. }
  1491. if (category != WLAN_ACTION_VENDOR_SPECIFIC)
  1492. return;
  1493. if (len < 4)
  1494. return;
  1495. if (WPA_GET_BE24(data) != OUI_WFA)
  1496. return;
  1497. data += 3;
  1498. len -= 3;
  1499. if (*data != P2P_OUI_TYPE)
  1500. return;
  1501. data++;
  1502. len--;
  1503. /* P2P action frame */
  1504. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  1505. "P2P: RX P2P Action from " MACSTR, MAC2STR(sa));
  1506. wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len);
  1507. if (len < 1)
  1508. return;
  1509. switch (data[0]) {
  1510. case P2P_NOA:
  1511. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  1512. "P2P: Received P2P Action - Notice of Absence");
  1513. /* TODO */
  1514. break;
  1515. case P2P_PRESENCE_REQ:
  1516. p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq);
  1517. break;
  1518. case P2P_PRESENCE_RESP:
  1519. p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1);
  1520. break;
  1521. case P2P_GO_DISC_REQ:
  1522. p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq);
  1523. break;
  1524. default:
  1525. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  1526. "P2P: Received P2P Action - unknown type %u", data[0]);
  1527. break;
  1528. }
  1529. }
  1530. static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx)
  1531. {
  1532. struct p2p_data *p2p = eloop_ctx;
  1533. if (p2p->go_neg_peer == NULL)
  1534. return;
  1535. p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
  1536. p2p->go_neg_peer->status = P2P_SC_SUCCESS;
  1537. p2p_connect_send(p2p, p2p->go_neg_peer);
  1538. }
  1539. static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx)
  1540. {
  1541. struct p2p_data *p2p = eloop_ctx;
  1542. if (p2p->invite_peer == NULL)
  1543. return;
  1544. p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
  1545. p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr);
  1546. }
  1547. static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr,
  1548. const u8 *ie, size_t ie_len)
  1549. {
  1550. struct p2p_message msg;
  1551. struct p2p_device *dev;
  1552. os_memset(&msg, 0, sizeof(msg));
  1553. if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL)
  1554. {
  1555. p2p_parse_free(&msg);
  1556. return; /* not a P2P probe */
  1557. }
  1558. if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
  1559. os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
  1560. != 0) {
  1561. /* The Probe Request is not part of P2P Device Discovery. It is
  1562. * not known whether the source address of the frame is the P2P
  1563. * Device Address or P2P Interface Address. Do not add a new
  1564. * peer entry based on this frames.
  1565. */
  1566. p2p_parse_free(&msg);
  1567. return;
  1568. }
  1569. dev = p2p_get_device(p2p, addr);
  1570. if (dev) {
  1571. if (dev->country[0] == 0 && msg.listen_channel)
  1572. os_memcpy(dev->country, msg.listen_channel, 3);
  1573. os_get_time(&dev->last_seen);
  1574. p2p_parse_free(&msg);
  1575. return; /* already known */
  1576. }
  1577. dev = p2p_create_device(p2p, addr);
  1578. if (dev == NULL) {
  1579. p2p_parse_free(&msg);
  1580. return;
  1581. }
  1582. os_get_time(&dev->last_seen);
  1583. dev->flags |= P2P_DEV_PROBE_REQ_ONLY;
  1584. if (msg.listen_channel) {
  1585. os_memcpy(dev->country, msg.listen_channel, 3);
  1586. dev->listen_freq = p2p_channel_to_freq(dev->country,
  1587. msg.listen_channel[3],
  1588. msg.listen_channel[4]);
  1589. }
  1590. p2p_copy_wps_info(dev, 1, &msg);
  1591. if (msg.wfd_subelems) {
  1592. wpabuf_free(dev->info.wfd_subelems);
  1593. dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
  1594. }
  1595. p2p_parse_free(&msg);
  1596. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  1597. "P2P: Created device entry based on Probe Req: " MACSTR
  1598. " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
  1599. MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab,
  1600. dev->info.group_capab, dev->info.device_name,
  1601. dev->listen_freq);
  1602. }
  1603. struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p,
  1604. const u8 *addr,
  1605. struct p2p_message *msg)
  1606. {
  1607. struct p2p_device *dev;
  1608. dev = p2p_get_device(p2p, addr);
  1609. if (dev) {
  1610. os_get_time(&dev->last_seen);
  1611. return dev; /* already known */
  1612. }
  1613. dev = p2p_create_device(p2p, addr);
  1614. if (dev == NULL)
  1615. return NULL;
  1616. p2p_add_dev_info(p2p, addr, dev, msg);
  1617. return dev;
  1618. }
  1619. static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type)
  1620. {
  1621. if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0)
  1622. return 1;
  1623. if (os_memcmp(dev_type, req_dev_type, 2) == 0 &&
  1624. WPA_GET_BE32(&req_dev_type[2]) == 0 &&
  1625. WPA_GET_BE16(&req_dev_type[6]) == 0)
  1626. return 1; /* Category match with wildcard OUI/sub-category */
  1627. return 0;
  1628. }
  1629. int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[],
  1630. size_t num_req_dev_type)
  1631. {
  1632. size_t i;
  1633. for (i = 0; i < num_req_dev_type; i++) {
  1634. if (dev_type_match(dev_type, req_dev_type[i]))
  1635. return 1;
  1636. }
  1637. return 0;
  1638. }
  1639. /**
  1640. * p2p_match_dev_type - Match local device type with requested type
  1641. * @p2p: P2P module context from p2p_init()
  1642. * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
  1643. * Returns: 1 on match, 0 on mismatch
  1644. *
  1645. * This function can be used to match the Requested Device Type attribute in
  1646. * WPS IE with the local device types for deciding whether to reply to a Probe
  1647. * Request frame.
  1648. */
  1649. int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps)
  1650. {
  1651. struct wps_parse_attr attr;
  1652. size_t i;
  1653. if (wps_parse_msg(wps, &attr))
  1654. return 1; /* assume no Requested Device Type attributes */
  1655. if (attr.num_req_dev_type == 0)
  1656. return 1; /* no Requested Device Type attributes -> match */
  1657. if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type,
  1658. attr.num_req_dev_type))
  1659. return 1; /* Own Primary Device Type matches */
  1660. for (i = 0; i < p2p->cfg->num_sec_dev_types; i++)
  1661. if (dev_type_list_match(p2p->cfg->sec_dev_type[i],
  1662. attr.req_dev_type,
  1663. attr.num_req_dev_type))
  1664. return 1; /* Own Secondary Device Type matches */
  1665. /* No matching device type found */
  1666. return 0;
  1667. }
  1668. struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p)
  1669. {
  1670. struct wpabuf *buf;
  1671. u8 *len;
  1672. int pw_id = -1;
  1673. size_t extra = 0;
  1674. #ifdef CONFIG_WIFI_DISPLAY
  1675. if (p2p->wfd_ie_probe_resp)
  1676. extra = wpabuf_len(p2p->wfd_ie_probe_resp);
  1677. #endif /* CONFIG_WIFI_DISPLAY */
  1678. buf = wpabuf_alloc(1000 + extra);
  1679. if (buf == NULL)
  1680. return NULL;
  1681. if (p2p->go_neg_peer) {
  1682. /* Advertise immediate availability of WPS credential */
  1683. pw_id = p2p_wps_method_pw_id(p2p->go_neg_peer->wps_method);
  1684. }
  1685. p2p_build_wps_ie(p2p, buf, pw_id, 1);
  1686. #ifdef CONFIG_WIFI_DISPLAY
  1687. if (p2p->wfd_ie_probe_resp)
  1688. wpabuf_put_buf(buf, p2p->wfd_ie_probe_resp);
  1689. #endif /* CONFIG_WIFI_DISPLAY */
  1690. /* P2P IE */
  1691. len = p2p_buf_add_ie_hdr(buf);
  1692. p2p_buf_add_capability(buf, p2p->dev_capab &
  1693. ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
  1694. if (p2p->ext_listen_interval)
  1695. p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
  1696. p2p->ext_listen_interval);
  1697. p2p_buf_add_device_info(buf, p2p, NULL);
  1698. p2p_buf_update_ie_hdr(buf, len);
  1699. return buf;
  1700. }
  1701. static int is_11b(u8 rate)
  1702. {
  1703. return rate == 0x02 || rate == 0x04 || rate == 0x0b || rate == 0x16;
  1704. }
  1705. static int supp_rates_11b_only(struct ieee802_11_elems *elems)
  1706. {
  1707. int num_11b = 0, num_others = 0;
  1708. int i;
  1709. if (elems->supp_rates == NULL && elems->ext_supp_rates == NULL)
  1710. return 0;
  1711. for (i = 0; elems->supp_rates && i < elems->supp_rates_len; i++) {
  1712. if (is_11b(elems->supp_rates[i]))
  1713. num_11b++;
  1714. else
  1715. num_others++;
  1716. }
  1717. for (i = 0; elems->ext_supp_rates && i < elems->ext_supp_rates_len;
  1718. i++) {
  1719. if (is_11b(elems->ext_supp_rates[i]))
  1720. num_11b++;
  1721. else
  1722. num_others++;
  1723. }
  1724. return num_11b > 0 && num_others == 0;
  1725. }
  1726. static enum p2p_probe_req_status
  1727. p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
  1728. const u8 *bssid, const u8 *ie, size_t ie_len)
  1729. {
  1730. struct ieee802_11_elems elems;
  1731. struct wpabuf *buf;
  1732. struct ieee80211_mgmt *resp;
  1733. struct p2p_message msg;
  1734. struct wpabuf *ies;
  1735. if (!p2p->in_listen || !p2p->drv_in_listen) {
  1736. /* not in Listen state - ignore Probe Request */
  1737. return P2P_PREQ_NOT_LISTEN;
  1738. }
  1739. if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) ==
  1740. ParseFailed) {
  1741. /* Ignore invalid Probe Request frames */
  1742. return P2P_PREQ_MALFORMED;
  1743. }
  1744. if (elems.p2p == NULL) {
  1745. /* not a P2P probe - ignore it */
  1746. return P2P_PREQ_NOT_P2P;
  1747. }
  1748. if (dst && !is_broadcast_ether_addr(dst) &&
  1749. os_memcmp(dst, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
  1750. /* Not sent to the broadcast address or our P2P Device Address
  1751. */
  1752. return P2P_PREQ_NOT_PROCESSED;
  1753. }
  1754. if (bssid && !is_broadcast_ether_addr(bssid)) {
  1755. /* Not sent to the Wildcard BSSID */
  1756. return P2P_PREQ_NOT_PROCESSED;
  1757. }
  1758. if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN ||
  1759. os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) !=
  1760. 0) {
  1761. /* not using P2P Wildcard SSID - ignore */
  1762. return P2P_PREQ_NOT_PROCESSED;
  1763. }
  1764. if (supp_rates_11b_only(&elems)) {
  1765. /* Indicates support for 11b rates only */
  1766. return P2P_PREQ_NOT_P2P;
  1767. }
  1768. os_memset(&msg, 0, sizeof(msg));
  1769. if (p2p_parse_ies(ie, ie_len, &msg) < 0) {
  1770. /* Could not parse P2P attributes */
  1771. return P2P_PREQ_NOT_P2P;
  1772. }
  1773. if (msg.device_id &&
  1774. os_memcmp(msg.device_id, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
  1775. /* Device ID did not match */
  1776. p2p_parse_free(&msg);
  1777. return P2P_PREQ_NOT_PROCESSED;
  1778. }
  1779. /* Check Requested Device Type match */
  1780. if (msg.wps_attributes &&
  1781. !p2p_match_dev_type(p2p, msg.wps_attributes)) {
  1782. /* No match with Requested Device Type */
  1783. p2p_parse_free(&msg);
  1784. return P2P_PREQ_NOT_PROCESSED;
  1785. }
  1786. p2p_parse_free(&msg);
  1787. if (!p2p->cfg->send_probe_resp) {
  1788. /* Response generated elsewhere */
  1789. return P2P_PREQ_NOT_PROCESSED;
  1790. }
  1791. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  1792. "P2P: Reply to P2P Probe Request in Listen state");
  1793. /*
  1794. * We do not really have a specific BSS that this frame is advertising,
  1795. * so build a frame that has some information in valid format. This is
  1796. * really only used for discovery purposes, not to learn exact BSS
  1797. * parameters.
  1798. */
  1799. ies = p2p_build_probe_resp_ies(p2p);
  1800. if (ies == NULL)
  1801. return P2P_PREQ_NOT_PROCESSED;
  1802. buf = wpabuf_alloc(200 + wpabuf_len(ies));
  1803. if (buf == NULL) {
  1804. wpabuf_free(ies);
  1805. return P2P_PREQ_NOT_PROCESSED;
  1806. }
  1807. resp = NULL;
  1808. resp = wpabuf_put(buf, resp->u.probe_resp.variable - (u8 *) resp);
  1809. resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
  1810. (WLAN_FC_STYPE_PROBE_RESP << 4));
  1811. os_memcpy(resp->da, addr, ETH_ALEN);
  1812. os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN);
  1813. os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN);
  1814. resp->u.probe_resp.beacon_int = host_to_le16(100);
  1815. /* hardware or low-level driver will setup seq_ctrl and timestamp */
  1816. resp->u.probe_resp.capab_info =
  1817. host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE |
  1818. WLAN_CAPABILITY_PRIVACY |
  1819. WLAN_CAPABILITY_SHORT_SLOT_TIME);
  1820. wpabuf_put_u8(buf, WLAN_EID_SSID);
  1821. wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN);
  1822. wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
  1823. wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES);
  1824. wpabuf_put_u8(buf, 8);
  1825. wpabuf_put_u8(buf, (60 / 5) | 0x80);
  1826. wpabuf_put_u8(buf, 90 / 5);
  1827. wpabuf_put_u8(buf, (120 / 5) | 0x80);
  1828. wpabuf_put_u8(buf, 180 / 5);
  1829. wpabuf_put_u8(buf, (240 / 5) | 0x80);
  1830. wpabuf_put_u8(buf, 360 / 5);
  1831. wpabuf_put_u8(buf, 480 / 5);
  1832. wpabuf_put_u8(buf, 540 / 5);
  1833. wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS);
  1834. wpabuf_put_u8(buf, 1);
  1835. wpabuf_put_u8(buf, p2p->cfg->channel);
  1836. wpabuf_put_buf(buf, ies);
  1837. wpabuf_free(ies);
  1838. p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf);
  1839. wpabuf_free(buf);
  1840. return P2P_PREQ_NOT_PROCESSED;
  1841. }
  1842. enum p2p_probe_req_status
  1843. p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
  1844. const u8 *bssid, const u8 *ie, size_t ie_len)
  1845. {
  1846. enum p2p_probe_req_status res;
  1847. p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len);
  1848. res = p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len);
  1849. if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) &&
  1850. p2p->go_neg_peer &&
  1851. os_memcmp(addr, p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN)
  1852. == 0 &&
  1853. !(p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
  1854. /* Received a Probe Request from GO Negotiation peer */
  1855. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  1856. "P2P: Found GO Negotiation peer - try to start GO "
  1857. "negotiation from timeout");
  1858. eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
  1859. eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL);
  1860. return P2P_PREQ_PROCESSED;
  1861. }
  1862. if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) &&
  1863. p2p->invite_peer &&
  1864. os_memcmp(addr, p2p->invite_peer->info.p2p_device_addr, ETH_ALEN)
  1865. == 0) {
  1866. /* Received a Probe Request from Invite peer */
  1867. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  1868. "P2P: Found Invite peer - try to start Invite from "
  1869. "timeout");
  1870. eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL);
  1871. return P2P_PREQ_PROCESSED;
  1872. }
  1873. return res;
  1874. }
  1875. static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid,
  1876. u8 *buf, size_t len, struct wpabuf *p2p_ie)
  1877. {
  1878. struct wpabuf *tmp;
  1879. u8 *lpos;
  1880. size_t tmplen;
  1881. int res;
  1882. u8 group_capab;
  1883. if (p2p_ie == NULL)
  1884. return 0; /* WLAN AP is not a P2P manager */
  1885. /*
  1886. * (Re)Association Request - P2P IE
  1887. * P2P Capability attribute (shall be present)
  1888. * P2P Interface attribute (present if concurrent device and
  1889. * P2P Management is enabled)
  1890. */
  1891. tmp = wpabuf_alloc(200);
  1892. if (tmp == NULL)
  1893. return -1;
  1894. lpos = p2p_buf_add_ie_hdr(tmp);
  1895. group_capab = 0;
  1896. if (p2p->num_groups > 0) {
  1897. group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
  1898. if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
  1899. (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) &&
  1900. p2p->cross_connect)
  1901. group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
  1902. }
  1903. p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab);
  1904. if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
  1905. (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED))
  1906. p2p_buf_add_p2p_interface(tmp, p2p);
  1907. p2p_buf_update_ie_hdr(tmp, lpos);
  1908. tmplen = wpabuf_len(tmp);
  1909. if (tmplen > len)
  1910. res = -1;
  1911. else {
  1912. os_memcpy(buf, wpabuf_head(tmp), tmplen);
  1913. res = tmplen;
  1914. }
  1915. wpabuf_free(tmp);
  1916. return res;
  1917. }
  1918. int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf,
  1919. size_t len, int p2p_group, struct wpabuf *p2p_ie)
  1920. {
  1921. struct wpabuf *tmp;
  1922. u8 *lpos;
  1923. struct p2p_device *peer;
  1924. size_t tmplen;
  1925. int res;
  1926. size_t extra = 0;
  1927. if (!p2p_group)
  1928. return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie);
  1929. #ifdef CONFIG_WIFI_DISPLAY
  1930. if (p2p->wfd_ie_assoc_req)
  1931. extra = wpabuf_len(p2p->wfd_ie_assoc_req);
  1932. #endif /* CONFIG_WIFI_DISPLAY */
  1933. /*
  1934. * (Re)Association Request - P2P IE
  1935. * P2P Capability attribute (shall be present)
  1936. * Extended Listen Timing (may be present)
  1937. * P2P Device Info attribute (shall be present)
  1938. */
  1939. tmp = wpabuf_alloc(200 + extra);
  1940. if (tmp == NULL)
  1941. return -1;
  1942. #ifdef CONFIG_WIFI_DISPLAY
  1943. if (p2p->wfd_ie_assoc_req)
  1944. wpabuf_put_buf(tmp, p2p->wfd_ie_assoc_req);
  1945. #endif /* CONFIG_WIFI_DISPLAY */
  1946. peer = bssid ? p2p_get_device(p2p, bssid) : NULL;
  1947. lpos = p2p_buf_add_ie_hdr(tmp);
  1948. p2p_buf_add_capability(tmp, p2p->dev_capab, 0);
  1949. if (p2p->ext_listen_interval)
  1950. p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period,
  1951. p2p->ext_listen_interval);
  1952. p2p_buf_add_device_info(tmp, p2p, peer);
  1953. p2p_buf_update_ie_hdr(tmp, lpos);
  1954. tmplen = wpabuf_len(tmp);
  1955. if (tmplen > len)
  1956. res = -1;
  1957. else {
  1958. os_memcpy(buf, wpabuf_head(tmp), tmplen);
  1959. res = tmplen;
  1960. }
  1961. wpabuf_free(tmp);
  1962. return res;
  1963. }
  1964. int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end)
  1965. {
  1966. struct wpabuf *p2p_ie;
  1967. int ret;
  1968. p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE);
  1969. if (p2p_ie == NULL)
  1970. return 0;
  1971. ret = p2p_attr_text(p2p_ie, buf, end);
  1972. wpabuf_free(p2p_ie);
  1973. return ret;
  1974. }
  1975. int p2p_parse_dev_addr_in_p2p_ie(struct wpabuf *p2p_ie, u8 *dev_addr)
  1976. {
  1977. struct p2p_message msg;
  1978. os_memset(&msg, 0, sizeof(msg));
  1979. if (p2p_parse_p2p_ie(p2p_ie, &msg))
  1980. return -1;
  1981. if (msg.p2p_device_addr) {
  1982. os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN);
  1983. return 0;
  1984. } else if (msg.device_id) {
  1985. os_memcpy(dev_addr, msg.device_id, ETH_ALEN);
  1986. return 0;
  1987. }
  1988. return -1;
  1989. }
  1990. int p2p_parse_dev_addr(const u8 *ies, size_t ies_len, u8 *dev_addr)
  1991. {
  1992. struct wpabuf *p2p_ie;
  1993. int ret;
  1994. p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
  1995. P2P_IE_VENDOR_TYPE);
  1996. if (p2p_ie == NULL)
  1997. return -1;
  1998. ret = p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr);
  1999. wpabuf_free(p2p_ie);
  2000. return ret;
  2001. }
  2002. static void p2p_clear_go_neg(struct p2p_data *p2p)
  2003. {
  2004. p2p->go_neg_peer = NULL;
  2005. p2p_clear_timeout(p2p);
  2006. p2p_set_state(p2p, P2P_IDLE);
  2007. }
  2008. void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
  2009. {
  2010. if (p2p->go_neg_peer == NULL) {
  2011. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  2012. "P2P: No pending Group Formation - "
  2013. "ignore WPS registration success notification");
  2014. return; /* No pending Group Formation */
  2015. }
  2016. if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) !=
  2017. 0) {
  2018. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  2019. "P2P: Ignore WPS registration success notification "
  2020. "for " MACSTR " (GO Negotiation peer " MACSTR ")",
  2021. MAC2STR(mac_addr),
  2022. MAC2STR(p2p->go_neg_peer->intended_addr));
  2023. return; /* Ignore unexpected peer address */
  2024. }
  2025. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  2026. "P2P: Group Formation completed successfully with " MACSTR,
  2027. MAC2STR(mac_addr));
  2028. p2p_clear_go_neg(p2p);
  2029. }
  2030. void p2p_group_formation_failed(struct p2p_data *p2p)
  2031. {
  2032. if (p2p->go_neg_peer == NULL) {
  2033. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  2034. "P2P: No pending Group Formation - "
  2035. "ignore group formation failure notification");
  2036. return; /* No pending Group Formation */
  2037. }
  2038. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  2039. "P2P: Group Formation failed with " MACSTR,
  2040. MAC2STR(p2p->go_neg_peer->intended_addr));
  2041. p2p_clear_go_neg(p2p);
  2042. }
  2043. struct p2p_data * p2p_init(const struct p2p_config *cfg)
  2044. {
  2045. struct p2p_data *p2p;
  2046. if (cfg->max_peers < 1)
  2047. return NULL;
  2048. p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg));
  2049. if (p2p == NULL)
  2050. return NULL;
  2051. p2p->cfg = (struct p2p_config *) (p2p + 1);
  2052. os_memcpy(p2p->cfg, cfg, sizeof(*cfg));
  2053. if (cfg->dev_name)
  2054. p2p->cfg->dev_name = os_strdup(cfg->dev_name);
  2055. if (cfg->manufacturer)
  2056. p2p->cfg->manufacturer = os_strdup(cfg->manufacturer);
  2057. if (cfg->model_name)
  2058. p2p->cfg->model_name = os_strdup(cfg->model_name);
  2059. if (cfg->model_number)
  2060. p2p->cfg->model_number = os_strdup(cfg->model_number);
  2061. if (cfg->serial_number)
  2062. p2p->cfg->serial_number = os_strdup(cfg->serial_number);
  2063. if (cfg->pref_chan) {
  2064. p2p->cfg->pref_chan = os_malloc(cfg->num_pref_chan *
  2065. sizeof(struct p2p_channel));
  2066. if (p2p->cfg->pref_chan) {
  2067. os_memcpy(p2p->cfg->pref_chan, cfg->pref_chan,
  2068. cfg->num_pref_chan *
  2069. sizeof(struct p2p_channel));
  2070. } else
  2071. p2p->cfg->num_pref_chan = 0;
  2072. }
  2073. p2p->min_disc_int = 1;
  2074. p2p->max_disc_int = 3;
  2075. p2p->max_disc_tu = -1;
  2076. os_get_random(&p2p->next_tie_breaker, 1);
  2077. p2p->next_tie_breaker &= 0x01;
  2078. if (cfg->sd_request)
  2079. p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
  2080. p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
  2081. if (cfg->concurrent_operations)
  2082. p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER;
  2083. p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
  2084. dl_list_init(&p2p->devices);
  2085. eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
  2086. p2p_expiration_timeout, p2p, NULL);
  2087. p2p->go_timeout = 100;
  2088. p2p->client_timeout = 20;
  2089. return p2p;
  2090. }
  2091. void p2p_deinit(struct p2p_data *p2p)
  2092. {
  2093. #ifdef CONFIG_WIFI_DISPLAY
  2094. wpabuf_free(p2p->wfd_ie_beacon);
  2095. wpabuf_free(p2p->wfd_ie_probe_req);
  2096. wpabuf_free(p2p->wfd_ie_probe_resp);
  2097. wpabuf_free(p2p->wfd_ie_assoc_req);
  2098. wpabuf_free(p2p->wfd_ie_invitation);
  2099. wpabuf_free(p2p->wfd_ie_prov_disc_req);
  2100. wpabuf_free(p2p->wfd_ie_prov_disc_resp);
  2101. wpabuf_free(p2p->wfd_ie_go_neg);
  2102. wpabuf_free(p2p->wfd_dev_info);
  2103. wpabuf_free(p2p->wfd_assoc_bssid);
  2104. wpabuf_free(p2p->wfd_coupled_sink_info);
  2105. #endif /* CONFIG_WIFI_DISPLAY */
  2106. eloop_cancel_timeout(p2p_expiration_timeout, p2p, NULL);
  2107. eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
  2108. eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
  2109. eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
  2110. p2p_flush(p2p);
  2111. p2p_free_req_dev_types(p2p);
  2112. os_free(p2p->cfg->dev_name);
  2113. os_free(p2p->cfg->manufacturer);
  2114. os_free(p2p->cfg->model_name);
  2115. os_free(p2p->cfg->model_number);
  2116. os_free(p2p->cfg->serial_number);
  2117. os_free(p2p->cfg->pref_chan);
  2118. os_free(p2p->groups);
  2119. wpabuf_free(p2p->sd_resp);
  2120. os_free(p2p->after_scan_tx);
  2121. p2p_remove_wps_vendor_extensions(p2p);
  2122. os_free(p2p);
  2123. }
  2124. void p2p_flush(struct p2p_data *p2p)
  2125. {
  2126. struct p2p_device *dev, *prev;
  2127. p2p_stop_find(p2p);
  2128. dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device,
  2129. list) {
  2130. dl_list_del(&dev->list);
  2131. p2p_device_free(p2p, dev);
  2132. }
  2133. p2p_free_sd_queries(p2p);
  2134. os_free(p2p->after_scan_tx);
  2135. p2p->after_scan_tx = NULL;
  2136. }
  2137. int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr)
  2138. {
  2139. struct p2p_device *dev;
  2140. dev = p2p_get_device(p2p, addr);
  2141. if (dev == NULL)
  2142. return -1;
  2143. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Unauthorizing " MACSTR,
  2144. MAC2STR(addr));
  2145. if (p2p->go_neg_peer == dev)
  2146. p2p->go_neg_peer = NULL;
  2147. dev->wps_method = WPS_NOT_READY;
  2148. dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
  2149. dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
  2150. /* Check if after_scan_tx is for this peer. If so free it */
  2151. if (p2p->after_scan_tx &&
  2152. os_memcmp(addr, p2p->after_scan_tx->dst, ETH_ALEN) == 0) {
  2153. os_free(p2p->after_scan_tx);
  2154. p2p->after_scan_tx = NULL;
  2155. }
  2156. return 0;
  2157. }
  2158. int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name)
  2159. {
  2160. os_free(p2p->cfg->dev_name);
  2161. if (dev_name) {
  2162. p2p->cfg->dev_name = os_strdup(dev_name);
  2163. if (p2p->cfg->dev_name == NULL)
  2164. return -1;
  2165. } else
  2166. p2p->cfg->dev_name = NULL;
  2167. return 0;
  2168. }
  2169. int p2p_set_manufacturer(struct p2p_data *p2p, const char *manufacturer)
  2170. {
  2171. os_free(p2p->cfg->manufacturer);
  2172. p2p->cfg->manufacturer = NULL;
  2173. if (manufacturer) {
  2174. p2p->cfg->manufacturer = os_strdup(manufacturer);
  2175. if (p2p->cfg->manufacturer == NULL)
  2176. return -1;
  2177. }
  2178. return 0;
  2179. }
  2180. int p2p_set_model_name(struct p2p_data *p2p, const char *model_name)
  2181. {
  2182. os_free(p2p->cfg->model_name);
  2183. p2p->cfg->model_name = NULL;
  2184. if (model_name) {
  2185. p2p->cfg->model_name = os_strdup(model_name);
  2186. if (p2p->cfg->model_name == NULL)
  2187. return -1;
  2188. }
  2189. return 0;
  2190. }
  2191. int p2p_set_model_number(struct p2p_data *p2p, const char *model_number)
  2192. {
  2193. os_free(p2p->cfg->model_number);
  2194. p2p->cfg->model_number = NULL;
  2195. if (model_number) {
  2196. p2p->cfg->model_number = os_strdup(model_number);
  2197. if (p2p->cfg->model_number == NULL)
  2198. return -1;
  2199. }
  2200. return 0;
  2201. }
  2202. int p2p_set_serial_number(struct p2p_data *p2p, const char *serial_number)
  2203. {
  2204. os_free(p2p->cfg->serial_number);
  2205. p2p->cfg->serial_number = NULL;
  2206. if (serial_number) {
  2207. p2p->cfg->serial_number = os_strdup(serial_number);
  2208. if (p2p->cfg->serial_number == NULL)
  2209. return -1;
  2210. }
  2211. return 0;
  2212. }
  2213. void p2p_set_config_methods(struct p2p_data *p2p, u16 config_methods)
  2214. {
  2215. p2p->cfg->config_methods = config_methods;
  2216. }
  2217. void p2p_set_uuid(struct p2p_data *p2p, const u8 *uuid)
  2218. {
  2219. os_memcpy(p2p->cfg->uuid, uuid, 16);
  2220. }
  2221. int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type)
  2222. {
  2223. os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8);
  2224. return 0;
  2225. }
  2226. int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8],
  2227. size_t num_dev_types)
  2228. {
  2229. if (num_dev_types > P2P_SEC_DEVICE_TYPES)
  2230. num_dev_types = P2P_SEC_DEVICE_TYPES;
  2231. p2p->cfg->num_sec_dev_types = num_dev_types;
  2232. os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8);
  2233. return 0;
  2234. }
  2235. void p2p_remove_wps_vendor_extensions(struct p2p_data *p2p)
  2236. {
  2237. int i;
  2238. for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
  2239. wpabuf_free(p2p->wps_vendor_ext[i]);
  2240. p2p->wps_vendor_ext[i] = NULL;
  2241. }
  2242. }
  2243. int p2p_add_wps_vendor_extension(struct p2p_data *p2p,
  2244. const struct wpabuf *vendor_ext)
  2245. {
  2246. int i;
  2247. if (vendor_ext == NULL)
  2248. return -1;
  2249. for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
  2250. if (p2p->wps_vendor_ext[i] == NULL)
  2251. break;
  2252. }
  2253. if (i >= P2P_MAX_WPS_VENDOR_EXT)
  2254. return -1;
  2255. p2p->wps_vendor_ext[i] = wpabuf_dup(vendor_ext);
  2256. if (p2p->wps_vendor_ext[i] == NULL)
  2257. return -1;
  2258. return 0;
  2259. }
  2260. int p2p_set_country(struct p2p_data *p2p, const char *country)
  2261. {
  2262. os_memcpy(p2p->cfg->country, country, 3);
  2263. return 0;
  2264. }
  2265. void p2p_continue_find(struct p2p_data *p2p)
  2266. {
  2267. struct p2p_device *dev;
  2268. p2p_set_state(p2p, P2P_SEARCH);
  2269. dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
  2270. if (dev->flags & P2P_DEV_SD_SCHEDULE) {
  2271. if (p2p_start_sd(p2p, dev) == 0)
  2272. return;
  2273. else
  2274. break;
  2275. } else if (dev->req_config_methods &&
  2276. !(dev->flags & P2P_DEV_PD_FOR_JOIN)) {
  2277. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send "
  2278. "pending Provision Discovery Request to "
  2279. MACSTR " (config methods 0x%x)",
  2280. MAC2STR(dev->info.p2p_device_addr),
  2281. dev->req_config_methods);
  2282. if (p2p_send_prov_disc_req(p2p, dev, 0, 0) == 0)
  2283. return;
  2284. }
  2285. }
  2286. p2p_listen_in_find(p2p, 1);
  2287. }
  2288. static void p2p_sd_cb(struct p2p_data *p2p, int success)
  2289. {
  2290. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  2291. "P2P: Service Discovery Query TX callback: success=%d",
  2292. success);
  2293. p2p->pending_action_state = P2P_NO_PENDING_ACTION;
  2294. if (!success) {
  2295. if (p2p->sd_peer) {
  2296. p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
  2297. p2p->sd_peer = NULL;
  2298. }
  2299. p2p_continue_find(p2p);
  2300. return;
  2301. }
  2302. if (p2p->sd_peer == NULL) {
  2303. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  2304. "P2P: No SD peer entry known");
  2305. p2p_continue_find(p2p);
  2306. return;
  2307. }
  2308. /* Wait for response from the peer */
  2309. p2p_set_state(p2p, P2P_SD_DURING_FIND);
  2310. p2p_set_timeout(p2p, 0, 200000);
  2311. }
  2312. /**
  2313. * p2p_retry_pd - Retry any pending provision disc requests in IDLE state
  2314. * @p2p: P2P module context from p2p_init()
  2315. */
  2316. static void p2p_retry_pd(struct p2p_data *p2p)
  2317. {
  2318. struct p2p_device *dev;
  2319. if (p2p->state != P2P_IDLE)
  2320. return;
  2321. /*
  2322. * Retry the prov disc req attempt only for the peer that the user had
  2323. * requested.
  2324. */
  2325. dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
  2326. if (os_memcmp(p2p->pending_pd_devaddr,
  2327. dev->info.p2p_device_addr, ETH_ALEN) != 0)
  2328. continue;
  2329. if (!dev->req_config_methods)
  2330. continue;
  2331. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send "
  2332. "pending Provision Discovery Request to "
  2333. MACSTR " (config methods 0x%x)",
  2334. MAC2STR(dev->info.p2p_device_addr),
  2335. dev->req_config_methods);
  2336. p2p_send_prov_disc_req(p2p, dev,
  2337. dev->flags & P2P_DEV_PD_FOR_JOIN, 0);
  2338. return;
  2339. }
  2340. }
  2341. static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
  2342. {
  2343. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  2344. "P2P: Provision Discovery Request TX callback: success=%d",
  2345. success);
  2346. /*
  2347. * Postpone resetting the pending action state till after we actually
  2348. * time out. This allows us to take some action like notifying any
  2349. * interested parties about no response to the request.
  2350. *
  2351. * When the timer (below) goes off we check in IDLE, SEARCH, or
  2352. * LISTEN_ONLY state, which are the only allowed states to issue a PD
  2353. * requests in, if this was still pending and then raise notification.
  2354. */
  2355. if (!success) {
  2356. p2p->pending_action_state = P2P_NO_PENDING_ACTION;
  2357. if (p2p->user_initiated_pd &&
  2358. (p2p->state == P2P_SEARCH || p2p->state == P2P_LISTEN_ONLY))
  2359. {
  2360. /* Retry request from timeout to avoid busy loops */
  2361. p2p->pending_action_state = P2P_PENDING_PD;
  2362. p2p_set_timeout(p2p, 0, 50000);
  2363. } else if (p2p->state != P2P_IDLE)
  2364. p2p_continue_find(p2p);
  2365. else if (p2p->user_initiated_pd) {
  2366. p2p->pending_action_state = P2P_PENDING_PD;
  2367. p2p_set_timeout(p2p, 0, 300000);
  2368. }
  2369. return;
  2370. }
  2371. /*
  2372. * This postponing, of resetting pending_action_state, needs to be
  2373. * done only for user initiated PD requests and not internal ones.
  2374. */
  2375. if (p2p->user_initiated_pd)
  2376. p2p->pending_action_state = P2P_PENDING_PD;
  2377. else
  2378. p2p->pending_action_state = P2P_NO_PENDING_ACTION;
  2379. /* Wait for response from the peer */
  2380. if (p2p->state == P2P_SEARCH)
  2381. p2p_set_state(p2p, P2P_PD_DURING_FIND);
  2382. p2p_set_timeout(p2p, 0, 200000);
  2383. }
  2384. int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
  2385. struct os_time *rx_time, int level, const u8 *ies,
  2386. size_t ies_len)
  2387. {
  2388. if (os_time_before(rx_time, &p2p->find_start)) {
  2389. /*
  2390. * The driver may have cached (e.g., in cfg80211 BSS table) the
  2391. * scan results for relatively long time. To avoid reporting
  2392. * stale information, update P2P peers only based on results
  2393. * that have based on frames received after the last p2p_find
  2394. * operation was started.
  2395. */
  2396. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Ignore old scan "
  2397. "result for " MACSTR " (rx_time=%u.%06u)",
  2398. MAC2STR(bssid), (unsigned int) rx_time->sec,
  2399. (unsigned int) rx_time->usec);
  2400. return 0;
  2401. }
  2402. p2p_add_device(p2p, bssid, freq, rx_time, level, ies, ies_len, 1);
  2403. return 0;
  2404. }
  2405. void p2p_scan_res_handled(struct p2p_data *p2p)
  2406. {
  2407. if (!p2p->p2p_scan_running) {
  2408. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan was not "
  2409. "running, but scan results received");
  2410. }
  2411. p2p->p2p_scan_running = 0;
  2412. eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
  2413. if (p2p_run_after_scan(p2p))
  2414. return;
  2415. if (p2p->state == P2P_SEARCH)
  2416. p2p_continue_find(p2p);
  2417. }
  2418. void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies, const u8 *dev_id)
  2419. {
  2420. u8 *len;
  2421. #ifdef CONFIG_WIFI_DISPLAY
  2422. if (p2p->wfd_ie_probe_req)
  2423. wpabuf_put_buf(ies, p2p->wfd_ie_probe_req);
  2424. #endif /* CONFIG_WIFI_DISPLAY */
  2425. len = p2p_buf_add_ie_hdr(ies);
  2426. p2p_buf_add_capability(ies, p2p->dev_capab &
  2427. ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
  2428. if (dev_id)
  2429. p2p_buf_add_device_id(ies, dev_id);
  2430. if (p2p->cfg->reg_class && p2p->cfg->channel)
  2431. p2p_buf_add_listen_channel(ies, p2p->cfg->country,
  2432. p2p->cfg->reg_class,
  2433. p2p->cfg->channel);
  2434. if (p2p->ext_listen_interval)
  2435. p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period,
  2436. p2p->ext_listen_interval);
  2437. /* TODO: p2p_buf_add_operating_channel() if GO */
  2438. p2p_buf_update_ie_hdr(ies, len);
  2439. }
  2440. size_t p2p_scan_ie_buf_len(struct p2p_data *p2p)
  2441. {
  2442. size_t len = 100;
  2443. #ifdef CONFIG_WIFI_DISPLAY
  2444. if (p2p && p2p->wfd_ie_probe_req)
  2445. len += wpabuf_len(p2p->wfd_ie_probe_req);
  2446. #endif /* CONFIG_WIFI_DISPLAY */
  2447. return len;
  2448. }
  2449. int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end)
  2450. {
  2451. return p2p_attr_text(p2p_ie, buf, end);
  2452. }
  2453. static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success)
  2454. {
  2455. struct p2p_device *dev = p2p->go_neg_peer;
  2456. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  2457. "P2P: GO Negotiation Request TX callback: success=%d",
  2458. success);
  2459. if (dev == NULL) {
  2460. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  2461. "P2P: No pending GO Negotiation");
  2462. return;
  2463. }
  2464. if (success) {
  2465. if (dev->flags & P2P_DEV_USER_REJECTED) {
  2466. p2p_set_state(p2p, P2P_IDLE);
  2467. return;
  2468. }
  2469. } else if (dev->go_neg_req_sent) {
  2470. /* Cancel the increment from p2p_connect_send() on failure */
  2471. dev->go_neg_req_sent--;
  2472. }
  2473. if (!success &&
  2474. (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) &&
  2475. !is_zero_ether_addr(dev->member_in_go_dev)) {
  2476. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  2477. "P2P: Peer " MACSTR " did not acknowledge request - "
  2478. "try to use device discoverability through its GO",
  2479. MAC2STR(dev->info.p2p_device_addr));
  2480. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  2481. p2p_send_dev_disc_req(p2p, dev);
  2482. return;
  2483. }
  2484. /*
  2485. * Use P2P find, if needed, to find the other device from its listen
  2486. * channel.
  2487. */
  2488. p2p_set_state(p2p, P2P_CONNECT);
  2489. p2p_set_timeout(p2p, 0, success ? 200000 : 100000);
  2490. }
  2491. static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success)
  2492. {
  2493. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  2494. "P2P: GO Negotiation Response TX callback: success=%d",
  2495. success);
  2496. if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) {
  2497. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  2498. "P2P: Ignore TX callback event - GO Negotiation is "
  2499. "not running anymore");
  2500. return;
  2501. }
  2502. p2p_set_state(p2p, P2P_CONNECT);
  2503. p2p_set_timeout(p2p, 0, 250000);
  2504. }
  2505. static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success)
  2506. {
  2507. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  2508. "P2P: GO Negotiation Response (failure) TX callback: "
  2509. "success=%d", success);
  2510. if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) {
  2511. p2p_go_neg_failed(p2p, p2p->go_neg_peer,
  2512. p2p->go_neg_peer->status);
  2513. }
  2514. }
  2515. static void p2p_go_neg_conf_cb(struct p2p_data *p2p,
  2516. enum p2p_send_action_result result)
  2517. {
  2518. struct p2p_device *dev;
  2519. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  2520. "P2P: GO Negotiation Confirm TX callback: result=%d",
  2521. result);
  2522. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  2523. if (result == P2P_SEND_ACTION_FAILED) {
  2524. p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
  2525. return;
  2526. }
  2527. if (result == P2P_SEND_ACTION_NO_ACK) {
  2528. /*
  2529. * It looks like the TX status for GO Negotiation Confirm is
  2530. * often showing failure even when the peer has actually
  2531. * received the frame. Since the peer may change channels
  2532. * immediately after having received the frame, we may not see
  2533. * an Ack for retries, so just dropping a single frame may
  2534. * trigger this. To allow the group formation to succeed if the
  2535. * peer did indeed receive the frame, continue regardless of
  2536. * the TX status.
  2537. */
  2538. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  2539. "P2P: Assume GO Negotiation Confirm TX was actually "
  2540. "received by the peer even though Ack was not "
  2541. "reported");
  2542. }
  2543. dev = p2p->go_neg_peer;
  2544. if (dev == NULL)
  2545. return;
  2546. p2p_go_complete(p2p, dev);
  2547. }
  2548. void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
  2549. const u8 *src, const u8 *bssid,
  2550. enum p2p_send_action_result result)
  2551. {
  2552. enum p2p_pending_action_state state;
  2553. int success;
  2554. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  2555. "P2P: Action frame TX callback (state=%d freq=%u dst=" MACSTR
  2556. " src=" MACSTR " bssid=" MACSTR " result=%d",
  2557. p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src),
  2558. MAC2STR(bssid), result);
  2559. success = result == P2P_SEND_ACTION_SUCCESS;
  2560. state = p2p->pending_action_state;
  2561. p2p->pending_action_state = P2P_NO_PENDING_ACTION;
  2562. switch (state) {
  2563. case P2P_NO_PENDING_ACTION:
  2564. break;
  2565. case P2P_PENDING_GO_NEG_REQUEST:
  2566. p2p_go_neg_req_cb(p2p, success);
  2567. break;
  2568. case P2P_PENDING_GO_NEG_RESPONSE:
  2569. p2p_go_neg_resp_cb(p2p, success);
  2570. break;
  2571. case P2P_PENDING_GO_NEG_RESPONSE_FAILURE:
  2572. p2p_go_neg_resp_failure_cb(p2p, success);
  2573. break;
  2574. case P2P_PENDING_GO_NEG_CONFIRM:
  2575. p2p_go_neg_conf_cb(p2p, result);
  2576. break;
  2577. case P2P_PENDING_SD:
  2578. p2p_sd_cb(p2p, success);
  2579. break;
  2580. case P2P_PENDING_PD:
  2581. p2p_prov_disc_cb(p2p, success);
  2582. break;
  2583. case P2P_PENDING_INVITATION_REQUEST:
  2584. p2p_invitation_req_cb(p2p, success);
  2585. break;
  2586. case P2P_PENDING_INVITATION_RESPONSE:
  2587. p2p_invitation_resp_cb(p2p, success);
  2588. break;
  2589. case P2P_PENDING_DEV_DISC_REQUEST:
  2590. p2p_dev_disc_req_cb(p2p, success);
  2591. break;
  2592. case P2P_PENDING_DEV_DISC_RESPONSE:
  2593. p2p_dev_disc_resp_cb(p2p, success);
  2594. break;
  2595. case P2P_PENDING_GO_DISC_REQ:
  2596. p2p_go_disc_req_cb(p2p, success);
  2597. break;
  2598. }
  2599. }
  2600. void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
  2601. unsigned int duration)
  2602. {
  2603. if (freq == p2p->pending_client_disc_freq) {
  2604. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  2605. "P2P: Client discoverability remain-awake completed");
  2606. p2p->pending_client_disc_freq = 0;
  2607. return;
  2608. }
  2609. if (freq != p2p->pending_listen_freq) {
  2610. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  2611. "P2P: Unexpected listen callback for freq=%u "
  2612. "duration=%u (pending_listen_freq=%u)",
  2613. freq, duration, p2p->pending_listen_freq);
  2614. return;
  2615. }
  2616. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  2617. "P2P: Starting Listen timeout(%u,%u) on freq=%u based on "
  2618. "callback",
  2619. p2p->pending_listen_sec, p2p->pending_listen_usec,
  2620. p2p->pending_listen_freq);
  2621. p2p->in_listen = 1;
  2622. p2p->drv_in_listen = freq;
  2623. if (p2p->pending_listen_sec || p2p->pending_listen_usec) {
  2624. /*
  2625. * Add 20 msec extra wait to avoid race condition with driver
  2626. * remain-on-channel end event, i.e., give driver more time to
  2627. * complete the operation before our timeout expires.
  2628. */
  2629. p2p_set_timeout(p2p, p2p->pending_listen_sec,
  2630. p2p->pending_listen_usec + 20000);
  2631. }
  2632. p2p->pending_listen_freq = 0;
  2633. }
  2634. int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
  2635. {
  2636. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver ended Listen "
  2637. "state (freq=%u)", freq);
  2638. p2p->drv_in_listen = 0;
  2639. if (p2p->in_listen)
  2640. return 0; /* Internal timeout will trigger the next step */
  2641. if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) {
  2642. if (p2p->go_neg_peer->connect_reqs >= 120) {
  2643. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  2644. "P2P: Timeout on sending GO Negotiation "
  2645. "Request without getting response");
  2646. p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
  2647. return 0;
  2648. }
  2649. p2p_set_state(p2p, P2P_CONNECT);
  2650. p2p_connect_send(p2p, p2p->go_neg_peer);
  2651. return 1;
  2652. } else if (p2p->state == P2P_SEARCH) {
  2653. if (p2p->p2p_scan_running) {
  2654. /*
  2655. * Search is already in progress. This can happen if
  2656. * an Action frame RX is reported immediately after
  2657. * the end of a remain-on-channel operation and the
  2658. * response frame to that is sent using an offchannel
  2659. * operation while in p2p_find. Avoid an attempt to
  2660. * restart a scan here.
  2661. */
  2662. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan "
  2663. "already in progress - do not try to start a "
  2664. "new one");
  2665. return 1;
  2666. }
  2667. if (p2p->pending_listen_freq) {
  2668. /*
  2669. * Better wait a bit if the driver is unable to start
  2670. * offchannel operation for some reason. p2p_search()
  2671. * will be started from internal timeout.
  2672. */
  2673. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Listen "
  2674. "operation did not seem to start - delay "
  2675. "search phase to avoid busy loop");
  2676. p2p_set_timeout(p2p, 0, 100000);
  2677. return 1;
  2678. }
  2679. if (p2p->search_delay) {
  2680. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay "
  2681. "search operation by %u ms",
  2682. p2p->search_delay);
  2683. p2p_set_timeout(p2p, p2p->search_delay / 1000,
  2684. (p2p->search_delay % 1000) * 1000);
  2685. return 1;
  2686. }
  2687. p2p_search(p2p);
  2688. return 1;
  2689. }
  2690. return 0;
  2691. }
  2692. static void p2p_timeout_connect(struct p2p_data *p2p)
  2693. {
  2694. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  2695. if (p2p->go_neg_peer &&
  2696. (p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
  2697. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Wait for GO "
  2698. "Negotiation Confirm timed out - assume GO "
  2699. "Negotiation failed");
  2700. p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
  2701. return;
  2702. }
  2703. p2p_set_state(p2p, P2P_CONNECT_LISTEN);
  2704. p2p_listen_in_find(p2p, 0);
  2705. }
  2706. static void p2p_timeout_connect_listen(struct p2p_data *p2p)
  2707. {
  2708. if (p2p->go_neg_peer) {
  2709. if (p2p->drv_in_listen) {
  2710. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is "
  2711. "still in Listen state; wait for it to "
  2712. "complete");
  2713. return;
  2714. }
  2715. if (p2p->go_neg_peer->connect_reqs >= 120) {
  2716. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  2717. "P2P: Timeout on sending GO Negotiation "
  2718. "Request without getting response");
  2719. p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
  2720. return;
  2721. }
  2722. p2p_set_state(p2p, P2P_CONNECT);
  2723. p2p_connect_send(p2p, p2p->go_neg_peer);
  2724. } else
  2725. p2p_set_state(p2p, P2P_IDLE);
  2726. }
  2727. static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p)
  2728. {
  2729. /*
  2730. * TODO: could remain constantly in Listen state for some time if there
  2731. * are no other concurrent uses for the radio. For now, go to listen
  2732. * state once per second to give other uses a chance to use the radio.
  2733. */
  2734. p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
  2735. p2p_set_timeout(p2p, 0, 500000);
  2736. }
  2737. static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p)
  2738. {
  2739. struct p2p_device *dev = p2p->go_neg_peer;
  2740. if (dev == NULL) {
  2741. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  2742. "P2P: Unknown GO Neg peer - stop GO Neg wait");
  2743. return;
  2744. }
  2745. dev->wait_count++;
  2746. if (dev->wait_count >= 120) {
  2747. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  2748. "P2P: Timeout on waiting peer to become ready for GO "
  2749. "Negotiation");
  2750. p2p_go_neg_failed(p2p, dev, -1);
  2751. return;
  2752. }
  2753. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  2754. "P2P: Go to Listen state while waiting for the peer to become "
  2755. "ready for GO Negotiation");
  2756. p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
  2757. p2p_listen_in_find(p2p, 0);
  2758. }
  2759. static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
  2760. {
  2761. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  2762. "P2P: Service Discovery Query timeout");
  2763. if (p2p->sd_peer) {
  2764. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  2765. p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
  2766. p2p->sd_peer = NULL;
  2767. }
  2768. p2p_continue_find(p2p);
  2769. }
  2770. static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p)
  2771. {
  2772. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  2773. "P2P: Provision Discovery Request timeout");
  2774. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  2775. p2p_continue_find(p2p);
  2776. }
  2777. static void p2p_timeout_prov_disc_req(struct p2p_data *p2p)
  2778. {
  2779. p2p->pending_action_state = P2P_NO_PENDING_ACTION;
  2780. /*
  2781. * For user initiated PD requests that we have not gotten any responses
  2782. * for while in IDLE state, we retry them a couple of times before
  2783. * giving up.
  2784. */
  2785. if (!p2p->user_initiated_pd)
  2786. return;
  2787. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  2788. "P2P: User initiated Provision Discovery Request timeout");
  2789. if (p2p->pd_retries) {
  2790. p2p->pd_retries--;
  2791. p2p_retry_pd(p2p);
  2792. } else {
  2793. struct p2p_device *dev;
  2794. int for_join = 0;
  2795. dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
  2796. if (os_memcmp(p2p->pending_pd_devaddr,
  2797. dev->info.p2p_device_addr, ETH_ALEN) != 0)
  2798. continue;
  2799. if (dev->req_config_methods &&
  2800. (dev->flags & P2P_DEV_PD_FOR_JOIN))
  2801. for_join = 1;
  2802. }
  2803. if (p2p->cfg->prov_disc_fail)
  2804. p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx,
  2805. p2p->pending_pd_devaddr,
  2806. for_join ?
  2807. P2P_PROV_DISC_TIMEOUT_JOIN :
  2808. P2P_PROV_DISC_TIMEOUT);
  2809. p2p_reset_pending_pd(p2p);
  2810. }
  2811. }
  2812. static void p2p_timeout_invite(struct p2p_data *p2p)
  2813. {
  2814. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  2815. p2p_set_state(p2p, P2P_INVITE_LISTEN);
  2816. if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
  2817. /*
  2818. * Better remain on operating channel instead of listen channel
  2819. * when running a group.
  2820. */
  2821. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Inviting in "
  2822. "active GO role - wait on operating channel");
  2823. p2p_set_timeout(p2p, 0, 100000);
  2824. return;
  2825. }
  2826. p2p_listen_in_find(p2p, 0);
  2827. }
  2828. static void p2p_timeout_invite_listen(struct p2p_data *p2p)
  2829. {
  2830. if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) {
  2831. p2p_set_state(p2p, P2P_INVITE);
  2832. p2p_invite_send(p2p, p2p->invite_peer,
  2833. p2p->invite_go_dev_addr);
  2834. } else {
  2835. if (p2p->invite_peer) {
  2836. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  2837. "P2P: Invitation Request retry limit reached");
  2838. if (p2p->cfg->invitation_result)
  2839. p2p->cfg->invitation_result(
  2840. p2p->cfg->cb_ctx, -1, NULL);
  2841. }
  2842. p2p_set_state(p2p, P2P_IDLE);
  2843. }
  2844. }
  2845. static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
  2846. {
  2847. struct p2p_data *p2p = eloop_ctx;
  2848. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Timeout (state=%s)",
  2849. p2p_state_txt(p2p->state));
  2850. p2p->in_listen = 0;
  2851. switch (p2p->state) {
  2852. case P2P_IDLE:
  2853. /* Check if we timed out waiting for PD req */
  2854. if (p2p->pending_action_state == P2P_PENDING_PD)
  2855. p2p_timeout_prov_disc_req(p2p);
  2856. break;
  2857. case P2P_SEARCH:
  2858. /* Check if we timed out waiting for PD req */
  2859. if (p2p->pending_action_state == P2P_PENDING_PD)
  2860. p2p_timeout_prov_disc_req(p2p);
  2861. if (p2p->search_delay && !p2p->in_search_delay) {
  2862. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay "
  2863. "search operation by %u ms",
  2864. p2p->search_delay);
  2865. p2p->in_search_delay = 1;
  2866. p2p_set_timeout(p2p, p2p->search_delay / 1000,
  2867. (p2p->search_delay % 1000) * 1000);
  2868. break;
  2869. }
  2870. p2p->in_search_delay = 0;
  2871. p2p_search(p2p);
  2872. break;
  2873. case P2P_CONNECT:
  2874. p2p_timeout_connect(p2p);
  2875. break;
  2876. case P2P_CONNECT_LISTEN:
  2877. p2p_timeout_connect_listen(p2p);
  2878. break;
  2879. case P2P_GO_NEG:
  2880. break;
  2881. case P2P_LISTEN_ONLY:
  2882. /* Check if we timed out waiting for PD req */
  2883. if (p2p->pending_action_state == P2P_PENDING_PD)
  2884. p2p_timeout_prov_disc_req(p2p);
  2885. if (p2p->ext_listen_only) {
  2886. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  2887. "P2P: Extended Listen Timing - Listen State "
  2888. "completed");
  2889. p2p->ext_listen_only = 0;
  2890. p2p_set_state(p2p, P2P_IDLE);
  2891. }
  2892. break;
  2893. case P2P_WAIT_PEER_CONNECT:
  2894. p2p_timeout_wait_peer_connect(p2p);
  2895. break;
  2896. case P2P_WAIT_PEER_IDLE:
  2897. p2p_timeout_wait_peer_idle(p2p);
  2898. break;
  2899. case P2P_SD_DURING_FIND:
  2900. p2p_timeout_sd_during_find(p2p);
  2901. break;
  2902. case P2P_PROVISIONING:
  2903. break;
  2904. case P2P_PD_DURING_FIND:
  2905. p2p_timeout_prov_disc_during_find(p2p);
  2906. break;
  2907. case P2P_INVITE:
  2908. p2p_timeout_invite(p2p);
  2909. break;
  2910. case P2P_INVITE_LISTEN:
  2911. p2p_timeout_invite_listen(p2p);
  2912. break;
  2913. case P2P_SEARCH_WHEN_READY:
  2914. break;
  2915. case P2P_CONTINUE_SEARCH_WHEN_READY:
  2916. break;
  2917. }
  2918. }
  2919. int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr)
  2920. {
  2921. struct p2p_device *dev;
  2922. dev = p2p_get_device(p2p, peer_addr);
  2923. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Local request to reject "
  2924. "connection attempts by peer " MACSTR, MAC2STR(peer_addr));
  2925. if (dev == NULL) {
  2926. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
  2927. " unknown", MAC2STR(peer_addr));
  2928. return -1;
  2929. }
  2930. dev->status = P2P_SC_FAIL_REJECTED_BY_USER;
  2931. dev->flags |= P2P_DEV_USER_REJECTED;
  2932. return 0;
  2933. }
  2934. const char * p2p_wps_method_text(enum p2p_wps_method method)
  2935. {
  2936. switch (method) {
  2937. case WPS_NOT_READY:
  2938. return "not-ready";
  2939. case WPS_PIN_DISPLAY:
  2940. return "Display";
  2941. case WPS_PIN_KEYPAD:
  2942. return "Keypad";
  2943. case WPS_PBC:
  2944. return "PBC";
  2945. }
  2946. return "??";
  2947. }
  2948. static const char * p2p_go_state_text(enum p2p_go_state go_state)
  2949. {
  2950. switch (go_state) {
  2951. case UNKNOWN_GO:
  2952. return "unknown";
  2953. case LOCAL_GO:
  2954. return "local";
  2955. case REMOTE_GO:
  2956. return "remote";
  2957. }
  2958. return "??";
  2959. }
  2960. const struct p2p_peer_info * p2p_get_peer_info(struct p2p_data *p2p,
  2961. const u8 *addr, int next)
  2962. {
  2963. struct p2p_device *dev;
  2964. if (addr)
  2965. dev = p2p_get_device(p2p, addr);
  2966. else
  2967. dev = dl_list_first(&p2p->devices, struct p2p_device, list);
  2968. if (dev && next) {
  2969. dev = dl_list_first(&dev->list, struct p2p_device, list);
  2970. if (&dev->list == &p2p->devices)
  2971. dev = NULL;
  2972. }
  2973. if (dev == NULL)
  2974. return NULL;
  2975. return &dev->info;
  2976. }
  2977. int p2p_get_peer_info_txt(const struct p2p_peer_info *info,
  2978. char *buf, size_t buflen)
  2979. {
  2980. struct p2p_device *dev;
  2981. int res;
  2982. char *pos, *end;
  2983. struct os_time now;
  2984. if (info == NULL)
  2985. return -1;
  2986. dev = (struct p2p_device *) (((u8 *) info) -
  2987. offsetof(struct p2p_device, info));
  2988. pos = buf;
  2989. end = buf + buflen;
  2990. os_get_time(&now);
  2991. res = os_snprintf(pos, end - pos,
  2992. "age=%d\n"
  2993. "listen_freq=%d\n"
  2994. "wps_method=%s\n"
  2995. "interface_addr=" MACSTR "\n"
  2996. "member_in_go_dev=" MACSTR "\n"
  2997. "member_in_go_iface=" MACSTR "\n"
  2998. "go_neg_req_sent=%d\n"
  2999. "go_state=%s\n"
  3000. "dialog_token=%u\n"
  3001. "intended_addr=" MACSTR "\n"
  3002. "country=%c%c\n"
  3003. "oper_freq=%d\n"
  3004. "req_config_methods=0x%x\n"
  3005. "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
  3006. "status=%d\n"
  3007. "wait_count=%u\n"
  3008. "invitation_reqs=%u\n",
  3009. (int) (now.sec - dev->last_seen.sec),
  3010. dev->listen_freq,
  3011. p2p_wps_method_text(dev->wps_method),
  3012. MAC2STR(dev->interface_addr),
  3013. MAC2STR(dev->member_in_go_dev),
  3014. MAC2STR(dev->member_in_go_iface),
  3015. dev->go_neg_req_sent,
  3016. p2p_go_state_text(dev->go_state),
  3017. dev->dialog_token,
  3018. MAC2STR(dev->intended_addr),
  3019. dev->country[0] ? dev->country[0] : '_',
  3020. dev->country[1] ? dev->country[1] : '_',
  3021. dev->oper_freq,
  3022. dev->req_config_methods,
  3023. dev->flags & P2P_DEV_PROBE_REQ_ONLY ?
  3024. "[PROBE_REQ_ONLY]" : "",
  3025. dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "",
  3026. dev->flags & P2P_DEV_NOT_YET_READY ?
  3027. "[NOT_YET_READY]" : "",
  3028. dev->flags & P2P_DEV_SD_INFO ? "[SD_INFO]" : "",
  3029. dev->flags & P2P_DEV_SD_SCHEDULE ? "[SD_SCHEDULE]" :
  3030. "",
  3031. dev->flags & P2P_DEV_PD_PEER_DISPLAY ?
  3032. "[PD_PEER_DISPLAY]" : "",
  3033. dev->flags & P2P_DEV_PD_PEER_KEYPAD ?
  3034. "[PD_PEER_KEYPAD]" : "",
  3035. dev->flags & P2P_DEV_USER_REJECTED ?
  3036. "[USER_REJECTED]" : "",
  3037. dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ?
  3038. "[PEER_WAITING_RESPONSE]" : "",
  3039. dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ?
  3040. "[PREFER_PERSISTENT_GROUP]" : "",
  3041. dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ?
  3042. "[WAIT_GO_NEG_RESPONSE]" : "",
  3043. dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ?
  3044. "[WAIT_GO_NEG_CONFIRM]" : "",
  3045. dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ?
  3046. "[GROUP_CLIENT_ONLY]" : "",
  3047. dev->flags & P2P_DEV_FORCE_FREQ ?
  3048. "[FORCE_FREQ]" : "",
  3049. dev->flags & P2P_DEV_PD_FOR_JOIN ?
  3050. "[PD_FOR_JOIN]" : "",
  3051. dev->status,
  3052. dev->wait_count,
  3053. dev->invitation_reqs);
  3054. if (res < 0 || res >= end - pos)
  3055. return pos - buf;
  3056. pos += res;
  3057. if (dev->ext_listen_period) {
  3058. res = os_snprintf(pos, end - pos,
  3059. "ext_listen_period=%u\n"
  3060. "ext_listen_interval=%u\n",
  3061. dev->ext_listen_period,
  3062. dev->ext_listen_interval);
  3063. if (res < 0 || res >= end - pos)
  3064. return pos - buf;
  3065. pos += res;
  3066. }
  3067. if (dev->oper_ssid_len) {
  3068. res = os_snprintf(pos, end - pos,
  3069. "oper_ssid=%s\n",
  3070. wpa_ssid_txt(dev->oper_ssid,
  3071. dev->oper_ssid_len));
  3072. if (res < 0 || res >= end - pos)
  3073. return pos - buf;
  3074. pos += res;
  3075. }
  3076. #ifdef CONFIG_WIFI_DISPLAY
  3077. if (dev->info.wfd_subelems) {
  3078. res = os_snprintf(pos, end - pos, "wfd_subelems=");
  3079. if (res < 0 || res >= end - pos)
  3080. return pos - buf;
  3081. pos += res;
  3082. pos += wpa_snprintf_hex(pos, end - pos,
  3083. wpabuf_head(dev->info.wfd_subelems),
  3084. wpabuf_len(dev->info.wfd_subelems));
  3085. res = os_snprintf(pos, end - pos, "\n");
  3086. if (res < 0 || res >= end - pos)
  3087. return pos - buf;
  3088. pos += res;
  3089. }
  3090. #endif /* CONFIG_WIFI_DISPLAY */
  3091. return pos - buf;
  3092. }
  3093. int p2p_peer_known(struct p2p_data *p2p, const u8 *addr)
  3094. {
  3095. return p2p_get_device(p2p, addr) != NULL;
  3096. }
  3097. void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled)
  3098. {
  3099. if (enabled) {
  3100. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
  3101. "discoverability enabled");
  3102. p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
  3103. } else {
  3104. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
  3105. "discoverability disabled");
  3106. p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
  3107. }
  3108. }
  3109. static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1,
  3110. u32 duration2, u32 interval2)
  3111. {
  3112. struct wpabuf *req;
  3113. struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL;
  3114. u8 *len;
  3115. req = wpabuf_alloc(100);
  3116. if (req == NULL)
  3117. return NULL;
  3118. if (duration1 || interval1) {
  3119. os_memset(&desc1, 0, sizeof(desc1));
  3120. desc1.count_type = 1;
  3121. desc1.duration = duration1;
  3122. desc1.interval = interval1;
  3123. ptr1 = &desc1;
  3124. if (duration2 || interval2) {
  3125. os_memset(&desc2, 0, sizeof(desc2));
  3126. desc2.count_type = 2;
  3127. desc2.duration = duration2;
  3128. desc2.interval = interval2;
  3129. ptr2 = &desc2;
  3130. }
  3131. }
  3132. p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1);
  3133. len = p2p_buf_add_ie_hdr(req);
  3134. p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2);
  3135. p2p_buf_update_ie_hdr(req, len);
  3136. return req;
  3137. }
  3138. int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
  3139. const u8 *own_interface_addr, unsigned int freq,
  3140. u32 duration1, u32 interval1, u32 duration2,
  3141. u32 interval2)
  3142. {
  3143. struct wpabuf *req;
  3144. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send Presence Request to "
  3145. "GO " MACSTR " (own interface " MACSTR ") freq=%u dur1=%u "
  3146. "int1=%u dur2=%u int2=%u",
  3147. MAC2STR(go_interface_addr), MAC2STR(own_interface_addr),
  3148. freq, duration1, interval1, duration2, interval2);
  3149. req = p2p_build_presence_req(duration1, interval1, duration2,
  3150. interval2);
  3151. if (req == NULL)
  3152. return -1;
  3153. p2p->pending_action_state = P2P_NO_PENDING_ACTION;
  3154. if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr,
  3155. go_interface_addr,
  3156. wpabuf_head(req), wpabuf_len(req), 200) < 0) {
  3157. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  3158. "P2P: Failed to send Action frame");
  3159. }
  3160. wpabuf_free(req);
  3161. return 0;
  3162. }
  3163. static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa,
  3164. size_t noa_len, u8 dialog_token)
  3165. {
  3166. struct wpabuf *resp;
  3167. u8 *len;
  3168. resp = wpabuf_alloc(100 + noa_len);
  3169. if (resp == NULL)
  3170. return NULL;
  3171. p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token);
  3172. len = p2p_buf_add_ie_hdr(resp);
  3173. p2p_buf_add_status(resp, status);
  3174. if (noa) {
  3175. wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE);
  3176. wpabuf_put_le16(resp, noa_len);
  3177. wpabuf_put_data(resp, noa, noa_len);
  3178. } else
  3179. p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL);
  3180. p2p_buf_update_ie_hdr(resp, len);
  3181. return resp;
  3182. }
  3183. static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
  3184. const u8 *sa, const u8 *data, size_t len,
  3185. int rx_freq)
  3186. {
  3187. struct p2p_message msg;
  3188. u8 status;
  3189. struct wpabuf *resp;
  3190. size_t g;
  3191. struct p2p_group *group = NULL;
  3192. int parsed = 0;
  3193. u8 noa[50];
  3194. int noa_len;
  3195. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  3196. "P2P: Received P2P Action - P2P Presence Request");
  3197. for (g = 0; g < p2p->num_groups; g++) {
  3198. if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]),
  3199. ETH_ALEN) == 0) {
  3200. group = p2p->groups[g];
  3201. break;
  3202. }
  3203. }
  3204. if (group == NULL) {
  3205. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  3206. "P2P: Ignore P2P Presence Request for unknown group "
  3207. MACSTR, MAC2STR(da));
  3208. return;
  3209. }
  3210. if (p2p_parse(data, len, &msg) < 0) {
  3211. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  3212. "P2P: Failed to parse P2P Presence Request");
  3213. status = P2P_SC_FAIL_INVALID_PARAMS;
  3214. goto fail;
  3215. }
  3216. parsed = 1;
  3217. if (msg.noa == NULL) {
  3218. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  3219. "P2P: No NoA attribute in P2P Presence Request");
  3220. status = P2P_SC_FAIL_INVALID_PARAMS;
  3221. goto fail;
  3222. }
  3223. status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len);
  3224. fail:
  3225. if (p2p->cfg->get_noa)
  3226. noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa,
  3227. sizeof(noa));
  3228. else
  3229. noa_len = -1;
  3230. resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL,
  3231. noa_len > 0 ? noa_len : 0,
  3232. msg.dialog_token);
  3233. if (parsed)
  3234. p2p_parse_free(&msg);
  3235. if (resp == NULL)
  3236. return;
  3237. p2p->pending_action_state = P2P_NO_PENDING_ACTION;
  3238. if (p2p_send_action(p2p, rx_freq, sa, da, da,
  3239. wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
  3240. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  3241. "P2P: Failed to send Action frame");
  3242. }
  3243. wpabuf_free(resp);
  3244. }
  3245. static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
  3246. const u8 *sa, const u8 *data, size_t len)
  3247. {
  3248. struct p2p_message msg;
  3249. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  3250. "P2P: Received P2P Action - P2P Presence Response");
  3251. if (p2p_parse(data, len, &msg) < 0) {
  3252. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  3253. "P2P: Failed to parse P2P Presence Response");
  3254. return;
  3255. }
  3256. if (msg.status == NULL || msg.noa == NULL) {
  3257. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  3258. "P2P: No Status or NoA attribute in P2P Presence "
  3259. "Response");
  3260. p2p_parse_free(&msg);
  3261. return;
  3262. }
  3263. if (*msg.status) {
  3264. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  3265. "P2P: P2P Presence Request was rejected: status %u",
  3266. *msg.status);
  3267. p2p_parse_free(&msg);
  3268. return;
  3269. }
  3270. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  3271. "P2P: P2P Presence Request was accepted");
  3272. wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA",
  3273. msg.noa, msg.noa_len);
  3274. /* TODO: process NoA */
  3275. p2p_parse_free(&msg);
  3276. }
  3277. static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx)
  3278. {
  3279. struct p2p_data *p2p = eloop_ctx;
  3280. if (p2p->ext_listen_interval) {
  3281. /* Schedule next extended listen timeout */
  3282. eloop_register_timeout(p2p->ext_listen_interval_sec,
  3283. p2p->ext_listen_interval_usec,
  3284. p2p_ext_listen_timeout, p2p, NULL);
  3285. }
  3286. if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) {
  3287. /*
  3288. * This should not really happen, but it looks like the Listen
  3289. * command may fail is something else (e.g., a scan) was
  3290. * running at an inconvenient time. As a workaround, allow new
  3291. * Extended Listen operation to be started.
  3292. */
  3293. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Previous "
  3294. "Extended Listen operation had not been completed - "
  3295. "try again");
  3296. p2p->ext_listen_only = 0;
  3297. p2p_set_state(p2p, P2P_IDLE);
  3298. }
  3299. if (p2p->state != P2P_IDLE) {
  3300. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip Extended "
  3301. "Listen timeout in active state (%s)",
  3302. p2p_state_txt(p2p->state));
  3303. return;
  3304. }
  3305. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Extended Listen timeout");
  3306. p2p->ext_listen_only = 1;
  3307. if (p2p_listen(p2p, p2p->ext_listen_period) < 0) {
  3308. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
  3309. "Listen state for Extended Listen Timing");
  3310. p2p->ext_listen_only = 0;
  3311. }
  3312. }
  3313. int p2p_ext_listen(struct p2p_data *p2p, unsigned int period,
  3314. unsigned int interval)
  3315. {
  3316. if (period > 65535 || interval > 65535 || period > interval ||
  3317. (period == 0 && interval > 0) || (period > 0 && interval == 0)) {
  3318. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  3319. "P2P: Invalid Extended Listen Timing request: "
  3320. "period=%u interval=%u", period, interval);
  3321. return -1;
  3322. }
  3323. eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
  3324. if (interval == 0) {
  3325. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  3326. "P2P: Disabling Extended Listen Timing");
  3327. p2p->ext_listen_period = 0;
  3328. p2p->ext_listen_interval = 0;
  3329. return 0;
  3330. }
  3331. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  3332. "P2P: Enabling Extended Listen Timing: period %u msec, "
  3333. "interval %u msec", period, interval);
  3334. p2p->ext_listen_period = period;
  3335. p2p->ext_listen_interval = interval;
  3336. p2p->ext_listen_interval_sec = interval / 1000;
  3337. p2p->ext_listen_interval_usec = (interval % 1000) * 1000;
  3338. eloop_register_timeout(p2p->ext_listen_interval_sec,
  3339. p2p->ext_listen_interval_usec,
  3340. p2p_ext_listen_timeout, p2p, NULL);
  3341. return 0;
  3342. }
  3343. void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
  3344. const u8 *ie, size_t ie_len)
  3345. {
  3346. struct p2p_message msg;
  3347. if (bssid == NULL || ie == NULL)
  3348. return;
  3349. os_memset(&msg, 0, sizeof(msg));
  3350. if (p2p_parse_ies(ie, ie_len, &msg))
  3351. return;
  3352. if (msg.minor_reason_code == NULL)
  3353. return;
  3354. wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
  3355. "P2P: Deauthentication notification BSSID " MACSTR
  3356. " reason_code=%u minor_reason_code=%u",
  3357. MAC2STR(bssid), reason_code, *msg.minor_reason_code);
  3358. p2p_parse_free(&msg);
  3359. }
  3360. void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
  3361. const u8 *ie, size_t ie_len)
  3362. {
  3363. struct p2p_message msg;
  3364. if (bssid == NULL || ie == NULL)
  3365. return;
  3366. os_memset(&msg, 0, sizeof(msg));
  3367. if (p2p_parse_ies(ie, ie_len, &msg))
  3368. return;
  3369. if (msg.minor_reason_code == NULL)
  3370. return;
  3371. wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
  3372. "P2P: Disassociation notification BSSID " MACSTR
  3373. " reason_code=%u minor_reason_code=%u",
  3374. MAC2STR(bssid), reason_code, *msg.minor_reason_code);
  3375. p2p_parse_free(&msg);
  3376. }
  3377. void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
  3378. {
  3379. if (enabled) {
  3380. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
  3381. "Device operations enabled");
  3382. p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED;
  3383. } else {
  3384. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
  3385. "Device operations disabled");
  3386. p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED;
  3387. }
  3388. }
  3389. int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel)
  3390. {
  3391. if (p2p_channel_to_freq(p2p->cfg->country, reg_class, channel) < 0)
  3392. return -1;
  3393. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Set Listen channel: "
  3394. "reg_class %u channel %u", reg_class, channel);
  3395. p2p->cfg->reg_class = reg_class;
  3396. p2p->cfg->channel = channel;
  3397. return 0;
  3398. }
  3399. int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
  3400. {
  3401. wpa_hexdump_ascii(MSG_DEBUG, "P2P: New SSID postfix", postfix, len);
  3402. if (postfix == NULL) {
  3403. p2p->cfg->ssid_postfix_len = 0;
  3404. return 0;
  3405. }
  3406. if (len > sizeof(p2p->cfg->ssid_postfix))
  3407. return -1;
  3408. os_memcpy(p2p->cfg->ssid_postfix, postfix, len);
  3409. p2p->cfg->ssid_postfix_len = len;
  3410. return 0;
  3411. }
  3412. int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel,
  3413. int cfg_op_channel)
  3414. {
  3415. if (p2p_channel_to_freq(p2p->cfg->country, op_reg_class, op_channel)
  3416. < 0)
  3417. return -1;
  3418. wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, "P2P: Set Operating channel: "
  3419. "reg_class %u channel %u", op_reg_class, op_channel);
  3420. p2p->cfg->op_reg_class = op_reg_class;
  3421. p2p->cfg->op_channel = op_channel;
  3422. p2p->cfg->cfg_op_channel = cfg_op_channel;
  3423. return 0;
  3424. }
  3425. int p2p_set_pref_chan(struct p2p_data *p2p, unsigned int num_pref_chan,
  3426. const struct p2p_channel *pref_chan)
  3427. {
  3428. struct p2p_channel *n;
  3429. if (pref_chan) {
  3430. n = os_malloc(num_pref_chan * sizeof(struct p2p_channel));
  3431. if (n == NULL)
  3432. return -1;
  3433. os_memcpy(n, pref_chan,
  3434. num_pref_chan * sizeof(struct p2p_channel));
  3435. } else
  3436. n = NULL;
  3437. os_free(p2p->cfg->pref_chan);
  3438. p2p->cfg->pref_chan = n;
  3439. p2p->cfg->num_pref_chan = num_pref_chan;
  3440. return 0;
  3441. }
  3442. int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr,
  3443. u8 *iface_addr)
  3444. {
  3445. struct p2p_device *dev = p2p_get_device(p2p, dev_addr);
  3446. if (dev == NULL || is_zero_ether_addr(dev->interface_addr))
  3447. return -1;
  3448. os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN);
  3449. return 0;
  3450. }
  3451. int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr,
  3452. u8 *dev_addr)
  3453. {
  3454. struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
  3455. if (dev == NULL)
  3456. return -1;
  3457. os_memcpy(dev_addr, dev->info.p2p_device_addr, ETH_ALEN);
  3458. return 0;
  3459. }
  3460. void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr)
  3461. {
  3462. os_memcpy(p2p->peer_filter, addr, ETH_ALEN);
  3463. if (is_zero_ether_addr(p2p->peer_filter))
  3464. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Disable peer "
  3465. "filter");
  3466. else
  3467. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Enable peer "
  3468. "filter for " MACSTR, MAC2STR(p2p->peer_filter));
  3469. }
  3470. void p2p_set_cross_connect(struct p2p_data *p2p, int enabled)
  3471. {
  3472. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Cross connection %s",
  3473. enabled ? "enabled" : "disabled");
  3474. if (p2p->cross_connect == enabled)
  3475. return;
  3476. p2p->cross_connect = enabled;
  3477. /* TODO: may need to tear down any action group where we are GO(?) */
  3478. }
  3479. int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr)
  3480. {
  3481. struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
  3482. if (dev == NULL)
  3483. return -1;
  3484. if (dev->oper_freq <= 0)
  3485. return -1;
  3486. return dev->oper_freq;
  3487. }
  3488. void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled)
  3489. {
  3490. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Intra BSS distribution %s",
  3491. enabled ? "enabled" : "disabled");
  3492. p2p->cfg->p2p_intra_bss = enabled;
  3493. }
  3494. void p2p_update_channel_list(struct p2p_data *p2p, struct p2p_channels *chan)
  3495. {
  3496. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update channel list");
  3497. os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels));
  3498. }
  3499. int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
  3500. const u8 *src, const u8 *bssid, const u8 *buf,
  3501. size_t len, unsigned int wait_time)
  3502. {
  3503. if (p2p->p2p_scan_running) {
  3504. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay Action "
  3505. "frame TX until p2p_scan completes");
  3506. if (p2p->after_scan_tx) {
  3507. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped "
  3508. "previous pending Action frame TX");
  3509. os_free(p2p->after_scan_tx);
  3510. }
  3511. p2p->after_scan_tx = os_malloc(sizeof(*p2p->after_scan_tx) +
  3512. len);
  3513. if (p2p->after_scan_tx == NULL)
  3514. return -1;
  3515. p2p->after_scan_tx->freq = freq;
  3516. os_memcpy(p2p->after_scan_tx->dst, dst, ETH_ALEN);
  3517. os_memcpy(p2p->after_scan_tx->src, src, ETH_ALEN);
  3518. os_memcpy(p2p->after_scan_tx->bssid, bssid, ETH_ALEN);
  3519. p2p->after_scan_tx->len = len;
  3520. p2p->after_scan_tx->wait_time = wait_time;
  3521. os_memcpy(p2p->after_scan_tx + 1, buf, len);
  3522. return 0;
  3523. }
  3524. return p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid,
  3525. buf, len, wait_time);
  3526. }
  3527. void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5,
  3528. int freq_overall)
  3529. {
  3530. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Best channel: 2.4 GHz: %d,"
  3531. " 5 GHz: %d, overall: %d", freq_24, freq_5, freq_overall);
  3532. p2p->best_freq_24 = freq_24;
  3533. p2p->best_freq_5 = freq_5;
  3534. p2p->best_freq_overall = freq_overall;
  3535. }
  3536. const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p)
  3537. {
  3538. if (p2p == NULL || p2p->go_neg_peer == NULL)
  3539. return NULL;
  3540. return p2p->go_neg_peer->info.p2p_device_addr;
  3541. }
  3542. const struct p2p_peer_info *
  3543. p2p_get_peer_found(struct p2p_data *p2p, const u8 *addr, int next)
  3544. {
  3545. struct p2p_device *dev;
  3546. if (addr) {
  3547. dev = p2p_get_device(p2p, addr);
  3548. if (!dev)
  3549. return NULL;
  3550. if (!next) {
  3551. if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
  3552. return NULL;
  3553. return &dev->info;
  3554. } else {
  3555. do {
  3556. dev = dl_list_first(&dev->list,
  3557. struct p2p_device,
  3558. list);
  3559. if (&dev->list == &p2p->devices)
  3560. return NULL;
  3561. } while (dev->flags & P2P_DEV_PROBE_REQ_ONLY);
  3562. }
  3563. } else {
  3564. dev = dl_list_first(&p2p->devices, struct p2p_device, list);
  3565. if (!dev)
  3566. return NULL;
  3567. while (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
  3568. dev = dl_list_first(&dev->list,
  3569. struct p2p_device,
  3570. list);
  3571. if (&dev->list == &p2p->devices)
  3572. return NULL;
  3573. }
  3574. }
  3575. return &dev->info;
  3576. }
  3577. int p2p_in_progress(struct p2p_data *p2p)
  3578. {
  3579. if (p2p == NULL)
  3580. return 0;
  3581. if (p2p->state == P2P_SEARCH || p2p->state == P2P_SEARCH_WHEN_READY ||
  3582. p2p->state == P2P_CONTINUE_SEARCH_WHEN_READY)
  3583. return 2;
  3584. return p2p->state != P2P_IDLE && p2p->state != P2P_PROVISIONING;
  3585. }
  3586. void p2p_set_config_timeout(struct p2p_data *p2p, u8 go_timeout,
  3587. u8 client_timeout)
  3588. {
  3589. if (p2p) {
  3590. p2p->go_timeout = go_timeout;
  3591. p2p->client_timeout = client_timeout;
  3592. }
  3593. }
  3594. void p2p_increase_search_delay(struct p2p_data *p2p, unsigned int delay)
  3595. {
  3596. if (p2p && p2p->search_delay < delay)
  3597. p2p->search_delay = delay;
  3598. }
  3599. #ifdef CONFIG_WIFI_DISPLAY
  3600. static void p2p_update_wfd_ie_groups(struct p2p_data *p2p)
  3601. {
  3602. size_t g;
  3603. struct p2p_group *group;
  3604. for (g = 0; g < p2p->num_groups; g++) {
  3605. group = p2p->groups[g];
  3606. p2p_group_update_ies(group);
  3607. }
  3608. }
  3609. int p2p_set_wfd_ie_beacon(struct p2p_data *p2p, struct wpabuf *ie)
  3610. {
  3611. wpabuf_free(p2p->wfd_ie_beacon);
  3612. p2p->wfd_ie_beacon = ie;
  3613. p2p_update_wfd_ie_groups(p2p);
  3614. return 0;
  3615. }
  3616. int p2p_set_wfd_ie_probe_req(struct p2p_data *p2p, struct wpabuf *ie)
  3617. {
  3618. wpabuf_free(p2p->wfd_ie_probe_req);
  3619. p2p->wfd_ie_probe_req = ie;
  3620. return 0;
  3621. }
  3622. int p2p_set_wfd_ie_probe_resp(struct p2p_data *p2p, struct wpabuf *ie)
  3623. {
  3624. wpabuf_free(p2p->wfd_ie_probe_resp);
  3625. p2p->wfd_ie_probe_resp = ie;
  3626. p2p_update_wfd_ie_groups(p2p);
  3627. return 0;
  3628. }
  3629. int p2p_set_wfd_ie_assoc_req(struct p2p_data *p2p, struct wpabuf *ie)
  3630. {
  3631. wpabuf_free(p2p->wfd_ie_assoc_req);
  3632. p2p->wfd_ie_assoc_req = ie;
  3633. return 0;
  3634. }
  3635. int p2p_set_wfd_ie_invitation(struct p2p_data *p2p, struct wpabuf *ie)
  3636. {
  3637. wpabuf_free(p2p->wfd_ie_invitation);
  3638. p2p->wfd_ie_invitation = ie;
  3639. return 0;
  3640. }
  3641. int p2p_set_wfd_ie_prov_disc_req(struct p2p_data *p2p, struct wpabuf *ie)
  3642. {
  3643. wpabuf_free(p2p->wfd_ie_prov_disc_req);
  3644. p2p->wfd_ie_prov_disc_req = ie;
  3645. return 0;
  3646. }
  3647. int p2p_set_wfd_ie_prov_disc_resp(struct p2p_data *p2p, struct wpabuf *ie)
  3648. {
  3649. wpabuf_free(p2p->wfd_ie_prov_disc_resp);
  3650. p2p->wfd_ie_prov_disc_resp = ie;
  3651. return 0;
  3652. }
  3653. int p2p_set_wfd_ie_go_neg(struct p2p_data *p2p, struct wpabuf *ie)
  3654. {
  3655. wpabuf_free(p2p->wfd_ie_go_neg);
  3656. p2p->wfd_ie_go_neg = ie;
  3657. return 0;
  3658. }
  3659. int p2p_set_wfd_dev_info(struct p2p_data *p2p, const struct wpabuf *elem)
  3660. {
  3661. wpabuf_free(p2p->wfd_dev_info);
  3662. if (elem) {
  3663. p2p->wfd_dev_info = wpabuf_dup(elem);
  3664. if (p2p->wfd_dev_info == NULL)
  3665. return -1;
  3666. } else
  3667. p2p->wfd_dev_info = NULL;
  3668. return 0;
  3669. }
  3670. int p2p_set_wfd_assoc_bssid(struct p2p_data *p2p, const struct wpabuf *elem)
  3671. {
  3672. wpabuf_free(p2p->wfd_assoc_bssid);
  3673. if (elem) {
  3674. p2p->wfd_assoc_bssid = wpabuf_dup(elem);
  3675. if (p2p->wfd_assoc_bssid == NULL)
  3676. return -1;
  3677. } else
  3678. p2p->wfd_assoc_bssid = NULL;
  3679. return 0;
  3680. }
  3681. int p2p_set_wfd_coupled_sink_info(struct p2p_data *p2p,
  3682. const struct wpabuf *elem)
  3683. {
  3684. wpabuf_free(p2p->wfd_coupled_sink_info);
  3685. if (elem) {
  3686. p2p->wfd_coupled_sink_info = wpabuf_dup(elem);
  3687. if (p2p->wfd_coupled_sink_info == NULL)
  3688. return -1;
  3689. } else
  3690. p2p->wfd_coupled_sink_info = NULL;
  3691. return 0;
  3692. }
  3693. #endif /* CONFIG_WIFI_DISPLAY */
  3694. int p2p_set_disc_int(struct p2p_data *p2p, int min_disc_int, int max_disc_int,
  3695. int max_disc_tu)
  3696. {
  3697. if (min_disc_int > max_disc_int || min_disc_int < 0 || max_disc_int < 0)
  3698. return -1;
  3699. p2p->min_disc_int = min_disc_int;
  3700. p2p->max_disc_int = max_disc_int;
  3701. p2p->max_disc_tu = max_disc_tu;
  3702. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Set discoverable interval: "
  3703. "min=%d max=%d max_tu=%d", min_disc_int, max_disc_int,
  3704. max_disc_tu);
  3705. return 0;
  3706. }