p2p.c 78 KB

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