p2p_supplicant.c 136 KB

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