p2p_supplicant.c 242 KB

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