p2p_supplicant.c 164 KB

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