p2p_supplicant.c 113 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150
  1. /*
  2. * wpa_supplicant - P2P
  3. * Copyright (c) 2009-2010, Atheros Communications
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "eloop.h"
  17. #include "common/ieee802_11_common.h"
  18. #include "common/ieee802_11_defs.h"
  19. #include "common/wpa_ctrl.h"
  20. #include "wps/wps_i.h"
  21. #include "p2p/p2p.h"
  22. #include "ap/hostapd.h"
  23. #include "ap/p2p_hostapd.h"
  24. #include "eapol_supp/eapol_supp_sm.h"
  25. #include "rsn_supp/wpa.h"
  26. #include "wpa_supplicant_i.h"
  27. #include "driver_i.h"
  28. #include "ap.h"
  29. #include "config_ssid.h"
  30. #include "config.h"
  31. #include "notify.h"
  32. #include "scan.h"
  33. #include "bss.h"
  34. #include "offchannel.h"
  35. #include "wps_supplicant.h"
  36. #include "p2p_supplicant.h"
  37. /*
  38. * How many times to try to scan to find the GO before giving up on join
  39. * request.
  40. */
  41. #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
  42. static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
  43. static struct wpa_supplicant *
  44. wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
  45. int go);
  46. static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
  47. static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
  48. static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
  49. const u8 *dev_addr, enum p2p_wps_method wps_method);
  50. static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
  51. static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
  52. static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
  53. static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
  54. static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
  55. struct wpa_scan_results *scan_res)
  56. {
  57. size_t i;
  58. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  59. return;
  60. wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
  61. (int) scan_res->num);
  62. for (i = 0; i < scan_res->num; i++) {
  63. struct wpa_scan_res *bss = scan_res->res[i];
  64. if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
  65. bss->freq, bss->level,
  66. (const u8 *) (bss + 1),
  67. bss->ie_len) > 0)
  68. break;
  69. }
  70. p2p_scan_res_handled(wpa_s->global->p2p);
  71. }
  72. static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
  73. unsigned int num_req_dev_types,
  74. const u8 *req_dev_types)
  75. {
  76. struct wpa_supplicant *wpa_s = ctx;
  77. struct wpa_driver_scan_params params;
  78. int ret;
  79. struct wpabuf *wps_ie, *ies;
  80. int social_channels[] = { 2412, 2437, 2462, 0, 0 };
  81. size_t ielen;
  82. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  83. return -1;
  84. os_memset(&params, 0, sizeof(params));
  85. /* P2P Wildcard SSID */
  86. params.num_ssids = 1;
  87. params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
  88. params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
  89. wpa_s->wps->dev.p2p = 1;
  90. wps_ie = wps_build_probe_req_ie(0, &wpa_s->wps->dev, wpa_s->wps->uuid,
  91. WPS_REQ_ENROLLEE,
  92. num_req_dev_types, req_dev_types);
  93. if (wps_ie == NULL)
  94. return -1;
  95. ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
  96. ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
  97. if (ies == NULL) {
  98. wpabuf_free(wps_ie);
  99. return -1;
  100. }
  101. wpabuf_put_buf(ies, wps_ie);
  102. wpabuf_free(wps_ie);
  103. p2p_scan_ie(wpa_s->global->p2p, ies);
  104. params.p2p_probe = 1;
  105. params.extra_ies = wpabuf_head(ies);
  106. params.extra_ies_len = wpabuf_len(ies);
  107. switch (type) {
  108. case P2P_SCAN_SOCIAL:
  109. params.freqs = social_channels;
  110. break;
  111. case P2P_SCAN_FULL:
  112. break;
  113. case P2P_SCAN_SPECIFIC:
  114. social_channels[0] = freq;
  115. social_channels[1] = 0;
  116. params.freqs = social_channels;
  117. break;
  118. case P2P_SCAN_SOCIAL_PLUS_ONE:
  119. social_channels[3] = freq;
  120. params.freqs = social_channels;
  121. break;
  122. }
  123. wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
  124. ret = wpa_drv_scan(wpa_s, &params);
  125. wpabuf_free(ies);
  126. if (ret < 0)
  127. wpa_s->scan_res_handler = NULL;
  128. return ret;
  129. }
  130. static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
  131. {
  132. switch (p2p_group_interface) {
  133. case P2P_GROUP_INTERFACE_PENDING:
  134. return WPA_IF_P2P_GROUP;
  135. case P2P_GROUP_INTERFACE_GO:
  136. return WPA_IF_P2P_GO;
  137. case P2P_GROUP_INTERFACE_CLIENT:
  138. return WPA_IF_P2P_CLIENT;
  139. }
  140. return WPA_IF_P2P_GROUP;
  141. }
  142. static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
  143. const u8 *ssid,
  144. size_t ssid_len, int *go)
  145. {
  146. struct wpa_ssid *s;
  147. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  148. for (s = wpa_s->conf->ssid; s; s = s->next) {
  149. if (s->disabled != 0 || !s->p2p_group ||
  150. s->ssid_len != ssid_len ||
  151. os_memcmp(ssid, s->ssid, ssid_len) != 0)
  152. continue;
  153. if (s->mode == WPAS_MODE_P2P_GO &&
  154. s != wpa_s->current_ssid)
  155. continue;
  156. if (go)
  157. *go = s->mode == WPAS_MODE_P2P_GO;
  158. return wpa_s;
  159. }
  160. }
  161. return NULL;
  162. }
  163. static void wpas_p2p_group_delete(struct wpa_supplicant *wpa_s)
  164. {
  165. struct wpa_ssid *ssid;
  166. char *gtype;
  167. const char *reason;
  168. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  169. ssid = wpa_s->current_ssid;
  170. if (ssid == NULL) {
  171. /*
  172. * The current SSID was not known, but there may still be a
  173. * pending P2P group interface waiting for provisioning.
  174. */
  175. ssid = wpa_s->conf->ssid;
  176. while (ssid) {
  177. if (ssid->p2p_group &&
  178. (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
  179. (ssid->key_mgmt & WPA_KEY_MGMT_WPS)))
  180. break;
  181. ssid = ssid->next;
  182. }
  183. }
  184. if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
  185. gtype = "GO";
  186. else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
  187. (ssid && ssid->mode == WPAS_MODE_INFRA)) {
  188. wpa_s->reassociate = 0;
  189. wpa_s->disconnected = 1;
  190. wpa_supplicant_deauthenticate(wpa_s,
  191. WLAN_REASON_DEAUTH_LEAVING);
  192. gtype = "client";
  193. } else
  194. gtype = "GO";
  195. if (wpa_s->cross_connect_in_use) {
  196. wpa_s->cross_connect_in_use = 0;
  197. wpa_msg(wpa_s->parent, MSG_INFO,
  198. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  199. wpa_s->ifname, wpa_s->cross_connect_uplink);
  200. }
  201. switch (wpa_s->removal_reason) {
  202. case P2P_GROUP_REMOVAL_REQUESTED:
  203. reason = " reason=REQUESTED";
  204. break;
  205. case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
  206. reason = " reason=IDLE";
  207. break;
  208. case P2P_GROUP_REMOVAL_UNAVAILABLE:
  209. reason = " reason=UNAVAILABLE";
  210. break;
  211. default:
  212. reason = "";
  213. break;
  214. }
  215. wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_REMOVED "%s %s%s",
  216. wpa_s->ifname, gtype, reason);
  217. if (ssid)
  218. wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
  219. if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
  220. struct wpa_global *global;
  221. char *ifname;
  222. enum wpa_driver_if_type type;
  223. wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
  224. wpa_s->ifname);
  225. global = wpa_s->global;
  226. ifname = os_strdup(wpa_s->ifname);
  227. type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
  228. wpa_supplicant_remove_iface(wpa_s->global, wpa_s);
  229. wpa_s = global->ifaces;
  230. if (wpa_s && ifname)
  231. wpa_drv_if_remove(wpa_s, type, ifname);
  232. os_free(ifname);
  233. return;
  234. }
  235. wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
  236. if (ssid && (ssid->p2p_group ||
  237. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
  238. (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
  239. int id = ssid->id;
  240. if (ssid == wpa_s->current_ssid) {
  241. wpa_sm_set_config(wpa_s->wpa, NULL);
  242. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  243. wpa_s->current_ssid = NULL;
  244. }
  245. /*
  246. * Networks objects created during any P2P activities are not
  247. * exposed out as they might/will confuse certain non-P2P aware
  248. * applications since these network objects won't behave like
  249. * regular ones.
  250. *
  251. * Likewise, we don't send out network removed signals for such
  252. * network objects.
  253. */
  254. wpa_config_remove_network(wpa_s->conf, id);
  255. wpa_supplicant_clear_status(wpa_s);
  256. } else {
  257. wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
  258. "found");
  259. }
  260. wpa_supplicant_ap_deinit(wpa_s);
  261. }
  262. static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
  263. u8 *go_dev_addr,
  264. const u8 *ssid, size_t ssid_len)
  265. {
  266. struct wpa_bss *bss;
  267. const u8 *bssid;
  268. struct wpabuf *p2p;
  269. u8 group_capab;
  270. const u8 *addr;
  271. if (wpa_s->go_params)
  272. bssid = wpa_s->go_params->peer_interface_addr;
  273. else
  274. bssid = wpa_s->bssid;
  275. bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
  276. if (bss == NULL) {
  277. u8 iface_addr[ETH_ALEN];
  278. if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
  279. iface_addr) == 0)
  280. bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
  281. }
  282. if (bss == NULL) {
  283. wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
  284. "group is persistent - BSS " MACSTR " not found",
  285. MAC2STR(bssid));
  286. return 0;
  287. }
  288. p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  289. if (p2p == NULL) {
  290. wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
  291. "group is persistent - BSS " MACSTR
  292. " did not include P2P IE", MAC2STR(bssid));
  293. wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
  294. (u8 *) (bss + 1), bss->ie_len);
  295. wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
  296. ((u8 *) bss + 1) + bss->ie_len,
  297. bss->beacon_ie_len);
  298. return 0;
  299. }
  300. group_capab = p2p_get_group_capab(p2p);
  301. addr = p2p_get_go_dev_addr(p2p);
  302. wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
  303. "group_capab=0x%x", group_capab);
  304. if (addr) {
  305. os_memcpy(go_dev_addr, addr, ETH_ALEN);
  306. wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
  307. MAC2STR(addr));
  308. } else
  309. os_memset(go_dev_addr, 0, ETH_ALEN);
  310. wpabuf_free(p2p);
  311. wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
  312. "go_dev_addr=" MACSTR,
  313. MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
  314. return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
  315. }
  316. static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
  317. struct wpa_ssid *ssid,
  318. const u8 *go_dev_addr)
  319. {
  320. struct wpa_ssid *s;
  321. int changed = 0;
  322. wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
  323. "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
  324. for (s = wpa_s->conf->ssid; s; s = s->next) {
  325. if (s->disabled == 2 &&
  326. os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
  327. s->ssid_len == ssid->ssid_len &&
  328. os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
  329. break;
  330. }
  331. if (s) {
  332. wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
  333. "entry");
  334. if (ssid->passphrase && !s->passphrase)
  335. changed = 1;
  336. else if (ssid->passphrase && s->passphrase &&
  337. os_strcmp(ssid->passphrase, s->passphrase) != 0)
  338. changed = 1;
  339. } else {
  340. wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
  341. "entry");
  342. changed = 1;
  343. s = wpa_config_add_network(wpa_s->conf);
  344. if (s == NULL)
  345. return -1;
  346. /*
  347. * Instead of network_added we emit persistent_group_added
  348. * notification. Also to keep the defense checks in
  349. * persistent_group obj registration method, we set the
  350. * relevant flags in s to designate it as a persistent group.
  351. */
  352. s->p2p_group = 1;
  353. s->p2p_persistent_group = 1;
  354. wpas_notify_persistent_group_added(wpa_s, s);
  355. wpa_config_set_network_defaults(s);
  356. }
  357. s->p2p_group = 1;
  358. s->p2p_persistent_group = 1;
  359. s->disabled = 2;
  360. s->bssid_set = 1;
  361. os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
  362. s->mode = ssid->mode;
  363. s->auth_alg = WPA_AUTH_ALG_OPEN;
  364. s->key_mgmt = WPA_KEY_MGMT_PSK;
  365. s->proto = WPA_PROTO_RSN;
  366. s->pairwise_cipher = WPA_CIPHER_CCMP;
  367. s->export_keys = 1;
  368. if (ssid->passphrase) {
  369. os_free(s->passphrase);
  370. s->passphrase = os_strdup(ssid->passphrase);
  371. }
  372. if (ssid->psk_set) {
  373. s->psk_set = 1;
  374. os_memcpy(s->psk, ssid->psk, 32);
  375. }
  376. if (s->passphrase && !s->psk_set)
  377. wpa_config_update_psk(s);
  378. if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
  379. os_free(s->ssid);
  380. s->ssid = os_malloc(ssid->ssid_len);
  381. }
  382. if (s->ssid) {
  383. s->ssid_len = ssid->ssid_len;
  384. os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
  385. }
  386. #ifndef CONFIG_NO_CONFIG_WRITE
  387. if (changed && wpa_s->conf->update_config &&
  388. wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  389. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  390. }
  391. #endif /* CONFIG_NO_CONFIG_WRITE */
  392. return s->id;
  393. }
  394. static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
  395. int success)
  396. {
  397. struct wpa_ssid *ssid;
  398. const char *ssid_txt;
  399. int client;
  400. int persistent;
  401. u8 go_dev_addr[ETH_ALEN];
  402. int network_id = -1;
  403. /*
  404. * This callback is likely called for the main interface. Update wpa_s
  405. * to use the group interface if a new interface was created for the
  406. * group.
  407. */
  408. if (wpa_s->global->p2p_group_formation)
  409. wpa_s = wpa_s->global->p2p_group_formation;
  410. wpa_s->global->p2p_group_formation = NULL;
  411. wpa_s->p2p_in_provisioning = 0;
  412. if (!success) {
  413. wpa_msg(wpa_s->parent, MSG_INFO,
  414. P2P_EVENT_GROUP_FORMATION_FAILURE);
  415. wpas_p2p_group_delete(wpa_s);
  416. return;
  417. }
  418. wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
  419. ssid = wpa_s->current_ssid;
  420. if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
  421. ssid->mode = WPAS_MODE_P2P_GO;
  422. p2p_group_notif_formation_done(wpa_s->p2p_group);
  423. wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
  424. }
  425. persistent = 0;
  426. if (ssid) {
  427. ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
  428. client = ssid->mode == WPAS_MODE_INFRA;
  429. if (ssid->mode == WPAS_MODE_P2P_GO) {
  430. persistent = ssid->p2p_persistent_group;
  431. os_memcpy(go_dev_addr, wpa_s->parent->own_addr,
  432. ETH_ALEN);
  433. } else
  434. persistent = wpas_p2p_persistent_group(wpa_s,
  435. go_dev_addr,
  436. ssid->ssid,
  437. ssid->ssid_len);
  438. } else {
  439. ssid_txt = "";
  440. client = wpa_s->p2p_group_interface ==
  441. P2P_GROUP_INTERFACE_CLIENT;
  442. os_memset(go_dev_addr, 0, ETH_ALEN);
  443. }
  444. wpa_s->show_group_started = 0;
  445. if (client) {
  446. /*
  447. * Indicate event only after successfully completed 4-way
  448. * handshake, i.e., when the interface is ready for data
  449. * packets.
  450. */
  451. wpa_s->show_group_started = 1;
  452. } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
  453. char psk[65];
  454. wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
  455. wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  456. "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
  457. "%s",
  458. wpa_s->ifname, ssid_txt, ssid->frequency, psk,
  459. MAC2STR(go_dev_addr),
  460. persistent ? " [PERSISTENT]" : "");
  461. wpas_p2p_cross_connect_setup(wpa_s);
  462. wpas_p2p_set_group_idle_timeout(wpa_s);
  463. } else {
  464. wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  465. "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
  466. "go_dev_addr=" MACSTR "%s",
  467. wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
  468. ssid && ssid->passphrase ? ssid->passphrase : "",
  469. MAC2STR(go_dev_addr),
  470. persistent ? " [PERSISTENT]" : "");
  471. wpas_p2p_cross_connect_setup(wpa_s);
  472. wpas_p2p_set_group_idle_timeout(wpa_s);
  473. }
  474. if (persistent)
  475. network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
  476. ssid, go_dev_addr);
  477. if (network_id < 0 && ssid)
  478. network_id = ssid->id;
  479. if (!client)
  480. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
  481. }
  482. static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
  483. unsigned int freq,
  484. const u8 *dst, const u8 *src,
  485. const u8 *bssid,
  486. const u8 *data, size_t data_len,
  487. enum offchannel_send_action_result
  488. result)
  489. {
  490. enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
  491. if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
  492. return;
  493. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  494. return;
  495. switch (result) {
  496. case OFFCHANNEL_SEND_ACTION_SUCCESS:
  497. res = P2P_SEND_ACTION_SUCCESS;
  498. break;
  499. case OFFCHANNEL_SEND_ACTION_NO_ACK:
  500. res = P2P_SEND_ACTION_NO_ACK;
  501. break;
  502. case OFFCHANNEL_SEND_ACTION_FAILED:
  503. res = P2P_SEND_ACTION_FAILED;
  504. break;
  505. }
  506. p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
  507. if (wpa_s->pending_pd_before_join &&
  508. (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
  509. os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
  510. wpa_s->pending_pd_before_join = 0;
  511. wpa_printf(MSG_DEBUG, "P2P: Starting pending "
  512. "join-existing-group operation");
  513. wpas_p2p_join_start(wpa_s);
  514. }
  515. }
  516. static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
  517. const u8 *src, const u8 *bssid, const u8 *buf,
  518. size_t len, unsigned int wait_time)
  519. {
  520. struct wpa_supplicant *wpa_s = ctx;
  521. return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
  522. wait_time,
  523. wpas_p2p_send_action_tx_status, 1);
  524. }
  525. static void wpas_send_action_done(void *ctx)
  526. {
  527. struct wpa_supplicant *wpa_s = ctx;
  528. offchannel_send_action_done(wpa_s);
  529. }
  530. static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
  531. struct p2p_go_neg_results *params)
  532. {
  533. if (wpa_s->go_params == NULL) {
  534. wpa_s->go_params = os_malloc(sizeof(*params));
  535. if (wpa_s->go_params == NULL)
  536. return -1;
  537. }
  538. os_memcpy(wpa_s->go_params, params, sizeof(*params));
  539. return 0;
  540. }
  541. static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
  542. struct p2p_go_neg_results *res)
  543. {
  544. wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
  545. MAC2STR(res->peer_interface_addr));
  546. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
  547. res->ssid, res->ssid_len);
  548. wpa_supplicant_ap_deinit(wpa_s);
  549. wpas_copy_go_neg_results(wpa_s, res);
  550. if (res->wps_method == WPS_PBC)
  551. wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
  552. else {
  553. u16 dev_pw_id = DEV_PW_DEFAULT;
  554. if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
  555. dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
  556. wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
  557. wpa_s->p2p_pin, 1, dev_pw_id);
  558. }
  559. }
  560. static void p2p_go_configured(void *ctx, void *data)
  561. {
  562. struct wpa_supplicant *wpa_s = ctx;
  563. struct p2p_go_neg_results *params = data;
  564. struct wpa_ssid *ssid;
  565. int network_id = -1;
  566. ssid = wpa_s->current_ssid;
  567. if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
  568. wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
  569. if (wpa_s->global->p2p_group_formation == wpa_s)
  570. wpa_s->global->p2p_group_formation = NULL;
  571. wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  572. "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
  573. "go_dev_addr=" MACSTR "%s",
  574. wpa_s->ifname,
  575. wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
  576. ssid->frequency,
  577. params->passphrase ? params->passphrase : "",
  578. MAC2STR(wpa_s->parent->own_addr),
  579. params->persistent_group ? " [PERSISTENT]" : "");
  580. if (params->persistent_group)
  581. network_id = wpas_p2p_store_persistent_group(
  582. wpa_s->parent, ssid,
  583. wpa_s->parent->own_addr);
  584. if (network_id < 0)
  585. network_id = ssid->id;
  586. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
  587. wpas_p2p_cross_connect_setup(wpa_s);
  588. wpas_p2p_set_group_idle_timeout(wpa_s);
  589. return;
  590. }
  591. wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
  592. if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
  593. params->peer_interface_addr)) {
  594. wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
  595. "filtering");
  596. return;
  597. }
  598. if (params->wps_method == WPS_PBC)
  599. wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
  600. NULL);
  601. else if (wpa_s->p2p_pin[0])
  602. wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
  603. wpa_s->p2p_pin, NULL, 0);
  604. os_free(wpa_s->go_params);
  605. wpa_s->go_params = NULL;
  606. }
  607. static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
  608. struct p2p_go_neg_results *params,
  609. int group_formation)
  610. {
  611. struct wpa_ssid *ssid;
  612. if (wpas_copy_go_neg_results(wpa_s, params) < 0)
  613. return;
  614. ssid = wpa_config_add_network(wpa_s->conf);
  615. if (ssid == NULL)
  616. return;
  617. wpa_config_set_network_defaults(ssid);
  618. ssid->temporary = 1;
  619. ssid->p2p_group = 1;
  620. ssid->p2p_persistent_group = params->persistent_group;
  621. ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
  622. WPAS_MODE_P2P_GO;
  623. ssid->frequency = params->freq;
  624. ssid->ssid = os_zalloc(params->ssid_len + 1);
  625. if (ssid->ssid) {
  626. os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
  627. ssid->ssid_len = params->ssid_len;
  628. }
  629. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  630. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  631. ssid->proto = WPA_PROTO_RSN;
  632. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  633. ssid->passphrase = os_strdup(params->passphrase);
  634. wpa_s->ap_configured_cb = p2p_go_configured;
  635. wpa_s->ap_configured_cb_ctx = wpa_s;
  636. wpa_s->ap_configured_cb_data = wpa_s->go_params;
  637. wpa_s->connect_without_scan = ssid;
  638. wpa_s->reassociate = 1;
  639. wpa_s->disconnected = 0;
  640. wpa_supplicant_req_scan(wpa_s, 0, 0);
  641. }
  642. static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
  643. const struct wpa_supplicant *src)
  644. {
  645. struct wpa_config *d;
  646. const struct wpa_config *s;
  647. d = dst->conf;
  648. s = src->conf;
  649. #define C(n) if (s->n) d->n = os_strdup(s->n)
  650. C(device_name);
  651. C(manufacturer);
  652. C(model_name);
  653. C(model_number);
  654. C(serial_number);
  655. C(config_methods);
  656. #undef C
  657. os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
  658. os_memcpy(d->sec_device_type, s->sec_device_type,
  659. sizeof(d->sec_device_type));
  660. d->num_sec_device_types = s->num_sec_device_types;
  661. d->p2p_group_idle = s->p2p_group_idle;
  662. d->p2p_intra_bss = s->p2p_intra_bss;
  663. d->persistent_reconnect = s->persistent_reconnect;
  664. }
  665. static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
  666. enum wpa_driver_if_type type)
  667. {
  668. char ifname[120], force_ifname[120];
  669. if (wpa_s->pending_interface_name[0]) {
  670. wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
  671. "- skip creation of a new one");
  672. if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
  673. wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
  674. "unknown?! ifname='%s'",
  675. wpa_s->pending_interface_name);
  676. return -1;
  677. }
  678. return 0;
  679. }
  680. os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
  681. wpa_s->p2p_group_idx);
  682. if (os_strlen(ifname) >= IFNAMSIZ &&
  683. os_strlen(wpa_s->ifname) < IFNAMSIZ) {
  684. /* Try to avoid going over the IFNAMSIZ length limit */
  685. os_snprintf(ifname, sizeof(ifname), "p2p-%d",
  686. wpa_s->p2p_group_idx);
  687. }
  688. force_ifname[0] = '\0';
  689. wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
  690. ifname);
  691. wpa_s->p2p_group_idx++;
  692. wpa_s->pending_interface_type = type;
  693. if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
  694. wpa_s->pending_interface_addr, NULL) < 0) {
  695. wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
  696. "interface");
  697. return -1;
  698. }
  699. if (force_ifname[0]) {
  700. wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
  701. force_ifname);
  702. os_strlcpy(wpa_s->pending_interface_name, force_ifname,
  703. sizeof(wpa_s->pending_interface_name));
  704. } else
  705. os_strlcpy(wpa_s->pending_interface_name, ifname,
  706. sizeof(wpa_s->pending_interface_name));
  707. wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
  708. MACSTR, wpa_s->pending_interface_name,
  709. MAC2STR(wpa_s->pending_interface_addr));
  710. return 0;
  711. }
  712. static void wpas_p2p_remove_pending_group_interface(
  713. struct wpa_supplicant *wpa_s)
  714. {
  715. if (!wpa_s->pending_interface_name[0] ||
  716. is_zero_ether_addr(wpa_s->pending_interface_addr))
  717. return; /* No pending virtual interface */
  718. wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
  719. wpa_s->pending_interface_name);
  720. wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
  721. wpa_s->pending_interface_name);
  722. os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
  723. wpa_s->pending_interface_name[0] = '\0';
  724. }
  725. static struct wpa_supplicant *
  726. wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
  727. {
  728. struct wpa_interface iface;
  729. struct wpa_supplicant *group_wpa_s;
  730. if (!wpa_s->pending_interface_name[0]) {
  731. wpa_printf(MSG_ERROR, "P2P: No pending group interface");
  732. if (!wpas_p2p_create_iface(wpa_s))
  733. return NULL;
  734. /*
  735. * Something has forced us to remove the pending interface; try
  736. * to create a new one and hope for the best that we will get
  737. * the same local address.
  738. */
  739. if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
  740. WPA_IF_P2P_CLIENT) < 0)
  741. return NULL;
  742. }
  743. os_memset(&iface, 0, sizeof(iface));
  744. iface.ifname = wpa_s->pending_interface_name;
  745. iface.driver = wpa_s->driver->name;
  746. iface.ctrl_interface = wpa_s->conf->ctrl_interface;
  747. iface.driver_param = wpa_s->conf->driver_param;
  748. group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
  749. if (group_wpa_s == NULL) {
  750. wpa_printf(MSG_ERROR, "P2P: Failed to create new "
  751. "wpa_supplicant interface");
  752. return NULL;
  753. }
  754. wpa_s->pending_interface_name[0] = '\0';
  755. group_wpa_s->parent = wpa_s;
  756. group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
  757. P2P_GROUP_INTERFACE_CLIENT;
  758. wpa_s->global->p2p_group_formation = group_wpa_s;
  759. wpas_p2p_clone_config(group_wpa_s, wpa_s);
  760. return group_wpa_s;
  761. }
  762. static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
  763. void *timeout_ctx)
  764. {
  765. struct wpa_supplicant *wpa_s = eloop_ctx;
  766. wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
  767. if (wpa_s->global->p2p)
  768. p2p_group_formation_failed(wpa_s->global->p2p);
  769. else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  770. wpa_drv_p2p_group_formation_failed(wpa_s);
  771. wpas_group_formation_completed(wpa_s, 0);
  772. }
  773. void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
  774. {
  775. struct wpa_supplicant *wpa_s = ctx;
  776. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  777. wpa_drv_cancel_remain_on_channel(wpa_s);
  778. wpa_s->off_channel_freq = 0;
  779. wpa_s->roc_waiting_drv_freq = 0;
  780. }
  781. if (res->status) {
  782. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
  783. res->status);
  784. wpas_notify_p2p_go_neg_completed(wpa_s, res->status);
  785. wpas_p2p_remove_pending_group_interface(wpa_s);
  786. return;
  787. }
  788. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
  789. wpas_notify_p2p_go_neg_completed(wpa_s, P2P_SC_SUCCESS);
  790. if (wpa_s->create_p2p_iface) {
  791. struct wpa_supplicant *group_wpa_s =
  792. wpas_p2p_init_group_interface(wpa_s, res->role_go);
  793. if (group_wpa_s == NULL) {
  794. wpas_p2p_remove_pending_group_interface(wpa_s);
  795. return;
  796. }
  797. if (group_wpa_s != wpa_s) {
  798. os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
  799. sizeof(group_wpa_s->p2p_pin));
  800. group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
  801. }
  802. os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
  803. wpa_s->pending_interface_name[0] = '\0';
  804. group_wpa_s->p2p_in_provisioning = 1;
  805. if (res->role_go)
  806. wpas_start_wps_go(group_wpa_s, res, 1);
  807. else
  808. wpas_start_wps_enrollee(group_wpa_s, res);
  809. } else {
  810. wpa_s->p2p_in_provisioning = 1;
  811. wpa_s->global->p2p_group_formation = wpa_s;
  812. if (res->role_go)
  813. wpas_start_wps_go(wpa_s, res, 1);
  814. else
  815. wpas_start_wps_enrollee(ctx, res);
  816. }
  817. wpa_s->p2p_long_listen = 0;
  818. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  819. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  820. eloop_register_timeout(15 + res->peer_config_timeout / 100,
  821. (res->peer_config_timeout % 100) * 10000,
  822. wpas_p2p_group_formation_timeout, wpa_s, NULL);
  823. }
  824. void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
  825. {
  826. struct wpa_supplicant *wpa_s = ctx;
  827. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
  828. " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
  829. wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
  830. }
  831. void wpas_dev_found(void *ctx, const u8 *addr,
  832. const struct p2p_peer_info *info,
  833. int new_device)
  834. {
  835. struct wpa_supplicant *wpa_s = ctx;
  836. char devtype[WPS_DEV_TYPE_BUFSIZE];
  837. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
  838. " p2p_dev_addr=" MACSTR
  839. " pri_dev_type=%s name='%s' config_methods=0x%x "
  840. "dev_capab=0x%x group_capab=0x%x",
  841. MAC2STR(addr), MAC2STR(info->p2p_device_addr),
  842. wps_dev_type_bin2str(info->pri_dev_type, devtype,
  843. sizeof(devtype)),
  844. info->device_name, info->config_methods,
  845. info->dev_capab, info->group_capab);
  846. wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
  847. }
  848. static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
  849. {
  850. struct wpa_supplicant *wpa_s = ctx;
  851. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
  852. "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
  853. wpas_notify_p2p_device_lost(wpa_s, dev_addr);
  854. }
  855. static int wpas_start_listen(void *ctx, unsigned int freq,
  856. unsigned int duration,
  857. const struct wpabuf *probe_resp_ie)
  858. {
  859. struct wpa_supplicant *wpa_s = ctx;
  860. wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
  861. if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
  862. wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
  863. "report received Probe Request frames");
  864. return -1;
  865. }
  866. wpa_s->pending_listen_freq = freq;
  867. wpa_s->pending_listen_duration = duration;
  868. if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
  869. wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
  870. "to remain on channel (%u MHz) for Listen "
  871. "state", freq);
  872. wpa_s->pending_listen_freq = 0;
  873. return -1;
  874. }
  875. wpa_s->off_channel_freq = 0;
  876. wpa_s->roc_waiting_drv_freq = freq;
  877. return 0;
  878. }
  879. static void wpas_stop_listen(void *ctx)
  880. {
  881. struct wpa_supplicant *wpa_s = ctx;
  882. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  883. wpa_drv_cancel_remain_on_channel(wpa_s);
  884. wpa_s->off_channel_freq = 0;
  885. wpa_s->roc_waiting_drv_freq = 0;
  886. }
  887. wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
  888. wpa_drv_probe_req_report(wpa_s, 0);
  889. }
  890. static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
  891. {
  892. struct wpa_supplicant *wpa_s = ctx;
  893. return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf));
  894. }
  895. static struct p2p_srv_bonjour *
  896. wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
  897. const struct wpabuf *query)
  898. {
  899. struct p2p_srv_bonjour *bsrv;
  900. size_t len;
  901. len = wpabuf_len(query);
  902. dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
  903. struct p2p_srv_bonjour, list) {
  904. if (len == wpabuf_len(bsrv->query) &&
  905. os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
  906. len) == 0)
  907. return bsrv;
  908. }
  909. return NULL;
  910. }
  911. static struct p2p_srv_upnp *
  912. wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
  913. const char *service)
  914. {
  915. struct p2p_srv_upnp *usrv;
  916. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  917. struct p2p_srv_upnp, list) {
  918. if (version == usrv->version &&
  919. os_strcmp(service, usrv->service) == 0)
  920. return usrv;
  921. }
  922. return NULL;
  923. }
  924. static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
  925. u8 srv_trans_id)
  926. {
  927. u8 *len_pos;
  928. if (wpabuf_tailroom(resp) < 5)
  929. return;
  930. /* Length (to be filled) */
  931. len_pos = wpabuf_put(resp, 2);
  932. wpabuf_put_u8(resp, srv_proto);
  933. wpabuf_put_u8(resp, srv_trans_id);
  934. /* Status Code */
  935. wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
  936. /* Response Data: empty */
  937. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  938. }
  939. static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
  940. struct wpabuf *resp, u8 srv_trans_id)
  941. {
  942. struct p2p_srv_bonjour *bsrv;
  943. u8 *len_pos;
  944. wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
  945. if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  946. wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
  947. return;
  948. }
  949. dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
  950. struct p2p_srv_bonjour, list) {
  951. if (wpabuf_tailroom(resp) <
  952. 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
  953. return;
  954. /* Length (to be filled) */
  955. len_pos = wpabuf_put(resp, 2);
  956. wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
  957. wpabuf_put_u8(resp, srv_trans_id);
  958. /* Status Code */
  959. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  960. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
  961. wpabuf_head(bsrv->resp),
  962. wpabuf_len(bsrv->resp));
  963. /* Response Data */
  964. wpabuf_put_buf(resp, bsrv->query); /* Key */
  965. wpabuf_put_buf(resp, bsrv->resp); /* Value */
  966. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  967. 2);
  968. }
  969. }
  970. static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
  971. struct wpabuf *resp, u8 srv_trans_id,
  972. const u8 *query, size_t query_len)
  973. {
  974. struct p2p_srv_bonjour *bsrv;
  975. struct wpabuf buf;
  976. u8 *len_pos;
  977. wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
  978. query, query_len);
  979. if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  980. wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
  981. wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
  982. srv_trans_id);
  983. return;
  984. }
  985. if (query_len == 0) {
  986. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  987. return;
  988. }
  989. if (wpabuf_tailroom(resp) < 5)
  990. return;
  991. /* Length (to be filled) */
  992. len_pos = wpabuf_put(resp, 2);
  993. wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
  994. wpabuf_put_u8(resp, srv_trans_id);
  995. wpabuf_set(&buf, query, query_len);
  996. bsrv = wpas_p2p_service_get_bonjour(wpa_s, &buf);
  997. if (bsrv == NULL) {
  998. wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
  999. "available");
  1000. /* Status Code */
  1001. wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
  1002. /* Response Data: empty */
  1003. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  1004. 2);
  1005. return;
  1006. }
  1007. /* Status Code */
  1008. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1009. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
  1010. wpabuf_head(bsrv->resp), wpabuf_len(bsrv->resp));
  1011. if (wpabuf_tailroom(resp) >=
  1012. wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) {
  1013. /* Response Data */
  1014. wpabuf_put_buf(resp, bsrv->query); /* Key */
  1015. wpabuf_put_buf(resp, bsrv->resp); /* Value */
  1016. }
  1017. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1018. }
  1019. static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
  1020. struct wpabuf *resp, u8 srv_trans_id)
  1021. {
  1022. struct p2p_srv_upnp *usrv;
  1023. u8 *len_pos;
  1024. wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
  1025. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
  1026. wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
  1027. return;
  1028. }
  1029. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  1030. struct p2p_srv_upnp, list) {
  1031. if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
  1032. return;
  1033. /* Length (to be filled) */
  1034. len_pos = wpabuf_put(resp, 2);
  1035. wpabuf_put_u8(resp, P2P_SERV_UPNP);
  1036. wpabuf_put_u8(resp, srv_trans_id);
  1037. /* Status Code */
  1038. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1039. /* Response Data */
  1040. wpabuf_put_u8(resp, usrv->version);
  1041. wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
  1042. usrv->service);
  1043. wpabuf_put_str(resp, usrv->service);
  1044. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  1045. 2);
  1046. }
  1047. }
  1048. static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
  1049. struct wpabuf *resp, u8 srv_trans_id,
  1050. const u8 *query, size_t query_len)
  1051. {
  1052. struct p2p_srv_upnp *usrv;
  1053. u8 *len_pos;
  1054. u8 version;
  1055. char *str;
  1056. int count = 0;
  1057. wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
  1058. query, query_len);
  1059. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
  1060. wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
  1061. wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
  1062. srv_trans_id);
  1063. return;
  1064. }
  1065. if (query_len == 0) {
  1066. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  1067. return;
  1068. }
  1069. if (wpabuf_tailroom(resp) < 5)
  1070. return;
  1071. /* Length (to be filled) */
  1072. len_pos = wpabuf_put(resp, 2);
  1073. wpabuf_put_u8(resp, P2P_SERV_UPNP);
  1074. wpabuf_put_u8(resp, srv_trans_id);
  1075. version = query[0];
  1076. str = os_malloc(query_len);
  1077. if (str == NULL)
  1078. return;
  1079. os_memcpy(str, query + 1, query_len - 1);
  1080. str[query_len - 1] = '\0';
  1081. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  1082. struct p2p_srv_upnp, list) {
  1083. if (version != usrv->version)
  1084. continue;
  1085. if (os_strcmp(str, "ssdp:all") != 0 &&
  1086. os_strstr(usrv->service, str) == NULL)
  1087. continue;
  1088. if (wpabuf_tailroom(resp) < 2)
  1089. break;
  1090. if (count == 0) {
  1091. /* Status Code */
  1092. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1093. /* Response Data */
  1094. wpabuf_put_u8(resp, version);
  1095. } else
  1096. wpabuf_put_u8(resp, ',');
  1097. count++;
  1098. wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
  1099. usrv->service);
  1100. if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
  1101. break;
  1102. wpabuf_put_str(resp, usrv->service);
  1103. }
  1104. os_free(str);
  1105. if (count == 0) {
  1106. wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
  1107. "available");
  1108. /* Status Code */
  1109. wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
  1110. /* Response Data: empty */
  1111. }
  1112. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1113. }
  1114. void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
  1115. u16 update_indic, const u8 *tlvs, size_t tlvs_len)
  1116. {
  1117. struct wpa_supplicant *wpa_s = ctx;
  1118. const u8 *pos = tlvs;
  1119. const u8 *end = tlvs + tlvs_len;
  1120. const u8 *tlv_end;
  1121. u16 slen;
  1122. struct wpabuf *resp;
  1123. u8 srv_proto, srv_trans_id;
  1124. size_t buf_len;
  1125. char *buf;
  1126. wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
  1127. tlvs, tlvs_len);
  1128. buf_len = 2 * tlvs_len + 1;
  1129. buf = os_malloc(buf_len);
  1130. if (buf) {
  1131. wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
  1132. wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
  1133. MACSTR " %u %u %s",
  1134. freq, MAC2STR(sa), dialog_token, update_indic,
  1135. buf);
  1136. os_free(buf);
  1137. }
  1138. if (wpa_s->p2p_sd_over_ctrl_iface)
  1139. return; /* to be processed by an external program */
  1140. resp = wpabuf_alloc(10000);
  1141. if (resp == NULL)
  1142. return;
  1143. while (pos + 1 < end) {
  1144. wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
  1145. slen = WPA_GET_LE16(pos);
  1146. pos += 2;
  1147. if (pos + slen > end || slen < 2) {
  1148. wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
  1149. "length");
  1150. wpabuf_free(resp);
  1151. return;
  1152. }
  1153. tlv_end = pos + slen;
  1154. srv_proto = *pos++;
  1155. wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
  1156. srv_proto);
  1157. srv_trans_id = *pos++;
  1158. wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
  1159. srv_trans_id);
  1160. wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
  1161. pos, tlv_end - pos);
  1162. if (wpa_s->force_long_sd) {
  1163. wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
  1164. "response");
  1165. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  1166. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  1167. goto done;
  1168. }
  1169. switch (srv_proto) {
  1170. case P2P_SERV_ALL_SERVICES:
  1171. wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
  1172. "for all services");
  1173. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
  1174. dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  1175. wpa_printf(MSG_DEBUG, "P2P: No service "
  1176. "discovery protocols available");
  1177. wpas_sd_add_proto_not_avail(
  1178. resp, P2P_SERV_ALL_SERVICES,
  1179. srv_trans_id);
  1180. break;
  1181. }
  1182. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  1183. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  1184. break;
  1185. case P2P_SERV_BONJOUR:
  1186. wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
  1187. pos, tlv_end - pos);
  1188. break;
  1189. case P2P_SERV_UPNP:
  1190. wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
  1191. pos, tlv_end - pos);
  1192. break;
  1193. default:
  1194. wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
  1195. "protocol %u", srv_proto);
  1196. wpas_sd_add_proto_not_avail(resp, srv_proto,
  1197. srv_trans_id);
  1198. break;
  1199. }
  1200. pos = tlv_end;
  1201. }
  1202. done:
  1203. wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
  1204. update_indic, tlvs, tlvs_len);
  1205. wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
  1206. wpabuf_free(resp);
  1207. }
  1208. void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
  1209. const u8 *tlvs, size_t tlvs_len)
  1210. {
  1211. struct wpa_supplicant *wpa_s = ctx;
  1212. const u8 *pos = tlvs;
  1213. const u8 *end = tlvs + tlvs_len;
  1214. const u8 *tlv_end;
  1215. u16 slen;
  1216. size_t buf_len;
  1217. char *buf;
  1218. wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
  1219. tlvs, tlvs_len);
  1220. if (tlvs_len > 1500) {
  1221. /* TODO: better way for handling this */
  1222. wpa_msg_ctrl(wpa_s, MSG_INFO,
  1223. P2P_EVENT_SERV_DISC_RESP MACSTR
  1224. " %u <long response: %u bytes>",
  1225. MAC2STR(sa), update_indic,
  1226. (unsigned int) tlvs_len);
  1227. } else {
  1228. buf_len = 2 * tlvs_len + 1;
  1229. buf = os_malloc(buf_len);
  1230. if (buf) {
  1231. wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
  1232. wpa_msg_ctrl(wpa_s, MSG_INFO,
  1233. P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
  1234. MAC2STR(sa), update_indic, buf);
  1235. os_free(buf);
  1236. }
  1237. }
  1238. while (pos < end) {
  1239. u8 srv_proto, srv_trans_id, status;
  1240. wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
  1241. slen = WPA_GET_LE16(pos);
  1242. pos += 2;
  1243. if (pos + slen > end || slen < 3) {
  1244. wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
  1245. "length");
  1246. return;
  1247. }
  1248. tlv_end = pos + slen;
  1249. srv_proto = *pos++;
  1250. wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
  1251. srv_proto);
  1252. srv_trans_id = *pos++;
  1253. wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
  1254. srv_trans_id);
  1255. status = *pos++;
  1256. wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
  1257. status);
  1258. wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
  1259. pos, tlv_end - pos);
  1260. pos = tlv_end;
  1261. }
  1262. wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
  1263. }
  1264. void * wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
  1265. const struct wpabuf *tlvs)
  1266. {
  1267. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  1268. return (void *) wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
  1269. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  1270. return NULL;
  1271. return p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
  1272. }
  1273. void * wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
  1274. u8 version, const char *query)
  1275. {
  1276. struct wpabuf *tlvs;
  1277. void *ret;
  1278. tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
  1279. if (tlvs == NULL)
  1280. return NULL;
  1281. wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
  1282. wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
  1283. wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
  1284. wpabuf_put_u8(tlvs, version);
  1285. wpabuf_put_str(tlvs, query);
  1286. ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
  1287. wpabuf_free(tlvs);
  1288. return ret;
  1289. }
  1290. int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, void *req)
  1291. {
  1292. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  1293. return wpa_drv_p2p_sd_cancel_request(wpa_s, (u64) req);
  1294. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  1295. return -1;
  1296. return p2p_sd_cancel_request(wpa_s->global->p2p, req);
  1297. }
  1298. void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
  1299. const u8 *dst, u8 dialog_token,
  1300. const struct wpabuf *resp_tlvs)
  1301. {
  1302. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  1303. wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
  1304. resp_tlvs);
  1305. return;
  1306. }
  1307. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  1308. return;
  1309. p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
  1310. resp_tlvs);
  1311. }
  1312. void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
  1313. {
  1314. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  1315. wpa_drv_p2p_service_update(wpa_s);
  1316. return;
  1317. }
  1318. if (wpa_s->global->p2p)
  1319. p2p_sd_service_update(wpa_s->global->p2p);
  1320. }
  1321. static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
  1322. {
  1323. dl_list_del(&bsrv->list);
  1324. wpabuf_free(bsrv->query);
  1325. wpabuf_free(bsrv->resp);
  1326. os_free(bsrv);
  1327. }
  1328. static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
  1329. {
  1330. dl_list_del(&usrv->list);
  1331. os_free(usrv->service);
  1332. os_free(usrv);
  1333. }
  1334. void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
  1335. {
  1336. struct p2p_srv_bonjour *bsrv, *bn;
  1337. struct p2p_srv_upnp *usrv, *un;
  1338. dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
  1339. struct p2p_srv_bonjour, list)
  1340. wpas_p2p_srv_bonjour_free(bsrv);
  1341. dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
  1342. struct p2p_srv_upnp, list)
  1343. wpas_p2p_srv_upnp_free(usrv);
  1344. wpas_p2p_sd_service_update(wpa_s);
  1345. }
  1346. int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
  1347. struct wpabuf *query, struct wpabuf *resp)
  1348. {
  1349. struct p2p_srv_bonjour *bsrv;
  1350. bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
  1351. if (bsrv) {
  1352. wpabuf_free(query);
  1353. wpabuf_free(bsrv->resp);
  1354. bsrv->resp = resp;
  1355. return 0;
  1356. }
  1357. bsrv = os_zalloc(sizeof(*bsrv));
  1358. if (bsrv == NULL)
  1359. return -1;
  1360. bsrv->query = query;
  1361. bsrv->resp = resp;
  1362. dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
  1363. wpas_p2p_sd_service_update(wpa_s);
  1364. return 0;
  1365. }
  1366. int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
  1367. const struct wpabuf *query)
  1368. {
  1369. struct p2p_srv_bonjour *bsrv;
  1370. bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
  1371. if (bsrv == NULL)
  1372. return -1;
  1373. wpas_p2p_srv_bonjour_free(bsrv);
  1374. wpas_p2p_sd_service_update(wpa_s);
  1375. return 0;
  1376. }
  1377. int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
  1378. const char *service)
  1379. {
  1380. struct p2p_srv_upnp *usrv;
  1381. if (wpas_p2p_service_get_upnp(wpa_s, version, service))
  1382. return 0; /* Already listed */
  1383. usrv = os_zalloc(sizeof(*usrv));
  1384. if (usrv == NULL)
  1385. return -1;
  1386. usrv->version = version;
  1387. usrv->service = os_strdup(service);
  1388. if (usrv->service == NULL) {
  1389. os_free(usrv);
  1390. return -1;
  1391. }
  1392. dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
  1393. wpas_p2p_sd_service_update(wpa_s);
  1394. return 0;
  1395. }
  1396. int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
  1397. const char *service)
  1398. {
  1399. struct p2p_srv_upnp *usrv;
  1400. usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
  1401. if (usrv == NULL)
  1402. return -1;
  1403. wpas_p2p_srv_upnp_free(usrv);
  1404. wpas_p2p_sd_service_update(wpa_s);
  1405. return 0;
  1406. }
  1407. static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
  1408. const u8 *peer, const char *params,
  1409. unsigned int generated_pin)
  1410. {
  1411. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
  1412. MAC2STR(peer), generated_pin, params);
  1413. }
  1414. static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
  1415. const u8 *peer, const char *params)
  1416. {
  1417. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
  1418. MAC2STR(peer), params);
  1419. }
  1420. void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
  1421. const u8 *dev_addr, const u8 *pri_dev_type,
  1422. const char *dev_name, u16 supp_config_methods,
  1423. u8 dev_capab, u8 group_capab)
  1424. {
  1425. struct wpa_supplicant *wpa_s = ctx;
  1426. char devtype[WPS_DEV_TYPE_BUFSIZE];
  1427. char params[200];
  1428. u8 empty_dev_type[8];
  1429. unsigned int generated_pin = 0;
  1430. if (pri_dev_type == NULL) {
  1431. os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
  1432. pri_dev_type = empty_dev_type;
  1433. }
  1434. os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
  1435. " pri_dev_type=%s name='%s' config_methods=0x%x "
  1436. "dev_capab=0x%x group_capab=0x%x",
  1437. MAC2STR(dev_addr),
  1438. wps_dev_type_bin2str(pri_dev_type, devtype,
  1439. sizeof(devtype)),
  1440. dev_name, supp_config_methods, dev_capab, group_capab);
  1441. params[sizeof(params) - 1] = '\0';
  1442. if (config_methods & WPS_CONFIG_DISPLAY) {
  1443. generated_pin = wps_generate_pin();
  1444. wpas_prov_disc_local_display(wpa_s, peer, params,
  1445. generated_pin);
  1446. } else if (config_methods & WPS_CONFIG_KEYPAD)
  1447. wpas_prov_disc_local_keypad(wpa_s, peer, params);
  1448. else if (config_methods & WPS_CONFIG_PUSHBUTTON)
  1449. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
  1450. "%s", MAC2STR(peer), params);
  1451. wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
  1452. P2P_PROV_DISC_SUCCESS,
  1453. config_methods, generated_pin);
  1454. }
  1455. void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
  1456. {
  1457. struct wpa_supplicant *wpa_s = ctx;
  1458. unsigned int generated_pin = 0;
  1459. if (config_methods & WPS_CONFIG_DISPLAY)
  1460. wpas_prov_disc_local_keypad(wpa_s, peer, "");
  1461. else if (config_methods & WPS_CONFIG_KEYPAD) {
  1462. generated_pin = wps_generate_pin();
  1463. wpas_prov_disc_local_display(wpa_s, peer, "", generated_pin);
  1464. } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
  1465. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR,
  1466. MAC2STR(peer));
  1467. wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
  1468. P2P_PROV_DISC_SUCCESS,
  1469. config_methods, generated_pin);
  1470. if (wpa_s->pending_pd_before_join &&
  1471. (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
  1472. os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
  1473. wpa_s->pending_pd_before_join = 0;
  1474. wpa_printf(MSG_DEBUG, "P2P: Starting pending "
  1475. "join-existing-group operation");
  1476. wpas_p2p_join_start(wpa_s);
  1477. }
  1478. }
  1479. void wpas_prov_disc_fail(void *ctx, const u8 *peer,
  1480. enum p2p_prov_disc_status status)
  1481. {
  1482. struct wpa_supplicant *wpa_s = ctx;
  1483. wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
  1484. status, 0, 0);
  1485. }
  1486. static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
  1487. const u8 *go_dev_addr, const u8 *ssid,
  1488. size_t ssid_len, int *go, u8 *group_bssid,
  1489. int *force_freq, int persistent_group)
  1490. {
  1491. struct wpa_supplicant *wpa_s = ctx;
  1492. struct wpa_ssid *s;
  1493. u8 cur_bssid[ETH_ALEN];
  1494. int res;
  1495. struct wpa_supplicant *grp;
  1496. if (!persistent_group) {
  1497. wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
  1498. " to join an active group", MAC2STR(sa));
  1499. if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
  1500. (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
  1501. == 0 ||
  1502. os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
  1503. wpa_printf(MSG_DEBUG, "P2P: Accept previously "
  1504. "authorized invitation");
  1505. goto accept_inv;
  1506. }
  1507. /*
  1508. * Do not accept the invitation automatically; notify user and
  1509. * request approval.
  1510. */
  1511. return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  1512. }
  1513. grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
  1514. if (grp) {
  1515. wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
  1516. "running persistent group");
  1517. if (*go)
  1518. os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
  1519. goto accept_inv;
  1520. }
  1521. if (!wpa_s->conf->persistent_reconnect)
  1522. return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  1523. for (s = wpa_s->conf->ssid; s; s = s->next) {
  1524. if (s->disabled == 2 &&
  1525. os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
  1526. s->ssid_len == ssid_len &&
  1527. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  1528. break;
  1529. }
  1530. if (!s) {
  1531. wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
  1532. " requested reinvocation of an unknown group",
  1533. MAC2STR(sa));
  1534. return P2P_SC_FAIL_UNKNOWN_GROUP;
  1535. }
  1536. if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
  1537. *go = 1;
  1538. if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
  1539. wpa_printf(MSG_DEBUG, "P2P: The only available "
  1540. "interface is already in use - reject "
  1541. "invitation");
  1542. return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
  1543. }
  1544. os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
  1545. } else if (s->mode == WPAS_MODE_P2P_GO) {
  1546. *go = 1;
  1547. if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
  1548. {
  1549. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  1550. "interface address for the group");
  1551. return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
  1552. }
  1553. os_memcpy(group_bssid, wpa_s->pending_interface_addr,
  1554. ETH_ALEN);
  1555. }
  1556. accept_inv:
  1557. if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
  1558. wpa_s->assoc_freq) {
  1559. wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
  1560. "the channel we are already using");
  1561. *force_freq = wpa_s->assoc_freq;
  1562. }
  1563. res = wpa_drv_shared_freq(wpa_s);
  1564. if (res > 0) {
  1565. wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
  1566. "with the channel we are already using on a "
  1567. "shared interface");
  1568. *force_freq = res;
  1569. }
  1570. return P2P_SC_SUCCESS;
  1571. }
  1572. static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
  1573. const u8 *ssid, size_t ssid_len,
  1574. const u8 *go_dev_addr, u8 status,
  1575. int op_freq)
  1576. {
  1577. struct wpa_supplicant *wpa_s = ctx;
  1578. struct wpa_ssid *s;
  1579. for (s = wpa_s->conf->ssid; s; s = s->next) {
  1580. if (s->disabled == 2 &&
  1581. s->ssid_len == ssid_len &&
  1582. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  1583. break;
  1584. }
  1585. if (status == P2P_SC_SUCCESS) {
  1586. wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
  1587. " was accepted; op_freq=%d MHz",
  1588. MAC2STR(sa), op_freq);
  1589. if (s) {
  1590. wpas_p2p_group_add_persistent(
  1591. wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0);
  1592. } else if (bssid) {
  1593. wpas_p2p_join(wpa_s, bssid, go_dev_addr,
  1594. wpa_s->p2p_wps_method);
  1595. }
  1596. return;
  1597. }
  1598. if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
  1599. wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
  1600. " was rejected (status %u)", MAC2STR(sa), status);
  1601. return;
  1602. }
  1603. if (!s) {
  1604. if (bssid) {
  1605. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
  1606. "sa=" MACSTR " go_dev_addr=" MACSTR
  1607. " bssid=" MACSTR " unknown-network",
  1608. MAC2STR(sa), MAC2STR(go_dev_addr),
  1609. MAC2STR(bssid));
  1610. } else {
  1611. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
  1612. "sa=" MACSTR " go_dev_addr=" MACSTR
  1613. " unknown-network",
  1614. MAC2STR(sa), MAC2STR(go_dev_addr));
  1615. }
  1616. return;
  1617. }
  1618. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
  1619. " persistent=%d", MAC2STR(sa), s->id);
  1620. }
  1621. static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
  1622. {
  1623. struct wpa_supplicant *wpa_s = ctx;
  1624. struct wpa_ssid *ssid;
  1625. if (bssid) {
  1626. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
  1627. "status=%d " MACSTR,
  1628. status, MAC2STR(bssid));
  1629. } else {
  1630. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
  1631. "status=%d ", status);
  1632. }
  1633. wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
  1634. if (wpa_s->pending_invite_ssid_id == -1)
  1635. return; /* Invitation to active group */
  1636. if (status != P2P_SC_SUCCESS) {
  1637. wpas_p2p_remove_pending_group_interface(wpa_s);
  1638. return;
  1639. }
  1640. ssid = wpa_config_get_network(wpa_s->conf,
  1641. wpa_s->pending_invite_ssid_id);
  1642. if (ssid == NULL) {
  1643. wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
  1644. "data matching with invitation");
  1645. return;
  1646. }
  1647. wpas_p2p_group_add_persistent(wpa_s, ssid,
  1648. ssid->mode == WPAS_MODE_P2P_GO, 0);
  1649. }
  1650. static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
  1651. struct p2p_channels *chan)
  1652. {
  1653. int i, cla = 0;
  1654. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
  1655. "band");
  1656. /* Operating class 81 - 2.4 GHz band channels 1..13 */
  1657. chan->reg_class[cla].reg_class = 81;
  1658. chan->reg_class[cla].channels = 11;
  1659. for (i = 0; i < 11; i++)
  1660. chan->reg_class[cla].channel[i] = i + 1;
  1661. cla++;
  1662. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
  1663. "band");
  1664. /* Operating class 115 - 5 GHz, channels 36-48 */
  1665. chan->reg_class[cla].reg_class = 115;
  1666. chan->reg_class[cla].channels = 4;
  1667. chan->reg_class[cla].channel[0] = 36;
  1668. chan->reg_class[cla].channel[1] = 40;
  1669. chan->reg_class[cla].channel[2] = 44;
  1670. chan->reg_class[cla].channel[3] = 48;
  1671. cla++;
  1672. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
  1673. "band");
  1674. /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
  1675. chan->reg_class[cla].reg_class = 124;
  1676. chan->reg_class[cla].channels = 4;
  1677. chan->reg_class[cla].channel[0] = 149;
  1678. chan->reg_class[cla].channel[1] = 153;
  1679. chan->reg_class[cla].channel[2] = 157;
  1680. chan->reg_class[cla].channel[3] = 161;
  1681. cla++;
  1682. chan->reg_classes = cla;
  1683. return 0;
  1684. }
  1685. static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
  1686. u16 num_modes,
  1687. enum hostapd_hw_mode mode)
  1688. {
  1689. u16 i;
  1690. for (i = 0; i < num_modes; i++) {
  1691. if (modes[i].mode == mode)
  1692. return &modes[i];
  1693. }
  1694. return NULL;
  1695. }
  1696. static int has_channel(struct hostapd_hw_modes *mode, u8 chan, int *flags)
  1697. {
  1698. int i;
  1699. for (i = 0; i < mode->num_channels; i++) {
  1700. if (mode->channels[i].chan == chan) {
  1701. if (flags)
  1702. *flags = mode->channels[i].flag;
  1703. return !(mode->channels[i].flag &
  1704. (HOSTAPD_CHAN_DISABLED |
  1705. HOSTAPD_CHAN_PASSIVE_SCAN |
  1706. HOSTAPD_CHAN_NO_IBSS |
  1707. HOSTAPD_CHAN_RADAR));
  1708. }
  1709. }
  1710. return 0;
  1711. }
  1712. struct p2p_oper_class_map {
  1713. enum hostapd_hw_mode mode;
  1714. u8 op_class;
  1715. u8 min_chan;
  1716. u8 max_chan;
  1717. u8 inc;
  1718. enum { BW20, BW40PLUS, BW40MINUS } bw;
  1719. };
  1720. static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
  1721. struct p2p_channels *chan)
  1722. {
  1723. struct hostapd_hw_modes *mode;
  1724. int cla, op;
  1725. struct p2p_oper_class_map op_class[] = {
  1726. { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
  1727. { HOSTAPD_MODE_IEEE80211G, 82, 14, 14, 1, BW20 },
  1728. #if 0 /* Do not enable HT40 on 2 GHz for now */
  1729. { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
  1730. { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
  1731. #endif
  1732. { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
  1733. { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
  1734. { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
  1735. { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
  1736. { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
  1737. { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
  1738. { -1, 0, 0, 0, 0, BW20 }
  1739. };
  1740. if (wpa_s->hw.modes == NULL) {
  1741. wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
  1742. "of all supported channels; assume dualband "
  1743. "support");
  1744. return wpas_p2p_default_channels(wpa_s, chan);
  1745. }
  1746. cla = 0;
  1747. for (op = 0; op_class[op].op_class; op++) {
  1748. struct p2p_oper_class_map *o = &op_class[op];
  1749. u8 ch;
  1750. struct p2p_reg_class *reg = NULL;
  1751. mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
  1752. if (mode == NULL)
  1753. continue;
  1754. for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
  1755. int flag;
  1756. if (!has_channel(mode, ch, &flag))
  1757. continue;
  1758. if (o->bw == BW40MINUS &&
  1759. (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
  1760. !has_channel(mode, ch - 4, NULL)))
  1761. continue;
  1762. if (o->bw == BW40PLUS &&
  1763. (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
  1764. !has_channel(mode, ch + 4, NULL)))
  1765. continue;
  1766. if (reg == NULL) {
  1767. wpa_printf(MSG_DEBUG, "P2P: Add operating "
  1768. "class %u", o->op_class);
  1769. reg = &chan->reg_class[cla];
  1770. cla++;
  1771. reg->reg_class = o->op_class;
  1772. }
  1773. reg->channel[reg->channels] = ch;
  1774. reg->channels++;
  1775. }
  1776. if (reg) {
  1777. wpa_hexdump(MSG_DEBUG, "P2P: Channels",
  1778. reg->channel, reg->channels);
  1779. }
  1780. }
  1781. chan->reg_classes = cla;
  1782. return 0;
  1783. }
  1784. static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
  1785. size_t buf_len)
  1786. {
  1787. struct wpa_supplicant *wpa_s = ctx;
  1788. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  1789. if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
  1790. break;
  1791. }
  1792. if (wpa_s == NULL)
  1793. return -1;
  1794. return wpa_drv_get_noa(wpa_s, buf, buf_len);
  1795. }
  1796. /**
  1797. * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
  1798. * @global: Pointer to global data from wpa_supplicant_init()
  1799. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  1800. * Returns: 0 on success, -1 on failure
  1801. */
  1802. int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
  1803. {
  1804. struct p2p_config p2p;
  1805. unsigned int r;
  1806. int i;
  1807. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
  1808. return 0;
  1809. if (global->p2p)
  1810. return 0;
  1811. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  1812. struct p2p_params params;
  1813. wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
  1814. os_memset(&params, 0, sizeof(params));
  1815. params.dev_name = wpa_s->conf->device_name;
  1816. os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
  1817. WPS_DEV_TYPE_LEN);
  1818. params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
  1819. os_memcpy(params.sec_dev_type,
  1820. wpa_s->conf->sec_device_type,
  1821. params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
  1822. if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
  1823. return -1;
  1824. return 0;
  1825. }
  1826. os_memset(&p2p, 0, sizeof(p2p));
  1827. p2p.msg_ctx = wpa_s;
  1828. p2p.cb_ctx = wpa_s;
  1829. p2p.p2p_scan = wpas_p2p_scan;
  1830. p2p.send_action = wpas_send_action;
  1831. p2p.send_action_done = wpas_send_action_done;
  1832. p2p.go_neg_completed = wpas_go_neg_completed;
  1833. p2p.go_neg_req_rx = wpas_go_neg_req_rx;
  1834. p2p.dev_found = wpas_dev_found;
  1835. p2p.dev_lost = wpas_dev_lost;
  1836. p2p.start_listen = wpas_start_listen;
  1837. p2p.stop_listen = wpas_stop_listen;
  1838. p2p.send_probe_resp = wpas_send_probe_resp;
  1839. p2p.sd_request = wpas_sd_request;
  1840. p2p.sd_response = wpas_sd_response;
  1841. p2p.prov_disc_req = wpas_prov_disc_req;
  1842. p2p.prov_disc_resp = wpas_prov_disc_resp;
  1843. p2p.prov_disc_fail = wpas_prov_disc_fail;
  1844. p2p.invitation_process = wpas_invitation_process;
  1845. p2p.invitation_received = wpas_invitation_received;
  1846. p2p.invitation_result = wpas_invitation_result;
  1847. p2p.get_noa = wpas_get_noa;
  1848. os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
  1849. os_memcpy(p2p.dev_addr, wpa_s->own_addr, ETH_ALEN);
  1850. p2p.dev_name = wpa_s->conf->device_name;
  1851. p2p.manufacturer = wpa_s->conf->manufacturer;
  1852. p2p.model_name = wpa_s->conf->model_name;
  1853. p2p.model_number = wpa_s->conf->model_number;
  1854. p2p.serial_number = wpa_s->conf->serial_number;
  1855. if (wpa_s->wps) {
  1856. os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
  1857. p2p.config_methods = wpa_s->wps->config_methods;
  1858. }
  1859. if (wpa_s->conf->p2p_listen_reg_class &&
  1860. wpa_s->conf->p2p_listen_channel) {
  1861. p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
  1862. p2p.channel = wpa_s->conf->p2p_listen_channel;
  1863. } else {
  1864. p2p.reg_class = 81;
  1865. /*
  1866. * Pick one of the social channels randomly as the listen
  1867. * channel.
  1868. */
  1869. os_get_random((u8 *) &r, sizeof(r));
  1870. p2p.channel = 1 + (r % 3) * 5;
  1871. }
  1872. wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
  1873. if (wpa_s->conf->p2p_oper_reg_class &&
  1874. wpa_s->conf->p2p_oper_channel) {
  1875. p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
  1876. p2p.op_channel = wpa_s->conf->p2p_oper_channel;
  1877. p2p.cfg_op_channel = 1;
  1878. wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
  1879. "%d:%d", p2p.op_reg_class, p2p.op_channel);
  1880. } else {
  1881. p2p.op_reg_class = 81;
  1882. /*
  1883. * Use random operation channel from (1, 6, 11) if no other
  1884. * preference is indicated.
  1885. */
  1886. os_get_random((u8 *) &r, sizeof(r));
  1887. p2p.op_channel = 1 + (r % 3) * 5;
  1888. p2p.cfg_op_channel = 0;
  1889. wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
  1890. "%d:%d", p2p.op_reg_class, p2p.op_channel);
  1891. }
  1892. if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  1893. os_memcpy(p2p.country, wpa_s->conf->country, 2);
  1894. p2p.country[2] = 0x04;
  1895. } else
  1896. os_memcpy(p2p.country, "XX\x04", 3);
  1897. if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
  1898. wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
  1899. "channel list");
  1900. return -1;
  1901. }
  1902. os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
  1903. WPS_DEV_TYPE_LEN);
  1904. p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
  1905. os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
  1906. p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
  1907. p2p.concurrent_operations = !!(wpa_s->drv_flags &
  1908. WPA_DRIVER_FLAGS_P2P_CONCURRENT);
  1909. p2p.max_peers = 100;
  1910. if (wpa_s->conf->p2p_ssid_postfix) {
  1911. p2p.ssid_postfix_len =
  1912. os_strlen(wpa_s->conf->p2p_ssid_postfix);
  1913. if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
  1914. p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
  1915. os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
  1916. p2p.ssid_postfix_len);
  1917. }
  1918. p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
  1919. global->p2p = p2p_init(&p2p);
  1920. if (global->p2p == NULL)
  1921. return -1;
  1922. for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
  1923. if (wpa_s->conf->wps_vendor_ext[i] == NULL)
  1924. continue;
  1925. p2p_add_wps_vendor_extension(
  1926. global->p2p, wpa_s->conf->wps_vendor_ext[i]);
  1927. }
  1928. return 0;
  1929. }
  1930. /**
  1931. * wpas_p2p_deinit - Deinitialize per-interface P2P data
  1932. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  1933. *
  1934. * This function deinitialize per-interface P2P data.
  1935. */
  1936. void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
  1937. {
  1938. if (wpa_s->driver && wpa_s->drv_priv)
  1939. wpa_drv_probe_req_report(wpa_s, 0);
  1940. os_free(wpa_s->go_params);
  1941. wpa_s->go_params = NULL;
  1942. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  1943. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  1944. wpa_s->p2p_long_listen = 0;
  1945. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  1946. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  1947. wpas_p2p_remove_pending_group_interface(wpa_s);
  1948. /* TODO: remove group interface from the driver if this wpa_s instance
  1949. * is on top of a P2P group interface */
  1950. }
  1951. /**
  1952. * wpas_p2p_deinit_global - Deinitialize global P2P module
  1953. * @global: Pointer to global data from wpa_supplicant_init()
  1954. *
  1955. * This function deinitializes the global (per device) P2P module.
  1956. */
  1957. void wpas_p2p_deinit_global(struct wpa_global *global)
  1958. {
  1959. struct wpa_supplicant *wpa_s, *tmp;
  1960. char *ifname;
  1961. if (global->p2p == NULL)
  1962. return;
  1963. /* Remove remaining P2P group interfaces */
  1964. wpa_s = global->ifaces;
  1965. if (wpa_s)
  1966. wpas_p2p_service_flush(wpa_s);
  1967. while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
  1968. wpa_s = wpa_s->next;
  1969. while (wpa_s) {
  1970. enum wpa_driver_if_type type;
  1971. tmp = global->ifaces;
  1972. while (tmp &&
  1973. (tmp == wpa_s ||
  1974. tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
  1975. tmp = tmp->next;
  1976. }
  1977. if (tmp == NULL)
  1978. break;
  1979. ifname = os_strdup(tmp->ifname);
  1980. type = wpas_p2p_if_type(tmp->p2p_group_interface);
  1981. wpa_supplicant_remove_iface(global, tmp);
  1982. if (ifname)
  1983. wpa_drv_if_remove(wpa_s, type, ifname);
  1984. os_free(ifname);
  1985. }
  1986. /*
  1987. * Deinit GO data on any possibly remaining interface (if main
  1988. * interface is used as GO).
  1989. */
  1990. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  1991. if (wpa_s->ap_iface)
  1992. wpas_p2p_group_deinit(wpa_s);
  1993. }
  1994. p2p_deinit(global->p2p);
  1995. global->p2p = NULL;
  1996. }
  1997. static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
  1998. {
  1999. if (wpa_s->drv_flags &
  2000. (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
  2001. WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
  2002. return 1; /* P2P group requires a new interface in every case
  2003. */
  2004. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
  2005. return 0; /* driver does not support concurrent operations */
  2006. if (wpa_s->global->ifaces->next)
  2007. return 1; /* more that one interface already in use */
  2008. if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
  2009. return 1; /* this interface is already in use */
  2010. return 0;
  2011. }
  2012. static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
  2013. const u8 *peer_addr,
  2014. enum p2p_wps_method wps_method,
  2015. int go_intent, const u8 *own_interface_addr,
  2016. unsigned int force_freq, int persistent_group)
  2017. {
  2018. if (persistent_group && wpa_s->conf->persistent_reconnect)
  2019. persistent_group = 2;
  2020. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  2021. return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
  2022. go_intent, own_interface_addr,
  2023. force_freq, persistent_group);
  2024. }
  2025. return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
  2026. go_intent, own_interface_addr, force_freq,
  2027. persistent_group);
  2028. }
  2029. static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
  2030. const u8 *peer_addr,
  2031. enum p2p_wps_method wps_method,
  2032. int go_intent, const u8 *own_interface_addr,
  2033. unsigned int force_freq, int persistent_group)
  2034. {
  2035. if (persistent_group && wpa_s->conf->persistent_reconnect)
  2036. persistent_group = 2;
  2037. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  2038. return -1;
  2039. return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
  2040. go_intent, own_interface_addr, force_freq,
  2041. persistent_group);
  2042. }
  2043. static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
  2044. {
  2045. wpa_s->p2p_join_scan_count++;
  2046. wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
  2047. wpa_s->p2p_join_scan_count);
  2048. if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
  2049. wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
  2050. " for join operationg - stop join attempt",
  2051. MAC2STR(wpa_s->pending_join_iface_addr));
  2052. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2053. wpa_msg(wpa_s->parent, MSG_INFO,
  2054. P2P_EVENT_GROUP_FORMATION_FAILURE);
  2055. }
  2056. }
  2057. static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
  2058. struct wpa_scan_results *scan_res)
  2059. {
  2060. struct wpa_bss *bss;
  2061. int freq;
  2062. u8 iface_addr[ETH_ALEN];
  2063. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2064. if (wpa_s->global->p2p_disabled)
  2065. return;
  2066. wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for join",
  2067. scan_res ? (int) scan_res->num : -1);
  2068. if (scan_res)
  2069. wpas_p2p_scan_res_handler(wpa_s, scan_res);
  2070. freq = p2p_get_oper_freq(wpa_s->global->p2p,
  2071. wpa_s->pending_join_iface_addr);
  2072. if (freq < 0 &&
  2073. p2p_get_interface_addr(wpa_s->global->p2p,
  2074. wpa_s->pending_join_dev_addr,
  2075. iface_addr) == 0 &&
  2076. os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
  2077. {
  2078. wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
  2079. "address for join from " MACSTR " to " MACSTR
  2080. " based on newly discovered P2P peer entry",
  2081. MAC2STR(wpa_s->pending_join_iface_addr),
  2082. MAC2STR(iface_addr));
  2083. os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
  2084. ETH_ALEN);
  2085. freq = p2p_get_oper_freq(wpa_s->global->p2p,
  2086. wpa_s->pending_join_iface_addr);
  2087. }
  2088. if (freq >= 0) {
  2089. wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
  2090. "from P2P peer table: %d MHz", freq);
  2091. }
  2092. bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
  2093. if (bss) {
  2094. freq = bss->freq;
  2095. wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
  2096. "from BSS table: %d MHz", freq);
  2097. }
  2098. if (freq > 0) {
  2099. u16 method;
  2100. wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
  2101. "prior to joining an existing group (GO " MACSTR
  2102. " freq=%u MHz)",
  2103. MAC2STR(wpa_s->pending_join_dev_addr), freq);
  2104. wpa_s->pending_pd_before_join = 1;
  2105. switch (wpa_s->pending_join_wps_method) {
  2106. case WPS_PIN_DISPLAY:
  2107. method = WPS_CONFIG_KEYPAD;
  2108. break;
  2109. case WPS_PIN_KEYPAD:
  2110. method = WPS_CONFIG_DISPLAY;
  2111. break;
  2112. case WPS_PBC:
  2113. method = WPS_CONFIG_PUSHBUTTON;
  2114. break;
  2115. default:
  2116. method = 0;
  2117. break;
  2118. }
  2119. if (p2p_prov_disc_req(wpa_s->global->p2p,
  2120. wpa_s->pending_join_dev_addr, method, 1)
  2121. < 0) {
  2122. wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
  2123. "Discovery Request before joining an "
  2124. "existing group");
  2125. wpa_s->pending_pd_before_join = 0;
  2126. goto start;
  2127. }
  2128. /*
  2129. * Actual join operation will be started from the Action frame
  2130. * TX status callback.
  2131. */
  2132. return;
  2133. }
  2134. wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
  2135. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2136. eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
  2137. wpas_p2p_check_join_scan_limit(wpa_s);
  2138. return;
  2139. start:
  2140. /* Start join operation immediately */
  2141. wpas_p2p_join_start(wpa_s);
  2142. }
  2143. static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
  2144. {
  2145. struct wpa_supplicant *wpa_s = eloop_ctx;
  2146. int ret;
  2147. struct wpa_driver_scan_params params;
  2148. struct wpabuf *wps_ie, *ies;
  2149. size_t ielen;
  2150. os_memset(&params, 0, sizeof(params));
  2151. /* P2P Wildcard SSID */
  2152. params.num_ssids = 1;
  2153. params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
  2154. params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
  2155. wpa_s->wps->dev.p2p = 1;
  2156. wps_ie = wps_build_probe_req_ie(0, &wpa_s->wps->dev, wpa_s->wps->uuid,
  2157. WPS_REQ_ENROLLEE, 0, NULL);
  2158. if (wps_ie == NULL) {
  2159. wpas_p2p_scan_res_join(wpa_s, NULL);
  2160. return;
  2161. }
  2162. ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
  2163. ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
  2164. if (ies == NULL) {
  2165. wpabuf_free(wps_ie);
  2166. wpas_p2p_scan_res_join(wpa_s, NULL);
  2167. return;
  2168. }
  2169. wpabuf_put_buf(ies, wps_ie);
  2170. wpabuf_free(wps_ie);
  2171. p2p_scan_ie(wpa_s->global->p2p, ies);
  2172. params.p2p_probe = 1;
  2173. params.extra_ies = wpabuf_head(ies);
  2174. params.extra_ies_len = wpabuf_len(ies);
  2175. /*
  2176. * Run a scan to update BSS table and start Provision Discovery once
  2177. * the new scan results become available.
  2178. */
  2179. wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
  2180. ret = wpa_drv_scan(wpa_s, &params);
  2181. wpabuf_free(ies);
  2182. if (ret) {
  2183. wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
  2184. "try again later");
  2185. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2186. eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
  2187. wpas_p2p_check_join_scan_limit(wpa_s);
  2188. }
  2189. }
  2190. static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
  2191. const u8 *dev_addr, enum p2p_wps_method wps_method)
  2192. {
  2193. wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
  2194. MACSTR " dev " MACSTR ")",
  2195. MAC2STR(iface_addr), MAC2STR(dev_addr));
  2196. os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
  2197. os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
  2198. wpa_s->pending_join_wps_method = wps_method;
  2199. /* Make sure we are not running find during connection establishment */
  2200. wpas_p2p_stop_find(wpa_s);
  2201. wpa_s->p2p_join_scan_count = 0;
  2202. wpas_p2p_join_scan(wpa_s, NULL);
  2203. return 0;
  2204. }
  2205. static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
  2206. {
  2207. struct wpa_supplicant *group;
  2208. struct p2p_go_neg_results res;
  2209. group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
  2210. if (group == NULL)
  2211. return -1;
  2212. if (group != wpa_s) {
  2213. os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
  2214. sizeof(group->p2p_pin));
  2215. group->p2p_wps_method = wpa_s->p2p_wps_method;
  2216. }
  2217. group->p2p_in_provisioning = 1;
  2218. os_memset(&res, 0, sizeof(res));
  2219. os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
  2220. ETH_ALEN);
  2221. res.wps_method = wpa_s->pending_join_wps_method;
  2222. wpas_start_wps_enrollee(group, &res);
  2223. /*
  2224. * Allow a longer timeout for join-a-running-group than normal 15
  2225. * second group formation timeout since the GO may not have authorized
  2226. * our connection yet.
  2227. */
  2228. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  2229. eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
  2230. wpa_s, NULL);
  2231. return 0;
  2232. }
  2233. /**
  2234. * wpas_p2p_connect - Request P2P Group Formation to be started
  2235. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2236. * @peer_addr: Address of the peer P2P Device
  2237. * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
  2238. * @persistent_group: Whether to create a persistent group
  2239. * @join: Whether to join an existing group (as a client) instead of starting
  2240. * Group Owner negotiation; @peer_addr is BSSID in that case
  2241. * @auth: Whether to only authorize the connection instead of doing that and
  2242. * initiating Group Owner negotiation
  2243. * @go_intent: GO Intent or -1 to use default
  2244. * @freq: Frequency for the group or 0 for auto-selection
  2245. * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
  2246. * failure, -2 on failure due to channel not currently available,
  2247. * -3 if forced channel is not supported
  2248. */
  2249. int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  2250. const char *pin, enum p2p_wps_method wps_method,
  2251. int persistent_group, int join, int auth, int go_intent,
  2252. int freq)
  2253. {
  2254. int force_freq = 0, oper_freq = 0;
  2255. u8 bssid[ETH_ALEN];
  2256. int ret = 0;
  2257. enum wpa_driver_if_type iftype;
  2258. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  2259. return -1;
  2260. if (go_intent < 0)
  2261. go_intent = wpa_s->conf->p2p_go_intent;
  2262. if (!auth)
  2263. wpa_s->p2p_long_listen = 0;
  2264. wpa_s->p2p_wps_method = wps_method;
  2265. if (pin)
  2266. os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
  2267. else if (wps_method == WPS_PIN_DISPLAY) {
  2268. ret = wps_generate_pin();
  2269. os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
  2270. ret);
  2271. wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
  2272. wpa_s->p2p_pin);
  2273. } else
  2274. wpa_s->p2p_pin[0] = '\0';
  2275. if (join) {
  2276. u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
  2277. if (auth) {
  2278. wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
  2279. "connect a running group from " MACSTR,
  2280. MAC2STR(peer_addr));
  2281. os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
  2282. return ret;
  2283. }
  2284. os_memcpy(dev_addr, peer_addr, ETH_ALEN);
  2285. if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
  2286. iface_addr) < 0) {
  2287. os_memcpy(iface_addr, peer_addr, ETH_ALEN);
  2288. p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
  2289. dev_addr);
  2290. }
  2291. if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method) <
  2292. 0)
  2293. return -1;
  2294. return ret;
  2295. }
  2296. if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
  2297. wpa_s->assoc_freq)
  2298. oper_freq = wpa_s->assoc_freq;
  2299. else {
  2300. oper_freq = wpa_drv_shared_freq(wpa_s);
  2301. if (oper_freq < 0)
  2302. oper_freq = 0;
  2303. }
  2304. if (freq > 0) {
  2305. if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
  2306. wpa_printf(MSG_DEBUG, "P2P: The forced channel "
  2307. "(%u MHz) is not supported for P2P uses",
  2308. freq);
  2309. return -3;
  2310. }
  2311. if (oper_freq > 0 && freq != oper_freq &&
  2312. !(wpa_s->drv_flags &
  2313. WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
  2314. wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
  2315. "on %u MHz while connected on another "
  2316. "channel (%u MHz)", freq, oper_freq);
  2317. return -2;
  2318. }
  2319. wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
  2320. "requested channel (%u MHz)", freq);
  2321. force_freq = freq;
  2322. } else if (oper_freq > 0 &&
  2323. !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
  2324. if (!(wpa_s->drv_flags &
  2325. WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
  2326. wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
  2327. "while connected on non-P2P supported "
  2328. "channel (%u MHz)", oper_freq);
  2329. return -2;
  2330. }
  2331. wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
  2332. "(%u MHz) not available for P2P - try to use "
  2333. "another channel", oper_freq);
  2334. force_freq = 0;
  2335. } else if (oper_freq > 0) {
  2336. wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
  2337. "channel we are already using (%u MHz) on another "
  2338. "interface", oper_freq);
  2339. force_freq = oper_freq;
  2340. }
  2341. wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
  2342. if (!wpa_s->create_p2p_iface) {
  2343. if (auth) {
  2344. if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
  2345. go_intent, wpa_s->own_addr,
  2346. force_freq, persistent_group)
  2347. < 0)
  2348. return -1;
  2349. return ret;
  2350. }
  2351. if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
  2352. go_intent, wpa_s->own_addr,
  2353. force_freq, persistent_group) < 0)
  2354. return -1;
  2355. return ret;
  2356. }
  2357. /* Prepare to add a new interface for the group */
  2358. iftype = WPA_IF_P2P_GROUP;
  2359. if (go_intent == 15)
  2360. iftype = WPA_IF_P2P_GO;
  2361. if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
  2362. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  2363. "interface for the group");
  2364. return -1;
  2365. }
  2366. if (auth) {
  2367. if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
  2368. go_intent,
  2369. wpa_s->pending_interface_addr,
  2370. force_freq, persistent_group) < 0)
  2371. return -1;
  2372. return ret;
  2373. }
  2374. if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method, go_intent,
  2375. wpa_s->pending_interface_addr,
  2376. force_freq, persistent_group) < 0) {
  2377. wpas_p2p_remove_pending_group_interface(wpa_s);
  2378. return -1;
  2379. }
  2380. return ret;
  2381. }
  2382. /**
  2383. * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
  2384. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2385. * @freq: Frequency of the channel in MHz
  2386. * @duration: Duration of the stay on the channel in milliseconds
  2387. *
  2388. * This callback is called when the driver indicates that it has started the
  2389. * requested remain-on-channel duration.
  2390. */
  2391. void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  2392. unsigned int freq, unsigned int duration)
  2393. {
  2394. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  2395. return;
  2396. if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
  2397. p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
  2398. wpa_s->pending_listen_duration);
  2399. wpa_s->pending_listen_freq = 0;
  2400. }
  2401. }
  2402. static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
  2403. unsigned int timeout)
  2404. {
  2405. /* Limit maximum Listen state time based on driver limitation. */
  2406. if (timeout > wpa_s->max_remain_on_chan)
  2407. timeout = wpa_s->max_remain_on_chan;
  2408. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  2409. return wpa_drv_p2p_listen(wpa_s, timeout);
  2410. return p2p_listen(wpa_s->global->p2p, timeout);
  2411. }
  2412. /**
  2413. * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
  2414. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2415. * @freq: Frequency of the channel in MHz
  2416. *
  2417. * This callback is called when the driver indicates that a remain-on-channel
  2418. * operation has been completed, i.e., the duration on the requested channel
  2419. * has timed out.
  2420. */
  2421. void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  2422. unsigned int freq)
  2423. {
  2424. wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
  2425. "(p2p_long_listen=%d ms pending_action_tx=%p)",
  2426. wpa_s->p2p_long_listen, wpa_s->pending_action_tx);
  2427. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  2428. return;
  2429. if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
  2430. return; /* P2P module started a new operation */
  2431. if (wpa_s->pending_action_tx)
  2432. return;
  2433. if (wpa_s->p2p_long_listen > 0)
  2434. wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
  2435. if (wpa_s->p2p_long_listen > 0) {
  2436. wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
  2437. wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
  2438. }
  2439. }
  2440. /**
  2441. * wpas_p2p_group_remove - Remove a P2P group
  2442. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2443. * @ifname: Network interface name of the group interface or "*" to remove all
  2444. * groups
  2445. * Returns: 0 on success, -1 on failure
  2446. *
  2447. * This function is used to remove a P2P group. This can be used to disconnect
  2448. * from a group in which the local end is a P2P Client or to end a P2P Group in
  2449. * case the local end is the Group Owner. If a virtual network interface was
  2450. * created for this group, that interface will be removed. Otherwise, only the
  2451. * configured P2P group network will be removed from the interface.
  2452. */
  2453. int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
  2454. {
  2455. struct wpa_global *global = wpa_s->global;
  2456. if (os_strcmp(ifname, "*") == 0) {
  2457. struct wpa_supplicant *prev;
  2458. wpa_s = global->ifaces;
  2459. while (wpa_s) {
  2460. prev = wpa_s;
  2461. wpa_s = wpa_s->next;
  2462. wpas_p2p_disconnect(prev);
  2463. }
  2464. return 0;
  2465. }
  2466. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2467. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  2468. break;
  2469. }
  2470. return wpas_p2p_disconnect(wpa_s);
  2471. }
  2472. static void wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
  2473. struct p2p_go_neg_results *params,
  2474. int freq)
  2475. {
  2476. u8 bssid[ETH_ALEN];
  2477. int res;
  2478. os_memset(params, 0, sizeof(*params));
  2479. params->role_go = 1;
  2480. if (freq) {
  2481. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
  2482. "frequency %d MHz", freq);
  2483. params->freq = freq;
  2484. } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
  2485. wpa_s->conf->p2p_oper_channel >= 1 &&
  2486. wpa_s->conf->p2p_oper_channel <= 11) {
  2487. params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
  2488. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
  2489. "frequency %d MHz", params->freq);
  2490. } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
  2491. wpa_s->conf->p2p_oper_reg_class == 124) {
  2492. params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
  2493. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
  2494. "frequency %d MHz", params->freq);
  2495. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  2496. wpa_s->best_overall_freq > 0 &&
  2497. p2p_supported_freq(wpa_s->global->p2p,
  2498. wpa_s->best_overall_freq)) {
  2499. params->freq = wpa_s->best_overall_freq;
  2500. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
  2501. "channel %d MHz", params->freq);
  2502. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  2503. wpa_s->best_24_freq > 0 &&
  2504. p2p_supported_freq(wpa_s->global->p2p,
  2505. wpa_s->best_24_freq)) {
  2506. params->freq = wpa_s->best_24_freq;
  2507. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
  2508. "channel %d MHz", params->freq);
  2509. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  2510. wpa_s->best_5_freq > 0 &&
  2511. p2p_supported_freq(wpa_s->global->p2p,
  2512. wpa_s->best_5_freq)) {
  2513. params->freq = wpa_s->best_5_freq;
  2514. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
  2515. "channel %d MHz", params->freq);
  2516. } else {
  2517. params->freq = 2412;
  2518. wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
  2519. "known)", params->freq);
  2520. }
  2521. if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
  2522. wpa_s->assoc_freq && !freq) {
  2523. wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
  2524. "already using");
  2525. params->freq = wpa_s->assoc_freq;
  2526. }
  2527. res = wpa_drv_shared_freq(wpa_s);
  2528. if (res > 0 && !freq) {
  2529. wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
  2530. "already using on a shared interface");
  2531. params->freq = res;
  2532. }
  2533. }
  2534. static struct wpa_supplicant *
  2535. wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
  2536. int go)
  2537. {
  2538. struct wpa_supplicant *group_wpa_s;
  2539. if (!wpas_p2p_create_iface(wpa_s))
  2540. return wpa_s;
  2541. if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
  2542. WPA_IF_P2P_CLIENT) < 0)
  2543. return NULL;
  2544. group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
  2545. if (group_wpa_s == NULL) {
  2546. wpas_p2p_remove_pending_group_interface(wpa_s);
  2547. return NULL;
  2548. }
  2549. return group_wpa_s;
  2550. }
  2551. /**
  2552. * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
  2553. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2554. * @persistent_group: Whether to create a persistent group
  2555. * @freq: Frequency for the group or 0 to indicate no hardcoding
  2556. * Returns: 0 on success, -1 on failure
  2557. *
  2558. * This function creates a new P2P group with the local end as the Group Owner,
  2559. * i.e., without using Group Owner Negotiation.
  2560. */
  2561. int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
  2562. int freq)
  2563. {
  2564. struct p2p_go_neg_results params;
  2565. unsigned int r;
  2566. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  2567. return -1;
  2568. /* Make sure we are not running find during connection establishment */
  2569. wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
  2570. wpas_p2p_stop_find(wpa_s);
  2571. if (freq == 2) {
  2572. wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
  2573. "band");
  2574. if (wpa_s->best_24_freq > 0 &&
  2575. p2p_supported_freq(wpa_s->global->p2p,
  2576. wpa_s->best_24_freq)) {
  2577. freq = wpa_s->best_24_freq;
  2578. wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
  2579. "channel: %d MHz", freq);
  2580. } else {
  2581. os_get_random((u8 *) &r, sizeof(r));
  2582. freq = 2412 + (r % 3) * 25;
  2583. wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
  2584. "channel: %d MHz", freq);
  2585. }
  2586. }
  2587. if (freq == 5) {
  2588. wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
  2589. "band");
  2590. if (wpa_s->best_5_freq > 0 &&
  2591. p2p_supported_freq(wpa_s->global->p2p,
  2592. wpa_s->best_5_freq)) {
  2593. freq = wpa_s->best_5_freq;
  2594. wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
  2595. "channel: %d MHz", freq);
  2596. } else {
  2597. os_get_random((u8 *) &r, sizeof(r));
  2598. freq = 5180 + (r % 4) * 20;
  2599. if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
  2600. wpa_printf(MSG_DEBUG, "P2P: Could not select "
  2601. "5 GHz channel for P2P group");
  2602. return -1;
  2603. }
  2604. wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
  2605. "channel: %d MHz", freq);
  2606. }
  2607. }
  2608. if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
  2609. wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
  2610. "(%u MHz) is not supported for P2P uses",
  2611. freq);
  2612. return -1;
  2613. }
  2614. wpas_p2p_init_go_params(wpa_s, &params, freq);
  2615. p2p_go_params(wpa_s->global->p2p, &params);
  2616. params.persistent_group = persistent_group;
  2617. wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
  2618. if (wpa_s == NULL)
  2619. return -1;
  2620. wpas_start_wps_go(wpa_s, &params, 0);
  2621. return 0;
  2622. }
  2623. static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
  2624. struct wpa_ssid *params, int addr_allocated)
  2625. {
  2626. struct wpa_ssid *ssid;
  2627. wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
  2628. if (wpa_s == NULL)
  2629. return -1;
  2630. wpa_supplicant_ap_deinit(wpa_s);
  2631. ssid = wpa_config_add_network(wpa_s->conf);
  2632. if (ssid == NULL)
  2633. return -1;
  2634. wpa_config_set_network_defaults(ssid);
  2635. ssid->temporary = 1;
  2636. ssid->proto = WPA_PROTO_RSN;
  2637. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  2638. ssid->group_cipher = WPA_CIPHER_CCMP;
  2639. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  2640. ssid->ssid = os_malloc(params->ssid_len);
  2641. if (ssid->ssid == NULL) {
  2642. wpa_config_remove_network(wpa_s->conf, ssid->id);
  2643. return -1;
  2644. }
  2645. os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
  2646. ssid->ssid_len = params->ssid_len;
  2647. ssid->p2p_group = 1;
  2648. ssid->export_keys = 1;
  2649. if (params->psk_set) {
  2650. os_memcpy(ssid->psk, params->psk, 32);
  2651. ssid->psk_set = 1;
  2652. }
  2653. if (params->passphrase)
  2654. ssid->passphrase = os_strdup(params->passphrase);
  2655. wpa_supplicant_select_network(wpa_s, ssid);
  2656. wpa_s->show_group_started = 1;
  2657. return 0;
  2658. }
  2659. int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
  2660. struct wpa_ssid *ssid, int addr_allocated,
  2661. int freq)
  2662. {
  2663. struct p2p_go_neg_results params;
  2664. int go = 0;
  2665. if (ssid->disabled != 2 || ssid->ssid == NULL)
  2666. return -1;
  2667. if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
  2668. go == (ssid->mode == WPAS_MODE_P2P_GO)) {
  2669. wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
  2670. "already running");
  2671. return 0;
  2672. }
  2673. /* Make sure we are not running find during connection establishment */
  2674. wpas_p2p_stop_find(wpa_s);
  2675. if (ssid->mode == WPAS_MODE_INFRA)
  2676. return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
  2677. if (ssid->mode != WPAS_MODE_P2P_GO)
  2678. return -1;
  2679. wpas_p2p_init_go_params(wpa_s, &params, freq);
  2680. params.role_go = 1;
  2681. if (ssid->passphrase == NULL ||
  2682. os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
  2683. wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
  2684. "group");
  2685. return -1;
  2686. }
  2687. os_strlcpy(params.passphrase, ssid->passphrase,
  2688. sizeof(params.passphrase));
  2689. os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
  2690. params.ssid_len = ssid->ssid_len;
  2691. params.persistent_group = 1;
  2692. wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
  2693. if (wpa_s == NULL)
  2694. return -1;
  2695. wpas_start_wps_go(wpa_s, &params, 0);
  2696. return 0;
  2697. }
  2698. static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
  2699. struct wpabuf *proberesp_ies)
  2700. {
  2701. struct wpa_supplicant *wpa_s = ctx;
  2702. if (wpa_s->ap_iface) {
  2703. struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
  2704. if (beacon_ies) {
  2705. wpabuf_free(hapd->p2p_beacon_ie);
  2706. hapd->p2p_beacon_ie = beacon_ies;
  2707. }
  2708. wpabuf_free(hapd->p2p_probe_resp_ie);
  2709. hapd->p2p_probe_resp_ie = proberesp_ies;
  2710. } else {
  2711. wpabuf_free(beacon_ies);
  2712. wpabuf_free(proberesp_ies);
  2713. }
  2714. wpa_supplicant_ap_update_beacon(wpa_s);
  2715. }
  2716. static void wpas_p2p_idle_update(void *ctx, int idle)
  2717. {
  2718. struct wpa_supplicant *wpa_s = ctx;
  2719. if (!wpa_s->ap_iface)
  2720. return;
  2721. wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
  2722. if (idle)
  2723. wpas_p2p_set_group_idle_timeout(wpa_s);
  2724. else
  2725. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  2726. }
  2727. struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
  2728. int persistent_group,
  2729. int group_formation)
  2730. {
  2731. struct p2p_group *group;
  2732. struct p2p_group_config *cfg;
  2733. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  2734. return NULL;
  2735. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  2736. return NULL;
  2737. cfg = os_zalloc(sizeof(*cfg));
  2738. if (cfg == NULL)
  2739. return NULL;
  2740. if (persistent_group && wpa_s->conf->persistent_reconnect)
  2741. cfg->persistent_group = 2;
  2742. else if (persistent_group)
  2743. cfg->persistent_group = 1;
  2744. os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
  2745. if (wpa_s->max_stations &&
  2746. wpa_s->max_stations < wpa_s->conf->max_num_sta)
  2747. cfg->max_clients = wpa_s->max_stations;
  2748. else
  2749. cfg->max_clients = wpa_s->conf->max_num_sta;
  2750. cfg->cb_ctx = wpa_s;
  2751. cfg->ie_update = wpas_p2p_ie_update;
  2752. cfg->idle_update = wpas_p2p_idle_update;
  2753. group = p2p_group_init(wpa_s->global->p2p, cfg);
  2754. if (group == NULL)
  2755. os_free(cfg);
  2756. if (!group_formation)
  2757. p2p_group_notif_formation_done(group);
  2758. wpa_s->p2p_group = group;
  2759. return group;
  2760. }
  2761. void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  2762. int registrar)
  2763. {
  2764. if (!wpa_s->p2p_in_provisioning) {
  2765. wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
  2766. "provisioning not in progress");
  2767. return;
  2768. }
  2769. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
  2770. NULL);
  2771. if (wpa_s->global->p2p)
  2772. p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
  2773. else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  2774. wpa_drv_wps_success_cb(wpa_s, peer_addr);
  2775. wpas_group_formation_completed(wpa_s, 1);
  2776. }
  2777. void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
  2778. struct wps_event_fail *fail)
  2779. {
  2780. if (!wpa_s->p2p_in_provisioning) {
  2781. wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
  2782. "provisioning not in progress");
  2783. return;
  2784. }
  2785. wpas_notify_p2p_wps_failed(wpa_s, fail);
  2786. }
  2787. int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  2788. const char *config_method)
  2789. {
  2790. u16 config_methods;
  2791. if (os_strcmp(config_method, "display") == 0)
  2792. config_methods = WPS_CONFIG_DISPLAY;
  2793. else if (os_strcmp(config_method, "keypad") == 0)
  2794. config_methods = WPS_CONFIG_KEYPAD;
  2795. else if (os_strcmp(config_method, "pbc") == 0 ||
  2796. os_strcmp(config_method, "pushbutton") == 0)
  2797. config_methods = WPS_CONFIG_PUSHBUTTON;
  2798. else
  2799. return -1;
  2800. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  2801. return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
  2802. config_methods);
  2803. }
  2804. if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
  2805. return -1;
  2806. return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
  2807. config_methods, 0);
  2808. }
  2809. int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
  2810. char *end)
  2811. {
  2812. return p2p_scan_result_text(ies, ies_len, buf, end);
  2813. }
  2814. static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
  2815. {
  2816. if (!wpa_s->pending_action_tx)
  2817. return;
  2818. wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
  2819. "operation request");
  2820. wpabuf_free(wpa_s->pending_action_tx);
  2821. wpa_s->pending_action_tx = NULL;
  2822. }
  2823. int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
  2824. enum p2p_discovery_type type,
  2825. unsigned int num_req_dev_types, const u8 *req_dev_types)
  2826. {
  2827. wpas_p2p_clear_pending_action_tx(wpa_s);
  2828. wpa_s->p2p_long_listen = 0;
  2829. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  2830. return wpa_drv_p2p_find(wpa_s, timeout, type);
  2831. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  2832. return -1;
  2833. return p2p_find(wpa_s->global->p2p, timeout, type,
  2834. num_req_dev_types, req_dev_types);
  2835. }
  2836. void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
  2837. {
  2838. wpas_p2p_clear_pending_action_tx(wpa_s);
  2839. wpa_s->p2p_long_listen = 0;
  2840. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  2841. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2842. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  2843. wpa_drv_p2p_stop_find(wpa_s);
  2844. return;
  2845. }
  2846. if (wpa_s->global->p2p)
  2847. p2p_stop_find(wpa_s->global->p2p);
  2848. wpas_p2p_remove_pending_group_interface(wpa_s);
  2849. }
  2850. static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
  2851. {
  2852. struct wpa_supplicant *wpa_s = eloop_ctx;
  2853. wpa_s->p2p_long_listen = 0;
  2854. }
  2855. int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
  2856. {
  2857. int res;
  2858. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  2859. return -1;
  2860. wpas_p2p_clear_pending_action_tx(wpa_s);
  2861. if (timeout == 0) {
  2862. /*
  2863. * This is a request for unlimited Listen state. However, at
  2864. * least for now, this is mapped to a Listen state for one
  2865. * hour.
  2866. */
  2867. timeout = 3600;
  2868. }
  2869. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  2870. wpa_s->p2p_long_listen = 0;
  2871. res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
  2872. if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
  2873. wpa_s->p2p_long_listen = timeout * 1000;
  2874. eloop_register_timeout(timeout, 0,
  2875. wpas_p2p_long_listen_timeout,
  2876. wpa_s, NULL);
  2877. }
  2878. return res;
  2879. }
  2880. int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  2881. u8 *buf, size_t len, int p2p_group)
  2882. {
  2883. struct wpabuf *p2p_ie;
  2884. int ret;
  2885. if (wpa_s->global->p2p_disabled)
  2886. return -1;
  2887. if (wpa_s->global->p2p == NULL)
  2888. return -1;
  2889. if (bss == NULL)
  2890. return -1;
  2891. p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  2892. ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
  2893. p2p_group, p2p_ie);
  2894. wpabuf_free(p2p_ie);
  2895. return ret;
  2896. }
  2897. int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
  2898. const u8 *dst, const u8 *bssid,
  2899. const u8 *ie, size_t ie_len)
  2900. {
  2901. if (wpa_s->global->p2p_disabled)
  2902. return 0;
  2903. if (wpa_s->global->p2p == NULL)
  2904. return 0;
  2905. return p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
  2906. ie, ie_len);
  2907. }
  2908. void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
  2909. const u8 *sa, const u8 *bssid,
  2910. u8 category, const u8 *data, size_t len, int freq)
  2911. {
  2912. if (wpa_s->global->p2p_disabled)
  2913. return;
  2914. if (wpa_s->global->p2p == NULL)
  2915. return;
  2916. p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
  2917. freq);
  2918. }
  2919. void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
  2920. {
  2921. if (wpa_s->global->p2p_disabled)
  2922. return;
  2923. if (wpa_s->global->p2p == NULL)
  2924. return;
  2925. p2p_scan_ie(wpa_s->global->p2p, ies);
  2926. }
  2927. void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
  2928. {
  2929. p2p_group_deinit(wpa_s->p2p_group);
  2930. wpa_s->p2p_group = NULL;
  2931. wpa_s->ap_configured_cb = NULL;
  2932. wpa_s->ap_configured_cb_ctx = NULL;
  2933. wpa_s->ap_configured_cb_data = NULL;
  2934. wpa_s->connect_without_scan = NULL;
  2935. }
  2936. int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
  2937. {
  2938. wpa_s->p2p_long_listen = 0;
  2939. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  2940. return wpa_drv_p2p_reject(wpa_s, addr);
  2941. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  2942. return -1;
  2943. return p2p_reject(wpa_s->global->p2p, addr);
  2944. }
  2945. /* Invite to reinvoke a persistent group */
  2946. int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  2947. struct wpa_ssid *ssid, const u8 *go_dev_addr)
  2948. {
  2949. enum p2p_invite_role role;
  2950. u8 *bssid = NULL;
  2951. if (ssid->mode == WPAS_MODE_P2P_GO) {
  2952. role = P2P_INVITE_ROLE_GO;
  2953. if (peer_addr == NULL) {
  2954. wpa_printf(MSG_DEBUG, "P2P: Missing peer "
  2955. "address in invitation command");
  2956. return -1;
  2957. }
  2958. if (wpas_p2p_create_iface(wpa_s)) {
  2959. if (wpas_p2p_add_group_interface(wpa_s,
  2960. WPA_IF_P2P_GO) < 0) {
  2961. wpa_printf(MSG_ERROR, "P2P: Failed to "
  2962. "allocate a new interface for the "
  2963. "group");
  2964. return -1;
  2965. }
  2966. bssid = wpa_s->pending_interface_addr;
  2967. } else
  2968. bssid = wpa_s->own_addr;
  2969. } else {
  2970. role = P2P_INVITE_ROLE_CLIENT;
  2971. peer_addr = ssid->bssid;
  2972. }
  2973. wpa_s->pending_invite_ssid_id = ssid->id;
  2974. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  2975. return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
  2976. ssid->ssid, ssid->ssid_len,
  2977. go_dev_addr, 1);
  2978. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  2979. return -1;
  2980. return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
  2981. ssid->ssid, ssid->ssid_len, 0, go_dev_addr, 1);
  2982. }
  2983. /* Invite to join an active group */
  2984. int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
  2985. const u8 *peer_addr, const u8 *go_dev_addr)
  2986. {
  2987. struct wpa_global *global = wpa_s->global;
  2988. enum p2p_invite_role role;
  2989. u8 *bssid = NULL;
  2990. struct wpa_ssid *ssid;
  2991. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2992. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  2993. break;
  2994. }
  2995. if (wpa_s == NULL) {
  2996. wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
  2997. return -1;
  2998. }
  2999. ssid = wpa_s->current_ssid;
  3000. if (ssid == NULL) {
  3001. wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
  3002. "invitation");
  3003. return -1;
  3004. }
  3005. if (ssid->mode == WPAS_MODE_P2P_GO) {
  3006. role = P2P_INVITE_ROLE_ACTIVE_GO;
  3007. bssid = wpa_s->own_addr;
  3008. if (go_dev_addr == NULL)
  3009. go_dev_addr = wpa_s->parent->own_addr;
  3010. } else {
  3011. role = P2P_INVITE_ROLE_CLIENT;
  3012. if (wpa_s->wpa_state < WPA_ASSOCIATED) {
  3013. wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
  3014. "invite to current group");
  3015. return -1;
  3016. }
  3017. bssid = wpa_s->bssid;
  3018. if (go_dev_addr == NULL &&
  3019. !is_zero_ether_addr(wpa_s->go_dev_addr))
  3020. go_dev_addr = wpa_s->go_dev_addr;
  3021. }
  3022. wpa_s->parent->pending_invite_ssid_id = -1;
  3023. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3024. return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
  3025. ssid->ssid, ssid->ssid_len,
  3026. go_dev_addr, 0);
  3027. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3028. return -1;
  3029. return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
  3030. ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
  3031. go_dev_addr, 0);
  3032. }
  3033. void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
  3034. {
  3035. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3036. const char *ssid_txt;
  3037. u8 go_dev_addr[ETH_ALEN];
  3038. int network_id = -1;
  3039. int persistent;
  3040. int freq;
  3041. if (!wpa_s->show_group_started || !ssid)
  3042. return;
  3043. wpa_s->show_group_started = 0;
  3044. ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
  3045. os_memset(go_dev_addr, 0, ETH_ALEN);
  3046. if (ssid->bssid_set)
  3047. os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
  3048. persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
  3049. ssid->ssid_len);
  3050. os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
  3051. if (wpa_s->global->p2p_group_formation == wpa_s)
  3052. wpa_s->global->p2p_group_formation = NULL;
  3053. freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
  3054. (int) wpa_s->assoc_freq;
  3055. if (ssid->passphrase == NULL && ssid->psk_set) {
  3056. char psk[65];
  3057. wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
  3058. wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  3059. "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
  3060. MACSTR "%s",
  3061. wpa_s->ifname, ssid_txt, freq, psk,
  3062. MAC2STR(go_dev_addr),
  3063. persistent ? " [PERSISTENT]" : "");
  3064. } else {
  3065. wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  3066. "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
  3067. "go_dev_addr=" MACSTR "%s",
  3068. wpa_s->ifname, ssid_txt, freq,
  3069. ssid->passphrase ? ssid->passphrase : "",
  3070. MAC2STR(go_dev_addr),
  3071. persistent ? " [PERSISTENT]" : "");
  3072. }
  3073. if (persistent)
  3074. network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
  3075. ssid, go_dev_addr);
  3076. if (network_id < 0)
  3077. network_id = ssid->id;
  3078. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
  3079. }
  3080. int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
  3081. u32 interval1, u32 duration2, u32 interval2)
  3082. {
  3083. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3084. return -1;
  3085. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3086. return -1;
  3087. if (wpa_s->wpa_state < WPA_ASSOCIATED ||
  3088. wpa_s->current_ssid == NULL ||
  3089. wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
  3090. return -1;
  3091. return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
  3092. wpa_s->own_addr, wpa_s->assoc_freq,
  3093. duration1, interval1, duration2, interval2);
  3094. }
  3095. int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
  3096. unsigned int interval)
  3097. {
  3098. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3099. return -1;
  3100. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3101. return -1;
  3102. return p2p_ext_listen(wpa_s->global->p2p, period, interval);
  3103. }
  3104. static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
  3105. {
  3106. struct wpa_supplicant *wpa_s = eloop_ctx;
  3107. if (wpa_s->conf->p2p_group_idle == 0) {
  3108. wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
  3109. "disabled");
  3110. return;
  3111. }
  3112. wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
  3113. "group");
  3114. wpa_s->removal_reason = P2P_GROUP_REMOVAL_IDLE_TIMEOUT;
  3115. wpas_p2p_group_delete(wpa_s);
  3116. }
  3117. static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
  3118. {
  3119. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  3120. if (wpa_s->conf->p2p_group_idle == 0)
  3121. return;
  3122. if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
  3123. return;
  3124. wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
  3125. wpa_s->conf->p2p_group_idle);
  3126. eloop_register_timeout(wpa_s->conf->p2p_group_idle, 0,
  3127. wpas_p2p_group_idle_timeout, wpa_s, NULL);
  3128. }
  3129. void wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
  3130. u16 reason_code, const u8 *ie, size_t ie_len)
  3131. {
  3132. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3133. return;
  3134. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3135. return;
  3136. p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
  3137. }
  3138. void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
  3139. u16 reason_code, const u8 *ie, size_t ie_len)
  3140. {
  3141. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3142. return;
  3143. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3144. return;
  3145. p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
  3146. }
  3147. void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
  3148. {
  3149. struct p2p_data *p2p = wpa_s->global->p2p;
  3150. if (p2p == NULL)
  3151. return;
  3152. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
  3153. return;
  3154. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
  3155. p2p_set_dev_name(p2p, wpa_s->conf->device_name);
  3156. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
  3157. p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
  3158. if (wpa_s->wps &&
  3159. (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
  3160. p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
  3161. if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
  3162. p2p_set_uuid(p2p, wpa_s->wps->uuid);
  3163. if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
  3164. p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
  3165. p2p_set_model_name(p2p, wpa_s->conf->model_name);
  3166. p2p_set_model_number(p2p, wpa_s->conf->model_number);
  3167. p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
  3168. }
  3169. if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
  3170. p2p_set_sec_dev_types(p2p,
  3171. (void *) wpa_s->conf->sec_device_type,
  3172. wpa_s->conf->num_sec_device_types);
  3173. if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
  3174. int i;
  3175. p2p_remove_wps_vendor_extensions(p2p);
  3176. for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
  3177. if (wpa_s->conf->wps_vendor_ext[i] == NULL)
  3178. continue;
  3179. p2p_add_wps_vendor_extension(
  3180. p2p, wpa_s->conf->wps_vendor_ext[i]);
  3181. }
  3182. }
  3183. if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
  3184. wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  3185. char country[3];
  3186. country[0] = wpa_s->conf->country[0];
  3187. country[1] = wpa_s->conf->country[1];
  3188. country[2] = 0x04;
  3189. p2p_set_country(p2p, country);
  3190. }
  3191. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
  3192. p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
  3193. wpa_s->conf->p2p_ssid_postfix ?
  3194. os_strlen(wpa_s->conf->p2p_ssid_postfix) :
  3195. 0);
  3196. }
  3197. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
  3198. p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
  3199. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
  3200. u8 reg_class, channel;
  3201. int ret;
  3202. unsigned int r;
  3203. if (wpa_s->conf->p2p_listen_reg_class &&
  3204. wpa_s->conf->p2p_listen_channel) {
  3205. reg_class = wpa_s->conf->p2p_listen_reg_class;
  3206. channel = wpa_s->conf->p2p_listen_channel;
  3207. } else {
  3208. reg_class = 81;
  3209. /*
  3210. * Pick one of the social channels randomly as the
  3211. * listen channel.
  3212. */
  3213. os_get_random((u8 *) &r, sizeof(r));
  3214. channel = 1 + (r % 3) * 5;
  3215. }
  3216. ret = p2p_set_listen_channel(p2p, reg_class, channel);
  3217. if (ret)
  3218. wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
  3219. "failed: %d", ret);
  3220. }
  3221. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
  3222. u8 op_reg_class, op_channel, cfg_op_channel;
  3223. int ret = 0;
  3224. unsigned int r;
  3225. if (wpa_s->conf->p2p_oper_reg_class &&
  3226. wpa_s->conf->p2p_oper_channel) {
  3227. op_reg_class = wpa_s->conf->p2p_oper_reg_class;
  3228. op_channel = wpa_s->conf->p2p_oper_channel;
  3229. cfg_op_channel = 1;
  3230. } else {
  3231. op_reg_class = 81;
  3232. /*
  3233. * Use random operation channel from (1, 6, 11)
  3234. *if no other preference is indicated.
  3235. */
  3236. os_get_random((u8 *) &r, sizeof(r));
  3237. op_channel = 1 + (r % 3) * 5;
  3238. cfg_op_channel = 0;
  3239. }
  3240. ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
  3241. cfg_op_channel);
  3242. if (ret)
  3243. wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
  3244. "failed: %d", ret);
  3245. }
  3246. }
  3247. int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
  3248. int duration)
  3249. {
  3250. if (!wpa_s->ap_iface)
  3251. return -1;
  3252. return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
  3253. duration);
  3254. }
  3255. int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
  3256. {
  3257. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3258. return -1;
  3259. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3260. return -1;
  3261. wpa_s->global->cross_connection = enabled;
  3262. p2p_set_cross_connect(wpa_s->global->p2p, enabled);
  3263. if (!enabled) {
  3264. struct wpa_supplicant *iface;
  3265. for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
  3266. {
  3267. if (iface->cross_connect_enabled == 0)
  3268. continue;
  3269. iface->cross_connect_enabled = 0;
  3270. iface->cross_connect_in_use = 0;
  3271. wpa_msg(iface->parent, MSG_INFO,
  3272. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  3273. iface->ifname, iface->cross_connect_uplink);
  3274. }
  3275. }
  3276. return 0;
  3277. }
  3278. static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
  3279. {
  3280. struct wpa_supplicant *iface;
  3281. if (!uplink->global->cross_connection)
  3282. return;
  3283. for (iface = uplink->global->ifaces; iface; iface = iface->next) {
  3284. if (!iface->cross_connect_enabled)
  3285. continue;
  3286. if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
  3287. 0)
  3288. continue;
  3289. if (iface->ap_iface == NULL)
  3290. continue;
  3291. if (iface->cross_connect_in_use)
  3292. continue;
  3293. iface->cross_connect_in_use = 1;
  3294. wpa_msg(iface->parent, MSG_INFO,
  3295. P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
  3296. iface->ifname, iface->cross_connect_uplink);
  3297. }
  3298. }
  3299. static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
  3300. {
  3301. struct wpa_supplicant *iface;
  3302. for (iface = uplink->global->ifaces; iface; iface = iface->next) {
  3303. if (!iface->cross_connect_enabled)
  3304. continue;
  3305. if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
  3306. 0)
  3307. continue;
  3308. if (!iface->cross_connect_in_use)
  3309. continue;
  3310. wpa_msg(iface->parent, MSG_INFO,
  3311. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  3312. iface->ifname, iface->cross_connect_uplink);
  3313. iface->cross_connect_in_use = 0;
  3314. }
  3315. }
  3316. void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
  3317. {
  3318. if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
  3319. wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
  3320. wpa_s->cross_connect_disallowed)
  3321. wpas_p2p_disable_cross_connect(wpa_s);
  3322. else
  3323. wpas_p2p_enable_cross_connect(wpa_s);
  3324. if (!wpa_s->ap_iface)
  3325. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  3326. }
  3327. void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
  3328. {
  3329. wpas_p2p_disable_cross_connect(wpa_s);
  3330. if (!wpa_s->ap_iface)
  3331. wpas_p2p_set_group_idle_timeout(wpa_s);
  3332. }
  3333. static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
  3334. {
  3335. struct wpa_supplicant *iface;
  3336. if (!wpa_s->global->cross_connection)
  3337. return;
  3338. for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
  3339. if (iface == wpa_s)
  3340. continue;
  3341. if (iface->drv_flags &
  3342. WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
  3343. continue;
  3344. if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
  3345. continue;
  3346. wpa_s->cross_connect_enabled = 1;
  3347. os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
  3348. sizeof(wpa_s->cross_connect_uplink));
  3349. wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
  3350. "%s to %s whenever uplink is available",
  3351. wpa_s->ifname, wpa_s->cross_connect_uplink);
  3352. if (iface->ap_iface || iface->current_ssid == NULL ||
  3353. iface->current_ssid->mode != WPAS_MODE_INFRA ||
  3354. iface->cross_connect_disallowed ||
  3355. iface->wpa_state != WPA_COMPLETED)
  3356. break;
  3357. wpa_s->cross_connect_in_use = 1;
  3358. wpa_msg(wpa_s->parent, MSG_INFO,
  3359. P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
  3360. wpa_s->ifname, wpa_s->cross_connect_uplink);
  3361. break;
  3362. }
  3363. }
  3364. int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
  3365. {
  3366. if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
  3367. !wpa_s->p2p_in_provisioning)
  3368. return 0; /* not P2P client operation */
  3369. wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
  3370. "session overlap");
  3371. if (wpa_s != wpa_s->parent)
  3372. wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
  3373. if (wpa_s->global->p2p)
  3374. p2p_group_formation_failed(wpa_s->global->p2p);
  3375. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  3376. wpa_s->parent, NULL);
  3377. wpas_group_formation_completed(wpa_s, 0);
  3378. return 1;
  3379. }
  3380. void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
  3381. {
  3382. struct p2p_channels chan;
  3383. if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
  3384. return;
  3385. os_memset(&chan, 0, sizeof(chan));
  3386. if (wpas_p2p_setup_channels(wpa_s, &chan)) {
  3387. wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
  3388. "channel list");
  3389. return;
  3390. }
  3391. p2p_update_channel_list(wpa_s->global->p2p, &chan);
  3392. }
  3393. int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
  3394. {
  3395. struct wpa_global *global = wpa_s->global;
  3396. int found = 0;
  3397. const u8 *peer;
  3398. if (global->p2p == NULL)
  3399. return -1;
  3400. wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
  3401. if (wpa_s->pending_interface_name[0] &&
  3402. !is_zero_ether_addr(wpa_s->pending_interface_addr))
  3403. found = 1;
  3404. peer = p2p_get_go_neg_peer(global->p2p);
  3405. if (peer) {
  3406. wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
  3407. MACSTR, MAC2STR(peer));
  3408. p2p_unauthorize(global->p2p, peer);
  3409. }
  3410. wpas_p2p_stop_find(wpa_s);
  3411. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3412. if (wpa_s == global->p2p_group_formation &&
  3413. (wpa_s->p2p_in_provisioning ||
  3414. wpa_s->parent->pending_interface_type ==
  3415. WPA_IF_P2P_CLIENT)) {
  3416. wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
  3417. "formation found - cancelling",
  3418. wpa_s->ifname);
  3419. found = 1;
  3420. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  3421. wpa_s->parent, NULL);
  3422. wpas_p2p_group_delete(wpa_s);
  3423. break;
  3424. }
  3425. }
  3426. if (!found) {
  3427. wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
  3428. return -1;
  3429. }
  3430. return 0;
  3431. }
  3432. void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
  3433. {
  3434. if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
  3435. return;
  3436. wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
  3437. "being available anymore");
  3438. wpa_s->removal_reason = P2P_GROUP_REMOVAL_UNAVAILABLE;
  3439. wpas_p2p_group_delete(wpa_s);
  3440. }
  3441. void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
  3442. int freq_24, int freq_5, int freq_overall)
  3443. {
  3444. struct p2p_data *p2p = wpa_s->global->p2p;
  3445. if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
  3446. return;
  3447. p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
  3448. }
  3449. int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
  3450. {
  3451. u8 peer[ETH_ALEN];
  3452. struct p2p_data *p2p = wpa_s->global->p2p;
  3453. if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
  3454. return -1;
  3455. if (hwaddr_aton(addr, peer))
  3456. return -1;
  3457. return p2p_unauthorize(p2p, peer);
  3458. }
  3459. /**
  3460. * wpas_p2p_disconnect - Disconnect from a P2P Group
  3461. * @wpa_s: Pointer to wpa_supplicant data
  3462. * Returns: 0 on success, -1 on failure
  3463. *
  3464. * This can be used to disconnect from a group in which the local end is a P2P
  3465. * Client or to end a P2P Group in case the local end is the Group Owner. If a
  3466. * virtual network interface was created for this group, that interface will be
  3467. * removed. Otherwise, only the configured P2P group network will be removed
  3468. * from the interface.
  3469. */
  3470. int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
  3471. {
  3472. if (wpa_s == NULL)
  3473. return -1;
  3474. wpa_s->removal_reason = P2P_GROUP_REMOVAL_REQUESTED;
  3475. wpas_p2p_group_delete(wpa_s);
  3476. return 0;
  3477. }
  3478. int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
  3479. {
  3480. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3481. return 0;
  3482. return p2p_in_progress(wpa_s->global->p2p);
  3483. }