p2p_supplicant.c 253 KB

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