p2p_supplicant.c 119 KB

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