p2p_supplicant.c 262 KB

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