p2p_supplicant.c 88 KB

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