p2p.c 99 KB

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