p2p_supplicant.c 259 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190
  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/wps_hostapd.h"
  22. #include "ap/p2p_hostapd.h"
  23. #include "ap/dfs.h"
  24. #include "eapol_supp/eapol_supp_sm.h"
  25. #include "rsn_supp/wpa.h"
  26. #include "wpa_supplicant_i.h"
  27. #include "driver_i.h"
  28. #include "ap.h"
  29. #include "config_ssid.h"
  30. #include "config.h"
  31. #include "notify.h"
  32. #include "scan.h"
  33. #include "bss.h"
  34. #include "offchannel.h"
  35. #include "wps_supplicant.h"
  36. #include "p2p_supplicant.h"
  37. #include "wifi_display.h"
  38. /*
  39. * How many times to try to scan to find the GO before giving up on join
  40. * request.
  41. */
  42. #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
  43. #define P2P_AUTO_PD_SCAN_ATTEMPTS 5
  44. /**
  45. * Defines time interval in seconds when a GO needs to evacuate a frequency that
  46. * it is currently using, but is no longer valid for P2P use cases.
  47. */
  48. #define P2P_GO_FREQ_CHANGE_TIME 5
  49. /**
  50. * Defines CSA parameters which are used when GO evacuates the no longer valid
  51. * channel (and if the driver supports channel switch).
  52. */
  53. #define P2P_GO_CSA_COUNT 7
  54. #define P2P_GO_CSA_BLOCK_TX 0
  55. #ifndef P2P_MAX_CLIENT_IDLE
  56. /*
  57. * How many seconds to try to reconnect to the GO when connection in P2P client
  58. * role has been lost.
  59. */
  60. #define P2P_MAX_CLIENT_IDLE 10
  61. #endif /* P2P_MAX_CLIENT_IDLE */
  62. #ifndef P2P_MAX_INITIAL_CONN_WAIT
  63. /*
  64. * How many seconds to wait for initial 4-way handshake to get completed after
  65. * WPS provisioning step or after the re-invocation of a persistent group on a
  66. * P2P Client.
  67. */
  68. #define P2P_MAX_INITIAL_CONN_WAIT 10
  69. #endif /* P2P_MAX_INITIAL_CONN_WAIT */
  70. #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
  71. /*
  72. * How many seconds to wait for initial 4-way handshake to get completed after
  73. * WPS provisioning step on the GO. This controls the extra time the P2P
  74. * operation is considered to be in progress (e.g., to delay other scans) after
  75. * WPS provisioning has been completed on the GO during group formation.
  76. */
  77. #define P2P_MAX_INITIAL_CONN_WAIT_GO 10
  78. #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
  79. #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
  80. /*
  81. * How many seconds to wait for initial 4-way handshake to get completed after
  82. * re-invocation of a persistent group on the GO when the client is expected
  83. * to connect automatically (no user interaction).
  84. */
  85. #define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
  86. #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
  87. #define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
  88. /*
  89. * How many seconds to wait to re-attempt to move GOs, in case previous attempt
  90. * was not possible.
  91. */
  92. #define P2P_RECONSIDER_GO_MOVE_DELAY 30
  93. enum p2p_group_removal_reason {
  94. P2P_GROUP_REMOVAL_UNKNOWN,
  95. P2P_GROUP_REMOVAL_SILENT,
  96. P2P_GROUP_REMOVAL_FORMATION_FAILED,
  97. P2P_GROUP_REMOVAL_REQUESTED,
  98. P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
  99. P2P_GROUP_REMOVAL_UNAVAILABLE,
  100. P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
  101. P2P_GROUP_REMOVAL_PSK_FAILURE,
  102. P2P_GROUP_REMOVAL_FREQ_CONFLICT,
  103. P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL
  104. };
  105. static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
  106. static struct wpa_supplicant *
  107. wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
  108. int go);
  109. static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
  110. const u8 *ssid, size_t ssid_len);
  111. static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
  112. int *force_freq, int *pref_freq, int go,
  113. unsigned int *pref_freq_list,
  114. unsigned int *num_pref_freq);
  115. static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
  116. const u8 *ssid, size_t ssid_len);
  117. static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
  118. static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
  119. const u8 *dev_addr, enum p2p_wps_method wps_method,
  120. int auto_join, int freq,
  121. const u8 *ssid, size_t ssid_len);
  122. static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
  123. static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
  124. static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
  125. static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
  126. static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
  127. void *timeout_ctx);
  128. static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
  129. static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
  130. int group_added);
  131. static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
  132. static void wpas_stop_listen(void *ctx);
  133. static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
  134. static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s);
  135. static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
  136. enum wpa_driver_if_type type);
  137. static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
  138. int already_deleted);
  139. static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
  140. struct wpa_used_freq_data *freqs,
  141. unsigned int num);
  142. static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx);
  143. static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq);
  144. static void
  145. wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
  146. struct wpa_used_freq_data *freqs, unsigned int num,
  147. enum wpas_p2p_channel_update_trig trig);
  148. static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx);
  149. /*
  150. * Get the number of concurrent channels that the HW can operate, but that are
  151. * currently not in use by any of the wpa_supplicant interfaces.
  152. */
  153. static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
  154. {
  155. int *freqs;
  156. int num, unused;
  157. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  158. if (!freqs)
  159. return -1;
  160. num = get_shared_radio_freqs(wpa_s, freqs,
  161. wpa_s->num_multichan_concurrent);
  162. os_free(freqs);
  163. unused = wpa_s->num_multichan_concurrent - num;
  164. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
  165. return unused;
  166. }
  167. /*
  168. * Get the frequencies that are currently in use by one or more of the virtual
  169. * interfaces, and that are also valid for P2P operation.
  170. */
  171. static unsigned int
  172. wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
  173. struct wpa_used_freq_data *p2p_freqs,
  174. unsigned int len)
  175. {
  176. struct wpa_used_freq_data *freqs;
  177. unsigned int num, i, j;
  178. freqs = os_calloc(wpa_s->num_multichan_concurrent,
  179. sizeof(struct wpa_used_freq_data));
  180. if (!freqs)
  181. return 0;
  182. num = get_shared_radio_freqs_data(wpa_s, freqs,
  183. wpa_s->num_multichan_concurrent);
  184. os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
  185. for (i = 0, j = 0; i < num && j < len; i++) {
  186. if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
  187. p2p_freqs[j++] = freqs[i];
  188. }
  189. os_free(freqs);
  190. dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
  191. return j;
  192. }
  193. static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
  194. int freq)
  195. {
  196. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  197. return;
  198. /* Use the wpa_s used to control the P2P Device operation */
  199. wpa_s = wpa_s->global->p2p_init_wpa_s;
  200. if (wpa_s->conf->p2p_ignore_shared_freq &&
  201. freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
  202. wpas_p2p_num_unused_channels(wpa_s) > 0) {
  203. wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
  204. freq);
  205. freq = 0;
  206. }
  207. p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
  208. }
  209. static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
  210. struct wpa_scan_results *scan_res)
  211. {
  212. size_t i;
  213. if (wpa_s->p2p_scan_work) {
  214. struct wpa_radio_work *work = wpa_s->p2p_scan_work;
  215. wpa_s->p2p_scan_work = NULL;
  216. radio_work_done(work);
  217. }
  218. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  219. return;
  220. wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
  221. (int) scan_res->num);
  222. for (i = 0; i < scan_res->num; i++) {
  223. struct wpa_scan_res *bss = scan_res->res[i];
  224. struct os_reltime time_tmp_age, entry_ts;
  225. const u8 *ies;
  226. size_t ies_len;
  227. time_tmp_age.sec = bss->age / 1000;
  228. time_tmp_age.usec = (bss->age % 1000) * 1000;
  229. os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
  230. ies = (const u8 *) (bss + 1);
  231. ies_len = bss->ie_len;
  232. if (bss->beacon_ie_len > 0 &&
  233. !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
  234. wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
  235. wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
  236. MACSTR, MAC2STR(bss->bssid));
  237. ies = ies + ies_len;
  238. ies_len = bss->beacon_ie_len;
  239. }
  240. if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
  241. bss->freq, &entry_ts, bss->level,
  242. ies, ies_len) > 0)
  243. break;
  244. }
  245. p2p_scan_res_handled(wpa_s->global->p2p);
  246. }
  247. static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
  248. {
  249. struct wpa_supplicant *wpa_s = work->wpa_s;
  250. struct wpa_driver_scan_params *params = work->ctx;
  251. int ret;
  252. if (deinit) {
  253. if (!work->started) {
  254. wpa_scan_free_params(params);
  255. return;
  256. }
  257. wpa_s->p2p_scan_work = NULL;
  258. return;
  259. }
  260. ret = wpa_drv_scan(wpa_s, params);
  261. wpa_scan_free_params(params);
  262. work->ctx = NULL;
  263. if (ret) {
  264. radio_work_done(work);
  265. p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
  266. return;
  267. }
  268. p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
  269. os_get_reltime(&wpa_s->scan_trigger_time);
  270. wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
  271. wpa_s->own_scan_requested = 1;
  272. wpa_s->p2p_scan_work = work;
  273. }
  274. static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
  275. int freq)
  276. {
  277. if (wpa_s->global->p2p_24ghz_social_channels &&
  278. (freq == 2412 || freq == 2437 || freq == 2462)) {
  279. /*
  280. * Search all social channels regardless of whether these have
  281. * been disabled for P2P operating channel use to avoid missing
  282. * peers.
  283. */
  284. return 1;
  285. }
  286. return p2p_supported_freq(wpa_s->global->p2p, freq);
  287. }
  288. static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
  289. unsigned int num_req_dev_types,
  290. const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
  291. {
  292. struct wpa_supplicant *wpa_s = ctx;
  293. struct wpa_driver_scan_params *params = NULL;
  294. struct wpabuf *wps_ie, *ies;
  295. unsigned int num_channels = 0;
  296. int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
  297. size_t ielen;
  298. u8 *n, i;
  299. unsigned int bands;
  300. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  301. return -1;
  302. if (wpa_s->p2p_scan_work) {
  303. wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
  304. return -1;
  305. }
  306. params = os_zalloc(sizeof(*params));
  307. if (params == NULL)
  308. return -1;
  309. /* P2P Wildcard SSID */
  310. params->num_ssids = 1;
  311. n = os_malloc(P2P_WILDCARD_SSID_LEN);
  312. if (n == NULL)
  313. goto fail;
  314. os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
  315. params->ssids[0].ssid = n;
  316. params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
  317. wpa_s->wps->dev.p2p = 1;
  318. wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
  319. wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
  320. num_req_dev_types, req_dev_types);
  321. if (wps_ie == NULL)
  322. goto fail;
  323. switch (type) {
  324. case P2P_SCAN_SOCIAL:
  325. params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
  326. sizeof(int));
  327. if (params->freqs == NULL)
  328. goto fail;
  329. for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
  330. if (wpas_p2p_search_social_channel(
  331. wpa_s, social_channels_freq[i]))
  332. params->freqs[num_channels++] =
  333. social_channels_freq[i];
  334. }
  335. params->freqs[num_channels++] = 0;
  336. break;
  337. case P2P_SCAN_FULL:
  338. break;
  339. case P2P_SCAN_SPECIFIC:
  340. params->freqs = os_calloc(2, sizeof(int));
  341. if (params->freqs == NULL)
  342. goto fail;
  343. params->freqs[0] = freq;
  344. params->freqs[1] = 0;
  345. break;
  346. case P2P_SCAN_SOCIAL_PLUS_ONE:
  347. params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
  348. sizeof(int));
  349. if (params->freqs == NULL)
  350. goto fail;
  351. for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
  352. if (wpas_p2p_search_social_channel(
  353. wpa_s, social_channels_freq[i]))
  354. params->freqs[num_channels++] =
  355. social_channels_freq[i];
  356. }
  357. if (p2p_supported_freq(wpa_s->global->p2p, freq))
  358. params->freqs[num_channels++] = freq;
  359. params->freqs[num_channels++] = 0;
  360. break;
  361. }
  362. ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
  363. ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
  364. if (ies == NULL) {
  365. wpabuf_free(wps_ie);
  366. goto fail;
  367. }
  368. wpabuf_put_buf(ies, wps_ie);
  369. wpabuf_free(wps_ie);
  370. bands = wpas_get_bands(wpa_s, params->freqs);
  371. p2p_scan_ie(wpa_s->global->p2p, ies, dev_id, bands);
  372. params->p2p_probe = 1;
  373. n = os_malloc(wpabuf_len(ies));
  374. if (n == NULL) {
  375. wpabuf_free(ies);
  376. goto fail;
  377. }
  378. os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
  379. params->extra_ies = n;
  380. params->extra_ies_len = wpabuf_len(ies);
  381. wpabuf_free(ies);
  382. radio_remove_works(wpa_s, "p2p-scan", 0);
  383. if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
  384. params) < 0)
  385. goto fail;
  386. return 0;
  387. fail:
  388. wpa_scan_free_params(params);
  389. return -1;
  390. }
  391. static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
  392. {
  393. switch (p2p_group_interface) {
  394. case P2P_GROUP_INTERFACE_PENDING:
  395. return WPA_IF_P2P_GROUP;
  396. case P2P_GROUP_INTERFACE_GO:
  397. return WPA_IF_P2P_GO;
  398. case P2P_GROUP_INTERFACE_CLIENT:
  399. return WPA_IF_P2P_CLIENT;
  400. }
  401. return WPA_IF_P2P_GROUP;
  402. }
  403. static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
  404. const u8 *ssid,
  405. size_t ssid_len, int *go)
  406. {
  407. struct wpa_ssid *s;
  408. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  409. for (s = wpa_s->conf->ssid; s; s = s->next) {
  410. if (s->disabled != 0 || !s->p2p_group ||
  411. s->ssid_len != ssid_len ||
  412. os_memcmp(ssid, s->ssid, ssid_len) != 0)
  413. continue;
  414. if (s->mode == WPAS_MODE_P2P_GO &&
  415. s != wpa_s->current_ssid)
  416. continue;
  417. if (go)
  418. *go = s->mode == WPAS_MODE_P2P_GO;
  419. return wpa_s;
  420. }
  421. }
  422. return NULL;
  423. }
  424. static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
  425. {
  426. struct wpa_supplicant *wpa_s = eloop_ctx;
  427. wpa_printf(MSG_DEBUG,
  428. "P2P: Complete previously requested removal of %s",
  429. wpa_s->ifname);
  430. wpas_p2p_disconnect(wpa_s);
  431. }
  432. static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
  433. struct wpa_supplicant *calling_wpa_s)
  434. {
  435. if (calling_wpa_s == wpa_s && wpa_s &&
  436. wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
  437. /*
  438. * The calling wpa_s instance is going to be removed. Do that
  439. * from an eloop callback to keep the instance available until
  440. * the caller has returned. This my be needed, e.g., to provide
  441. * control interface responses on the per-interface socket.
  442. */
  443. if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
  444. wpa_s, NULL) < 0)
  445. return -1;
  446. return 0;
  447. }
  448. return wpas_p2p_disconnect(wpa_s);
  449. }
  450. /* Determine total number of clients in active groups where we are the GO */
  451. static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
  452. {
  453. unsigned int count = 0;
  454. struct wpa_ssid *s;
  455. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  456. for (s = wpa_s->conf->ssid; s; s = s->next) {
  457. wpa_printf(MSG_DEBUG,
  458. "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
  459. wpa_s, s, s->disabled, s->p2p_group,
  460. s->mode);
  461. if (!s->disabled && s->p2p_group &&
  462. s->mode == WPAS_MODE_P2P_GO) {
  463. count += p2p_get_group_num_members(
  464. wpa_s->p2p_group);
  465. }
  466. }
  467. }
  468. return count;
  469. }
  470. static unsigned int p2p_is_active_persistent_group(struct wpa_supplicant *wpa_s)
  471. {
  472. return !wpa_s->p2p_mgmt && wpa_s->current_ssid &&
  473. !wpa_s->current_ssid->disabled &&
  474. wpa_s->current_ssid->p2p_group &&
  475. wpa_s->current_ssid->p2p_persistent_group;
  476. }
  477. static unsigned int p2p_is_active_persistent_go(struct wpa_supplicant *wpa_s)
  478. {
  479. return p2p_is_active_persistent_group(wpa_s) &&
  480. wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO;
  481. }
  482. /* Find an interface for a P2P group where we are the GO */
  483. static struct wpa_supplicant *
  484. wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
  485. {
  486. struct wpa_supplicant *save = NULL;
  487. if (!wpa_s)
  488. return NULL;
  489. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  490. if (!p2p_is_active_persistent_go(wpa_s))
  491. continue;
  492. /* Prefer a group with connected clients */
  493. if (p2p_get_group_num_members(wpa_s->p2p_group))
  494. return wpa_s;
  495. save = wpa_s;
  496. }
  497. /* No group with connected clients, so pick the one without (if any) */
  498. return save;
  499. }
  500. static unsigned int p2p_is_active_persistent_cli(struct wpa_supplicant *wpa_s)
  501. {
  502. return p2p_is_active_persistent_group(wpa_s) &&
  503. wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
  504. }
  505. /* Find an interface for a P2P group where we are the P2P Client */
  506. static struct wpa_supplicant *
  507. wpas_p2p_get_cli_group(struct wpa_supplicant *wpa_s)
  508. {
  509. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  510. if (p2p_is_active_persistent_cli(wpa_s))
  511. return wpa_s;
  512. }
  513. return NULL;
  514. }
  515. /* Find a persistent group where we are the GO */
  516. static struct wpa_ssid *
  517. wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
  518. {
  519. struct wpa_ssid *s;
  520. for (s = wpa_s->conf->ssid; s; s = s->next) {
  521. if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
  522. return s;
  523. }
  524. return NULL;
  525. }
  526. static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role,
  527. unsigned int *force_freq,
  528. unsigned int *pref_freq)
  529. {
  530. struct wpa_supplicant *wpa_s = ctx;
  531. struct wpa_ssid *s;
  532. u8 conncap = P2PS_SETUP_NONE;
  533. unsigned int owned_members = 0;
  534. struct wpa_supplicant *go_wpa_s, *cli_wpa_s;
  535. struct wpa_ssid *persistent_go;
  536. int p2p_no_group_iface;
  537. unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
  538. wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
  539. if (force_freq)
  540. *force_freq = 0;
  541. if (pref_freq)
  542. *pref_freq = 0;
  543. size = P2P_MAX_PREF_CHANNELS;
  544. if (force_freq && pref_freq &&
  545. !wpas_p2p_setup_freqs(wpa_s, 0, (int *) force_freq,
  546. (int *) pref_freq, 0, pref_freq_list, &size))
  547. wpas_p2p_set_own_freq_preference(wpa_s,
  548. *force_freq ? *force_freq :
  549. *pref_freq);
  550. /*
  551. * For non-concurrent capable devices:
  552. * If persistent_go, then no new.
  553. * If GO, then no client.
  554. * If client, then no GO.
  555. */
  556. go_wpa_s = wpas_p2p_get_go_group(wpa_s);
  557. if (go_wpa_s)
  558. owned_members = p2p_get_group_num_members(go_wpa_s->p2p_group);
  559. persistent_go = wpas_p2p_get_persistent_go(wpa_s);
  560. p2p_no_group_iface = !wpas_p2p_create_iface(wpa_s);
  561. cli_wpa_s = wpas_p2p_get_cli_group(wpa_s);
  562. wpa_printf(MSG_DEBUG,
  563. "P2P: GO(iface)=%p members=%u CLI(iface)=%p persistent(ssid)=%p",
  564. go_wpa_s, owned_members, cli_wpa_s, persistent_go);
  565. /* If not concurrent, restrict our choices */
  566. if (p2p_no_group_iface) {
  567. wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
  568. if (cli_wpa_s)
  569. return P2PS_SETUP_NONE;
  570. if (go_wpa_s) {
  571. if (role == P2PS_SETUP_CLIENT ||
  572. incoming == P2PS_SETUP_GROUP_OWNER ||
  573. p2p_client_limit_reached(go_wpa_s->p2p_group))
  574. return P2PS_SETUP_NONE;
  575. return P2PS_SETUP_GROUP_OWNER;
  576. }
  577. if (persistent_go) {
  578. if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
  579. if (!incoming)
  580. return P2PS_SETUP_GROUP_OWNER |
  581. P2PS_SETUP_CLIENT;
  582. if (incoming == P2PS_SETUP_NEW) {
  583. u8 r;
  584. if (os_get_random(&r, sizeof(r)) < 0 ||
  585. (r & 1))
  586. return P2PS_SETUP_CLIENT;
  587. return P2PS_SETUP_GROUP_OWNER;
  588. }
  589. }
  590. }
  591. }
  592. /* If a required role has been specified, handle it here */
  593. if (role && role != P2PS_SETUP_NEW) {
  594. switch (incoming) {
  595. case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
  596. case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
  597. /*
  598. * Peer has an active GO, so if the role allows it and
  599. * we do not have any active roles, become client.
  600. */
  601. if ((role & P2PS_SETUP_CLIENT) && !go_wpa_s &&
  602. !cli_wpa_s)
  603. return P2PS_SETUP_CLIENT;
  604. /* fall through */
  605. case P2PS_SETUP_NONE:
  606. case P2PS_SETUP_NEW:
  607. conncap = role;
  608. goto grp_owner;
  609. case P2PS_SETUP_GROUP_OWNER:
  610. /*
  611. * Must be a complimentary role - cannot be a client to
  612. * more than one peer.
  613. */
  614. if (incoming == role || cli_wpa_s)
  615. return P2PS_SETUP_NONE;
  616. return P2PS_SETUP_CLIENT;
  617. case P2PS_SETUP_CLIENT:
  618. /* Must be a complimentary role */
  619. if (incoming != role) {
  620. conncap = P2PS_SETUP_GROUP_OWNER;
  621. goto grp_owner;
  622. }
  623. default:
  624. return P2PS_SETUP_NONE;
  625. }
  626. }
  627. /*
  628. * For now, we only will support ownership of one group, and being a
  629. * client of one group. Therefore, if we have either an existing GO
  630. * group, or an existing client group, we will not do a new GO
  631. * negotiation, but rather try to re-use the existing groups.
  632. */
  633. switch (incoming) {
  634. case P2PS_SETUP_NONE:
  635. case P2PS_SETUP_NEW:
  636. if (cli_wpa_s)
  637. conncap = P2PS_SETUP_GROUP_OWNER;
  638. else if (!owned_members)
  639. conncap = P2PS_SETUP_NEW;
  640. else if (incoming == P2PS_SETUP_NONE)
  641. conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
  642. else
  643. conncap = P2PS_SETUP_CLIENT;
  644. break;
  645. case P2PS_SETUP_CLIENT:
  646. conncap = P2PS_SETUP_GROUP_OWNER;
  647. break;
  648. case P2PS_SETUP_GROUP_OWNER:
  649. if (!cli_wpa_s)
  650. conncap = P2PS_SETUP_CLIENT;
  651. break;
  652. case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
  653. case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
  654. if (cli_wpa_s)
  655. conncap = P2PS_SETUP_GROUP_OWNER;
  656. else {
  657. u8 r;
  658. if (os_get_random(&r, sizeof(r)) < 0 ||
  659. (r & 1))
  660. conncap = P2PS_SETUP_CLIENT;
  661. else
  662. conncap = P2PS_SETUP_GROUP_OWNER;
  663. }
  664. break;
  665. default:
  666. return P2PS_SETUP_NONE;
  667. }
  668. grp_owner:
  669. if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
  670. (!incoming && (conncap & P2PS_SETUP_NEW))) {
  671. if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
  672. conncap &= ~P2PS_SETUP_GROUP_OWNER;
  673. s = wpas_p2p_get_persistent_go(wpa_s);
  674. if (!s && !go_wpa_s && p2p_no_group_iface) {
  675. p2p_set_intended_addr(wpa_s->global->p2p,
  676. wpa_s->p2p_mgmt ?
  677. wpa_s->parent->own_addr :
  678. wpa_s->own_addr);
  679. } else if (!s && !go_wpa_s) {
  680. if (wpas_p2p_add_group_interface(wpa_s,
  681. WPA_IF_P2P_GO) < 0) {
  682. wpa_printf(MSG_ERROR,
  683. "P2P: Failed to allocate a new interface for the group");
  684. return P2PS_SETUP_NONE;
  685. }
  686. wpa_s->global->pending_group_iface_for_p2ps = 1;
  687. p2p_set_intended_addr(wpa_s->global->p2p,
  688. wpa_s->pending_interface_addr);
  689. }
  690. }
  691. return conncap;
  692. }
  693. static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
  694. enum p2p_group_removal_reason removal_reason)
  695. {
  696. struct wpa_ssid *ssid;
  697. char *gtype;
  698. const char *reason;
  699. ssid = wpa_s->current_ssid;
  700. if (ssid == NULL) {
  701. /*
  702. * The current SSID was not known, but there may still be a
  703. * pending P2P group interface waiting for provisioning or a
  704. * P2P group that is trying to reconnect.
  705. */
  706. ssid = wpa_s->conf->ssid;
  707. while (ssid) {
  708. if (ssid->p2p_group && ssid->disabled != 2)
  709. break;
  710. ssid = ssid->next;
  711. }
  712. if (ssid == NULL &&
  713. wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
  714. {
  715. wpa_printf(MSG_ERROR, "P2P: P2P group interface "
  716. "not found");
  717. return -1;
  718. }
  719. }
  720. if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
  721. gtype = "GO";
  722. else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
  723. (ssid && ssid->mode == WPAS_MODE_INFRA)) {
  724. wpa_s->reassociate = 0;
  725. wpa_s->disconnected = 1;
  726. gtype = "client";
  727. } else
  728. gtype = "GO";
  729. if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
  730. wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
  731. if (os_strcmp(gtype, "client") == 0) {
  732. wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  733. if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
  734. wpa_s, NULL)) {
  735. wpa_printf(MSG_DEBUG,
  736. "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
  737. removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
  738. eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
  739. wpa_s, NULL);
  740. }
  741. }
  742. if (wpa_s->cross_connect_in_use) {
  743. wpa_s->cross_connect_in_use = 0;
  744. wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
  745. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  746. wpa_s->ifname, wpa_s->cross_connect_uplink);
  747. }
  748. switch (removal_reason) {
  749. case P2P_GROUP_REMOVAL_REQUESTED:
  750. reason = " reason=REQUESTED";
  751. break;
  752. case P2P_GROUP_REMOVAL_FORMATION_FAILED:
  753. reason = " reason=FORMATION_FAILED";
  754. break;
  755. case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
  756. reason = " reason=IDLE";
  757. break;
  758. case P2P_GROUP_REMOVAL_UNAVAILABLE:
  759. reason = " reason=UNAVAILABLE";
  760. break;
  761. case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
  762. reason = " reason=GO_ENDING_SESSION";
  763. break;
  764. case P2P_GROUP_REMOVAL_PSK_FAILURE:
  765. reason = " reason=PSK_FAILURE";
  766. break;
  767. case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
  768. reason = " reason=FREQ_CONFLICT";
  769. break;
  770. default:
  771. reason = "";
  772. break;
  773. }
  774. if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
  775. wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
  776. P2P_EVENT_GROUP_REMOVED "%s %s%s",
  777. wpa_s->ifname, gtype, reason);
  778. }
  779. if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
  780. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
  781. if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
  782. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
  783. if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  784. wpa_s->p2pdev, NULL) > 0) {
  785. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
  786. "timeout");
  787. wpa_s->p2p_in_provisioning = 0;
  788. wpas_p2p_group_formation_failed(wpa_s, 1);
  789. }
  790. wpa_s->p2p_in_invitation = 0;
  791. eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
  792. eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
  793. /*
  794. * Make sure wait for the first client does not remain active after the
  795. * group has been removed.
  796. */
  797. wpa_s->global->p2p_go_wait_client.sec = 0;
  798. if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
  799. struct wpa_global *global;
  800. char *ifname;
  801. enum wpa_driver_if_type type;
  802. wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
  803. wpa_s->ifname);
  804. global = wpa_s->global;
  805. ifname = os_strdup(wpa_s->ifname);
  806. type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
  807. eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
  808. wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
  809. wpa_s = global->ifaces;
  810. if (wpa_s && ifname)
  811. wpa_drv_if_remove(wpa_s, type, ifname);
  812. os_free(ifname);
  813. return 1;
  814. }
  815. /*
  816. * The primary interface was used for P2P group operations, so
  817. * need to reset its p2pdev.
  818. */
  819. wpa_s->p2pdev = wpa_s->parent;
  820. if (!wpa_s->p2p_go_group_formation_completed) {
  821. wpa_s->global->p2p_group_formation = NULL;
  822. wpa_s->p2p_in_provisioning = 0;
  823. }
  824. wpa_s->show_group_started = 0;
  825. os_free(wpa_s->go_params);
  826. wpa_s->go_params = NULL;
  827. os_free(wpa_s->p2p_group_common_freqs);
  828. wpa_s->p2p_group_common_freqs = NULL;
  829. wpa_s->p2p_group_common_freqs_num = 0;
  830. wpa_s->waiting_presence_resp = 0;
  831. wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
  832. if (ssid && (ssid->p2p_group ||
  833. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
  834. (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
  835. int id = ssid->id;
  836. if (ssid == wpa_s->current_ssid) {
  837. wpa_sm_set_config(wpa_s->wpa, NULL);
  838. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  839. wpa_s->current_ssid = NULL;
  840. }
  841. /*
  842. * Networks objects created during any P2P activities are not
  843. * exposed out as they might/will confuse certain non-P2P aware
  844. * applications since these network objects won't behave like
  845. * regular ones.
  846. *
  847. * Likewise, we don't send out network removed signals for such
  848. * network objects.
  849. */
  850. wpa_config_remove_network(wpa_s->conf, id);
  851. wpa_supplicant_clear_status(wpa_s);
  852. wpa_supplicant_cancel_sched_scan(wpa_s);
  853. } else {
  854. wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
  855. "found");
  856. }
  857. if (wpa_s->ap_iface)
  858. wpa_supplicant_ap_deinit(wpa_s);
  859. else
  860. wpa_drv_deinit_p2p_cli(wpa_s);
  861. os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
  862. return 0;
  863. }
  864. static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
  865. u8 *go_dev_addr,
  866. const u8 *ssid, size_t ssid_len)
  867. {
  868. struct wpa_bss *bss;
  869. const u8 *bssid;
  870. struct wpabuf *p2p;
  871. u8 group_capab;
  872. const u8 *addr;
  873. if (wpa_s->go_params)
  874. bssid = wpa_s->go_params->peer_interface_addr;
  875. else
  876. bssid = wpa_s->bssid;
  877. bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
  878. if (bss == NULL && wpa_s->go_params &&
  879. !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
  880. bss = wpa_bss_get_p2p_dev_addr(
  881. wpa_s, wpa_s->go_params->peer_device_addr);
  882. if (bss == NULL) {
  883. u8 iface_addr[ETH_ALEN];
  884. if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
  885. iface_addr) == 0)
  886. bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
  887. }
  888. if (bss == NULL) {
  889. wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
  890. "group is persistent - BSS " MACSTR " not found",
  891. MAC2STR(bssid));
  892. return 0;
  893. }
  894. p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  895. if (p2p == NULL)
  896. p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
  897. P2P_IE_VENDOR_TYPE);
  898. if (p2p == NULL) {
  899. wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
  900. "group is persistent - BSS " MACSTR
  901. " did not include P2P IE", MAC2STR(bssid));
  902. wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
  903. (u8 *) (bss + 1), bss->ie_len);
  904. wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
  905. ((u8 *) bss + 1) + bss->ie_len,
  906. bss->beacon_ie_len);
  907. return 0;
  908. }
  909. group_capab = p2p_get_group_capab(p2p);
  910. addr = p2p_get_go_dev_addr(p2p);
  911. wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
  912. "group_capab=0x%x", group_capab);
  913. if (addr) {
  914. os_memcpy(go_dev_addr, addr, ETH_ALEN);
  915. wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
  916. MAC2STR(addr));
  917. } else
  918. os_memset(go_dev_addr, 0, ETH_ALEN);
  919. wpabuf_free(p2p);
  920. wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
  921. "go_dev_addr=" MACSTR,
  922. MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
  923. return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
  924. }
  925. static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
  926. struct wpa_ssid *ssid,
  927. const u8 *go_dev_addr)
  928. {
  929. struct wpa_ssid *s;
  930. int changed = 0;
  931. wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
  932. "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
  933. for (s = wpa_s->conf->ssid; s; s = s->next) {
  934. if (s->disabled == 2 &&
  935. os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
  936. s->ssid_len == ssid->ssid_len &&
  937. os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
  938. break;
  939. }
  940. if (s) {
  941. wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
  942. "entry");
  943. if (ssid->passphrase && !s->passphrase)
  944. changed = 1;
  945. else if (ssid->passphrase && s->passphrase &&
  946. os_strcmp(ssid->passphrase, s->passphrase) != 0)
  947. changed = 1;
  948. } else {
  949. wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
  950. "entry");
  951. changed = 1;
  952. s = wpa_config_add_network(wpa_s->conf);
  953. if (s == NULL)
  954. return -1;
  955. /*
  956. * Instead of network_added we emit persistent_group_added
  957. * notification. Also to keep the defense checks in
  958. * persistent_group obj registration method, we set the
  959. * relevant flags in s to designate it as a persistent group.
  960. */
  961. s->p2p_group = 1;
  962. s->p2p_persistent_group = 1;
  963. wpas_notify_persistent_group_added(wpa_s, s);
  964. wpa_config_set_network_defaults(s);
  965. }
  966. s->p2p_group = 1;
  967. s->p2p_persistent_group = 1;
  968. s->disabled = 2;
  969. s->bssid_set = 1;
  970. os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
  971. s->mode = ssid->mode;
  972. s->auth_alg = WPA_AUTH_ALG_OPEN;
  973. s->key_mgmt = WPA_KEY_MGMT_PSK;
  974. s->proto = WPA_PROTO_RSN;
  975. s->pairwise_cipher = WPA_CIPHER_CCMP;
  976. s->export_keys = 1;
  977. if (ssid->passphrase) {
  978. os_free(s->passphrase);
  979. s->passphrase = os_strdup(ssid->passphrase);
  980. }
  981. if (ssid->psk_set) {
  982. s->psk_set = 1;
  983. os_memcpy(s->psk, ssid->psk, 32);
  984. }
  985. if (s->passphrase && !s->psk_set)
  986. wpa_config_update_psk(s);
  987. if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
  988. os_free(s->ssid);
  989. s->ssid = os_malloc(ssid->ssid_len);
  990. }
  991. if (s->ssid) {
  992. s->ssid_len = ssid->ssid_len;
  993. os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
  994. }
  995. if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
  996. dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
  997. wpa_s->global->add_psk = NULL;
  998. changed = 1;
  999. }
  1000. if (changed && wpa_s->conf->update_config &&
  1001. wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  1002. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  1003. }
  1004. return s->id;
  1005. }
  1006. static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
  1007. const u8 *addr)
  1008. {
  1009. struct wpa_ssid *ssid, *s;
  1010. u8 *n;
  1011. size_t i;
  1012. int found = 0;
  1013. struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
  1014. ssid = wpa_s->current_ssid;
  1015. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  1016. !ssid->p2p_persistent_group)
  1017. return;
  1018. for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
  1019. if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
  1020. continue;
  1021. if (s->ssid_len == ssid->ssid_len &&
  1022. os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
  1023. break;
  1024. }
  1025. if (s == NULL)
  1026. return;
  1027. for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
  1028. if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
  1029. ETH_ALEN) != 0)
  1030. continue;
  1031. if (i == s->num_p2p_clients - 1)
  1032. return; /* already the most recent entry */
  1033. /* move the entry to mark it most recent */
  1034. os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
  1035. s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
  1036. (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
  1037. os_memcpy(s->p2p_client_list +
  1038. (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
  1039. ETH_ALEN);
  1040. os_memset(s->p2p_client_list +
  1041. (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
  1042. 0xff, ETH_ALEN);
  1043. found = 1;
  1044. break;
  1045. }
  1046. if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
  1047. n = os_realloc_array(s->p2p_client_list,
  1048. s->num_p2p_clients + 1, 2 * ETH_ALEN);
  1049. if (n == NULL)
  1050. return;
  1051. os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
  1052. ETH_ALEN);
  1053. os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
  1054. 0xff, ETH_ALEN);
  1055. s->p2p_client_list = n;
  1056. s->num_p2p_clients++;
  1057. } else if (!found && s->p2p_client_list) {
  1058. /* Not enough room for an additional entry - drop the oldest
  1059. * entry */
  1060. os_memmove(s->p2p_client_list,
  1061. s->p2p_client_list + 2 * ETH_ALEN,
  1062. (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
  1063. os_memcpy(s->p2p_client_list +
  1064. (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
  1065. addr, ETH_ALEN);
  1066. os_memset(s->p2p_client_list +
  1067. (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
  1068. 0xff, ETH_ALEN);
  1069. }
  1070. if (p2p_wpa_s->conf->update_config &&
  1071. wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
  1072. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  1073. }
  1074. static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
  1075. int go, struct wpa_ssid *ssid, int freq,
  1076. const u8 *psk, const char *passphrase,
  1077. const u8 *go_dev_addr, int persistent,
  1078. const char *extra)
  1079. {
  1080. const char *ssid_txt;
  1081. char psk_txt[65];
  1082. if (psk)
  1083. wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
  1084. else
  1085. psk_txt[0] = '\0';
  1086. if (ssid)
  1087. ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
  1088. else
  1089. ssid_txt = "";
  1090. if (passphrase && passphrase[0] == '\0')
  1091. passphrase = NULL;
  1092. /*
  1093. * Include PSK/passphrase only in the control interface message and
  1094. * leave it out from the debug log entry.
  1095. */
  1096. wpa_msg_global_ctrl(wpa_s->p2pdev, MSG_INFO,
  1097. P2P_EVENT_GROUP_STARTED
  1098. "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
  1099. MACSTR "%s%s",
  1100. wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
  1101. psk ? " psk=" : "", psk_txt,
  1102. passphrase ? " passphrase=\"" : "",
  1103. passphrase ? passphrase : "",
  1104. passphrase ? "\"" : "",
  1105. MAC2STR(go_dev_addr),
  1106. persistent ? " [PERSISTENT]" : "", extra);
  1107. wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
  1108. "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
  1109. wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
  1110. MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
  1111. extra);
  1112. }
  1113. static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
  1114. int success, int already_deleted)
  1115. {
  1116. struct wpa_ssid *ssid;
  1117. int client;
  1118. int persistent;
  1119. u8 go_dev_addr[ETH_ALEN];
  1120. int network_id = -1;
  1121. /*
  1122. * This callback is likely called for the main interface. Update wpa_s
  1123. * to use the group interface if a new interface was created for the
  1124. * group.
  1125. */
  1126. if (wpa_s->global->p2p_group_formation)
  1127. wpa_s = wpa_s->global->p2p_group_formation;
  1128. if (wpa_s->p2p_go_group_formation_completed) {
  1129. wpa_s->global->p2p_group_formation = NULL;
  1130. wpa_s->p2p_in_provisioning = 0;
  1131. }
  1132. wpa_s->p2p_in_invitation = 0;
  1133. wpa_s->group_formation_reported = 1;
  1134. if (!success) {
  1135. wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
  1136. P2P_EVENT_GROUP_FORMATION_FAILURE);
  1137. wpas_notify_p2p_group_formation_failure(wpa_s, "");
  1138. if (already_deleted)
  1139. return;
  1140. wpas_p2p_group_delete(wpa_s,
  1141. P2P_GROUP_REMOVAL_FORMATION_FAILED);
  1142. return;
  1143. }
  1144. wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
  1145. P2P_EVENT_GROUP_FORMATION_SUCCESS);
  1146. ssid = wpa_s->current_ssid;
  1147. if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
  1148. ssid->mode = WPAS_MODE_P2P_GO;
  1149. p2p_group_notif_formation_done(wpa_s->p2p_group);
  1150. wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
  1151. }
  1152. persistent = 0;
  1153. if (ssid) {
  1154. client = ssid->mode == WPAS_MODE_INFRA;
  1155. if (ssid->mode == WPAS_MODE_P2P_GO) {
  1156. persistent = ssid->p2p_persistent_group;
  1157. os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
  1158. ETH_ALEN);
  1159. } else
  1160. persistent = wpas_p2p_persistent_group(wpa_s,
  1161. go_dev_addr,
  1162. ssid->ssid,
  1163. ssid->ssid_len);
  1164. } else {
  1165. client = wpa_s->p2p_group_interface ==
  1166. P2P_GROUP_INTERFACE_CLIENT;
  1167. os_memset(go_dev_addr, 0, ETH_ALEN);
  1168. }
  1169. wpa_s->show_group_started = 0;
  1170. if (client) {
  1171. /*
  1172. * Indicate event only after successfully completed 4-way
  1173. * handshake, i.e., when the interface is ready for data
  1174. * packets.
  1175. */
  1176. wpa_s->show_group_started = 1;
  1177. } else {
  1178. wpas_p2p_group_started(wpa_s, 1, ssid,
  1179. ssid ? ssid->frequency : 0,
  1180. ssid && ssid->passphrase == NULL &&
  1181. ssid->psk_set ? ssid->psk : NULL,
  1182. ssid ? ssid->passphrase : NULL,
  1183. go_dev_addr, persistent, "");
  1184. wpas_p2p_cross_connect_setup(wpa_s);
  1185. wpas_p2p_set_group_idle_timeout(wpa_s);
  1186. }
  1187. if (persistent)
  1188. network_id = wpas_p2p_store_persistent_group(wpa_s->p2pdev,
  1189. ssid, go_dev_addr);
  1190. else {
  1191. os_free(wpa_s->global->add_psk);
  1192. wpa_s->global->add_psk = NULL;
  1193. }
  1194. if (network_id < 0 && ssid)
  1195. network_id = ssid->id;
  1196. if (!client) {
  1197. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
  1198. os_get_reltime(&wpa_s->global->p2p_go_wait_client);
  1199. }
  1200. }
  1201. struct send_action_work {
  1202. unsigned int freq;
  1203. u8 dst[ETH_ALEN];
  1204. u8 src[ETH_ALEN];
  1205. u8 bssid[ETH_ALEN];
  1206. size_t len;
  1207. unsigned int wait_time;
  1208. u8 buf[0];
  1209. };
  1210. static void wpas_p2p_free_send_action_work(struct wpa_supplicant *wpa_s)
  1211. {
  1212. struct send_action_work *awork = wpa_s->p2p_send_action_work->ctx;
  1213. wpa_printf(MSG_DEBUG,
  1214. "P2P: Free Action frame radio work @%p (freq=%u dst="
  1215. MACSTR " src=" MACSTR " bssid=" MACSTR " wait_time=%u)",
  1216. wpa_s->p2p_send_action_work, awork->freq,
  1217. MAC2STR(awork->dst), MAC2STR(awork->src),
  1218. MAC2STR(awork->bssid), awork->wait_time);
  1219. wpa_hexdump(MSG_DEBUG, "P2P: Freeing pending Action frame",
  1220. awork->buf, awork->len);
  1221. os_free(awork);
  1222. wpa_s->p2p_send_action_work->ctx = NULL;
  1223. radio_work_done(wpa_s->p2p_send_action_work);
  1224. wpa_s->p2p_send_action_work = NULL;
  1225. }
  1226. static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
  1227. void *timeout_ctx)
  1228. {
  1229. struct wpa_supplicant *wpa_s = eloop_ctx;
  1230. if (!wpa_s->p2p_send_action_work)
  1231. return;
  1232. wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
  1233. wpas_p2p_free_send_action_work(wpa_s);
  1234. }
  1235. static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
  1236. {
  1237. if (wpa_s->p2p_send_action_work) {
  1238. struct send_action_work *awork;
  1239. awork = wpa_s->p2p_send_action_work->ctx;
  1240. wpa_printf(MSG_DEBUG,
  1241. "P2P: Clear Action TX work @%p (wait_time=%u)",
  1242. wpa_s->p2p_send_action_work, awork->wait_time);
  1243. if (awork->wait_time == 0) {
  1244. wpas_p2p_free_send_action_work(wpa_s);
  1245. } else {
  1246. /*
  1247. * In theory, this should not be needed, but number of
  1248. * places in the P2P code is still using non-zero wait
  1249. * time for the last Action frame in the sequence and
  1250. * some of these do not call send_action_done().
  1251. */
  1252. eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
  1253. wpa_s, NULL);
  1254. eloop_register_timeout(
  1255. 0, awork->wait_time * 1000,
  1256. wpas_p2p_send_action_work_timeout,
  1257. wpa_s, NULL);
  1258. }
  1259. }
  1260. }
  1261. static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
  1262. unsigned int freq,
  1263. const u8 *dst, const u8 *src,
  1264. const u8 *bssid,
  1265. const u8 *data, size_t data_len,
  1266. enum offchannel_send_action_result
  1267. result)
  1268. {
  1269. enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
  1270. wpas_p2p_action_tx_clear(wpa_s);
  1271. if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
  1272. return;
  1273. switch (result) {
  1274. case OFFCHANNEL_SEND_ACTION_SUCCESS:
  1275. res = P2P_SEND_ACTION_SUCCESS;
  1276. break;
  1277. case OFFCHANNEL_SEND_ACTION_NO_ACK:
  1278. res = P2P_SEND_ACTION_NO_ACK;
  1279. break;
  1280. case OFFCHANNEL_SEND_ACTION_FAILED:
  1281. res = P2P_SEND_ACTION_FAILED;
  1282. break;
  1283. }
  1284. p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
  1285. if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
  1286. wpa_s->pending_pd_before_join &&
  1287. (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
  1288. os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
  1289. wpa_s->p2p_fallback_to_go_neg) {
  1290. wpa_s->pending_pd_before_join = 0;
  1291. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
  1292. "during p2p_connect-auto");
  1293. wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
  1294. P2P_EVENT_FALLBACK_TO_GO_NEG
  1295. "reason=no-ACK-to-PD-Req");
  1296. wpas_p2p_fallback_to_go_neg(wpa_s, 0);
  1297. return;
  1298. }
  1299. }
  1300. static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
  1301. {
  1302. struct wpa_supplicant *wpa_s = work->wpa_s;
  1303. struct send_action_work *awork = work->ctx;
  1304. if (deinit) {
  1305. if (work->started) {
  1306. eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
  1307. wpa_s, NULL);
  1308. wpa_s->p2p_send_action_work = NULL;
  1309. offchannel_send_action_done(wpa_s);
  1310. }
  1311. os_free(awork);
  1312. return;
  1313. }
  1314. if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
  1315. awork->bssid, awork->buf, awork->len,
  1316. awork->wait_time,
  1317. wpas_p2p_send_action_tx_status, 1) < 0) {
  1318. os_free(awork);
  1319. radio_work_done(work);
  1320. return;
  1321. }
  1322. wpa_s->p2p_send_action_work = work;
  1323. }
  1324. static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
  1325. unsigned int freq, const u8 *dst,
  1326. const u8 *src, const u8 *bssid, const u8 *buf,
  1327. size_t len, unsigned int wait_time)
  1328. {
  1329. struct send_action_work *awork;
  1330. if (wpa_s->p2p_send_action_work) {
  1331. wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
  1332. return -1;
  1333. }
  1334. awork = os_zalloc(sizeof(*awork) + len);
  1335. if (awork == NULL)
  1336. return -1;
  1337. awork->freq = freq;
  1338. os_memcpy(awork->dst, dst, ETH_ALEN);
  1339. os_memcpy(awork->src, src, ETH_ALEN);
  1340. os_memcpy(awork->bssid, bssid, ETH_ALEN);
  1341. awork->len = len;
  1342. awork->wait_time = wait_time;
  1343. os_memcpy(awork->buf, buf, len);
  1344. if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
  1345. wpas_send_action_cb, awork) < 0) {
  1346. os_free(awork);
  1347. return -1;
  1348. }
  1349. return 0;
  1350. }
  1351. static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
  1352. const u8 *src, const u8 *bssid, const u8 *buf,
  1353. size_t len, unsigned int wait_time)
  1354. {
  1355. struct wpa_supplicant *wpa_s = ctx;
  1356. int listen_freq = -1, send_freq = -1;
  1357. if (wpa_s->p2p_listen_work)
  1358. listen_freq = wpa_s->p2p_listen_work->freq;
  1359. if (wpa_s->p2p_send_action_work)
  1360. send_freq = wpa_s->p2p_send_action_work->freq;
  1361. if (listen_freq != (int) freq && send_freq != (int) freq) {
  1362. wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
  1363. listen_freq, send_freq);
  1364. return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
  1365. len, wait_time);
  1366. }
  1367. wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
  1368. return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
  1369. wait_time,
  1370. wpas_p2p_send_action_tx_status, 1);
  1371. }
  1372. static void wpas_send_action_done(void *ctx)
  1373. {
  1374. struct wpa_supplicant *wpa_s = ctx;
  1375. if (wpa_s->p2p_send_action_work) {
  1376. eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
  1377. wpa_s, NULL);
  1378. os_free(wpa_s->p2p_send_action_work->ctx);
  1379. radio_work_done(wpa_s->p2p_send_action_work);
  1380. wpa_s->p2p_send_action_work = NULL;
  1381. }
  1382. offchannel_send_action_done(wpa_s);
  1383. }
  1384. static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
  1385. struct p2p_go_neg_results *params)
  1386. {
  1387. if (wpa_s->go_params == NULL) {
  1388. wpa_s->go_params = os_malloc(sizeof(*params));
  1389. if (wpa_s->go_params == NULL)
  1390. return -1;
  1391. }
  1392. os_memcpy(wpa_s->go_params, params, sizeof(*params));
  1393. return 0;
  1394. }
  1395. static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
  1396. struct p2p_go_neg_results *res)
  1397. {
  1398. wpa_s->group_formation_reported = 0;
  1399. wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
  1400. " dev_addr " MACSTR " wps_method %d",
  1401. MAC2STR(res->peer_interface_addr),
  1402. MAC2STR(res->peer_device_addr), res->wps_method);
  1403. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
  1404. res->ssid, res->ssid_len);
  1405. wpa_supplicant_ap_deinit(wpa_s);
  1406. wpas_copy_go_neg_results(wpa_s, res);
  1407. if (res->wps_method == WPS_PBC) {
  1408. wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
  1409. #ifdef CONFIG_WPS_NFC
  1410. } else if (res->wps_method == WPS_NFC) {
  1411. wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
  1412. res->peer_interface_addr,
  1413. wpa_s->p2pdev->p2p_oob_dev_pw,
  1414. wpa_s->p2pdev->p2p_oob_dev_pw_id, 1,
  1415. wpa_s->p2pdev->p2p_oob_dev_pw_id ==
  1416. DEV_PW_NFC_CONNECTION_HANDOVER ?
  1417. wpa_s->p2pdev->p2p_peer_oob_pubkey_hash :
  1418. NULL,
  1419. NULL, 0, 0);
  1420. #endif /* CONFIG_WPS_NFC */
  1421. } else {
  1422. u16 dev_pw_id = DEV_PW_DEFAULT;
  1423. if (wpa_s->p2p_wps_method == WPS_P2PS)
  1424. dev_pw_id = DEV_PW_P2PS_DEFAULT;
  1425. if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
  1426. dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
  1427. wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
  1428. wpa_s->p2p_pin, 1, dev_pw_id);
  1429. }
  1430. }
  1431. static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
  1432. struct wpa_ssid *ssid)
  1433. {
  1434. struct wpa_ssid *persistent;
  1435. struct psk_list_entry *psk;
  1436. struct hostapd_data *hapd;
  1437. if (!wpa_s->ap_iface)
  1438. return;
  1439. persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
  1440. ssid->ssid_len);
  1441. if (persistent == NULL)
  1442. return;
  1443. hapd = wpa_s->ap_iface->bss[0];
  1444. dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
  1445. list) {
  1446. struct hostapd_wpa_psk *hpsk;
  1447. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
  1448. MACSTR " psk=%d",
  1449. MAC2STR(psk->addr), psk->p2p);
  1450. hpsk = os_zalloc(sizeof(*hpsk));
  1451. if (hpsk == NULL)
  1452. break;
  1453. os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
  1454. if (psk->p2p)
  1455. os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
  1456. else
  1457. os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
  1458. hpsk->next = hapd->conf->ssid.wpa_psk;
  1459. hapd->conf->ssid.wpa_psk = hpsk;
  1460. }
  1461. }
  1462. static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
  1463. {
  1464. unsigned int i;
  1465. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies (len=%u):",
  1466. wpa_s->p2p_group_common_freqs_num);
  1467. for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++)
  1468. wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d",
  1469. i, wpa_s->p2p_group_common_freqs[i]);
  1470. }
  1471. static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
  1472. struct p2p_go_neg_results *params)
  1473. {
  1474. unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
  1475. wpa_s->p2p_group_common_freqs_num = 0;
  1476. os_free(wpa_s->p2p_group_common_freqs);
  1477. wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
  1478. if (!wpa_s->p2p_group_common_freqs)
  1479. return;
  1480. for (i = 0; i < len; i++) {
  1481. if (!wpa_s->go_params->freq_list[i])
  1482. break;
  1483. wpa_s->p2p_group_common_freqs[i] =
  1484. wpa_s->go_params->freq_list[i];
  1485. }
  1486. wpa_s->p2p_group_common_freqs_num = i;
  1487. }
  1488. static void p2p_config_write(struct wpa_supplicant *wpa_s)
  1489. {
  1490. #ifndef CONFIG_NO_CONFIG_WRITE
  1491. if (wpa_s->p2pdev->conf->update_config &&
  1492. wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
  1493. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  1494. #endif /* CONFIG_NO_CONFIG_WRITE */
  1495. }
  1496. static void p2p_go_configured(void *ctx, void *data)
  1497. {
  1498. struct wpa_supplicant *wpa_s = ctx;
  1499. struct p2p_go_neg_results *params = data;
  1500. struct wpa_ssid *ssid;
  1501. int network_id = -1;
  1502. wpa_s->ap_configured_cb = NULL;
  1503. wpa_s->ap_configured_cb_ctx = NULL;
  1504. wpa_s->ap_configured_cb_data = NULL;
  1505. if (!wpa_s->go_params) {
  1506. wpa_printf(MSG_ERROR,
  1507. "P2P: p2p_go_configured() called with wpa_s->go_params == NULL");
  1508. return;
  1509. }
  1510. p2p_go_save_group_common_freqs(wpa_s, params);
  1511. p2p_go_dump_common_freqs(wpa_s);
  1512. ssid = wpa_s->current_ssid;
  1513. if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
  1514. wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
  1515. if (wpa_s->global->p2p_group_formation == wpa_s)
  1516. wpa_s->global->p2p_group_formation = NULL;
  1517. wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
  1518. params->passphrase[0] == '\0' ?
  1519. params->psk : NULL,
  1520. params->passphrase,
  1521. wpa_s->global->p2p_dev_addr,
  1522. params->persistent_group, "");
  1523. wpa_s->group_formation_reported = 1;
  1524. if (wpa_s->p2pdev->p2ps_method_config_any) {
  1525. if (is_zero_ether_addr(wpa_s->p2pdev->p2ps_join_addr)) {
  1526. wpa_dbg(wpa_s, MSG_DEBUG,
  1527. "P2PS: Setting default PIN for ANY");
  1528. wpa_supplicant_ap_wps_pin(wpa_s, NULL,
  1529. "12345670", NULL, 0,
  1530. 0);
  1531. } else {
  1532. wpa_dbg(wpa_s, MSG_DEBUG,
  1533. "P2PS: Setting default PIN for " MACSTR,
  1534. MAC2STR(wpa_s->p2pdev->p2ps_join_addr));
  1535. wpa_supplicant_ap_wps_pin(
  1536. wpa_s, wpa_s->p2pdev->p2ps_join_addr,
  1537. "12345670", NULL, 0, 0);
  1538. }
  1539. wpa_s->p2pdev->p2ps_method_config_any = 0;
  1540. }
  1541. os_get_reltime(&wpa_s->global->p2p_go_wait_client);
  1542. if (params->persistent_group) {
  1543. network_id = wpas_p2p_store_persistent_group(
  1544. wpa_s->p2pdev, ssid,
  1545. wpa_s->global->p2p_dev_addr);
  1546. wpas_p2p_add_psk_list(wpa_s, ssid);
  1547. }
  1548. if (network_id < 0)
  1549. network_id = ssid->id;
  1550. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
  1551. wpas_p2p_cross_connect_setup(wpa_s);
  1552. wpas_p2p_set_group_idle_timeout(wpa_s);
  1553. if (wpa_s->p2p_first_connection_timeout) {
  1554. wpa_dbg(wpa_s, MSG_DEBUG,
  1555. "P2P: Start group formation timeout of %d seconds until first data connection on GO",
  1556. wpa_s->p2p_first_connection_timeout);
  1557. wpa_s->p2p_go_group_formation_completed = 0;
  1558. wpa_s->global->p2p_group_formation = wpa_s;
  1559. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  1560. wpa_s->p2pdev, NULL);
  1561. eloop_register_timeout(
  1562. wpa_s->p2p_first_connection_timeout, 0,
  1563. wpas_p2p_group_formation_timeout,
  1564. wpa_s->p2pdev, NULL);
  1565. }
  1566. return;
  1567. }
  1568. wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
  1569. if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
  1570. params->peer_interface_addr)) {
  1571. wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
  1572. "filtering");
  1573. return;
  1574. }
  1575. if (params->wps_method == WPS_PBC) {
  1576. wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
  1577. params->peer_device_addr);
  1578. #ifdef CONFIG_WPS_NFC
  1579. } else if (params->wps_method == WPS_NFC) {
  1580. if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
  1581. DEV_PW_NFC_CONNECTION_HANDOVER &&
  1582. !wpa_s->p2pdev->p2p_oob_dev_pw) {
  1583. wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
  1584. return;
  1585. }
  1586. wpas_ap_wps_add_nfc_pw(
  1587. wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
  1588. wpa_s->p2pdev->p2p_oob_dev_pw,
  1589. wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
  1590. wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
  1591. #endif /* CONFIG_WPS_NFC */
  1592. } else if (wpa_s->p2p_pin[0])
  1593. wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
  1594. wpa_s->p2p_pin, NULL, 0, 0);
  1595. os_free(wpa_s->go_params);
  1596. wpa_s->go_params = NULL;
  1597. }
  1598. static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
  1599. struct p2p_go_neg_results *params,
  1600. int group_formation)
  1601. {
  1602. struct wpa_ssid *ssid;
  1603. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
  1604. if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
  1605. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
  1606. "results");
  1607. return;
  1608. }
  1609. ssid = wpa_config_add_network(wpa_s->conf);
  1610. if (ssid == NULL) {
  1611. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
  1612. return;
  1613. }
  1614. wpa_s->show_group_started = 0;
  1615. wpa_s->p2p_go_group_formation_completed = 0;
  1616. wpa_s->group_formation_reported = 0;
  1617. os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
  1618. wpa_config_set_network_defaults(ssid);
  1619. ssid->temporary = 1;
  1620. ssid->p2p_group = 1;
  1621. ssid->p2p_persistent_group = params->persistent_group;
  1622. ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
  1623. WPAS_MODE_P2P_GO;
  1624. ssid->frequency = params->freq;
  1625. ssid->ht40 = params->ht40;
  1626. ssid->vht = params->vht;
  1627. ssid->max_oper_chwidth = params->max_oper_chwidth;
  1628. ssid->vht_center_freq2 = params->vht_center_freq2;
  1629. ssid->ssid = os_zalloc(params->ssid_len + 1);
  1630. if (ssid->ssid) {
  1631. os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
  1632. ssid->ssid_len = params->ssid_len;
  1633. }
  1634. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  1635. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  1636. ssid->proto = WPA_PROTO_RSN;
  1637. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  1638. ssid->group_cipher = WPA_CIPHER_CCMP;
  1639. if (params->freq > 56160) {
  1640. /*
  1641. * Enable GCMP instead of CCMP as pairwise_cipher and
  1642. * group_cipher in 60 GHz.
  1643. */
  1644. ssid->pairwise_cipher = WPA_CIPHER_GCMP;
  1645. ssid->group_cipher = WPA_CIPHER_GCMP;
  1646. /* P2P GO in 60 GHz is always a PCP (PBSS) */
  1647. ssid->pbss = 1;
  1648. }
  1649. if (os_strlen(params->passphrase) > 0) {
  1650. ssid->passphrase = os_strdup(params->passphrase);
  1651. if (ssid->passphrase == NULL) {
  1652. wpa_msg_global(wpa_s, MSG_ERROR,
  1653. "P2P: Failed to copy passphrase for GO");
  1654. wpa_config_remove_network(wpa_s->conf, ssid->id);
  1655. return;
  1656. }
  1657. } else
  1658. ssid->passphrase = NULL;
  1659. ssid->psk_set = params->psk_set;
  1660. if (ssid->psk_set)
  1661. os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
  1662. else if (ssid->passphrase)
  1663. wpa_config_update_psk(ssid);
  1664. ssid->ap_max_inactivity = wpa_s->p2pdev->conf->p2p_go_max_inactivity;
  1665. wpa_s->ap_configured_cb = p2p_go_configured;
  1666. wpa_s->ap_configured_cb_ctx = wpa_s;
  1667. wpa_s->ap_configured_cb_data = wpa_s->go_params;
  1668. wpa_s->scan_req = NORMAL_SCAN_REQ;
  1669. wpa_s->connect_without_scan = ssid;
  1670. wpa_s->reassociate = 1;
  1671. wpa_s->disconnected = 0;
  1672. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
  1673. "start GO)");
  1674. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1675. }
  1676. static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
  1677. const struct wpa_supplicant *src)
  1678. {
  1679. struct wpa_config *d;
  1680. const struct wpa_config *s;
  1681. d = dst->conf;
  1682. s = src->conf;
  1683. #define C(n) if (s->n) d->n = os_strdup(s->n)
  1684. C(device_name);
  1685. C(manufacturer);
  1686. C(model_name);
  1687. C(model_number);
  1688. C(serial_number);
  1689. C(config_methods);
  1690. #undef C
  1691. os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
  1692. os_memcpy(d->sec_device_type, s->sec_device_type,
  1693. sizeof(d->sec_device_type));
  1694. d->num_sec_device_types = s->num_sec_device_types;
  1695. d->p2p_group_idle = s->p2p_group_idle;
  1696. d->p2p_go_freq_change_policy = s->p2p_go_freq_change_policy;
  1697. d->p2p_intra_bss = s->p2p_intra_bss;
  1698. d->persistent_reconnect = s->persistent_reconnect;
  1699. d->max_num_sta = s->max_num_sta;
  1700. d->pbc_in_m1 = s->pbc_in_m1;
  1701. d->ignore_old_scan_res = s->ignore_old_scan_res;
  1702. d->beacon_int = s->beacon_int;
  1703. d->dtim_period = s->dtim_period;
  1704. d->p2p_go_ctwindow = s->p2p_go_ctwindow;
  1705. d->disassoc_low_ack = s->disassoc_low_ack;
  1706. d->disable_scan_offload = s->disable_scan_offload;
  1707. d->passive_scan = s->passive_scan;
  1708. if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) {
  1709. d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
  1710. d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
  1711. }
  1712. d->p2p_cli_probe = s->p2p_cli_probe;
  1713. }
  1714. static void wpas_p2p_clone_config_dh(struct wpa_supplicant *dst,
  1715. const struct wpa_supplicant *src)
  1716. {
  1717. struct wpa_config *d;
  1718. const struct wpa_config *s;
  1719. d = dst->conf;
  1720. s = src->conf;
  1721. if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey &&
  1722. !d->wps_nfc_dh_privkey && !d->wps_nfc_dh_pubkey) {
  1723. d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
  1724. d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
  1725. }
  1726. }
  1727. static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
  1728. char *ifname, size_t len)
  1729. {
  1730. char *ifname_ptr = wpa_s->ifname;
  1731. if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
  1732. os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
  1733. ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
  1734. }
  1735. os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
  1736. if (os_strlen(ifname) >= IFNAMSIZ &&
  1737. os_strlen(wpa_s->ifname) < IFNAMSIZ) {
  1738. int res;
  1739. /* Try to avoid going over the IFNAMSIZ length limit */
  1740. res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
  1741. if (os_snprintf_error(len, res) && len)
  1742. ifname[len - 1] = '\0';
  1743. }
  1744. }
  1745. static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
  1746. enum wpa_driver_if_type type)
  1747. {
  1748. char ifname[120], force_ifname[120];
  1749. if (wpa_s->pending_interface_name[0]) {
  1750. wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
  1751. "- skip creation of a new one");
  1752. if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
  1753. wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
  1754. "unknown?! ifname='%s'",
  1755. wpa_s->pending_interface_name);
  1756. return -1;
  1757. }
  1758. return 0;
  1759. }
  1760. wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
  1761. force_ifname[0] = '\0';
  1762. wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
  1763. ifname);
  1764. wpa_s->p2p_group_idx++;
  1765. wpa_s->pending_interface_type = type;
  1766. if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
  1767. wpa_s->pending_interface_addr, NULL) < 0) {
  1768. wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
  1769. "interface");
  1770. return -1;
  1771. }
  1772. if (force_ifname[0]) {
  1773. wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
  1774. force_ifname);
  1775. os_strlcpy(wpa_s->pending_interface_name, force_ifname,
  1776. sizeof(wpa_s->pending_interface_name));
  1777. } else
  1778. os_strlcpy(wpa_s->pending_interface_name, ifname,
  1779. sizeof(wpa_s->pending_interface_name));
  1780. wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
  1781. MACSTR, wpa_s->pending_interface_name,
  1782. MAC2STR(wpa_s->pending_interface_addr));
  1783. return 0;
  1784. }
  1785. static void wpas_p2p_remove_pending_group_interface(
  1786. struct wpa_supplicant *wpa_s)
  1787. {
  1788. if (!wpa_s->pending_interface_name[0] ||
  1789. is_zero_ether_addr(wpa_s->pending_interface_addr))
  1790. return; /* No pending virtual interface */
  1791. wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
  1792. wpa_s->pending_interface_name);
  1793. wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
  1794. wpa_s->pending_interface_name);
  1795. os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
  1796. wpa_s->pending_interface_name[0] = '\0';
  1797. wpa_s->global->pending_group_iface_for_p2ps = 0;
  1798. }
  1799. static struct wpa_supplicant *
  1800. wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
  1801. {
  1802. struct wpa_interface iface;
  1803. struct wpa_supplicant *group_wpa_s;
  1804. if (!wpa_s->pending_interface_name[0]) {
  1805. wpa_printf(MSG_ERROR, "P2P: No pending group interface");
  1806. if (!wpas_p2p_create_iface(wpa_s))
  1807. return NULL;
  1808. /*
  1809. * Something has forced us to remove the pending interface; try
  1810. * to create a new one and hope for the best that we will get
  1811. * the same local address.
  1812. */
  1813. if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
  1814. WPA_IF_P2P_CLIENT) < 0)
  1815. return NULL;
  1816. }
  1817. os_memset(&iface, 0, sizeof(iface));
  1818. iface.ifname = wpa_s->pending_interface_name;
  1819. iface.driver = wpa_s->driver->name;
  1820. if (wpa_s->conf->ctrl_interface == NULL &&
  1821. wpa_s->parent != wpa_s &&
  1822. wpa_s->p2p_mgmt &&
  1823. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
  1824. iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
  1825. else
  1826. iface.ctrl_interface = wpa_s->conf->ctrl_interface;
  1827. iface.driver_param = wpa_s->conf->driver_param;
  1828. group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
  1829. if (group_wpa_s == NULL) {
  1830. wpa_printf(MSG_ERROR, "P2P: Failed to create new "
  1831. "wpa_supplicant interface");
  1832. return NULL;
  1833. }
  1834. wpa_s->pending_interface_name[0] = '\0';
  1835. group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
  1836. P2P_GROUP_INTERFACE_CLIENT;
  1837. wpa_s->global->p2p_group_formation = group_wpa_s;
  1838. wpa_s->global->pending_group_iface_for_p2ps = 0;
  1839. wpas_p2p_clone_config(group_wpa_s, wpa_s);
  1840. return group_wpa_s;
  1841. }
  1842. static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
  1843. void *timeout_ctx)
  1844. {
  1845. struct wpa_supplicant *wpa_s = eloop_ctx;
  1846. wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
  1847. wpas_p2p_group_formation_failed(wpa_s, 0);
  1848. }
  1849. static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
  1850. int already_deleted)
  1851. {
  1852. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  1853. wpa_s->p2pdev, NULL);
  1854. if (wpa_s->global->p2p)
  1855. p2p_group_formation_failed(wpa_s->global->p2p);
  1856. wpas_group_formation_completed(wpa_s, 0, already_deleted);
  1857. }
  1858. static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
  1859. {
  1860. wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
  1861. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  1862. wpa_s->p2pdev, NULL);
  1863. eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
  1864. wpa_s->p2pdev, NULL);
  1865. wpa_s->global->p2p_fail_on_wps_complete = 0;
  1866. }
  1867. void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
  1868. {
  1869. if (wpa_s->global->p2p_group_formation != wpa_s)
  1870. return;
  1871. /* Speed up group formation timeout since this cannot succeed */
  1872. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  1873. wpa_s->p2pdev, NULL);
  1874. eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
  1875. wpa_s->p2pdev, NULL);
  1876. }
  1877. static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
  1878. {
  1879. struct wpa_supplicant *wpa_s = ctx;
  1880. struct wpa_supplicant *group_wpa_s;
  1881. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  1882. wpa_drv_cancel_remain_on_channel(wpa_s);
  1883. wpa_s->off_channel_freq = 0;
  1884. wpa_s->roc_waiting_drv_freq = 0;
  1885. }
  1886. if (res->status) {
  1887. wpa_msg_global(wpa_s, MSG_INFO,
  1888. P2P_EVENT_GO_NEG_FAILURE "status=%d",
  1889. res->status);
  1890. wpas_notify_p2p_go_neg_completed(wpa_s, res);
  1891. wpas_p2p_remove_pending_group_interface(wpa_s);
  1892. return;
  1893. }
  1894. if (!res->role_go) {
  1895. /* Inform driver of the operating channel of GO. */
  1896. wpa_drv_set_prob_oper_freq(wpa_s, res->freq);
  1897. }
  1898. if (wpa_s->p2p_go_ht40)
  1899. res->ht40 = 1;
  1900. if (wpa_s->p2p_go_vht)
  1901. res->vht = 1;
  1902. res->max_oper_chwidth = wpa_s->p2p_go_max_oper_chwidth;
  1903. res->vht_center_freq2 = wpa_s->p2p_go_vht_center_freq2;
  1904. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
  1905. "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
  1906. " wps_method=%s",
  1907. res->role_go ? "GO" : "client", res->freq, res->ht40,
  1908. MAC2STR(res->peer_device_addr),
  1909. MAC2STR(res->peer_interface_addr),
  1910. p2p_wps_method_text(res->wps_method));
  1911. wpas_notify_p2p_go_neg_completed(wpa_s, res);
  1912. if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
  1913. struct wpa_ssid *ssid;
  1914. ssid = wpa_config_get_network(wpa_s->conf,
  1915. wpa_s->p2p_persistent_id);
  1916. if (ssid && ssid->disabled == 2 &&
  1917. ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
  1918. size_t len = os_strlen(ssid->passphrase);
  1919. wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
  1920. "on requested persistent group");
  1921. os_memcpy(res->passphrase, ssid->passphrase, len);
  1922. res->passphrase[len] = '\0';
  1923. }
  1924. }
  1925. if (wpa_s->create_p2p_iface) {
  1926. group_wpa_s =
  1927. wpas_p2p_init_group_interface(wpa_s, res->role_go);
  1928. if (group_wpa_s == NULL) {
  1929. wpas_p2p_remove_pending_group_interface(wpa_s);
  1930. eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
  1931. wpa_s, NULL);
  1932. wpas_p2p_group_formation_failed(wpa_s, 1);
  1933. return;
  1934. }
  1935. os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
  1936. wpa_s->pending_interface_name[0] = '\0';
  1937. } else {
  1938. group_wpa_s = wpa_s->parent;
  1939. wpa_s->global->p2p_group_formation = group_wpa_s;
  1940. if (group_wpa_s != wpa_s)
  1941. wpas_p2p_clone_config_dh(group_wpa_s, wpa_s);
  1942. }
  1943. group_wpa_s->p2p_in_provisioning = 1;
  1944. group_wpa_s->p2pdev = wpa_s;
  1945. if (group_wpa_s != wpa_s) {
  1946. os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
  1947. sizeof(group_wpa_s->p2p_pin));
  1948. group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
  1949. }
  1950. if (res->role_go) {
  1951. wpas_start_wps_go(group_wpa_s, res, 1);
  1952. } else {
  1953. os_get_reltime(&group_wpa_s->scan_min_time);
  1954. wpas_start_wps_enrollee(group_wpa_s, res);
  1955. }
  1956. wpa_s->p2p_long_listen = 0;
  1957. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  1958. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  1959. eloop_register_timeout(15 + res->peer_config_timeout / 100,
  1960. (res->peer_config_timeout % 100) * 10000,
  1961. wpas_p2p_group_formation_timeout, wpa_s, NULL);
  1962. }
  1963. static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
  1964. u8 go_intent)
  1965. {
  1966. struct wpa_supplicant *wpa_s = ctx;
  1967. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
  1968. " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
  1969. dev_passwd_id, go_intent);
  1970. wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
  1971. }
  1972. static void wpas_dev_found(void *ctx, const u8 *addr,
  1973. const struct p2p_peer_info *info,
  1974. int new_device)
  1975. {
  1976. #ifndef CONFIG_NO_STDOUT_DEBUG
  1977. struct wpa_supplicant *wpa_s = ctx;
  1978. char devtype[WPS_DEV_TYPE_BUFSIZE];
  1979. char *wfd_dev_info_hex = NULL;
  1980. #ifdef CONFIG_WIFI_DISPLAY
  1981. wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
  1982. WFD_SUBELEM_DEVICE_INFO);
  1983. #endif /* CONFIG_WIFI_DISPLAY */
  1984. if (info->p2ps_instance) {
  1985. char str[256];
  1986. const u8 *buf = wpabuf_head(info->p2ps_instance);
  1987. size_t len = wpabuf_len(info->p2ps_instance);
  1988. while (len) {
  1989. u32 id;
  1990. u16 methods;
  1991. u8 str_len;
  1992. if (len < 4 + 2 + 1)
  1993. break;
  1994. id = WPA_GET_LE32(buf);
  1995. buf += sizeof(u32);
  1996. methods = WPA_GET_BE16(buf);
  1997. buf += sizeof(u16);
  1998. str_len = *buf++;
  1999. if (str_len > len - 4 - 2 - 1)
  2000. break;
  2001. os_memcpy(str, buf, str_len);
  2002. str[str_len] = '\0';
  2003. buf += str_len;
  2004. len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
  2005. wpa_msg_global(wpa_s, MSG_INFO,
  2006. P2P_EVENT_DEVICE_FOUND MACSTR
  2007. " p2p_dev_addr=" MACSTR
  2008. " pri_dev_type=%s name='%s'"
  2009. " config_methods=0x%x"
  2010. " dev_capab=0x%x"
  2011. " group_capab=0x%x"
  2012. " adv_id=%x asp_svc=%s%s",
  2013. MAC2STR(addr),
  2014. MAC2STR(info->p2p_device_addr),
  2015. wps_dev_type_bin2str(
  2016. info->pri_dev_type,
  2017. devtype, sizeof(devtype)),
  2018. info->device_name, methods,
  2019. info->dev_capab, info->group_capab,
  2020. id, str,
  2021. info->vendor_elems ?
  2022. " vendor_elems=1" : "");
  2023. }
  2024. goto done;
  2025. }
  2026. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
  2027. " p2p_dev_addr=" MACSTR
  2028. " pri_dev_type=%s name='%s' config_methods=0x%x "
  2029. "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
  2030. MAC2STR(addr), MAC2STR(info->p2p_device_addr),
  2031. wps_dev_type_bin2str(info->pri_dev_type, devtype,
  2032. sizeof(devtype)),
  2033. info->device_name, info->config_methods,
  2034. info->dev_capab, info->group_capab,
  2035. wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
  2036. wfd_dev_info_hex ? wfd_dev_info_hex : "",
  2037. info->vendor_elems ? " vendor_elems=1" : "",
  2038. new_device);
  2039. done:
  2040. os_free(wfd_dev_info_hex);
  2041. #endif /* CONFIG_NO_STDOUT_DEBUG */
  2042. wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
  2043. }
  2044. static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
  2045. {
  2046. struct wpa_supplicant *wpa_s = ctx;
  2047. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
  2048. "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
  2049. wpas_notify_p2p_device_lost(wpa_s, dev_addr);
  2050. }
  2051. static void wpas_find_stopped(void *ctx)
  2052. {
  2053. struct wpa_supplicant *wpa_s = ctx;
  2054. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
  2055. wpas_notify_p2p_find_stopped(wpa_s);
  2056. }
  2057. struct wpas_p2p_listen_work {
  2058. unsigned int freq;
  2059. unsigned int duration;
  2060. struct wpabuf *probe_resp_ie;
  2061. };
  2062. static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
  2063. {
  2064. if (lwork == NULL)
  2065. return;
  2066. wpabuf_free(lwork->probe_resp_ie);
  2067. os_free(lwork);
  2068. }
  2069. static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
  2070. {
  2071. struct wpas_p2p_listen_work *lwork;
  2072. if (!wpa_s->p2p_listen_work)
  2073. return;
  2074. lwork = wpa_s->p2p_listen_work->ctx;
  2075. wpas_p2p_listen_work_free(lwork);
  2076. radio_work_done(wpa_s->p2p_listen_work);
  2077. wpa_s->p2p_listen_work = NULL;
  2078. }
  2079. static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
  2080. {
  2081. struct wpa_supplicant *wpa_s = work->wpa_s;
  2082. struct wpas_p2p_listen_work *lwork = work->ctx;
  2083. unsigned int duration;
  2084. if (deinit) {
  2085. if (work->started) {
  2086. wpa_s->p2p_listen_work = NULL;
  2087. wpas_stop_listen(wpa_s);
  2088. }
  2089. wpas_p2p_listen_work_free(lwork);
  2090. return;
  2091. }
  2092. wpa_s->p2p_listen_work = work;
  2093. wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
  2094. if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
  2095. wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
  2096. "report received Probe Request frames");
  2097. wpas_p2p_listen_work_done(wpa_s);
  2098. return;
  2099. }
  2100. wpa_s->pending_listen_freq = lwork->freq;
  2101. wpa_s->pending_listen_duration = lwork->duration;
  2102. duration = lwork->duration;
  2103. #ifdef CONFIG_TESTING_OPTIONS
  2104. if (wpa_s->extra_roc_dur) {
  2105. wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
  2106. duration, duration + wpa_s->extra_roc_dur);
  2107. duration += wpa_s->extra_roc_dur;
  2108. }
  2109. #endif /* CONFIG_TESTING_OPTIONS */
  2110. if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
  2111. wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
  2112. "to remain on channel (%u MHz) for Listen "
  2113. "state", lwork->freq);
  2114. wpas_p2p_listen_work_done(wpa_s);
  2115. wpa_s->pending_listen_freq = 0;
  2116. return;
  2117. }
  2118. wpa_s->off_channel_freq = 0;
  2119. wpa_s->roc_waiting_drv_freq = lwork->freq;
  2120. }
  2121. static int wpas_start_listen(void *ctx, unsigned int freq,
  2122. unsigned int duration,
  2123. const struct wpabuf *probe_resp_ie)
  2124. {
  2125. struct wpa_supplicant *wpa_s = ctx;
  2126. struct wpas_p2p_listen_work *lwork;
  2127. if (wpa_s->p2p_listen_work) {
  2128. wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
  2129. return -1;
  2130. }
  2131. lwork = os_zalloc(sizeof(*lwork));
  2132. if (lwork == NULL)
  2133. return -1;
  2134. lwork->freq = freq;
  2135. lwork->duration = duration;
  2136. if (probe_resp_ie) {
  2137. lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
  2138. if (lwork->probe_resp_ie == NULL) {
  2139. wpas_p2p_listen_work_free(lwork);
  2140. return -1;
  2141. }
  2142. }
  2143. if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
  2144. lwork) < 0) {
  2145. wpas_p2p_listen_work_free(lwork);
  2146. return -1;
  2147. }
  2148. return 0;
  2149. }
  2150. static void wpas_stop_listen(void *ctx)
  2151. {
  2152. struct wpa_supplicant *wpa_s = ctx;
  2153. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  2154. wpa_drv_cancel_remain_on_channel(wpa_s);
  2155. wpa_s->off_channel_freq = 0;
  2156. wpa_s->roc_waiting_drv_freq = 0;
  2157. }
  2158. wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
  2159. /*
  2160. * Don't cancel Probe Request RX reporting for a connected P2P Client
  2161. * handling Probe Request frames.
  2162. */
  2163. if (!wpa_s->p2p_cli_probe)
  2164. wpa_drv_probe_req_report(wpa_s, 0);
  2165. wpas_p2p_listen_work_done(wpa_s);
  2166. }
  2167. static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
  2168. unsigned int freq)
  2169. {
  2170. struct wpa_supplicant *wpa_s = ctx;
  2171. return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
  2172. freq);
  2173. }
  2174. static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
  2175. const u8 *peer, const char *params,
  2176. unsigned int generated_pin)
  2177. {
  2178. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
  2179. " %08d%s", MAC2STR(peer), generated_pin, params);
  2180. }
  2181. static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
  2182. const u8 *peer, const char *params)
  2183. {
  2184. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
  2185. "%s", MAC2STR(peer), params);
  2186. }
  2187. static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
  2188. const u8 *dev_addr, const u8 *pri_dev_type,
  2189. const char *dev_name, u16 supp_config_methods,
  2190. u8 dev_capab, u8 group_capab, const u8 *group_id,
  2191. size_t group_id_len)
  2192. {
  2193. struct wpa_supplicant *wpa_s = ctx;
  2194. char devtype[WPS_DEV_TYPE_BUFSIZE];
  2195. char params[300];
  2196. u8 empty_dev_type[8];
  2197. unsigned int generated_pin = 0;
  2198. struct wpa_supplicant *group = NULL;
  2199. int res;
  2200. if (group_id) {
  2201. for (group = wpa_s->global->ifaces; group; group = group->next)
  2202. {
  2203. struct wpa_ssid *s = group->current_ssid;
  2204. if (s != NULL &&
  2205. s->mode == WPAS_MODE_P2P_GO &&
  2206. group_id_len - ETH_ALEN == s->ssid_len &&
  2207. os_memcmp(group_id + ETH_ALEN, s->ssid,
  2208. s->ssid_len) == 0)
  2209. break;
  2210. }
  2211. }
  2212. if (pri_dev_type == NULL) {
  2213. os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
  2214. pri_dev_type = empty_dev_type;
  2215. }
  2216. res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
  2217. " pri_dev_type=%s name='%s' config_methods=0x%x "
  2218. "dev_capab=0x%x group_capab=0x%x%s%s",
  2219. MAC2STR(dev_addr),
  2220. wps_dev_type_bin2str(pri_dev_type, devtype,
  2221. sizeof(devtype)),
  2222. dev_name, supp_config_methods, dev_capab, group_capab,
  2223. group ? " group=" : "",
  2224. group ? group->ifname : "");
  2225. if (os_snprintf_error(sizeof(params), res))
  2226. wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
  2227. params[sizeof(params) - 1] = '\0';
  2228. if (config_methods & WPS_CONFIG_DISPLAY) {
  2229. if (wps_generate_pin(&generated_pin) < 0) {
  2230. wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
  2231. wpas_notify_p2p_provision_discovery(
  2232. wpa_s, peer, 0 /* response */,
  2233. P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
  2234. return;
  2235. }
  2236. wpas_prov_disc_local_display(wpa_s, peer, params,
  2237. generated_pin);
  2238. } else if (config_methods & WPS_CONFIG_KEYPAD)
  2239. wpas_prov_disc_local_keypad(wpa_s, peer, params);
  2240. else if (config_methods & WPS_CONFIG_PUSHBUTTON)
  2241. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
  2242. MACSTR "%s", MAC2STR(peer), params);
  2243. wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
  2244. P2P_PROV_DISC_SUCCESS,
  2245. config_methods, generated_pin);
  2246. }
  2247. static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
  2248. {
  2249. struct wpa_supplicant *wpa_s = ctx;
  2250. unsigned int generated_pin = 0;
  2251. char params[20];
  2252. if (wpa_s->pending_pd_before_join &&
  2253. (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
  2254. os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
  2255. wpa_s->pending_pd_before_join = 0;
  2256. wpa_printf(MSG_DEBUG, "P2P: Starting pending "
  2257. "join-existing-group operation");
  2258. wpas_p2p_join_start(wpa_s, 0, NULL, 0);
  2259. return;
  2260. }
  2261. if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
  2262. wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
  2263. int res;
  2264. res = os_snprintf(params, sizeof(params), " peer_go=%d",
  2265. wpa_s->pending_pd_use == AUTO_PD_JOIN);
  2266. if (os_snprintf_error(sizeof(params), res))
  2267. params[sizeof(params) - 1] = '\0';
  2268. } else
  2269. params[0] = '\0';
  2270. if (config_methods & WPS_CONFIG_DISPLAY)
  2271. wpas_prov_disc_local_keypad(wpa_s, peer, params);
  2272. else if (config_methods & WPS_CONFIG_KEYPAD) {
  2273. if (wps_generate_pin(&generated_pin) < 0) {
  2274. wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
  2275. wpas_notify_p2p_provision_discovery(
  2276. wpa_s, peer, 0 /* response */,
  2277. P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
  2278. return;
  2279. }
  2280. wpas_prov_disc_local_display(wpa_s, peer, params,
  2281. generated_pin);
  2282. } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
  2283. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
  2284. MACSTR "%s", MAC2STR(peer), params);
  2285. wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
  2286. P2P_PROV_DISC_SUCCESS,
  2287. config_methods, generated_pin);
  2288. }
  2289. static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
  2290. enum p2p_prov_disc_status status,
  2291. u32 adv_id, const u8 *adv_mac,
  2292. const char *deferred_session_resp)
  2293. {
  2294. struct wpa_supplicant *wpa_s = ctx;
  2295. if (wpa_s->p2p_fallback_to_go_neg) {
  2296. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
  2297. "failed - fall back to GO Negotiation");
  2298. wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
  2299. P2P_EVENT_FALLBACK_TO_GO_NEG
  2300. "reason=PD-failed");
  2301. wpas_p2p_fallback_to_go_neg(wpa_s, 0);
  2302. return;
  2303. }
  2304. if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
  2305. wpa_s->pending_pd_before_join = 0;
  2306. wpa_printf(MSG_DEBUG, "P2P: Starting pending "
  2307. "join-existing-group operation (no ACK for PD "
  2308. "Req attempts)");
  2309. wpas_p2p_join_start(wpa_s, 0, NULL, 0);
  2310. return;
  2311. }
  2312. if (adv_id && adv_mac && deferred_session_resp) {
  2313. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
  2314. " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
  2315. " deferred_session_resp='%s'",
  2316. MAC2STR(peer), status, adv_id,
  2317. deferred_session_resp);
  2318. } else if (adv_id && adv_mac) {
  2319. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
  2320. " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
  2321. MAC2STR(peer), status, adv_id);
  2322. } else {
  2323. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
  2324. " p2p_dev_addr=" MACSTR " status=%d",
  2325. MAC2STR(peer), status);
  2326. }
  2327. wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
  2328. status, 0, 0);
  2329. }
  2330. static int freq_included(struct wpa_supplicant *wpa_s,
  2331. const struct p2p_channels *channels,
  2332. unsigned int freq)
  2333. {
  2334. if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
  2335. wpas_p2p_go_is_peer_freq(wpa_s, freq))
  2336. return 1;
  2337. return 0;
  2338. }
  2339. static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
  2340. {
  2341. unsigned int num = P2P_MAX_CHANNELS;
  2342. int *common_freqs;
  2343. int ret;
  2344. p2p_go_dump_common_freqs(wpa_s);
  2345. common_freqs = os_calloc(num, sizeof(int));
  2346. if (!common_freqs)
  2347. return;
  2348. ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
  2349. if (ret < 0) {
  2350. wpa_dbg(wpa_s, MSG_DEBUG,
  2351. "P2P: Failed to get group common freqs");
  2352. os_free(common_freqs);
  2353. return;
  2354. }
  2355. os_free(wpa_s->p2p_group_common_freqs);
  2356. wpa_s->p2p_group_common_freqs = common_freqs;
  2357. wpa_s->p2p_group_common_freqs_num = num;
  2358. p2p_go_dump_common_freqs(wpa_s);
  2359. }
  2360. /*
  2361. * Check if the given frequency is one of the possible operating frequencies
  2362. * set after the completion of the GO Negotiation.
  2363. */
  2364. static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
  2365. {
  2366. unsigned int i;
  2367. p2p_go_dump_common_freqs(wpa_s);
  2368. /* assume no restrictions */
  2369. if (!wpa_s->p2p_group_common_freqs_num)
  2370. return 1;
  2371. for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
  2372. if (wpa_s->p2p_group_common_freqs[i] == freq)
  2373. return 1;
  2374. }
  2375. return 0;
  2376. }
  2377. static int wpas_sta_check_ecsa(struct hostapd_data *hapd,
  2378. struct sta_info *sta, void *ctx)
  2379. {
  2380. int *ecsa_support = ctx;
  2381. *ecsa_support &= sta->ecsa_supported;
  2382. return 0;
  2383. }
  2384. /* Check if all the peers support eCSA */
  2385. static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s)
  2386. {
  2387. int ecsa_support = 1;
  2388. ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa,
  2389. &ecsa_support);
  2390. return ecsa_support;
  2391. }
  2392. /**
  2393. * Pick the best frequency to use from all the currently used frequencies.
  2394. */
  2395. static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
  2396. struct wpa_used_freq_data *freqs,
  2397. unsigned int num)
  2398. {
  2399. unsigned int i, c;
  2400. /* find a candidate freq that is supported by P2P */
  2401. for (c = 0; c < num; c++)
  2402. if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
  2403. break;
  2404. if (c == num)
  2405. return 0;
  2406. /* once we have a candidate, try to find a 'better' one */
  2407. for (i = c + 1; i < num; i++) {
  2408. if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
  2409. continue;
  2410. /*
  2411. * 1. Infrastructure station interfaces have higher preference.
  2412. * 2. P2P Clients have higher preference.
  2413. * 3. All others.
  2414. */
  2415. if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
  2416. c = i;
  2417. break;
  2418. }
  2419. if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
  2420. c = i;
  2421. }
  2422. return freqs[c].freq;
  2423. }
  2424. static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
  2425. const u8 *go_dev_addr, const u8 *ssid,
  2426. size_t ssid_len, int *go, u8 *group_bssid,
  2427. int *force_freq, int persistent_group,
  2428. const struct p2p_channels *channels,
  2429. int dev_pw_id)
  2430. {
  2431. struct wpa_supplicant *wpa_s = ctx;
  2432. struct wpa_ssid *s;
  2433. struct wpa_used_freq_data *freqs;
  2434. struct wpa_supplicant *grp;
  2435. int best_freq;
  2436. if (!persistent_group) {
  2437. wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
  2438. " to join an active group (SSID: %s)",
  2439. MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
  2440. if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
  2441. (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
  2442. == 0 ||
  2443. os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
  2444. wpa_printf(MSG_DEBUG, "P2P: Accept previously "
  2445. "authorized invitation");
  2446. goto accept_inv;
  2447. }
  2448. #ifdef CONFIG_WPS_NFC
  2449. if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
  2450. dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
  2451. wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
  2452. wpa_s->p2p_wps_method = WPS_NFC;
  2453. wpa_s->pending_join_wps_method = WPS_NFC;
  2454. os_memcpy(wpa_s->pending_join_dev_addr,
  2455. go_dev_addr, ETH_ALEN);
  2456. os_memcpy(wpa_s->pending_join_iface_addr,
  2457. bssid, ETH_ALEN);
  2458. goto accept_inv;
  2459. }
  2460. #endif /* CONFIG_WPS_NFC */
  2461. /*
  2462. * Do not accept the invitation automatically; notify user and
  2463. * request approval.
  2464. */
  2465. return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  2466. }
  2467. grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
  2468. if (grp) {
  2469. wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
  2470. "running persistent group");
  2471. if (*go)
  2472. os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
  2473. goto accept_inv;
  2474. }
  2475. if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
  2476. os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
  2477. wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
  2478. "invitation to re-invoke a persistent group");
  2479. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  2480. } else if (!wpa_s->conf->persistent_reconnect)
  2481. return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  2482. for (s = wpa_s->conf->ssid; s; s = s->next) {
  2483. if (s->disabled == 2 &&
  2484. os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
  2485. s->ssid_len == ssid_len &&
  2486. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  2487. break;
  2488. }
  2489. if (!s) {
  2490. wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
  2491. " requested reinvocation of an unknown group",
  2492. MAC2STR(sa));
  2493. return P2P_SC_FAIL_UNKNOWN_GROUP;
  2494. }
  2495. if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
  2496. *go = 1;
  2497. if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
  2498. wpa_printf(MSG_DEBUG, "P2P: The only available "
  2499. "interface is already in use - reject "
  2500. "invitation");
  2501. return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
  2502. }
  2503. if (wpa_s->p2p_mgmt)
  2504. os_memcpy(group_bssid, wpa_s->parent->own_addr,
  2505. ETH_ALEN);
  2506. else
  2507. os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
  2508. } else if (s->mode == WPAS_MODE_P2P_GO) {
  2509. *go = 1;
  2510. if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
  2511. {
  2512. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  2513. "interface address for the group");
  2514. return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
  2515. }
  2516. os_memcpy(group_bssid, wpa_s->pending_interface_addr,
  2517. ETH_ALEN);
  2518. }
  2519. accept_inv:
  2520. wpas_p2p_set_own_freq_preference(wpa_s, 0);
  2521. best_freq = 0;
  2522. freqs = os_calloc(wpa_s->num_multichan_concurrent,
  2523. sizeof(struct wpa_used_freq_data));
  2524. if (freqs) {
  2525. int num_channels = wpa_s->num_multichan_concurrent;
  2526. int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
  2527. best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
  2528. os_free(freqs);
  2529. }
  2530. /* Get one of the frequencies currently in use */
  2531. if (best_freq > 0) {
  2532. wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
  2533. wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
  2534. if (wpa_s->num_multichan_concurrent < 2 ||
  2535. wpas_p2p_num_unused_channels(wpa_s) < 1) {
  2536. wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
  2537. *force_freq = best_freq;
  2538. }
  2539. }
  2540. if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
  2541. wpas_p2p_num_unused_channels(wpa_s) > 0) {
  2542. if (*go == 0) {
  2543. /* We are the client */
  2544. wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
  2545. "running a GO but we are capable of MCC, "
  2546. "figure out the best channel to use");
  2547. *force_freq = 0;
  2548. } else if (!freq_included(wpa_s, channels, *force_freq)) {
  2549. /* We are the GO, and *force_freq is not in the
  2550. * intersection */
  2551. wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
  2552. "in intersection but we are capable of MCC, "
  2553. "figure out the best channel to use",
  2554. *force_freq);
  2555. *force_freq = 0;
  2556. }
  2557. }
  2558. return P2P_SC_SUCCESS;
  2559. }
  2560. static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
  2561. const u8 *ssid, size_t ssid_len,
  2562. const u8 *go_dev_addr, u8 status,
  2563. int op_freq)
  2564. {
  2565. struct wpa_supplicant *wpa_s = ctx;
  2566. struct wpa_ssid *s;
  2567. for (s = wpa_s->conf->ssid; s; s = s->next) {
  2568. if (s->disabled == 2 &&
  2569. s->ssid_len == ssid_len &&
  2570. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  2571. break;
  2572. }
  2573. if (status == P2P_SC_SUCCESS) {
  2574. wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
  2575. " was accepted; op_freq=%d MHz, SSID=%s",
  2576. MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
  2577. if (s) {
  2578. int go = s->mode == WPAS_MODE_P2P_GO;
  2579. wpas_p2p_group_add_persistent(
  2580. wpa_s, s, go, 0, op_freq, 0, 0, 0, 0, NULL,
  2581. go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
  2582. 1);
  2583. } else if (bssid) {
  2584. wpa_s->user_initiated_pd = 0;
  2585. wpas_p2p_join(wpa_s, bssid, go_dev_addr,
  2586. wpa_s->p2p_wps_method, 0, op_freq,
  2587. ssid, ssid_len);
  2588. }
  2589. return;
  2590. }
  2591. if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
  2592. wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
  2593. " was rejected (status %u)", MAC2STR(sa), status);
  2594. return;
  2595. }
  2596. if (!s) {
  2597. if (bssid) {
  2598. wpa_msg_global(wpa_s, MSG_INFO,
  2599. P2P_EVENT_INVITATION_RECEIVED
  2600. "sa=" MACSTR " go_dev_addr=" MACSTR
  2601. " bssid=" MACSTR " unknown-network",
  2602. MAC2STR(sa), MAC2STR(go_dev_addr),
  2603. MAC2STR(bssid));
  2604. } else {
  2605. wpa_msg_global(wpa_s, MSG_INFO,
  2606. P2P_EVENT_INVITATION_RECEIVED
  2607. "sa=" MACSTR " go_dev_addr=" MACSTR
  2608. " unknown-network",
  2609. MAC2STR(sa), MAC2STR(go_dev_addr));
  2610. }
  2611. wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
  2612. bssid, 0, op_freq);
  2613. return;
  2614. }
  2615. if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
  2616. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
  2617. "sa=" MACSTR " persistent=%d freq=%d",
  2618. MAC2STR(sa), s->id, op_freq);
  2619. } else {
  2620. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
  2621. "sa=" MACSTR " persistent=%d",
  2622. MAC2STR(sa), s->id);
  2623. }
  2624. wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
  2625. s->id, op_freq);
  2626. }
  2627. static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
  2628. struct wpa_ssid *ssid,
  2629. const u8 *peer, int inv)
  2630. {
  2631. size_t i;
  2632. struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
  2633. if (ssid == NULL)
  2634. return;
  2635. for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
  2636. if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
  2637. ETH_ALEN) == 0)
  2638. break;
  2639. }
  2640. if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
  2641. if (ssid->mode != WPAS_MODE_P2P_GO &&
  2642. os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
  2643. wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
  2644. "due to invitation result", ssid->id);
  2645. wpas_notify_network_removed(wpa_s, ssid);
  2646. wpa_config_remove_network(wpa_s->conf, ssid->id);
  2647. return;
  2648. }
  2649. return; /* Peer not found in client list */
  2650. }
  2651. wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
  2652. "group %d client list%s",
  2653. MAC2STR(peer), ssid->id,
  2654. inv ? " due to invitation result" : "");
  2655. os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
  2656. ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
  2657. (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
  2658. ssid->num_p2p_clients--;
  2659. if (p2p_wpa_s->conf->update_config &&
  2660. wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
  2661. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  2662. }
  2663. static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
  2664. const u8 *peer)
  2665. {
  2666. struct wpa_ssid *ssid;
  2667. wpa_s = wpa_s->global->p2p_invite_group;
  2668. if (wpa_s == NULL)
  2669. return; /* No known invitation group */
  2670. ssid = wpa_s->current_ssid;
  2671. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  2672. !ssid->p2p_persistent_group)
  2673. return; /* Not operating as a GO in persistent group */
  2674. ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer,
  2675. ssid->ssid, ssid->ssid_len);
  2676. wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
  2677. }
  2678. static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
  2679. const struct p2p_channels *channels,
  2680. const u8 *peer, int neg_freq,
  2681. int peer_oper_freq)
  2682. {
  2683. struct wpa_supplicant *wpa_s = ctx;
  2684. struct wpa_ssid *ssid;
  2685. int freq;
  2686. if (bssid) {
  2687. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
  2688. "status=%d " MACSTR,
  2689. status, MAC2STR(bssid));
  2690. } else {
  2691. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
  2692. "status=%d ", status);
  2693. }
  2694. wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
  2695. wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
  2696. status, MAC2STR(peer));
  2697. if (wpa_s->pending_invite_ssid_id == -1) {
  2698. struct wpa_supplicant *group_if =
  2699. wpa_s->global->p2p_invite_group;
  2700. if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
  2701. wpas_remove_persistent_client(wpa_s, peer);
  2702. /*
  2703. * Invitation to an active group. If this is successful and we
  2704. * are the GO, set the client wait to postpone some concurrent
  2705. * operations and to allow provisioning and connection to happen
  2706. * more quickly.
  2707. */
  2708. if (status == P2P_SC_SUCCESS &&
  2709. group_if && group_if->current_ssid &&
  2710. group_if->current_ssid->mode == WPAS_MODE_P2P_GO) {
  2711. os_get_reltime(&wpa_s->global->p2p_go_wait_client);
  2712. #ifdef CONFIG_TESTING_OPTIONS
  2713. if (group_if->p2p_go_csa_on_inv) {
  2714. wpa_printf(MSG_DEBUG,
  2715. "Testing: force P2P GO CSA after invitation");
  2716. eloop_cancel_timeout(
  2717. wpas_p2p_reconsider_moving_go,
  2718. wpa_s, NULL);
  2719. eloop_register_timeout(
  2720. 0, 50000,
  2721. wpas_p2p_reconsider_moving_go,
  2722. wpa_s, NULL);
  2723. }
  2724. #endif /* CONFIG_TESTING_OPTIONS */
  2725. }
  2726. return;
  2727. }
  2728. if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
  2729. wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
  2730. "invitation exchange to indicate readiness for "
  2731. "re-invocation");
  2732. }
  2733. if (status != P2P_SC_SUCCESS) {
  2734. if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
  2735. ssid = wpa_config_get_network(
  2736. wpa_s->conf, wpa_s->pending_invite_ssid_id);
  2737. wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
  2738. }
  2739. wpas_p2p_remove_pending_group_interface(wpa_s);
  2740. return;
  2741. }
  2742. ssid = wpa_config_get_network(wpa_s->conf,
  2743. wpa_s->pending_invite_ssid_id);
  2744. if (ssid == NULL) {
  2745. wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
  2746. "data matching with invitation");
  2747. return;
  2748. }
  2749. /*
  2750. * The peer could have missed our ctrl::ack frame for Invitation
  2751. * Response and continue retransmitting the frame. To reduce the
  2752. * likelihood of the peer not getting successful TX status for the
  2753. * Invitation Response frame, wait a short time here before starting
  2754. * the persistent group so that we will remain on the current channel to
  2755. * acknowledge any possible retransmission from the peer.
  2756. */
  2757. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
  2758. "starting persistent group");
  2759. os_sleep(0, 50000);
  2760. if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
  2761. freq_included(wpa_s, channels, neg_freq))
  2762. freq = neg_freq;
  2763. else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
  2764. freq_included(wpa_s, channels, peer_oper_freq))
  2765. freq = peer_oper_freq;
  2766. else
  2767. freq = 0;
  2768. wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
  2769. freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  2770. wpas_p2p_group_add_persistent(wpa_s, ssid,
  2771. ssid->mode == WPAS_MODE_P2P_GO,
  2772. wpa_s->p2p_persistent_go_freq,
  2773. freq,
  2774. wpa_s->p2p_go_vht_center_freq2,
  2775. wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
  2776. wpa_s->p2p_go_max_oper_chwidth,
  2777. channels,
  2778. ssid->mode == WPAS_MODE_P2P_GO ?
  2779. P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
  2780. 0, 1);
  2781. }
  2782. static int wpas_p2p_disallowed_freq(struct wpa_global *global,
  2783. unsigned int freq)
  2784. {
  2785. if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
  2786. return 1;
  2787. return freq_range_list_includes(&global->p2p_disallow_freq, freq);
  2788. }
  2789. static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
  2790. {
  2791. reg->channel[reg->channels] = chan;
  2792. reg->channels++;
  2793. }
  2794. static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
  2795. struct p2p_channels *chan,
  2796. struct p2p_channels *cli_chan)
  2797. {
  2798. int i, cla = 0;
  2799. wpa_s->global->p2p_24ghz_social_channels = 1;
  2800. os_memset(cli_chan, 0, sizeof(*cli_chan));
  2801. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
  2802. "band");
  2803. /* Operating class 81 - 2.4 GHz band channels 1..13 */
  2804. chan->reg_class[cla].reg_class = 81;
  2805. chan->reg_class[cla].channels = 0;
  2806. for (i = 0; i < 11; i++) {
  2807. if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
  2808. wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
  2809. }
  2810. if (chan->reg_class[cla].channels)
  2811. cla++;
  2812. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
  2813. "band");
  2814. /* Operating class 115 - 5 GHz, channels 36-48 */
  2815. chan->reg_class[cla].reg_class = 115;
  2816. chan->reg_class[cla].channels = 0;
  2817. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
  2818. wpas_p2p_add_chan(&chan->reg_class[cla], 36);
  2819. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
  2820. wpas_p2p_add_chan(&chan->reg_class[cla], 40);
  2821. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
  2822. wpas_p2p_add_chan(&chan->reg_class[cla], 44);
  2823. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
  2824. wpas_p2p_add_chan(&chan->reg_class[cla], 48);
  2825. if (chan->reg_class[cla].channels)
  2826. cla++;
  2827. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
  2828. "band");
  2829. /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
  2830. chan->reg_class[cla].reg_class = 124;
  2831. chan->reg_class[cla].channels = 0;
  2832. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
  2833. wpas_p2p_add_chan(&chan->reg_class[cla], 149);
  2834. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
  2835. wpas_p2p_add_chan(&chan->reg_class[cla], 153);
  2836. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
  2837. wpas_p2p_add_chan(&chan->reg_class[cla], 157);
  2838. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
  2839. wpas_p2p_add_chan(&chan->reg_class[cla], 161);
  2840. if (chan->reg_class[cla].channels)
  2841. cla++;
  2842. chan->reg_classes = cla;
  2843. return 0;
  2844. }
  2845. enum chan_allowed {
  2846. NOT_ALLOWED, NO_IR, ALLOWED
  2847. };
  2848. static int has_channel(struct wpa_global *global,
  2849. struct hostapd_hw_modes *mode, u8 chan, int *flags)
  2850. {
  2851. int i;
  2852. unsigned int freq;
  2853. freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
  2854. chan * 5;
  2855. if (wpas_p2p_disallowed_freq(global, freq))
  2856. return NOT_ALLOWED;
  2857. for (i = 0; i < mode->num_channels; i++) {
  2858. if (mode->channels[i].chan == chan) {
  2859. if (flags)
  2860. *flags = mode->channels[i].flag;
  2861. if (mode->channels[i].flag &
  2862. (HOSTAPD_CHAN_DISABLED |
  2863. HOSTAPD_CHAN_RADAR))
  2864. return NOT_ALLOWED;
  2865. if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
  2866. return NO_IR;
  2867. return ALLOWED;
  2868. }
  2869. }
  2870. return NOT_ALLOWED;
  2871. }
  2872. static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
  2873. struct hostapd_hw_modes *mode,
  2874. u8 channel)
  2875. {
  2876. u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
  2877. unsigned int i;
  2878. if (mode->mode != HOSTAPD_MODE_IEEE80211A)
  2879. return 0;
  2880. for (i = 0; i < ARRAY_SIZE(center_channels); i++)
  2881. /*
  2882. * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
  2883. * so the center channel is 6 channels away from the start/end.
  2884. */
  2885. if (channel >= center_channels[i] - 6 &&
  2886. channel <= center_channels[i] + 6)
  2887. return center_channels[i];
  2888. return 0;
  2889. }
  2890. static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
  2891. struct hostapd_hw_modes *mode,
  2892. u8 channel, u8 bw)
  2893. {
  2894. u8 center_chan;
  2895. int i, flags;
  2896. enum chan_allowed res, ret = ALLOWED;
  2897. center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
  2898. if (!center_chan)
  2899. return NOT_ALLOWED;
  2900. if (center_chan >= 58 && center_chan <= 138)
  2901. return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
  2902. /* check all the channels are available */
  2903. for (i = 0; i < 4; i++) {
  2904. int adj_chan = center_chan - 6 + i * 4;
  2905. res = has_channel(wpa_s->global, mode, adj_chan, &flags);
  2906. if (res == NOT_ALLOWED)
  2907. return NOT_ALLOWED;
  2908. if (res == NO_IR)
  2909. ret = NO_IR;
  2910. if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
  2911. return NOT_ALLOWED;
  2912. if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
  2913. return NOT_ALLOWED;
  2914. if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
  2915. return NOT_ALLOWED;
  2916. if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
  2917. return NOT_ALLOWED;
  2918. }
  2919. return ret;
  2920. }
  2921. static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s,
  2922. struct hostapd_hw_modes *mode,
  2923. u8 channel)
  2924. {
  2925. u8 center_channels[] = { 50, 114 };
  2926. unsigned int i;
  2927. if (mode->mode != HOSTAPD_MODE_IEEE80211A)
  2928. return 0;
  2929. for (i = 0; i < ARRAY_SIZE(center_channels); i++)
  2930. /*
  2931. * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
  2932. * so the center channel is 14 channels away from the start/end.
  2933. */
  2934. if (channel >= center_channels[i] - 14 &&
  2935. channel <= center_channels[i] + 14)
  2936. return center_channels[i];
  2937. return 0;
  2938. }
  2939. static enum chan_allowed wpas_p2p_verify_160mhz(struct wpa_supplicant *wpa_s,
  2940. struct hostapd_hw_modes *mode,
  2941. u8 channel, u8 bw)
  2942. {
  2943. u8 center_chan;
  2944. int i, flags;
  2945. enum chan_allowed res, ret = ALLOWED;
  2946. center_chan = wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
  2947. if (!center_chan)
  2948. return NOT_ALLOWED;
  2949. /* VHT 160 MHz uses DFS channels in most countries. */
  2950. /* Check all the channels are available */
  2951. for (i = 0; i < 8; i++) {
  2952. int adj_chan = center_chan - 14 + i * 4;
  2953. res = has_channel(wpa_s->global, mode, adj_chan, &flags);
  2954. if (res == NOT_ALLOWED)
  2955. return NOT_ALLOWED;
  2956. if (res == NO_IR)
  2957. ret = NO_IR;
  2958. if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_150))
  2959. return NOT_ALLOWED;
  2960. if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_130))
  2961. return NOT_ALLOWED;
  2962. if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_110))
  2963. return NOT_ALLOWED;
  2964. if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_90))
  2965. return NOT_ALLOWED;
  2966. if (i == 4 && !(flags & HOSTAPD_CHAN_VHT_90_70))
  2967. return NOT_ALLOWED;
  2968. if (i == 5 && !(flags & HOSTAPD_CHAN_VHT_110_50))
  2969. return NOT_ALLOWED;
  2970. if (i == 6 && !(flags & HOSTAPD_CHAN_VHT_130_30))
  2971. return NOT_ALLOWED;
  2972. if (i == 7 && !(flags & HOSTAPD_CHAN_VHT_150_10))
  2973. return NOT_ALLOWED;
  2974. }
  2975. return ret;
  2976. }
  2977. static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
  2978. struct hostapd_hw_modes *mode,
  2979. u8 channel, u8 bw)
  2980. {
  2981. int flag = 0;
  2982. enum chan_allowed res, res2;
  2983. res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
  2984. if (bw == BW40MINUS) {
  2985. if (!(flag & HOSTAPD_CHAN_HT40MINUS))
  2986. return NOT_ALLOWED;
  2987. res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
  2988. } else if (bw == BW40PLUS) {
  2989. if (!(flag & HOSTAPD_CHAN_HT40PLUS))
  2990. return NOT_ALLOWED;
  2991. res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
  2992. } else if (bw == BW80) {
  2993. res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
  2994. } else if (bw == BW160) {
  2995. res2 = wpas_p2p_verify_160mhz(wpa_s, mode, channel, bw);
  2996. }
  2997. if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
  2998. return NOT_ALLOWED;
  2999. if (res == NO_IR || res2 == NO_IR)
  3000. return NO_IR;
  3001. return res;
  3002. }
  3003. static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
  3004. struct p2p_channels *chan,
  3005. struct p2p_channels *cli_chan)
  3006. {
  3007. struct hostapd_hw_modes *mode;
  3008. int cla, op, cli_cla;
  3009. if (wpa_s->hw.modes == NULL) {
  3010. wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
  3011. "of all supported channels; assume dualband "
  3012. "support");
  3013. return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
  3014. }
  3015. cla = cli_cla = 0;
  3016. for (op = 0; global_op_class[op].op_class; op++) {
  3017. const struct oper_class_map *o = &global_op_class[op];
  3018. u8 ch;
  3019. struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
  3020. if (o->p2p == NO_P2P_SUPP)
  3021. continue;
  3022. mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
  3023. if (mode == NULL)
  3024. continue;
  3025. if (mode->mode == HOSTAPD_MODE_IEEE80211G)
  3026. wpa_s->global->p2p_24ghz_social_channels = 1;
  3027. for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
  3028. enum chan_allowed res;
  3029. res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
  3030. if (res == ALLOWED) {
  3031. if (reg == NULL) {
  3032. wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
  3033. o->op_class);
  3034. reg = &chan->reg_class[cla];
  3035. cla++;
  3036. reg->reg_class = o->op_class;
  3037. }
  3038. reg->channel[reg->channels] = ch;
  3039. reg->channels++;
  3040. } else if (res == NO_IR &&
  3041. wpa_s->conf->p2p_add_cli_chan) {
  3042. if (cli_reg == NULL) {
  3043. wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
  3044. o->op_class);
  3045. cli_reg = &cli_chan->reg_class[cli_cla];
  3046. cli_cla++;
  3047. cli_reg->reg_class = o->op_class;
  3048. }
  3049. cli_reg->channel[cli_reg->channels] = ch;
  3050. cli_reg->channels++;
  3051. }
  3052. }
  3053. if (reg) {
  3054. wpa_hexdump(MSG_DEBUG, "P2P: Channels",
  3055. reg->channel, reg->channels);
  3056. }
  3057. if (cli_reg) {
  3058. wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
  3059. cli_reg->channel, cli_reg->channels);
  3060. }
  3061. }
  3062. chan->reg_classes = cla;
  3063. cli_chan->reg_classes = cli_cla;
  3064. return 0;
  3065. }
  3066. int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
  3067. struct hostapd_hw_modes *mode, u8 channel)
  3068. {
  3069. int op;
  3070. enum chan_allowed ret;
  3071. for (op = 0; global_op_class[op].op_class; op++) {
  3072. const struct oper_class_map *o = &global_op_class[op];
  3073. u8 ch;
  3074. if (o->p2p == NO_P2P_SUPP)
  3075. continue;
  3076. for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
  3077. if (o->mode != HOSTAPD_MODE_IEEE80211A ||
  3078. (o->bw != BW40PLUS && o->bw != BW40MINUS) ||
  3079. ch != channel)
  3080. continue;
  3081. ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
  3082. if (ret == ALLOWED)
  3083. return (o->bw == BW40MINUS) ? -1 : 1;
  3084. }
  3085. }
  3086. return 0;
  3087. }
  3088. int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
  3089. struct hostapd_hw_modes *mode, u8 channel)
  3090. {
  3091. if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
  3092. return 0;
  3093. return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
  3094. }
  3095. int wpas_p2p_get_vht160_center(struct wpa_supplicant *wpa_s,
  3096. struct hostapd_hw_modes *mode, u8 channel)
  3097. {
  3098. if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW160))
  3099. return 0;
  3100. return wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
  3101. }
  3102. static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
  3103. size_t buf_len)
  3104. {
  3105. struct wpa_supplicant *wpa_s = ctx;
  3106. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3107. if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
  3108. break;
  3109. }
  3110. if (wpa_s == NULL)
  3111. return -1;
  3112. return wpa_drv_get_noa(wpa_s, buf, buf_len);
  3113. }
  3114. struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
  3115. const u8 *ssid, size_t ssid_len)
  3116. {
  3117. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3118. struct wpa_ssid *s = wpa_s->current_ssid;
  3119. if (s == NULL)
  3120. continue;
  3121. if (s->mode != WPAS_MODE_P2P_GO &&
  3122. s->mode != WPAS_MODE_AP &&
  3123. s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
  3124. continue;
  3125. if (s->ssid_len != ssid_len ||
  3126. os_memcmp(ssid, s->ssid, ssid_len) != 0)
  3127. continue;
  3128. return wpa_s;
  3129. }
  3130. return NULL;
  3131. }
  3132. struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
  3133. const u8 *peer_dev_addr)
  3134. {
  3135. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3136. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3137. if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
  3138. continue;
  3139. if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
  3140. return wpa_s;
  3141. }
  3142. return NULL;
  3143. }
  3144. static int wpas_go_connected(void *ctx, const u8 *dev_addr)
  3145. {
  3146. struct wpa_supplicant *wpa_s = ctx;
  3147. return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
  3148. }
  3149. static int wpas_is_concurrent_session_active(void *ctx)
  3150. {
  3151. struct wpa_supplicant *wpa_s = ctx;
  3152. struct wpa_supplicant *ifs;
  3153. for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
  3154. if (ifs == wpa_s)
  3155. continue;
  3156. if (ifs->wpa_state > WPA_ASSOCIATED)
  3157. return 1;
  3158. }
  3159. return 0;
  3160. }
  3161. static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
  3162. {
  3163. struct wpa_supplicant *wpa_s = ctx;
  3164. wpa_msg_global(wpa_s, level, "P2P: %s", msg);
  3165. }
  3166. int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
  3167. const char *conf_p2p_dev)
  3168. {
  3169. struct wpa_interface iface;
  3170. struct wpa_supplicant *p2pdev_wpa_s;
  3171. char ifname[100];
  3172. char force_name[100];
  3173. int ret;
  3174. ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
  3175. wpa_s->ifname);
  3176. if (os_snprintf_error(sizeof(ifname), ret))
  3177. return -1;
  3178. force_name[0] = '\0';
  3179. wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
  3180. ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
  3181. force_name, wpa_s->pending_interface_addr, NULL);
  3182. if (ret < 0) {
  3183. wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
  3184. return ret;
  3185. }
  3186. os_strlcpy(wpa_s->pending_interface_name, ifname,
  3187. sizeof(wpa_s->pending_interface_name));
  3188. os_memset(&iface, 0, sizeof(iface));
  3189. iface.p2p_mgmt = 1;
  3190. iface.ifname = wpa_s->pending_interface_name;
  3191. iface.driver = wpa_s->driver->name;
  3192. iface.driver_param = wpa_s->conf->driver_param;
  3193. /*
  3194. * If a P2P Device configuration file was given, use it as the interface
  3195. * configuration file (instead of using parent's configuration file.
  3196. */
  3197. if (conf_p2p_dev) {
  3198. iface.confname = conf_p2p_dev;
  3199. iface.ctrl_interface = NULL;
  3200. } else {
  3201. iface.confname = wpa_s->confname;
  3202. iface.ctrl_interface = wpa_s->conf->ctrl_interface;
  3203. }
  3204. p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
  3205. if (!p2pdev_wpa_s) {
  3206. wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
  3207. return -1;
  3208. }
  3209. p2pdev_wpa_s->p2pdev = p2pdev_wpa_s;
  3210. wpa_s->pending_interface_name[0] = '\0';
  3211. return 0;
  3212. }
  3213. static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
  3214. const u8 *noa, size_t noa_len)
  3215. {
  3216. struct wpa_supplicant *wpa_s, *intf = ctx;
  3217. char hex[100];
  3218. for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3219. if (wpa_s->waiting_presence_resp)
  3220. break;
  3221. }
  3222. if (!wpa_s) {
  3223. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
  3224. return;
  3225. }
  3226. wpa_s->waiting_presence_resp = 0;
  3227. wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
  3228. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
  3229. " status=%u noa=%s", MAC2STR(src), status, hex);
  3230. }
  3231. static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
  3232. size_t ssid_len, u8 *go_dev_addr,
  3233. u8 *ret_ssid, size_t *ret_ssid_len,
  3234. u8 *intended_iface_addr)
  3235. {
  3236. struct wpa_supplicant *wpa_s = ctx;
  3237. struct wpa_ssid *s;
  3238. s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
  3239. if (s) {
  3240. os_memcpy(ret_ssid, s->ssid, s->ssid_len);
  3241. *ret_ssid_len = s->ssid_len;
  3242. os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
  3243. if (s->mode != WPAS_MODE_P2P_GO) {
  3244. os_memset(intended_iface_addr, 0, ETH_ALEN);
  3245. } else if (wpas_p2p_create_iface(wpa_s)) {
  3246. if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
  3247. return 0;
  3248. os_memcpy(intended_iface_addr,
  3249. wpa_s->pending_interface_addr, ETH_ALEN);
  3250. } else {
  3251. os_memcpy(intended_iface_addr, wpa_s->own_addr,
  3252. ETH_ALEN);
  3253. }
  3254. return 1;
  3255. }
  3256. return 0;
  3257. }
  3258. static int wpas_get_go_info(void *ctx, u8 *intended_addr,
  3259. u8 *ssid, size_t *ssid_len, int *group_iface,
  3260. unsigned int *freq)
  3261. {
  3262. struct wpa_supplicant *wpa_s = ctx;
  3263. struct wpa_supplicant *go;
  3264. struct wpa_ssid *s;
  3265. /*
  3266. * group_iface will be set to 1 only if a dedicated interface for P2P
  3267. * role is required. First, we try to reuse an active GO. However,
  3268. * if it is not present, we will try to reactivate an existing
  3269. * persistent group and set group_iface to 1, so the caller will know
  3270. * that the pending interface should be used.
  3271. */
  3272. *group_iface = 0;
  3273. if (freq)
  3274. *freq = 0;
  3275. go = wpas_p2p_get_go_group(wpa_s);
  3276. if (!go) {
  3277. s = wpas_p2p_get_persistent_go(wpa_s);
  3278. *group_iface = wpas_p2p_create_iface(wpa_s);
  3279. if (s)
  3280. os_memcpy(intended_addr, s->bssid, ETH_ALEN);
  3281. else
  3282. return 0;
  3283. } else {
  3284. s = go->current_ssid;
  3285. os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
  3286. if (freq)
  3287. *freq = go->assoc_freq;
  3288. }
  3289. os_memcpy(ssid, s->ssid, s->ssid_len);
  3290. *ssid_len = s->ssid_len;
  3291. return 1;
  3292. }
  3293. static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
  3294. const u8 *ssid, size_t ssid_len)
  3295. {
  3296. struct wpa_supplicant *wpa_s = ctx;
  3297. struct wpa_ssid *s;
  3298. int save_config = 0;
  3299. size_t i;
  3300. /* Start with our first choice of Persistent Groups */
  3301. while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
  3302. if (go && ssid && ssid_len &&
  3303. s->ssid_len == ssid_len &&
  3304. os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
  3305. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  3306. break;
  3307. /* Remove stale persistent group */
  3308. if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
  3309. wpa_config_remove_network(wpa_s->conf, s->id);
  3310. save_config = 1;
  3311. continue;
  3312. }
  3313. for (i = 0; i < s->num_p2p_clients; i++) {
  3314. if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
  3315. peer, ETH_ALEN) != 0)
  3316. continue;
  3317. os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
  3318. s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
  3319. (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
  3320. break;
  3321. }
  3322. s->num_p2p_clients--;
  3323. save_config = 1;
  3324. }
  3325. if (save_config)
  3326. p2p_config_write(wpa_s);
  3327. /* Return TRUE if valid SSID remains */
  3328. return s != NULL;
  3329. }
  3330. static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
  3331. const u8 *feat_cap, size_t feat_cap_len)
  3332. {
  3333. static const char pref[] = " feature_cap=";
  3334. int ret;
  3335. buf[0] = '\0';
  3336. /*
  3337. * We expect a feature capability to contain at least one byte to be
  3338. * reported. The string buffer provided by the caller function is
  3339. * expected to be big enough to contain all bytes of the attribute for
  3340. * known specifications. This function truncates the reported bytes if
  3341. * the feature capability data exceeds the string buffer size.
  3342. */
  3343. if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
  3344. return;
  3345. os_memcpy(buf, pref, sizeof(pref));
  3346. ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
  3347. buf_len - sizeof(pref) + 1,
  3348. feat_cap, feat_cap_len);
  3349. if (ret != (2 * (int) feat_cap_len))
  3350. wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
  3351. }
  3352. static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
  3353. const u8 *adv_mac, const u8 *ses_mac,
  3354. const u8 *grp_mac, u32 adv_id, u32 ses_id,
  3355. u8 conncap, int passwd_id,
  3356. const u8 *persist_ssid,
  3357. size_t persist_ssid_size, int response_done,
  3358. int prov_start, const char *session_info,
  3359. const u8 *feat_cap, size_t feat_cap_len,
  3360. unsigned int freq,
  3361. const u8 *group_ssid, size_t group_ssid_len)
  3362. {
  3363. struct wpa_supplicant *wpa_s = ctx;
  3364. u8 mac[ETH_ALEN];
  3365. struct wpa_ssid *persistent_go, *stale, *s = NULL;
  3366. int save_config = 0;
  3367. struct wpa_supplicant *go_wpa_s;
  3368. char feat_cap_str[256];
  3369. if (!dev)
  3370. return;
  3371. os_memset(mac, 0, ETH_ALEN);
  3372. if (!adv_mac)
  3373. adv_mac = mac;
  3374. if (!ses_mac)
  3375. ses_mac = mac;
  3376. if (!grp_mac)
  3377. grp_mac = mac;
  3378. wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
  3379. feat_cap, feat_cap_len);
  3380. if (prov_start) {
  3381. if (session_info == NULL) {
  3382. wpa_msg_global(wpa_s, MSG_INFO,
  3383. P2P_EVENT_P2PS_PROVISION_START MACSTR
  3384. " adv_id=%x conncap=%x"
  3385. " adv_mac=" MACSTR
  3386. " session=%x mac=" MACSTR
  3387. " dev_passwd_id=%d%s",
  3388. MAC2STR(dev), adv_id, conncap,
  3389. MAC2STR(adv_mac),
  3390. ses_id, MAC2STR(ses_mac),
  3391. passwd_id, feat_cap_str);
  3392. } else {
  3393. wpa_msg_global(wpa_s, MSG_INFO,
  3394. P2P_EVENT_P2PS_PROVISION_START MACSTR
  3395. " adv_id=%x conncap=%x"
  3396. " adv_mac=" MACSTR
  3397. " session=%x mac=" MACSTR
  3398. " dev_passwd_id=%d info='%s'%s",
  3399. MAC2STR(dev), adv_id, conncap,
  3400. MAC2STR(adv_mac),
  3401. ses_id, MAC2STR(ses_mac),
  3402. passwd_id, session_info, feat_cap_str);
  3403. }
  3404. return;
  3405. }
  3406. go_wpa_s = wpas_p2p_get_go_group(wpa_s);
  3407. persistent_go = wpas_p2p_get_persistent_go(wpa_s);
  3408. if (status && status != P2P_SC_SUCCESS_DEFERRED) {
  3409. if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
  3410. wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
  3411. if (persistent_go && !persistent_go->num_p2p_clients) {
  3412. /* remove empty persistent GO */
  3413. wpa_config_remove_network(wpa_s->conf,
  3414. persistent_go->id);
  3415. }
  3416. wpa_msg_global(wpa_s, MSG_INFO,
  3417. P2P_EVENT_P2PS_PROVISION_DONE MACSTR
  3418. " status=%d"
  3419. " adv_id=%x adv_mac=" MACSTR
  3420. " session=%x mac=" MACSTR "%s",
  3421. MAC2STR(dev), status,
  3422. adv_id, MAC2STR(adv_mac),
  3423. ses_id, MAC2STR(ses_mac), feat_cap_str);
  3424. return;
  3425. }
  3426. /* Clean up stale persistent groups with this device */
  3427. if (persist_ssid && persist_ssid_size)
  3428. s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
  3429. persist_ssid_size);
  3430. if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
  3431. is_zero_ether_addr(grp_mac)) {
  3432. wpa_dbg(wpa_s, MSG_ERROR,
  3433. "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
  3434. return;
  3435. }
  3436. for (;;) {
  3437. stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
  3438. if (!stale)
  3439. break;
  3440. if (s && s->ssid_len == stale->ssid_len &&
  3441. os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
  3442. os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
  3443. break;
  3444. /* Remove stale persistent group */
  3445. if (stale->mode != WPAS_MODE_P2P_GO ||
  3446. stale->num_p2p_clients <= 1) {
  3447. wpa_config_remove_network(wpa_s->conf, stale->id);
  3448. } else {
  3449. size_t i;
  3450. for (i = 0; i < stale->num_p2p_clients; i++) {
  3451. if (os_memcmp(stale->p2p_client_list +
  3452. i * ETH_ALEN,
  3453. dev, ETH_ALEN) == 0) {
  3454. os_memmove(stale->p2p_client_list +
  3455. i * ETH_ALEN,
  3456. stale->p2p_client_list +
  3457. (i + 1) * ETH_ALEN,
  3458. (stale->num_p2p_clients -
  3459. i - 1) * ETH_ALEN);
  3460. break;
  3461. }
  3462. }
  3463. stale->num_p2p_clients--;
  3464. }
  3465. save_config = 1;
  3466. }
  3467. if (save_config)
  3468. p2p_config_write(wpa_s);
  3469. if (s) {
  3470. if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
  3471. wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
  3472. if (persistent_go && s != persistent_go &&
  3473. !persistent_go->num_p2p_clients) {
  3474. /* remove empty persistent GO */
  3475. wpa_config_remove_network(wpa_s->conf,
  3476. persistent_go->id);
  3477. /* Save config */
  3478. }
  3479. wpa_msg_global(wpa_s, MSG_INFO,
  3480. P2P_EVENT_P2PS_PROVISION_DONE MACSTR
  3481. " status=%d"
  3482. " adv_id=%x adv_mac=" MACSTR
  3483. " session=%x mac=" MACSTR
  3484. " persist=%d%s",
  3485. MAC2STR(dev), status,
  3486. adv_id, MAC2STR(adv_mac),
  3487. ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
  3488. return;
  3489. }
  3490. if (conncap == P2PS_SETUP_GROUP_OWNER) {
  3491. /*
  3492. * We need to copy the interface name. Simply saving a
  3493. * pointer isn't enough, since if we use pending_interface_name
  3494. * it will be overwritten when the group is added.
  3495. */
  3496. char go_ifname[100];
  3497. go_ifname[0] = '\0';
  3498. if (!go_wpa_s) {
  3499. wpa_s->global->pending_p2ps_group = 1;
  3500. wpa_s->global->pending_p2ps_group_freq = freq;
  3501. if (!wpas_p2p_create_iface(wpa_s))
  3502. os_memcpy(go_ifname, wpa_s->ifname,
  3503. sizeof(go_ifname));
  3504. else if (wpa_s->pending_interface_name[0])
  3505. os_memcpy(go_ifname,
  3506. wpa_s->pending_interface_name,
  3507. sizeof(go_ifname));
  3508. if (!go_ifname[0]) {
  3509. wpas_p2ps_prov_complete(
  3510. wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
  3511. dev, adv_mac, ses_mac,
  3512. grp_mac, adv_id, ses_id, 0, 0,
  3513. NULL, 0, 0, 0, NULL, NULL, 0, 0,
  3514. NULL, 0);
  3515. return;
  3516. }
  3517. /* If PD Resp complete, start up the GO */
  3518. if (response_done && persistent_go) {
  3519. wpas_p2p_group_add_persistent(
  3520. wpa_s, persistent_go,
  3521. 0, 0, freq, 0, 0, 0, 0, NULL,
  3522. persistent_go->mode ==
  3523. WPAS_MODE_P2P_GO ?
  3524. P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
  3525. 0, 0);
  3526. } else if (response_done) {
  3527. wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0);
  3528. }
  3529. if (passwd_id == DEV_PW_P2PS_DEFAULT) {
  3530. os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
  3531. ETH_ALEN);
  3532. wpa_s->p2ps_method_config_any = 1;
  3533. }
  3534. } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
  3535. os_memcpy(go_ifname, go_wpa_s->ifname,
  3536. sizeof(go_ifname));
  3537. if (is_zero_ether_addr(grp_mac)) {
  3538. wpa_dbg(go_wpa_s, MSG_DEBUG,
  3539. "P2P: Setting PIN-1 for ANY");
  3540. wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
  3541. "12345670", NULL, 0,
  3542. 0);
  3543. } else {
  3544. wpa_dbg(go_wpa_s, MSG_DEBUG,
  3545. "P2P: Setting PIN-1 for " MACSTR,
  3546. MAC2STR(grp_mac));
  3547. wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
  3548. "12345670", NULL, 0,
  3549. 0);
  3550. }
  3551. os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
  3552. wpa_s->p2ps_method_config_any = 1;
  3553. }
  3554. wpa_msg_global(wpa_s, MSG_INFO,
  3555. P2P_EVENT_P2PS_PROVISION_DONE MACSTR
  3556. " status=%d conncap=%x"
  3557. " adv_id=%x adv_mac=" MACSTR
  3558. " session=%x mac=" MACSTR
  3559. " dev_passwd_id=%d go=%s%s",
  3560. MAC2STR(dev), status, conncap,
  3561. adv_id, MAC2STR(adv_mac),
  3562. ses_id, MAC2STR(ses_mac),
  3563. passwd_id, go_ifname, feat_cap_str);
  3564. return;
  3565. }
  3566. if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
  3567. wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
  3568. if (persistent_go && !persistent_go->num_p2p_clients) {
  3569. /* remove empty persistent GO */
  3570. wpa_config_remove_network(wpa_s->conf, persistent_go->id);
  3571. }
  3572. if (conncap == P2PS_SETUP_CLIENT) {
  3573. char ssid_hex[32 * 2 + 1];
  3574. if (group_ssid)
  3575. wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex),
  3576. group_ssid, group_ssid_len);
  3577. else
  3578. ssid_hex[0] = '\0';
  3579. wpa_msg_global(wpa_s, MSG_INFO,
  3580. P2P_EVENT_P2PS_PROVISION_DONE MACSTR
  3581. " status=%d conncap=%x"
  3582. " adv_id=%x adv_mac=" MACSTR
  3583. " session=%x mac=" MACSTR
  3584. " dev_passwd_id=%d join=" MACSTR "%s%s%s",
  3585. MAC2STR(dev), status, conncap,
  3586. adv_id, MAC2STR(adv_mac),
  3587. ses_id, MAC2STR(ses_mac),
  3588. passwd_id, MAC2STR(grp_mac), feat_cap_str,
  3589. group_ssid ? " group_ssid=" : "", ssid_hex);
  3590. } else {
  3591. wpa_msg_global(wpa_s, MSG_INFO,
  3592. P2P_EVENT_P2PS_PROVISION_DONE MACSTR
  3593. " status=%d conncap=%x"
  3594. " adv_id=%x adv_mac=" MACSTR
  3595. " session=%x mac=" MACSTR
  3596. " dev_passwd_id=%d%s",
  3597. MAC2STR(dev), status, conncap,
  3598. adv_id, MAC2STR(adv_mac),
  3599. ses_id, MAC2STR(ses_mac),
  3600. passwd_id, feat_cap_str);
  3601. }
  3602. }
  3603. static int _wpas_p2p_in_progress(void *ctx)
  3604. {
  3605. struct wpa_supplicant *wpa_s = ctx;
  3606. return wpas_p2p_in_progress(wpa_s);
  3607. }
  3608. static int wpas_prov_disc_resp_cb(void *ctx)
  3609. {
  3610. struct wpa_supplicant *wpa_s = ctx;
  3611. struct wpa_ssid *persistent_go;
  3612. unsigned int freq;
  3613. if (!wpa_s->global->pending_p2ps_group)
  3614. return 0;
  3615. freq = wpa_s->global->pending_p2ps_group_freq;
  3616. wpa_s->global->pending_p2ps_group_freq = 0;
  3617. wpa_s->global->pending_p2ps_group = 0;
  3618. if (wpas_p2p_get_go_group(wpa_s))
  3619. return 0;
  3620. persistent_go = wpas_p2p_get_persistent_go(wpa_s);
  3621. if (persistent_go) {
  3622. wpas_p2p_group_add_persistent(
  3623. wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, NULL,
  3624. persistent_go->mode == WPAS_MODE_P2P_GO ?
  3625. P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0);
  3626. } else {
  3627. wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0);
  3628. }
  3629. return 1;
  3630. }
  3631. static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
  3632. unsigned int *len,
  3633. unsigned int *freq_list)
  3634. {
  3635. struct wpa_supplicant *wpa_s = ctx;
  3636. return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
  3637. WPA_IF_P2P_CLIENT, len, freq_list);
  3638. }
  3639. /**
  3640. * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
  3641. * @global: Pointer to global data from wpa_supplicant_init()
  3642. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3643. * Returns: 0 on success, -1 on failure
  3644. */
  3645. int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
  3646. {
  3647. struct p2p_config p2p;
  3648. int i;
  3649. if (wpa_s->conf->p2p_disabled)
  3650. return 0;
  3651. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
  3652. return 0;
  3653. if (global->p2p)
  3654. return 0;
  3655. os_memset(&p2p, 0, sizeof(p2p));
  3656. p2p.cb_ctx = wpa_s;
  3657. p2p.debug_print = wpas_p2p_debug_print;
  3658. p2p.p2p_scan = wpas_p2p_scan;
  3659. p2p.send_action = wpas_send_action;
  3660. p2p.send_action_done = wpas_send_action_done;
  3661. p2p.go_neg_completed = wpas_go_neg_completed;
  3662. p2p.go_neg_req_rx = wpas_go_neg_req_rx;
  3663. p2p.dev_found = wpas_dev_found;
  3664. p2p.dev_lost = wpas_dev_lost;
  3665. p2p.find_stopped = wpas_find_stopped;
  3666. p2p.start_listen = wpas_start_listen;
  3667. p2p.stop_listen = wpas_stop_listen;
  3668. p2p.send_probe_resp = wpas_send_probe_resp;
  3669. p2p.sd_request = wpas_sd_request;
  3670. p2p.sd_response = wpas_sd_response;
  3671. p2p.prov_disc_req = wpas_prov_disc_req;
  3672. p2p.prov_disc_resp = wpas_prov_disc_resp;
  3673. p2p.prov_disc_fail = wpas_prov_disc_fail;
  3674. p2p.invitation_process = wpas_invitation_process;
  3675. p2p.invitation_received = wpas_invitation_received;
  3676. p2p.invitation_result = wpas_invitation_result;
  3677. p2p.get_noa = wpas_get_noa;
  3678. p2p.go_connected = wpas_go_connected;
  3679. p2p.presence_resp = wpas_presence_resp;
  3680. p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
  3681. p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
  3682. p2p.get_persistent_group = wpas_get_persistent_group;
  3683. p2p.get_go_info = wpas_get_go_info;
  3684. p2p.remove_stale_groups = wpas_remove_stale_groups;
  3685. p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
  3686. p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
  3687. p2p.p2ps_group_capability = p2ps_group_capability;
  3688. p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
  3689. os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
  3690. os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
  3691. p2p.dev_name = wpa_s->conf->device_name;
  3692. p2p.manufacturer = wpa_s->conf->manufacturer;
  3693. p2p.model_name = wpa_s->conf->model_name;
  3694. p2p.model_number = wpa_s->conf->model_number;
  3695. p2p.serial_number = wpa_s->conf->serial_number;
  3696. if (wpa_s->wps) {
  3697. os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
  3698. p2p.config_methods = wpa_s->wps->config_methods;
  3699. }
  3700. if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
  3701. wpa_printf(MSG_ERROR,
  3702. "P2P: Failed to configure supported channel list");
  3703. return -1;
  3704. }
  3705. if (wpa_s->conf->p2p_listen_reg_class &&
  3706. wpa_s->conf->p2p_listen_channel) {
  3707. p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
  3708. p2p.channel = wpa_s->conf->p2p_listen_channel;
  3709. p2p.channel_forced = 1;
  3710. } else {
  3711. /*
  3712. * Pick one of the social channels randomly as the listen
  3713. * channel.
  3714. */
  3715. if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
  3716. &p2p.channel) != 0) {
  3717. wpa_printf(MSG_INFO,
  3718. "P2P: No social channels supported by the driver - do not enable P2P");
  3719. return 0;
  3720. }
  3721. p2p.channel_forced = 0;
  3722. }
  3723. wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
  3724. p2p.reg_class, p2p.channel);
  3725. if (wpa_s->conf->p2p_oper_reg_class &&
  3726. wpa_s->conf->p2p_oper_channel) {
  3727. p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
  3728. p2p.op_channel = wpa_s->conf->p2p_oper_channel;
  3729. p2p.cfg_op_channel = 1;
  3730. wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
  3731. "%d:%d", p2p.op_reg_class, p2p.op_channel);
  3732. } else {
  3733. /*
  3734. * Use random operation channel from 2.4 GHz band social
  3735. * channels (1, 6, 11) or band 60 GHz social channel (2) if no
  3736. * other preference is indicated.
  3737. */
  3738. if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
  3739. &p2p.op_channel) != 0) {
  3740. wpa_printf(MSG_ERROR,
  3741. "P2P: Failed to select random social channel as operation channel");
  3742. return -1;
  3743. }
  3744. p2p.cfg_op_channel = 0;
  3745. wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
  3746. "%d:%d", p2p.op_reg_class, p2p.op_channel);
  3747. }
  3748. if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
  3749. p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
  3750. p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
  3751. }
  3752. if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  3753. os_memcpy(p2p.country, wpa_s->conf->country, 2);
  3754. p2p.country[2] = 0x04;
  3755. } else
  3756. os_memcpy(p2p.country, "XX\x04", 3);
  3757. os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
  3758. WPS_DEV_TYPE_LEN);
  3759. p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
  3760. os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
  3761. p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
  3762. p2p.concurrent_operations = !!(wpa_s->drv_flags &
  3763. WPA_DRIVER_FLAGS_P2P_CONCURRENT);
  3764. p2p.max_peers = 100;
  3765. if (wpa_s->conf->p2p_ssid_postfix) {
  3766. p2p.ssid_postfix_len =
  3767. os_strlen(wpa_s->conf->p2p_ssid_postfix);
  3768. if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
  3769. p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
  3770. os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
  3771. p2p.ssid_postfix_len);
  3772. }
  3773. p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
  3774. p2p.max_listen = wpa_s->max_remain_on_chan;
  3775. if (wpa_s->conf->p2p_passphrase_len >= 8 &&
  3776. wpa_s->conf->p2p_passphrase_len <= 63)
  3777. p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
  3778. else
  3779. p2p.passphrase_len = 8;
  3780. global->p2p = p2p_init(&p2p);
  3781. if (global->p2p == NULL)
  3782. return -1;
  3783. global->p2p_init_wpa_s = wpa_s;
  3784. for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
  3785. if (wpa_s->conf->wps_vendor_ext[i] == NULL)
  3786. continue;
  3787. p2p_add_wps_vendor_extension(
  3788. global->p2p, wpa_s->conf->wps_vendor_ext[i]);
  3789. }
  3790. p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
  3791. return 0;
  3792. }
  3793. /**
  3794. * wpas_p2p_deinit - Deinitialize per-interface P2P data
  3795. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3796. *
  3797. * This function deinitialize per-interface P2P data.
  3798. */
  3799. void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
  3800. {
  3801. if (wpa_s->driver && wpa_s->drv_priv)
  3802. wpa_drv_probe_req_report(wpa_s, 0);
  3803. if (wpa_s->go_params) {
  3804. /* Clear any stored provisioning info */
  3805. p2p_clear_provisioning_info(
  3806. wpa_s->global->p2p,
  3807. wpa_s->go_params->peer_device_addr);
  3808. }
  3809. os_free(wpa_s->go_params);
  3810. wpa_s->go_params = NULL;
  3811. eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
  3812. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  3813. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3814. wpa_s->p2p_long_listen = 0;
  3815. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  3816. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  3817. wpas_p2p_remove_pending_group_interface(wpa_s);
  3818. eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
  3819. wpas_p2p_listen_work_done(wpa_s);
  3820. if (wpa_s->p2p_send_action_work) {
  3821. os_free(wpa_s->p2p_send_action_work->ctx);
  3822. radio_work_done(wpa_s->p2p_send_action_work);
  3823. wpa_s->p2p_send_action_work = NULL;
  3824. }
  3825. eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
  3826. wpabuf_free(wpa_s->p2p_oob_dev_pw);
  3827. wpa_s->p2p_oob_dev_pw = NULL;
  3828. os_free(wpa_s->p2p_group_common_freqs);
  3829. wpa_s->p2p_group_common_freqs = NULL;
  3830. wpa_s->p2p_group_common_freqs_num = 0;
  3831. /* TODO: remove group interface from the driver if this wpa_s instance
  3832. * is on top of a P2P group interface */
  3833. }
  3834. /**
  3835. * wpas_p2p_deinit_global - Deinitialize global P2P module
  3836. * @global: Pointer to global data from wpa_supplicant_init()
  3837. *
  3838. * This function deinitializes the global (per device) P2P module.
  3839. */
  3840. static void wpas_p2p_deinit_global(struct wpa_global *global)
  3841. {
  3842. struct wpa_supplicant *wpa_s, *tmp;
  3843. wpa_s = global->ifaces;
  3844. wpas_p2p_service_flush(global->p2p_init_wpa_s);
  3845. /* Remove remaining P2P group interfaces */
  3846. while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
  3847. wpa_s = wpa_s->next;
  3848. while (wpa_s) {
  3849. tmp = global->ifaces;
  3850. while (tmp &&
  3851. (tmp == wpa_s ||
  3852. tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
  3853. tmp = tmp->next;
  3854. }
  3855. if (tmp == NULL)
  3856. break;
  3857. /* Disconnect from the P2P group and deinit the interface */
  3858. wpas_p2p_disconnect(tmp);
  3859. }
  3860. /*
  3861. * Deinit GO data on any possibly remaining interface (if main
  3862. * interface is used as GO).
  3863. */
  3864. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3865. if (wpa_s->ap_iface)
  3866. wpas_p2p_group_deinit(wpa_s);
  3867. }
  3868. p2p_deinit(global->p2p);
  3869. global->p2p = NULL;
  3870. global->p2p_init_wpa_s = NULL;
  3871. }
  3872. static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
  3873. {
  3874. if (wpa_s->conf->p2p_no_group_iface)
  3875. return 0; /* separate interface disabled per configuration */
  3876. if (wpa_s->drv_flags &
  3877. (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
  3878. WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
  3879. return 1; /* P2P group requires a new interface in every case
  3880. */
  3881. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
  3882. return 0; /* driver does not support concurrent operations */
  3883. if (wpa_s->global->ifaces->next)
  3884. return 1; /* more that one interface already in use */
  3885. if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
  3886. return 1; /* this interface is already in use */
  3887. return 0;
  3888. }
  3889. static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
  3890. const u8 *peer_addr,
  3891. enum p2p_wps_method wps_method,
  3892. int go_intent, const u8 *own_interface_addr,
  3893. unsigned int force_freq, int persistent_group,
  3894. struct wpa_ssid *ssid, unsigned int pref_freq)
  3895. {
  3896. if (persistent_group && wpa_s->conf->persistent_reconnect)
  3897. persistent_group = 2;
  3898. /*
  3899. * Increase GO config timeout if HT40 is used since it takes some time
  3900. * to scan channels for coex purposes before the BSS can be started.
  3901. */
  3902. p2p_set_config_timeout(wpa_s->global->p2p,
  3903. wpa_s->p2p_go_ht40 ? 255 : 100, 20);
  3904. return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
  3905. go_intent, own_interface_addr, force_freq,
  3906. persistent_group, ssid ? ssid->ssid : NULL,
  3907. ssid ? ssid->ssid_len : 0,
  3908. wpa_s->p2p_pd_before_go_neg, pref_freq,
  3909. wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
  3910. 0);
  3911. }
  3912. static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
  3913. const u8 *peer_addr,
  3914. enum p2p_wps_method wps_method,
  3915. int go_intent, const u8 *own_interface_addr,
  3916. unsigned int force_freq, int persistent_group,
  3917. struct wpa_ssid *ssid, unsigned int pref_freq)
  3918. {
  3919. if (persistent_group && wpa_s->conf->persistent_reconnect)
  3920. persistent_group = 2;
  3921. return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
  3922. go_intent, own_interface_addr, force_freq,
  3923. persistent_group, ssid ? ssid->ssid : NULL,
  3924. ssid ? ssid->ssid_len : 0, pref_freq,
  3925. wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
  3926. 0);
  3927. }
  3928. static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
  3929. {
  3930. wpa_s->p2p_join_scan_count++;
  3931. wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
  3932. wpa_s->p2p_join_scan_count);
  3933. if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
  3934. wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
  3935. " for join operationg - stop join attempt",
  3936. MAC2STR(wpa_s->pending_join_iface_addr));
  3937. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3938. if (wpa_s->p2p_auto_pd) {
  3939. wpa_s->p2p_auto_pd = 0;
  3940. wpa_msg_global(wpa_s, MSG_INFO,
  3941. P2P_EVENT_PROV_DISC_FAILURE
  3942. " p2p_dev_addr=" MACSTR " status=N/A",
  3943. MAC2STR(wpa_s->pending_join_dev_addr));
  3944. return;
  3945. }
  3946. wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
  3947. P2P_EVENT_GROUP_FORMATION_FAILURE);
  3948. wpas_notify_p2p_group_formation_failure(wpa_s, "");
  3949. }
  3950. }
  3951. static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
  3952. {
  3953. int res;
  3954. unsigned int num, i;
  3955. struct wpa_used_freq_data *freqs;
  3956. if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
  3957. /* Multiple channels are supported and not all are in use */
  3958. return 0;
  3959. }
  3960. freqs = os_calloc(wpa_s->num_multichan_concurrent,
  3961. sizeof(struct wpa_used_freq_data));
  3962. if (!freqs)
  3963. return 1;
  3964. num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
  3965. wpa_s->num_multichan_concurrent);
  3966. for (i = 0; i < num; i++) {
  3967. if (freqs[i].freq == freq) {
  3968. wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
  3969. freq);
  3970. res = 0;
  3971. goto exit_free;
  3972. }
  3973. }
  3974. wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
  3975. res = 1;
  3976. exit_free:
  3977. os_free(freqs);
  3978. return res;
  3979. }
  3980. static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
  3981. const u8 *peer_dev_addr)
  3982. {
  3983. struct wpa_bss *bss;
  3984. int updated;
  3985. bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
  3986. if (bss == NULL)
  3987. return -1;
  3988. if (bss->last_update_idx < wpa_s->bss_update_idx) {
  3989. wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
  3990. "last scan");
  3991. return 0;
  3992. }
  3993. updated = os_reltime_before(&wpa_s->p2p_auto_started,
  3994. &bss->last_update);
  3995. wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
  3996. "%ld.%06ld (%supdated in last scan)",
  3997. bss->last_update.sec, bss->last_update.usec,
  3998. updated ? "": "not ");
  3999. return updated;
  4000. }
  4001. static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
  4002. struct wpa_scan_results *scan_res)
  4003. {
  4004. struct wpa_bss *bss = NULL;
  4005. int freq;
  4006. u8 iface_addr[ETH_ALEN];
  4007. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  4008. if (wpa_s->global->p2p_disabled)
  4009. return;
  4010. wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
  4011. scan_res ? (int) scan_res->num : -1,
  4012. wpa_s->p2p_auto_join ? "auto_" : "");
  4013. if (scan_res)
  4014. wpas_p2p_scan_res_handler(wpa_s, scan_res);
  4015. if (wpa_s->p2p_auto_pd) {
  4016. int join = wpas_p2p_peer_go(wpa_s,
  4017. wpa_s->pending_join_dev_addr);
  4018. if (join == 0 &&
  4019. wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
  4020. wpa_s->auto_pd_scan_retry++;
  4021. bss = wpa_bss_get_bssid_latest(
  4022. wpa_s, wpa_s->pending_join_dev_addr);
  4023. if (bss) {
  4024. freq = bss->freq;
  4025. wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
  4026. "the peer " MACSTR " at %d MHz",
  4027. wpa_s->auto_pd_scan_retry,
  4028. MAC2STR(wpa_s->
  4029. pending_join_dev_addr),
  4030. freq);
  4031. wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
  4032. return;
  4033. }
  4034. }
  4035. if (join < 0)
  4036. join = 0;
  4037. wpa_s->p2p_auto_pd = 0;
  4038. wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
  4039. wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
  4040. MAC2STR(wpa_s->pending_join_dev_addr), join);
  4041. if (p2p_prov_disc_req(wpa_s->global->p2p,
  4042. wpa_s->pending_join_dev_addr, NULL,
  4043. wpa_s->pending_pd_config_methods, join,
  4044. 0, wpa_s->user_initiated_pd) < 0) {
  4045. wpa_s->p2p_auto_pd = 0;
  4046. wpa_msg_global(wpa_s, MSG_INFO,
  4047. P2P_EVENT_PROV_DISC_FAILURE
  4048. " p2p_dev_addr=" MACSTR " status=N/A",
  4049. MAC2STR(wpa_s->pending_join_dev_addr));
  4050. }
  4051. return;
  4052. }
  4053. if (wpa_s->p2p_auto_join) {
  4054. int join = wpas_p2p_peer_go(wpa_s,
  4055. wpa_s->pending_join_dev_addr);
  4056. if (join < 0) {
  4057. wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
  4058. "running a GO -> use GO Negotiation");
  4059. wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
  4060. P2P_EVENT_FALLBACK_TO_GO_NEG
  4061. "reason=peer-not-running-GO");
  4062. wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
  4063. wpa_s->p2p_pin, wpa_s->p2p_wps_method,
  4064. wpa_s->p2p_persistent_group, 0, 0, 0,
  4065. wpa_s->p2p_go_intent,
  4066. wpa_s->p2p_connect_freq,
  4067. wpa_s->p2p_go_vht_center_freq2,
  4068. wpa_s->p2p_persistent_id,
  4069. wpa_s->p2p_pd_before_go_neg,
  4070. wpa_s->p2p_go_ht40,
  4071. wpa_s->p2p_go_vht,
  4072. wpa_s->p2p_go_max_oper_chwidth,
  4073. NULL, 0);
  4074. return;
  4075. }
  4076. wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
  4077. "try to join the group", join ? "" :
  4078. " in older scan");
  4079. if (!join) {
  4080. wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
  4081. P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
  4082. wpa_s->p2p_fallback_to_go_neg = 1;
  4083. }
  4084. }
  4085. freq = p2p_get_oper_freq(wpa_s->global->p2p,
  4086. wpa_s->pending_join_iface_addr);
  4087. if (freq < 0 &&
  4088. p2p_get_interface_addr(wpa_s->global->p2p,
  4089. wpa_s->pending_join_dev_addr,
  4090. iface_addr) == 0 &&
  4091. os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
  4092. && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
  4093. wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
  4094. "address for join from " MACSTR " to " MACSTR
  4095. " based on newly discovered P2P peer entry",
  4096. MAC2STR(wpa_s->pending_join_iface_addr),
  4097. MAC2STR(iface_addr));
  4098. os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
  4099. ETH_ALEN);
  4100. freq = p2p_get_oper_freq(wpa_s->global->p2p,
  4101. wpa_s->pending_join_iface_addr);
  4102. }
  4103. if (freq >= 0) {
  4104. wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
  4105. "from P2P peer table: %d MHz", freq);
  4106. }
  4107. if (wpa_s->p2p_join_ssid_len) {
  4108. wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
  4109. MACSTR " and SSID %s",
  4110. MAC2STR(wpa_s->pending_join_iface_addr),
  4111. wpa_ssid_txt(wpa_s->p2p_join_ssid,
  4112. wpa_s->p2p_join_ssid_len));
  4113. bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
  4114. wpa_s->p2p_join_ssid,
  4115. wpa_s->p2p_join_ssid_len);
  4116. } else if (!bss) {
  4117. wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
  4118. MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
  4119. bss = wpa_bss_get_bssid_latest(wpa_s,
  4120. wpa_s->pending_join_iface_addr);
  4121. }
  4122. if (bss) {
  4123. u8 dev_addr[ETH_ALEN];
  4124. freq = bss->freq;
  4125. wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
  4126. "from BSS table: %d MHz (SSID %s)", freq,
  4127. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  4128. if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
  4129. dev_addr) == 0 &&
  4130. os_memcmp(wpa_s->pending_join_dev_addr,
  4131. wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
  4132. os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
  4133. ETH_ALEN) != 0) {
  4134. wpa_printf(MSG_DEBUG,
  4135. "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
  4136. MAC2STR(dev_addr),
  4137. MAC2STR(wpa_s->pending_join_dev_addr));
  4138. os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
  4139. ETH_ALEN);
  4140. }
  4141. }
  4142. if (freq > 0) {
  4143. u16 method;
  4144. if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
  4145. wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
  4146. P2P_EVENT_GROUP_FORMATION_FAILURE
  4147. "reason=FREQ_CONFLICT");
  4148. wpas_notify_p2p_group_formation_failure(
  4149. wpa_s, "FREQ_CONFLICT");
  4150. return;
  4151. }
  4152. wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
  4153. "prior to joining an existing group (GO " MACSTR
  4154. " freq=%u MHz)",
  4155. MAC2STR(wpa_s->pending_join_dev_addr), freq);
  4156. wpa_s->pending_pd_before_join = 1;
  4157. switch (wpa_s->pending_join_wps_method) {
  4158. case WPS_PIN_DISPLAY:
  4159. method = WPS_CONFIG_KEYPAD;
  4160. break;
  4161. case WPS_PIN_KEYPAD:
  4162. method = WPS_CONFIG_DISPLAY;
  4163. break;
  4164. case WPS_PBC:
  4165. method = WPS_CONFIG_PUSHBUTTON;
  4166. break;
  4167. case WPS_P2PS:
  4168. method = WPS_CONFIG_P2PS;
  4169. break;
  4170. default:
  4171. method = 0;
  4172. break;
  4173. }
  4174. if ((p2p_get_provisioning_info(wpa_s->global->p2p,
  4175. wpa_s->pending_join_dev_addr) ==
  4176. method)) {
  4177. /*
  4178. * We have already performed provision discovery for
  4179. * joining the group. Proceed directly to join
  4180. * operation without duplicated provision discovery. */
  4181. wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
  4182. "with " MACSTR " already done - proceed to "
  4183. "join",
  4184. MAC2STR(wpa_s->pending_join_dev_addr));
  4185. wpa_s->pending_pd_before_join = 0;
  4186. goto start;
  4187. }
  4188. if (p2p_prov_disc_req(wpa_s->global->p2p,
  4189. wpa_s->pending_join_dev_addr,
  4190. NULL, method, 1,
  4191. freq, wpa_s->user_initiated_pd) < 0) {
  4192. wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
  4193. "Discovery Request before joining an "
  4194. "existing group");
  4195. wpa_s->pending_pd_before_join = 0;
  4196. goto start;
  4197. }
  4198. return;
  4199. }
  4200. wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
  4201. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  4202. eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
  4203. wpas_p2p_check_join_scan_limit(wpa_s);
  4204. return;
  4205. start:
  4206. /* Start join operation immediately */
  4207. wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
  4208. wpa_s->p2p_join_ssid_len);
  4209. }
  4210. static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
  4211. const u8 *ssid, size_t ssid_len)
  4212. {
  4213. int ret;
  4214. struct wpa_driver_scan_params params;
  4215. struct wpabuf *wps_ie, *ies;
  4216. size_t ielen;
  4217. int freqs[2] = { 0, 0 };
  4218. unsigned int bands;
  4219. os_memset(&params, 0, sizeof(params));
  4220. /* P2P Wildcard SSID */
  4221. params.num_ssids = 1;
  4222. if (ssid && ssid_len) {
  4223. params.ssids[0].ssid = ssid;
  4224. params.ssids[0].ssid_len = ssid_len;
  4225. os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
  4226. wpa_s->p2p_join_ssid_len = ssid_len;
  4227. } else {
  4228. params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
  4229. params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
  4230. wpa_s->p2p_join_ssid_len = 0;
  4231. }
  4232. wpa_s->wps->dev.p2p = 1;
  4233. wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
  4234. wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
  4235. NULL);
  4236. if (wps_ie == NULL) {
  4237. wpas_p2p_scan_res_join(wpa_s, NULL);
  4238. return;
  4239. }
  4240. if (!freq) {
  4241. int oper_freq;
  4242. /*
  4243. * If freq is not provided, check the operating freq of the GO
  4244. * and use a single channel scan on if possible.
  4245. */
  4246. oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
  4247. wpa_s->pending_join_iface_addr);
  4248. if (oper_freq > 0)
  4249. freq = oper_freq;
  4250. }
  4251. if (freq > 0) {
  4252. freqs[0] = freq;
  4253. params.freqs = freqs;
  4254. }
  4255. ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
  4256. ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
  4257. if (ies == NULL) {
  4258. wpabuf_free(wps_ie);
  4259. wpas_p2p_scan_res_join(wpa_s, NULL);
  4260. return;
  4261. }
  4262. wpabuf_put_buf(ies, wps_ie);
  4263. wpabuf_free(wps_ie);
  4264. bands = wpas_get_bands(wpa_s, freqs);
  4265. p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
  4266. params.p2p_probe = 1;
  4267. params.extra_ies = wpabuf_head(ies);
  4268. params.extra_ies_len = wpabuf_len(ies);
  4269. /*
  4270. * Run a scan to update BSS table and start Provision Discovery once
  4271. * the new scan results become available.
  4272. */
  4273. ret = wpa_drv_scan(wpa_s, &params);
  4274. if (!ret) {
  4275. os_get_reltime(&wpa_s->scan_trigger_time);
  4276. wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
  4277. wpa_s->own_scan_requested = 1;
  4278. }
  4279. wpabuf_free(ies);
  4280. if (ret) {
  4281. wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
  4282. "try again later");
  4283. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  4284. eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
  4285. wpas_p2p_check_join_scan_limit(wpa_s);
  4286. }
  4287. }
  4288. static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
  4289. {
  4290. struct wpa_supplicant *wpa_s = eloop_ctx;
  4291. wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
  4292. }
  4293. static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
  4294. const u8 *dev_addr, enum p2p_wps_method wps_method,
  4295. int auto_join, int op_freq,
  4296. const u8 *ssid, size_t ssid_len)
  4297. {
  4298. wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
  4299. MACSTR " dev " MACSTR " op_freq=%d)%s",
  4300. MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
  4301. auto_join ? " (auto_join)" : "");
  4302. if (ssid && ssid_len) {
  4303. wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
  4304. wpa_ssid_txt(ssid, ssid_len));
  4305. }
  4306. wpa_s->p2p_auto_pd = 0;
  4307. wpa_s->p2p_auto_join = !!auto_join;
  4308. os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
  4309. os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
  4310. wpa_s->pending_join_wps_method = wps_method;
  4311. /* Make sure we are not running find during connection establishment */
  4312. wpas_p2p_stop_find(wpa_s);
  4313. wpa_s->p2p_join_scan_count = 0;
  4314. wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
  4315. return 0;
  4316. }
  4317. static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
  4318. const u8 *ssid, size_t ssid_len)
  4319. {
  4320. struct wpa_supplicant *group;
  4321. struct p2p_go_neg_results res;
  4322. struct wpa_bss *bss;
  4323. group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
  4324. if (group == NULL)
  4325. return -1;
  4326. if (group != wpa_s) {
  4327. os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
  4328. sizeof(group->p2p_pin));
  4329. group->p2p_wps_method = wpa_s->p2p_wps_method;
  4330. } else {
  4331. /*
  4332. * Need to mark the current interface for p2p_group_formation
  4333. * when a separate group interface is not used. This is needed
  4334. * to allow p2p_cancel stop a pending p2p_connect-join.
  4335. * wpas_p2p_init_group_interface() addresses this for the case
  4336. * where a separate group interface is used.
  4337. */
  4338. wpa_s->global->p2p_group_formation = wpa_s;
  4339. }
  4340. group->p2p_in_provisioning = 1;
  4341. group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
  4342. os_memset(&res, 0, sizeof(res));
  4343. os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
  4344. os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
  4345. ETH_ALEN);
  4346. res.wps_method = wpa_s->pending_join_wps_method;
  4347. if (freq && ssid && ssid_len) {
  4348. res.freq = freq;
  4349. res.ssid_len = ssid_len;
  4350. os_memcpy(res.ssid, ssid, ssid_len);
  4351. } else {
  4352. if (ssid && ssid_len) {
  4353. bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
  4354. ssid, ssid_len);
  4355. } else {
  4356. bss = wpa_bss_get_bssid_latest(
  4357. wpa_s, wpa_s->pending_join_iface_addr);
  4358. }
  4359. if (bss) {
  4360. res.freq = bss->freq;
  4361. res.ssid_len = bss->ssid_len;
  4362. os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
  4363. wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
  4364. bss->freq,
  4365. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  4366. } else if (ssid && ssid_len) {
  4367. res.ssid_len = ssid_len;
  4368. os_memcpy(res.ssid, ssid, ssid_len);
  4369. wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
  4370. wpa_ssid_txt(ssid, ssid_len));
  4371. }
  4372. }
  4373. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  4374. wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
  4375. "starting client");
  4376. wpa_drv_cancel_remain_on_channel(wpa_s);
  4377. wpa_s->off_channel_freq = 0;
  4378. wpa_s->roc_waiting_drv_freq = 0;
  4379. }
  4380. wpas_start_wps_enrollee(group, &res);
  4381. /*
  4382. * Allow a longer timeout for join-a-running-group than normal 15
  4383. * second group formation timeout since the GO may not have authorized
  4384. * our connection yet.
  4385. */
  4386. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  4387. eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
  4388. wpa_s, NULL);
  4389. return 0;
  4390. }
  4391. static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
  4392. int *force_freq, int *pref_freq, int go,
  4393. unsigned int *pref_freq_list,
  4394. unsigned int *num_pref_freq)
  4395. {
  4396. struct wpa_used_freq_data *freqs;
  4397. int res, best_freq, num_unused;
  4398. unsigned int freq_in_use = 0, num, i, max_pref_freq;
  4399. max_pref_freq = *num_pref_freq;
  4400. *num_pref_freq = 0;
  4401. freqs = os_calloc(wpa_s->num_multichan_concurrent,
  4402. sizeof(struct wpa_used_freq_data));
  4403. if (!freqs)
  4404. return -1;
  4405. num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
  4406. wpa_s->num_multichan_concurrent);
  4407. /*
  4408. * It is possible that the total number of used frequencies is bigger
  4409. * than the number of frequencies used for P2P, so get the system wide
  4410. * number of unused frequencies.
  4411. */
  4412. num_unused = wpas_p2p_num_unused_channels(wpa_s);
  4413. wpa_printf(MSG_DEBUG,
  4414. "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
  4415. freq, wpa_s->num_multichan_concurrent, num, num_unused);
  4416. if (freq > 0) {
  4417. int ret;
  4418. if (go)
  4419. ret = p2p_supported_freq(wpa_s->global->p2p, freq);
  4420. else
  4421. ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
  4422. if (!ret) {
  4423. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
  4424. ieee80211_is_dfs(freq)) {
  4425. /*
  4426. * If freq is a DFS channel and DFS is offloaded
  4427. * to the driver, allow P2P GO to use it.
  4428. */
  4429. wpa_printf(MSG_DEBUG,
  4430. "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
  4431. freq);
  4432. } else {
  4433. wpa_printf(MSG_DEBUG,
  4434. "P2P: The forced channel (%u MHz) is not supported for P2P uses",
  4435. freq);
  4436. res = -3;
  4437. goto exit_free;
  4438. }
  4439. }
  4440. for (i = 0; i < num; i++) {
  4441. if (freqs[i].freq == freq)
  4442. freq_in_use = 1;
  4443. }
  4444. if (num_unused <= 0 && !freq_in_use) {
  4445. wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
  4446. freq);
  4447. res = -2;
  4448. goto exit_free;
  4449. }
  4450. wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
  4451. "requested channel (%u MHz)", freq);
  4452. *force_freq = freq;
  4453. goto exit_ok;
  4454. }
  4455. best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
  4456. if (!wpa_s->conf->num_p2p_pref_chan && *pref_freq == 0) {
  4457. enum wpa_driver_if_type iface_type;
  4458. if (go)
  4459. iface_type = WPA_IF_P2P_GO;
  4460. else
  4461. iface_type = WPA_IF_P2P_CLIENT;
  4462. wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
  4463. best_freq, go);
  4464. res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
  4465. &max_pref_freq,
  4466. pref_freq_list);
  4467. if (!res && max_pref_freq > 0) {
  4468. *num_pref_freq = max_pref_freq;
  4469. i = 0;
  4470. while (wpas_p2p_disallowed_freq(wpa_s->global,
  4471. pref_freq_list[i]) &&
  4472. i < *num_pref_freq) {
  4473. wpa_printf(MSG_DEBUG,
  4474. "P2P: preferred_freq_list[%d]=%d is disallowed",
  4475. i, pref_freq_list[i]);
  4476. i++;
  4477. }
  4478. if (i != *num_pref_freq) {
  4479. best_freq = pref_freq_list[i];
  4480. wpa_printf(MSG_DEBUG,
  4481. "P2P: Using preferred_freq_list[%d]=%d",
  4482. i, best_freq);
  4483. } else {
  4484. wpa_printf(MSG_DEBUG,
  4485. "P2P: All driver preferred frequencies are disallowed for P2P use");
  4486. *num_pref_freq = 0;
  4487. }
  4488. } else {
  4489. wpa_printf(MSG_DEBUG,
  4490. "P2P: No preferred frequency list available");
  4491. }
  4492. }
  4493. /* We have a candidate frequency to use */
  4494. if (best_freq > 0) {
  4495. if (*pref_freq == 0 && num_unused > 0) {
  4496. wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
  4497. best_freq);
  4498. *pref_freq = best_freq;
  4499. } else {
  4500. wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
  4501. best_freq);
  4502. *force_freq = best_freq;
  4503. }
  4504. } else if (num_unused > 0) {
  4505. wpa_printf(MSG_DEBUG,
  4506. "P2P: Current operating channels are not available for P2P. Try to use another channel");
  4507. *force_freq = 0;
  4508. } else {
  4509. wpa_printf(MSG_DEBUG,
  4510. "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
  4511. res = -2;
  4512. goto exit_free;
  4513. }
  4514. exit_ok:
  4515. res = 0;
  4516. exit_free:
  4517. os_free(freqs);
  4518. return res;
  4519. }
  4520. /**
  4521. * wpas_p2p_connect - Request P2P Group Formation to be started
  4522. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  4523. * @peer_addr: Address of the peer P2P Device
  4524. * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
  4525. * @persistent_group: Whether to create a persistent group
  4526. * @auto_join: Whether to select join vs. GO Negotiation automatically
  4527. * @join: Whether to join an existing group (as a client) instead of starting
  4528. * Group Owner negotiation; @peer_addr is BSSID in that case
  4529. * @auth: Whether to only authorize the connection instead of doing that and
  4530. * initiating Group Owner negotiation
  4531. * @go_intent: GO Intent or -1 to use default
  4532. * @freq: Frequency for the group or 0 for auto-selection
  4533. * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
  4534. * @persistent_id: Persistent group credentials to use for forcing GO
  4535. * parameters or -1 to generate new values (SSID/passphrase)
  4536. * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
  4537. * interoperability workaround when initiating group formation
  4538. * @ht40: Start GO with 40 MHz channel width
  4539. * @vht: Start GO with VHT support
  4540. * @vht_chwidth: Channel width supported by GO operating with VHT support
  4541. * (VHT_CHANWIDTH_*).
  4542. * @group_ssid: Specific Group SSID for join or %NULL if not set
  4543. * @group_ssid_len: Length of @group_ssid in octets
  4544. * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
  4545. * failure, -2 on failure due to channel not currently available,
  4546. * -3 if forced channel is not supported
  4547. */
  4548. int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  4549. const char *pin, enum p2p_wps_method wps_method,
  4550. int persistent_group, int auto_join, int join, int auth,
  4551. int go_intent, int freq, unsigned int vht_center_freq2,
  4552. int persistent_id, int pd, int ht40, int vht,
  4553. unsigned int vht_chwidth, const u8 *group_ssid,
  4554. size_t group_ssid_len)
  4555. {
  4556. int force_freq = 0, pref_freq = 0;
  4557. int ret = 0, res;
  4558. enum wpa_driver_if_type iftype;
  4559. const u8 *if_addr;
  4560. struct wpa_ssid *ssid = NULL;
  4561. unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
  4562. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4563. return -1;
  4564. if (persistent_id >= 0) {
  4565. ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
  4566. if (ssid == NULL || ssid->disabled != 2 ||
  4567. ssid->mode != WPAS_MODE_P2P_GO)
  4568. return -1;
  4569. }
  4570. os_free(wpa_s->global->add_psk);
  4571. wpa_s->global->add_psk = NULL;
  4572. wpa_s->global->p2p_fail_on_wps_complete = 0;
  4573. wpa_s->global->pending_p2ps_group = 0;
  4574. wpa_s->global->pending_p2ps_group_freq = 0;
  4575. wpa_s->p2ps_method_config_any = 0;
  4576. if (go_intent < 0)
  4577. go_intent = wpa_s->conf->p2p_go_intent;
  4578. if (!auth)
  4579. wpa_s->p2p_long_listen = 0;
  4580. wpa_s->p2p_wps_method = wps_method;
  4581. wpa_s->p2p_persistent_group = !!persistent_group;
  4582. wpa_s->p2p_persistent_id = persistent_id;
  4583. wpa_s->p2p_go_intent = go_intent;
  4584. wpa_s->p2p_connect_freq = freq;
  4585. wpa_s->p2p_fallback_to_go_neg = 0;
  4586. wpa_s->p2p_pd_before_go_neg = !!pd;
  4587. wpa_s->p2p_go_ht40 = !!ht40;
  4588. wpa_s->p2p_go_vht = !!vht;
  4589. wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
  4590. wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
  4591. if (pin)
  4592. os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
  4593. else if (wps_method == WPS_PIN_DISPLAY) {
  4594. if (wps_generate_pin((unsigned int *) &ret) < 0)
  4595. return -1;
  4596. res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
  4597. "%08d", ret);
  4598. if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
  4599. wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
  4600. wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
  4601. wpa_s->p2p_pin);
  4602. } else
  4603. wpa_s->p2p_pin[0] = '\0';
  4604. if (join || auto_join) {
  4605. u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
  4606. if (auth) {
  4607. wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
  4608. "connect a running group from " MACSTR,
  4609. MAC2STR(peer_addr));
  4610. os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
  4611. return ret;
  4612. }
  4613. os_memcpy(dev_addr, peer_addr, ETH_ALEN);
  4614. if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
  4615. iface_addr) < 0) {
  4616. os_memcpy(iface_addr, peer_addr, ETH_ALEN);
  4617. p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
  4618. dev_addr);
  4619. }
  4620. if (auto_join) {
  4621. os_get_reltime(&wpa_s->p2p_auto_started);
  4622. wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
  4623. "%ld.%06ld",
  4624. wpa_s->p2p_auto_started.sec,
  4625. wpa_s->p2p_auto_started.usec);
  4626. }
  4627. wpa_s->user_initiated_pd = 1;
  4628. if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
  4629. auto_join, freq,
  4630. group_ssid, group_ssid_len) < 0)
  4631. return -1;
  4632. return ret;
  4633. }
  4634. size = P2P_MAX_PREF_CHANNELS;
  4635. res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
  4636. go_intent == 15, pref_freq_list, &size);
  4637. if (res)
  4638. return res;
  4639. wpas_p2p_set_own_freq_preference(wpa_s,
  4640. force_freq ? force_freq : pref_freq);
  4641. p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
  4642. wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
  4643. if (wpa_s->create_p2p_iface) {
  4644. /* Prepare to add a new interface for the group */
  4645. iftype = WPA_IF_P2P_GROUP;
  4646. if (go_intent == 15)
  4647. iftype = WPA_IF_P2P_GO;
  4648. if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
  4649. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  4650. "interface for the group");
  4651. return -1;
  4652. }
  4653. if_addr = wpa_s->pending_interface_addr;
  4654. } else {
  4655. if (wpa_s->p2p_mgmt)
  4656. if_addr = wpa_s->parent->own_addr;
  4657. else
  4658. if_addr = wpa_s->own_addr;
  4659. os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
  4660. }
  4661. if (auth) {
  4662. if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
  4663. go_intent, if_addr,
  4664. force_freq, persistent_group, ssid,
  4665. pref_freq) < 0)
  4666. return -1;
  4667. return ret;
  4668. }
  4669. if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
  4670. go_intent, if_addr, force_freq,
  4671. persistent_group, ssid, pref_freq) < 0) {
  4672. if (wpa_s->create_p2p_iface)
  4673. wpas_p2p_remove_pending_group_interface(wpa_s);
  4674. return -1;
  4675. }
  4676. return ret;
  4677. }
  4678. /**
  4679. * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
  4680. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  4681. * @freq: Frequency of the channel in MHz
  4682. * @duration: Duration of the stay on the channel in milliseconds
  4683. *
  4684. * This callback is called when the driver indicates that it has started the
  4685. * requested remain-on-channel duration.
  4686. */
  4687. void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  4688. unsigned int freq, unsigned int duration)
  4689. {
  4690. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4691. return;
  4692. wpa_printf(MSG_DEBUG, "P2P: remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d roc_waiting_drv_freq=%d freq=%u duration=%u)",
  4693. wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
  4694. wpa_s->roc_waiting_drv_freq, freq, duration);
  4695. if (wpa_s->off_channel_freq &&
  4696. wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
  4697. p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
  4698. wpa_s->pending_listen_duration);
  4699. wpa_s->pending_listen_freq = 0;
  4700. } else {
  4701. wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
  4702. wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
  4703. freq, duration);
  4704. }
  4705. }
  4706. int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
  4707. {
  4708. /* Limit maximum Listen state time based on driver limitation. */
  4709. if (timeout > wpa_s->max_remain_on_chan)
  4710. timeout = wpa_s->max_remain_on_chan;
  4711. return p2p_listen(wpa_s->global->p2p, timeout);
  4712. }
  4713. /**
  4714. * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
  4715. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  4716. * @freq: Frequency of the channel in MHz
  4717. *
  4718. * This callback is called when the driver indicates that a remain-on-channel
  4719. * operation has been completed, i.e., the duration on the requested channel
  4720. * has timed out.
  4721. */
  4722. void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  4723. unsigned int freq)
  4724. {
  4725. wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
  4726. "(p2p_long_listen=%d ms pending_action_tx=%p)",
  4727. wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
  4728. wpas_p2p_listen_work_done(wpa_s);
  4729. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4730. return;
  4731. if (wpa_s->p2p_long_listen > 0)
  4732. wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
  4733. if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
  4734. return; /* P2P module started a new operation */
  4735. if (offchannel_pending_action_tx(wpa_s))
  4736. return;
  4737. if (wpa_s->p2p_long_listen > 0) {
  4738. wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
  4739. wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
  4740. } else {
  4741. /*
  4742. * When listen duration is over, stop listen & update p2p_state
  4743. * to IDLE.
  4744. */
  4745. p2p_stop_listen(wpa_s->global->p2p);
  4746. }
  4747. }
  4748. /**
  4749. * wpas_p2p_group_remove - Remove a P2P group
  4750. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  4751. * @ifname: Network interface name of the group interface or "*" to remove all
  4752. * groups
  4753. * Returns: 0 on success, -1 on failure
  4754. *
  4755. * This function is used to remove a P2P group. This can be used to disconnect
  4756. * from a group in which the local end is a P2P Client or to end a P2P Group in
  4757. * case the local end is the Group Owner. If a virtual network interface was
  4758. * created for this group, that interface will be removed. Otherwise, only the
  4759. * configured P2P group network will be removed from the interface.
  4760. */
  4761. int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
  4762. {
  4763. struct wpa_global *global = wpa_s->global;
  4764. struct wpa_supplicant *calling_wpa_s = wpa_s;
  4765. if (os_strcmp(ifname, "*") == 0) {
  4766. struct wpa_supplicant *prev;
  4767. wpa_s = global->ifaces;
  4768. while (wpa_s) {
  4769. prev = wpa_s;
  4770. wpa_s = wpa_s->next;
  4771. if (prev->p2p_group_interface !=
  4772. NOT_P2P_GROUP_INTERFACE ||
  4773. (prev->current_ssid &&
  4774. prev->current_ssid->p2p_group))
  4775. wpas_p2p_disconnect_safely(prev, calling_wpa_s);
  4776. }
  4777. return 0;
  4778. }
  4779. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  4780. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  4781. break;
  4782. }
  4783. return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
  4784. }
  4785. static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
  4786. {
  4787. unsigned int r;
  4788. if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
  4789. unsigned int i, size = P2P_MAX_PREF_CHANNELS;
  4790. unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS];
  4791. int res;
  4792. res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
  4793. &size, pref_freq_list);
  4794. if (!res && size > 0) {
  4795. i = 0;
  4796. while (wpas_p2p_disallowed_freq(wpa_s->global,
  4797. pref_freq_list[i]) &&
  4798. i < size) {
  4799. wpa_printf(MSG_DEBUG,
  4800. "P2P: preferred_freq_list[%d]=%d is disallowed",
  4801. i, pref_freq_list[i]);
  4802. i++;
  4803. }
  4804. if (i != size) {
  4805. freq = pref_freq_list[i];
  4806. wpa_printf(MSG_DEBUG,
  4807. "P2P: Using preferred_freq_list[%d]=%d",
  4808. i, freq);
  4809. } else {
  4810. wpa_printf(MSG_DEBUG,
  4811. "P2P: All driver preferred frequencies are disallowed for P2P use");
  4812. }
  4813. } else {
  4814. wpa_printf(MSG_DEBUG,
  4815. "P2P: No preferred frequency list available");
  4816. }
  4817. }
  4818. if (freq == 2) {
  4819. wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
  4820. "band");
  4821. if (wpa_s->best_24_freq > 0 &&
  4822. p2p_supported_freq_go(wpa_s->global->p2p,
  4823. wpa_s->best_24_freq)) {
  4824. freq = wpa_s->best_24_freq;
  4825. wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
  4826. "channel: %d MHz", freq);
  4827. } else {
  4828. if (os_get_random((u8 *) &r, sizeof(r)) < 0)
  4829. return -1;
  4830. freq = 2412 + (r % 3) * 25;
  4831. wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
  4832. "channel: %d MHz", freq);
  4833. }
  4834. }
  4835. if (freq == 5) {
  4836. wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
  4837. "band");
  4838. if (wpa_s->best_5_freq > 0 &&
  4839. p2p_supported_freq_go(wpa_s->global->p2p,
  4840. wpa_s->best_5_freq)) {
  4841. freq = wpa_s->best_5_freq;
  4842. wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
  4843. "channel: %d MHz", freq);
  4844. } else {
  4845. if (os_get_random((u8 *) &r, sizeof(r)) < 0)
  4846. return -1;
  4847. freq = 5180 + (r % 4) * 20;
  4848. if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
  4849. wpa_printf(MSG_DEBUG, "P2P: Could not select "
  4850. "5 GHz channel for P2P group");
  4851. return -1;
  4852. }
  4853. wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
  4854. "channel: %d MHz", freq);
  4855. }
  4856. }
  4857. if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
  4858. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
  4859. ieee80211_is_dfs(freq)) {
  4860. /*
  4861. * If freq is a DFS channel and DFS is offloaded to the
  4862. * driver, allow P2P GO to use it.
  4863. */
  4864. wpa_printf(MSG_DEBUG, "P2P: "
  4865. "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
  4866. __func__, freq);
  4867. return freq;
  4868. }
  4869. wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
  4870. "(%u MHz) is not supported for P2P uses",
  4871. freq);
  4872. return -1;
  4873. }
  4874. return freq;
  4875. }
  4876. static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
  4877. const struct p2p_channels *channels,
  4878. int freq)
  4879. {
  4880. if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
  4881. p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
  4882. freq_included(wpa_s, channels, freq))
  4883. return 1;
  4884. return 0;
  4885. }
  4886. static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
  4887. struct p2p_go_neg_results *params,
  4888. const struct p2p_channels *channels)
  4889. {
  4890. unsigned int i, r;
  4891. /* first try some random selection of the social channels */
  4892. if (os_get_random((u8 *) &r, sizeof(r)) < 0)
  4893. return;
  4894. for (i = 0; i < 3; i++) {
  4895. params->freq = 2412 + ((r + i) % 3) * 25;
  4896. if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
  4897. goto out;
  4898. }
  4899. /* try all other channels in operating class 81 */
  4900. for (i = 0; i < 11; i++) {
  4901. params->freq = 2412 + i * 5;
  4902. /* skip social channels; covered in the previous loop */
  4903. if (params->freq == 2412 ||
  4904. params->freq == 2437 ||
  4905. params->freq == 2462)
  4906. continue;
  4907. if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
  4908. goto out;
  4909. }
  4910. /* try all channels in operating class 115 */
  4911. for (i = 0; i < 4; i++) {
  4912. params->freq = 5180 + i * 20;
  4913. if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
  4914. freq_included(wpa_s, channels, params->freq) &&
  4915. p2p_supported_freq(wpa_s->global->p2p, params->freq))
  4916. goto out;
  4917. }
  4918. /* try all channels in operating class 124 */
  4919. for (i = 0; i < 4; i++) {
  4920. params->freq = 5745 + i * 20;
  4921. if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
  4922. freq_included(wpa_s, channels, params->freq) &&
  4923. p2p_supported_freq(wpa_s->global->p2p, params->freq))
  4924. goto out;
  4925. }
  4926. /* try social channel class 180 channel 2 */
  4927. params->freq = 58320 + 1 * 2160;
  4928. if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
  4929. freq_included(wpa_s, channels, params->freq) &&
  4930. p2p_supported_freq(wpa_s->global->p2p, params->freq))
  4931. goto out;
  4932. /* try all channels in reg. class 180 */
  4933. for (i = 0; i < 4; i++) {
  4934. params->freq = 58320 + i * 2160;
  4935. if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
  4936. freq_included(wpa_s, channels, params->freq) &&
  4937. p2p_supported_freq(wpa_s->global->p2p, params->freq))
  4938. goto out;
  4939. }
  4940. params->freq = 0;
  4941. wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
  4942. return;
  4943. out:
  4944. wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
  4945. params->freq);
  4946. }
  4947. static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
  4948. struct p2p_go_neg_results *params,
  4949. int freq, int vht_center_freq2, int ht40,
  4950. int vht, int max_oper_chwidth,
  4951. const struct p2p_channels *channels)
  4952. {
  4953. struct wpa_used_freq_data *freqs;
  4954. unsigned int cand;
  4955. unsigned int num, i;
  4956. int ignore_no_freqs = 0;
  4957. os_memset(params, 0, sizeof(*params));
  4958. params->role_go = 1;
  4959. params->ht40 = ht40;
  4960. params->vht = vht;
  4961. params->max_oper_chwidth = max_oper_chwidth;
  4962. params->vht_center_freq2 = vht_center_freq2;
  4963. freqs = os_calloc(wpa_s->num_multichan_concurrent,
  4964. sizeof(struct wpa_used_freq_data));
  4965. if (!freqs)
  4966. return -1;
  4967. num = get_shared_radio_freqs_data(wpa_s, freqs,
  4968. wpa_s->num_multichan_concurrent);
  4969. if (wpa_s->current_ssid &&
  4970. wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
  4971. wpa_s->wpa_state == WPA_COMPLETED) {
  4972. wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
  4973. __func__);
  4974. /*
  4975. * If the frequency selection is done for an active P2P GO that
  4976. * is not sharing a frequency, allow to select a new frequency
  4977. * even if there are no unused frequencies as we are about to
  4978. * move the P2P GO so its frequency can be re-used.
  4979. */
  4980. for (i = 0; i < num; i++) {
  4981. if (freqs[i].freq == wpa_s->current_ssid->frequency &&
  4982. freqs[i].flags == 0) {
  4983. ignore_no_freqs = 1;
  4984. break;
  4985. }
  4986. }
  4987. }
  4988. /* try using the forced freq */
  4989. if (freq) {
  4990. if (!wpas_p2p_supported_freq_go(wpa_s, channels, freq)) {
  4991. wpa_printf(MSG_DEBUG,
  4992. "P2P: Forced GO freq %d MHz not accepted",
  4993. freq);
  4994. goto fail;
  4995. }
  4996. for (i = 0; i < num; i++) {
  4997. if (freqs[i].freq == freq) {
  4998. wpa_printf(MSG_DEBUG,
  4999. "P2P: forced freq (%d MHz) is also shared",
  5000. freq);
  5001. params->freq = freq;
  5002. goto success;
  5003. }
  5004. }
  5005. if (!ignore_no_freqs &&
  5006. wpas_p2p_num_unused_channels(wpa_s) <= 0) {
  5007. wpa_printf(MSG_DEBUG,
  5008. "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
  5009. freq);
  5010. goto fail;
  5011. }
  5012. wpa_printf(MSG_DEBUG,
  5013. "P2P: force GO freq (%d MHz) on a free channel",
  5014. freq);
  5015. params->freq = freq;
  5016. goto success;
  5017. }
  5018. /* consider using one of the shared frequencies */
  5019. if (num) {
  5020. cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
  5021. if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
  5022. wpa_printf(MSG_DEBUG,
  5023. "P2P: Use shared freq (%d MHz) for GO",
  5024. freq);
  5025. params->freq = cand;
  5026. goto success;
  5027. }
  5028. /* try using one of the shared freqs */
  5029. for (i = 0; i < num; i++) {
  5030. if (wpas_p2p_supported_freq_go(wpa_s, channels,
  5031. freqs[i].freq)) {
  5032. wpa_printf(MSG_DEBUG,
  5033. "P2P: Use shared freq (%d MHz) for GO",
  5034. freq);
  5035. params->freq = freqs[i].freq;
  5036. goto success;
  5037. }
  5038. }
  5039. }
  5040. if (!ignore_no_freqs &&
  5041. wpas_p2p_num_unused_channels(wpa_s) <= 0) {
  5042. wpa_printf(MSG_DEBUG,
  5043. "P2P: Cannot force GO on any of the channels we are already using");
  5044. goto fail;
  5045. }
  5046. /* try using the setting from the configuration file */
  5047. if (wpa_s->conf->p2p_oper_reg_class == 81 &&
  5048. wpa_s->conf->p2p_oper_channel >= 1 &&
  5049. wpa_s->conf->p2p_oper_channel <= 11 &&
  5050. wpas_p2p_supported_freq_go(
  5051. wpa_s, channels,
  5052. 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
  5053. params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
  5054. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
  5055. "frequency %d MHz", params->freq);
  5056. goto success;
  5057. }
  5058. if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
  5059. wpa_s->conf->p2p_oper_reg_class == 116 ||
  5060. wpa_s->conf->p2p_oper_reg_class == 117 ||
  5061. wpa_s->conf->p2p_oper_reg_class == 124 ||
  5062. wpa_s->conf->p2p_oper_reg_class == 125 ||
  5063. wpa_s->conf->p2p_oper_reg_class == 126 ||
  5064. wpa_s->conf->p2p_oper_reg_class == 127) &&
  5065. wpas_p2p_supported_freq_go(wpa_s, channels,
  5066. 5000 +
  5067. 5 * wpa_s->conf->p2p_oper_channel)) {
  5068. params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
  5069. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
  5070. "frequency %d MHz", params->freq);
  5071. goto success;
  5072. }
  5073. /* Try using best channels */
  5074. if (wpa_s->conf->p2p_oper_channel == 0 &&
  5075. wpa_s->best_overall_freq > 0 &&
  5076. wpas_p2p_supported_freq_go(wpa_s, channels,
  5077. wpa_s->best_overall_freq)) {
  5078. params->freq = wpa_s->best_overall_freq;
  5079. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
  5080. "channel %d MHz", params->freq);
  5081. goto success;
  5082. }
  5083. if (wpa_s->conf->p2p_oper_channel == 0 &&
  5084. wpa_s->best_24_freq > 0 &&
  5085. wpas_p2p_supported_freq_go(wpa_s, channels,
  5086. wpa_s->best_24_freq)) {
  5087. params->freq = wpa_s->best_24_freq;
  5088. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
  5089. "channel %d MHz", params->freq);
  5090. goto success;
  5091. }
  5092. if (wpa_s->conf->p2p_oper_channel == 0 &&
  5093. wpa_s->best_5_freq > 0 &&
  5094. wpas_p2p_supported_freq_go(wpa_s, channels,
  5095. wpa_s->best_5_freq)) {
  5096. params->freq = wpa_s->best_5_freq;
  5097. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
  5098. "channel %d MHz", params->freq);
  5099. goto success;
  5100. }
  5101. /* try using preferred channels */
  5102. cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
  5103. if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
  5104. params->freq = cand;
  5105. wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
  5106. "channels", params->freq);
  5107. goto success;
  5108. }
  5109. /* Try using one of the group common freqs */
  5110. if (wpa_s->p2p_group_common_freqs) {
  5111. for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
  5112. cand = wpa_s->p2p_group_common_freqs[i];
  5113. if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
  5114. params->freq = cand;
  5115. wpa_printf(MSG_DEBUG,
  5116. "P2P: Use freq %d MHz common with the peer",
  5117. params->freq);
  5118. goto success;
  5119. }
  5120. }
  5121. }
  5122. /* no preference, select some channel */
  5123. wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
  5124. if (params->freq == 0) {
  5125. wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
  5126. goto fail;
  5127. }
  5128. success:
  5129. os_free(freqs);
  5130. return 0;
  5131. fail:
  5132. os_free(freqs);
  5133. return -1;
  5134. }
  5135. static struct wpa_supplicant *
  5136. wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
  5137. int go)
  5138. {
  5139. struct wpa_supplicant *group_wpa_s;
  5140. if (!wpas_p2p_create_iface(wpa_s)) {
  5141. if (wpa_s->p2p_mgmt) {
  5142. /*
  5143. * We may be called on the p2p_dev interface which
  5144. * cannot be used for group operations, so always use
  5145. * the primary interface.
  5146. */
  5147. wpa_s->parent->p2pdev = wpa_s;
  5148. wpa_s = wpa_s->parent;
  5149. }
  5150. wpa_dbg(wpa_s, MSG_DEBUG,
  5151. "P2P: Use primary interface for group operations");
  5152. wpa_s->p2p_first_connection_timeout = 0;
  5153. if (wpa_s != wpa_s->p2pdev)
  5154. wpas_p2p_clone_config_dh(wpa_s, wpa_s->p2pdev);
  5155. return wpa_s;
  5156. }
  5157. if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
  5158. WPA_IF_P2P_CLIENT) < 0) {
  5159. wpa_msg_global(wpa_s, MSG_ERROR,
  5160. "P2P: Failed to add group interface");
  5161. return NULL;
  5162. }
  5163. group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
  5164. if (group_wpa_s == NULL) {
  5165. wpa_msg_global(wpa_s, MSG_ERROR,
  5166. "P2P: Failed to initialize group interface");
  5167. wpas_p2p_remove_pending_group_interface(wpa_s);
  5168. return NULL;
  5169. }
  5170. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
  5171. group_wpa_s->ifname);
  5172. group_wpa_s->p2p_first_connection_timeout = 0;
  5173. return group_wpa_s;
  5174. }
  5175. /**
  5176. * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
  5177. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  5178. * @persistent_group: Whether to create a persistent group
  5179. * @freq: Frequency for the group or 0 to indicate no hardcoding
  5180. * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
  5181. * @ht40: Start GO with 40 MHz channel width
  5182. * @vht: Start GO with VHT support
  5183. * @vht_chwidth: channel bandwidth for GO operating with VHT support
  5184. * Returns: 0 on success, -1 on failure
  5185. *
  5186. * This function creates a new P2P group with the local end as the Group Owner,
  5187. * i.e., without using Group Owner Negotiation.
  5188. */
  5189. int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
  5190. int freq, int vht_center_freq2, int ht40, int vht,
  5191. int max_oper_chwidth)
  5192. {
  5193. struct p2p_go_neg_results params;
  5194. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5195. return -1;
  5196. os_free(wpa_s->global->add_psk);
  5197. wpa_s->global->add_psk = NULL;
  5198. /* Make sure we are not running find during connection establishment */
  5199. wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
  5200. wpas_p2p_stop_find_oper(wpa_s);
  5201. freq = wpas_p2p_select_go_freq(wpa_s, freq);
  5202. if (freq < 0)
  5203. return -1;
  5204. if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
  5205. ht40, vht, max_oper_chwidth, NULL))
  5206. return -1;
  5207. if (params.freq &&
  5208. !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
  5209. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
  5210. ieee80211_is_dfs(params.freq)) {
  5211. /*
  5212. * If freq is a DFS channel and DFS is offloaded to the
  5213. * driver, allow P2P GO to use it.
  5214. */
  5215. wpa_printf(MSG_DEBUG,
  5216. "P2P: %s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to driver",
  5217. __func__, params.freq);
  5218. } else {
  5219. wpa_printf(MSG_DEBUG,
  5220. "P2P: The selected channel for GO (%u MHz) is not supported for P2P uses",
  5221. params.freq);
  5222. return -1;
  5223. }
  5224. }
  5225. p2p_go_params(wpa_s->global->p2p, &params);
  5226. params.persistent_group = persistent_group;
  5227. wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
  5228. if (wpa_s == NULL)
  5229. return -1;
  5230. wpas_start_wps_go(wpa_s, &params, 0);
  5231. return 0;
  5232. }
  5233. static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
  5234. struct wpa_ssid *params, int addr_allocated,
  5235. int freq, int force_scan)
  5236. {
  5237. struct wpa_ssid *ssid;
  5238. wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
  5239. if (wpa_s == NULL)
  5240. return -1;
  5241. if (force_scan)
  5242. os_get_reltime(&wpa_s->scan_min_time);
  5243. wpa_s->p2p_last_4way_hs_fail = NULL;
  5244. wpa_supplicant_ap_deinit(wpa_s);
  5245. ssid = wpa_config_add_network(wpa_s->conf);
  5246. if (ssid == NULL)
  5247. return -1;
  5248. os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
  5249. wpa_config_set_network_defaults(ssid);
  5250. ssid->temporary = 1;
  5251. ssid->proto = WPA_PROTO_RSN;
  5252. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  5253. ssid->group_cipher = WPA_CIPHER_CCMP;
  5254. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  5255. ssid->ssid = os_malloc(params->ssid_len);
  5256. if (ssid->ssid == NULL) {
  5257. wpa_config_remove_network(wpa_s->conf, ssid->id);
  5258. return -1;
  5259. }
  5260. os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
  5261. ssid->ssid_len = params->ssid_len;
  5262. ssid->p2p_group = 1;
  5263. ssid->export_keys = 1;
  5264. if (params->psk_set) {
  5265. os_memcpy(ssid->psk, params->psk, 32);
  5266. ssid->psk_set = 1;
  5267. }
  5268. if (params->passphrase)
  5269. ssid->passphrase = os_strdup(params->passphrase);
  5270. wpa_s->show_group_started = 1;
  5271. wpa_s->p2p_in_invitation = 1;
  5272. wpa_s->p2p_invite_go_freq = freq;
  5273. wpa_s->p2p_go_group_formation_completed = 0;
  5274. wpa_s->global->p2p_group_formation = wpa_s;
  5275. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
  5276. NULL);
  5277. eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
  5278. wpas_p2p_group_formation_timeout,
  5279. wpa_s->p2pdev, NULL);
  5280. wpa_supplicant_select_network(wpa_s, ssid);
  5281. return 0;
  5282. }
  5283. int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
  5284. struct wpa_ssid *ssid, int addr_allocated,
  5285. int force_freq, int neg_freq,
  5286. int vht_center_freq2, int ht40,
  5287. int vht, int max_oper_chwidth,
  5288. const struct p2p_channels *channels,
  5289. int connection_timeout, int force_scan)
  5290. {
  5291. struct p2p_go_neg_results params;
  5292. int go = 0, freq;
  5293. if (ssid->disabled != 2 || ssid->ssid == NULL)
  5294. return -1;
  5295. if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
  5296. go == (ssid->mode == WPAS_MODE_P2P_GO)) {
  5297. wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
  5298. "already running");
  5299. if (go == 0 &&
  5300. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  5301. wpa_s->p2pdev, NULL)) {
  5302. /*
  5303. * This can happen if Invitation Response frame was lost
  5304. * and the peer (GO of a persistent group) tries to
  5305. * invite us again. Reschedule the timeout to avoid
  5306. * terminating the wait for the connection too early
  5307. * since we now know that the peer is still trying to
  5308. * invite us instead of having already started the GO.
  5309. */
  5310. wpa_printf(MSG_DEBUG,
  5311. "P2P: Reschedule group formation timeout since peer is still trying to invite us");
  5312. eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
  5313. wpas_p2p_group_formation_timeout,
  5314. wpa_s->p2pdev, NULL);
  5315. }
  5316. return 0;
  5317. }
  5318. os_free(wpa_s->global->add_psk);
  5319. wpa_s->global->add_psk = NULL;
  5320. /* Make sure we are not running find during connection establishment */
  5321. wpas_p2p_stop_find_oper(wpa_s);
  5322. wpa_s->p2p_fallback_to_go_neg = 0;
  5323. if (ssid->mode == WPAS_MODE_P2P_GO) {
  5324. if (force_freq > 0) {
  5325. freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
  5326. if (freq < 0)
  5327. return -1;
  5328. } else {
  5329. freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
  5330. if (freq < 0 ||
  5331. (freq > 0 && !freq_included(wpa_s, channels, freq)))
  5332. freq = 0;
  5333. }
  5334. } else if (ssid->mode == WPAS_MODE_INFRA) {
  5335. freq = neg_freq;
  5336. if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
  5337. struct os_reltime now;
  5338. struct wpa_bss *bss =
  5339. wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
  5340. os_get_reltime(&now);
  5341. if (bss &&
  5342. !os_reltime_expired(&now, &bss->last_update, 5) &&
  5343. freq_included(wpa_s, channels, bss->freq))
  5344. freq = bss->freq;
  5345. else
  5346. freq = 0;
  5347. }
  5348. return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
  5349. force_scan);
  5350. } else {
  5351. return -1;
  5352. }
  5353. if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
  5354. ht40, vht, max_oper_chwidth, channels))
  5355. return -1;
  5356. params.role_go = 1;
  5357. params.psk_set = ssid->psk_set;
  5358. if (params.psk_set)
  5359. os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
  5360. if (ssid->passphrase) {
  5361. if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
  5362. wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
  5363. "persistent group");
  5364. return -1;
  5365. }
  5366. os_strlcpy(params.passphrase, ssid->passphrase,
  5367. sizeof(params.passphrase));
  5368. }
  5369. os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
  5370. params.ssid_len = ssid->ssid_len;
  5371. params.persistent_group = 1;
  5372. wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
  5373. if (wpa_s == NULL)
  5374. return -1;
  5375. p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
  5376. wpa_s->p2p_first_connection_timeout = connection_timeout;
  5377. wpas_start_wps_go(wpa_s, &params, 0);
  5378. return 0;
  5379. }
  5380. static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
  5381. struct wpabuf *proberesp_ies)
  5382. {
  5383. struct wpa_supplicant *wpa_s = ctx;
  5384. if (wpa_s->ap_iface) {
  5385. struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
  5386. if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
  5387. wpabuf_free(beacon_ies);
  5388. wpabuf_free(proberesp_ies);
  5389. return;
  5390. }
  5391. if (beacon_ies) {
  5392. wpabuf_free(hapd->p2p_beacon_ie);
  5393. hapd->p2p_beacon_ie = beacon_ies;
  5394. }
  5395. wpabuf_free(hapd->p2p_probe_resp_ie);
  5396. hapd->p2p_probe_resp_ie = proberesp_ies;
  5397. } else {
  5398. wpabuf_free(beacon_ies);
  5399. wpabuf_free(proberesp_ies);
  5400. }
  5401. wpa_supplicant_ap_update_beacon(wpa_s);
  5402. }
  5403. static void wpas_p2p_idle_update(void *ctx, int idle)
  5404. {
  5405. struct wpa_supplicant *wpa_s = ctx;
  5406. if (!wpa_s->ap_iface)
  5407. return;
  5408. wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
  5409. if (idle) {
  5410. if (wpa_s->global->p2p_fail_on_wps_complete &&
  5411. wpa_s->p2p_in_provisioning) {
  5412. wpas_p2p_grpform_fail_after_wps(wpa_s);
  5413. return;
  5414. }
  5415. wpas_p2p_set_group_idle_timeout(wpa_s);
  5416. } else
  5417. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  5418. }
  5419. struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
  5420. struct wpa_ssid *ssid)
  5421. {
  5422. struct p2p_group *group;
  5423. struct p2p_group_config *cfg;
  5424. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5425. return NULL;
  5426. cfg = os_zalloc(sizeof(*cfg));
  5427. if (cfg == NULL)
  5428. return NULL;
  5429. if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
  5430. cfg->persistent_group = 2;
  5431. else if (ssid->p2p_persistent_group)
  5432. cfg->persistent_group = 1;
  5433. os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
  5434. if (wpa_s->max_stations &&
  5435. wpa_s->max_stations < wpa_s->conf->max_num_sta)
  5436. cfg->max_clients = wpa_s->max_stations;
  5437. else
  5438. cfg->max_clients = wpa_s->conf->max_num_sta;
  5439. os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
  5440. cfg->ssid_len = ssid->ssid_len;
  5441. cfg->freq = ssid->frequency;
  5442. cfg->cb_ctx = wpa_s;
  5443. cfg->ie_update = wpas_p2p_ie_update;
  5444. cfg->idle_update = wpas_p2p_idle_update;
  5445. group = p2p_group_init(wpa_s->global->p2p, cfg);
  5446. if (group == NULL)
  5447. os_free(cfg);
  5448. if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
  5449. p2p_group_notif_formation_done(group);
  5450. wpa_s->p2p_group = group;
  5451. return group;
  5452. }
  5453. void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  5454. int registrar)
  5455. {
  5456. struct wpa_ssid *ssid = wpa_s->current_ssid;
  5457. if (!wpa_s->p2p_in_provisioning) {
  5458. wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
  5459. "provisioning not in progress");
  5460. return;
  5461. }
  5462. if (ssid && ssid->mode == WPAS_MODE_INFRA) {
  5463. u8 go_dev_addr[ETH_ALEN];
  5464. os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
  5465. wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
  5466. ssid->ssid_len);
  5467. /* Clear any stored provisioning info */
  5468. p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
  5469. }
  5470. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
  5471. NULL);
  5472. wpa_s->p2p_go_group_formation_completed = 1;
  5473. if (ssid && ssid->mode == WPAS_MODE_INFRA) {
  5474. /*
  5475. * Use a separate timeout for initial data connection to
  5476. * complete to allow the group to be removed automatically if
  5477. * something goes wrong in this step before the P2P group idle
  5478. * timeout mechanism is taken into use.
  5479. */
  5480. wpa_dbg(wpa_s, MSG_DEBUG,
  5481. "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
  5482. P2P_MAX_INITIAL_CONN_WAIT);
  5483. eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
  5484. wpas_p2p_group_formation_timeout,
  5485. wpa_s->p2pdev, NULL);
  5486. /* Complete group formation on successful data connection. */
  5487. wpa_s->p2p_go_group_formation_completed = 0;
  5488. } else if (ssid) {
  5489. /*
  5490. * Use a separate timeout for initial data connection to
  5491. * complete to allow the group to be removed automatically if
  5492. * the client does not complete data connection successfully.
  5493. */
  5494. wpa_dbg(wpa_s, MSG_DEBUG,
  5495. "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
  5496. P2P_MAX_INITIAL_CONN_WAIT_GO);
  5497. eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
  5498. wpas_p2p_group_formation_timeout,
  5499. wpa_s->p2pdev, NULL);
  5500. /*
  5501. * Complete group formation on first successful data connection
  5502. */
  5503. wpa_s->p2p_go_group_formation_completed = 0;
  5504. }
  5505. if (wpa_s->global->p2p)
  5506. p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
  5507. wpas_group_formation_completed(wpa_s, 1, 0);
  5508. }
  5509. void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
  5510. struct wps_event_fail *fail)
  5511. {
  5512. if (!wpa_s->p2p_in_provisioning) {
  5513. wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
  5514. "provisioning not in progress");
  5515. return;
  5516. }
  5517. if (wpa_s->go_params) {
  5518. p2p_clear_provisioning_info(
  5519. wpa_s->global->p2p,
  5520. wpa_s->go_params->peer_device_addr);
  5521. }
  5522. wpas_notify_p2p_wps_failed(wpa_s, fail);
  5523. if (wpa_s == wpa_s->global->p2p_group_formation) {
  5524. /*
  5525. * Allow some time for the failed WPS negotiation exchange to
  5526. * complete, but remove the group since group formation cannot
  5527. * succeed after provisioning failure.
  5528. */
  5529. wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
  5530. wpa_s->global->p2p_fail_on_wps_complete = 1;
  5531. eloop_deplete_timeout(0, 50000,
  5532. wpas_p2p_group_formation_timeout,
  5533. wpa_s->p2pdev, NULL);
  5534. }
  5535. }
  5536. int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
  5537. {
  5538. if (!wpa_s->global->p2p_fail_on_wps_complete ||
  5539. !wpa_s->p2p_in_provisioning)
  5540. return 0;
  5541. wpas_p2p_grpform_fail_after_wps(wpa_s);
  5542. return 1;
  5543. }
  5544. int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  5545. const char *config_method,
  5546. enum wpas_p2p_prov_disc_use use,
  5547. struct p2ps_provision *p2ps_prov)
  5548. {
  5549. u16 config_methods;
  5550. wpa_s->global->pending_p2ps_group = 0;
  5551. wpa_s->global->pending_p2ps_group_freq = 0;
  5552. wpa_s->p2p_fallback_to_go_neg = 0;
  5553. wpa_s->pending_pd_use = NORMAL_PD;
  5554. if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
  5555. p2ps_prov->conncap = p2ps_group_capability(
  5556. wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
  5557. &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
  5558. wpa_printf(MSG_DEBUG,
  5559. "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
  5560. __func__, p2ps_prov->conncap,
  5561. p2ps_prov->adv_id, p2ps_prov->conncap,
  5562. p2ps_prov->status, p2ps_prov->info);
  5563. config_methods = 0;
  5564. } else if (os_strncmp(config_method, "display", 7) == 0)
  5565. config_methods = WPS_CONFIG_DISPLAY;
  5566. else if (os_strncmp(config_method, "keypad", 6) == 0)
  5567. config_methods = WPS_CONFIG_KEYPAD;
  5568. else if (os_strncmp(config_method, "pbc", 3) == 0 ||
  5569. os_strncmp(config_method, "pushbutton", 10) == 0)
  5570. config_methods = WPS_CONFIG_PUSHBUTTON;
  5571. else {
  5572. wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
  5573. os_free(p2ps_prov);
  5574. return -1;
  5575. }
  5576. if (use == WPAS_P2P_PD_AUTO) {
  5577. os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
  5578. wpa_s->pending_pd_config_methods = config_methods;
  5579. wpa_s->p2p_auto_pd = 1;
  5580. wpa_s->p2p_auto_join = 0;
  5581. wpa_s->pending_pd_before_join = 0;
  5582. wpa_s->auto_pd_scan_retry = 0;
  5583. wpas_p2p_stop_find(wpa_s);
  5584. wpa_s->p2p_join_scan_count = 0;
  5585. os_get_reltime(&wpa_s->p2p_auto_started);
  5586. wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
  5587. wpa_s->p2p_auto_started.sec,
  5588. wpa_s->p2p_auto_started.usec);
  5589. wpas_p2p_join_scan(wpa_s, NULL);
  5590. return 0;
  5591. }
  5592. if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
  5593. os_free(p2ps_prov);
  5594. return -1;
  5595. }
  5596. return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
  5597. config_methods, use == WPAS_P2P_PD_FOR_JOIN,
  5598. 0, 1);
  5599. }
  5600. int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
  5601. char *end)
  5602. {
  5603. return p2p_scan_result_text(ies, ies_len, buf, end);
  5604. }
  5605. static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
  5606. {
  5607. if (!offchannel_pending_action_tx(wpa_s))
  5608. return;
  5609. if (wpa_s->p2p_send_action_work) {
  5610. wpas_p2p_free_send_action_work(wpa_s);
  5611. eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
  5612. wpa_s, NULL);
  5613. offchannel_send_action_done(wpa_s);
  5614. }
  5615. wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
  5616. "operation request");
  5617. offchannel_clear_pending_action_tx(wpa_s);
  5618. }
  5619. int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
  5620. enum p2p_discovery_type type,
  5621. unsigned int num_req_dev_types, const u8 *req_dev_types,
  5622. const u8 *dev_id, unsigned int search_delay,
  5623. u8 seek_cnt, const char **seek_string, int freq)
  5624. {
  5625. wpas_p2p_clear_pending_action_tx(wpa_s);
  5626. wpa_s->p2p_long_listen = 0;
  5627. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
  5628. wpa_s->p2p_in_provisioning)
  5629. return -1;
  5630. wpa_supplicant_cancel_sched_scan(wpa_s);
  5631. return p2p_find(wpa_s->global->p2p, timeout, type,
  5632. num_req_dev_types, req_dev_types, dev_id,
  5633. search_delay, seek_cnt, seek_string, freq);
  5634. }
  5635. static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
  5636. struct wpa_scan_results *scan_res)
  5637. {
  5638. wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
  5639. if (wpa_s->p2p_scan_work) {
  5640. struct wpa_radio_work *work = wpa_s->p2p_scan_work;
  5641. wpa_s->p2p_scan_work = NULL;
  5642. radio_work_done(work);
  5643. }
  5644. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5645. return;
  5646. /*
  5647. * Indicate that results have been processed so that the P2P module can
  5648. * continue pending tasks.
  5649. */
  5650. p2p_scan_res_handled(wpa_s->global->p2p);
  5651. }
  5652. static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
  5653. {
  5654. wpas_p2p_clear_pending_action_tx(wpa_s);
  5655. wpa_s->p2p_long_listen = 0;
  5656. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  5657. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  5658. if (wpa_s->global->p2p)
  5659. p2p_stop_find(wpa_s->global->p2p);
  5660. if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
  5661. wpa_printf(MSG_DEBUG,
  5662. "P2P: Do not consider the scan results after stop_find");
  5663. wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
  5664. }
  5665. }
  5666. void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
  5667. {
  5668. wpas_p2p_stop_find_oper(wpa_s);
  5669. if (!wpa_s->global->pending_group_iface_for_p2ps)
  5670. wpas_p2p_remove_pending_group_interface(wpa_s);
  5671. }
  5672. static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
  5673. {
  5674. struct wpa_supplicant *wpa_s = eloop_ctx;
  5675. wpa_s->p2p_long_listen = 0;
  5676. }
  5677. int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
  5678. {
  5679. int res;
  5680. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5681. return -1;
  5682. wpa_supplicant_cancel_sched_scan(wpa_s);
  5683. wpas_p2p_clear_pending_action_tx(wpa_s);
  5684. if (timeout == 0) {
  5685. /*
  5686. * This is a request for unlimited Listen state. However, at
  5687. * least for now, this is mapped to a Listen state for one
  5688. * hour.
  5689. */
  5690. timeout = 3600;
  5691. }
  5692. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  5693. wpa_s->p2p_long_listen = 0;
  5694. /*
  5695. * Stop previous find/listen operation to avoid trying to request a new
  5696. * remain-on-channel operation while the driver is still running the
  5697. * previous one.
  5698. */
  5699. if (wpa_s->global->p2p)
  5700. p2p_stop_find(wpa_s->global->p2p);
  5701. res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
  5702. if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
  5703. wpa_s->p2p_long_listen = timeout * 1000;
  5704. eloop_register_timeout(timeout, 0,
  5705. wpas_p2p_long_listen_timeout,
  5706. wpa_s, NULL);
  5707. }
  5708. return res;
  5709. }
  5710. int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  5711. u8 *buf, size_t len, int p2p_group)
  5712. {
  5713. struct wpabuf *p2p_ie;
  5714. int ret;
  5715. if (wpa_s->global->p2p_disabled)
  5716. return -1;
  5717. /*
  5718. * Advertize mandatory cross connection capability even on
  5719. * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
  5720. */
  5721. if (wpa_s->conf->p2p_disabled && p2p_group)
  5722. return -1;
  5723. if (wpa_s->global->p2p == NULL)
  5724. return -1;
  5725. if (bss == NULL)
  5726. return -1;
  5727. p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  5728. ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
  5729. p2p_group, p2p_ie);
  5730. wpabuf_free(p2p_ie);
  5731. return ret;
  5732. }
  5733. int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
  5734. const u8 *dst, const u8 *bssid,
  5735. const u8 *ie, size_t ie_len,
  5736. unsigned int rx_freq, int ssi_signal)
  5737. {
  5738. if (wpa_s->global->p2p_disabled)
  5739. return 0;
  5740. if (wpa_s->global->p2p == NULL)
  5741. return 0;
  5742. switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
  5743. ie, ie_len, rx_freq)) {
  5744. case P2P_PREQ_NOT_P2P:
  5745. wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
  5746. ssi_signal);
  5747. /* fall through */
  5748. case P2P_PREQ_MALFORMED:
  5749. case P2P_PREQ_NOT_LISTEN:
  5750. case P2P_PREQ_NOT_PROCESSED:
  5751. default: /* make gcc happy */
  5752. return 0;
  5753. case P2P_PREQ_PROCESSED:
  5754. return 1;
  5755. }
  5756. }
  5757. void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
  5758. const u8 *sa, const u8 *bssid,
  5759. u8 category, const u8 *data, size_t len, int freq)
  5760. {
  5761. if (wpa_s->global->p2p_disabled)
  5762. return;
  5763. if (wpa_s->global->p2p == NULL)
  5764. return;
  5765. p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
  5766. freq);
  5767. }
  5768. void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
  5769. {
  5770. unsigned int bands;
  5771. if (wpa_s->global->p2p_disabled)
  5772. return;
  5773. if (wpa_s->global->p2p == NULL)
  5774. return;
  5775. bands = wpas_get_bands(wpa_s, NULL);
  5776. p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
  5777. }
  5778. static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
  5779. {
  5780. p2p_group_deinit(wpa_s->p2p_group);
  5781. wpa_s->p2p_group = NULL;
  5782. wpa_s->ap_configured_cb = NULL;
  5783. wpa_s->ap_configured_cb_ctx = NULL;
  5784. wpa_s->ap_configured_cb_data = NULL;
  5785. wpa_s->connect_without_scan = NULL;
  5786. }
  5787. int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
  5788. {
  5789. wpa_s->p2p_long_listen = 0;
  5790. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5791. return -1;
  5792. return p2p_reject(wpa_s->global->p2p, addr);
  5793. }
  5794. /* Invite to reinvoke a persistent group */
  5795. int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  5796. struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
  5797. int vht_center_freq2, int ht40, int vht, int max_chwidth,
  5798. int pref_freq)
  5799. {
  5800. enum p2p_invite_role role;
  5801. u8 *bssid = NULL;
  5802. int force_freq = 0;
  5803. int res;
  5804. int no_pref_freq_given = pref_freq == 0;
  5805. unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
  5806. wpa_s->global->p2p_invite_group = NULL;
  5807. if (peer_addr)
  5808. os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
  5809. else
  5810. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  5811. wpa_s->p2p_persistent_go_freq = freq;
  5812. wpa_s->p2p_go_ht40 = !!ht40;
  5813. wpa_s->p2p_go_vht = !!vht;
  5814. wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
  5815. wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
  5816. if (ssid->mode == WPAS_MODE_P2P_GO) {
  5817. role = P2P_INVITE_ROLE_GO;
  5818. if (peer_addr == NULL) {
  5819. wpa_printf(MSG_DEBUG, "P2P: Missing peer "
  5820. "address in invitation command");
  5821. return -1;
  5822. }
  5823. if (wpas_p2p_create_iface(wpa_s)) {
  5824. if (wpas_p2p_add_group_interface(wpa_s,
  5825. WPA_IF_P2P_GO) < 0) {
  5826. wpa_printf(MSG_ERROR, "P2P: Failed to "
  5827. "allocate a new interface for the "
  5828. "group");
  5829. return -1;
  5830. }
  5831. bssid = wpa_s->pending_interface_addr;
  5832. } else if (wpa_s->p2p_mgmt)
  5833. bssid = wpa_s->parent->own_addr;
  5834. else
  5835. bssid = wpa_s->own_addr;
  5836. } else {
  5837. role = P2P_INVITE_ROLE_CLIENT;
  5838. peer_addr = ssid->bssid;
  5839. }
  5840. wpa_s->pending_invite_ssid_id = ssid->id;
  5841. size = P2P_MAX_PREF_CHANNELS;
  5842. res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
  5843. role == P2P_INVITE_ROLE_GO,
  5844. pref_freq_list, &size);
  5845. if (res)
  5846. return res;
  5847. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5848. return -1;
  5849. p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
  5850. if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
  5851. no_pref_freq_given && pref_freq > 0 &&
  5852. wpa_s->num_multichan_concurrent > 1 &&
  5853. wpas_p2p_num_unused_channels(wpa_s) > 0) {
  5854. wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
  5855. pref_freq);
  5856. pref_freq = 0;
  5857. }
  5858. /*
  5859. * Stop any find/listen operations before invitation and possibly
  5860. * connection establishment.
  5861. */
  5862. wpas_p2p_stop_find_oper(wpa_s);
  5863. return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
  5864. ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
  5865. 1, pref_freq, -1);
  5866. }
  5867. /* Invite to join an active group */
  5868. int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
  5869. const u8 *peer_addr, const u8 *go_dev_addr)
  5870. {
  5871. struct wpa_global *global = wpa_s->global;
  5872. enum p2p_invite_role role;
  5873. u8 *bssid = NULL;
  5874. struct wpa_ssid *ssid;
  5875. int persistent;
  5876. int freq = 0, force_freq = 0, pref_freq = 0;
  5877. int res;
  5878. unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
  5879. wpa_s->p2p_persistent_go_freq = 0;
  5880. wpa_s->p2p_go_ht40 = 0;
  5881. wpa_s->p2p_go_vht = 0;
  5882. wpa_s->p2p_go_vht_center_freq2 = 0;
  5883. wpa_s->p2p_go_max_oper_chwidth = 0;
  5884. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  5885. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  5886. break;
  5887. }
  5888. if (wpa_s == NULL) {
  5889. wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
  5890. return -1;
  5891. }
  5892. ssid = wpa_s->current_ssid;
  5893. if (ssid == NULL) {
  5894. wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
  5895. "invitation");
  5896. return -1;
  5897. }
  5898. wpa_s->global->p2p_invite_group = wpa_s;
  5899. persistent = ssid->p2p_persistent_group &&
  5900. wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
  5901. ssid->ssid, ssid->ssid_len);
  5902. if (ssid->mode == WPAS_MODE_P2P_GO) {
  5903. role = P2P_INVITE_ROLE_ACTIVE_GO;
  5904. bssid = wpa_s->own_addr;
  5905. if (go_dev_addr == NULL)
  5906. go_dev_addr = wpa_s->global->p2p_dev_addr;
  5907. freq = ssid->frequency;
  5908. } else {
  5909. role = P2P_INVITE_ROLE_CLIENT;
  5910. if (wpa_s->wpa_state < WPA_ASSOCIATED) {
  5911. wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
  5912. "invite to current group");
  5913. return -1;
  5914. }
  5915. bssid = wpa_s->bssid;
  5916. if (go_dev_addr == NULL &&
  5917. !is_zero_ether_addr(wpa_s->go_dev_addr))
  5918. go_dev_addr = wpa_s->go_dev_addr;
  5919. freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
  5920. (int) wpa_s->assoc_freq;
  5921. }
  5922. wpa_s->p2pdev->pending_invite_ssid_id = -1;
  5923. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5924. return -1;
  5925. size = P2P_MAX_PREF_CHANNELS;
  5926. res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
  5927. role == P2P_INVITE_ROLE_ACTIVE_GO,
  5928. pref_freq_list, &size);
  5929. if (res)
  5930. return res;
  5931. wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
  5932. return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
  5933. ssid->ssid, ssid->ssid_len, force_freq,
  5934. go_dev_addr, persistent, pref_freq, -1);
  5935. }
  5936. void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
  5937. {
  5938. struct wpa_ssid *ssid = wpa_s->current_ssid;
  5939. u8 go_dev_addr[ETH_ALEN];
  5940. int network_id = -1;
  5941. int persistent;
  5942. int freq;
  5943. u8 ip[3 * 4];
  5944. char ip_addr[100];
  5945. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
  5946. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  5947. wpa_s->p2pdev, NULL);
  5948. }
  5949. if (!wpa_s->show_group_started || !ssid)
  5950. return;
  5951. wpa_s->show_group_started = 0;
  5952. if (!wpa_s->p2p_go_group_formation_completed &&
  5953. wpa_s->global->p2p_group_formation == wpa_s) {
  5954. wpa_dbg(wpa_s, MSG_DEBUG,
  5955. "P2P: Marking group formation completed on client on data connection");
  5956. wpa_s->p2p_go_group_formation_completed = 1;
  5957. wpa_s->global->p2p_group_formation = NULL;
  5958. wpa_s->p2p_in_provisioning = 0;
  5959. wpa_s->p2p_in_invitation = 0;
  5960. }
  5961. os_memset(go_dev_addr, 0, ETH_ALEN);
  5962. if (ssid->bssid_set)
  5963. os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
  5964. persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
  5965. ssid->ssid_len);
  5966. os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
  5967. if (wpa_s->global->p2p_group_formation == wpa_s)
  5968. wpa_s->global->p2p_group_formation = NULL;
  5969. freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
  5970. (int) wpa_s->assoc_freq;
  5971. ip_addr[0] = '\0';
  5972. if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
  5973. int res;
  5974. res = os_snprintf(ip_addr, sizeof(ip_addr),
  5975. " ip_addr=%u.%u.%u.%u "
  5976. "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
  5977. ip[0], ip[1], ip[2], ip[3],
  5978. ip[4], ip[5], ip[6], ip[7],
  5979. ip[8], ip[9], ip[10], ip[11]);
  5980. if (os_snprintf_error(sizeof(ip_addr), res))
  5981. ip_addr[0] = '\0';
  5982. }
  5983. wpas_p2p_group_started(wpa_s, 0, ssid, freq,
  5984. ssid->passphrase == NULL && ssid->psk_set ?
  5985. ssid->psk : NULL,
  5986. ssid->passphrase, go_dev_addr, persistent,
  5987. ip_addr);
  5988. if (persistent)
  5989. network_id = wpas_p2p_store_persistent_group(wpa_s->p2pdev,
  5990. ssid, go_dev_addr);
  5991. if (network_id < 0)
  5992. network_id = ssid->id;
  5993. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
  5994. }
  5995. int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
  5996. u32 interval1, u32 duration2, u32 interval2)
  5997. {
  5998. int ret;
  5999. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  6000. return -1;
  6001. if (wpa_s->wpa_state < WPA_ASSOCIATED ||
  6002. wpa_s->current_ssid == NULL ||
  6003. wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
  6004. return -1;
  6005. ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
  6006. wpa_s->own_addr, wpa_s->assoc_freq,
  6007. duration1, interval1, duration2, interval2);
  6008. if (ret == 0)
  6009. wpa_s->waiting_presence_resp = 1;
  6010. return ret;
  6011. }
  6012. int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
  6013. unsigned int interval)
  6014. {
  6015. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  6016. return -1;
  6017. return p2p_ext_listen(wpa_s->global->p2p, period, interval);
  6018. }
  6019. static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
  6020. {
  6021. if (wpa_s->current_ssid == NULL) {
  6022. /*
  6023. * current_ssid can be cleared when P2P client interface gets
  6024. * disconnected, so assume this interface was used as P2P
  6025. * client.
  6026. */
  6027. return 1;
  6028. }
  6029. return wpa_s->current_ssid->p2p_group &&
  6030. wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
  6031. }
  6032. static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
  6033. {
  6034. struct wpa_supplicant *wpa_s = eloop_ctx;
  6035. if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
  6036. wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
  6037. "disabled");
  6038. return;
  6039. }
  6040. wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
  6041. "group");
  6042. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
  6043. }
  6044. static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
  6045. {
  6046. int timeout;
  6047. if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
  6048. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
  6049. if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
  6050. return;
  6051. timeout = wpa_s->conf->p2p_group_idle;
  6052. if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
  6053. (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
  6054. timeout = P2P_MAX_CLIENT_IDLE;
  6055. if (timeout == 0)
  6056. return;
  6057. if (timeout < 0) {
  6058. if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
  6059. timeout = 0; /* special client mode no-timeout */
  6060. else
  6061. return;
  6062. }
  6063. if (wpa_s->p2p_in_provisioning) {
  6064. /*
  6065. * Use the normal group formation timeout during the
  6066. * provisioning phase to avoid terminating this process too
  6067. * early due to group idle timeout.
  6068. */
  6069. wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
  6070. "during provisioning");
  6071. return;
  6072. }
  6073. if (wpa_s->show_group_started) {
  6074. /*
  6075. * Use the normal group formation timeout between the end of
  6076. * the provisioning phase and completion of 4-way handshake to
  6077. * avoid terminating this process too early due to group idle
  6078. * timeout.
  6079. */
  6080. wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
  6081. "while waiting for initial 4-way handshake to "
  6082. "complete");
  6083. return;
  6084. }
  6085. wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
  6086. timeout);
  6087. eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
  6088. wpa_s, NULL);
  6089. }
  6090. /* Returns 1 if the interface was removed */
  6091. int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
  6092. u16 reason_code, const u8 *ie, size_t ie_len,
  6093. int locally_generated)
  6094. {
  6095. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  6096. return 0;
  6097. if (!locally_generated)
  6098. p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
  6099. ie_len);
  6100. if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
  6101. wpa_s->current_ssid &&
  6102. wpa_s->current_ssid->p2p_group &&
  6103. wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
  6104. wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
  6105. "session is ending");
  6106. if (wpas_p2p_group_delete(wpa_s,
  6107. P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
  6108. > 0)
  6109. return 1;
  6110. }
  6111. return 0;
  6112. }
  6113. void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
  6114. u16 reason_code, const u8 *ie, size_t ie_len,
  6115. int locally_generated)
  6116. {
  6117. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  6118. return;
  6119. if (!locally_generated)
  6120. p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
  6121. ie_len);
  6122. }
  6123. void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
  6124. {
  6125. struct p2p_data *p2p = wpa_s->global->p2p;
  6126. if (p2p == NULL)
  6127. return;
  6128. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
  6129. return;
  6130. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
  6131. p2p_set_dev_name(p2p, wpa_s->conf->device_name);
  6132. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
  6133. p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
  6134. if (wpa_s->wps &&
  6135. (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
  6136. p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
  6137. if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
  6138. p2p_set_uuid(p2p, wpa_s->wps->uuid);
  6139. if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
  6140. p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
  6141. p2p_set_model_name(p2p, wpa_s->conf->model_name);
  6142. p2p_set_model_number(p2p, wpa_s->conf->model_number);
  6143. p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
  6144. }
  6145. if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
  6146. p2p_set_sec_dev_types(p2p,
  6147. (void *) wpa_s->conf->sec_device_type,
  6148. wpa_s->conf->num_sec_device_types);
  6149. if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
  6150. int i;
  6151. p2p_remove_wps_vendor_extensions(p2p);
  6152. for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
  6153. if (wpa_s->conf->wps_vendor_ext[i] == NULL)
  6154. continue;
  6155. p2p_add_wps_vendor_extension(
  6156. p2p, wpa_s->conf->wps_vendor_ext[i]);
  6157. }
  6158. }
  6159. if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
  6160. wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  6161. char country[3];
  6162. country[0] = wpa_s->conf->country[0];
  6163. country[1] = wpa_s->conf->country[1];
  6164. country[2] = 0x04;
  6165. p2p_set_country(p2p, country);
  6166. }
  6167. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
  6168. p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
  6169. wpa_s->conf->p2p_ssid_postfix ?
  6170. os_strlen(wpa_s->conf->p2p_ssid_postfix) :
  6171. 0);
  6172. }
  6173. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
  6174. p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
  6175. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
  6176. u8 reg_class, channel;
  6177. int ret;
  6178. unsigned int r;
  6179. u8 channel_forced;
  6180. if (wpa_s->conf->p2p_listen_reg_class &&
  6181. wpa_s->conf->p2p_listen_channel) {
  6182. reg_class = wpa_s->conf->p2p_listen_reg_class;
  6183. channel = wpa_s->conf->p2p_listen_channel;
  6184. channel_forced = 1;
  6185. } else {
  6186. reg_class = 81;
  6187. /*
  6188. * Pick one of the social channels randomly as the
  6189. * listen channel.
  6190. */
  6191. if (os_get_random((u8 *) &r, sizeof(r)) < 0)
  6192. channel = 1;
  6193. else
  6194. channel = 1 + (r % 3) * 5;
  6195. channel_forced = 0;
  6196. }
  6197. ret = p2p_set_listen_channel(p2p, reg_class, channel,
  6198. channel_forced);
  6199. if (ret)
  6200. wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
  6201. "failed: %d", ret);
  6202. }
  6203. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
  6204. u8 op_reg_class, op_channel, cfg_op_channel;
  6205. int ret = 0;
  6206. unsigned int r;
  6207. if (wpa_s->conf->p2p_oper_reg_class &&
  6208. wpa_s->conf->p2p_oper_channel) {
  6209. op_reg_class = wpa_s->conf->p2p_oper_reg_class;
  6210. op_channel = wpa_s->conf->p2p_oper_channel;
  6211. cfg_op_channel = 1;
  6212. } else {
  6213. op_reg_class = 81;
  6214. /*
  6215. * Use random operation channel from (1, 6, 11)
  6216. *if no other preference is indicated.
  6217. */
  6218. if (os_get_random((u8 *) &r, sizeof(r)) < 0)
  6219. op_channel = 1;
  6220. else
  6221. op_channel = 1 + (r % 3) * 5;
  6222. cfg_op_channel = 0;
  6223. }
  6224. ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
  6225. cfg_op_channel);
  6226. if (ret)
  6227. wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
  6228. "failed: %d", ret);
  6229. }
  6230. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
  6231. if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
  6232. wpa_s->conf->p2p_pref_chan) < 0) {
  6233. wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
  6234. "update failed");
  6235. }
  6236. if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
  6237. wpa_printf(MSG_ERROR, "P2P: No GO channel list "
  6238. "update failed");
  6239. }
  6240. }
  6241. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
  6242. p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
  6243. }
  6244. int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
  6245. int duration)
  6246. {
  6247. if (!wpa_s->ap_iface)
  6248. return -1;
  6249. return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
  6250. duration);
  6251. }
  6252. int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
  6253. {
  6254. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  6255. return -1;
  6256. wpa_s->global->cross_connection = enabled;
  6257. p2p_set_cross_connect(wpa_s->global->p2p, enabled);
  6258. if (!enabled) {
  6259. struct wpa_supplicant *iface;
  6260. for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
  6261. {
  6262. if (iface->cross_connect_enabled == 0)
  6263. continue;
  6264. iface->cross_connect_enabled = 0;
  6265. iface->cross_connect_in_use = 0;
  6266. wpa_msg_global(iface->p2pdev, MSG_INFO,
  6267. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  6268. iface->ifname,
  6269. iface->cross_connect_uplink);
  6270. }
  6271. }
  6272. return 0;
  6273. }
  6274. static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
  6275. {
  6276. struct wpa_supplicant *iface;
  6277. if (!uplink->global->cross_connection)
  6278. return;
  6279. for (iface = uplink->global->ifaces; iface; iface = iface->next) {
  6280. if (!iface->cross_connect_enabled)
  6281. continue;
  6282. if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
  6283. 0)
  6284. continue;
  6285. if (iface->ap_iface == NULL)
  6286. continue;
  6287. if (iface->cross_connect_in_use)
  6288. continue;
  6289. iface->cross_connect_in_use = 1;
  6290. wpa_msg_global(iface->p2pdev, MSG_INFO,
  6291. P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
  6292. iface->ifname, iface->cross_connect_uplink);
  6293. }
  6294. }
  6295. static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
  6296. {
  6297. struct wpa_supplicant *iface;
  6298. for (iface = uplink->global->ifaces; iface; iface = iface->next) {
  6299. if (!iface->cross_connect_enabled)
  6300. continue;
  6301. if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
  6302. 0)
  6303. continue;
  6304. if (!iface->cross_connect_in_use)
  6305. continue;
  6306. wpa_msg_global(iface->p2pdev, MSG_INFO,
  6307. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  6308. iface->ifname, iface->cross_connect_uplink);
  6309. iface->cross_connect_in_use = 0;
  6310. }
  6311. }
  6312. void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
  6313. {
  6314. if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
  6315. wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
  6316. wpa_s->cross_connect_disallowed)
  6317. wpas_p2p_disable_cross_connect(wpa_s);
  6318. else
  6319. wpas_p2p_enable_cross_connect(wpa_s);
  6320. if (!wpa_s->ap_iface &&
  6321. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
  6322. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
  6323. }
  6324. void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
  6325. {
  6326. wpas_p2p_disable_cross_connect(wpa_s);
  6327. if (!wpa_s->ap_iface &&
  6328. !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
  6329. wpa_s, NULL))
  6330. wpas_p2p_set_group_idle_timeout(wpa_s);
  6331. }
  6332. static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
  6333. {
  6334. struct wpa_supplicant *iface;
  6335. if (!wpa_s->global->cross_connection)
  6336. return;
  6337. for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
  6338. if (iface == wpa_s)
  6339. continue;
  6340. if (iface->drv_flags &
  6341. WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
  6342. continue;
  6343. if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
  6344. iface != wpa_s->parent)
  6345. continue;
  6346. wpa_s->cross_connect_enabled = 1;
  6347. os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
  6348. sizeof(wpa_s->cross_connect_uplink));
  6349. wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
  6350. "%s to %s whenever uplink is available",
  6351. wpa_s->ifname, wpa_s->cross_connect_uplink);
  6352. if (iface->ap_iface || iface->current_ssid == NULL ||
  6353. iface->current_ssid->mode != WPAS_MODE_INFRA ||
  6354. iface->cross_connect_disallowed ||
  6355. iface->wpa_state != WPA_COMPLETED)
  6356. break;
  6357. wpa_s->cross_connect_in_use = 1;
  6358. wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
  6359. P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
  6360. wpa_s->ifname, wpa_s->cross_connect_uplink);
  6361. break;
  6362. }
  6363. }
  6364. int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
  6365. {
  6366. if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
  6367. !wpa_s->p2p_in_provisioning)
  6368. return 0; /* not P2P client operation */
  6369. wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
  6370. "session overlap");
  6371. if (wpa_s != wpa_s->p2pdev)
  6372. wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
  6373. wpas_p2p_group_formation_failed(wpa_s, 0);
  6374. return 1;
  6375. }
  6376. void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
  6377. {
  6378. struct wpa_supplicant *wpa_s = eloop_ctx;
  6379. wpas_p2p_notif_pbc_overlap(wpa_s);
  6380. }
  6381. void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
  6382. enum wpas_p2p_channel_update_trig trig)
  6383. {
  6384. struct p2p_channels chan, cli_chan;
  6385. struct wpa_used_freq_data *freqs = NULL;
  6386. unsigned int num = wpa_s->num_multichan_concurrent;
  6387. if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
  6388. return;
  6389. freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
  6390. if (!freqs)
  6391. return;
  6392. num = get_shared_radio_freqs_data(wpa_s, freqs, num);
  6393. os_memset(&chan, 0, sizeof(chan));
  6394. os_memset(&cli_chan, 0, sizeof(cli_chan));
  6395. if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
  6396. wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
  6397. "channel list");
  6398. return;
  6399. }
  6400. p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
  6401. wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
  6402. /*
  6403. * The used frequencies map changed, so it is possible that a GO is
  6404. * using a channel that is no longer valid for P2P use. It is also
  6405. * possible that due to policy consideration, it would be preferable to
  6406. * move it to a frequency already used by other station interfaces.
  6407. */
  6408. wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
  6409. os_free(freqs);
  6410. }
  6411. static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
  6412. struct wpa_scan_results *scan_res)
  6413. {
  6414. wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
  6415. }
  6416. int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
  6417. {
  6418. struct wpa_global *global = wpa_s->global;
  6419. int found = 0;
  6420. const u8 *peer;
  6421. if (global->p2p == NULL)
  6422. return -1;
  6423. wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
  6424. if (wpa_s->pending_interface_name[0] &&
  6425. !is_zero_ether_addr(wpa_s->pending_interface_addr))
  6426. found = 1;
  6427. peer = p2p_get_go_neg_peer(global->p2p);
  6428. if (peer) {
  6429. wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
  6430. MACSTR, MAC2STR(peer));
  6431. p2p_unauthorize(global->p2p, peer);
  6432. found = 1;
  6433. }
  6434. if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
  6435. wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
  6436. wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
  6437. found = 1;
  6438. }
  6439. if (wpa_s->pending_pd_before_join) {
  6440. wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
  6441. wpa_s->pending_pd_before_join = 0;
  6442. found = 1;
  6443. }
  6444. wpas_p2p_stop_find(wpa_s);
  6445. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  6446. if (wpa_s == global->p2p_group_formation &&
  6447. (wpa_s->p2p_in_provisioning ||
  6448. wpa_s->parent->pending_interface_type ==
  6449. WPA_IF_P2P_CLIENT)) {
  6450. wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
  6451. "formation found - cancelling",
  6452. wpa_s->ifname);
  6453. found = 1;
  6454. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  6455. wpa_s->p2pdev, NULL);
  6456. if (wpa_s->p2p_in_provisioning) {
  6457. wpas_group_formation_completed(wpa_s, 0, 0);
  6458. break;
  6459. }
  6460. wpas_p2p_group_delete(wpa_s,
  6461. P2P_GROUP_REMOVAL_REQUESTED);
  6462. break;
  6463. } else if (wpa_s->p2p_in_invitation) {
  6464. wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
  6465. wpa_s->ifname);
  6466. found = 1;
  6467. wpas_p2p_group_formation_failed(wpa_s, 0);
  6468. break;
  6469. }
  6470. }
  6471. if (!found) {
  6472. wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
  6473. return -1;
  6474. }
  6475. return 0;
  6476. }
  6477. void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
  6478. {
  6479. if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
  6480. return;
  6481. wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
  6482. "being available anymore");
  6483. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
  6484. }
  6485. void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
  6486. int freq_24, int freq_5, int freq_overall)
  6487. {
  6488. struct p2p_data *p2p = wpa_s->global->p2p;
  6489. if (p2p == NULL)
  6490. return;
  6491. p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
  6492. }
  6493. int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
  6494. {
  6495. u8 peer[ETH_ALEN];
  6496. struct p2p_data *p2p = wpa_s->global->p2p;
  6497. if (p2p == NULL)
  6498. return -1;
  6499. if (hwaddr_aton(addr, peer))
  6500. return -1;
  6501. return p2p_unauthorize(p2p, peer);
  6502. }
  6503. /**
  6504. * wpas_p2p_disconnect - Disconnect from a P2P Group
  6505. * @wpa_s: Pointer to wpa_supplicant data
  6506. * Returns: 0 on success, -1 on failure
  6507. *
  6508. * This can be used to disconnect from a group in which the local end is a P2P
  6509. * Client or to end a P2P Group in case the local end is the Group Owner. If a
  6510. * virtual network interface was created for this group, that interface will be
  6511. * removed. Otherwise, only the configured P2P group network will be removed
  6512. * from the interface.
  6513. */
  6514. int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
  6515. {
  6516. if (wpa_s == NULL)
  6517. return -1;
  6518. return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
  6519. -1 : 0;
  6520. }
  6521. int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
  6522. {
  6523. int ret;
  6524. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  6525. return 0;
  6526. ret = p2p_in_progress(wpa_s->global->p2p);
  6527. if (ret == 0) {
  6528. /*
  6529. * Check whether there is an ongoing WPS provisioning step (or
  6530. * other parts of group formation) on another interface since
  6531. * p2p_in_progress() does not report this to avoid issues for
  6532. * scans during such provisioning step.
  6533. */
  6534. if (wpa_s->global->p2p_group_formation &&
  6535. wpa_s->global->p2p_group_formation != wpa_s) {
  6536. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
  6537. "in group formation",
  6538. wpa_s->global->p2p_group_formation->ifname);
  6539. ret = 1;
  6540. }
  6541. }
  6542. if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
  6543. struct os_reltime now;
  6544. os_get_reltime(&now);
  6545. if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
  6546. P2P_MAX_INITIAL_CONN_WAIT_GO)) {
  6547. /* Wait for the first client has expired */
  6548. wpa_s->global->p2p_go_wait_client.sec = 0;
  6549. } else {
  6550. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
  6551. ret = 1;
  6552. }
  6553. }
  6554. return ret;
  6555. }
  6556. void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
  6557. struct wpa_ssid *ssid)
  6558. {
  6559. if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
  6560. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  6561. wpa_s->p2pdev, NULL) > 0) {
  6562. /**
  6563. * Remove the network by scheduling the group formation
  6564. * timeout to happen immediately. The teardown code
  6565. * needs to be scheduled to run asynch later so that we
  6566. * don't delete data from under ourselves unexpectedly.
  6567. * Calling wpas_p2p_group_formation_timeout directly
  6568. * causes a series of crashes in WPS failure scenarios.
  6569. */
  6570. wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
  6571. "P2P group network getting removed");
  6572. eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
  6573. wpa_s->p2pdev, NULL);
  6574. }
  6575. }
  6576. struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
  6577. const u8 *addr, const u8 *ssid,
  6578. size_t ssid_len)
  6579. {
  6580. struct wpa_ssid *s;
  6581. size_t i;
  6582. for (s = wpa_s->conf->ssid; s; s = s->next) {
  6583. if (s->disabled != 2)
  6584. continue;
  6585. if (ssid &&
  6586. (ssid_len != s->ssid_len ||
  6587. os_memcmp(ssid, s->ssid, ssid_len) != 0))
  6588. continue;
  6589. if (addr == NULL) {
  6590. if (s->mode == WPAS_MODE_P2P_GO)
  6591. return s;
  6592. continue;
  6593. }
  6594. if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
  6595. return s; /* peer is GO in the persistent group */
  6596. if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
  6597. continue;
  6598. for (i = 0; i < s->num_p2p_clients; i++) {
  6599. if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
  6600. addr, ETH_ALEN) == 0)
  6601. return s; /* peer is P2P client in persistent
  6602. * group */
  6603. }
  6604. }
  6605. return NULL;
  6606. }
  6607. void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
  6608. const u8 *addr)
  6609. {
  6610. if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  6611. wpa_s->p2pdev, NULL) > 0) {
  6612. /*
  6613. * This can happen if WPS provisioning step is not terminated
  6614. * cleanly (e.g., P2P Client does not send WSC_Done). Since the
  6615. * peer was able to connect, there is no need to time out group
  6616. * formation after this, though. In addition, this is used with
  6617. * the initial connection wait on the GO as a separate formation
  6618. * timeout and as such, expected to be hit after the initial WPS
  6619. * provisioning step.
  6620. */
  6621. wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
  6622. if (!wpa_s->p2p_go_group_formation_completed &&
  6623. !wpa_s->group_formation_reported) {
  6624. /*
  6625. * GO has not yet notified group formation success since
  6626. * the WPS step was not completed cleanly. Do that
  6627. * notification now since the P2P Client was able to
  6628. * connect and as such, must have received the
  6629. * credential from the WPS step.
  6630. */
  6631. if (wpa_s->global->p2p)
  6632. p2p_wps_success_cb(wpa_s->global->p2p, addr);
  6633. wpas_group_formation_completed(wpa_s, 1, 0);
  6634. }
  6635. }
  6636. if (!wpa_s->p2p_go_group_formation_completed) {
  6637. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
  6638. wpa_s->p2p_go_group_formation_completed = 1;
  6639. wpa_s->global->p2p_group_formation = NULL;
  6640. wpa_s->p2p_in_provisioning = 0;
  6641. wpa_s->p2p_in_invitation = 0;
  6642. }
  6643. wpa_s->global->p2p_go_wait_client.sec = 0;
  6644. if (addr == NULL)
  6645. return;
  6646. wpas_p2p_add_persistent_group_client(wpa_s, addr);
  6647. }
  6648. static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
  6649. int group_added)
  6650. {
  6651. struct wpa_supplicant *group = wpa_s;
  6652. int ret = 0;
  6653. if (wpa_s->global->p2p_group_formation)
  6654. group = wpa_s->global->p2p_group_formation;
  6655. wpa_s = wpa_s->global->p2p_init_wpa_s;
  6656. offchannel_send_action_done(wpa_s);
  6657. if (group_added)
  6658. ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
  6659. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
  6660. wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
  6661. wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
  6662. 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
  6663. wpa_s->p2p_go_vht_center_freq2,
  6664. wpa_s->p2p_persistent_id,
  6665. wpa_s->p2p_pd_before_go_neg,
  6666. wpa_s->p2p_go_ht40,
  6667. wpa_s->p2p_go_vht,
  6668. wpa_s->p2p_go_max_oper_chwidth, NULL, 0);
  6669. return ret;
  6670. }
  6671. int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
  6672. {
  6673. int res;
  6674. if (!wpa_s->p2p_fallback_to_go_neg ||
  6675. wpa_s->p2p_in_provisioning <= 5)
  6676. return 0;
  6677. if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
  6678. return 0; /* peer operating as a GO */
  6679. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
  6680. "fallback to GO Negotiation");
  6681. wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
  6682. "reason=GO-not-found");
  6683. res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
  6684. return res == 1 ? 2 : 1;
  6685. }
  6686. unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
  6687. {
  6688. struct wpa_supplicant *ifs;
  6689. if (wpa_s->wpa_state > WPA_SCANNING) {
  6690. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
  6691. "concurrent operation",
  6692. wpa_s->conf->p2p_search_delay);
  6693. return wpa_s->conf->p2p_search_delay;
  6694. }
  6695. dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
  6696. radio_list) {
  6697. if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
  6698. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
  6699. "delay due to concurrent operation on "
  6700. "interface %s",
  6701. wpa_s->conf->p2p_search_delay,
  6702. ifs->ifname);
  6703. return wpa_s->conf->p2p_search_delay;
  6704. }
  6705. }
  6706. return 0;
  6707. }
  6708. static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
  6709. struct wpa_ssid *s, const u8 *addr,
  6710. int iface_addr)
  6711. {
  6712. struct psk_list_entry *psk, *tmp;
  6713. int changed = 0;
  6714. dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
  6715. list) {
  6716. if ((iface_addr && !psk->p2p &&
  6717. os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
  6718. (!iface_addr && psk->p2p &&
  6719. os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
  6720. wpa_dbg(wpa_s, MSG_DEBUG,
  6721. "P2P: Remove persistent group PSK list entry for "
  6722. MACSTR " p2p=%u",
  6723. MAC2STR(psk->addr), psk->p2p);
  6724. dl_list_del(&psk->list);
  6725. os_free(psk);
  6726. changed++;
  6727. }
  6728. }
  6729. return changed;
  6730. }
  6731. void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
  6732. const u8 *p2p_dev_addr,
  6733. const u8 *psk, size_t psk_len)
  6734. {
  6735. struct wpa_ssid *ssid = wpa_s->current_ssid;
  6736. struct wpa_ssid *persistent;
  6737. struct psk_list_entry *p, *last;
  6738. if (psk_len != sizeof(p->psk))
  6739. return;
  6740. if (p2p_dev_addr) {
  6741. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
  6742. " p2p_dev_addr=" MACSTR,
  6743. MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
  6744. if (is_zero_ether_addr(p2p_dev_addr))
  6745. p2p_dev_addr = NULL;
  6746. } else {
  6747. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
  6748. MAC2STR(mac_addr));
  6749. }
  6750. if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
  6751. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
  6752. /* To be added to persistent group once created */
  6753. if (wpa_s->global->add_psk == NULL) {
  6754. wpa_s->global->add_psk = os_zalloc(sizeof(*p));
  6755. if (wpa_s->global->add_psk == NULL)
  6756. return;
  6757. }
  6758. p = wpa_s->global->add_psk;
  6759. if (p2p_dev_addr) {
  6760. p->p2p = 1;
  6761. os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
  6762. } else {
  6763. p->p2p = 0;
  6764. os_memcpy(p->addr, mac_addr, ETH_ALEN);
  6765. }
  6766. os_memcpy(p->psk, psk, psk_len);
  6767. return;
  6768. }
  6769. if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
  6770. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
  6771. return;
  6772. }
  6773. persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
  6774. ssid->ssid_len);
  6775. if (!persistent) {
  6776. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
  6777. return;
  6778. }
  6779. p = os_zalloc(sizeof(*p));
  6780. if (p == NULL)
  6781. return;
  6782. if (p2p_dev_addr) {
  6783. p->p2p = 1;
  6784. os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
  6785. } else {
  6786. p->p2p = 0;
  6787. os_memcpy(p->addr, mac_addr, ETH_ALEN);
  6788. }
  6789. os_memcpy(p->psk, psk, psk_len);
  6790. if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
  6791. (last = dl_list_last(&persistent->psk_list,
  6792. struct psk_list_entry, list))) {
  6793. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
  6794. MACSTR " (p2p=%u) to make room for a new one",
  6795. MAC2STR(last->addr), last->p2p);
  6796. dl_list_del(&last->list);
  6797. os_free(last);
  6798. }
  6799. wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
  6800. p2p_dev_addr ? p2p_dev_addr : mac_addr,
  6801. p2p_dev_addr == NULL);
  6802. if (p2p_dev_addr) {
  6803. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
  6804. MACSTR, MAC2STR(p2p_dev_addr));
  6805. } else {
  6806. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
  6807. MAC2STR(mac_addr));
  6808. }
  6809. dl_list_add(&persistent->psk_list, &p->list);
  6810. if (wpa_s->p2pdev->conf->update_config &&
  6811. wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
  6812. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  6813. }
  6814. static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
  6815. struct wpa_ssid *s, const u8 *addr,
  6816. int iface_addr)
  6817. {
  6818. int res;
  6819. res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
  6820. if (res > 0 && wpa_s->conf->update_config &&
  6821. wpa_config_write(wpa_s->confname, wpa_s->conf))
  6822. wpa_dbg(wpa_s, MSG_DEBUG,
  6823. "P2P: Failed to update configuration");
  6824. }
  6825. static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
  6826. const u8 *peer, int iface_addr)
  6827. {
  6828. struct hostapd_data *hapd;
  6829. struct hostapd_wpa_psk *psk, *prev, *rem;
  6830. struct sta_info *sta;
  6831. if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
  6832. wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
  6833. return;
  6834. /* Remove per-station PSK entry */
  6835. hapd = wpa_s->ap_iface->bss[0];
  6836. prev = NULL;
  6837. psk = hapd->conf->ssid.wpa_psk;
  6838. while (psk) {
  6839. if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
  6840. (!iface_addr &&
  6841. os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
  6842. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
  6843. MACSTR " iface_addr=%d",
  6844. MAC2STR(peer), iface_addr);
  6845. if (prev)
  6846. prev->next = psk->next;
  6847. else
  6848. hapd->conf->ssid.wpa_psk = psk->next;
  6849. rem = psk;
  6850. psk = psk->next;
  6851. os_free(rem);
  6852. } else {
  6853. prev = psk;
  6854. psk = psk->next;
  6855. }
  6856. }
  6857. /* Disconnect from group */
  6858. if (iface_addr)
  6859. sta = ap_get_sta(hapd, peer);
  6860. else
  6861. sta = ap_get_sta_p2p(hapd, peer);
  6862. if (sta) {
  6863. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
  6864. " (iface_addr=%d) from group",
  6865. MAC2STR(peer), iface_addr);
  6866. hostapd_drv_sta_deauth(hapd, sta->addr,
  6867. WLAN_REASON_DEAUTH_LEAVING);
  6868. ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
  6869. }
  6870. }
  6871. void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
  6872. int iface_addr)
  6873. {
  6874. struct wpa_ssid *s;
  6875. struct wpa_supplicant *w;
  6876. struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
  6877. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
  6878. /* Remove from any persistent group */
  6879. for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
  6880. if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
  6881. continue;
  6882. if (!iface_addr)
  6883. wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
  6884. wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
  6885. }
  6886. /* Remove from any operating group */
  6887. for (w = wpa_s->global->ifaces; w; w = w->next)
  6888. wpas_p2p_remove_client_go(w, peer, iface_addr);
  6889. }
  6890. static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
  6891. {
  6892. struct wpa_supplicant *wpa_s = eloop_ctx;
  6893. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
  6894. }
  6895. static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
  6896. {
  6897. struct wpa_supplicant *wpa_s = eloop_ctx;
  6898. wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
  6899. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
  6900. }
  6901. int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
  6902. struct wpa_ssid *ssid)
  6903. {
  6904. struct wpa_supplicant *iface;
  6905. for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
  6906. if (!iface->current_ssid ||
  6907. iface->current_ssid->frequency == freq ||
  6908. (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
  6909. !iface->current_ssid->p2p_group))
  6910. continue;
  6911. /* Remove the connection with least priority */
  6912. if (!wpas_is_p2p_prioritized(iface)) {
  6913. /* STA connection has priority over existing
  6914. * P2P connection, so remove the interface. */
  6915. wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
  6916. eloop_register_timeout(0, 0,
  6917. wpas_p2p_group_freq_conflict,
  6918. iface, NULL);
  6919. /* If connection in progress is P2P connection, do not
  6920. * proceed for the connection. */
  6921. if (wpa_s == iface)
  6922. return -1;
  6923. else
  6924. return 0;
  6925. } else {
  6926. /* P2P connection has priority, disable the STA network
  6927. */
  6928. wpa_supplicant_disable_network(wpa_s->global->ifaces,
  6929. ssid);
  6930. wpa_msg(wpa_s->global->ifaces, MSG_INFO,
  6931. WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
  6932. os_memset(wpa_s->global->ifaces->pending_bssid, 0,
  6933. ETH_ALEN);
  6934. /* If P2P connection is in progress, continue
  6935. * connecting...*/
  6936. if (wpa_s == iface)
  6937. return 0;
  6938. else
  6939. return -1;
  6940. }
  6941. }
  6942. return 0;
  6943. }
  6944. int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
  6945. {
  6946. struct wpa_ssid *ssid = wpa_s->current_ssid;
  6947. if (ssid == NULL || !ssid->p2p_group)
  6948. return 0;
  6949. if (wpa_s->p2p_last_4way_hs_fail &&
  6950. wpa_s->p2p_last_4way_hs_fail == ssid) {
  6951. u8 go_dev_addr[ETH_ALEN];
  6952. struct wpa_ssid *persistent;
  6953. if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
  6954. ssid->ssid,
  6955. ssid->ssid_len) <= 0) {
  6956. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
  6957. goto disconnect;
  6958. }
  6959. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
  6960. MACSTR, MAC2STR(go_dev_addr));
  6961. persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
  6962. ssid->ssid,
  6963. ssid->ssid_len);
  6964. if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
  6965. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
  6966. goto disconnect;
  6967. }
  6968. wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
  6969. P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
  6970. persistent->id);
  6971. disconnect:
  6972. wpa_s->p2p_last_4way_hs_fail = NULL;
  6973. /*
  6974. * Remove the group from a timeout to avoid issues with caller
  6975. * continuing to use the interface if this is on a P2P group
  6976. * interface.
  6977. */
  6978. eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
  6979. wpa_s, NULL);
  6980. return 1;
  6981. }
  6982. wpa_s->p2p_last_4way_hs_fail = ssid;
  6983. return 0;
  6984. }
  6985. #ifdef CONFIG_WPS_NFC
  6986. static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
  6987. struct wpabuf *p2p)
  6988. {
  6989. struct wpabuf *ret;
  6990. size_t wsc_len;
  6991. if (p2p == NULL) {
  6992. wpabuf_free(wsc);
  6993. wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
  6994. return NULL;
  6995. }
  6996. wsc_len = wsc ? wpabuf_len(wsc) : 0;
  6997. ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
  6998. if (ret == NULL) {
  6999. wpabuf_free(wsc);
  7000. wpabuf_free(p2p);
  7001. return NULL;
  7002. }
  7003. wpabuf_put_be16(ret, wsc_len);
  7004. if (wsc)
  7005. wpabuf_put_buf(ret, wsc);
  7006. wpabuf_put_be16(ret, wpabuf_len(p2p));
  7007. wpabuf_put_buf(ret, p2p);
  7008. wpabuf_free(wsc);
  7009. wpabuf_free(p2p);
  7010. wpa_hexdump_buf(MSG_DEBUG,
  7011. "P2P: Generated NFC connection handover message", ret);
  7012. if (ndef && ret) {
  7013. struct wpabuf *tmp;
  7014. tmp = ndef_build_p2p(ret);
  7015. wpabuf_free(ret);
  7016. if (tmp == NULL) {
  7017. wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
  7018. return NULL;
  7019. }
  7020. ret = tmp;
  7021. }
  7022. return ret;
  7023. }
  7024. static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
  7025. struct wpa_ssid **ssid, u8 *go_dev_addr)
  7026. {
  7027. struct wpa_supplicant *iface;
  7028. if (go_dev_addr)
  7029. os_memset(go_dev_addr, 0, ETH_ALEN);
  7030. if (ssid)
  7031. *ssid = NULL;
  7032. for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
  7033. if (iface->wpa_state < WPA_ASSOCIATING ||
  7034. iface->current_ssid == NULL || iface->assoc_freq == 0 ||
  7035. !iface->current_ssid->p2p_group ||
  7036. iface->current_ssid->mode != WPAS_MODE_INFRA)
  7037. continue;
  7038. if (ssid)
  7039. *ssid = iface->current_ssid;
  7040. if (go_dev_addr)
  7041. os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
  7042. return iface->assoc_freq;
  7043. }
  7044. return 0;
  7045. }
  7046. struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
  7047. int ndef)
  7048. {
  7049. struct wpabuf *wsc, *p2p;
  7050. struct wpa_ssid *ssid;
  7051. u8 go_dev_addr[ETH_ALEN];
  7052. int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
  7053. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
  7054. wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
  7055. return NULL;
  7056. }
  7057. if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
  7058. wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
  7059. &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
  7060. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
  7061. return NULL;
  7062. }
  7063. if (cli_freq == 0) {
  7064. wsc = wps_build_nfc_handover_req_p2p(
  7065. wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
  7066. } else
  7067. wsc = NULL;
  7068. p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
  7069. go_dev_addr, ssid ? ssid->ssid : NULL,
  7070. ssid ? ssid->ssid_len : 0);
  7071. return wpas_p2p_nfc_handover(ndef, wsc, p2p);
  7072. }
  7073. struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
  7074. int ndef, int tag)
  7075. {
  7076. struct wpabuf *wsc, *p2p;
  7077. struct wpa_ssid *ssid;
  7078. u8 go_dev_addr[ETH_ALEN];
  7079. int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
  7080. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  7081. return NULL;
  7082. if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
  7083. wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
  7084. &wpa_s->conf->wps_nfc_dh_privkey) < 0)
  7085. return NULL;
  7086. if (cli_freq == 0) {
  7087. wsc = wps_build_nfc_handover_sel_p2p(
  7088. wpa_s->parent->wps,
  7089. tag ? wpa_s->conf->wps_nfc_dev_pw_id :
  7090. DEV_PW_NFC_CONNECTION_HANDOVER,
  7091. wpa_s->conf->wps_nfc_dh_pubkey,
  7092. tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
  7093. } else
  7094. wsc = NULL;
  7095. p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
  7096. go_dev_addr, ssid ? ssid->ssid : NULL,
  7097. ssid ? ssid->ssid_len : 0);
  7098. return wpas_p2p_nfc_handover(ndef, wsc, p2p);
  7099. }
  7100. static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
  7101. struct p2p_nfc_params *params)
  7102. {
  7103. wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
  7104. "connection handover (freq=%d)",
  7105. params->go_freq);
  7106. if (params->go_freq && params->go_ssid_len) {
  7107. wpa_s->p2p_wps_method = WPS_NFC;
  7108. wpa_s->pending_join_wps_method = WPS_NFC;
  7109. os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
  7110. os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
  7111. ETH_ALEN);
  7112. return wpas_p2p_join_start(wpa_s, params->go_freq,
  7113. params->go_ssid,
  7114. params->go_ssid_len);
  7115. }
  7116. return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
  7117. WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
  7118. params->go_freq, wpa_s->p2p_go_vht_center_freq2,
  7119. -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
  7120. params->go_ssid_len ? params->go_ssid : NULL,
  7121. params->go_ssid_len);
  7122. }
  7123. static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
  7124. struct p2p_nfc_params *params, int tag)
  7125. {
  7126. int res, persistent;
  7127. struct wpa_ssid *ssid;
  7128. wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
  7129. "connection handover");
  7130. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  7131. ssid = wpa_s->current_ssid;
  7132. if (ssid == NULL)
  7133. continue;
  7134. if (ssid->mode != WPAS_MODE_P2P_GO)
  7135. continue;
  7136. if (wpa_s->ap_iface == NULL)
  7137. continue;
  7138. break;
  7139. }
  7140. if (wpa_s == NULL) {
  7141. wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
  7142. return -1;
  7143. }
  7144. if (wpa_s->parent->p2p_oob_dev_pw_id !=
  7145. DEV_PW_NFC_CONNECTION_HANDOVER &&
  7146. !wpa_s->parent->p2p_oob_dev_pw) {
  7147. wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
  7148. return -1;
  7149. }
  7150. res = wpas_ap_wps_add_nfc_pw(
  7151. wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
  7152. wpa_s->parent->p2p_oob_dev_pw,
  7153. wpa_s->parent->p2p_peer_oob_pk_hash_known ?
  7154. wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
  7155. if (res)
  7156. return res;
  7157. if (!tag) {
  7158. wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
  7159. return 0;
  7160. }
  7161. if (!params->peer ||
  7162. !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
  7163. return 0;
  7164. wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
  7165. " to join", MAC2STR(params->peer->p2p_device_addr));
  7166. wpa_s->global->p2p_invite_group = wpa_s;
  7167. persistent = ssid->p2p_persistent_group &&
  7168. wpas_p2p_get_persistent(wpa_s->parent,
  7169. params->peer->p2p_device_addr,
  7170. ssid->ssid, ssid->ssid_len);
  7171. wpa_s->parent->pending_invite_ssid_id = -1;
  7172. return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
  7173. P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
  7174. ssid->ssid, ssid->ssid_len, ssid->frequency,
  7175. wpa_s->global->p2p_dev_addr, persistent, 0,
  7176. wpa_s->parent->p2p_oob_dev_pw_id);
  7177. }
  7178. static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
  7179. struct p2p_nfc_params *params,
  7180. int forced_freq)
  7181. {
  7182. wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
  7183. "connection handover");
  7184. return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
  7185. WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
  7186. forced_freq, wpa_s->p2p_go_vht_center_freq2,
  7187. -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
  7188. NULL, 0);
  7189. }
  7190. static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
  7191. struct p2p_nfc_params *params,
  7192. int forced_freq)
  7193. {
  7194. int res;
  7195. wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
  7196. "connection handover");
  7197. res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
  7198. WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
  7199. forced_freq, wpa_s->p2p_go_vht_center_freq2,
  7200. -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
  7201. NULL, 0);
  7202. if (res)
  7203. return res;
  7204. res = wpas_p2p_listen(wpa_s, 60);
  7205. if (res) {
  7206. p2p_unauthorize(wpa_s->global->p2p,
  7207. params->peer->p2p_device_addr);
  7208. }
  7209. return res;
  7210. }
  7211. static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
  7212. const struct wpabuf *data,
  7213. int sel, int tag, int forced_freq)
  7214. {
  7215. const u8 *pos, *end;
  7216. u16 len, id;
  7217. struct p2p_nfc_params params;
  7218. int res;
  7219. os_memset(&params, 0, sizeof(params));
  7220. params.sel = sel;
  7221. wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
  7222. pos = wpabuf_head(data);
  7223. end = pos + wpabuf_len(data);
  7224. if (end - pos < 2) {
  7225. wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
  7226. "attributes");
  7227. return -1;
  7228. }
  7229. len = WPA_GET_BE16(pos);
  7230. pos += 2;
  7231. if (len > end - pos) {
  7232. wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
  7233. "attributes");
  7234. return -1;
  7235. }
  7236. params.wsc_attr = pos;
  7237. params.wsc_len = len;
  7238. pos += len;
  7239. if (end - pos < 2) {
  7240. wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
  7241. "attributes");
  7242. return -1;
  7243. }
  7244. len = WPA_GET_BE16(pos);
  7245. pos += 2;
  7246. if (len > end - pos) {
  7247. wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
  7248. "attributes");
  7249. return -1;
  7250. }
  7251. params.p2p_attr = pos;
  7252. params.p2p_len = len;
  7253. pos += len;
  7254. wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
  7255. params.wsc_attr, params.wsc_len);
  7256. wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
  7257. params.p2p_attr, params.p2p_len);
  7258. if (pos < end) {
  7259. wpa_hexdump(MSG_DEBUG,
  7260. "P2P: Ignored extra data after P2P attributes",
  7261. pos, end - pos);
  7262. }
  7263. res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
  7264. if (res)
  7265. return res;
  7266. if (params.next_step == NO_ACTION)
  7267. return 0;
  7268. if (params.next_step == BOTH_GO) {
  7269. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
  7270. MAC2STR(params.peer->p2p_device_addr));
  7271. return 0;
  7272. }
  7273. if (params.next_step == PEER_CLIENT) {
  7274. if (!is_zero_ether_addr(params.go_dev_addr)) {
  7275. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
  7276. "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
  7277. " ssid=\"%s\"",
  7278. MAC2STR(params.peer->p2p_device_addr),
  7279. params.go_freq,
  7280. MAC2STR(params.go_dev_addr),
  7281. wpa_ssid_txt(params.go_ssid,
  7282. params.go_ssid_len));
  7283. } else {
  7284. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
  7285. "peer=" MACSTR " freq=%d",
  7286. MAC2STR(params.peer->p2p_device_addr),
  7287. params.go_freq);
  7288. }
  7289. return 0;
  7290. }
  7291. if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
  7292. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
  7293. MACSTR, MAC2STR(params.peer->p2p_device_addr));
  7294. return 0;
  7295. }
  7296. wpabuf_free(wpa_s->p2p_oob_dev_pw);
  7297. wpa_s->p2p_oob_dev_pw = NULL;
  7298. if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
  7299. wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
  7300. "received");
  7301. return -1;
  7302. }
  7303. id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
  7304. wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
  7305. wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
  7306. params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
  7307. os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
  7308. params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
  7309. wpa_s->p2p_peer_oob_pk_hash_known = 1;
  7310. if (tag) {
  7311. if (id < 0x10) {
  7312. wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
  7313. "peer OOB Device Password Id %u", id);
  7314. return -1;
  7315. }
  7316. wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
  7317. "Device Password Id %u", id);
  7318. wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
  7319. params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
  7320. params.oob_dev_pw_len -
  7321. WPS_OOB_PUBKEY_HASH_LEN - 2);
  7322. wpa_s->p2p_oob_dev_pw_id = id;
  7323. wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
  7324. params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
  7325. params.oob_dev_pw_len -
  7326. WPS_OOB_PUBKEY_HASH_LEN - 2);
  7327. if (wpa_s->p2p_oob_dev_pw == NULL)
  7328. return -1;
  7329. if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
  7330. wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
  7331. &wpa_s->conf->wps_nfc_dh_privkey) < 0)
  7332. return -1;
  7333. } else {
  7334. wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
  7335. "without Device Password");
  7336. wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
  7337. }
  7338. switch (params.next_step) {
  7339. case NO_ACTION:
  7340. case BOTH_GO:
  7341. case PEER_CLIENT:
  7342. /* already covered above */
  7343. return 0;
  7344. case JOIN_GROUP:
  7345. return wpas_p2p_nfc_join_group(wpa_s, &params);
  7346. case AUTH_JOIN:
  7347. return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
  7348. case INIT_GO_NEG:
  7349. return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
  7350. case RESP_GO_NEG:
  7351. /* TODO: use own OOB Dev Pw */
  7352. return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
  7353. }
  7354. return -1;
  7355. }
  7356. int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
  7357. const struct wpabuf *data, int forced_freq)
  7358. {
  7359. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  7360. return -1;
  7361. return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
  7362. }
  7363. int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
  7364. const struct wpabuf *req,
  7365. const struct wpabuf *sel, int forced_freq)
  7366. {
  7367. struct wpabuf *tmp;
  7368. int ret;
  7369. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  7370. return -1;
  7371. wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
  7372. wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
  7373. wpabuf_head(req), wpabuf_len(req));
  7374. wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
  7375. wpabuf_head(sel), wpabuf_len(sel));
  7376. if (forced_freq)
  7377. wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
  7378. tmp = ndef_parse_p2p(init ? sel : req);
  7379. if (tmp == NULL) {
  7380. wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
  7381. return -1;
  7382. }
  7383. ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
  7384. forced_freq);
  7385. wpabuf_free(tmp);
  7386. return ret;
  7387. }
  7388. int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
  7389. {
  7390. const u8 *if_addr;
  7391. int go_intent = wpa_s->conf->p2p_go_intent;
  7392. struct wpa_supplicant *iface;
  7393. if (wpa_s->global->p2p == NULL)
  7394. return -1;
  7395. if (!enabled) {
  7396. wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
  7397. for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
  7398. {
  7399. if (!iface->ap_iface)
  7400. continue;
  7401. hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
  7402. }
  7403. p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
  7404. 0, NULL);
  7405. if (wpa_s->p2p_nfc_tag_enabled)
  7406. wpas_p2p_remove_pending_group_interface(wpa_s);
  7407. wpa_s->p2p_nfc_tag_enabled = 0;
  7408. return 0;
  7409. }
  7410. if (wpa_s->global->p2p_disabled)
  7411. return -1;
  7412. if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
  7413. wpa_s->conf->wps_nfc_dh_privkey == NULL ||
  7414. wpa_s->conf->wps_nfc_dev_pw == NULL ||
  7415. wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
  7416. wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
  7417. "to allow static handover cases");
  7418. return -1;
  7419. }
  7420. wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
  7421. wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
  7422. wpabuf_free(wpa_s->p2p_oob_dev_pw);
  7423. wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
  7424. if (wpa_s->p2p_oob_dev_pw == NULL)
  7425. return -1;
  7426. wpa_s->p2p_peer_oob_pk_hash_known = 0;
  7427. if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
  7428. wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
  7429. /*
  7430. * P2P Group Interface present and the command came on group
  7431. * interface, so enable the token for the current interface.
  7432. */
  7433. wpa_s->create_p2p_iface = 0;
  7434. } else {
  7435. wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
  7436. }
  7437. if (wpa_s->create_p2p_iface) {
  7438. enum wpa_driver_if_type iftype;
  7439. /* Prepare to add a new interface for the group */
  7440. iftype = WPA_IF_P2P_GROUP;
  7441. if (go_intent == 15)
  7442. iftype = WPA_IF_P2P_GO;
  7443. if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
  7444. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  7445. "interface for the group");
  7446. return -1;
  7447. }
  7448. if_addr = wpa_s->pending_interface_addr;
  7449. } else if (wpa_s->p2p_mgmt)
  7450. if_addr = wpa_s->parent->own_addr;
  7451. else
  7452. if_addr = wpa_s->own_addr;
  7453. wpa_s->p2p_nfc_tag_enabled = enabled;
  7454. for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
  7455. struct hostapd_data *hapd;
  7456. if (iface->ap_iface == NULL)
  7457. continue;
  7458. hapd = iface->ap_iface->bss[0];
  7459. wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
  7460. hapd->conf->wps_nfc_dh_pubkey =
  7461. wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
  7462. wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
  7463. hapd->conf->wps_nfc_dh_privkey =
  7464. wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
  7465. wpabuf_free(hapd->conf->wps_nfc_dev_pw);
  7466. hapd->conf->wps_nfc_dev_pw =
  7467. wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
  7468. hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
  7469. if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
  7470. wpa_dbg(iface, MSG_DEBUG,
  7471. "P2P: Failed to enable NFC Tag for GO");
  7472. }
  7473. }
  7474. p2p_set_authorized_oob_dev_pw_id(
  7475. wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
  7476. if_addr);
  7477. return 0;
  7478. }
  7479. #endif /* CONFIG_WPS_NFC */
  7480. static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
  7481. struct wpa_used_freq_data *freqs,
  7482. unsigned int num)
  7483. {
  7484. u8 curr_chan, cand, chan;
  7485. unsigned int i;
  7486. /*
  7487. * If possible, optimize the Listen channel to be a channel that is
  7488. * already used by one of the other interfaces.
  7489. */
  7490. if (!wpa_s->conf->p2p_optimize_listen_chan)
  7491. return;
  7492. if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
  7493. return;
  7494. curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
  7495. for (i = 0, cand = 0; i < num; i++) {
  7496. ieee80211_freq_to_chan(freqs[i].freq, &chan);
  7497. if (curr_chan == chan) {
  7498. cand = 0;
  7499. break;
  7500. }
  7501. if (chan == 1 || chan == 6 || chan == 11)
  7502. cand = chan;
  7503. }
  7504. if (cand) {
  7505. wpa_dbg(wpa_s, MSG_DEBUG,
  7506. "P2P: Update Listen channel to %u based on operating channel",
  7507. cand);
  7508. p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
  7509. }
  7510. }
  7511. static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
  7512. {
  7513. struct hostapd_config *conf;
  7514. struct p2p_go_neg_results params;
  7515. struct csa_settings csa_settings;
  7516. struct wpa_ssid *current_ssid = wpa_s->current_ssid;
  7517. int old_freq = current_ssid->frequency;
  7518. int ret;
  7519. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
  7520. wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
  7521. return -1;
  7522. }
  7523. /*
  7524. * TODO: This function may not always work correctly. For example,
  7525. * when we have a running GO and a BSS on a DFS channel.
  7526. */
  7527. if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, NULL)) {
  7528. wpa_dbg(wpa_s, MSG_DEBUG,
  7529. "P2P CSA: Failed to select new frequency for GO");
  7530. return -1;
  7531. }
  7532. if (current_ssid->frequency == params.freq) {
  7533. wpa_dbg(wpa_s, MSG_DEBUG,
  7534. "P2P CSA: Selected same frequency - not moving GO");
  7535. return 0;
  7536. }
  7537. conf = hostapd_config_defaults();
  7538. if (!conf) {
  7539. wpa_dbg(wpa_s, MSG_DEBUG,
  7540. "P2P CSA: Failed to allocate default config");
  7541. return -1;
  7542. }
  7543. current_ssid->frequency = params.freq;
  7544. if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
  7545. wpa_dbg(wpa_s, MSG_DEBUG,
  7546. "P2P CSA: Failed to create new GO config");
  7547. ret = -1;
  7548. goto out;
  7549. }
  7550. if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode) {
  7551. wpa_dbg(wpa_s, MSG_DEBUG,
  7552. "P2P CSA: CSA to a different band is not supported");
  7553. ret = -1;
  7554. goto out;
  7555. }
  7556. os_memset(&csa_settings, 0, sizeof(csa_settings));
  7557. csa_settings.cs_count = P2P_GO_CSA_COUNT;
  7558. csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
  7559. csa_settings.freq_params.freq = params.freq;
  7560. csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
  7561. csa_settings.freq_params.ht_enabled = conf->ieee80211n;
  7562. csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
  7563. if (conf->ieee80211ac) {
  7564. int freq1 = 0, freq2 = 0;
  7565. u8 chan, opclass;
  7566. if (ieee80211_freq_to_channel_ext(params.freq,
  7567. conf->secondary_channel,
  7568. conf->vht_oper_chwidth,
  7569. &opclass, &chan) ==
  7570. NUM_HOSTAPD_MODES) {
  7571. wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
  7572. ret = -1;
  7573. goto out;
  7574. }
  7575. if (conf->vht_oper_centr_freq_seg0_idx)
  7576. freq1 = ieee80211_chan_to_freq(
  7577. NULL, opclass,
  7578. conf->vht_oper_centr_freq_seg0_idx);
  7579. if (conf->vht_oper_centr_freq_seg1_idx)
  7580. freq2 = ieee80211_chan_to_freq(
  7581. NULL, opclass,
  7582. conf->vht_oper_centr_freq_seg1_idx);
  7583. if (freq1 < 0 || freq2 < 0) {
  7584. wpa_dbg(wpa_s, MSG_DEBUG,
  7585. "P2P CSA: Selected invalid VHT center freqs");
  7586. ret = -1;
  7587. goto out;
  7588. }
  7589. csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
  7590. csa_settings.freq_params.center_freq1 = freq1;
  7591. csa_settings.freq_params.center_freq2 = freq2;
  7592. switch (conf->vht_oper_chwidth) {
  7593. case VHT_CHANWIDTH_80MHZ:
  7594. case VHT_CHANWIDTH_80P80MHZ:
  7595. csa_settings.freq_params.bandwidth = 80;
  7596. break;
  7597. case VHT_CHANWIDTH_160MHZ:
  7598. csa_settings.freq_params.bandwidth = 160;
  7599. break;
  7600. }
  7601. }
  7602. ret = ap_switch_channel(wpa_s, &csa_settings);
  7603. out:
  7604. current_ssid->frequency = old_freq;
  7605. hostapd_config_free(conf);
  7606. return ret;
  7607. }
  7608. static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
  7609. {
  7610. struct p2p_go_neg_results params;
  7611. struct wpa_ssid *current_ssid = wpa_s->current_ssid;
  7612. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
  7613. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
  7614. current_ssid->frequency);
  7615. /* Stop the AP functionality */
  7616. /* TODO: Should do this in a way that does not indicated to possible
  7617. * P2P Clients in the group that the group is terminated. */
  7618. wpa_supplicant_ap_deinit(wpa_s);
  7619. /* Reselect the GO frequency */
  7620. if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, NULL)) {
  7621. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
  7622. wpas_p2p_group_delete(wpa_s,
  7623. P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
  7624. return;
  7625. }
  7626. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
  7627. params.freq);
  7628. if (params.freq &&
  7629. !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
  7630. wpa_printf(MSG_DEBUG,
  7631. "P2P: Selected freq (%u MHz) is not valid for P2P",
  7632. params.freq);
  7633. wpas_p2p_group_delete(wpa_s,
  7634. P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
  7635. return;
  7636. }
  7637. /* Update the frequency */
  7638. current_ssid->frequency = params.freq;
  7639. wpa_s->connect_without_scan = current_ssid;
  7640. wpa_s->reassociate = 1;
  7641. wpa_s->disconnected = 0;
  7642. wpa_supplicant_req_scan(wpa_s, 0, 0);
  7643. }
  7644. static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
  7645. {
  7646. struct wpa_supplicant *wpa_s = eloop_ctx;
  7647. if (!wpa_s->ap_iface || !wpa_s->current_ssid)
  7648. return;
  7649. wpas_p2p_go_update_common_freqs(wpa_s);
  7650. /* Do not move GO in the middle of a CSA */
  7651. if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
  7652. wpa_printf(MSG_DEBUG,
  7653. "P2P: CSA is in progress - not moving GO");
  7654. return;
  7655. }
  7656. /*
  7657. * First, try a channel switch flow. If it is not supported or fails,
  7658. * take down the GO and bring it up again.
  7659. */
  7660. if (wpas_p2p_move_go_csa(wpa_s) < 0)
  7661. wpas_p2p_move_go_no_csa(wpa_s);
  7662. }
  7663. static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
  7664. {
  7665. struct wpa_supplicant *wpa_s = eloop_ctx;
  7666. struct wpa_used_freq_data *freqs = NULL;
  7667. unsigned int num = wpa_s->num_multichan_concurrent;
  7668. freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
  7669. if (!freqs)
  7670. return;
  7671. num = get_shared_radio_freqs_data(wpa_s, freqs, num);
  7672. /* Previous attempt to move a GO was not possible -- try again. */
  7673. wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
  7674. WPAS_P2P_CHANNEL_UPDATE_ANY);
  7675. os_free(freqs);
  7676. }
  7677. /*
  7678. * Consider moving a GO from its currently used frequency:
  7679. * 1. It is possible that due to regulatory consideration the frequency
  7680. * can no longer be used and there is a need to evacuate the GO.
  7681. * 2. It is possible that due to MCC considerations, it would be preferable
  7682. * to move the GO to a channel that is currently used by some other
  7683. * station interface.
  7684. *
  7685. * In case a frequency that became invalid is once again valid, cancel a
  7686. * previously initiated GO frequency change.
  7687. */
  7688. static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
  7689. struct wpa_used_freq_data *freqs,
  7690. unsigned int num)
  7691. {
  7692. unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
  7693. unsigned int timeout;
  7694. int freq;
  7695. wpas_p2p_go_update_common_freqs(wpa_s);
  7696. freq = wpa_s->current_ssid->frequency;
  7697. for (i = 0, invalid_freq = 0; i < num; i++) {
  7698. if (freqs[i].freq == freq) {
  7699. flags = freqs[i].flags;
  7700. /* The channel is invalid, must change it */
  7701. if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
  7702. wpa_dbg(wpa_s, MSG_DEBUG,
  7703. "P2P: Freq=%d MHz no longer valid for GO",
  7704. freq);
  7705. invalid_freq = 1;
  7706. }
  7707. } else if (freqs[i].flags == 0) {
  7708. /* Freq is not used by any other station interface */
  7709. continue;
  7710. } else if (!p2p_supported_freq(wpa_s->global->p2p,
  7711. freqs[i].freq)) {
  7712. /* Freq is not valid for P2P use cases */
  7713. continue;
  7714. } else if (wpa_s->conf->p2p_go_freq_change_policy ==
  7715. P2P_GO_FREQ_MOVE_SCM) {
  7716. policy_move = 1;
  7717. } else if (wpa_s->conf->p2p_go_freq_change_policy ==
  7718. P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
  7719. wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
  7720. policy_move = 1;
  7721. } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
  7722. P2P_GO_FREQ_MOVE_SCM_ECSA) &&
  7723. wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
  7724. if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
  7725. policy_move = 1;
  7726. } else if ((wpa_s->drv_flags &
  7727. WPA_DRIVER_FLAGS_AP_CSA) &&
  7728. wpas_p2p_go_clients_support_ecsa(wpa_s)) {
  7729. u8 chan;
  7730. /*
  7731. * We do not support CSA between bands, so move
  7732. * GO only within the same band.
  7733. */
  7734. if (wpa_s->ap_iface->current_mode->mode ==
  7735. ieee80211_freq_to_chan(freqs[i].freq,
  7736. &chan))
  7737. policy_move = 1;
  7738. }
  7739. }
  7740. }
  7741. wpa_dbg(wpa_s, MSG_DEBUG,
  7742. "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
  7743. invalid_freq, policy_move, flags);
  7744. /*
  7745. * The channel is valid, or we are going to have a policy move, so
  7746. * cancel timeout.
  7747. */
  7748. if (!invalid_freq || policy_move) {
  7749. wpa_dbg(wpa_s, MSG_DEBUG,
  7750. "P2P: Cancel a GO move from freq=%d MHz", freq);
  7751. eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
  7752. if (wpas_p2p_in_progress(wpa_s)) {
  7753. wpa_dbg(wpa_s, MSG_DEBUG,
  7754. "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
  7755. eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
  7756. wpa_s, NULL);
  7757. eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
  7758. wpas_p2p_reconsider_moving_go,
  7759. wpa_s, NULL);
  7760. return;
  7761. }
  7762. }
  7763. if (!invalid_freq && (!policy_move || flags != 0)) {
  7764. wpa_dbg(wpa_s, MSG_DEBUG,
  7765. "P2P: Not initiating a GO frequency change");
  7766. return;
  7767. }
  7768. /*
  7769. * Do not consider moving GO if it is in the middle of a CSA. When the
  7770. * CSA is finished this flow should be retriggered.
  7771. */
  7772. if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
  7773. wpa_dbg(wpa_s, MSG_DEBUG,
  7774. "P2P: Not initiating a GO frequency change - CSA is in progress");
  7775. return;
  7776. }
  7777. if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
  7778. timeout = P2P_GO_FREQ_CHANGE_TIME;
  7779. else
  7780. timeout = 0;
  7781. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
  7782. freq, timeout);
  7783. eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
  7784. eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
  7785. }
  7786. static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
  7787. struct wpa_used_freq_data *freqs,
  7788. unsigned int num,
  7789. enum wpas_p2p_channel_update_trig trig)
  7790. {
  7791. struct wpa_supplicant *ifs;
  7792. eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
  7793. NULL);
  7794. /*
  7795. * Travers all the radio interfaces, and for each GO interface, check
  7796. * if there is a need to move the GO from the frequency it is using,
  7797. * or in case the frequency is valid again, cancel the evacuation flow.
  7798. */
  7799. dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
  7800. radio_list) {
  7801. if (ifs->current_ssid == NULL ||
  7802. ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
  7803. continue;
  7804. /*
  7805. * The GO was just started or completed channel switch, no need
  7806. * to move it.
  7807. */
  7808. if (wpa_s == ifs &&
  7809. (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
  7810. trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
  7811. wpa_dbg(wpa_s, MSG_DEBUG,
  7812. "P2P: GO move - schedule re-consideration");
  7813. eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
  7814. wpas_p2p_reconsider_moving_go,
  7815. wpa_s, NULL);
  7816. continue;
  7817. }
  7818. wpas_p2p_consider_moving_one_go(ifs, freqs, num);
  7819. }
  7820. }
  7821. void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
  7822. {
  7823. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  7824. return;
  7825. wpas_p2p_update_channel_list(wpa_s,
  7826. WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
  7827. }
  7828. void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
  7829. {
  7830. if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
  7831. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
  7832. "the management interface is being removed");
  7833. wpas_p2p_deinit_global(wpa_s->global);
  7834. }
  7835. }
  7836. void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
  7837. {
  7838. if (wpa_s->ap_iface->bss)
  7839. wpa_s->ap_iface->bss[0]->p2p_group = NULL;
  7840. wpas_p2p_group_deinit(wpa_s);
  7841. }