p2p_supplicant.c 183 KB

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