p2p_supplicant.c 84 KB

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