p2p_supplicant.c 122 KB

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