p2p_supplicant.c 182 KB

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