p2p.c 76 KB

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