p2p_supplicant.c 115 KB

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