p2p_supplicant.c 183 KB

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