p2p_supplicant.c 251 KB

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