p2p_supplicant.c 246 KB

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