p2p_supplicant.c 106 KB

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