p2p_supplicant.c 115 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225
  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,
  986. const struct p2p_peer_info *info,
  987. int new_device)
  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(info->p2p_device_addr),
  996. wps_dev_type_bin2str(info->pri_dev_type, devtype,
  997. sizeof(devtype)),
  998. info->device_name, info->config_methods,
  999. info->dev_capab, info->group_capab);
  1000. }
  1001. static int wpas_start_listen(void *ctx, unsigned int freq,
  1002. unsigned int duration,
  1003. const struct wpabuf *probe_resp_ie)
  1004. {
  1005. struct wpa_supplicant *wpa_s = ctx;
  1006. wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
  1007. if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
  1008. wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
  1009. "report received Probe Request frames");
  1010. return -1;
  1011. }
  1012. wpa_s->pending_listen_freq = freq;
  1013. wpa_s->pending_listen_duration = duration;
  1014. if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
  1015. wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
  1016. "to remain on channel (%u MHz) for Listen "
  1017. "state", freq);
  1018. wpa_s->pending_listen_freq = 0;
  1019. return -1;
  1020. }
  1021. wpa_s->off_channel_freq = 0;
  1022. wpa_s->roc_waiting_drv_freq = freq;
  1023. return 0;
  1024. }
  1025. static void wpas_stop_listen(void *ctx)
  1026. {
  1027. struct wpa_supplicant *wpa_s = ctx;
  1028. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  1029. wpa_drv_cancel_remain_on_channel(wpa_s);
  1030. wpa_s->off_channel_freq = 0;
  1031. wpa_s->roc_waiting_drv_freq = 0;
  1032. }
  1033. wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
  1034. wpa_drv_probe_req_report(wpa_s, 0);
  1035. }
  1036. static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
  1037. {
  1038. struct wpa_supplicant *wpa_s = ctx;
  1039. return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf));
  1040. }
  1041. static struct p2p_srv_bonjour *
  1042. wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
  1043. const struct wpabuf *query)
  1044. {
  1045. struct p2p_srv_bonjour *bsrv;
  1046. size_t len;
  1047. len = wpabuf_len(query);
  1048. dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
  1049. struct p2p_srv_bonjour, list) {
  1050. if (len == wpabuf_len(bsrv->query) &&
  1051. os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
  1052. len) == 0)
  1053. return bsrv;
  1054. }
  1055. return NULL;
  1056. }
  1057. static struct p2p_srv_upnp *
  1058. wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
  1059. const char *service)
  1060. {
  1061. struct p2p_srv_upnp *usrv;
  1062. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  1063. struct p2p_srv_upnp, list) {
  1064. if (version == usrv->version &&
  1065. os_strcmp(service, usrv->service) == 0)
  1066. return usrv;
  1067. }
  1068. return NULL;
  1069. }
  1070. static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
  1071. u8 srv_trans_id)
  1072. {
  1073. u8 *len_pos;
  1074. if (wpabuf_tailroom(resp) < 5)
  1075. return;
  1076. /* Length (to be filled) */
  1077. len_pos = wpabuf_put(resp, 2);
  1078. wpabuf_put_u8(resp, srv_proto);
  1079. wpabuf_put_u8(resp, srv_trans_id);
  1080. /* Status Code */
  1081. wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
  1082. /* Response Data: empty */
  1083. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1084. }
  1085. static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
  1086. struct wpabuf *resp, u8 srv_trans_id)
  1087. {
  1088. struct p2p_srv_bonjour *bsrv;
  1089. u8 *len_pos;
  1090. wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
  1091. if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  1092. wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
  1093. return;
  1094. }
  1095. dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
  1096. struct p2p_srv_bonjour, list) {
  1097. if (wpabuf_tailroom(resp) <
  1098. 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
  1099. return;
  1100. /* Length (to be filled) */
  1101. len_pos = wpabuf_put(resp, 2);
  1102. wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
  1103. wpabuf_put_u8(resp, srv_trans_id);
  1104. /* Status Code */
  1105. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1106. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
  1107. wpabuf_head(bsrv->resp),
  1108. wpabuf_len(bsrv->resp));
  1109. /* Response Data */
  1110. wpabuf_put_buf(resp, bsrv->query); /* Key */
  1111. wpabuf_put_buf(resp, bsrv->resp); /* Value */
  1112. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  1113. 2);
  1114. }
  1115. }
  1116. static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
  1117. struct wpabuf *resp, u8 srv_trans_id,
  1118. const u8 *query, size_t query_len)
  1119. {
  1120. struct p2p_srv_bonjour *bsrv;
  1121. struct wpabuf buf;
  1122. u8 *len_pos;
  1123. wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
  1124. query, query_len);
  1125. if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  1126. wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
  1127. wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
  1128. srv_trans_id);
  1129. return;
  1130. }
  1131. if (query_len == 0) {
  1132. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  1133. return;
  1134. }
  1135. if (wpabuf_tailroom(resp) < 5)
  1136. return;
  1137. /* Length (to be filled) */
  1138. len_pos = wpabuf_put(resp, 2);
  1139. wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
  1140. wpabuf_put_u8(resp, srv_trans_id);
  1141. wpabuf_set(&buf, query, query_len);
  1142. bsrv = wpas_p2p_service_get_bonjour(wpa_s, &buf);
  1143. if (bsrv == NULL) {
  1144. wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
  1145. "available");
  1146. /* Status Code */
  1147. wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
  1148. /* Response Data: empty */
  1149. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  1150. 2);
  1151. return;
  1152. }
  1153. /* Status Code */
  1154. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1155. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
  1156. wpabuf_head(bsrv->resp), wpabuf_len(bsrv->resp));
  1157. if (wpabuf_tailroom(resp) >=
  1158. wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) {
  1159. /* Response Data */
  1160. wpabuf_put_buf(resp, bsrv->query); /* Key */
  1161. wpabuf_put_buf(resp, bsrv->resp); /* Value */
  1162. }
  1163. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1164. }
  1165. static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
  1166. struct wpabuf *resp, u8 srv_trans_id)
  1167. {
  1168. struct p2p_srv_upnp *usrv;
  1169. u8 *len_pos;
  1170. wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
  1171. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
  1172. wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
  1173. return;
  1174. }
  1175. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  1176. struct p2p_srv_upnp, list) {
  1177. if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
  1178. return;
  1179. /* Length (to be filled) */
  1180. len_pos = wpabuf_put(resp, 2);
  1181. wpabuf_put_u8(resp, P2P_SERV_UPNP);
  1182. wpabuf_put_u8(resp, srv_trans_id);
  1183. /* Status Code */
  1184. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1185. /* Response Data */
  1186. wpabuf_put_u8(resp, usrv->version);
  1187. wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
  1188. usrv->service);
  1189. wpabuf_put_str(resp, usrv->service);
  1190. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  1191. 2);
  1192. }
  1193. }
  1194. static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
  1195. struct wpabuf *resp, u8 srv_trans_id,
  1196. const u8 *query, size_t query_len)
  1197. {
  1198. struct p2p_srv_upnp *usrv;
  1199. u8 *len_pos;
  1200. u8 version;
  1201. char *str;
  1202. int count = 0;
  1203. wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
  1204. query, query_len);
  1205. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
  1206. wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
  1207. wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
  1208. srv_trans_id);
  1209. return;
  1210. }
  1211. if (query_len == 0) {
  1212. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  1213. return;
  1214. }
  1215. if (wpabuf_tailroom(resp) < 5)
  1216. return;
  1217. /* Length (to be filled) */
  1218. len_pos = wpabuf_put(resp, 2);
  1219. wpabuf_put_u8(resp, P2P_SERV_UPNP);
  1220. wpabuf_put_u8(resp, srv_trans_id);
  1221. version = query[0];
  1222. str = os_malloc(query_len);
  1223. if (str == NULL)
  1224. return;
  1225. os_memcpy(str, query + 1, query_len - 1);
  1226. str[query_len - 1] = '\0';
  1227. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  1228. struct p2p_srv_upnp, list) {
  1229. if (version != usrv->version)
  1230. continue;
  1231. if (os_strcmp(str, "ssdp:all") != 0 &&
  1232. os_strstr(usrv->service, str) == NULL)
  1233. continue;
  1234. if (wpabuf_tailroom(resp) < 2)
  1235. break;
  1236. if (count == 0) {
  1237. /* Status Code */
  1238. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1239. /* Response Data */
  1240. wpabuf_put_u8(resp, version);
  1241. } else
  1242. wpabuf_put_u8(resp, ',');
  1243. count++;
  1244. wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
  1245. usrv->service);
  1246. if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
  1247. break;
  1248. wpabuf_put_str(resp, usrv->service);
  1249. }
  1250. if (count == 0) {
  1251. wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
  1252. "available");
  1253. /* Status Code */
  1254. wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
  1255. /* Response Data: empty */
  1256. }
  1257. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1258. }
  1259. void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
  1260. u16 update_indic, const u8 *tlvs, size_t tlvs_len)
  1261. {
  1262. struct wpa_supplicant *wpa_s = ctx;
  1263. const u8 *pos = tlvs;
  1264. const u8 *end = tlvs + tlvs_len;
  1265. const u8 *tlv_end;
  1266. u16 slen;
  1267. struct wpabuf *resp;
  1268. u8 srv_proto, srv_trans_id;
  1269. size_t buf_len;
  1270. char *buf;
  1271. wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
  1272. tlvs, tlvs_len);
  1273. buf_len = 2 * tlvs_len + 1;
  1274. buf = os_malloc(buf_len);
  1275. if (buf) {
  1276. wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
  1277. wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
  1278. MACSTR " %u %u %s",
  1279. freq, MAC2STR(sa), dialog_token, update_indic,
  1280. buf);
  1281. os_free(buf);
  1282. }
  1283. if (wpa_s->p2p_sd_over_ctrl_iface)
  1284. return; /* to be processed by an external program */
  1285. resp = wpabuf_alloc(10000);
  1286. if (resp == NULL)
  1287. return;
  1288. while (pos + 1 < end) {
  1289. wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
  1290. slen = WPA_GET_LE16(pos);
  1291. pos += 2;
  1292. if (pos + slen > end || slen < 2) {
  1293. wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
  1294. "length");
  1295. wpabuf_free(resp);
  1296. return;
  1297. }
  1298. tlv_end = pos + slen;
  1299. srv_proto = *pos++;
  1300. wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
  1301. srv_proto);
  1302. srv_trans_id = *pos++;
  1303. wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
  1304. srv_trans_id);
  1305. wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
  1306. pos, tlv_end - pos);
  1307. if (wpa_s->force_long_sd) {
  1308. wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
  1309. "response");
  1310. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  1311. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  1312. goto done;
  1313. }
  1314. switch (srv_proto) {
  1315. case P2P_SERV_ALL_SERVICES:
  1316. wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
  1317. "for all services");
  1318. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
  1319. dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  1320. wpa_printf(MSG_DEBUG, "P2P: No service "
  1321. "discovery protocols available");
  1322. wpas_sd_add_proto_not_avail(
  1323. resp, P2P_SERV_ALL_SERVICES,
  1324. srv_trans_id);
  1325. break;
  1326. }
  1327. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  1328. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  1329. break;
  1330. case P2P_SERV_BONJOUR:
  1331. wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
  1332. pos, tlv_end - pos);
  1333. break;
  1334. case P2P_SERV_UPNP:
  1335. wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
  1336. pos, tlv_end - pos);
  1337. break;
  1338. default:
  1339. wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
  1340. "protocol %u", srv_proto);
  1341. wpas_sd_add_proto_not_avail(resp, srv_proto,
  1342. srv_trans_id);
  1343. break;
  1344. }
  1345. pos = tlv_end;
  1346. }
  1347. done:
  1348. wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
  1349. wpabuf_free(resp);
  1350. }
  1351. void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
  1352. const u8 *tlvs, size_t tlvs_len)
  1353. {
  1354. struct wpa_supplicant *wpa_s = ctx;
  1355. const u8 *pos = tlvs;
  1356. const u8 *end = tlvs + tlvs_len;
  1357. const u8 *tlv_end;
  1358. u16 slen;
  1359. size_t buf_len;
  1360. char *buf;
  1361. wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
  1362. tlvs, tlvs_len);
  1363. if (tlvs_len > 1500) {
  1364. /* TODO: better way for handling this */
  1365. wpa_msg_ctrl(wpa_s, MSG_INFO,
  1366. P2P_EVENT_SERV_DISC_RESP MACSTR
  1367. " %u <long response: %u bytes>",
  1368. MAC2STR(sa), update_indic,
  1369. (unsigned int) tlvs_len);
  1370. } else {
  1371. buf_len = 2 * tlvs_len + 1;
  1372. buf = os_malloc(buf_len);
  1373. if (buf) {
  1374. wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
  1375. wpa_msg_ctrl(wpa_s, MSG_INFO,
  1376. P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
  1377. MAC2STR(sa), update_indic, buf);
  1378. os_free(buf);
  1379. }
  1380. }
  1381. while (pos < end) {
  1382. u8 srv_proto, srv_trans_id, status;
  1383. wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
  1384. slen = WPA_GET_LE16(pos);
  1385. pos += 2;
  1386. if (pos + slen > end || slen < 3) {
  1387. wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
  1388. "length");
  1389. return;
  1390. }
  1391. tlv_end = pos + slen;
  1392. srv_proto = *pos++;
  1393. wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
  1394. srv_proto);
  1395. srv_trans_id = *pos++;
  1396. wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
  1397. srv_trans_id);
  1398. status = *pos++;
  1399. wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
  1400. status);
  1401. wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
  1402. pos, tlv_end - pos);
  1403. pos = tlv_end;
  1404. }
  1405. }
  1406. void * wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
  1407. const struct wpabuf *tlvs)
  1408. {
  1409. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  1410. return (void *) wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
  1411. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  1412. return NULL;
  1413. return p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
  1414. }
  1415. void * wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
  1416. u8 version, const char *query)
  1417. {
  1418. struct wpabuf *tlvs;
  1419. void *ret;
  1420. tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
  1421. if (tlvs == NULL)
  1422. return NULL;
  1423. wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
  1424. wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
  1425. wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
  1426. wpabuf_put_u8(tlvs, version);
  1427. wpabuf_put_str(tlvs, query);
  1428. ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
  1429. wpabuf_free(tlvs);
  1430. return ret;
  1431. }
  1432. int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, void *req)
  1433. {
  1434. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  1435. return wpa_drv_p2p_sd_cancel_request(wpa_s, (u64) req);
  1436. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  1437. return -1;
  1438. return p2p_sd_cancel_request(wpa_s->global->p2p, req);
  1439. }
  1440. void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
  1441. const u8 *dst, u8 dialog_token,
  1442. const struct wpabuf *resp_tlvs)
  1443. {
  1444. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  1445. wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
  1446. resp_tlvs);
  1447. return;
  1448. }
  1449. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  1450. return;
  1451. p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
  1452. resp_tlvs);
  1453. }
  1454. void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
  1455. {
  1456. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  1457. wpa_drv_p2p_service_update(wpa_s);
  1458. return;
  1459. }
  1460. if (wpa_s->global->p2p)
  1461. p2p_sd_service_update(wpa_s->global->p2p);
  1462. }
  1463. static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
  1464. {
  1465. dl_list_del(&bsrv->list);
  1466. wpabuf_free(bsrv->query);
  1467. wpabuf_free(bsrv->resp);
  1468. os_free(bsrv);
  1469. }
  1470. static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
  1471. {
  1472. dl_list_del(&usrv->list);
  1473. os_free(usrv->service);
  1474. os_free(usrv);
  1475. }
  1476. void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
  1477. {
  1478. struct p2p_srv_bonjour *bsrv, *bn;
  1479. struct p2p_srv_upnp *usrv, *un;
  1480. dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
  1481. struct p2p_srv_bonjour, list)
  1482. wpas_p2p_srv_bonjour_free(bsrv);
  1483. dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
  1484. struct p2p_srv_upnp, list)
  1485. wpas_p2p_srv_upnp_free(usrv);
  1486. wpas_p2p_sd_service_update(wpa_s);
  1487. }
  1488. int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
  1489. struct wpabuf *query, struct wpabuf *resp)
  1490. {
  1491. struct p2p_srv_bonjour *bsrv;
  1492. bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
  1493. if (bsrv) {
  1494. wpabuf_free(query);
  1495. wpabuf_free(bsrv->resp);
  1496. bsrv->resp = resp;
  1497. return 0;
  1498. }
  1499. bsrv = os_zalloc(sizeof(*bsrv));
  1500. if (bsrv == NULL)
  1501. return -1;
  1502. bsrv->query = query;
  1503. bsrv->resp = resp;
  1504. dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
  1505. wpas_p2p_sd_service_update(wpa_s);
  1506. return 0;
  1507. }
  1508. int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
  1509. const struct wpabuf *query)
  1510. {
  1511. struct p2p_srv_bonjour *bsrv;
  1512. bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
  1513. if (bsrv == NULL)
  1514. return -1;
  1515. wpas_p2p_srv_bonjour_free(bsrv);
  1516. wpas_p2p_sd_service_update(wpa_s);
  1517. return 0;
  1518. }
  1519. int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
  1520. const char *service)
  1521. {
  1522. struct p2p_srv_upnp *usrv;
  1523. if (wpas_p2p_service_get_upnp(wpa_s, version, service))
  1524. return 0; /* Already listed */
  1525. usrv = os_zalloc(sizeof(*usrv));
  1526. if (usrv == NULL)
  1527. return -1;
  1528. usrv->version = version;
  1529. usrv->service = os_strdup(service);
  1530. if (usrv->service == NULL) {
  1531. os_free(usrv);
  1532. return -1;
  1533. }
  1534. dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
  1535. wpas_p2p_sd_service_update(wpa_s);
  1536. return 0;
  1537. }
  1538. int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
  1539. const char *service)
  1540. {
  1541. struct p2p_srv_upnp *usrv;
  1542. usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
  1543. if (usrv == NULL)
  1544. return -1;
  1545. wpas_p2p_srv_upnp_free(usrv);
  1546. wpas_p2p_sd_service_update(wpa_s);
  1547. return 0;
  1548. }
  1549. static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
  1550. const u8 *peer, const char *params,
  1551. unsigned int generated_pin)
  1552. {
  1553. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
  1554. MAC2STR(peer), generated_pin, params);
  1555. }
  1556. static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
  1557. const u8 *peer, const char *params)
  1558. {
  1559. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
  1560. MAC2STR(peer), params);
  1561. }
  1562. void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
  1563. const u8 *dev_addr, const u8 *pri_dev_type,
  1564. const char *dev_name, u16 supp_config_methods,
  1565. u8 dev_capab, u8 group_capab)
  1566. {
  1567. struct wpa_supplicant *wpa_s = ctx;
  1568. char devtype[WPS_DEV_TYPE_BUFSIZE];
  1569. char params[200];
  1570. u8 empty_dev_type[8];
  1571. unsigned int generated_pin = 0;
  1572. if (pri_dev_type == NULL) {
  1573. os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
  1574. pri_dev_type = empty_dev_type;
  1575. }
  1576. os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
  1577. " pri_dev_type=%s name='%s' config_methods=0x%x "
  1578. "dev_capab=0x%x group_capab=0x%x",
  1579. MAC2STR(dev_addr),
  1580. wps_dev_type_bin2str(pri_dev_type, devtype,
  1581. sizeof(devtype)),
  1582. dev_name, supp_config_methods, dev_capab, group_capab);
  1583. params[sizeof(params) - 1] = '\0';
  1584. if (config_methods & WPS_CONFIG_DISPLAY) {
  1585. generated_pin = wps_generate_pin();
  1586. wpas_prov_disc_local_display(wpa_s, peer, params,
  1587. generated_pin);
  1588. } else if (config_methods & WPS_CONFIG_KEYPAD)
  1589. wpas_prov_disc_local_keypad(wpa_s, peer, params);
  1590. else if (config_methods & WPS_CONFIG_PUSHBUTTON)
  1591. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
  1592. "%s", MAC2STR(peer), params);
  1593. }
  1594. void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
  1595. {
  1596. struct wpa_supplicant *wpa_s = ctx;
  1597. unsigned int generated_pin = 0;
  1598. if (config_methods & WPS_CONFIG_DISPLAY)
  1599. wpas_prov_disc_local_keypad(wpa_s, peer, "");
  1600. else if (config_methods & WPS_CONFIG_KEYPAD) {
  1601. generated_pin = wps_generate_pin();
  1602. wpas_prov_disc_local_display(wpa_s, peer, "", generated_pin);
  1603. } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
  1604. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR,
  1605. MAC2STR(peer));
  1606. if (wpa_s->pending_pd_before_join &&
  1607. (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
  1608. os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
  1609. wpa_s->pending_pd_before_join = 0;
  1610. wpa_printf(MSG_DEBUG, "P2P: Starting pending "
  1611. "join-existing-group operation");
  1612. wpas_p2p_join_start(wpa_s);
  1613. }
  1614. }
  1615. static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
  1616. const u8 *go_dev_addr, const u8 *ssid,
  1617. size_t ssid_len, int *go, u8 *group_bssid,
  1618. int *force_freq, int persistent_group)
  1619. {
  1620. struct wpa_supplicant *wpa_s = ctx;
  1621. struct wpa_ssid *s;
  1622. u8 cur_bssid[ETH_ALEN];
  1623. int res;
  1624. struct wpa_supplicant *grp;
  1625. if (!persistent_group) {
  1626. wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
  1627. " to join an active group", MAC2STR(sa));
  1628. if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
  1629. (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
  1630. == 0 ||
  1631. os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
  1632. wpa_printf(MSG_DEBUG, "P2P: Accept previously "
  1633. "authorized invitation");
  1634. goto accept_inv;
  1635. }
  1636. /*
  1637. * Do not accept the invitation automatically; notify user and
  1638. * request approval.
  1639. */
  1640. return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  1641. }
  1642. grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
  1643. if (grp) {
  1644. wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
  1645. "running persistent group");
  1646. if (*go)
  1647. os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
  1648. goto accept_inv;
  1649. }
  1650. if (!wpa_s->conf->persistent_reconnect)
  1651. return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  1652. for (s = wpa_s->conf->ssid; s; s = s->next) {
  1653. if (s->disabled == 2 &&
  1654. os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
  1655. s->ssid_len == ssid_len &&
  1656. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  1657. break;
  1658. }
  1659. if (!s) {
  1660. wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
  1661. " requested reinvocation of an unknown group",
  1662. MAC2STR(sa));
  1663. return P2P_SC_FAIL_UNKNOWN_GROUP;
  1664. }
  1665. if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
  1666. *go = 1;
  1667. if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
  1668. wpa_printf(MSG_DEBUG, "P2P: The only available "
  1669. "interface is already in use - reject "
  1670. "invitation");
  1671. return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
  1672. }
  1673. os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
  1674. } else if (s->mode == WPAS_MODE_P2P_GO) {
  1675. *go = 1;
  1676. if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
  1677. {
  1678. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  1679. "interface address for the group");
  1680. return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
  1681. }
  1682. os_memcpy(group_bssid, wpa_s->pending_interface_addr,
  1683. ETH_ALEN);
  1684. }
  1685. accept_inv:
  1686. if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
  1687. wpa_s->assoc_freq) {
  1688. wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
  1689. "the channel we are already using");
  1690. *force_freq = wpa_s->assoc_freq;
  1691. }
  1692. res = wpa_drv_shared_freq(wpa_s);
  1693. if (res > 0) {
  1694. wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
  1695. "with the channel we are already using on a "
  1696. "shared interface");
  1697. *force_freq = res;
  1698. }
  1699. return P2P_SC_SUCCESS;
  1700. }
  1701. static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
  1702. const u8 *ssid, size_t ssid_len,
  1703. const u8 *go_dev_addr, u8 status,
  1704. int op_freq)
  1705. {
  1706. struct wpa_supplicant *wpa_s = ctx;
  1707. struct wpa_ssid *s;
  1708. for (s = wpa_s->conf->ssid; s; s = s->next) {
  1709. if (s->disabled == 2 &&
  1710. s->ssid_len == ssid_len &&
  1711. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  1712. break;
  1713. }
  1714. if (status == P2P_SC_SUCCESS) {
  1715. wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
  1716. " was accepted; op_freq=%d MHz",
  1717. MAC2STR(sa), op_freq);
  1718. if (s) {
  1719. wpas_p2p_group_add_persistent(
  1720. wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0);
  1721. } else if (bssid) {
  1722. wpas_p2p_join(wpa_s, bssid, go_dev_addr,
  1723. wpa_s->p2p_wps_method);
  1724. }
  1725. return;
  1726. }
  1727. if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
  1728. wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
  1729. " was rejected (status %u)", MAC2STR(sa), status);
  1730. return;
  1731. }
  1732. if (!s) {
  1733. if (bssid) {
  1734. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
  1735. "sa=" MACSTR " go_dev_addr=" MACSTR
  1736. " bssid=" MACSTR " unknown-network",
  1737. MAC2STR(sa), MAC2STR(go_dev_addr),
  1738. MAC2STR(bssid));
  1739. } else {
  1740. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
  1741. "sa=" MACSTR " go_dev_addr=" MACSTR
  1742. " unknown-network",
  1743. MAC2STR(sa), MAC2STR(go_dev_addr));
  1744. }
  1745. return;
  1746. }
  1747. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
  1748. " persistent=%d", MAC2STR(sa), s->id);
  1749. }
  1750. static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
  1751. {
  1752. struct wpa_supplicant *wpa_s = ctx;
  1753. struct wpa_ssid *ssid;
  1754. if (bssid) {
  1755. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
  1756. "status=%d " MACSTR,
  1757. status, MAC2STR(bssid));
  1758. } else {
  1759. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
  1760. "status=%d ", status);
  1761. }
  1762. if (wpa_s->pending_invite_ssid_id == -1)
  1763. return; /* Invitation to active group */
  1764. if (status != P2P_SC_SUCCESS) {
  1765. wpas_p2p_remove_pending_group_interface(wpa_s);
  1766. return;
  1767. }
  1768. ssid = wpa_config_get_network(wpa_s->conf,
  1769. wpa_s->pending_invite_ssid_id);
  1770. if (ssid == NULL) {
  1771. wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
  1772. "data matching with invitation");
  1773. return;
  1774. }
  1775. wpas_p2p_group_add_persistent(wpa_s, ssid,
  1776. ssid->mode == WPAS_MODE_P2P_GO, 0);
  1777. }
  1778. static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
  1779. struct p2p_channels *chan)
  1780. {
  1781. int i, cla = 0;
  1782. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
  1783. "band");
  1784. /* Operating class 81 - 2.4 GHz band channels 1..13 */
  1785. chan->reg_class[cla].reg_class = 81;
  1786. chan->reg_class[cla].channels = 11;
  1787. for (i = 0; i < 11; i++)
  1788. chan->reg_class[cla].channel[i] = i + 1;
  1789. cla++;
  1790. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
  1791. "band");
  1792. /* Operating class 115 - 5 GHz, channels 36-48 */
  1793. chan->reg_class[cla].reg_class = 115;
  1794. chan->reg_class[cla].channels = 4;
  1795. chan->reg_class[cla].channel[0] = 36;
  1796. chan->reg_class[cla].channel[1] = 40;
  1797. chan->reg_class[cla].channel[2] = 44;
  1798. chan->reg_class[cla].channel[3] = 48;
  1799. cla++;
  1800. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
  1801. "band");
  1802. /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
  1803. chan->reg_class[cla].reg_class = 124;
  1804. chan->reg_class[cla].channels = 4;
  1805. chan->reg_class[cla].channel[0] = 149;
  1806. chan->reg_class[cla].channel[1] = 153;
  1807. chan->reg_class[cla].channel[2] = 157;
  1808. chan->reg_class[cla].channel[3] = 161;
  1809. cla++;
  1810. chan->reg_classes = cla;
  1811. return 0;
  1812. }
  1813. static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
  1814. u16 num_modes,
  1815. enum hostapd_hw_mode mode)
  1816. {
  1817. u16 i;
  1818. for (i = 0; i < num_modes; i++) {
  1819. if (modes[i].mode == mode)
  1820. return &modes[i];
  1821. }
  1822. return NULL;
  1823. }
  1824. static int has_channel(struct hostapd_hw_modes *mode, u8 chan, int *flags)
  1825. {
  1826. int i;
  1827. for (i = 0; i < mode->num_channels; i++) {
  1828. if (mode->channels[i].chan == chan) {
  1829. if (flags)
  1830. *flags = mode->channels[i].flag;
  1831. return !(mode->channels[i].flag &
  1832. (HOSTAPD_CHAN_DISABLED |
  1833. HOSTAPD_CHAN_PASSIVE_SCAN |
  1834. HOSTAPD_CHAN_NO_IBSS |
  1835. HOSTAPD_CHAN_RADAR));
  1836. }
  1837. }
  1838. return 0;
  1839. }
  1840. struct p2p_oper_class_map {
  1841. enum hostapd_hw_mode mode;
  1842. u8 op_class;
  1843. u8 min_chan;
  1844. u8 max_chan;
  1845. u8 inc;
  1846. enum { BW20, BW40PLUS, BW40MINUS } bw;
  1847. };
  1848. static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
  1849. struct p2p_channels *chan)
  1850. {
  1851. struct hostapd_hw_modes *modes, *mode;
  1852. u16 num_modes, flags;
  1853. int cla, op;
  1854. struct p2p_oper_class_map op_class[] = {
  1855. { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
  1856. { HOSTAPD_MODE_IEEE80211G, 82, 14, 14, 1, BW20 },
  1857. #if 0 /* Do not enable HT40 on 2 GHz for now */
  1858. { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
  1859. { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
  1860. #endif
  1861. { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
  1862. { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
  1863. { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
  1864. { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
  1865. { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
  1866. { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
  1867. { -1, 0, 0, 0, 0, BW20 }
  1868. };
  1869. modes = wpa_drv_get_hw_feature_data(wpa_s, &num_modes, &flags);
  1870. if (modes == NULL) {
  1871. wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
  1872. "of all supported channels; assume dualband "
  1873. "support");
  1874. return wpas_p2p_default_channels(wpa_s, chan);
  1875. }
  1876. cla = 0;
  1877. for (op = 0; op_class[op].op_class; op++) {
  1878. struct p2p_oper_class_map *o = &op_class[op];
  1879. u8 ch;
  1880. struct p2p_reg_class *reg = NULL;
  1881. mode = get_mode(modes, num_modes, o->mode);
  1882. if (mode == NULL)
  1883. continue;
  1884. for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
  1885. int flag;
  1886. if (!has_channel(mode, ch, &flag))
  1887. continue;
  1888. if (o->bw == BW40MINUS &&
  1889. (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
  1890. !has_channel(mode, ch - 4, NULL)))
  1891. continue;
  1892. if (o->bw == BW40PLUS &&
  1893. (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
  1894. !has_channel(mode, ch + 4, NULL)))
  1895. continue;
  1896. if (reg == NULL) {
  1897. wpa_printf(MSG_DEBUG, "P2P: Add operating "
  1898. "class %u", o->op_class);
  1899. reg = &chan->reg_class[cla];
  1900. cla++;
  1901. reg->reg_class = o->op_class;
  1902. }
  1903. reg->channel[reg->channels] = ch;
  1904. reg->channels++;
  1905. }
  1906. if (reg) {
  1907. wpa_hexdump(MSG_DEBUG, "P2P: Channels",
  1908. reg->channel, reg->channels);
  1909. }
  1910. }
  1911. chan->reg_classes = cla;
  1912. ieee80211_sta_free_hw_features(modes, num_modes);
  1913. return 0;
  1914. }
  1915. static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
  1916. size_t buf_len)
  1917. {
  1918. struct wpa_supplicant *wpa_s = ctx;
  1919. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  1920. if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
  1921. break;
  1922. }
  1923. if (wpa_s == NULL)
  1924. return -1;
  1925. return wpa_drv_get_noa(wpa_s, buf, buf_len);
  1926. }
  1927. /**
  1928. * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
  1929. * @global: Pointer to global data from wpa_supplicant_init()
  1930. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  1931. * Returns: 0 on success, -1 on failure
  1932. */
  1933. int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
  1934. {
  1935. struct p2p_config p2p;
  1936. unsigned int r;
  1937. int i;
  1938. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
  1939. return 0;
  1940. #ifdef CONFIG_CLIENT_MLME
  1941. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)) {
  1942. wpa_s->mlme.public_action_cb = p2p_rx_action_mlme;
  1943. wpa_s->mlme.public_action_cb_ctx = wpa_s;
  1944. }
  1945. #endif /* CONFIG_CLIENT_MLME */
  1946. if (wpa_drv_disable_11b_rates(wpa_s, 1) < 0) {
  1947. wpa_printf(MSG_DEBUG, "P2P: Failed to disable 11b rates");
  1948. /* Continue anyway; this is not really a fatal error */
  1949. }
  1950. if (global->p2p)
  1951. return 0;
  1952. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  1953. struct p2p_params params;
  1954. wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
  1955. os_memset(&params, 0, sizeof(params));
  1956. params.dev_name = wpa_s->conf->device_name;
  1957. if (wpa_s->conf->device_type &&
  1958. wps_dev_type_str2bin(wpa_s->conf->device_type,
  1959. params.pri_dev_type) < 0) {
  1960. wpa_printf(MSG_ERROR, "P2P: Invalid device_type");
  1961. return -1;
  1962. }
  1963. for (i = 0; i < MAX_SEC_DEVICE_TYPES; i++) {
  1964. if (wpa_s->conf->sec_device_type[i] == NULL)
  1965. continue;
  1966. if (wps_dev_type_str2bin(
  1967. wpa_s->conf->sec_device_type[i],
  1968. params.sec_dev_type[
  1969. params.num_sec_dev_types]) < 0) {
  1970. wpa_printf(MSG_ERROR, "P2P: Invalid "
  1971. "sec_device_type");
  1972. return -1;
  1973. }
  1974. params.num_sec_dev_types++;
  1975. if (params.num_sec_dev_types == DRV_MAX_SEC_DEV_TYPES)
  1976. break;
  1977. }
  1978. if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
  1979. return -1;
  1980. return 0;
  1981. }
  1982. os_memset(&p2p, 0, sizeof(p2p));
  1983. p2p.msg_ctx = wpa_s;
  1984. p2p.cb_ctx = wpa_s;
  1985. p2p.p2p_scan = wpas_p2p_scan;
  1986. p2p.send_action = wpas_send_action;
  1987. p2p.send_action_done = wpas_send_action_done;
  1988. p2p.go_neg_completed = wpas_go_neg_completed;
  1989. p2p.go_neg_req_rx = wpas_go_neg_req_rx;
  1990. p2p.dev_found = wpas_dev_found;
  1991. p2p.start_listen = wpas_start_listen;
  1992. p2p.stop_listen = wpas_stop_listen;
  1993. p2p.send_probe_resp = wpas_send_probe_resp;
  1994. p2p.sd_request = wpas_sd_request;
  1995. p2p.sd_response = wpas_sd_response;
  1996. p2p.prov_disc_req = wpas_prov_disc_req;
  1997. p2p.prov_disc_resp = wpas_prov_disc_resp;
  1998. p2p.invitation_process = wpas_invitation_process;
  1999. p2p.invitation_received = wpas_invitation_received;
  2000. p2p.invitation_result = wpas_invitation_result;
  2001. p2p.get_noa = wpas_get_noa;
  2002. os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
  2003. os_memcpy(p2p.dev_addr, wpa_s->own_addr, ETH_ALEN);
  2004. p2p.dev_name = wpa_s->conf->device_name;
  2005. if (wpa_s->conf->p2p_listen_reg_class &&
  2006. wpa_s->conf->p2p_listen_channel) {
  2007. p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
  2008. p2p.channel = wpa_s->conf->p2p_listen_channel;
  2009. } else {
  2010. p2p.reg_class = 81;
  2011. /*
  2012. * Pick one of the social channels randomly as the listen
  2013. * channel.
  2014. */
  2015. os_get_random((u8 *) &r, sizeof(r));
  2016. p2p.channel = 1 + (r % 3) * 5;
  2017. }
  2018. wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
  2019. if (wpa_s->conf->p2p_oper_reg_class &&
  2020. wpa_s->conf->p2p_oper_channel) {
  2021. p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
  2022. p2p.op_channel = wpa_s->conf->p2p_oper_channel;
  2023. p2p.cfg_op_channel = 1;
  2024. wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
  2025. "%d:%d", p2p.op_reg_class, p2p.op_channel);
  2026. } else {
  2027. p2p.op_reg_class = 81;
  2028. /*
  2029. * Use random operation channel from (1, 6, 11) if no other
  2030. * preference is indicated.
  2031. */
  2032. os_get_random((u8 *) &r, sizeof(r));
  2033. p2p.op_channel = 1 + (r % 3) * 5;
  2034. p2p.cfg_op_channel = 0;
  2035. wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
  2036. "%d:%d", p2p.op_reg_class, p2p.op_channel);
  2037. }
  2038. if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  2039. os_memcpy(p2p.country, wpa_s->conf->country, 2);
  2040. p2p.country[2] = 0x04;
  2041. } else
  2042. os_memcpy(p2p.country, "XX\x04", 3);
  2043. if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
  2044. wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
  2045. "channel list");
  2046. return -1;
  2047. }
  2048. if (wpa_s->conf->device_type &&
  2049. wps_dev_type_str2bin(wpa_s->conf->device_type, p2p.pri_dev_type) <
  2050. 0) {
  2051. wpa_printf(MSG_ERROR, "P2P: Invalid device_type");
  2052. return -1;
  2053. }
  2054. for (i = 0; i < MAX_SEC_DEVICE_TYPES; i++) {
  2055. if (wpa_s->conf->sec_device_type[i] == NULL)
  2056. continue;
  2057. if (wps_dev_type_str2bin(
  2058. wpa_s->conf->sec_device_type[i],
  2059. p2p.sec_dev_type[p2p.num_sec_dev_types]) < 0) {
  2060. wpa_printf(MSG_ERROR, "P2P: Invalid sec_device_type");
  2061. return -1;
  2062. }
  2063. p2p.num_sec_dev_types++;
  2064. if (p2p.num_sec_dev_types == P2P_SEC_DEVICE_TYPES)
  2065. break;
  2066. }
  2067. p2p.concurrent_operations = !!(wpa_s->drv_flags &
  2068. WPA_DRIVER_FLAGS_P2P_CONCURRENT);
  2069. p2p.max_peers = 100;
  2070. if (wpa_s->conf->p2p_ssid_postfix) {
  2071. p2p.ssid_postfix_len =
  2072. os_strlen(wpa_s->conf->p2p_ssid_postfix);
  2073. if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
  2074. p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
  2075. os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
  2076. p2p.ssid_postfix_len);
  2077. }
  2078. p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
  2079. global->p2p = p2p_init(&p2p);
  2080. if (global->p2p == NULL)
  2081. return -1;
  2082. return 0;
  2083. }
  2084. /**
  2085. * wpas_p2p_deinit - Deinitialize per-interface P2P data
  2086. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2087. *
  2088. * This function deinitialize per-interface P2P data.
  2089. */
  2090. void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
  2091. {
  2092. if (wpa_s->driver && wpa_s->drv_priv)
  2093. wpa_drv_probe_req_report(wpa_s, 0);
  2094. os_free(wpa_s->go_params);
  2095. wpa_s->go_params = NULL;
  2096. wpabuf_free(wpa_s->pending_action_tx);
  2097. wpa_s->pending_action_tx = NULL;
  2098. eloop_cancel_timeout(wpas_send_action_cb, wpa_s, NULL);
  2099. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  2100. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2101. wpa_s->p2p_long_listen = 0;
  2102. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  2103. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  2104. wpas_p2p_remove_pending_group_interface(wpa_s);
  2105. /* TODO: remove group interface from the driver if this wpa_s instance
  2106. * is on top of a P2P group interface */
  2107. }
  2108. /**
  2109. * wpas_p2p_deinit_global - Deinitialize global P2P module
  2110. * @global: Pointer to global data from wpa_supplicant_init()
  2111. *
  2112. * This function deinitializes the global (per device) P2P module.
  2113. */
  2114. void wpas_p2p_deinit_global(struct wpa_global *global)
  2115. {
  2116. struct wpa_supplicant *wpa_s, *tmp;
  2117. char *ifname;
  2118. if (global->p2p == NULL)
  2119. return;
  2120. /* Remove remaining P2P group interfaces */
  2121. wpa_s = global->ifaces;
  2122. if (wpa_s)
  2123. wpas_p2p_service_flush(wpa_s);
  2124. while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
  2125. wpa_s = wpa_s->next;
  2126. while (wpa_s) {
  2127. enum wpa_driver_if_type type;
  2128. tmp = global->ifaces;
  2129. while (tmp &&
  2130. (tmp == wpa_s ||
  2131. tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
  2132. tmp = tmp->next;
  2133. }
  2134. if (tmp == NULL)
  2135. break;
  2136. ifname = os_strdup(tmp->ifname);
  2137. type = wpas_p2p_if_type(tmp->p2p_group_interface);
  2138. wpa_supplicant_remove_iface(global, tmp);
  2139. if (ifname)
  2140. wpa_drv_if_remove(wpa_s, type, ifname);
  2141. os_free(ifname);
  2142. }
  2143. /*
  2144. * Deinit GO data on any possibly remaining interface (if main
  2145. * interface is used as GO).
  2146. */
  2147. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2148. if (wpa_s->ap_iface)
  2149. wpas_p2p_group_deinit(wpa_s);
  2150. }
  2151. p2p_deinit(global->p2p);
  2152. global->p2p = NULL;
  2153. }
  2154. static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
  2155. {
  2156. if (wpa_s->drv_flags &
  2157. (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
  2158. WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
  2159. return 1; /* P2P group requires a new interface in every case
  2160. */
  2161. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
  2162. return 0; /* driver does not support concurrent operations */
  2163. if (wpa_s->global->ifaces->next)
  2164. return 1; /* more that one interface already in use */
  2165. if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
  2166. return 1; /* this interface is already in use */
  2167. return 0;
  2168. }
  2169. static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
  2170. const u8 *peer_addr,
  2171. enum p2p_wps_method wps_method,
  2172. int go_intent, const u8 *own_interface_addr,
  2173. unsigned int force_freq, int persistent_group)
  2174. {
  2175. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  2176. return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
  2177. go_intent, own_interface_addr,
  2178. force_freq, persistent_group);
  2179. }
  2180. return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
  2181. go_intent, own_interface_addr, force_freq,
  2182. persistent_group);
  2183. }
  2184. static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
  2185. const u8 *peer_addr,
  2186. enum p2p_wps_method wps_method,
  2187. int go_intent, const u8 *own_interface_addr,
  2188. unsigned int force_freq, int persistent_group)
  2189. {
  2190. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  2191. return -1;
  2192. return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
  2193. go_intent, own_interface_addr, force_freq,
  2194. persistent_group);
  2195. }
  2196. static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
  2197. {
  2198. wpa_s->p2p_join_scan_count++;
  2199. wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
  2200. wpa_s->p2p_join_scan_count);
  2201. if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
  2202. wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
  2203. " for join operationg - stop join attempt",
  2204. MAC2STR(wpa_s->pending_join_iface_addr));
  2205. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2206. wpa_msg(wpa_s->parent, MSG_INFO,
  2207. P2P_EVENT_GROUP_FORMATION_FAILURE);
  2208. }
  2209. }
  2210. static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
  2211. struct wpa_scan_results *scan_res)
  2212. {
  2213. struct wpa_bss *bss;
  2214. int freq;
  2215. u8 iface_addr[ETH_ALEN];
  2216. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2217. if (wpa_s->global->p2p_disabled)
  2218. return;
  2219. wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for join",
  2220. scan_res ? (int) scan_res->num : -1);
  2221. if (scan_res)
  2222. wpas_p2p_scan_res_handler(wpa_s, scan_res);
  2223. freq = p2p_get_oper_freq(wpa_s->global->p2p,
  2224. wpa_s->pending_join_iface_addr);
  2225. if (freq < 0 &&
  2226. p2p_get_interface_addr(wpa_s->global->p2p,
  2227. wpa_s->pending_join_dev_addr,
  2228. iface_addr) == 0 &&
  2229. os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
  2230. {
  2231. wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
  2232. "address for join from " MACSTR " to " MACSTR
  2233. " based on newly discovered P2P peer entry",
  2234. MAC2STR(wpa_s->pending_join_iface_addr),
  2235. MAC2STR(iface_addr));
  2236. os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
  2237. ETH_ALEN);
  2238. freq = p2p_get_oper_freq(wpa_s->global->p2p,
  2239. wpa_s->pending_join_iface_addr);
  2240. }
  2241. if (freq >= 0) {
  2242. wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
  2243. "from P2P peer table: %d MHz", freq);
  2244. }
  2245. bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
  2246. if (bss) {
  2247. freq = bss->freq;
  2248. wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
  2249. "from BSS table: %d MHz", freq);
  2250. }
  2251. if (freq > 0) {
  2252. u16 method;
  2253. wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
  2254. "prior to joining an existing group (GO " MACSTR
  2255. " freq=%u MHz)",
  2256. MAC2STR(wpa_s->pending_join_dev_addr), freq);
  2257. wpa_s->pending_pd_before_join = 1;
  2258. switch (wpa_s->pending_join_wps_method) {
  2259. case WPS_PIN_LABEL:
  2260. case WPS_PIN_DISPLAY:
  2261. method = WPS_CONFIG_KEYPAD;
  2262. break;
  2263. case WPS_PIN_KEYPAD:
  2264. method = WPS_CONFIG_DISPLAY;
  2265. break;
  2266. case WPS_PBC:
  2267. method = WPS_CONFIG_PUSHBUTTON;
  2268. break;
  2269. default:
  2270. method = 0;
  2271. break;
  2272. }
  2273. if (p2p_prov_disc_req(wpa_s->global->p2p,
  2274. wpa_s->pending_join_dev_addr, method, 1)
  2275. < 0) {
  2276. wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
  2277. "Discovery Request before joining an "
  2278. "existing group");
  2279. wpa_s->pending_pd_before_join = 0;
  2280. goto start;
  2281. }
  2282. /*
  2283. * Actual join operation will be started from the Action frame
  2284. * TX status callback.
  2285. */
  2286. return;
  2287. }
  2288. wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
  2289. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2290. eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
  2291. wpas_p2p_check_join_scan_limit(wpa_s);
  2292. return;
  2293. start:
  2294. /* Start join operation immediately */
  2295. wpas_p2p_join_start(wpa_s);
  2296. }
  2297. static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
  2298. {
  2299. struct wpa_supplicant *wpa_s = eloop_ctx;
  2300. int ret;
  2301. struct wpa_driver_scan_params params;
  2302. struct wpabuf *wps_ie, *ies;
  2303. os_memset(&params, 0, sizeof(params));
  2304. /* P2P Wildcard SSID */
  2305. params.num_ssids = 1;
  2306. params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
  2307. params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
  2308. wpa_s->wps->dev.p2p = 1;
  2309. wps_ie = wps_build_probe_req_ie(0, &wpa_s->wps->dev, wpa_s->wps->uuid,
  2310. WPS_REQ_ENROLLEE);
  2311. if (wps_ie == NULL) {
  2312. wpas_p2p_scan_res_join(wpa_s, NULL);
  2313. return;
  2314. }
  2315. ies = wpabuf_alloc(wpabuf_len(wps_ie) + 100);
  2316. if (ies == NULL) {
  2317. wpabuf_free(wps_ie);
  2318. wpas_p2p_scan_res_join(wpa_s, NULL);
  2319. return;
  2320. }
  2321. wpabuf_put_buf(ies, wps_ie);
  2322. wpabuf_free(wps_ie);
  2323. p2p_scan_ie(wpa_s->global->p2p, ies);
  2324. params.extra_ies = wpabuf_head(ies);
  2325. params.extra_ies_len = wpabuf_len(ies);
  2326. /*
  2327. * Run a scan to update BSS table and start Provision Discovery once
  2328. * the new scan results become available.
  2329. */
  2330. wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
  2331. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  2332. ret = ieee80211_sta_req_scan(wpa_s, &params);
  2333. else
  2334. ret = wpa_drv_scan(wpa_s, &params);
  2335. wpabuf_free(ies);
  2336. if (ret) {
  2337. wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
  2338. "try again later");
  2339. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2340. eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
  2341. wpas_p2p_check_join_scan_limit(wpa_s);
  2342. }
  2343. }
  2344. static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
  2345. const u8 *dev_addr, enum p2p_wps_method wps_method)
  2346. {
  2347. wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
  2348. MACSTR " dev " MACSTR ")",
  2349. MAC2STR(iface_addr), MAC2STR(dev_addr));
  2350. os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
  2351. os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
  2352. wpa_s->pending_join_wps_method = wps_method;
  2353. /* Make sure we are not running find during connection establishment */
  2354. wpas_p2p_stop_find(wpa_s);
  2355. wpa_s->p2p_join_scan_count = 0;
  2356. wpas_p2p_join_scan(wpa_s, NULL);
  2357. return 0;
  2358. }
  2359. static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
  2360. {
  2361. struct wpa_supplicant *group;
  2362. struct p2p_go_neg_results res;
  2363. group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
  2364. if (group == NULL)
  2365. return -1;
  2366. if (group != wpa_s) {
  2367. os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
  2368. sizeof(group->p2p_pin));
  2369. group->p2p_wps_method = wpa_s->p2p_wps_method;
  2370. }
  2371. group->p2p_in_provisioning = 1;
  2372. os_memset(&res, 0, sizeof(res));
  2373. os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
  2374. ETH_ALEN);
  2375. res.wps_method = wpa_s->pending_join_wps_method;
  2376. wpas_start_wps_enrollee(group, &res);
  2377. /*
  2378. * Allow a longer timeout for join-a-running-group than normal 15
  2379. * second group formation timeout since the GO may not have authorized
  2380. * our connection yet.
  2381. */
  2382. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  2383. eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
  2384. wpa_s, NULL);
  2385. return 0;
  2386. }
  2387. /**
  2388. * wpas_p2p_connect - Request P2P Group Formation to be started
  2389. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2390. * @peer_addr: Address of the peer P2P Device
  2391. * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
  2392. * @persistent_group: Whether to create a persistent group
  2393. * @join: Whether to join an existing group (as a client) instead of starting
  2394. * Group Owner negotiation; @peer_addr is BSSID in that case
  2395. * @auth: Whether to only authorize the connection instead of doing that and
  2396. * initiating Group Owner negotiation
  2397. * @go_intent: GO Intent or -1 to use default
  2398. * @freq: Frequency for the group or 0 for auto-selection
  2399. * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
  2400. * failure, -2 on failure due to channel not currently available,
  2401. * -3 if forced channel is not supported
  2402. */
  2403. int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  2404. const char *pin, enum p2p_wps_method wps_method,
  2405. int persistent_group, int join, int auth, int go_intent,
  2406. int freq)
  2407. {
  2408. int force_freq = 0, oper_freq = 0;
  2409. u8 bssid[ETH_ALEN];
  2410. int ret = 0;
  2411. enum wpa_driver_if_type iftype;
  2412. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  2413. return -1;
  2414. if (go_intent < 0)
  2415. go_intent = wpa_s->conf->p2p_go_intent;
  2416. if (!auth)
  2417. wpa_s->p2p_long_listen = 0;
  2418. wpa_s->p2p_wps_method = wps_method;
  2419. if (pin)
  2420. os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
  2421. else if (wps_method == WPS_PIN_DISPLAY) {
  2422. ret = wps_generate_pin();
  2423. os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
  2424. ret);
  2425. wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
  2426. wpa_s->p2p_pin);
  2427. } else
  2428. wpa_s->p2p_pin[0] = '\0';
  2429. if (join) {
  2430. u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
  2431. if (auth) {
  2432. wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
  2433. "connect a running group from " MACSTR,
  2434. MAC2STR(peer_addr));
  2435. os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
  2436. return ret;
  2437. }
  2438. os_memcpy(dev_addr, peer_addr, ETH_ALEN);
  2439. if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
  2440. iface_addr) < 0) {
  2441. os_memcpy(iface_addr, peer_addr, ETH_ALEN);
  2442. p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
  2443. dev_addr);
  2444. }
  2445. if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method) <
  2446. 0)
  2447. return -1;
  2448. return ret;
  2449. }
  2450. if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
  2451. wpa_s->assoc_freq)
  2452. oper_freq = wpa_s->assoc_freq;
  2453. else {
  2454. oper_freq = wpa_drv_shared_freq(wpa_s);
  2455. if (oper_freq < 0)
  2456. oper_freq = 0;
  2457. }
  2458. if (freq > 0) {
  2459. if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
  2460. wpa_printf(MSG_DEBUG, "P2P: The forced channel "
  2461. "(%u MHz) is not supported for P2P uses",
  2462. freq);
  2463. return -3;
  2464. }
  2465. if (oper_freq > 0 && freq != oper_freq &&
  2466. !(wpa_s->drv_flags &
  2467. WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
  2468. wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
  2469. "on %u MHz while connected on another "
  2470. "channel (%u MHz)", freq, oper_freq);
  2471. return -2;
  2472. }
  2473. wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
  2474. "requested channel (%u MHz)", freq);
  2475. force_freq = freq;
  2476. } else if (oper_freq > 0 &&
  2477. !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
  2478. if (!(wpa_s->drv_flags &
  2479. WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
  2480. wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
  2481. "while connected on non-P2P supported "
  2482. "channel (%u MHz)", oper_freq);
  2483. return -2;
  2484. }
  2485. wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
  2486. "(%u MHz) not available for P2P - try to use "
  2487. "another channel", oper_freq);
  2488. force_freq = 0;
  2489. } else if (oper_freq > 0) {
  2490. wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
  2491. "channel we are already using (%u MHz) on another "
  2492. "interface", oper_freq);
  2493. force_freq = oper_freq;
  2494. }
  2495. wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
  2496. if (!wpa_s->create_p2p_iface) {
  2497. if (auth) {
  2498. if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
  2499. go_intent, wpa_s->own_addr,
  2500. force_freq, persistent_group)
  2501. < 0)
  2502. return -1;
  2503. return ret;
  2504. }
  2505. if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
  2506. go_intent, wpa_s->own_addr,
  2507. force_freq, persistent_group) < 0)
  2508. return -1;
  2509. return ret;
  2510. }
  2511. /* Prepare to add a new interface for the group */
  2512. iftype = WPA_IF_P2P_GROUP;
  2513. if (join)
  2514. iftype = WPA_IF_P2P_CLIENT;
  2515. else if (go_intent == 15)
  2516. iftype = WPA_IF_P2P_GO;
  2517. if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
  2518. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  2519. "interface for the group");
  2520. return -1;
  2521. }
  2522. if (auth) {
  2523. if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
  2524. go_intent,
  2525. wpa_s->pending_interface_addr,
  2526. force_freq, persistent_group) < 0)
  2527. return -1;
  2528. return ret;
  2529. }
  2530. if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method, go_intent,
  2531. wpa_s->pending_interface_addr,
  2532. force_freq, persistent_group) < 0) {
  2533. wpas_p2p_remove_pending_group_interface(wpa_s);
  2534. return -1;
  2535. }
  2536. return ret;
  2537. }
  2538. /**
  2539. * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
  2540. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2541. * @freq: Frequency of the channel in MHz
  2542. * @duration: Duration of the stay on the channel in milliseconds
  2543. *
  2544. * This callback is called when the driver indicates that it has started the
  2545. * requested remain-on-channel duration.
  2546. */
  2547. void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  2548. unsigned int freq, unsigned int duration)
  2549. {
  2550. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  2551. return;
  2552. wpa_s->roc_waiting_drv_freq = 0;
  2553. wpa_s->off_channel_freq = freq;
  2554. wpas_send_action_cb(wpa_s, NULL);
  2555. if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
  2556. p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
  2557. wpa_s->pending_listen_duration);
  2558. wpa_s->pending_listen_freq = 0;
  2559. }
  2560. }
  2561. static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
  2562. unsigned int timeout)
  2563. {
  2564. /* Limit maximum Listen state time based on driver limitation. */
  2565. if (timeout > wpa_s->max_remain_on_chan)
  2566. timeout = wpa_s->max_remain_on_chan;
  2567. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  2568. return wpa_drv_p2p_listen(wpa_s, timeout);
  2569. return p2p_listen(wpa_s->global->p2p, timeout);
  2570. }
  2571. /**
  2572. * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
  2573. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2574. * @freq: Frequency of the channel in MHz
  2575. *
  2576. * This callback is called when the driver indicates that a remain-on-channel
  2577. * operation has been completed, i.e., the duration on the requested channel
  2578. * has timed out.
  2579. */
  2580. void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  2581. unsigned int freq)
  2582. {
  2583. wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
  2584. "(p2p_long_listen=%d ms pending_action_tx=%p)",
  2585. wpa_s->p2p_long_listen, wpa_s->pending_action_tx);
  2586. wpa_s->off_channel_freq = 0;
  2587. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  2588. return;
  2589. if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
  2590. return; /* P2P module started a new operation */
  2591. if (wpa_s->pending_action_tx)
  2592. return;
  2593. if (wpa_s->p2p_long_listen > 0)
  2594. wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
  2595. if (wpa_s->p2p_long_listen > 0) {
  2596. wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
  2597. wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
  2598. }
  2599. }
  2600. /**
  2601. * wpas_p2p_group_remove - Remove a P2P group
  2602. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2603. * @ifname: Network interface name of the group interface or "*" to remove all
  2604. * groups
  2605. * Returns: 0 on success, -1 on failure
  2606. *
  2607. * This function is used to remove a P2P group. This can be used to disconnect
  2608. * from a group in which the local end is a P2P Client or to end a P2P Group in
  2609. * case the local end is the Group Owner. If a virtual network interface was
  2610. * created for this group, that interface will be removed. Otherwise, only the
  2611. * configured P2P group network will be removed from the interface.
  2612. */
  2613. int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
  2614. {
  2615. struct wpa_global *global = wpa_s->global;
  2616. if (os_strcmp(ifname, "*") == 0) {
  2617. struct wpa_supplicant *prev;
  2618. wpa_s = global->ifaces;
  2619. while (wpa_s) {
  2620. prev = wpa_s;
  2621. wpa_s = wpa_s->next;
  2622. wpas_p2p_disconnect(prev);
  2623. }
  2624. return 0;
  2625. }
  2626. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2627. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  2628. break;
  2629. }
  2630. return wpas_p2p_disconnect(wpa_s);
  2631. }
  2632. static void wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
  2633. struct p2p_go_neg_results *params,
  2634. int freq)
  2635. {
  2636. u8 bssid[ETH_ALEN];
  2637. int res;
  2638. os_memset(params, 0, sizeof(*params));
  2639. params->role_go = 1;
  2640. if (freq) {
  2641. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
  2642. "frequency %d MHz", freq);
  2643. params->freq = freq;
  2644. } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
  2645. wpa_s->conf->p2p_oper_channel >= 1 &&
  2646. wpa_s->conf->p2p_oper_channel <= 11) {
  2647. params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
  2648. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
  2649. "frequency %d MHz", params->freq);
  2650. } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
  2651. wpa_s->conf->p2p_oper_reg_class == 118) {
  2652. params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
  2653. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
  2654. "frequency %d MHz", params->freq);
  2655. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  2656. wpa_s->best_overall_freq > 0 &&
  2657. p2p_supported_freq(wpa_s->global->p2p,
  2658. wpa_s->best_overall_freq)) {
  2659. params->freq = wpa_s->best_overall_freq;
  2660. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
  2661. "channel %d MHz", params->freq);
  2662. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  2663. wpa_s->best_24_freq > 0 &&
  2664. p2p_supported_freq(wpa_s->global->p2p,
  2665. wpa_s->best_24_freq)) {
  2666. params->freq = wpa_s->best_24_freq;
  2667. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
  2668. "channel %d MHz", params->freq);
  2669. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  2670. wpa_s->best_5_freq > 0 &&
  2671. p2p_supported_freq(wpa_s->global->p2p,
  2672. wpa_s->best_5_freq)) {
  2673. params->freq = wpa_s->best_5_freq;
  2674. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
  2675. "channel %d MHz", params->freq);
  2676. } else {
  2677. params->freq = 2412;
  2678. wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
  2679. "known)", params->freq);
  2680. }
  2681. if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
  2682. wpa_s->assoc_freq && !freq) {
  2683. wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
  2684. "already using");
  2685. params->freq = wpa_s->assoc_freq;
  2686. }
  2687. res = wpa_drv_shared_freq(wpa_s);
  2688. if (res > 0 && !freq) {
  2689. wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
  2690. "already using on a shared interface");
  2691. params->freq = res;
  2692. }
  2693. }
  2694. static struct wpa_supplicant *
  2695. wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
  2696. int go)
  2697. {
  2698. struct wpa_supplicant *group_wpa_s;
  2699. if (!wpas_p2p_create_iface(wpa_s))
  2700. return wpa_s;
  2701. if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
  2702. WPA_IF_P2P_CLIENT) < 0)
  2703. return NULL;
  2704. group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
  2705. if (group_wpa_s == NULL) {
  2706. wpas_p2p_remove_pending_group_interface(wpa_s);
  2707. return NULL;
  2708. }
  2709. return group_wpa_s;
  2710. }
  2711. /**
  2712. * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
  2713. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2714. * @persistent_group: Whether to create a persistent group
  2715. * @freq: Frequency for the group or 0 to indicate no hardcoding
  2716. * Returns: 0 on success, -1 on failure
  2717. *
  2718. * This function creates a new P2P group with the local end as the Group Owner,
  2719. * i.e., without using Group Owner Negotiation.
  2720. */
  2721. int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
  2722. int freq)
  2723. {
  2724. struct p2p_go_neg_results params;
  2725. unsigned int r;
  2726. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  2727. return -1;
  2728. if (freq == 2) {
  2729. wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
  2730. "band");
  2731. if (wpa_s->best_24_freq > 0 &&
  2732. p2p_supported_freq(wpa_s->global->p2p,
  2733. wpa_s->best_24_freq)) {
  2734. freq = wpa_s->best_24_freq;
  2735. wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
  2736. "channel: %d MHz", freq);
  2737. } else {
  2738. os_get_random((u8 *) &r, sizeof(r));
  2739. freq = 2412 + (r % 3) * 25;
  2740. wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
  2741. "channel: %d MHz", freq);
  2742. }
  2743. }
  2744. if (freq == 5) {
  2745. wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
  2746. "band");
  2747. if (wpa_s->best_5_freq > 0 &&
  2748. p2p_supported_freq(wpa_s->global->p2p,
  2749. wpa_s->best_5_freq)) {
  2750. freq = wpa_s->best_5_freq;
  2751. wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
  2752. "channel: %d MHz", freq);
  2753. } else {
  2754. os_get_random((u8 *) &r, sizeof(r));
  2755. freq = 5180 + (r % 4) * 20;
  2756. if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
  2757. wpa_printf(MSG_DEBUG, "P2P: Could not select "
  2758. "5 GHz channel for P2P group");
  2759. return -1;
  2760. }
  2761. wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
  2762. "channel: %d MHz", freq);
  2763. }
  2764. }
  2765. if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
  2766. wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
  2767. "(%u MHz) is not supported for P2P uses",
  2768. freq);
  2769. return -1;
  2770. }
  2771. wpas_p2p_init_go_params(wpa_s, &params, freq);
  2772. p2p_go_params(wpa_s->global->p2p, &params);
  2773. params.persistent_group = persistent_group;
  2774. wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
  2775. if (wpa_s == NULL)
  2776. return -1;
  2777. wpas_start_wps_go(wpa_s, &params, 0);
  2778. return 0;
  2779. }
  2780. static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
  2781. struct wpa_ssid *params, int addr_allocated)
  2782. {
  2783. struct wpa_ssid *ssid;
  2784. wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
  2785. if (wpa_s == NULL)
  2786. return -1;
  2787. wpa_supplicant_ap_deinit(wpa_s);
  2788. ssid = wpa_config_add_network(wpa_s->conf);
  2789. if (ssid == NULL)
  2790. return -1;
  2791. wpas_notify_network_added(wpa_s, ssid);
  2792. wpa_config_set_network_defaults(ssid);
  2793. ssid->temporary = 1;
  2794. ssid->proto = WPA_PROTO_RSN;
  2795. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  2796. ssid->group_cipher = WPA_CIPHER_CCMP;
  2797. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  2798. ssid->ssid = os_malloc(params->ssid_len);
  2799. if (ssid->ssid == NULL) {
  2800. wpas_notify_network_removed(wpa_s, ssid);
  2801. wpa_config_remove_network(wpa_s->conf, ssid->id);
  2802. return -1;
  2803. }
  2804. os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
  2805. ssid->ssid_len = params->ssid_len;
  2806. ssid->p2p_group = 1;
  2807. ssid->export_keys = 1;
  2808. if (params->psk_set) {
  2809. os_memcpy(ssid->psk, params->psk, 32);
  2810. ssid->psk_set = 1;
  2811. }
  2812. if (params->passphrase)
  2813. ssid->passphrase = os_strdup(params->passphrase);
  2814. wpa_supplicant_select_network(wpa_s, ssid);
  2815. wpa_s->show_group_started = 1;
  2816. return 0;
  2817. }
  2818. int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
  2819. struct wpa_ssid *ssid, int addr_allocated,
  2820. int freq)
  2821. {
  2822. struct p2p_go_neg_results params;
  2823. int go = 0;
  2824. if (ssid->disabled != 2 || ssid->ssid == NULL)
  2825. return -1;
  2826. if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
  2827. go == (ssid->mode == WPAS_MODE_P2P_GO)) {
  2828. wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
  2829. "already running");
  2830. return 0;
  2831. }
  2832. /* Make sure we are not running find during connection establishment */
  2833. wpas_p2p_stop_find(wpa_s);
  2834. if (ssid->mode == WPAS_MODE_INFRA)
  2835. return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
  2836. if (ssid->mode != WPAS_MODE_P2P_GO)
  2837. return -1;
  2838. wpas_p2p_init_go_params(wpa_s, &params, freq);
  2839. params.role_go = 1;
  2840. if (ssid->passphrase == NULL ||
  2841. os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
  2842. wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
  2843. "group");
  2844. return -1;
  2845. }
  2846. os_strlcpy(params.passphrase, ssid->passphrase,
  2847. sizeof(params.passphrase));
  2848. os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
  2849. params.ssid_len = ssid->ssid_len;
  2850. params.persistent_group = 1;
  2851. wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
  2852. if (wpa_s == NULL)
  2853. return -1;
  2854. wpas_start_wps_go(wpa_s, &params, 0);
  2855. return 0;
  2856. }
  2857. static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
  2858. struct wpabuf *proberesp_ies)
  2859. {
  2860. struct wpa_supplicant *wpa_s = ctx;
  2861. if (wpa_s->ap_iface) {
  2862. struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
  2863. if (beacon_ies) {
  2864. wpabuf_free(hapd->p2p_beacon_ie);
  2865. hapd->p2p_beacon_ie = beacon_ies;
  2866. }
  2867. wpabuf_free(hapd->p2p_probe_resp_ie);
  2868. hapd->p2p_probe_resp_ie = proberesp_ies;
  2869. } else {
  2870. wpabuf_free(beacon_ies);
  2871. wpabuf_free(proberesp_ies);
  2872. }
  2873. wpa_supplicant_ap_update_beacon(wpa_s);
  2874. }
  2875. static void wpas_p2p_idle_update(void *ctx, int idle)
  2876. {
  2877. struct wpa_supplicant *wpa_s = ctx;
  2878. if (!wpa_s->ap_iface)
  2879. return;
  2880. wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
  2881. if (idle)
  2882. wpas_p2p_set_group_idle_timeout(wpa_s);
  2883. else
  2884. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  2885. }
  2886. struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
  2887. int persistent_group,
  2888. int group_formation)
  2889. {
  2890. struct p2p_group *group;
  2891. struct p2p_group_config *cfg;
  2892. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  2893. return NULL;
  2894. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  2895. return NULL;
  2896. cfg = os_zalloc(sizeof(*cfg));
  2897. if (cfg == NULL)
  2898. return NULL;
  2899. cfg->persistent_group = persistent_group;
  2900. os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
  2901. if (wpa_s->max_stations &&
  2902. wpa_s->max_stations < wpa_s->conf->max_num_sta)
  2903. cfg->max_clients = wpa_s->max_stations;
  2904. else
  2905. cfg->max_clients = wpa_s->conf->max_num_sta;
  2906. cfg->cb_ctx = wpa_s;
  2907. cfg->ie_update = wpas_p2p_ie_update;
  2908. cfg->idle_update = wpas_p2p_idle_update;
  2909. group = p2p_group_init(wpa_s->global->p2p, cfg);
  2910. if (group == NULL)
  2911. os_free(cfg);
  2912. if (!group_formation)
  2913. p2p_group_notif_formation_done(group);
  2914. wpa_s->p2p_group = group;
  2915. return group;
  2916. }
  2917. void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  2918. int registrar)
  2919. {
  2920. if (!wpa_s->p2p_in_provisioning) {
  2921. wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
  2922. "provisioning not in progress");
  2923. return;
  2924. }
  2925. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
  2926. NULL);
  2927. if (wpa_s->global->p2p)
  2928. p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
  2929. else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  2930. wpa_drv_wps_success_cb(wpa_s, peer_addr);
  2931. wpas_group_formation_completed(wpa_s, 1);
  2932. }
  2933. int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  2934. const char *config_method)
  2935. {
  2936. u16 config_methods;
  2937. if (os_strcmp(config_method, "display") == 0)
  2938. config_methods = WPS_CONFIG_DISPLAY;
  2939. else if (os_strcmp(config_method, "keypad") == 0)
  2940. config_methods = WPS_CONFIG_KEYPAD;
  2941. else if (os_strcmp(config_method, "pbc") == 0 ||
  2942. os_strcmp(config_method, "pushbutton") == 0)
  2943. config_methods = WPS_CONFIG_PUSHBUTTON;
  2944. else
  2945. return -1;
  2946. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  2947. return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
  2948. config_methods);
  2949. }
  2950. if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
  2951. return -1;
  2952. return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
  2953. config_methods, 0);
  2954. }
  2955. int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
  2956. char *end)
  2957. {
  2958. return p2p_scan_result_text(ies, ies_len, buf, end);
  2959. }
  2960. static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
  2961. {
  2962. if (!wpa_s->pending_action_tx)
  2963. return;
  2964. wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
  2965. "operation request");
  2966. wpabuf_free(wpa_s->pending_action_tx);
  2967. wpa_s->pending_action_tx = NULL;
  2968. }
  2969. int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
  2970. enum p2p_discovery_type type)
  2971. {
  2972. wpas_p2p_clear_pending_action_tx(wpa_s);
  2973. wpa_s->p2p_long_listen = 0;
  2974. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  2975. return wpa_drv_p2p_find(wpa_s, timeout, type);
  2976. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  2977. return -1;
  2978. return p2p_find(wpa_s->global->p2p, timeout, type);
  2979. }
  2980. void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
  2981. {
  2982. wpas_p2p_clear_pending_action_tx(wpa_s);
  2983. wpa_s->p2p_long_listen = 0;
  2984. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  2985. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2986. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  2987. wpa_drv_p2p_stop_find(wpa_s);
  2988. return;
  2989. }
  2990. if (wpa_s->global->p2p)
  2991. p2p_stop_find(wpa_s->global->p2p);
  2992. wpas_p2p_remove_pending_group_interface(wpa_s);
  2993. }
  2994. static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
  2995. {
  2996. struct wpa_supplicant *wpa_s = eloop_ctx;
  2997. wpa_s->p2p_long_listen = 0;
  2998. }
  2999. int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
  3000. {
  3001. int res;
  3002. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3003. return -1;
  3004. wpas_p2p_clear_pending_action_tx(wpa_s);
  3005. if (timeout == 0) {
  3006. /*
  3007. * This is a request for unlimited Listen state. However, at
  3008. * least for now, this is mapped to a Listen state for one
  3009. * hour.
  3010. */
  3011. timeout = 3600;
  3012. }
  3013. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  3014. wpa_s->p2p_long_listen = 0;
  3015. res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
  3016. if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
  3017. wpa_s->p2p_long_listen = timeout * 1000;
  3018. eloop_register_timeout(timeout, 0,
  3019. wpas_p2p_long_listen_timeout,
  3020. wpa_s, NULL);
  3021. }
  3022. return res;
  3023. }
  3024. int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  3025. u8 *buf, size_t len, int p2p_group)
  3026. {
  3027. struct wpabuf *p2p_ie;
  3028. int ret;
  3029. if (wpa_s->global->p2p_disabled)
  3030. return -1;
  3031. if (wpa_s->global->p2p == NULL)
  3032. return -1;
  3033. if (bss == NULL)
  3034. return -1;
  3035. p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  3036. ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
  3037. p2p_group, p2p_ie);
  3038. wpabuf_free(p2p_ie);
  3039. return ret;
  3040. }
  3041. int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
  3042. const u8 *ie, size_t ie_len)
  3043. {
  3044. if (wpa_s->global->p2p_disabled)
  3045. return 0;
  3046. if (wpa_s->global->p2p == NULL)
  3047. return 0;
  3048. return p2p_probe_req_rx(wpa_s->global->p2p, addr, ie, ie_len);
  3049. }
  3050. void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
  3051. const u8 *sa, const u8 *bssid,
  3052. u8 category, const u8 *data, size_t len, int freq)
  3053. {
  3054. if (wpa_s->global->p2p_disabled)
  3055. return;
  3056. if (wpa_s->global->p2p == NULL)
  3057. return;
  3058. p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
  3059. freq);
  3060. }
  3061. void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
  3062. {
  3063. if (wpa_s->global->p2p_disabled)
  3064. return;
  3065. if (wpa_s->global->p2p == NULL)
  3066. return;
  3067. p2p_scan_ie(wpa_s->global->p2p, ies);
  3068. }
  3069. void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
  3070. {
  3071. p2p_group_deinit(wpa_s->p2p_group);
  3072. wpa_s->p2p_group = NULL;
  3073. }
  3074. int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
  3075. {
  3076. wpa_s->p2p_long_listen = 0;
  3077. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3078. return wpa_drv_p2p_reject(wpa_s, addr);
  3079. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3080. return -1;
  3081. return p2p_reject(wpa_s->global->p2p, addr);
  3082. }
  3083. /* Invite to reinvoke a persistent group */
  3084. int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  3085. struct wpa_ssid *ssid, const u8 *go_dev_addr)
  3086. {
  3087. enum p2p_invite_role role;
  3088. u8 *bssid = NULL;
  3089. if (ssid->mode == WPAS_MODE_P2P_GO) {
  3090. role = P2P_INVITE_ROLE_GO;
  3091. if (peer_addr == NULL) {
  3092. wpa_printf(MSG_DEBUG, "P2P: Missing peer "
  3093. "address in invitation command");
  3094. return -1;
  3095. }
  3096. if (wpas_p2p_create_iface(wpa_s)) {
  3097. if (wpas_p2p_add_group_interface(wpa_s,
  3098. WPA_IF_P2P_GO) < 0) {
  3099. wpa_printf(MSG_ERROR, "P2P: Failed to "
  3100. "allocate a new interface for the "
  3101. "group");
  3102. return -1;
  3103. }
  3104. bssid = wpa_s->pending_interface_addr;
  3105. } else
  3106. bssid = wpa_s->own_addr;
  3107. } else {
  3108. role = P2P_INVITE_ROLE_CLIENT;
  3109. peer_addr = ssid->bssid;
  3110. }
  3111. wpa_s->pending_invite_ssid_id = ssid->id;
  3112. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3113. return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
  3114. ssid->ssid, ssid->ssid_len,
  3115. go_dev_addr, 1);
  3116. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3117. return -1;
  3118. return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
  3119. ssid->ssid, ssid->ssid_len, 0, go_dev_addr, 1);
  3120. }
  3121. /* Invite to join an active group */
  3122. int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
  3123. const u8 *peer_addr, const u8 *go_dev_addr)
  3124. {
  3125. struct wpa_global *global = wpa_s->global;
  3126. enum p2p_invite_role role;
  3127. u8 *bssid = NULL;
  3128. struct wpa_ssid *ssid;
  3129. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3130. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  3131. break;
  3132. }
  3133. if (wpa_s == NULL) {
  3134. wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
  3135. return -1;
  3136. }
  3137. ssid = wpa_s->current_ssid;
  3138. if (ssid == NULL) {
  3139. wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
  3140. "invitation");
  3141. return -1;
  3142. }
  3143. if (ssid->mode == WPAS_MODE_P2P_GO) {
  3144. role = P2P_INVITE_ROLE_ACTIVE_GO;
  3145. bssid = wpa_s->own_addr;
  3146. if (go_dev_addr == NULL)
  3147. go_dev_addr = wpa_s->parent->own_addr;
  3148. } else {
  3149. role = P2P_INVITE_ROLE_CLIENT;
  3150. if (wpa_s->wpa_state < WPA_ASSOCIATED) {
  3151. wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
  3152. "invite to current group");
  3153. return -1;
  3154. }
  3155. bssid = wpa_s->bssid;
  3156. if (go_dev_addr == NULL &&
  3157. !is_zero_ether_addr(wpa_s->go_dev_addr))
  3158. go_dev_addr = wpa_s->go_dev_addr;
  3159. }
  3160. wpa_s->parent->pending_invite_ssid_id = -1;
  3161. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3162. return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
  3163. ssid->ssid, ssid->ssid_len,
  3164. go_dev_addr, 0);
  3165. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3166. return -1;
  3167. return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
  3168. ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
  3169. go_dev_addr, 0);
  3170. }
  3171. void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
  3172. {
  3173. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3174. const char *ssid_txt;
  3175. u8 go_dev_addr[ETH_ALEN];
  3176. int persistent;
  3177. if (!wpa_s->show_group_started || !ssid)
  3178. return;
  3179. wpa_s->show_group_started = 0;
  3180. ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
  3181. os_memset(go_dev_addr, 0, ETH_ALEN);
  3182. if (ssid->bssid_set)
  3183. os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
  3184. persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
  3185. ssid->ssid_len);
  3186. os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
  3187. if (wpa_s->global->p2p_group_formation == wpa_s)
  3188. wpa_s->global->p2p_group_formation = NULL;
  3189. if (ssid->passphrase == NULL && ssid->psk_set) {
  3190. char psk[65];
  3191. wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
  3192. wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  3193. "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
  3194. MACSTR "%s",
  3195. wpa_s->ifname, ssid_txt, ssid->frequency, psk,
  3196. MAC2STR(go_dev_addr),
  3197. persistent ? " [PERSISTENT]" : "");
  3198. } else {
  3199. wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  3200. "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
  3201. "go_dev_addr=" MACSTR "%s",
  3202. wpa_s->ifname, ssid_txt, ssid->frequency,
  3203. ssid->passphrase ? ssid->passphrase : "",
  3204. MAC2STR(go_dev_addr),
  3205. persistent ? " [PERSISTENT]" : "");
  3206. }
  3207. if (persistent)
  3208. wpas_p2p_store_persistent_group(wpa_s->parent, ssid,
  3209. go_dev_addr);
  3210. }
  3211. int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
  3212. u32 interval1, u32 duration2, u32 interval2)
  3213. {
  3214. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3215. return -1;
  3216. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3217. return -1;
  3218. if (wpa_s->wpa_state < WPA_ASSOCIATED ||
  3219. wpa_s->current_ssid == NULL ||
  3220. wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
  3221. return -1;
  3222. return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
  3223. wpa_s->own_addr, wpa_s->assoc_freq,
  3224. duration1, interval1, duration2, interval2);
  3225. }
  3226. int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
  3227. unsigned int interval)
  3228. {
  3229. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3230. return -1;
  3231. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3232. return -1;
  3233. return p2p_ext_listen(wpa_s->global->p2p, period, interval);
  3234. }
  3235. static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
  3236. {
  3237. struct wpa_supplicant *wpa_s = eloop_ctx;
  3238. if (wpa_s->conf->p2p_group_idle == 0) {
  3239. wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
  3240. "disabled");
  3241. return;
  3242. }
  3243. wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
  3244. "group");
  3245. wpa_s->removal_reason = P2P_GROUP_REMOVAL_IDLE_TIMEOUT;
  3246. wpas_p2p_group_delete(wpa_s);
  3247. }
  3248. static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
  3249. {
  3250. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  3251. if (wpa_s->conf->p2p_group_idle == 0)
  3252. return;
  3253. if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
  3254. return;
  3255. wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
  3256. wpa_s->conf->p2p_group_idle);
  3257. eloop_register_timeout(wpa_s->conf->p2p_group_idle, 0,
  3258. wpas_p2p_group_idle_timeout, wpa_s, NULL);
  3259. }
  3260. void wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
  3261. u16 reason_code, const u8 *ie, size_t ie_len)
  3262. {
  3263. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3264. return;
  3265. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3266. return;
  3267. p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
  3268. }
  3269. void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
  3270. u16 reason_code, const u8 *ie, size_t ie_len)
  3271. {
  3272. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3273. return;
  3274. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3275. return;
  3276. p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
  3277. }
  3278. void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
  3279. {
  3280. struct p2p_data *p2p = wpa_s->global->p2p;
  3281. if (p2p == NULL)
  3282. return;
  3283. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
  3284. return;
  3285. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
  3286. p2p_set_dev_name(p2p, wpa_s->conf->device_name);
  3287. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE) {
  3288. u8 pri_dev_type[8];
  3289. if (wpa_s->conf->device_type) {
  3290. if (wps_dev_type_str2bin(wpa_s->conf->device_type,
  3291. pri_dev_type) < 0) {
  3292. wpa_printf(MSG_ERROR, "P2P: Invalid "
  3293. "device_type");
  3294. } else
  3295. p2p_set_pri_dev_type(p2p, pri_dev_type);
  3296. }
  3297. }
  3298. if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
  3299. u8 sec_dev_type[P2P_SEC_DEVICE_TYPES][8];
  3300. size_t num = 0;
  3301. int i;
  3302. for (i = 0; i < MAX_SEC_DEVICE_TYPES; i++) {
  3303. if (wpa_s->conf->sec_device_type[i] == NULL)
  3304. continue;
  3305. if (wps_dev_type_str2bin(
  3306. wpa_s->conf->sec_device_type[i],
  3307. sec_dev_type[num]) < 0) {
  3308. wpa_printf(MSG_ERROR, "P2P: Invalid "
  3309. "sec_device_type");
  3310. continue;
  3311. }
  3312. num++;
  3313. if (num == P2P_SEC_DEVICE_TYPES)
  3314. break;
  3315. }
  3316. p2p_set_sec_dev_types(p2p, (void *) sec_dev_type, num);
  3317. }
  3318. if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
  3319. wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  3320. char country[3];
  3321. country[0] = wpa_s->conf->country[0];
  3322. country[1] = wpa_s->conf->country[1];
  3323. country[2] = 0x04;
  3324. p2p_set_country(p2p, country);
  3325. }
  3326. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
  3327. p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
  3328. wpa_s->conf->p2p_ssid_postfix ?
  3329. os_strlen(wpa_s->conf->p2p_ssid_postfix) :
  3330. 0);
  3331. }
  3332. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
  3333. p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
  3334. }
  3335. int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
  3336. int duration)
  3337. {
  3338. if (!wpa_s->ap_iface)
  3339. return -1;
  3340. return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
  3341. duration);
  3342. }
  3343. int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
  3344. {
  3345. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3346. return -1;
  3347. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3348. return -1;
  3349. wpa_s->global->cross_connection = enabled;
  3350. p2p_set_cross_connect(wpa_s->global->p2p, enabled);
  3351. if (!enabled) {
  3352. struct wpa_supplicant *iface;
  3353. for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
  3354. {
  3355. if (iface->cross_connect_enabled == 0)
  3356. continue;
  3357. iface->cross_connect_enabled = 0;
  3358. iface->cross_connect_in_use = 0;
  3359. wpa_msg(iface->parent, MSG_INFO,
  3360. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  3361. iface->ifname, iface->cross_connect_uplink);
  3362. }
  3363. }
  3364. return 0;
  3365. }
  3366. static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
  3367. {
  3368. struct wpa_supplicant *iface;
  3369. if (!uplink->global->cross_connection)
  3370. return;
  3371. for (iface = uplink->global->ifaces; iface; iface = iface->next) {
  3372. if (!iface->cross_connect_enabled)
  3373. continue;
  3374. if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
  3375. 0)
  3376. continue;
  3377. if (iface->ap_iface == NULL)
  3378. continue;
  3379. if (iface->cross_connect_in_use)
  3380. continue;
  3381. iface->cross_connect_in_use = 1;
  3382. wpa_msg(iface->parent, MSG_INFO,
  3383. P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
  3384. iface->ifname, iface->cross_connect_uplink);
  3385. }
  3386. }
  3387. static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
  3388. {
  3389. struct wpa_supplicant *iface;
  3390. for (iface = uplink->global->ifaces; iface; iface = iface->next) {
  3391. if (!iface->cross_connect_enabled)
  3392. continue;
  3393. if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
  3394. 0)
  3395. continue;
  3396. if (!iface->cross_connect_in_use)
  3397. continue;
  3398. wpa_msg(iface->parent, MSG_INFO,
  3399. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  3400. iface->ifname, iface->cross_connect_uplink);
  3401. iface->cross_connect_in_use = 0;
  3402. }
  3403. }
  3404. void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
  3405. {
  3406. if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
  3407. wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
  3408. wpa_s->cross_connect_disallowed)
  3409. wpas_p2p_disable_cross_connect(wpa_s);
  3410. else
  3411. wpas_p2p_enable_cross_connect(wpa_s);
  3412. if (!wpa_s->ap_iface)
  3413. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  3414. }
  3415. void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
  3416. {
  3417. wpas_p2p_disable_cross_connect(wpa_s);
  3418. if (!wpa_s->ap_iface)
  3419. wpas_p2p_set_group_idle_timeout(wpa_s);
  3420. }
  3421. static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
  3422. {
  3423. struct wpa_supplicant *iface;
  3424. if (!wpa_s->global->cross_connection)
  3425. return;
  3426. for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
  3427. if (iface == wpa_s)
  3428. continue;
  3429. if (iface->drv_flags &
  3430. WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
  3431. continue;
  3432. if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
  3433. continue;
  3434. wpa_s->cross_connect_enabled = 1;
  3435. os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
  3436. sizeof(wpa_s->cross_connect_uplink));
  3437. wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
  3438. "%s to %s whenever uplink is available",
  3439. wpa_s->ifname, wpa_s->cross_connect_uplink);
  3440. if (iface->ap_iface || iface->current_ssid == NULL ||
  3441. iface->current_ssid->mode != WPAS_MODE_INFRA ||
  3442. iface->cross_connect_disallowed ||
  3443. iface->wpa_state != WPA_COMPLETED)
  3444. break;
  3445. wpa_s->cross_connect_in_use = 1;
  3446. wpa_msg(wpa_s->parent, MSG_INFO,
  3447. P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
  3448. wpa_s->ifname, wpa_s->cross_connect_uplink);
  3449. break;
  3450. }
  3451. }
  3452. int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
  3453. {
  3454. if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
  3455. !wpa_s->p2p_in_provisioning)
  3456. return 0; /* not P2P client operation */
  3457. wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
  3458. "session overlap");
  3459. if (wpa_s != wpa_s->parent)
  3460. wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
  3461. if (wpa_s->global->p2p)
  3462. p2p_group_formation_failed(wpa_s->global->p2p);
  3463. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  3464. wpa_s->parent, NULL);
  3465. wpas_group_formation_completed(wpa_s, 0);
  3466. return 1;
  3467. }
  3468. void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
  3469. {
  3470. struct p2p_channels chan;
  3471. if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
  3472. return;
  3473. os_memset(&chan, 0, sizeof(chan));
  3474. if (wpas_p2p_setup_channels(wpa_s, &chan)) {
  3475. wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
  3476. "channel list");
  3477. return;
  3478. }
  3479. p2p_update_channel_list(wpa_s->global->p2p, &chan);
  3480. }
  3481. int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
  3482. {
  3483. struct wpa_global *global = wpa_s->global;
  3484. int found = 0;
  3485. const u8 *peer;
  3486. if (global->p2p == NULL)
  3487. return -1;
  3488. wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
  3489. if (wpa_s->pending_interface_name[0] &&
  3490. !is_zero_ether_addr(wpa_s->pending_interface_addr))
  3491. found = 1;
  3492. peer = p2p_get_go_neg_peer(global->p2p);
  3493. if (peer) {
  3494. wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
  3495. MACSTR, MAC2STR(peer));
  3496. p2p_unauthorize(global->p2p, peer);
  3497. }
  3498. wpas_p2p_stop_find(wpa_s);
  3499. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3500. if (wpa_s == global->p2p_group_formation &&
  3501. (wpa_s->p2p_in_provisioning ||
  3502. wpa_s->parent->pending_interface_type ==
  3503. WPA_IF_P2P_CLIENT)) {
  3504. wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
  3505. "formation found - cancelling",
  3506. wpa_s->ifname);
  3507. found = 1;
  3508. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  3509. wpa_s->parent, NULL);
  3510. wpas_p2p_group_delete(wpa_s);
  3511. break;
  3512. }
  3513. }
  3514. if (!found) {
  3515. wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
  3516. return -1;
  3517. }
  3518. return 0;
  3519. }
  3520. void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
  3521. {
  3522. if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
  3523. return;
  3524. wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
  3525. "being available anymore");
  3526. wpa_s->removal_reason = P2P_GROUP_REMOVAL_UNAVAILABLE;
  3527. wpas_p2p_group_delete(wpa_s);
  3528. }
  3529. void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
  3530. int freq_24, int freq_5, int freq_overall)
  3531. {
  3532. struct p2p_data *p2p = wpa_s->global->p2p;
  3533. if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
  3534. return;
  3535. p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
  3536. }
  3537. int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
  3538. {
  3539. u8 peer[ETH_ALEN];
  3540. struct p2p_data *p2p = wpa_s->global->p2p;
  3541. if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
  3542. return -1;
  3543. if (hwaddr_aton(addr, peer))
  3544. return -1;
  3545. return p2p_unauthorize(p2p, peer);
  3546. }
  3547. /**
  3548. * wpas_p2p_disconnect - Disconnect from a P2P Group
  3549. * @wpa_s: Pointer to wpa_supplicant data
  3550. * Returns: 0 on success, -1 on failure
  3551. *
  3552. * This can be used to disconnect from a group in which the local end is a P2P
  3553. * Client or to end a P2P Group in case the local end is the Group Owner. If a
  3554. * virtual network interface was created for this group, that interface will be
  3555. * removed. Otherwise, only the configured P2P group network will be removed
  3556. * from the interface.
  3557. */
  3558. int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
  3559. {
  3560. if (wpa_s == NULL)
  3561. return -1;
  3562. wpa_s->removal_reason = P2P_GROUP_REMOVAL_REQUESTED;
  3563. wpas_p2p_group_delete(wpa_s);
  3564. return 0;
  3565. }