p2p_supplicant.c 102 KB

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