p2p.c 85 KB

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