p2p_supplicant.c 87 KB

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