p2p_supplicant.c 259 KB

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