p2p_supplicant.c 115 KB

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