p2p_supplicant.c 131 KB

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