p2p.c 94 KB

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