p2p_supplicant.c 98 KB

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