p2p_supplicant.c 190 KB

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