p2p_supplicant.c 101 KB

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