p2p.c 101 KB

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