p2p_supplicant.c 192 KB

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