p2p_supplicant.c 258 KB

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