p2p_supplicant.c 112 KB

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