p2p_supplicant.c 113 KB

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