p2p_supplicant.c 113 KB

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