p2p_supplicant.c 245 KB

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