p2p_supplicant.c 182 KB

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