p2p_supplicant.c 260 KB

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