p2p_supplicant.c 185 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685
  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/p2p_hostapd.h"
  22. #include "eapol_supp/eapol_supp_sm.h"
  23. #include "rsn_supp/wpa.h"
  24. #include "wpa_supplicant_i.h"
  25. #include "driver_i.h"
  26. #include "ap.h"
  27. #include "config_ssid.h"
  28. #include "config.h"
  29. #include "notify.h"
  30. #include "scan.h"
  31. #include "bss.h"
  32. #include "offchannel.h"
  33. #include "wps_supplicant.h"
  34. #include "p2p_supplicant.h"
  35. #include "wifi_display.h"
  36. /*
  37. * How many times to try to scan to find the GO before giving up on join
  38. * request.
  39. */
  40. #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
  41. #define P2P_AUTO_PD_SCAN_ATTEMPTS 5
  42. #ifndef P2P_MAX_CLIENT_IDLE
  43. /*
  44. * How many seconds to try to reconnect to the GO when connection in P2P client
  45. * role has been lost.
  46. */
  47. #define P2P_MAX_CLIENT_IDLE 10
  48. #endif /* P2P_MAX_CLIENT_IDLE */
  49. #ifndef P2P_MAX_INITIAL_CONN_WAIT
  50. /*
  51. * How many seconds to wait for initial 4-way handshake to get completed after
  52. * WPS provisioning step.
  53. */
  54. #define P2P_MAX_INITIAL_CONN_WAIT 10
  55. #endif /* P2P_MAX_INITIAL_CONN_WAIT */
  56. #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
  57. /*
  58. * How many seconds to wait for initial 4-way handshake to get completed after
  59. * WPS provisioning step on the GO. This controls the extra time the P2P
  60. * operation is considered to be in progress (e.g., to delay other scans) after
  61. * WPS provisioning has been completed on the GO during group formation.
  62. */
  63. #define P2P_MAX_INITIAL_CONN_WAIT_GO 10
  64. #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
  65. #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
  66. /*
  67. * How many seconds to wait for initial 4-way handshake to get completed after
  68. * re-invocation of a persistent group on the GO when the client is expected
  69. * to connect automatically (no user interaction).
  70. */
  71. #define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
  72. #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
  73. #ifndef P2P_CONCURRENT_SEARCH_DELAY
  74. #define P2P_CONCURRENT_SEARCH_DELAY 500
  75. #endif /* P2P_CONCURRENT_SEARCH_DELAY */
  76. #define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
  77. enum p2p_group_removal_reason {
  78. P2P_GROUP_REMOVAL_UNKNOWN,
  79. P2P_GROUP_REMOVAL_SILENT,
  80. P2P_GROUP_REMOVAL_FORMATION_FAILED,
  81. P2P_GROUP_REMOVAL_REQUESTED,
  82. P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
  83. P2P_GROUP_REMOVAL_UNAVAILABLE,
  84. P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
  85. P2P_GROUP_REMOVAL_PSK_FAILURE,
  86. P2P_GROUP_REMOVAL_FREQ_CONFLICT
  87. };
  88. static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
  89. static struct wpa_supplicant *
  90. wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
  91. int go);
  92. static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
  93. static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
  94. static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
  95. static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
  96. const u8 *dev_addr, enum p2p_wps_method wps_method,
  97. int auto_join);
  98. static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
  99. static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
  100. static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
  101. static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
  102. static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
  103. void *timeout_ctx);
  104. static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
  105. static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
  106. int group_added);
  107. static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
  108. /*
  109. * Get the number of concurrent channels that the HW can operate, but that are
  110. * currently not in use by any of the wpa_supplicant interfaces.
  111. */
  112. static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
  113. {
  114. int *freqs;
  115. int num, unused;
  116. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  117. if (!freqs)
  118. return -1;
  119. num = get_shared_radio_freqs(wpa_s, freqs,
  120. wpa_s->num_multichan_concurrent);
  121. os_free(freqs);
  122. unused = wpa_s->num_multichan_concurrent - num;
  123. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
  124. return unused;
  125. }
  126. /*
  127. * Get the frequencies that are currently in use by one or more of the virtual
  128. * interfaces, and that are also valid for P2P operation.
  129. */
  130. static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
  131. int *p2p_freqs, unsigned int len)
  132. {
  133. int *freqs;
  134. unsigned int num, i, j;
  135. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  136. if (!freqs)
  137. return -1;
  138. num = get_shared_radio_freqs(wpa_s, freqs,
  139. wpa_s->num_multichan_concurrent);
  140. os_memset(p2p_freqs, 0, sizeof(int) * len);
  141. for (i = 0, j = 0; i < num && j < len; i++) {
  142. if (p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
  143. p2p_freqs[j++] = freqs[i];
  144. }
  145. os_free(freqs);
  146. dump_freq_array(wpa_s, "valid for P2P", p2p_freqs, j);
  147. return j;
  148. }
  149. static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
  150. int freq)
  151. {
  152. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  153. return;
  154. if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
  155. freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
  156. wpas_p2p_num_unused_channels(wpa_s) > 0) {
  157. wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
  158. freq);
  159. freq = 0;
  160. }
  161. p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
  162. }
  163. static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
  164. struct wpa_scan_results *scan_res)
  165. {
  166. size_t i;
  167. if (wpa_s->p2p_scan_work) {
  168. struct wpa_radio_work *work = wpa_s->p2p_scan_work;
  169. wpa_s->p2p_scan_work = NULL;
  170. radio_work_done(work);
  171. }
  172. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  173. return;
  174. wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
  175. (int) scan_res->num);
  176. for (i = 0; i < scan_res->num; i++) {
  177. struct wpa_scan_res *bss = scan_res->res[i];
  178. struct os_reltime time_tmp_age, entry_ts;
  179. const u8 *ies;
  180. size_t ies_len;
  181. time_tmp_age.sec = bss->age / 1000;
  182. time_tmp_age.usec = (bss->age % 1000) * 1000;
  183. os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
  184. ies = (const u8 *) (bss + 1);
  185. ies_len = bss->ie_len;
  186. if (bss->beacon_ie_len > 0 &&
  187. !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
  188. wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
  189. wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
  190. MACSTR, MAC2STR(bss->bssid));
  191. ies = ies + ies_len;
  192. ies_len = bss->beacon_ie_len;
  193. }
  194. if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
  195. bss->freq, &entry_ts, bss->level,
  196. ies, ies_len) > 0)
  197. break;
  198. }
  199. p2p_scan_res_handled(wpa_s->global->p2p);
  200. }
  201. static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
  202. {
  203. struct wpa_supplicant *wpa_s = work->wpa_s;
  204. struct wpa_driver_scan_params *params = work->ctx;
  205. int ret;
  206. if (deinit) {
  207. wpa_scan_free_params(params);
  208. return;
  209. }
  210. ret = wpa_drv_scan(wpa_s, params);
  211. wpa_scan_free_params(params);
  212. work->ctx = NULL;
  213. if (ret) {
  214. radio_work_done(work);
  215. return;
  216. }
  217. os_get_reltime(&wpa_s->scan_trigger_time);
  218. wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
  219. wpa_s->own_scan_requested = 1;
  220. wpa_s->p2p_scan_work = work;
  221. }
  222. static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
  223. unsigned int num_req_dev_types,
  224. const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
  225. {
  226. struct wpa_supplicant *wpa_s = ctx;
  227. struct wpa_driver_scan_params *params = NULL;
  228. struct wpabuf *wps_ie, *ies;
  229. size_t ielen;
  230. u8 *n;
  231. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  232. return -1;
  233. if (wpa_s->p2p_scan_work) {
  234. wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
  235. return -1;
  236. }
  237. params = os_zalloc(sizeof(*params));
  238. if (params == NULL)
  239. return -1;
  240. /* P2P Wildcard SSID */
  241. params->num_ssids = 1;
  242. n = os_malloc(P2P_WILDCARD_SSID_LEN);
  243. if (n == NULL)
  244. goto fail;
  245. os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
  246. params->ssids[0].ssid = n;
  247. params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
  248. wpa_s->wps->dev.p2p = 1;
  249. wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
  250. wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
  251. num_req_dev_types, req_dev_types);
  252. if (wps_ie == NULL)
  253. goto fail;
  254. ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
  255. ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
  256. if (ies == NULL) {
  257. wpabuf_free(wps_ie);
  258. goto fail;
  259. }
  260. wpabuf_put_buf(ies, wps_ie);
  261. wpabuf_free(wps_ie);
  262. p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
  263. params->p2p_probe = 1;
  264. n = os_malloc(wpabuf_len(ies));
  265. if (n == NULL) {
  266. wpabuf_free(ies);
  267. goto fail;
  268. }
  269. os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
  270. params->extra_ies = n;
  271. params->extra_ies_len = wpabuf_len(ies);
  272. wpabuf_free(ies);
  273. switch (type) {
  274. case P2P_SCAN_SOCIAL:
  275. params->freqs = os_malloc(4 * sizeof(int));
  276. if (params->freqs == NULL)
  277. goto fail;
  278. params->freqs[0] = 2412;
  279. params->freqs[1] = 2437;
  280. params->freqs[2] = 2462;
  281. params->freqs[3] = 0;
  282. break;
  283. case P2P_SCAN_FULL:
  284. break;
  285. case P2P_SCAN_SOCIAL_PLUS_ONE:
  286. params->freqs = os_malloc(5 * sizeof(int));
  287. if (params->freqs == NULL)
  288. goto fail;
  289. params->freqs[0] = 2412;
  290. params->freqs[1] = 2437;
  291. params->freqs[2] = 2462;
  292. params->freqs[3] = freq;
  293. params->freqs[4] = 0;
  294. break;
  295. }
  296. radio_remove_unstarted_work(wpa_s, "p2p-scan");
  297. if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
  298. params) < 0)
  299. goto fail;
  300. return 0;
  301. fail:
  302. wpa_scan_free_params(params);
  303. return -1;
  304. }
  305. static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
  306. {
  307. switch (p2p_group_interface) {
  308. case P2P_GROUP_INTERFACE_PENDING:
  309. return WPA_IF_P2P_GROUP;
  310. case P2P_GROUP_INTERFACE_GO:
  311. return WPA_IF_P2P_GO;
  312. case P2P_GROUP_INTERFACE_CLIENT:
  313. return WPA_IF_P2P_CLIENT;
  314. }
  315. return WPA_IF_P2P_GROUP;
  316. }
  317. static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
  318. const u8 *ssid,
  319. size_t ssid_len, int *go)
  320. {
  321. struct wpa_ssid *s;
  322. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  323. for (s = wpa_s->conf->ssid; s; s = s->next) {
  324. if (s->disabled != 0 || !s->p2p_group ||
  325. s->ssid_len != ssid_len ||
  326. os_memcmp(ssid, s->ssid, ssid_len) != 0)
  327. continue;
  328. if (s->mode == WPAS_MODE_P2P_GO &&
  329. s != wpa_s->current_ssid)
  330. continue;
  331. if (go)
  332. *go = s->mode == WPAS_MODE_P2P_GO;
  333. return wpa_s;
  334. }
  335. }
  336. return NULL;
  337. }
  338. static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
  339. enum p2p_group_removal_reason removal_reason)
  340. {
  341. struct wpa_ssid *ssid;
  342. char *gtype;
  343. const char *reason;
  344. ssid = wpa_s->current_ssid;
  345. if (ssid == NULL) {
  346. /*
  347. * The current SSID was not known, but there may still be a
  348. * pending P2P group interface waiting for provisioning or a
  349. * P2P group that is trying to reconnect.
  350. */
  351. ssid = wpa_s->conf->ssid;
  352. while (ssid) {
  353. if (ssid->p2p_group && ssid->disabled != 2)
  354. break;
  355. ssid = ssid->next;
  356. }
  357. if (ssid == NULL &&
  358. wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
  359. {
  360. wpa_printf(MSG_ERROR, "P2P: P2P group interface "
  361. "not found");
  362. return -1;
  363. }
  364. }
  365. if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
  366. gtype = "GO";
  367. else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
  368. (ssid && ssid->mode == WPAS_MODE_INFRA)) {
  369. wpa_s->reassociate = 0;
  370. wpa_s->disconnected = 1;
  371. wpa_supplicant_deauthenticate(wpa_s,
  372. WLAN_REASON_DEAUTH_LEAVING);
  373. gtype = "client";
  374. } else
  375. gtype = "GO";
  376. if (wpa_s->cross_connect_in_use) {
  377. wpa_s->cross_connect_in_use = 0;
  378. wpa_msg_global(wpa_s->parent, MSG_INFO,
  379. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  380. wpa_s->ifname, wpa_s->cross_connect_uplink);
  381. }
  382. switch (removal_reason) {
  383. case P2P_GROUP_REMOVAL_REQUESTED:
  384. reason = " reason=REQUESTED";
  385. break;
  386. case P2P_GROUP_REMOVAL_FORMATION_FAILED:
  387. reason = " reason=FORMATION_FAILED";
  388. break;
  389. case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
  390. reason = " reason=IDLE";
  391. break;
  392. case P2P_GROUP_REMOVAL_UNAVAILABLE:
  393. reason = " reason=UNAVAILABLE";
  394. break;
  395. case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
  396. reason = " reason=GO_ENDING_SESSION";
  397. break;
  398. case P2P_GROUP_REMOVAL_PSK_FAILURE:
  399. reason = " reason=PSK_FAILURE";
  400. break;
  401. case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
  402. reason = " reason=FREQ_CONFLICT";
  403. break;
  404. default:
  405. reason = "";
  406. break;
  407. }
  408. if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
  409. wpa_msg_global(wpa_s->parent, MSG_INFO,
  410. P2P_EVENT_GROUP_REMOVED "%s %s%s",
  411. wpa_s->ifname, gtype, reason);
  412. }
  413. if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
  414. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
  415. if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
  416. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
  417. if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  418. wpa_s->parent, NULL) > 0) {
  419. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
  420. "timeout");
  421. wpa_s->p2p_in_provisioning = 0;
  422. }
  423. /*
  424. * Make sure wait for the first client does not remain active after the
  425. * group has been removed.
  426. */
  427. wpa_s->global->p2p_go_wait_client.sec = 0;
  428. if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
  429. wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
  430. if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
  431. struct wpa_global *global;
  432. char *ifname;
  433. enum wpa_driver_if_type type;
  434. wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
  435. wpa_s->ifname);
  436. global = wpa_s->global;
  437. ifname = os_strdup(wpa_s->ifname);
  438. type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
  439. wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
  440. wpa_s = global->ifaces;
  441. if (wpa_s && ifname)
  442. wpa_drv_if_remove(wpa_s, type, ifname);
  443. os_free(ifname);
  444. return 1;
  445. }
  446. if (!wpa_s->p2p_go_group_formation_completed) {
  447. wpa_s->global->p2p_group_formation = NULL;
  448. wpa_s->p2p_in_provisioning = 0;
  449. }
  450. wpa_s->show_group_started = 0;
  451. os_free(wpa_s->go_params);
  452. wpa_s->go_params = NULL;
  453. wpa_s->waiting_presence_resp = 0;
  454. wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
  455. if (ssid && (ssid->p2p_group ||
  456. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
  457. (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
  458. int id = ssid->id;
  459. if (ssid == wpa_s->current_ssid) {
  460. wpa_sm_set_config(wpa_s->wpa, NULL);
  461. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  462. wpa_s->current_ssid = NULL;
  463. }
  464. /*
  465. * Networks objects created during any P2P activities are not
  466. * exposed out as they might/will confuse certain non-P2P aware
  467. * applications since these network objects won't behave like
  468. * regular ones.
  469. *
  470. * Likewise, we don't send out network removed signals for such
  471. * network objects.
  472. */
  473. wpa_config_remove_network(wpa_s->conf, id);
  474. wpa_supplicant_clear_status(wpa_s);
  475. wpa_supplicant_cancel_sched_scan(wpa_s);
  476. } else {
  477. wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
  478. "found");
  479. }
  480. if (wpa_s->ap_iface)
  481. wpa_supplicant_ap_deinit(wpa_s);
  482. else
  483. wpa_drv_deinit_p2p_cli(wpa_s);
  484. return 0;
  485. }
  486. static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
  487. u8 *go_dev_addr,
  488. const u8 *ssid, size_t ssid_len)
  489. {
  490. struct wpa_bss *bss;
  491. const u8 *bssid;
  492. struct wpabuf *p2p;
  493. u8 group_capab;
  494. const u8 *addr;
  495. if (wpa_s->go_params)
  496. bssid = wpa_s->go_params->peer_interface_addr;
  497. else
  498. bssid = wpa_s->bssid;
  499. bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
  500. if (bss == NULL) {
  501. u8 iface_addr[ETH_ALEN];
  502. if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
  503. iface_addr) == 0)
  504. bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
  505. }
  506. if (bss == NULL) {
  507. wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
  508. "group is persistent - BSS " MACSTR " not found",
  509. MAC2STR(bssid));
  510. return 0;
  511. }
  512. p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  513. if (p2p == NULL)
  514. p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
  515. P2P_IE_VENDOR_TYPE);
  516. if (p2p == NULL) {
  517. wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
  518. "group is persistent - BSS " MACSTR
  519. " did not include P2P IE", MAC2STR(bssid));
  520. wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
  521. (u8 *) (bss + 1), bss->ie_len);
  522. wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
  523. ((u8 *) bss + 1) + bss->ie_len,
  524. bss->beacon_ie_len);
  525. return 0;
  526. }
  527. group_capab = p2p_get_group_capab(p2p);
  528. addr = p2p_get_go_dev_addr(p2p);
  529. wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
  530. "group_capab=0x%x", group_capab);
  531. if (addr) {
  532. os_memcpy(go_dev_addr, addr, ETH_ALEN);
  533. wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
  534. MAC2STR(addr));
  535. } else
  536. os_memset(go_dev_addr, 0, ETH_ALEN);
  537. wpabuf_free(p2p);
  538. wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
  539. "go_dev_addr=" MACSTR,
  540. MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
  541. return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
  542. }
  543. static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
  544. struct wpa_ssid *ssid,
  545. const u8 *go_dev_addr)
  546. {
  547. struct wpa_ssid *s;
  548. int changed = 0;
  549. wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
  550. "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
  551. for (s = wpa_s->conf->ssid; s; s = s->next) {
  552. if (s->disabled == 2 &&
  553. os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
  554. s->ssid_len == ssid->ssid_len &&
  555. os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
  556. break;
  557. }
  558. if (s) {
  559. wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
  560. "entry");
  561. if (ssid->passphrase && !s->passphrase)
  562. changed = 1;
  563. else if (ssid->passphrase && s->passphrase &&
  564. os_strcmp(ssid->passphrase, s->passphrase) != 0)
  565. changed = 1;
  566. } else {
  567. wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
  568. "entry");
  569. changed = 1;
  570. s = wpa_config_add_network(wpa_s->conf);
  571. if (s == NULL)
  572. return -1;
  573. /*
  574. * Instead of network_added we emit persistent_group_added
  575. * notification. Also to keep the defense checks in
  576. * persistent_group obj registration method, we set the
  577. * relevant flags in s to designate it as a persistent group.
  578. */
  579. s->p2p_group = 1;
  580. s->p2p_persistent_group = 1;
  581. wpas_notify_persistent_group_added(wpa_s, s);
  582. wpa_config_set_network_defaults(s);
  583. }
  584. s->p2p_group = 1;
  585. s->p2p_persistent_group = 1;
  586. s->disabled = 2;
  587. s->bssid_set = 1;
  588. os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
  589. s->mode = ssid->mode;
  590. s->auth_alg = WPA_AUTH_ALG_OPEN;
  591. s->key_mgmt = WPA_KEY_MGMT_PSK;
  592. s->proto = WPA_PROTO_RSN;
  593. s->pairwise_cipher = WPA_CIPHER_CCMP;
  594. s->export_keys = 1;
  595. if (ssid->passphrase) {
  596. os_free(s->passphrase);
  597. s->passphrase = os_strdup(ssid->passphrase);
  598. }
  599. if (ssid->psk_set) {
  600. s->psk_set = 1;
  601. os_memcpy(s->psk, ssid->psk, 32);
  602. }
  603. if (s->passphrase && !s->psk_set)
  604. wpa_config_update_psk(s);
  605. if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
  606. os_free(s->ssid);
  607. s->ssid = os_malloc(ssid->ssid_len);
  608. }
  609. if (s->ssid) {
  610. s->ssid_len = ssid->ssid_len;
  611. os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
  612. }
  613. if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
  614. dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
  615. wpa_s->global->add_psk = NULL;
  616. changed = 1;
  617. }
  618. #ifndef CONFIG_NO_CONFIG_WRITE
  619. if (changed && wpa_s->conf->update_config &&
  620. wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  621. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  622. }
  623. #endif /* CONFIG_NO_CONFIG_WRITE */
  624. return s->id;
  625. }
  626. static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
  627. const u8 *addr)
  628. {
  629. struct wpa_ssid *ssid, *s;
  630. u8 *n;
  631. size_t i;
  632. int found = 0;
  633. ssid = wpa_s->current_ssid;
  634. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  635. !ssid->p2p_persistent_group)
  636. return;
  637. for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
  638. if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
  639. continue;
  640. if (s->ssid_len == ssid->ssid_len &&
  641. os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
  642. break;
  643. }
  644. if (s == NULL)
  645. return;
  646. for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
  647. if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
  648. ETH_ALEN) != 0)
  649. continue;
  650. if (i == s->num_p2p_clients - 1)
  651. return; /* already the most recent entry */
  652. /* move the entry to mark it most recent */
  653. os_memmove(s->p2p_client_list + i * ETH_ALEN,
  654. s->p2p_client_list + (i + 1) * ETH_ALEN,
  655. (s->num_p2p_clients - i - 1) * ETH_ALEN);
  656. os_memcpy(s->p2p_client_list +
  657. (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
  658. found = 1;
  659. break;
  660. }
  661. if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
  662. n = os_realloc_array(s->p2p_client_list,
  663. s->num_p2p_clients + 1, ETH_ALEN);
  664. if (n == NULL)
  665. return;
  666. os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
  667. s->p2p_client_list = n;
  668. s->num_p2p_clients++;
  669. } else if (!found) {
  670. /* Not enough room for an additional entry - drop the oldest
  671. * entry */
  672. os_memmove(s->p2p_client_list,
  673. s->p2p_client_list + ETH_ALEN,
  674. (s->num_p2p_clients - 1) * ETH_ALEN);
  675. os_memcpy(s->p2p_client_list +
  676. (s->num_p2p_clients - 1) * ETH_ALEN,
  677. addr, ETH_ALEN);
  678. }
  679. #ifndef CONFIG_NO_CONFIG_WRITE
  680. if (wpa_s->parent->conf->update_config &&
  681. wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
  682. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  683. #endif /* CONFIG_NO_CONFIG_WRITE */
  684. }
  685. static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
  686. int success)
  687. {
  688. struct wpa_ssid *ssid;
  689. const char *ssid_txt;
  690. int client;
  691. int persistent;
  692. u8 go_dev_addr[ETH_ALEN];
  693. int network_id = -1;
  694. /*
  695. * This callback is likely called for the main interface. Update wpa_s
  696. * to use the group interface if a new interface was created for the
  697. * group.
  698. */
  699. if (wpa_s->global->p2p_group_formation)
  700. wpa_s = wpa_s->global->p2p_group_formation;
  701. if (wpa_s->p2p_go_group_formation_completed) {
  702. wpa_s->global->p2p_group_formation = NULL;
  703. wpa_s->p2p_in_provisioning = 0;
  704. }
  705. if (!success) {
  706. wpa_msg_global(wpa_s->parent, MSG_INFO,
  707. P2P_EVENT_GROUP_FORMATION_FAILURE);
  708. wpas_p2p_group_delete(wpa_s,
  709. P2P_GROUP_REMOVAL_FORMATION_FAILED);
  710. return;
  711. }
  712. wpa_msg_global(wpa_s->parent, MSG_INFO,
  713. P2P_EVENT_GROUP_FORMATION_SUCCESS);
  714. ssid = wpa_s->current_ssid;
  715. if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
  716. ssid->mode = WPAS_MODE_P2P_GO;
  717. p2p_group_notif_formation_done(wpa_s->p2p_group);
  718. wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
  719. }
  720. persistent = 0;
  721. if (ssid) {
  722. ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
  723. client = ssid->mode == WPAS_MODE_INFRA;
  724. if (ssid->mode == WPAS_MODE_P2P_GO) {
  725. persistent = ssid->p2p_persistent_group;
  726. os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
  727. ETH_ALEN);
  728. } else
  729. persistent = wpas_p2p_persistent_group(wpa_s,
  730. go_dev_addr,
  731. ssid->ssid,
  732. ssid->ssid_len);
  733. } else {
  734. ssid_txt = "";
  735. client = wpa_s->p2p_group_interface ==
  736. P2P_GROUP_INTERFACE_CLIENT;
  737. os_memset(go_dev_addr, 0, ETH_ALEN);
  738. }
  739. wpa_s->show_group_started = 0;
  740. if (client) {
  741. /*
  742. * Indicate event only after successfully completed 4-way
  743. * handshake, i.e., when the interface is ready for data
  744. * packets.
  745. */
  746. wpa_s->show_group_started = 1;
  747. } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
  748. char psk[65];
  749. wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
  750. wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  751. "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
  752. MACSTR "%s",
  753. wpa_s->ifname, ssid_txt, ssid->frequency, psk,
  754. MAC2STR(go_dev_addr),
  755. persistent ? " [PERSISTENT]" : "");
  756. wpas_p2p_cross_connect_setup(wpa_s);
  757. wpas_p2p_set_group_idle_timeout(wpa_s);
  758. } else {
  759. wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  760. "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
  761. "go_dev_addr=" MACSTR "%s",
  762. wpa_s->ifname, ssid_txt,
  763. ssid ? ssid->frequency : 0,
  764. ssid && ssid->passphrase ? ssid->passphrase : "",
  765. MAC2STR(go_dev_addr),
  766. persistent ? " [PERSISTENT]" : "");
  767. wpas_p2p_cross_connect_setup(wpa_s);
  768. wpas_p2p_set_group_idle_timeout(wpa_s);
  769. }
  770. if (persistent)
  771. network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
  772. ssid, go_dev_addr);
  773. else {
  774. os_free(wpa_s->global->add_psk);
  775. wpa_s->global->add_psk = NULL;
  776. }
  777. if (network_id < 0 && ssid)
  778. network_id = ssid->id;
  779. if (!client) {
  780. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
  781. os_get_reltime(&wpa_s->global->p2p_go_wait_client);
  782. }
  783. }
  784. static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
  785. unsigned int freq,
  786. const u8 *dst, const u8 *src,
  787. const u8 *bssid,
  788. const u8 *data, size_t data_len,
  789. enum offchannel_send_action_result
  790. result)
  791. {
  792. enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
  793. if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
  794. return;
  795. switch (result) {
  796. case OFFCHANNEL_SEND_ACTION_SUCCESS:
  797. res = P2P_SEND_ACTION_SUCCESS;
  798. break;
  799. case OFFCHANNEL_SEND_ACTION_NO_ACK:
  800. res = P2P_SEND_ACTION_NO_ACK;
  801. break;
  802. case OFFCHANNEL_SEND_ACTION_FAILED:
  803. res = P2P_SEND_ACTION_FAILED;
  804. break;
  805. }
  806. p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
  807. if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
  808. wpa_s->pending_pd_before_join &&
  809. (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
  810. os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
  811. wpa_s->p2p_fallback_to_go_neg) {
  812. wpa_s->pending_pd_before_join = 0;
  813. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
  814. "during p2p_connect-auto");
  815. wpas_p2p_fallback_to_go_neg(wpa_s, 0);
  816. return;
  817. }
  818. }
  819. static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
  820. const u8 *src, const u8 *bssid, const u8 *buf,
  821. size_t len, unsigned int wait_time)
  822. {
  823. struct wpa_supplicant *wpa_s = ctx;
  824. return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
  825. wait_time,
  826. wpas_p2p_send_action_tx_status, 1);
  827. }
  828. static void wpas_send_action_done(void *ctx)
  829. {
  830. struct wpa_supplicant *wpa_s = ctx;
  831. offchannel_send_action_done(wpa_s);
  832. }
  833. static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
  834. struct p2p_go_neg_results *params)
  835. {
  836. if (wpa_s->go_params == NULL) {
  837. wpa_s->go_params = os_malloc(sizeof(*params));
  838. if (wpa_s->go_params == NULL)
  839. return -1;
  840. }
  841. os_memcpy(wpa_s->go_params, params, sizeof(*params));
  842. return 0;
  843. }
  844. static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
  845. struct p2p_go_neg_results *res)
  846. {
  847. wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
  848. MAC2STR(res->peer_interface_addr));
  849. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
  850. res->ssid, res->ssid_len);
  851. wpa_supplicant_ap_deinit(wpa_s);
  852. wpas_copy_go_neg_results(wpa_s, res);
  853. if (res->wps_method == WPS_PBC)
  854. wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
  855. else {
  856. u16 dev_pw_id = DEV_PW_DEFAULT;
  857. if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
  858. dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
  859. wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
  860. wpa_s->p2p_pin, 1, dev_pw_id);
  861. }
  862. }
  863. static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
  864. struct wpa_ssid *ssid)
  865. {
  866. struct wpa_ssid *persistent;
  867. struct psk_list_entry *psk;
  868. struct hostapd_data *hapd;
  869. if (!wpa_s->ap_iface)
  870. return;
  871. persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
  872. ssid->ssid_len);
  873. if (persistent == NULL)
  874. return;
  875. hapd = wpa_s->ap_iface->bss[0];
  876. dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
  877. list) {
  878. struct hostapd_wpa_psk *hpsk;
  879. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
  880. MACSTR " psk=%d",
  881. MAC2STR(psk->addr), psk->p2p);
  882. hpsk = os_zalloc(sizeof(*hpsk));
  883. if (hpsk == NULL)
  884. break;
  885. os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
  886. if (psk->p2p)
  887. os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
  888. else
  889. os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
  890. hpsk->next = hapd->conf->ssid.wpa_psk;
  891. hapd->conf->ssid.wpa_psk = hpsk;
  892. }
  893. }
  894. static void p2p_go_configured(void *ctx, void *data)
  895. {
  896. struct wpa_supplicant *wpa_s = ctx;
  897. struct p2p_go_neg_results *params = data;
  898. struct wpa_ssid *ssid;
  899. int network_id = -1;
  900. ssid = wpa_s->current_ssid;
  901. if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
  902. wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
  903. if (wpa_s->global->p2p_group_formation == wpa_s)
  904. wpa_s->global->p2p_group_formation = NULL;
  905. if (os_strlen(params->passphrase) > 0) {
  906. wpa_msg_global(wpa_s->parent, MSG_INFO,
  907. P2P_EVENT_GROUP_STARTED
  908. "%s GO ssid=\"%s\" freq=%d "
  909. "passphrase=\"%s\" go_dev_addr=" MACSTR
  910. "%s", wpa_s->ifname,
  911. wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
  912. ssid->frequency, params->passphrase,
  913. MAC2STR(wpa_s->global->p2p_dev_addr),
  914. params->persistent_group ?
  915. " [PERSISTENT]" : "");
  916. } else {
  917. char psk[65];
  918. wpa_snprintf_hex(psk, sizeof(psk), params->psk,
  919. sizeof(params->psk));
  920. wpa_msg_global(wpa_s->parent, MSG_INFO,
  921. P2P_EVENT_GROUP_STARTED
  922. "%s GO ssid=\"%s\" freq=%d psk=%s "
  923. "go_dev_addr=" MACSTR "%s",
  924. wpa_s->ifname,
  925. wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
  926. ssid->frequency, psk,
  927. MAC2STR(wpa_s->global->p2p_dev_addr),
  928. params->persistent_group ?
  929. " [PERSISTENT]" : "");
  930. }
  931. os_get_reltime(&wpa_s->global->p2p_go_wait_client);
  932. if (params->persistent_group) {
  933. network_id = wpas_p2p_store_persistent_group(
  934. wpa_s->parent, ssid,
  935. wpa_s->global->p2p_dev_addr);
  936. wpas_p2p_add_psk_list(wpa_s, ssid);
  937. }
  938. if (network_id < 0)
  939. network_id = ssid->id;
  940. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
  941. wpas_p2p_cross_connect_setup(wpa_s);
  942. wpas_p2p_set_group_idle_timeout(wpa_s);
  943. if (wpa_s->p2p_first_connection_timeout) {
  944. wpa_dbg(wpa_s, MSG_DEBUG,
  945. "P2P: Start group formation timeout of %d seconds until first data connection on GO",
  946. wpa_s->p2p_first_connection_timeout);
  947. wpa_s->p2p_go_group_formation_completed = 0;
  948. wpa_s->global->p2p_group_formation = wpa_s;
  949. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  950. wpa_s->parent, NULL);
  951. eloop_register_timeout(
  952. wpa_s->p2p_first_connection_timeout, 0,
  953. wpas_p2p_group_formation_timeout,
  954. wpa_s->parent, NULL);
  955. }
  956. return;
  957. }
  958. wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
  959. if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
  960. params->peer_interface_addr)) {
  961. wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
  962. "filtering");
  963. return;
  964. }
  965. if (params->wps_method == WPS_PBC)
  966. wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
  967. params->peer_device_addr);
  968. else if (wpa_s->p2p_pin[0])
  969. wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
  970. wpa_s->p2p_pin, NULL, 0, 0);
  971. os_free(wpa_s->go_params);
  972. wpa_s->go_params = NULL;
  973. }
  974. static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
  975. struct p2p_go_neg_results *params,
  976. int group_formation)
  977. {
  978. struct wpa_ssid *ssid;
  979. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
  980. if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
  981. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
  982. "results");
  983. return;
  984. }
  985. ssid = wpa_config_add_network(wpa_s->conf);
  986. if (ssid == NULL) {
  987. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
  988. return;
  989. }
  990. wpa_s->show_group_started = 0;
  991. wpa_config_set_network_defaults(ssid);
  992. ssid->temporary = 1;
  993. ssid->p2p_group = 1;
  994. ssid->p2p_persistent_group = params->persistent_group;
  995. ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
  996. WPAS_MODE_P2P_GO;
  997. ssid->frequency = params->freq;
  998. ssid->ht40 = params->ht40;
  999. ssid->vht = params->vht;
  1000. ssid->ssid = os_zalloc(params->ssid_len + 1);
  1001. if (ssid->ssid) {
  1002. os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
  1003. ssid->ssid_len = params->ssid_len;
  1004. }
  1005. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  1006. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  1007. ssid->proto = WPA_PROTO_RSN;
  1008. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  1009. if (os_strlen(params->passphrase) > 0) {
  1010. ssid->passphrase = os_strdup(params->passphrase);
  1011. if (ssid->passphrase == NULL) {
  1012. wpa_msg_global(wpa_s, MSG_ERROR,
  1013. "P2P: Failed to copy passphrase for GO");
  1014. wpa_config_remove_network(wpa_s->conf, ssid->id);
  1015. return;
  1016. }
  1017. } else
  1018. ssid->passphrase = NULL;
  1019. ssid->psk_set = params->psk_set;
  1020. if (ssid->psk_set)
  1021. os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
  1022. else if (ssid->passphrase)
  1023. wpa_config_update_psk(ssid);
  1024. ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
  1025. wpa_s->ap_configured_cb = p2p_go_configured;
  1026. wpa_s->ap_configured_cb_ctx = wpa_s;
  1027. wpa_s->ap_configured_cb_data = wpa_s->go_params;
  1028. wpa_s->connect_without_scan = ssid;
  1029. wpa_s->reassociate = 1;
  1030. wpa_s->disconnected = 0;
  1031. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
  1032. "start GO)");
  1033. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1034. }
  1035. static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
  1036. const struct wpa_supplicant *src)
  1037. {
  1038. struct wpa_config *d;
  1039. const struct wpa_config *s;
  1040. d = dst->conf;
  1041. s = src->conf;
  1042. #define C(n) if (s->n) d->n = os_strdup(s->n)
  1043. C(device_name);
  1044. C(manufacturer);
  1045. C(model_name);
  1046. C(model_number);
  1047. C(serial_number);
  1048. C(config_methods);
  1049. #undef C
  1050. os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
  1051. os_memcpy(d->sec_device_type, s->sec_device_type,
  1052. sizeof(d->sec_device_type));
  1053. d->num_sec_device_types = s->num_sec_device_types;
  1054. d->p2p_group_idle = s->p2p_group_idle;
  1055. d->p2p_intra_bss = s->p2p_intra_bss;
  1056. d->persistent_reconnect = s->persistent_reconnect;
  1057. d->max_num_sta = s->max_num_sta;
  1058. d->pbc_in_m1 = s->pbc_in_m1;
  1059. d->ignore_old_scan_res = s->ignore_old_scan_res;
  1060. d->beacon_int = s->beacon_int;
  1061. d->disassoc_low_ack = s->disassoc_low_ack;
  1062. d->disable_scan_offload = s->disable_scan_offload;
  1063. }
  1064. static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
  1065. char *ifname, size_t len)
  1066. {
  1067. char *ifname_ptr = wpa_s->ifname;
  1068. if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
  1069. os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
  1070. ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
  1071. }
  1072. os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
  1073. if (os_strlen(ifname) >= IFNAMSIZ &&
  1074. os_strlen(wpa_s->ifname) < IFNAMSIZ) {
  1075. /* Try to avoid going over the IFNAMSIZ length limit */
  1076. os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
  1077. }
  1078. }
  1079. static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
  1080. enum wpa_driver_if_type type)
  1081. {
  1082. char ifname[120], force_ifname[120];
  1083. if (wpa_s->pending_interface_name[0]) {
  1084. wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
  1085. "- skip creation of a new one");
  1086. if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
  1087. wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
  1088. "unknown?! ifname='%s'",
  1089. wpa_s->pending_interface_name);
  1090. return -1;
  1091. }
  1092. return 0;
  1093. }
  1094. wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
  1095. force_ifname[0] = '\0';
  1096. wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
  1097. ifname);
  1098. wpa_s->p2p_group_idx++;
  1099. wpa_s->pending_interface_type = type;
  1100. if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
  1101. wpa_s->pending_interface_addr, NULL) < 0) {
  1102. wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
  1103. "interface");
  1104. return -1;
  1105. }
  1106. if (force_ifname[0]) {
  1107. wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
  1108. force_ifname);
  1109. os_strlcpy(wpa_s->pending_interface_name, force_ifname,
  1110. sizeof(wpa_s->pending_interface_name));
  1111. } else
  1112. os_strlcpy(wpa_s->pending_interface_name, ifname,
  1113. sizeof(wpa_s->pending_interface_name));
  1114. wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
  1115. MACSTR, wpa_s->pending_interface_name,
  1116. MAC2STR(wpa_s->pending_interface_addr));
  1117. return 0;
  1118. }
  1119. static void wpas_p2p_remove_pending_group_interface(
  1120. struct wpa_supplicant *wpa_s)
  1121. {
  1122. if (!wpa_s->pending_interface_name[0] ||
  1123. is_zero_ether_addr(wpa_s->pending_interface_addr))
  1124. return; /* No pending virtual interface */
  1125. wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
  1126. wpa_s->pending_interface_name);
  1127. wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
  1128. wpa_s->pending_interface_name);
  1129. os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
  1130. wpa_s->pending_interface_name[0] = '\0';
  1131. }
  1132. static struct wpa_supplicant *
  1133. wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
  1134. {
  1135. struct wpa_interface iface;
  1136. struct wpa_supplicant *group_wpa_s;
  1137. if (!wpa_s->pending_interface_name[0]) {
  1138. wpa_printf(MSG_ERROR, "P2P: No pending group interface");
  1139. if (!wpas_p2p_create_iface(wpa_s))
  1140. return NULL;
  1141. /*
  1142. * Something has forced us to remove the pending interface; try
  1143. * to create a new one and hope for the best that we will get
  1144. * the same local address.
  1145. */
  1146. if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
  1147. WPA_IF_P2P_CLIENT) < 0)
  1148. return NULL;
  1149. }
  1150. os_memset(&iface, 0, sizeof(iface));
  1151. iface.ifname = wpa_s->pending_interface_name;
  1152. iface.driver = wpa_s->driver->name;
  1153. if (wpa_s->conf->ctrl_interface == NULL &&
  1154. wpa_s->parent != wpa_s &&
  1155. wpa_s->p2p_mgmt &&
  1156. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
  1157. iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
  1158. else
  1159. iface.ctrl_interface = wpa_s->conf->ctrl_interface;
  1160. iface.driver_param = wpa_s->conf->driver_param;
  1161. group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
  1162. if (group_wpa_s == NULL) {
  1163. wpa_printf(MSG_ERROR, "P2P: Failed to create new "
  1164. "wpa_supplicant interface");
  1165. return NULL;
  1166. }
  1167. wpa_s->pending_interface_name[0] = '\0';
  1168. group_wpa_s->parent = wpa_s;
  1169. group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
  1170. P2P_GROUP_INTERFACE_CLIENT;
  1171. wpa_s->global->p2p_group_formation = group_wpa_s;
  1172. wpas_p2p_clone_config(group_wpa_s, wpa_s);
  1173. return group_wpa_s;
  1174. }
  1175. static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
  1176. void *timeout_ctx)
  1177. {
  1178. struct wpa_supplicant *wpa_s = eloop_ctx;
  1179. wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
  1180. wpas_p2p_group_formation_failed(wpa_s);
  1181. }
  1182. void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
  1183. {
  1184. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  1185. wpa_s->parent, NULL);
  1186. if (wpa_s->global->p2p)
  1187. p2p_group_formation_failed(wpa_s->global->p2p);
  1188. wpas_group_formation_completed(wpa_s, 0);
  1189. }
  1190. void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
  1191. {
  1192. if (wpa_s->global->p2p_group_formation != wpa_s)
  1193. return;
  1194. /* Speed up group formation timeout since this cannot succeed */
  1195. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  1196. wpa_s->parent, NULL);
  1197. eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
  1198. wpa_s->parent, NULL);
  1199. }
  1200. static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
  1201. {
  1202. struct wpa_supplicant *wpa_s = ctx;
  1203. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  1204. wpa_drv_cancel_remain_on_channel(wpa_s);
  1205. wpa_s->off_channel_freq = 0;
  1206. wpa_s->roc_waiting_drv_freq = 0;
  1207. }
  1208. if (res->status) {
  1209. wpa_msg_global(wpa_s, MSG_INFO,
  1210. P2P_EVENT_GO_NEG_FAILURE "status=%d",
  1211. res->status);
  1212. wpas_notify_p2p_go_neg_completed(wpa_s, res);
  1213. wpas_p2p_remove_pending_group_interface(wpa_s);
  1214. return;
  1215. }
  1216. if (wpa_s->p2p_go_ht40)
  1217. res->ht40 = 1;
  1218. if (wpa_s->p2p_go_vht)
  1219. res->vht = 1;
  1220. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
  1221. "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
  1222. " wps_method=%s",
  1223. res->role_go ? "GO" : "client", res->freq, res->ht40,
  1224. MAC2STR(res->peer_device_addr),
  1225. MAC2STR(res->peer_interface_addr),
  1226. p2p_wps_method_text(res->wps_method));
  1227. wpas_notify_p2p_go_neg_completed(wpa_s, res);
  1228. if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
  1229. struct wpa_ssid *ssid;
  1230. ssid = wpa_config_get_network(wpa_s->conf,
  1231. wpa_s->p2p_persistent_id);
  1232. if (ssid && ssid->disabled == 2 &&
  1233. ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
  1234. size_t len = os_strlen(ssid->passphrase);
  1235. wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
  1236. "on requested persistent group");
  1237. os_memcpy(res->passphrase, ssid->passphrase, len);
  1238. res->passphrase[len] = '\0';
  1239. }
  1240. }
  1241. if (wpa_s->create_p2p_iface) {
  1242. struct wpa_supplicant *group_wpa_s =
  1243. wpas_p2p_init_group_interface(wpa_s, res->role_go);
  1244. if (group_wpa_s == NULL) {
  1245. wpas_p2p_remove_pending_group_interface(wpa_s);
  1246. return;
  1247. }
  1248. if (group_wpa_s != wpa_s) {
  1249. os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
  1250. sizeof(group_wpa_s->p2p_pin));
  1251. group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
  1252. }
  1253. os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
  1254. wpa_s->pending_interface_name[0] = '\0';
  1255. group_wpa_s->p2p_in_provisioning = 1;
  1256. if (res->role_go)
  1257. wpas_start_wps_go(group_wpa_s, res, 1);
  1258. else
  1259. wpas_start_wps_enrollee(group_wpa_s, res);
  1260. } else {
  1261. wpa_s->p2p_in_provisioning = 1;
  1262. wpa_s->global->p2p_group_formation = wpa_s;
  1263. if (res->role_go)
  1264. wpas_start_wps_go(wpa_s, res, 1);
  1265. else
  1266. wpas_start_wps_enrollee(ctx, res);
  1267. }
  1268. wpa_s->p2p_long_listen = 0;
  1269. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  1270. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  1271. eloop_register_timeout(15 + res->peer_config_timeout / 100,
  1272. (res->peer_config_timeout % 100) * 10000,
  1273. wpas_p2p_group_formation_timeout, wpa_s, NULL);
  1274. }
  1275. static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
  1276. {
  1277. struct wpa_supplicant *wpa_s = ctx;
  1278. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
  1279. " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
  1280. wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
  1281. }
  1282. static void wpas_dev_found(void *ctx, const u8 *addr,
  1283. const struct p2p_peer_info *info,
  1284. int new_device)
  1285. {
  1286. #ifndef CONFIG_NO_STDOUT_DEBUG
  1287. struct wpa_supplicant *wpa_s = ctx;
  1288. char devtype[WPS_DEV_TYPE_BUFSIZE];
  1289. char *wfd_dev_info_hex = NULL;
  1290. #ifdef CONFIG_WIFI_DISPLAY
  1291. wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
  1292. WFD_SUBELEM_DEVICE_INFO);
  1293. #endif /* CONFIG_WIFI_DISPLAY */
  1294. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
  1295. " p2p_dev_addr=" MACSTR
  1296. " pri_dev_type=%s name='%s' config_methods=0x%x "
  1297. "dev_capab=0x%x group_capab=0x%x%s%s",
  1298. MAC2STR(addr), MAC2STR(info->p2p_device_addr),
  1299. wps_dev_type_bin2str(info->pri_dev_type, devtype,
  1300. sizeof(devtype)),
  1301. info->device_name, info->config_methods,
  1302. info->dev_capab, info->group_capab,
  1303. wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
  1304. wfd_dev_info_hex ? wfd_dev_info_hex : "");
  1305. #endif /* CONFIG_NO_STDOUT_DEBUG */
  1306. os_free(wfd_dev_info_hex);
  1307. wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
  1308. }
  1309. static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
  1310. {
  1311. struct wpa_supplicant *wpa_s = ctx;
  1312. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
  1313. "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
  1314. wpas_notify_p2p_device_lost(wpa_s, dev_addr);
  1315. }
  1316. static void wpas_find_stopped(void *ctx)
  1317. {
  1318. struct wpa_supplicant *wpa_s = ctx;
  1319. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
  1320. }
  1321. struct wpas_p2p_listen_work {
  1322. unsigned int freq;
  1323. unsigned int duration;
  1324. struct wpabuf *probe_resp_ie;
  1325. };
  1326. static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
  1327. {
  1328. if (lwork == NULL)
  1329. return;
  1330. wpabuf_free(lwork->probe_resp_ie);
  1331. os_free(lwork);
  1332. }
  1333. static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
  1334. {
  1335. struct wpas_p2p_listen_work *lwork;
  1336. if (!wpa_s->p2p_listen_work)
  1337. return;
  1338. lwork = wpa_s->p2p_listen_work->ctx;
  1339. wpas_p2p_listen_work_free(lwork);
  1340. radio_work_done(wpa_s->p2p_listen_work);
  1341. wpa_s->p2p_listen_work = NULL;
  1342. }
  1343. static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
  1344. {
  1345. struct wpa_supplicant *wpa_s = work->wpa_s;
  1346. struct wpas_p2p_listen_work *lwork = work->ctx;
  1347. if (deinit) {
  1348. wpas_p2p_listen_work_free(lwork);
  1349. return;
  1350. }
  1351. wpa_s->p2p_listen_work = work;
  1352. wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
  1353. if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
  1354. wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
  1355. "report received Probe Request frames");
  1356. wpas_p2p_listen_work_done(wpa_s);
  1357. return;
  1358. }
  1359. wpa_s->pending_listen_freq = lwork->freq;
  1360. wpa_s->pending_listen_duration = lwork->duration;
  1361. if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, lwork->duration) < 0)
  1362. {
  1363. wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
  1364. "to remain on channel (%u MHz) for Listen "
  1365. "state", lwork->freq);
  1366. wpas_p2p_listen_work_done(wpa_s);
  1367. wpa_s->pending_listen_freq = 0;
  1368. return;
  1369. }
  1370. wpa_s->off_channel_freq = 0;
  1371. wpa_s->roc_waiting_drv_freq = lwork->freq;
  1372. }
  1373. static int wpas_start_listen(void *ctx, unsigned int freq,
  1374. unsigned int duration,
  1375. const struct wpabuf *probe_resp_ie)
  1376. {
  1377. struct wpa_supplicant *wpa_s = ctx;
  1378. struct wpas_p2p_listen_work *lwork;
  1379. if (wpa_s->p2p_listen_work) {
  1380. wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
  1381. return -1;
  1382. }
  1383. lwork = os_zalloc(sizeof(*lwork));
  1384. if (lwork == NULL)
  1385. return -1;
  1386. lwork->freq = freq;
  1387. lwork->duration = duration;
  1388. if (probe_resp_ie) {
  1389. lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
  1390. if (lwork->probe_resp_ie == NULL) {
  1391. wpas_p2p_listen_work_free(lwork);
  1392. return -1;
  1393. }
  1394. }
  1395. if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
  1396. lwork) < 0) {
  1397. wpas_p2p_listen_work_free(lwork);
  1398. return -1;
  1399. }
  1400. return 0;
  1401. }
  1402. static void wpas_stop_listen(void *ctx)
  1403. {
  1404. struct wpa_supplicant *wpa_s = ctx;
  1405. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  1406. wpa_drv_cancel_remain_on_channel(wpa_s);
  1407. wpa_s->off_channel_freq = 0;
  1408. wpa_s->roc_waiting_drv_freq = 0;
  1409. }
  1410. wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
  1411. wpa_drv_probe_req_report(wpa_s, 0);
  1412. wpas_p2p_listen_work_done(wpa_s);
  1413. }
  1414. static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
  1415. {
  1416. struct wpa_supplicant *wpa_s = ctx;
  1417. return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
  1418. }
  1419. /*
  1420. * DNS Header section is used only to calculate compression pointers, so the
  1421. * contents of this data does not matter, but the length needs to be reserved
  1422. * in the virtual packet.
  1423. */
  1424. #define DNS_HEADER_LEN 12
  1425. /*
  1426. * 27-octet in-memory packet from P2P specification containing two implied
  1427. * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
  1428. */
  1429. #define P2P_SD_IN_MEMORY_LEN 27
  1430. static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
  1431. u8 **spos, const u8 *end)
  1432. {
  1433. while (*spos < end) {
  1434. u8 val = ((*spos)[0] & 0xc0) >> 6;
  1435. int len;
  1436. if (val == 1 || val == 2) {
  1437. /* These are reserved values in RFC 1035 */
  1438. wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
  1439. "sequence starting with 0x%x", val);
  1440. return -1;
  1441. }
  1442. if (val == 3) {
  1443. u16 offset;
  1444. u8 *spos_tmp;
  1445. /* Offset */
  1446. if (*spos + 2 > end) {
  1447. wpa_printf(MSG_DEBUG, "P2P: No room for full "
  1448. "DNS offset field");
  1449. return -1;
  1450. }
  1451. offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
  1452. if (offset >= *spos - start) {
  1453. wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
  1454. "pointer offset %u", offset);
  1455. return -1;
  1456. }
  1457. (*spos) += 2;
  1458. spos_tmp = start + offset;
  1459. return p2p_sd_dns_uncompress_label(upos, uend, start,
  1460. &spos_tmp,
  1461. *spos - 2);
  1462. }
  1463. /* Label */
  1464. len = (*spos)[0] & 0x3f;
  1465. if (len == 0)
  1466. return 0;
  1467. (*spos)++;
  1468. if (*spos + len > end) {
  1469. wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
  1470. "sequence - no room for label with length "
  1471. "%u", len);
  1472. return -1;
  1473. }
  1474. if (*upos + len + 2 > uend)
  1475. return -2;
  1476. os_memcpy(*upos, *spos, len);
  1477. *spos += len;
  1478. *upos += len;
  1479. (*upos)[0] = '.';
  1480. (*upos)++;
  1481. (*upos)[0] = '\0';
  1482. }
  1483. return 0;
  1484. }
  1485. /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
  1486. * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
  1487. * not large enough */
  1488. static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
  1489. size_t msg_len, size_t offset)
  1490. {
  1491. /* 27-octet in-memory packet from P2P specification */
  1492. const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
  1493. "\x04_udp\xC0\x11\x00\x0C\x00\x01";
  1494. u8 *tmp, *end, *spos;
  1495. char *upos, *uend;
  1496. int ret = 0;
  1497. if (buf_len < 2)
  1498. return -1;
  1499. if (offset > msg_len)
  1500. return -1;
  1501. tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
  1502. if (tmp == NULL)
  1503. return -1;
  1504. spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
  1505. end = spos + msg_len;
  1506. spos += offset;
  1507. os_memset(tmp, 0, DNS_HEADER_LEN);
  1508. os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
  1509. os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
  1510. upos = buf;
  1511. uend = buf + buf_len;
  1512. ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
  1513. if (ret) {
  1514. os_free(tmp);
  1515. return ret;
  1516. }
  1517. if (upos == buf) {
  1518. upos[0] = '.';
  1519. upos[1] = '\0';
  1520. } else if (upos[-1] == '.')
  1521. upos[-1] = '\0';
  1522. os_free(tmp);
  1523. return 0;
  1524. }
  1525. static struct p2p_srv_bonjour *
  1526. wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
  1527. const struct wpabuf *query)
  1528. {
  1529. struct p2p_srv_bonjour *bsrv;
  1530. size_t len;
  1531. len = wpabuf_len(query);
  1532. dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
  1533. struct p2p_srv_bonjour, list) {
  1534. if (len == wpabuf_len(bsrv->query) &&
  1535. os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
  1536. len) == 0)
  1537. return bsrv;
  1538. }
  1539. return NULL;
  1540. }
  1541. static struct p2p_srv_upnp *
  1542. wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
  1543. const char *service)
  1544. {
  1545. struct p2p_srv_upnp *usrv;
  1546. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  1547. struct p2p_srv_upnp, list) {
  1548. if (version == usrv->version &&
  1549. os_strcmp(service, usrv->service) == 0)
  1550. return usrv;
  1551. }
  1552. return NULL;
  1553. }
  1554. static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
  1555. u8 srv_trans_id)
  1556. {
  1557. u8 *len_pos;
  1558. if (wpabuf_tailroom(resp) < 5)
  1559. return;
  1560. /* Length (to be filled) */
  1561. len_pos = wpabuf_put(resp, 2);
  1562. wpabuf_put_u8(resp, srv_proto);
  1563. wpabuf_put_u8(resp, srv_trans_id);
  1564. /* Status Code */
  1565. wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
  1566. /* Response Data: empty */
  1567. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1568. }
  1569. static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
  1570. struct wpabuf *resp, u8 srv_trans_id)
  1571. {
  1572. struct p2p_srv_bonjour *bsrv;
  1573. u8 *len_pos;
  1574. wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
  1575. if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  1576. wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
  1577. return;
  1578. }
  1579. dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
  1580. struct p2p_srv_bonjour, list) {
  1581. if (wpabuf_tailroom(resp) <
  1582. 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
  1583. return;
  1584. /* Length (to be filled) */
  1585. len_pos = wpabuf_put(resp, 2);
  1586. wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
  1587. wpabuf_put_u8(resp, srv_trans_id);
  1588. /* Status Code */
  1589. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1590. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
  1591. wpabuf_head(bsrv->resp),
  1592. wpabuf_len(bsrv->resp));
  1593. /* Response Data */
  1594. wpabuf_put_buf(resp, bsrv->query); /* Key */
  1595. wpabuf_put_buf(resp, bsrv->resp); /* Value */
  1596. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  1597. 2);
  1598. }
  1599. }
  1600. static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
  1601. size_t query_len)
  1602. {
  1603. char str_rx[256], str_srv[256];
  1604. if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
  1605. return 0; /* Too short to include DNS Type and Version */
  1606. if (os_memcmp(query + query_len - 3,
  1607. wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
  1608. 3) != 0)
  1609. return 0; /* Mismatch in DNS Type or Version */
  1610. if (query_len == wpabuf_len(bsrv->query) &&
  1611. os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
  1612. return 1; /* Binary match */
  1613. if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
  1614. 0))
  1615. return 0; /* Failed to uncompress query */
  1616. if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
  1617. wpabuf_head(bsrv->query),
  1618. wpabuf_len(bsrv->query) - 3, 0))
  1619. return 0; /* Failed to uncompress service */
  1620. return os_strcmp(str_rx, str_srv) == 0;
  1621. }
  1622. static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
  1623. struct wpabuf *resp, u8 srv_trans_id,
  1624. const u8 *query, size_t query_len)
  1625. {
  1626. struct p2p_srv_bonjour *bsrv;
  1627. u8 *len_pos;
  1628. int matches = 0;
  1629. wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
  1630. query, query_len);
  1631. if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  1632. wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
  1633. wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
  1634. srv_trans_id);
  1635. return;
  1636. }
  1637. if (query_len == 0) {
  1638. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  1639. return;
  1640. }
  1641. dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
  1642. struct p2p_srv_bonjour, list) {
  1643. if (!match_bonjour_query(bsrv, query, query_len))
  1644. continue;
  1645. if (wpabuf_tailroom(resp) <
  1646. 5 + query_len + wpabuf_len(bsrv->resp))
  1647. return;
  1648. matches++;
  1649. /* Length (to be filled) */
  1650. len_pos = wpabuf_put(resp, 2);
  1651. wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
  1652. wpabuf_put_u8(resp, srv_trans_id);
  1653. /* Status Code */
  1654. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1655. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
  1656. wpabuf_head(bsrv->resp),
  1657. wpabuf_len(bsrv->resp));
  1658. /* Response Data */
  1659. wpabuf_put_data(resp, query, query_len); /* Key */
  1660. wpabuf_put_buf(resp, bsrv->resp); /* Value */
  1661. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1662. }
  1663. if (matches == 0) {
  1664. wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
  1665. "available");
  1666. if (wpabuf_tailroom(resp) < 5)
  1667. return;
  1668. /* Length (to be filled) */
  1669. len_pos = wpabuf_put(resp, 2);
  1670. wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
  1671. wpabuf_put_u8(resp, srv_trans_id);
  1672. /* Status Code */
  1673. wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
  1674. /* Response Data: empty */
  1675. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  1676. 2);
  1677. }
  1678. }
  1679. static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
  1680. struct wpabuf *resp, u8 srv_trans_id)
  1681. {
  1682. struct p2p_srv_upnp *usrv;
  1683. u8 *len_pos;
  1684. wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
  1685. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
  1686. wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
  1687. return;
  1688. }
  1689. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  1690. struct p2p_srv_upnp, list) {
  1691. if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
  1692. return;
  1693. /* Length (to be filled) */
  1694. len_pos = wpabuf_put(resp, 2);
  1695. wpabuf_put_u8(resp, P2P_SERV_UPNP);
  1696. wpabuf_put_u8(resp, srv_trans_id);
  1697. /* Status Code */
  1698. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1699. /* Response Data */
  1700. wpabuf_put_u8(resp, usrv->version);
  1701. wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
  1702. usrv->service);
  1703. wpabuf_put_str(resp, usrv->service);
  1704. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  1705. 2);
  1706. }
  1707. }
  1708. static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
  1709. struct wpabuf *resp, u8 srv_trans_id,
  1710. const u8 *query, size_t query_len)
  1711. {
  1712. struct p2p_srv_upnp *usrv;
  1713. u8 *len_pos;
  1714. u8 version;
  1715. char *str;
  1716. int count = 0;
  1717. wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
  1718. query, query_len);
  1719. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
  1720. wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
  1721. wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
  1722. srv_trans_id);
  1723. return;
  1724. }
  1725. if (query_len == 0) {
  1726. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  1727. return;
  1728. }
  1729. if (wpabuf_tailroom(resp) < 5)
  1730. return;
  1731. /* Length (to be filled) */
  1732. len_pos = wpabuf_put(resp, 2);
  1733. wpabuf_put_u8(resp, P2P_SERV_UPNP);
  1734. wpabuf_put_u8(resp, srv_trans_id);
  1735. version = query[0];
  1736. str = os_malloc(query_len);
  1737. if (str == NULL)
  1738. return;
  1739. os_memcpy(str, query + 1, query_len - 1);
  1740. str[query_len - 1] = '\0';
  1741. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  1742. struct p2p_srv_upnp, list) {
  1743. if (version != usrv->version)
  1744. continue;
  1745. if (os_strcmp(str, "ssdp:all") != 0 &&
  1746. os_strstr(usrv->service, str) == NULL)
  1747. continue;
  1748. if (wpabuf_tailroom(resp) < 2)
  1749. break;
  1750. if (count == 0) {
  1751. /* Status Code */
  1752. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1753. /* Response Data */
  1754. wpabuf_put_u8(resp, version);
  1755. } else
  1756. wpabuf_put_u8(resp, ',');
  1757. count++;
  1758. wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
  1759. usrv->service);
  1760. if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
  1761. break;
  1762. wpabuf_put_str(resp, usrv->service);
  1763. }
  1764. os_free(str);
  1765. if (count == 0) {
  1766. wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
  1767. "available");
  1768. /* Status Code */
  1769. wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
  1770. /* Response Data: empty */
  1771. }
  1772. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1773. }
  1774. #ifdef CONFIG_WIFI_DISPLAY
  1775. static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
  1776. struct wpabuf *resp, u8 srv_trans_id,
  1777. const u8 *query, size_t query_len)
  1778. {
  1779. const u8 *pos;
  1780. u8 role;
  1781. u8 *len_pos;
  1782. wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
  1783. if (!wpa_s->global->wifi_display) {
  1784. wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
  1785. wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
  1786. srv_trans_id);
  1787. return;
  1788. }
  1789. if (query_len < 1) {
  1790. wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
  1791. "Role");
  1792. return;
  1793. }
  1794. if (wpabuf_tailroom(resp) < 5)
  1795. return;
  1796. pos = query;
  1797. role = *pos++;
  1798. wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
  1799. /* TODO: role specific handling */
  1800. /* Length (to be filled) */
  1801. len_pos = wpabuf_put(resp, 2);
  1802. wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
  1803. wpabuf_put_u8(resp, srv_trans_id);
  1804. wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
  1805. while (pos < query + query_len) {
  1806. if (*pos < MAX_WFD_SUBELEMS &&
  1807. wpa_s->global->wfd_subelem[*pos] &&
  1808. wpabuf_tailroom(resp) >=
  1809. wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
  1810. wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
  1811. "subelement %u", *pos);
  1812. wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
  1813. }
  1814. pos++;
  1815. }
  1816. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1817. }
  1818. #endif /* CONFIG_WIFI_DISPLAY */
  1819. static void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
  1820. u16 update_indic, const u8 *tlvs, size_t tlvs_len)
  1821. {
  1822. struct wpa_supplicant *wpa_s = ctx;
  1823. const u8 *pos = tlvs;
  1824. const u8 *end = tlvs + tlvs_len;
  1825. const u8 *tlv_end;
  1826. u16 slen;
  1827. struct wpabuf *resp;
  1828. u8 srv_proto, srv_trans_id;
  1829. size_t buf_len;
  1830. char *buf;
  1831. wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
  1832. tlvs, tlvs_len);
  1833. buf_len = 2 * tlvs_len + 1;
  1834. buf = os_malloc(buf_len);
  1835. if (buf) {
  1836. wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
  1837. wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
  1838. MACSTR " %u %u %s",
  1839. freq, MAC2STR(sa), dialog_token, update_indic,
  1840. buf);
  1841. os_free(buf);
  1842. }
  1843. if (wpa_s->p2p_sd_over_ctrl_iface) {
  1844. wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
  1845. update_indic, tlvs, tlvs_len);
  1846. return; /* to be processed by an external program */
  1847. }
  1848. resp = wpabuf_alloc(10000);
  1849. if (resp == NULL)
  1850. return;
  1851. while (pos + 1 < end) {
  1852. wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
  1853. slen = WPA_GET_LE16(pos);
  1854. pos += 2;
  1855. if (pos + slen > end || slen < 2) {
  1856. wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
  1857. "length");
  1858. wpabuf_free(resp);
  1859. return;
  1860. }
  1861. tlv_end = pos + slen;
  1862. srv_proto = *pos++;
  1863. wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
  1864. srv_proto);
  1865. srv_trans_id = *pos++;
  1866. wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
  1867. srv_trans_id);
  1868. wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
  1869. pos, tlv_end - pos);
  1870. if (wpa_s->force_long_sd) {
  1871. wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
  1872. "response");
  1873. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  1874. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  1875. goto done;
  1876. }
  1877. switch (srv_proto) {
  1878. case P2P_SERV_ALL_SERVICES:
  1879. wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
  1880. "for all services");
  1881. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
  1882. dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  1883. wpa_printf(MSG_DEBUG, "P2P: No service "
  1884. "discovery protocols available");
  1885. wpas_sd_add_proto_not_avail(
  1886. resp, P2P_SERV_ALL_SERVICES,
  1887. srv_trans_id);
  1888. break;
  1889. }
  1890. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  1891. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  1892. break;
  1893. case P2P_SERV_BONJOUR:
  1894. wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
  1895. pos, tlv_end - pos);
  1896. break;
  1897. case P2P_SERV_UPNP:
  1898. wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
  1899. pos, tlv_end - pos);
  1900. break;
  1901. #ifdef CONFIG_WIFI_DISPLAY
  1902. case P2P_SERV_WIFI_DISPLAY:
  1903. wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
  1904. pos, tlv_end - pos);
  1905. break;
  1906. #endif /* CONFIG_WIFI_DISPLAY */
  1907. default:
  1908. wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
  1909. "protocol %u", srv_proto);
  1910. wpas_sd_add_proto_not_avail(resp, srv_proto,
  1911. srv_trans_id);
  1912. break;
  1913. }
  1914. pos = tlv_end;
  1915. }
  1916. done:
  1917. wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
  1918. update_indic, tlvs, tlvs_len);
  1919. wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
  1920. wpabuf_free(resp);
  1921. }
  1922. static void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
  1923. const u8 *tlvs, size_t tlvs_len)
  1924. {
  1925. struct wpa_supplicant *wpa_s = ctx;
  1926. const u8 *pos = tlvs;
  1927. const u8 *end = tlvs + tlvs_len;
  1928. const u8 *tlv_end;
  1929. u16 slen;
  1930. size_t buf_len;
  1931. char *buf;
  1932. wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
  1933. tlvs, tlvs_len);
  1934. if (tlvs_len > 1500) {
  1935. /* TODO: better way for handling this */
  1936. wpa_msg_ctrl(wpa_s, MSG_INFO,
  1937. P2P_EVENT_SERV_DISC_RESP MACSTR
  1938. " %u <long response: %u bytes>",
  1939. MAC2STR(sa), update_indic,
  1940. (unsigned int) tlvs_len);
  1941. } else {
  1942. buf_len = 2 * tlvs_len + 1;
  1943. buf = os_malloc(buf_len);
  1944. if (buf) {
  1945. wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
  1946. wpa_msg_ctrl(wpa_s, MSG_INFO,
  1947. P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
  1948. MAC2STR(sa), update_indic, buf);
  1949. os_free(buf);
  1950. }
  1951. }
  1952. while (pos < end) {
  1953. u8 srv_proto, srv_trans_id, status;
  1954. wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
  1955. slen = WPA_GET_LE16(pos);
  1956. pos += 2;
  1957. if (pos + slen > end || slen < 3) {
  1958. wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
  1959. "length");
  1960. return;
  1961. }
  1962. tlv_end = pos + slen;
  1963. srv_proto = *pos++;
  1964. wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
  1965. srv_proto);
  1966. srv_trans_id = *pos++;
  1967. wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
  1968. srv_trans_id);
  1969. status = *pos++;
  1970. wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
  1971. status);
  1972. wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
  1973. pos, tlv_end - pos);
  1974. pos = tlv_end;
  1975. }
  1976. wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
  1977. }
  1978. u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
  1979. const struct wpabuf *tlvs)
  1980. {
  1981. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  1982. return 0;
  1983. return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
  1984. }
  1985. u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
  1986. u8 version, const char *query)
  1987. {
  1988. struct wpabuf *tlvs;
  1989. u64 ret;
  1990. tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
  1991. if (tlvs == NULL)
  1992. return 0;
  1993. wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
  1994. wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
  1995. wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
  1996. wpabuf_put_u8(tlvs, version);
  1997. wpabuf_put_str(tlvs, query);
  1998. ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
  1999. wpabuf_free(tlvs);
  2000. return ret;
  2001. }
  2002. #ifdef CONFIG_WIFI_DISPLAY
  2003. static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
  2004. const struct wpabuf *tlvs)
  2005. {
  2006. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  2007. return 0;
  2008. return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
  2009. }
  2010. #define MAX_WFD_SD_SUBELEMS 20
  2011. static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
  2012. const char *subelems)
  2013. {
  2014. u8 *len;
  2015. const char *pos;
  2016. int val;
  2017. int count = 0;
  2018. len = wpabuf_put(tlvs, 2);
  2019. wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
  2020. wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
  2021. wpabuf_put_u8(tlvs, role);
  2022. pos = subelems;
  2023. while (*pos) {
  2024. val = atoi(pos);
  2025. if (val >= 0 && val < 256) {
  2026. wpabuf_put_u8(tlvs, val);
  2027. count++;
  2028. if (count == MAX_WFD_SD_SUBELEMS)
  2029. break;
  2030. }
  2031. pos = os_strchr(pos + 1, ',');
  2032. if (pos == NULL)
  2033. break;
  2034. pos++;
  2035. }
  2036. WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
  2037. }
  2038. u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
  2039. const u8 *dst, const char *role)
  2040. {
  2041. struct wpabuf *tlvs;
  2042. u64 ret;
  2043. const char *subelems;
  2044. u8 id = 1;
  2045. subelems = os_strchr(role, ' ');
  2046. if (subelems == NULL)
  2047. return 0;
  2048. subelems++;
  2049. tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
  2050. if (tlvs == NULL)
  2051. return 0;
  2052. if (os_strstr(role, "[source]"))
  2053. wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
  2054. if (os_strstr(role, "[pri-sink]"))
  2055. wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
  2056. if (os_strstr(role, "[sec-sink]"))
  2057. wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
  2058. if (os_strstr(role, "[source+sink]"))
  2059. wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
  2060. ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
  2061. wpabuf_free(tlvs);
  2062. return ret;
  2063. }
  2064. #endif /* CONFIG_WIFI_DISPLAY */
  2065. int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
  2066. {
  2067. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  2068. return -1;
  2069. return p2p_sd_cancel_request(wpa_s->global->p2p,
  2070. (void *) (uintptr_t) req);
  2071. }
  2072. void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
  2073. const u8 *dst, u8 dialog_token,
  2074. const struct wpabuf *resp_tlvs)
  2075. {
  2076. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  2077. return;
  2078. p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
  2079. resp_tlvs);
  2080. }
  2081. void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
  2082. {
  2083. if (wpa_s->global->p2p)
  2084. p2p_sd_service_update(wpa_s->global->p2p);
  2085. }
  2086. static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
  2087. {
  2088. dl_list_del(&bsrv->list);
  2089. wpabuf_free(bsrv->query);
  2090. wpabuf_free(bsrv->resp);
  2091. os_free(bsrv);
  2092. }
  2093. static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
  2094. {
  2095. dl_list_del(&usrv->list);
  2096. os_free(usrv->service);
  2097. os_free(usrv);
  2098. }
  2099. void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
  2100. {
  2101. struct p2p_srv_bonjour *bsrv, *bn;
  2102. struct p2p_srv_upnp *usrv, *un;
  2103. dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
  2104. struct p2p_srv_bonjour, list)
  2105. wpas_p2p_srv_bonjour_free(bsrv);
  2106. dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
  2107. struct p2p_srv_upnp, list)
  2108. wpas_p2p_srv_upnp_free(usrv);
  2109. wpas_p2p_sd_service_update(wpa_s);
  2110. }
  2111. int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
  2112. struct wpabuf *query, struct wpabuf *resp)
  2113. {
  2114. struct p2p_srv_bonjour *bsrv;
  2115. bsrv = os_zalloc(sizeof(*bsrv));
  2116. if (bsrv == NULL)
  2117. return -1;
  2118. bsrv->query = query;
  2119. bsrv->resp = resp;
  2120. dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
  2121. wpas_p2p_sd_service_update(wpa_s);
  2122. return 0;
  2123. }
  2124. int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
  2125. const struct wpabuf *query)
  2126. {
  2127. struct p2p_srv_bonjour *bsrv;
  2128. bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
  2129. if (bsrv == NULL)
  2130. return -1;
  2131. wpas_p2p_srv_bonjour_free(bsrv);
  2132. wpas_p2p_sd_service_update(wpa_s);
  2133. return 0;
  2134. }
  2135. int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
  2136. const char *service)
  2137. {
  2138. struct p2p_srv_upnp *usrv;
  2139. if (wpas_p2p_service_get_upnp(wpa_s, version, service))
  2140. return 0; /* Already listed */
  2141. usrv = os_zalloc(sizeof(*usrv));
  2142. if (usrv == NULL)
  2143. return -1;
  2144. usrv->version = version;
  2145. usrv->service = os_strdup(service);
  2146. if (usrv->service == NULL) {
  2147. os_free(usrv);
  2148. return -1;
  2149. }
  2150. dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
  2151. wpas_p2p_sd_service_update(wpa_s);
  2152. return 0;
  2153. }
  2154. int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
  2155. const char *service)
  2156. {
  2157. struct p2p_srv_upnp *usrv;
  2158. usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
  2159. if (usrv == NULL)
  2160. return -1;
  2161. wpas_p2p_srv_upnp_free(usrv);
  2162. wpas_p2p_sd_service_update(wpa_s);
  2163. return 0;
  2164. }
  2165. static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
  2166. const u8 *peer, const char *params,
  2167. unsigned int generated_pin)
  2168. {
  2169. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
  2170. " %08d%s", MAC2STR(peer), generated_pin, params);
  2171. }
  2172. static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
  2173. const u8 *peer, const char *params)
  2174. {
  2175. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
  2176. "%s", MAC2STR(peer), params);
  2177. }
  2178. static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
  2179. const u8 *dev_addr, const u8 *pri_dev_type,
  2180. const char *dev_name, u16 supp_config_methods,
  2181. u8 dev_capab, u8 group_capab, const u8 *group_id,
  2182. size_t group_id_len)
  2183. {
  2184. struct wpa_supplicant *wpa_s = ctx;
  2185. char devtype[WPS_DEV_TYPE_BUFSIZE];
  2186. char params[300];
  2187. u8 empty_dev_type[8];
  2188. unsigned int generated_pin = 0;
  2189. struct wpa_supplicant *group = NULL;
  2190. if (group_id) {
  2191. for (group = wpa_s->global->ifaces; group; group = group->next)
  2192. {
  2193. struct wpa_ssid *s = group->current_ssid;
  2194. if (s != NULL &&
  2195. s->mode == WPAS_MODE_P2P_GO &&
  2196. group_id_len - ETH_ALEN == s->ssid_len &&
  2197. os_memcmp(group_id + ETH_ALEN, s->ssid,
  2198. s->ssid_len) == 0)
  2199. break;
  2200. }
  2201. }
  2202. if (pri_dev_type == NULL) {
  2203. os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
  2204. pri_dev_type = empty_dev_type;
  2205. }
  2206. os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
  2207. " pri_dev_type=%s name='%s' config_methods=0x%x "
  2208. "dev_capab=0x%x group_capab=0x%x%s%s",
  2209. MAC2STR(dev_addr),
  2210. wps_dev_type_bin2str(pri_dev_type, devtype,
  2211. sizeof(devtype)),
  2212. dev_name, supp_config_methods, dev_capab, group_capab,
  2213. group ? " group=" : "",
  2214. group ? group->ifname : "");
  2215. params[sizeof(params) - 1] = '\0';
  2216. if (config_methods & WPS_CONFIG_DISPLAY) {
  2217. generated_pin = wps_generate_pin();
  2218. wpas_prov_disc_local_display(wpa_s, peer, params,
  2219. generated_pin);
  2220. } else if (config_methods & WPS_CONFIG_KEYPAD)
  2221. wpas_prov_disc_local_keypad(wpa_s, peer, params);
  2222. else if (config_methods & WPS_CONFIG_PUSHBUTTON)
  2223. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
  2224. MACSTR "%s", MAC2STR(peer), params);
  2225. wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
  2226. P2P_PROV_DISC_SUCCESS,
  2227. config_methods, generated_pin);
  2228. }
  2229. static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
  2230. {
  2231. struct wpa_supplicant *wpa_s = ctx;
  2232. unsigned int generated_pin = 0;
  2233. char params[20];
  2234. if (wpa_s->pending_pd_before_join &&
  2235. (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
  2236. os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
  2237. wpa_s->pending_pd_before_join = 0;
  2238. wpa_printf(MSG_DEBUG, "P2P: Starting pending "
  2239. "join-existing-group operation");
  2240. wpas_p2p_join_start(wpa_s);
  2241. return;
  2242. }
  2243. if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
  2244. wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
  2245. os_snprintf(params, sizeof(params), " peer_go=%d",
  2246. wpa_s->pending_pd_use == AUTO_PD_JOIN);
  2247. else
  2248. params[0] = '\0';
  2249. if (config_methods & WPS_CONFIG_DISPLAY)
  2250. wpas_prov_disc_local_keypad(wpa_s, peer, params);
  2251. else if (config_methods & WPS_CONFIG_KEYPAD) {
  2252. generated_pin = wps_generate_pin();
  2253. wpas_prov_disc_local_display(wpa_s, peer, params,
  2254. generated_pin);
  2255. } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
  2256. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
  2257. MACSTR "%s", MAC2STR(peer), params);
  2258. wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
  2259. P2P_PROV_DISC_SUCCESS,
  2260. config_methods, generated_pin);
  2261. }
  2262. static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
  2263. enum p2p_prov_disc_status status)
  2264. {
  2265. struct wpa_supplicant *wpa_s = ctx;
  2266. if (wpa_s->p2p_fallback_to_go_neg) {
  2267. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
  2268. "failed - fall back to GO Negotiation");
  2269. wpas_p2p_fallback_to_go_neg(wpa_s, 0);
  2270. return;
  2271. }
  2272. if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
  2273. wpa_s->pending_pd_before_join = 0;
  2274. wpa_printf(MSG_DEBUG, "P2P: Starting pending "
  2275. "join-existing-group operation (no ACK for PD "
  2276. "Req attempts)");
  2277. wpas_p2p_join_start(wpa_s);
  2278. return;
  2279. }
  2280. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
  2281. " p2p_dev_addr=" MACSTR " status=%d",
  2282. MAC2STR(peer), status);
  2283. wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
  2284. status, 0, 0);
  2285. }
  2286. static int freq_included(const struct p2p_channels *channels, unsigned int freq)
  2287. {
  2288. if (channels == NULL)
  2289. return 1; /* Assume no restrictions */
  2290. return p2p_channels_includes_freq(channels, freq);
  2291. }
  2292. static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
  2293. const u8 *go_dev_addr, const u8 *ssid,
  2294. size_t ssid_len, int *go, u8 *group_bssid,
  2295. int *force_freq, int persistent_group,
  2296. const struct p2p_channels *channels)
  2297. {
  2298. struct wpa_supplicant *wpa_s = ctx;
  2299. struct wpa_ssid *s;
  2300. int res;
  2301. struct wpa_supplicant *grp;
  2302. if (!persistent_group) {
  2303. wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
  2304. " to join an active group", MAC2STR(sa));
  2305. if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
  2306. (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
  2307. == 0 ||
  2308. os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
  2309. wpa_printf(MSG_DEBUG, "P2P: Accept previously "
  2310. "authorized invitation");
  2311. goto accept_inv;
  2312. }
  2313. /*
  2314. * Do not accept the invitation automatically; notify user and
  2315. * request approval.
  2316. */
  2317. return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  2318. }
  2319. grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
  2320. if (grp) {
  2321. wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
  2322. "running persistent group");
  2323. if (*go)
  2324. os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
  2325. goto accept_inv;
  2326. }
  2327. if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
  2328. os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
  2329. wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
  2330. "invitation to re-invoke a persistent group");
  2331. } else if (!wpa_s->conf->persistent_reconnect)
  2332. return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  2333. for (s = wpa_s->conf->ssid; s; s = s->next) {
  2334. if (s->disabled == 2 &&
  2335. os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
  2336. s->ssid_len == ssid_len &&
  2337. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  2338. break;
  2339. }
  2340. if (!s) {
  2341. wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
  2342. " requested reinvocation of an unknown group",
  2343. MAC2STR(sa));
  2344. return P2P_SC_FAIL_UNKNOWN_GROUP;
  2345. }
  2346. if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
  2347. *go = 1;
  2348. if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
  2349. wpa_printf(MSG_DEBUG, "P2P: The only available "
  2350. "interface is already in use - reject "
  2351. "invitation");
  2352. return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
  2353. }
  2354. os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
  2355. } else if (s->mode == WPAS_MODE_P2P_GO) {
  2356. *go = 1;
  2357. if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
  2358. {
  2359. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  2360. "interface address for the group");
  2361. return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
  2362. }
  2363. os_memcpy(group_bssid, wpa_s->pending_interface_addr,
  2364. ETH_ALEN);
  2365. }
  2366. accept_inv:
  2367. wpas_p2p_set_own_freq_preference(wpa_s, 0);
  2368. /* Get one of the frequencies currently in use */
  2369. if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) {
  2370. wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
  2371. wpas_p2p_set_own_freq_preference(wpa_s, res);
  2372. if (wpa_s->num_multichan_concurrent < 2 ||
  2373. wpas_p2p_num_unused_channels(wpa_s) < 1) {
  2374. wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
  2375. *force_freq = res;
  2376. }
  2377. }
  2378. if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
  2379. wpas_p2p_num_unused_channels(wpa_s) > 0) {
  2380. if (*go == 0) {
  2381. /* We are the client */
  2382. wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
  2383. "running a GO but we are capable of MCC, "
  2384. "figure out the best channel to use");
  2385. *force_freq = 0;
  2386. } else if (!freq_included(channels, *force_freq)) {
  2387. /* We are the GO, and *force_freq is not in the
  2388. * intersection */
  2389. wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
  2390. "in intersection but we are capable of MCC, "
  2391. "figure out the best channel to use",
  2392. *force_freq);
  2393. *force_freq = 0;
  2394. }
  2395. }
  2396. return P2P_SC_SUCCESS;
  2397. }
  2398. static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
  2399. const u8 *ssid, size_t ssid_len,
  2400. const u8 *go_dev_addr, u8 status,
  2401. int op_freq)
  2402. {
  2403. struct wpa_supplicant *wpa_s = ctx;
  2404. struct wpa_ssid *s;
  2405. for (s = wpa_s->conf->ssid; s; s = s->next) {
  2406. if (s->disabled == 2 &&
  2407. s->ssid_len == ssid_len &&
  2408. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  2409. break;
  2410. }
  2411. if (status == P2P_SC_SUCCESS) {
  2412. wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
  2413. " was accepted; op_freq=%d MHz",
  2414. MAC2STR(sa), op_freq);
  2415. if (s) {
  2416. int go = s->mode == WPAS_MODE_P2P_GO;
  2417. wpas_p2p_group_add_persistent(
  2418. wpa_s, s, go, go ? op_freq : 0, 0, 0, NULL,
  2419. go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
  2420. } else if (bssid) {
  2421. wpa_s->user_initiated_pd = 0;
  2422. wpas_p2p_join(wpa_s, bssid, go_dev_addr,
  2423. wpa_s->p2p_wps_method, 0);
  2424. }
  2425. return;
  2426. }
  2427. if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
  2428. wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
  2429. " was rejected (status %u)", MAC2STR(sa), status);
  2430. return;
  2431. }
  2432. if (!s) {
  2433. if (bssid) {
  2434. wpa_msg_global(wpa_s, MSG_INFO,
  2435. P2P_EVENT_INVITATION_RECEIVED
  2436. "sa=" MACSTR " go_dev_addr=" MACSTR
  2437. " bssid=" MACSTR " unknown-network",
  2438. MAC2STR(sa), MAC2STR(go_dev_addr),
  2439. MAC2STR(bssid));
  2440. } else {
  2441. wpa_msg_global(wpa_s, MSG_INFO,
  2442. P2P_EVENT_INVITATION_RECEIVED
  2443. "sa=" MACSTR " go_dev_addr=" MACSTR
  2444. " unknown-network",
  2445. MAC2STR(sa), MAC2STR(go_dev_addr));
  2446. }
  2447. return;
  2448. }
  2449. if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
  2450. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
  2451. "sa=" MACSTR " persistent=%d freq=%d",
  2452. MAC2STR(sa), s->id, op_freq);
  2453. } else {
  2454. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
  2455. "sa=" MACSTR " persistent=%d",
  2456. MAC2STR(sa), s->id);
  2457. }
  2458. }
  2459. static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
  2460. struct wpa_ssid *ssid,
  2461. const u8 *peer, int inv)
  2462. {
  2463. size_t i;
  2464. if (ssid == NULL)
  2465. return;
  2466. for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
  2467. if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
  2468. ETH_ALEN) == 0)
  2469. break;
  2470. }
  2471. if (i >= ssid->num_p2p_clients) {
  2472. if (ssid->mode != WPAS_MODE_P2P_GO &&
  2473. os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
  2474. wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
  2475. "due to invitation result", ssid->id);
  2476. wpas_notify_network_removed(wpa_s, ssid);
  2477. wpa_config_remove_network(wpa_s->conf, ssid->id);
  2478. return;
  2479. }
  2480. return; /* Peer not found in client list */
  2481. }
  2482. wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
  2483. "group %d client list%s",
  2484. MAC2STR(peer), ssid->id,
  2485. inv ? " due to invitation result" : "");
  2486. os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
  2487. ssid->p2p_client_list + (i + 1) * ETH_ALEN,
  2488. (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
  2489. ssid->num_p2p_clients--;
  2490. #ifndef CONFIG_NO_CONFIG_WRITE
  2491. if (wpa_s->parent->conf->update_config &&
  2492. wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
  2493. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  2494. #endif /* CONFIG_NO_CONFIG_WRITE */
  2495. }
  2496. static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
  2497. const u8 *peer)
  2498. {
  2499. struct wpa_ssid *ssid;
  2500. wpa_s = wpa_s->global->p2p_invite_group;
  2501. if (wpa_s == NULL)
  2502. return; /* No known invitation group */
  2503. ssid = wpa_s->current_ssid;
  2504. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  2505. !ssid->p2p_persistent_group)
  2506. return; /* Not operating as a GO in persistent group */
  2507. ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
  2508. ssid->ssid, ssid->ssid_len);
  2509. wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
  2510. }
  2511. static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
  2512. const struct p2p_channels *channels,
  2513. const u8 *peer, int neg_freq)
  2514. {
  2515. struct wpa_supplicant *wpa_s = ctx;
  2516. struct wpa_ssid *ssid;
  2517. int freq;
  2518. if (bssid) {
  2519. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
  2520. "status=%d " MACSTR,
  2521. status, MAC2STR(bssid));
  2522. } else {
  2523. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
  2524. "status=%d ", status);
  2525. }
  2526. wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
  2527. wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
  2528. status, MAC2STR(peer));
  2529. if (wpa_s->pending_invite_ssid_id == -1) {
  2530. if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
  2531. wpas_remove_persistent_client(wpa_s, peer);
  2532. return; /* Invitation to active group */
  2533. }
  2534. if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
  2535. wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
  2536. "invitation exchange to indicate readiness for "
  2537. "re-invocation");
  2538. }
  2539. if (status != P2P_SC_SUCCESS) {
  2540. if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
  2541. ssid = wpa_config_get_network(
  2542. wpa_s->conf, wpa_s->pending_invite_ssid_id);
  2543. wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
  2544. }
  2545. wpas_p2p_remove_pending_group_interface(wpa_s);
  2546. return;
  2547. }
  2548. ssid = wpa_config_get_network(wpa_s->conf,
  2549. wpa_s->pending_invite_ssid_id);
  2550. if (ssid == NULL) {
  2551. wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
  2552. "data matching with invitation");
  2553. return;
  2554. }
  2555. /*
  2556. * The peer could have missed our ctrl::ack frame for Invitation
  2557. * Response and continue retransmitting the frame. To reduce the
  2558. * likelihood of the peer not getting successful TX status for the
  2559. * Invitation Response frame, wait a short time here before starting
  2560. * the persistent group so that we will remain on the current channel to
  2561. * acknowledge any possible retransmission from the peer.
  2562. */
  2563. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
  2564. "starting persistent group");
  2565. os_sleep(0, 50000);
  2566. freq = wpa_s->p2p_persistent_go_freq;
  2567. if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
  2568. freq_included(channels, neg_freq)) {
  2569. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use frequence %d MHz from invitation for GO mode",
  2570. neg_freq);
  2571. freq = neg_freq;
  2572. }
  2573. wpas_p2p_group_add_persistent(wpa_s, ssid,
  2574. ssid->mode == WPAS_MODE_P2P_GO,
  2575. freq,
  2576. wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
  2577. channels,
  2578. ssid->mode == WPAS_MODE_P2P_GO ?
  2579. P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
  2580. 0);
  2581. }
  2582. static int wpas_p2p_disallowed_freq(struct wpa_global *global,
  2583. unsigned int freq)
  2584. {
  2585. return freq_range_list_includes(&global->p2p_disallow_freq, freq);
  2586. }
  2587. static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
  2588. {
  2589. reg->channel[reg->channels] = chan;
  2590. reg->channels++;
  2591. }
  2592. static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
  2593. struct p2p_channels *chan,
  2594. struct p2p_channels *cli_chan)
  2595. {
  2596. int i, cla = 0;
  2597. os_memset(cli_chan, 0, sizeof(*cli_chan));
  2598. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
  2599. "band");
  2600. /* Operating class 81 - 2.4 GHz band channels 1..13 */
  2601. chan->reg_class[cla].reg_class = 81;
  2602. chan->reg_class[cla].channels = 0;
  2603. for (i = 0; i < 11; i++) {
  2604. if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
  2605. wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
  2606. }
  2607. if (chan->reg_class[cla].channels)
  2608. cla++;
  2609. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
  2610. "band");
  2611. /* Operating class 115 - 5 GHz, channels 36-48 */
  2612. chan->reg_class[cla].reg_class = 115;
  2613. chan->reg_class[cla].channels = 0;
  2614. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
  2615. wpas_p2p_add_chan(&chan->reg_class[cla], 36);
  2616. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
  2617. wpas_p2p_add_chan(&chan->reg_class[cla], 40);
  2618. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
  2619. wpas_p2p_add_chan(&chan->reg_class[cla], 44);
  2620. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
  2621. wpas_p2p_add_chan(&chan->reg_class[cla], 48);
  2622. if (chan->reg_class[cla].channels)
  2623. cla++;
  2624. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
  2625. "band");
  2626. /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
  2627. chan->reg_class[cla].reg_class = 124;
  2628. chan->reg_class[cla].channels = 0;
  2629. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
  2630. wpas_p2p_add_chan(&chan->reg_class[cla], 149);
  2631. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
  2632. wpas_p2p_add_chan(&chan->reg_class[cla], 153);
  2633. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
  2634. wpas_p2p_add_chan(&chan->reg_class[cla], 157);
  2635. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
  2636. wpas_p2p_add_chan(&chan->reg_class[cla], 161);
  2637. if (chan->reg_class[cla].channels)
  2638. cla++;
  2639. chan->reg_classes = cla;
  2640. return 0;
  2641. }
  2642. static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
  2643. u16 num_modes,
  2644. enum hostapd_hw_mode mode)
  2645. {
  2646. u16 i;
  2647. for (i = 0; i < num_modes; i++) {
  2648. if (modes[i].mode == mode)
  2649. return &modes[i];
  2650. }
  2651. return NULL;
  2652. }
  2653. enum chan_allowed {
  2654. NOT_ALLOWED, PASSIVE_ONLY, ALLOWED
  2655. };
  2656. static int has_channel(struct wpa_global *global,
  2657. struct hostapd_hw_modes *mode, u8 chan, int *flags)
  2658. {
  2659. int i;
  2660. unsigned int freq;
  2661. freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
  2662. chan * 5;
  2663. if (wpas_p2p_disallowed_freq(global, freq))
  2664. return NOT_ALLOWED;
  2665. for (i = 0; i < mode->num_channels; i++) {
  2666. if (mode->channels[i].chan == chan) {
  2667. if (flags)
  2668. *flags = mode->channels[i].flag;
  2669. if (mode->channels[i].flag &
  2670. (HOSTAPD_CHAN_DISABLED |
  2671. HOSTAPD_CHAN_RADAR))
  2672. return NOT_ALLOWED;
  2673. if (mode->channels[i].flag &
  2674. (HOSTAPD_CHAN_PASSIVE_SCAN |
  2675. HOSTAPD_CHAN_NO_IBSS))
  2676. return PASSIVE_ONLY;
  2677. return ALLOWED;
  2678. }
  2679. }
  2680. return NOT_ALLOWED;
  2681. }
  2682. struct p2p_oper_class_map {
  2683. enum hostapd_hw_mode mode;
  2684. u8 op_class;
  2685. u8 min_chan;
  2686. u8 max_chan;
  2687. u8 inc;
  2688. enum { BW20, BW40PLUS, BW40MINUS, BW80 } bw;
  2689. };
  2690. static struct p2p_oper_class_map op_class[] = {
  2691. { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
  2692. #if 0 /* Do not enable HT40 on 2 GHz for now */
  2693. { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
  2694. { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
  2695. #endif
  2696. { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
  2697. { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
  2698. { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
  2699. { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
  2700. { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
  2701. { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
  2702. /*
  2703. * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
  2704. * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
  2705. * 80 MHz, but currently use the following definition for simplicity
  2706. * (these center frequencies are not actual channels, which makes
  2707. * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
  2708. * removing invalid channels.
  2709. */
  2710. { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
  2711. { -1, 0, 0, 0, 0, BW20 }
  2712. };
  2713. static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
  2714. struct hostapd_hw_modes *mode,
  2715. u8 channel)
  2716. {
  2717. u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
  2718. unsigned int i;
  2719. if (mode->mode != HOSTAPD_MODE_IEEE80211A)
  2720. return 0;
  2721. for (i = 0; i < ARRAY_SIZE(center_channels); i++)
  2722. /*
  2723. * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
  2724. * so the center channel is 6 channels away from the start/end.
  2725. */
  2726. if (channel >= center_channels[i] - 6 &&
  2727. channel <= center_channels[i] + 6)
  2728. return center_channels[i];
  2729. return 0;
  2730. }
  2731. static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
  2732. struct hostapd_hw_modes *mode,
  2733. u8 channel, u8 bw)
  2734. {
  2735. u8 center_chan;
  2736. int i, flags;
  2737. enum chan_allowed res, ret = ALLOWED;
  2738. center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
  2739. if (!center_chan)
  2740. return NOT_ALLOWED;
  2741. if (center_chan >= 58 && center_chan <= 138)
  2742. return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
  2743. /* check all the channels are available */
  2744. for (i = 0; i < 4; i++) {
  2745. int adj_chan = center_chan - 6 + i * 4;
  2746. res = has_channel(wpa_s->global, mode, adj_chan, &flags);
  2747. if (res == NOT_ALLOWED)
  2748. return NOT_ALLOWED;
  2749. if (res == PASSIVE_ONLY)
  2750. ret = PASSIVE_ONLY;
  2751. if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
  2752. return NOT_ALLOWED;
  2753. if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
  2754. return NOT_ALLOWED;
  2755. if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
  2756. return NOT_ALLOWED;
  2757. if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
  2758. return NOT_ALLOWED;
  2759. }
  2760. return ret;
  2761. }
  2762. static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
  2763. struct hostapd_hw_modes *mode,
  2764. u8 channel, u8 bw)
  2765. {
  2766. int flag;
  2767. enum chan_allowed res, res2;
  2768. res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
  2769. if (bw == BW40MINUS) {
  2770. if (!(flag & HOSTAPD_CHAN_HT40MINUS))
  2771. return NOT_ALLOWED;
  2772. res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
  2773. } else if (bw == BW40PLUS) {
  2774. if (!(flag & HOSTAPD_CHAN_HT40PLUS))
  2775. return NOT_ALLOWED;
  2776. res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
  2777. } else if (bw == BW80) {
  2778. res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
  2779. }
  2780. if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
  2781. return NOT_ALLOWED;
  2782. if (res == PASSIVE_ONLY || res2 == PASSIVE_ONLY)
  2783. return PASSIVE_ONLY;
  2784. return res;
  2785. }
  2786. static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
  2787. struct p2p_channels *chan,
  2788. struct p2p_channels *cli_chan)
  2789. {
  2790. struct hostapd_hw_modes *mode;
  2791. int cla, op, cli_cla;
  2792. if (wpa_s->hw.modes == NULL) {
  2793. wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
  2794. "of all supported channels; assume dualband "
  2795. "support");
  2796. return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
  2797. }
  2798. cla = cli_cla = 0;
  2799. for (op = 0; op_class[op].op_class; op++) {
  2800. struct p2p_oper_class_map *o = &op_class[op];
  2801. u8 ch;
  2802. struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
  2803. mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
  2804. if (mode == NULL)
  2805. continue;
  2806. for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
  2807. enum chan_allowed res;
  2808. res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
  2809. if (res == ALLOWED) {
  2810. if (reg == NULL) {
  2811. wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
  2812. o->op_class);
  2813. reg = &chan->reg_class[cla];
  2814. cla++;
  2815. reg->reg_class = o->op_class;
  2816. }
  2817. reg->channel[reg->channels] = ch;
  2818. reg->channels++;
  2819. } else if (res == PASSIVE_ONLY &&
  2820. wpa_s->conf->p2p_add_cli_chan) {
  2821. if (cli_reg == NULL) {
  2822. wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
  2823. o->op_class);
  2824. cli_reg = &cli_chan->reg_class[cli_cla];
  2825. cli_cla++;
  2826. cli_reg->reg_class = o->op_class;
  2827. }
  2828. cli_reg->channel[cli_reg->channels] = ch;
  2829. cli_reg->channels++;
  2830. }
  2831. }
  2832. if (reg) {
  2833. wpa_hexdump(MSG_DEBUG, "P2P: Channels",
  2834. reg->channel, reg->channels);
  2835. }
  2836. if (cli_reg) {
  2837. wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
  2838. cli_reg->channel, cli_reg->channels);
  2839. }
  2840. }
  2841. chan->reg_classes = cla;
  2842. cli_chan->reg_classes = cli_cla;
  2843. return 0;
  2844. }
  2845. int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
  2846. struct hostapd_hw_modes *mode, u8 channel)
  2847. {
  2848. int op;
  2849. enum chan_allowed ret;
  2850. for (op = 0; op_class[op].op_class; op++) {
  2851. struct p2p_oper_class_map *o = &op_class[op];
  2852. u8 ch;
  2853. for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
  2854. if (o->mode != HOSTAPD_MODE_IEEE80211A ||
  2855. o->bw == BW20 || ch != channel)
  2856. continue;
  2857. ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
  2858. if (ret == ALLOWED)
  2859. return (o->bw == BW40MINUS) ? -1 : 1;
  2860. }
  2861. }
  2862. return 0;
  2863. }
  2864. int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
  2865. struct hostapd_hw_modes *mode, u8 channel)
  2866. {
  2867. if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
  2868. return 0;
  2869. return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
  2870. }
  2871. static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
  2872. size_t buf_len)
  2873. {
  2874. struct wpa_supplicant *wpa_s = ctx;
  2875. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2876. if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
  2877. break;
  2878. }
  2879. if (wpa_s == NULL)
  2880. return -1;
  2881. return wpa_drv_get_noa(wpa_s, buf, buf_len);
  2882. }
  2883. static int wpas_go_connected(void *ctx, const u8 *dev_addr)
  2884. {
  2885. struct wpa_supplicant *wpa_s = ctx;
  2886. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2887. struct wpa_ssid *ssid = wpa_s->current_ssid;
  2888. if (ssid == NULL)
  2889. continue;
  2890. if (ssid->mode != WPAS_MODE_INFRA)
  2891. continue;
  2892. if (wpa_s->wpa_state != WPA_COMPLETED &&
  2893. wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
  2894. continue;
  2895. if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
  2896. return 1;
  2897. }
  2898. return 0;
  2899. }
  2900. static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
  2901. {
  2902. struct wpa_supplicant *wpa_s = ctx;
  2903. wpa_msg_global(wpa_s, level, "P2P: %s", msg);
  2904. }
  2905. int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
  2906. {
  2907. struct wpa_interface iface;
  2908. struct wpa_supplicant *p2pdev_wpa_s;
  2909. char ifname[100];
  2910. char force_name[100];
  2911. int ret;
  2912. os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
  2913. wpa_s->ifname);
  2914. force_name[0] = '\0';
  2915. wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
  2916. ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
  2917. force_name, wpa_s->pending_interface_addr, NULL);
  2918. if (ret < 0) {
  2919. wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
  2920. return ret;
  2921. }
  2922. os_strlcpy(wpa_s->pending_interface_name, ifname,
  2923. sizeof(wpa_s->pending_interface_name));
  2924. os_memset(&iface, 0, sizeof(iface));
  2925. iface.p2p_mgmt = 1;
  2926. iface.ifname = wpa_s->pending_interface_name;
  2927. iface.driver = wpa_s->driver->name;
  2928. iface.driver_param = wpa_s->conf->driver_param;
  2929. iface.confname = wpa_s->confname;
  2930. p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
  2931. if (!p2pdev_wpa_s) {
  2932. wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
  2933. return -1;
  2934. }
  2935. p2pdev_wpa_s->parent = wpa_s;
  2936. wpa_s->pending_interface_name[0] = '\0';
  2937. return 0;
  2938. }
  2939. static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
  2940. const u8 *noa, size_t noa_len)
  2941. {
  2942. struct wpa_supplicant *wpa_s, *intf = ctx;
  2943. char hex[100];
  2944. for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2945. if (wpa_s->waiting_presence_resp)
  2946. break;
  2947. }
  2948. if (!wpa_s) {
  2949. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
  2950. return;
  2951. }
  2952. wpa_s->waiting_presence_resp = 0;
  2953. wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
  2954. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
  2955. " status=%u noa=%s", MAC2STR(src), status, hex);
  2956. }
  2957. /**
  2958. * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
  2959. * @global: Pointer to global data from wpa_supplicant_init()
  2960. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2961. * Returns: 0 on success, -1 on failure
  2962. */
  2963. int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
  2964. {
  2965. struct p2p_config p2p;
  2966. unsigned int r;
  2967. int i;
  2968. if (wpa_s->conf->p2p_disabled)
  2969. return 0;
  2970. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
  2971. return 0;
  2972. if (global->p2p)
  2973. return 0;
  2974. os_memset(&p2p, 0, sizeof(p2p));
  2975. p2p.cb_ctx = wpa_s;
  2976. p2p.debug_print = wpas_p2p_debug_print;
  2977. p2p.p2p_scan = wpas_p2p_scan;
  2978. p2p.send_action = wpas_send_action;
  2979. p2p.send_action_done = wpas_send_action_done;
  2980. p2p.go_neg_completed = wpas_go_neg_completed;
  2981. p2p.go_neg_req_rx = wpas_go_neg_req_rx;
  2982. p2p.dev_found = wpas_dev_found;
  2983. p2p.dev_lost = wpas_dev_lost;
  2984. p2p.find_stopped = wpas_find_stopped;
  2985. p2p.start_listen = wpas_start_listen;
  2986. p2p.stop_listen = wpas_stop_listen;
  2987. p2p.send_probe_resp = wpas_send_probe_resp;
  2988. p2p.sd_request = wpas_sd_request;
  2989. p2p.sd_response = wpas_sd_response;
  2990. p2p.prov_disc_req = wpas_prov_disc_req;
  2991. p2p.prov_disc_resp = wpas_prov_disc_resp;
  2992. p2p.prov_disc_fail = wpas_prov_disc_fail;
  2993. p2p.invitation_process = wpas_invitation_process;
  2994. p2p.invitation_received = wpas_invitation_received;
  2995. p2p.invitation_result = wpas_invitation_result;
  2996. p2p.get_noa = wpas_get_noa;
  2997. p2p.go_connected = wpas_go_connected;
  2998. p2p.presence_resp = wpas_presence_resp;
  2999. os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
  3000. os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
  3001. p2p.dev_name = wpa_s->conf->device_name;
  3002. p2p.manufacturer = wpa_s->conf->manufacturer;
  3003. p2p.model_name = wpa_s->conf->model_name;
  3004. p2p.model_number = wpa_s->conf->model_number;
  3005. p2p.serial_number = wpa_s->conf->serial_number;
  3006. if (wpa_s->wps) {
  3007. os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
  3008. p2p.config_methods = wpa_s->wps->config_methods;
  3009. }
  3010. if (wpa_s->conf->p2p_listen_reg_class &&
  3011. wpa_s->conf->p2p_listen_channel) {
  3012. p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
  3013. p2p.channel = wpa_s->conf->p2p_listen_channel;
  3014. } else {
  3015. p2p.reg_class = 81;
  3016. /*
  3017. * Pick one of the social channels randomly as the listen
  3018. * channel.
  3019. */
  3020. os_get_random((u8 *) &r, sizeof(r));
  3021. p2p.channel = 1 + (r % 3) * 5;
  3022. }
  3023. wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
  3024. if (wpa_s->conf->p2p_oper_reg_class &&
  3025. wpa_s->conf->p2p_oper_channel) {
  3026. p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
  3027. p2p.op_channel = wpa_s->conf->p2p_oper_channel;
  3028. p2p.cfg_op_channel = 1;
  3029. wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
  3030. "%d:%d", p2p.op_reg_class, p2p.op_channel);
  3031. } else {
  3032. p2p.op_reg_class = 81;
  3033. /*
  3034. * Use random operation channel from (1, 6, 11) if no other
  3035. * preference is indicated.
  3036. */
  3037. os_get_random((u8 *) &r, sizeof(r));
  3038. p2p.op_channel = 1 + (r % 3) * 5;
  3039. p2p.cfg_op_channel = 0;
  3040. wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
  3041. "%d:%d", p2p.op_reg_class, p2p.op_channel);
  3042. }
  3043. if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
  3044. p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
  3045. p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
  3046. }
  3047. if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  3048. os_memcpy(p2p.country, wpa_s->conf->country, 2);
  3049. p2p.country[2] = 0x04;
  3050. } else
  3051. os_memcpy(p2p.country, "XX\x04", 3);
  3052. if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
  3053. wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
  3054. "channel list");
  3055. return -1;
  3056. }
  3057. os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
  3058. WPS_DEV_TYPE_LEN);
  3059. p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
  3060. os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
  3061. p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
  3062. p2p.concurrent_operations = !!(wpa_s->drv_flags &
  3063. WPA_DRIVER_FLAGS_P2P_CONCURRENT);
  3064. p2p.max_peers = 100;
  3065. if (wpa_s->conf->p2p_ssid_postfix) {
  3066. p2p.ssid_postfix_len =
  3067. os_strlen(wpa_s->conf->p2p_ssid_postfix);
  3068. if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
  3069. p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
  3070. os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
  3071. p2p.ssid_postfix_len);
  3072. }
  3073. p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
  3074. p2p.max_listen = wpa_s->max_remain_on_chan;
  3075. global->p2p = p2p_init(&p2p);
  3076. if (global->p2p == NULL)
  3077. return -1;
  3078. global->p2p_init_wpa_s = wpa_s;
  3079. for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
  3080. if (wpa_s->conf->wps_vendor_ext[i] == NULL)
  3081. continue;
  3082. p2p_add_wps_vendor_extension(
  3083. global->p2p, wpa_s->conf->wps_vendor_ext[i]);
  3084. }
  3085. p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
  3086. return 0;
  3087. }
  3088. /**
  3089. * wpas_p2p_deinit - Deinitialize per-interface P2P data
  3090. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3091. *
  3092. * This function deinitialize per-interface P2P data.
  3093. */
  3094. void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
  3095. {
  3096. if (wpa_s->driver && wpa_s->drv_priv)
  3097. wpa_drv_probe_req_report(wpa_s, 0);
  3098. if (wpa_s->go_params) {
  3099. /* Clear any stored provisioning info */
  3100. p2p_clear_provisioning_info(
  3101. wpa_s->global->p2p,
  3102. wpa_s->go_params->peer_device_addr);
  3103. }
  3104. os_free(wpa_s->go_params);
  3105. wpa_s->go_params = NULL;
  3106. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  3107. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3108. wpa_s->p2p_long_listen = 0;
  3109. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  3110. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  3111. wpas_p2p_remove_pending_group_interface(wpa_s);
  3112. eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
  3113. wpas_p2p_listen_work_done(wpa_s);
  3114. /* TODO: remove group interface from the driver if this wpa_s instance
  3115. * is on top of a P2P group interface */
  3116. }
  3117. /**
  3118. * wpas_p2p_deinit_global - Deinitialize global P2P module
  3119. * @global: Pointer to global data from wpa_supplicant_init()
  3120. *
  3121. * This function deinitializes the global (per device) P2P module.
  3122. */
  3123. void wpas_p2p_deinit_global(struct wpa_global *global)
  3124. {
  3125. struct wpa_supplicant *wpa_s, *tmp;
  3126. wpa_s = global->ifaces;
  3127. if (wpa_s)
  3128. wpas_p2p_service_flush(wpa_s);
  3129. if (global->p2p == NULL)
  3130. return;
  3131. /* Remove remaining P2P group interfaces */
  3132. while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
  3133. wpa_s = wpa_s->next;
  3134. while (wpa_s) {
  3135. tmp = global->ifaces;
  3136. while (tmp &&
  3137. (tmp == wpa_s ||
  3138. tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
  3139. tmp = tmp->next;
  3140. }
  3141. if (tmp == NULL)
  3142. break;
  3143. /* Disconnect from the P2P group and deinit the interface */
  3144. wpas_p2p_disconnect(tmp);
  3145. }
  3146. /*
  3147. * Deinit GO data on any possibly remaining interface (if main
  3148. * interface is used as GO).
  3149. */
  3150. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3151. if (wpa_s->ap_iface)
  3152. wpas_p2p_group_deinit(wpa_s);
  3153. }
  3154. p2p_deinit(global->p2p);
  3155. global->p2p = NULL;
  3156. global->p2p_init_wpa_s = NULL;
  3157. }
  3158. static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
  3159. {
  3160. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
  3161. wpa_s->conf->p2p_no_group_iface)
  3162. return 0; /* separate interface disabled per configuration */
  3163. if (wpa_s->drv_flags &
  3164. (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
  3165. WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
  3166. return 1; /* P2P group requires a new interface in every case
  3167. */
  3168. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
  3169. return 0; /* driver does not support concurrent operations */
  3170. if (wpa_s->global->ifaces->next)
  3171. return 1; /* more that one interface already in use */
  3172. if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
  3173. return 1; /* this interface is already in use */
  3174. return 0;
  3175. }
  3176. static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
  3177. const u8 *peer_addr,
  3178. enum p2p_wps_method wps_method,
  3179. int go_intent, const u8 *own_interface_addr,
  3180. unsigned int force_freq, int persistent_group,
  3181. struct wpa_ssid *ssid, unsigned int pref_freq)
  3182. {
  3183. if (persistent_group && wpa_s->conf->persistent_reconnect)
  3184. persistent_group = 2;
  3185. /*
  3186. * Increase GO config timeout if HT40 is used since it takes some time
  3187. * to scan channels for coex purposes before the BSS can be started.
  3188. */
  3189. p2p_set_config_timeout(wpa_s->global->p2p,
  3190. wpa_s->p2p_go_ht40 ? 255 : 100, 20);
  3191. return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
  3192. go_intent, own_interface_addr, force_freq,
  3193. persistent_group, ssid ? ssid->ssid : NULL,
  3194. ssid ? ssid->ssid_len : 0,
  3195. wpa_s->p2p_pd_before_go_neg, pref_freq);
  3196. }
  3197. static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
  3198. const u8 *peer_addr,
  3199. enum p2p_wps_method wps_method,
  3200. int go_intent, const u8 *own_interface_addr,
  3201. unsigned int force_freq, int persistent_group,
  3202. struct wpa_ssid *ssid, unsigned int pref_freq)
  3203. {
  3204. if (persistent_group && wpa_s->conf->persistent_reconnect)
  3205. persistent_group = 2;
  3206. return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
  3207. go_intent, own_interface_addr, force_freq,
  3208. persistent_group, ssid ? ssid->ssid : NULL,
  3209. ssid ? ssid->ssid_len : 0, pref_freq);
  3210. }
  3211. static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
  3212. {
  3213. wpa_s->p2p_join_scan_count++;
  3214. wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
  3215. wpa_s->p2p_join_scan_count);
  3216. if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
  3217. wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
  3218. " for join operationg - stop join attempt",
  3219. MAC2STR(wpa_s->pending_join_iface_addr));
  3220. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3221. if (wpa_s->p2p_auto_pd) {
  3222. wpa_s->p2p_auto_pd = 0;
  3223. wpa_msg_global(wpa_s, MSG_INFO,
  3224. P2P_EVENT_PROV_DISC_FAILURE
  3225. " p2p_dev_addr=" MACSTR " status=N/A",
  3226. MAC2STR(wpa_s->pending_join_dev_addr));
  3227. return;
  3228. }
  3229. wpa_msg_global(wpa_s->parent, MSG_INFO,
  3230. P2P_EVENT_GROUP_FORMATION_FAILURE);
  3231. }
  3232. }
  3233. static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
  3234. {
  3235. int *freqs, res, num, i;
  3236. if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
  3237. /* Multiple channels are supported and not all are in use */
  3238. return 0;
  3239. }
  3240. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  3241. if (!freqs)
  3242. return 1;
  3243. num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
  3244. wpa_s->num_multichan_concurrent);
  3245. if (num < 0) {
  3246. res = 1;
  3247. goto exit_free;
  3248. }
  3249. for (i = 0; i < num; i++) {
  3250. if (freqs[i] == freq) {
  3251. wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
  3252. freq);
  3253. res = 0;
  3254. goto exit_free;
  3255. }
  3256. }
  3257. res = 1;
  3258. exit_free:
  3259. os_free(freqs);
  3260. return res;
  3261. }
  3262. static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
  3263. const u8 *peer_dev_addr)
  3264. {
  3265. struct wpa_bss *bss;
  3266. int updated;
  3267. bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
  3268. if (bss == NULL)
  3269. return -1;
  3270. if (bss->last_update_idx < wpa_s->bss_update_idx) {
  3271. wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
  3272. "last scan");
  3273. return 0;
  3274. }
  3275. updated = os_reltime_before(&wpa_s->p2p_auto_started,
  3276. &bss->last_update);
  3277. wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
  3278. "%ld.%06ld (%supdated in last scan)",
  3279. bss->last_update.sec, bss->last_update.usec,
  3280. updated ? "": "not ");
  3281. return updated;
  3282. }
  3283. static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
  3284. struct wpa_scan_results *scan_res)
  3285. {
  3286. struct wpa_bss *bss;
  3287. int freq;
  3288. u8 iface_addr[ETH_ALEN];
  3289. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3290. if (wpa_s->global->p2p_disabled)
  3291. return;
  3292. wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
  3293. scan_res ? (int) scan_res->num : -1,
  3294. wpa_s->p2p_auto_join ? "auto_" : "");
  3295. if (scan_res)
  3296. wpas_p2p_scan_res_handler(wpa_s, scan_res);
  3297. if (wpa_s->p2p_auto_pd) {
  3298. int join = wpas_p2p_peer_go(wpa_s,
  3299. wpa_s->pending_join_dev_addr);
  3300. if (join == 0 &&
  3301. wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
  3302. wpa_s->auto_pd_scan_retry++;
  3303. bss = wpa_bss_get_bssid_latest(
  3304. wpa_s, wpa_s->pending_join_dev_addr);
  3305. if (bss) {
  3306. freq = bss->freq;
  3307. wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
  3308. "the peer " MACSTR " at %d MHz",
  3309. wpa_s->auto_pd_scan_retry,
  3310. MAC2STR(wpa_s->
  3311. pending_join_dev_addr),
  3312. freq);
  3313. wpas_p2p_join_scan_req(wpa_s, freq);
  3314. return;
  3315. }
  3316. }
  3317. if (join < 0)
  3318. join = 0;
  3319. wpa_s->p2p_auto_pd = 0;
  3320. wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
  3321. wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
  3322. MAC2STR(wpa_s->pending_join_dev_addr), join);
  3323. if (p2p_prov_disc_req(wpa_s->global->p2p,
  3324. wpa_s->pending_join_dev_addr,
  3325. wpa_s->pending_pd_config_methods, join,
  3326. 0, wpa_s->user_initiated_pd) < 0) {
  3327. wpa_s->p2p_auto_pd = 0;
  3328. wpa_msg_global(wpa_s, MSG_INFO,
  3329. P2P_EVENT_PROV_DISC_FAILURE
  3330. " p2p_dev_addr=" MACSTR " status=N/A",
  3331. MAC2STR(wpa_s->pending_join_dev_addr));
  3332. }
  3333. return;
  3334. }
  3335. if (wpa_s->p2p_auto_join) {
  3336. int join = wpas_p2p_peer_go(wpa_s,
  3337. wpa_s->pending_join_dev_addr);
  3338. if (join < 0) {
  3339. wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
  3340. "running a GO -> use GO Negotiation");
  3341. wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
  3342. wpa_s->p2p_pin, wpa_s->p2p_wps_method,
  3343. wpa_s->p2p_persistent_group, 0, 0, 0,
  3344. wpa_s->p2p_go_intent,
  3345. wpa_s->p2p_connect_freq,
  3346. wpa_s->p2p_persistent_id,
  3347. wpa_s->p2p_pd_before_go_neg,
  3348. wpa_s->p2p_go_ht40,
  3349. wpa_s->p2p_go_vht);
  3350. return;
  3351. }
  3352. wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
  3353. "try to join the group", join ? "" :
  3354. " in older scan");
  3355. if (!join)
  3356. wpa_s->p2p_fallback_to_go_neg = 1;
  3357. }
  3358. freq = p2p_get_oper_freq(wpa_s->global->p2p,
  3359. wpa_s->pending_join_iface_addr);
  3360. if (freq < 0 &&
  3361. p2p_get_interface_addr(wpa_s->global->p2p,
  3362. wpa_s->pending_join_dev_addr,
  3363. iface_addr) == 0 &&
  3364. os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
  3365. {
  3366. wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
  3367. "address for join from " MACSTR " to " MACSTR
  3368. " based on newly discovered P2P peer entry",
  3369. MAC2STR(wpa_s->pending_join_iface_addr),
  3370. MAC2STR(iface_addr));
  3371. os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
  3372. ETH_ALEN);
  3373. freq = p2p_get_oper_freq(wpa_s->global->p2p,
  3374. wpa_s->pending_join_iface_addr);
  3375. }
  3376. if (freq >= 0) {
  3377. wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
  3378. "from P2P peer table: %d MHz", freq);
  3379. }
  3380. bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
  3381. if (bss) {
  3382. freq = bss->freq;
  3383. wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
  3384. "from BSS table: %d MHz (SSID %s)", freq,
  3385. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  3386. }
  3387. if (freq > 0) {
  3388. u16 method;
  3389. if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
  3390. wpa_msg_global(wpa_s->parent, MSG_INFO,
  3391. P2P_EVENT_GROUP_FORMATION_FAILURE
  3392. "reason=FREQ_CONFLICT");
  3393. return;
  3394. }
  3395. wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
  3396. "prior to joining an existing group (GO " MACSTR
  3397. " freq=%u MHz)",
  3398. MAC2STR(wpa_s->pending_join_dev_addr), freq);
  3399. wpa_s->pending_pd_before_join = 1;
  3400. switch (wpa_s->pending_join_wps_method) {
  3401. case WPS_PIN_DISPLAY:
  3402. method = WPS_CONFIG_KEYPAD;
  3403. break;
  3404. case WPS_PIN_KEYPAD:
  3405. method = WPS_CONFIG_DISPLAY;
  3406. break;
  3407. case WPS_PBC:
  3408. method = WPS_CONFIG_PUSHBUTTON;
  3409. break;
  3410. default:
  3411. method = 0;
  3412. break;
  3413. }
  3414. if ((p2p_get_provisioning_info(wpa_s->global->p2p,
  3415. wpa_s->pending_join_dev_addr) ==
  3416. method)) {
  3417. /*
  3418. * We have already performed provision discovery for
  3419. * joining the group. Proceed directly to join
  3420. * operation without duplicated provision discovery. */
  3421. wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
  3422. "with " MACSTR " already done - proceed to "
  3423. "join",
  3424. MAC2STR(wpa_s->pending_join_dev_addr));
  3425. wpa_s->pending_pd_before_join = 0;
  3426. goto start;
  3427. }
  3428. if (p2p_prov_disc_req(wpa_s->global->p2p,
  3429. wpa_s->pending_join_dev_addr, method, 1,
  3430. freq, wpa_s->user_initiated_pd) < 0) {
  3431. wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
  3432. "Discovery Request before joining an "
  3433. "existing group");
  3434. wpa_s->pending_pd_before_join = 0;
  3435. goto start;
  3436. }
  3437. return;
  3438. }
  3439. wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
  3440. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3441. eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
  3442. wpas_p2p_check_join_scan_limit(wpa_s);
  3443. return;
  3444. start:
  3445. /* Start join operation immediately */
  3446. wpas_p2p_join_start(wpa_s);
  3447. }
  3448. static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
  3449. {
  3450. int ret;
  3451. struct wpa_driver_scan_params params;
  3452. struct wpabuf *wps_ie, *ies;
  3453. size_t ielen;
  3454. int freqs[2] = { 0, 0 };
  3455. os_memset(&params, 0, sizeof(params));
  3456. /* P2P Wildcard SSID */
  3457. params.num_ssids = 1;
  3458. params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
  3459. params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
  3460. wpa_s->wps->dev.p2p = 1;
  3461. wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
  3462. wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
  3463. NULL);
  3464. if (wps_ie == NULL) {
  3465. wpas_p2p_scan_res_join(wpa_s, NULL);
  3466. return;
  3467. }
  3468. ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
  3469. ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
  3470. if (ies == NULL) {
  3471. wpabuf_free(wps_ie);
  3472. wpas_p2p_scan_res_join(wpa_s, NULL);
  3473. return;
  3474. }
  3475. wpabuf_put_buf(ies, wps_ie);
  3476. wpabuf_free(wps_ie);
  3477. p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
  3478. params.p2p_probe = 1;
  3479. params.extra_ies = wpabuf_head(ies);
  3480. params.extra_ies_len = wpabuf_len(ies);
  3481. if (freq > 0) {
  3482. freqs[0] = freq;
  3483. params.freqs = freqs;
  3484. }
  3485. /*
  3486. * Run a scan to update BSS table and start Provision Discovery once
  3487. * the new scan results become available.
  3488. */
  3489. ret = wpa_drv_scan(wpa_s, &params);
  3490. if (!ret) {
  3491. os_get_reltime(&wpa_s->scan_trigger_time);
  3492. wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
  3493. wpa_s->own_scan_requested = 1;
  3494. }
  3495. wpabuf_free(ies);
  3496. if (ret) {
  3497. wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
  3498. "try again later");
  3499. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3500. eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
  3501. wpas_p2p_check_join_scan_limit(wpa_s);
  3502. }
  3503. }
  3504. static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
  3505. {
  3506. struct wpa_supplicant *wpa_s = eloop_ctx;
  3507. wpas_p2p_join_scan_req(wpa_s, 0);
  3508. }
  3509. static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
  3510. const u8 *dev_addr, enum p2p_wps_method wps_method,
  3511. int auto_join)
  3512. {
  3513. wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
  3514. MACSTR " dev " MACSTR ")%s",
  3515. MAC2STR(iface_addr), MAC2STR(dev_addr),
  3516. auto_join ? " (auto_join)" : "");
  3517. wpa_s->p2p_auto_pd = 0;
  3518. wpa_s->p2p_auto_join = !!auto_join;
  3519. os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
  3520. os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
  3521. wpa_s->pending_join_wps_method = wps_method;
  3522. /* Make sure we are not running find during connection establishment */
  3523. wpas_p2p_stop_find(wpa_s);
  3524. wpa_s->p2p_join_scan_count = 0;
  3525. wpas_p2p_join_scan(wpa_s, NULL);
  3526. return 0;
  3527. }
  3528. static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
  3529. {
  3530. struct wpa_supplicant *group;
  3531. struct p2p_go_neg_results res;
  3532. struct wpa_bss *bss;
  3533. group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
  3534. if (group == NULL)
  3535. return -1;
  3536. if (group != wpa_s) {
  3537. os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
  3538. sizeof(group->p2p_pin));
  3539. group->p2p_wps_method = wpa_s->p2p_wps_method;
  3540. } else {
  3541. /*
  3542. * Need to mark the current interface for p2p_group_formation
  3543. * when a separate group interface is not used. This is needed
  3544. * to allow p2p_cancel stop a pending p2p_connect-join.
  3545. * wpas_p2p_init_group_interface() addresses this for the case
  3546. * where a separate group interface is used.
  3547. */
  3548. wpa_s->global->p2p_group_formation = wpa_s;
  3549. }
  3550. group->p2p_in_provisioning = 1;
  3551. group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
  3552. os_memset(&res, 0, sizeof(res));
  3553. os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
  3554. ETH_ALEN);
  3555. res.wps_method = wpa_s->pending_join_wps_method;
  3556. bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
  3557. if (bss) {
  3558. res.freq = bss->freq;
  3559. res.ssid_len = bss->ssid_len;
  3560. os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
  3561. wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency "
  3562. "from BSS table: %d MHz (SSID %s)", bss->freq,
  3563. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  3564. }
  3565. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  3566. wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
  3567. "starting client");
  3568. wpa_drv_cancel_remain_on_channel(wpa_s);
  3569. wpa_s->off_channel_freq = 0;
  3570. wpa_s->roc_waiting_drv_freq = 0;
  3571. }
  3572. wpas_start_wps_enrollee(group, &res);
  3573. /*
  3574. * Allow a longer timeout for join-a-running-group than normal 15
  3575. * second group formation timeout since the GO may not have authorized
  3576. * our connection yet.
  3577. */
  3578. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  3579. eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
  3580. wpa_s, NULL);
  3581. return 0;
  3582. }
  3583. static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
  3584. int *force_freq, int *pref_freq, int go)
  3585. {
  3586. int *freqs, res;
  3587. unsigned int freq_in_use = 0, num, i;
  3588. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  3589. if (!freqs)
  3590. return -1;
  3591. num = get_shared_radio_freqs(wpa_s, freqs,
  3592. wpa_s->num_multichan_concurrent);
  3593. wpa_printf(MSG_DEBUG,
  3594. "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
  3595. freq, wpa_s->num_multichan_concurrent, num);
  3596. if (freq > 0) {
  3597. int ret;
  3598. if (go)
  3599. ret = p2p_supported_freq(wpa_s->global->p2p, freq);
  3600. else
  3601. ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
  3602. if (!ret) {
  3603. wpa_printf(MSG_DEBUG, "P2P: The forced channel "
  3604. "(%u MHz) is not supported for P2P uses",
  3605. freq);
  3606. res = -3;
  3607. goto exit_free;
  3608. }
  3609. for (i = 0; i < num; i++) {
  3610. if (freqs[i] == freq)
  3611. freq_in_use = 1;
  3612. }
  3613. if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
  3614. wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
  3615. freq);
  3616. res = -2;
  3617. goto exit_free;
  3618. }
  3619. wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
  3620. "requested channel (%u MHz)", freq);
  3621. *force_freq = freq;
  3622. goto exit_ok;
  3623. }
  3624. for (i = 0; i < num; i++) {
  3625. if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
  3626. continue;
  3627. if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
  3628. wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
  3629. freqs[i]);
  3630. *pref_freq = freqs[i];
  3631. } else {
  3632. wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
  3633. freqs[i]);
  3634. *force_freq = freqs[i];
  3635. }
  3636. break;
  3637. }
  3638. if (i == num) {
  3639. if (num < wpa_s->num_multichan_concurrent && num > 0) {
  3640. wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
  3641. *force_freq = 0;
  3642. } else if (num < wpa_s->num_multichan_concurrent) {
  3643. wpa_printf(MSG_DEBUG, "P2P: No current operating channels - try to use a new channel");
  3644. *force_freq = 0;
  3645. } else {
  3646. wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
  3647. res = -2;
  3648. goto exit_free;
  3649. }
  3650. }
  3651. exit_ok:
  3652. res = 0;
  3653. exit_free:
  3654. os_free(freqs);
  3655. return res;
  3656. }
  3657. /**
  3658. * wpas_p2p_connect - Request P2P Group Formation to be started
  3659. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3660. * @peer_addr: Address of the peer P2P Device
  3661. * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
  3662. * @persistent_group: Whether to create a persistent group
  3663. * @auto_join: Whether to select join vs. GO Negotiation automatically
  3664. * @join: Whether to join an existing group (as a client) instead of starting
  3665. * Group Owner negotiation; @peer_addr is BSSID in that case
  3666. * @auth: Whether to only authorize the connection instead of doing that and
  3667. * initiating Group Owner negotiation
  3668. * @go_intent: GO Intent or -1 to use default
  3669. * @freq: Frequency for the group or 0 for auto-selection
  3670. * @persistent_id: Persistent group credentials to use for forcing GO
  3671. * parameters or -1 to generate new values (SSID/passphrase)
  3672. * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
  3673. * interoperability workaround when initiating group formation
  3674. * @ht40: Start GO with 40 MHz channel width
  3675. * @vht: Start GO with VHT support
  3676. * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
  3677. * failure, -2 on failure due to channel not currently available,
  3678. * -3 if forced channel is not supported
  3679. */
  3680. int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  3681. const char *pin, enum p2p_wps_method wps_method,
  3682. int persistent_group, int auto_join, int join, int auth,
  3683. int go_intent, int freq, int persistent_id, int pd,
  3684. int ht40, int vht)
  3685. {
  3686. int force_freq = 0, pref_freq = 0;
  3687. int ret = 0, res;
  3688. enum wpa_driver_if_type iftype;
  3689. const u8 *if_addr;
  3690. struct wpa_ssid *ssid = NULL;
  3691. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3692. return -1;
  3693. if (persistent_id >= 0) {
  3694. ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
  3695. if (ssid == NULL || ssid->disabled != 2 ||
  3696. ssid->mode != WPAS_MODE_P2P_GO)
  3697. return -1;
  3698. }
  3699. os_free(wpa_s->global->add_psk);
  3700. wpa_s->global->add_psk = NULL;
  3701. if (go_intent < 0)
  3702. go_intent = wpa_s->conf->p2p_go_intent;
  3703. if (!auth)
  3704. wpa_s->p2p_long_listen = 0;
  3705. wpa_s->p2p_wps_method = wps_method;
  3706. wpa_s->p2p_persistent_group = !!persistent_group;
  3707. wpa_s->p2p_persistent_id = persistent_id;
  3708. wpa_s->p2p_go_intent = go_intent;
  3709. wpa_s->p2p_connect_freq = freq;
  3710. wpa_s->p2p_fallback_to_go_neg = 0;
  3711. wpa_s->p2p_pd_before_go_neg = !!pd;
  3712. wpa_s->p2p_go_ht40 = !!ht40;
  3713. wpa_s->p2p_go_vht = !!vht;
  3714. if (pin)
  3715. os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
  3716. else if (wps_method == WPS_PIN_DISPLAY) {
  3717. ret = wps_generate_pin();
  3718. os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
  3719. ret);
  3720. wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
  3721. wpa_s->p2p_pin);
  3722. } else
  3723. wpa_s->p2p_pin[0] = '\0';
  3724. if (join || auto_join) {
  3725. u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
  3726. if (auth) {
  3727. wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
  3728. "connect a running group from " MACSTR,
  3729. MAC2STR(peer_addr));
  3730. os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
  3731. return ret;
  3732. }
  3733. os_memcpy(dev_addr, peer_addr, ETH_ALEN);
  3734. if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
  3735. iface_addr) < 0) {
  3736. os_memcpy(iface_addr, peer_addr, ETH_ALEN);
  3737. p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
  3738. dev_addr);
  3739. }
  3740. if (auto_join) {
  3741. os_get_reltime(&wpa_s->p2p_auto_started);
  3742. wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
  3743. "%ld.%06ld",
  3744. wpa_s->p2p_auto_started.sec,
  3745. wpa_s->p2p_auto_started.usec);
  3746. }
  3747. wpa_s->user_initiated_pd = 1;
  3748. if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
  3749. auto_join) < 0)
  3750. return -1;
  3751. return ret;
  3752. }
  3753. res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
  3754. go_intent == 15);
  3755. if (res)
  3756. return res;
  3757. wpas_p2p_set_own_freq_preference(wpa_s,
  3758. force_freq ? force_freq : pref_freq);
  3759. wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
  3760. if (wpa_s->create_p2p_iface) {
  3761. /* Prepare to add a new interface for the group */
  3762. iftype = WPA_IF_P2P_GROUP;
  3763. if (go_intent == 15)
  3764. iftype = WPA_IF_P2P_GO;
  3765. if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
  3766. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  3767. "interface for the group");
  3768. return -1;
  3769. }
  3770. if_addr = wpa_s->pending_interface_addr;
  3771. } else
  3772. if_addr = wpa_s->own_addr;
  3773. if (auth) {
  3774. if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
  3775. go_intent, if_addr,
  3776. force_freq, persistent_group, ssid,
  3777. pref_freq) < 0)
  3778. return -1;
  3779. return ret;
  3780. }
  3781. if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
  3782. go_intent, if_addr, force_freq,
  3783. persistent_group, ssid, pref_freq) < 0) {
  3784. if (wpa_s->create_p2p_iface)
  3785. wpas_p2p_remove_pending_group_interface(wpa_s);
  3786. return -1;
  3787. }
  3788. return ret;
  3789. }
  3790. /**
  3791. * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
  3792. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3793. * @freq: Frequency of the channel in MHz
  3794. * @duration: Duration of the stay on the channel in milliseconds
  3795. *
  3796. * This callback is called when the driver indicates that it has started the
  3797. * requested remain-on-channel duration.
  3798. */
  3799. void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  3800. unsigned int freq, unsigned int duration)
  3801. {
  3802. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3803. return;
  3804. if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
  3805. p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
  3806. wpa_s->pending_listen_duration);
  3807. wpa_s->pending_listen_freq = 0;
  3808. }
  3809. }
  3810. static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
  3811. unsigned int timeout)
  3812. {
  3813. /* Limit maximum Listen state time based on driver limitation. */
  3814. if (timeout > wpa_s->max_remain_on_chan)
  3815. timeout = wpa_s->max_remain_on_chan;
  3816. return p2p_listen(wpa_s->global->p2p, timeout);
  3817. }
  3818. /**
  3819. * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
  3820. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3821. * @freq: Frequency of the channel in MHz
  3822. *
  3823. * This callback is called when the driver indicates that a remain-on-channel
  3824. * operation has been completed, i.e., the duration on the requested channel
  3825. * has timed out.
  3826. */
  3827. void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  3828. unsigned int freq)
  3829. {
  3830. wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
  3831. "(p2p_long_listen=%d ms pending_action_tx=%p)",
  3832. wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
  3833. wpas_p2p_listen_work_done(wpa_s);
  3834. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3835. return;
  3836. if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
  3837. return; /* P2P module started a new operation */
  3838. if (offchannel_pending_action_tx(wpa_s))
  3839. return;
  3840. if (wpa_s->p2p_long_listen > 0)
  3841. wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
  3842. if (wpa_s->p2p_long_listen > 0) {
  3843. wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
  3844. wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
  3845. } else {
  3846. /*
  3847. * When listen duration is over, stop listen & update p2p_state
  3848. * to IDLE.
  3849. */
  3850. p2p_stop_listen(wpa_s->global->p2p);
  3851. }
  3852. }
  3853. /**
  3854. * wpas_p2p_group_remove - Remove a P2P group
  3855. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3856. * @ifname: Network interface name of the group interface or "*" to remove all
  3857. * groups
  3858. * Returns: 0 on success, -1 on failure
  3859. *
  3860. * This function is used to remove a P2P group. This can be used to disconnect
  3861. * from a group in which the local end is a P2P Client or to end a P2P Group in
  3862. * case the local end is the Group Owner. If a virtual network interface was
  3863. * created for this group, that interface will be removed. Otherwise, only the
  3864. * configured P2P group network will be removed from the interface.
  3865. */
  3866. int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
  3867. {
  3868. struct wpa_global *global = wpa_s->global;
  3869. if (os_strcmp(ifname, "*") == 0) {
  3870. struct wpa_supplicant *prev;
  3871. wpa_s = global->ifaces;
  3872. while (wpa_s) {
  3873. prev = wpa_s;
  3874. wpa_s = wpa_s->next;
  3875. if (prev->p2p_group_interface !=
  3876. NOT_P2P_GROUP_INTERFACE ||
  3877. (prev->current_ssid &&
  3878. prev->current_ssid->p2p_group))
  3879. wpas_p2p_disconnect(prev);
  3880. }
  3881. return 0;
  3882. }
  3883. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3884. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  3885. break;
  3886. }
  3887. return wpas_p2p_disconnect(wpa_s);
  3888. }
  3889. static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
  3890. {
  3891. unsigned int r;
  3892. if (freq == 2) {
  3893. wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
  3894. "band");
  3895. if (wpa_s->best_24_freq > 0 &&
  3896. p2p_supported_freq_go(wpa_s->global->p2p,
  3897. wpa_s->best_24_freq)) {
  3898. freq = wpa_s->best_24_freq;
  3899. wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
  3900. "channel: %d MHz", freq);
  3901. } else {
  3902. os_get_random((u8 *) &r, sizeof(r));
  3903. freq = 2412 + (r % 3) * 25;
  3904. wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
  3905. "channel: %d MHz", freq);
  3906. }
  3907. }
  3908. if (freq == 5) {
  3909. wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
  3910. "band");
  3911. if (wpa_s->best_5_freq > 0 &&
  3912. p2p_supported_freq_go(wpa_s->global->p2p,
  3913. wpa_s->best_5_freq)) {
  3914. freq = wpa_s->best_5_freq;
  3915. wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
  3916. "channel: %d MHz", freq);
  3917. } else {
  3918. os_get_random((u8 *) &r, sizeof(r));
  3919. freq = 5180 + (r % 4) * 20;
  3920. if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
  3921. wpa_printf(MSG_DEBUG, "P2P: Could not select "
  3922. "5 GHz channel for P2P group");
  3923. return -1;
  3924. }
  3925. wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
  3926. "channel: %d MHz", freq);
  3927. }
  3928. }
  3929. if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
  3930. wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
  3931. "(%u MHz) is not supported for P2P uses",
  3932. freq);
  3933. return -1;
  3934. }
  3935. return freq;
  3936. }
  3937. static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
  3938. struct p2p_go_neg_results *params,
  3939. int freq, int ht40, int vht,
  3940. const struct p2p_channels *channels)
  3941. {
  3942. int res, *freqs;
  3943. unsigned int pref_freq;
  3944. unsigned int num, i;
  3945. os_memset(params, 0, sizeof(*params));
  3946. params->role_go = 1;
  3947. params->ht40 = ht40;
  3948. params->vht = vht;
  3949. if (freq) {
  3950. if (!freq_included(channels, freq)) {
  3951. wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
  3952. "accepted", freq);
  3953. return -1;
  3954. }
  3955. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
  3956. "frequency %d MHz", freq);
  3957. params->freq = freq;
  3958. } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
  3959. wpa_s->conf->p2p_oper_channel >= 1 &&
  3960. wpa_s->conf->p2p_oper_channel <= 11 &&
  3961. freq_included(channels,
  3962. 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
  3963. params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
  3964. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
  3965. "frequency %d MHz", params->freq);
  3966. } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
  3967. wpa_s->conf->p2p_oper_reg_class == 116 ||
  3968. wpa_s->conf->p2p_oper_reg_class == 117 ||
  3969. wpa_s->conf->p2p_oper_reg_class == 124 ||
  3970. wpa_s->conf->p2p_oper_reg_class == 126 ||
  3971. wpa_s->conf->p2p_oper_reg_class == 127) &&
  3972. freq_included(channels,
  3973. 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
  3974. params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
  3975. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
  3976. "frequency %d MHz", params->freq);
  3977. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  3978. wpa_s->best_overall_freq > 0 &&
  3979. p2p_supported_freq_go(wpa_s->global->p2p,
  3980. wpa_s->best_overall_freq) &&
  3981. freq_included(channels, wpa_s->best_overall_freq)) {
  3982. params->freq = wpa_s->best_overall_freq;
  3983. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
  3984. "channel %d MHz", params->freq);
  3985. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  3986. wpa_s->best_24_freq > 0 &&
  3987. p2p_supported_freq_go(wpa_s->global->p2p,
  3988. wpa_s->best_24_freq) &&
  3989. freq_included(channels, wpa_s->best_24_freq)) {
  3990. params->freq = wpa_s->best_24_freq;
  3991. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
  3992. "channel %d MHz", params->freq);
  3993. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  3994. wpa_s->best_5_freq > 0 &&
  3995. p2p_supported_freq_go(wpa_s->global->p2p,
  3996. wpa_s->best_5_freq) &&
  3997. freq_included(channels, wpa_s->best_5_freq)) {
  3998. params->freq = wpa_s->best_5_freq;
  3999. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
  4000. "channel %d MHz", params->freq);
  4001. } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
  4002. channels))) {
  4003. params->freq = pref_freq;
  4004. wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
  4005. "channels", params->freq);
  4006. } else {
  4007. int chan;
  4008. for (chan = 0; chan < 11; chan++) {
  4009. params->freq = 2412 + chan * 5;
  4010. if (!wpas_p2p_disallowed_freq(wpa_s->global,
  4011. params->freq) &&
  4012. freq_included(channels, params->freq))
  4013. break;
  4014. }
  4015. if (chan == 11) {
  4016. wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
  4017. "allowed");
  4018. return -1;
  4019. }
  4020. wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
  4021. "known)", params->freq);
  4022. }
  4023. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  4024. if (!freqs)
  4025. return -1;
  4026. res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
  4027. wpa_s->num_multichan_concurrent);
  4028. if (res < 0) {
  4029. os_free(freqs);
  4030. return -1;
  4031. }
  4032. num = res;
  4033. for (i = 0; i < num; i++) {
  4034. if (freq && freqs[i] == freq)
  4035. break;
  4036. if (!freq && freq_included(channels, freqs[i])) {
  4037. wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
  4038. freqs[i]);
  4039. params->freq = freqs[i];
  4040. break;
  4041. }
  4042. }
  4043. if (i == num) {
  4044. if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
  4045. if (freq)
  4046. wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
  4047. else
  4048. wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
  4049. os_free(freqs);
  4050. return -1;
  4051. } else if (num == 0) {
  4052. wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
  4053. } else {
  4054. wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
  4055. }
  4056. }
  4057. os_free(freqs);
  4058. return 0;
  4059. }
  4060. static struct wpa_supplicant *
  4061. wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
  4062. int go)
  4063. {
  4064. struct wpa_supplicant *group_wpa_s;
  4065. if (!wpas_p2p_create_iface(wpa_s)) {
  4066. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
  4067. "operations");
  4068. wpa_s->p2p_first_connection_timeout = 0;
  4069. return wpa_s;
  4070. }
  4071. if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
  4072. WPA_IF_P2P_CLIENT) < 0) {
  4073. wpa_msg_global(wpa_s, MSG_ERROR,
  4074. "P2P: Failed to add group interface");
  4075. return NULL;
  4076. }
  4077. group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
  4078. if (group_wpa_s == NULL) {
  4079. wpa_msg_global(wpa_s, MSG_ERROR,
  4080. "P2P: Failed to initialize group interface");
  4081. wpas_p2p_remove_pending_group_interface(wpa_s);
  4082. return NULL;
  4083. }
  4084. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
  4085. group_wpa_s->ifname);
  4086. group_wpa_s->p2p_first_connection_timeout = 0;
  4087. return group_wpa_s;
  4088. }
  4089. /**
  4090. * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
  4091. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  4092. * @persistent_group: Whether to create a persistent group
  4093. * @freq: Frequency for the group or 0 to indicate no hardcoding
  4094. * @ht40: Start GO with 40 MHz channel width
  4095. * @vht: Start GO with VHT support
  4096. * Returns: 0 on success, -1 on failure
  4097. *
  4098. * This function creates a new P2P group with the local end as the Group Owner,
  4099. * i.e., without using Group Owner Negotiation.
  4100. */
  4101. int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
  4102. int freq, int ht40, int vht)
  4103. {
  4104. struct p2p_go_neg_results params;
  4105. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4106. return -1;
  4107. os_free(wpa_s->global->add_psk);
  4108. wpa_s->global->add_psk = NULL;
  4109. /* Make sure we are not running find during connection establishment */
  4110. wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
  4111. wpas_p2p_stop_find_oper(wpa_s);
  4112. freq = wpas_p2p_select_go_freq(wpa_s, freq);
  4113. if (freq < 0)
  4114. return -1;
  4115. if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, NULL))
  4116. return -1;
  4117. if (params.freq &&
  4118. !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
  4119. wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
  4120. "(%u MHz) is not supported for P2P uses",
  4121. params.freq);
  4122. return -1;
  4123. }
  4124. p2p_go_params(wpa_s->global->p2p, &params);
  4125. params.persistent_group = persistent_group;
  4126. wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
  4127. if (wpa_s == NULL)
  4128. return -1;
  4129. wpas_start_wps_go(wpa_s, &params, 0);
  4130. return 0;
  4131. }
  4132. static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
  4133. struct wpa_ssid *params, int addr_allocated)
  4134. {
  4135. struct wpa_ssid *ssid;
  4136. wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
  4137. if (wpa_s == NULL)
  4138. return -1;
  4139. wpa_s->p2p_last_4way_hs_fail = NULL;
  4140. wpa_supplicant_ap_deinit(wpa_s);
  4141. ssid = wpa_config_add_network(wpa_s->conf);
  4142. if (ssid == NULL)
  4143. return -1;
  4144. wpa_config_set_network_defaults(ssid);
  4145. ssid->temporary = 1;
  4146. ssid->proto = WPA_PROTO_RSN;
  4147. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  4148. ssid->group_cipher = WPA_CIPHER_CCMP;
  4149. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  4150. ssid->ssid = os_malloc(params->ssid_len);
  4151. if (ssid->ssid == NULL) {
  4152. wpa_config_remove_network(wpa_s->conf, ssid->id);
  4153. return -1;
  4154. }
  4155. os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
  4156. ssid->ssid_len = params->ssid_len;
  4157. ssid->p2p_group = 1;
  4158. ssid->export_keys = 1;
  4159. if (params->psk_set) {
  4160. os_memcpy(ssid->psk, params->psk, 32);
  4161. ssid->psk_set = 1;
  4162. }
  4163. if (params->passphrase)
  4164. ssid->passphrase = os_strdup(params->passphrase);
  4165. wpa_s->show_group_started = 1;
  4166. wpa_supplicant_select_network(wpa_s, ssid);
  4167. return 0;
  4168. }
  4169. int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
  4170. struct wpa_ssid *ssid, int addr_allocated,
  4171. int freq, int ht40, int vht,
  4172. const struct p2p_channels *channels,
  4173. int connection_timeout)
  4174. {
  4175. struct p2p_go_neg_results params;
  4176. int go = 0;
  4177. if (ssid->disabled != 2 || ssid->ssid == NULL)
  4178. return -1;
  4179. if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
  4180. go == (ssid->mode == WPAS_MODE_P2P_GO)) {
  4181. wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
  4182. "already running");
  4183. return 0;
  4184. }
  4185. os_free(wpa_s->global->add_psk);
  4186. wpa_s->global->add_psk = NULL;
  4187. /* Make sure we are not running find during connection establishment */
  4188. wpas_p2p_stop_find_oper(wpa_s);
  4189. wpa_s->p2p_fallback_to_go_neg = 0;
  4190. if (ssid->mode == WPAS_MODE_INFRA)
  4191. return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
  4192. if (ssid->mode != WPAS_MODE_P2P_GO)
  4193. return -1;
  4194. freq = wpas_p2p_select_go_freq(wpa_s, freq);
  4195. if (freq < 0)
  4196. return -1;
  4197. if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, channels))
  4198. return -1;
  4199. params.role_go = 1;
  4200. params.psk_set = ssid->psk_set;
  4201. if (params.psk_set)
  4202. os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
  4203. if (ssid->passphrase) {
  4204. if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
  4205. wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
  4206. "persistent group");
  4207. return -1;
  4208. }
  4209. os_strlcpy(params.passphrase, ssid->passphrase,
  4210. sizeof(params.passphrase));
  4211. }
  4212. os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
  4213. params.ssid_len = ssid->ssid_len;
  4214. params.persistent_group = 1;
  4215. wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
  4216. if (wpa_s == NULL)
  4217. return -1;
  4218. wpa_s->p2p_first_connection_timeout = connection_timeout;
  4219. wpas_start_wps_go(wpa_s, &params, 0);
  4220. return 0;
  4221. }
  4222. static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
  4223. struct wpabuf *proberesp_ies)
  4224. {
  4225. struct wpa_supplicant *wpa_s = ctx;
  4226. if (wpa_s->ap_iface) {
  4227. struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
  4228. if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
  4229. wpabuf_free(beacon_ies);
  4230. wpabuf_free(proberesp_ies);
  4231. return;
  4232. }
  4233. if (beacon_ies) {
  4234. wpabuf_free(hapd->p2p_beacon_ie);
  4235. hapd->p2p_beacon_ie = beacon_ies;
  4236. }
  4237. wpabuf_free(hapd->p2p_probe_resp_ie);
  4238. hapd->p2p_probe_resp_ie = proberesp_ies;
  4239. } else {
  4240. wpabuf_free(beacon_ies);
  4241. wpabuf_free(proberesp_ies);
  4242. }
  4243. wpa_supplicant_ap_update_beacon(wpa_s);
  4244. }
  4245. static void wpas_p2p_idle_update(void *ctx, int idle)
  4246. {
  4247. struct wpa_supplicant *wpa_s = ctx;
  4248. if (!wpa_s->ap_iface)
  4249. return;
  4250. wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
  4251. if (idle)
  4252. wpas_p2p_set_group_idle_timeout(wpa_s);
  4253. else
  4254. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  4255. }
  4256. struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
  4257. struct wpa_ssid *ssid)
  4258. {
  4259. struct p2p_group *group;
  4260. struct p2p_group_config *cfg;
  4261. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4262. return NULL;
  4263. cfg = os_zalloc(sizeof(*cfg));
  4264. if (cfg == NULL)
  4265. return NULL;
  4266. if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
  4267. cfg->persistent_group = 2;
  4268. else if (ssid->p2p_persistent_group)
  4269. cfg->persistent_group = 1;
  4270. os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
  4271. if (wpa_s->max_stations &&
  4272. wpa_s->max_stations < wpa_s->conf->max_num_sta)
  4273. cfg->max_clients = wpa_s->max_stations;
  4274. else
  4275. cfg->max_clients = wpa_s->conf->max_num_sta;
  4276. os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
  4277. cfg->ssid_len = ssid->ssid_len;
  4278. cfg->cb_ctx = wpa_s;
  4279. cfg->ie_update = wpas_p2p_ie_update;
  4280. cfg->idle_update = wpas_p2p_idle_update;
  4281. group = p2p_group_init(wpa_s->global->p2p, cfg);
  4282. if (group == NULL)
  4283. os_free(cfg);
  4284. if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
  4285. p2p_group_notif_formation_done(group);
  4286. wpa_s->p2p_group = group;
  4287. return group;
  4288. }
  4289. void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  4290. int registrar)
  4291. {
  4292. struct wpa_ssid *ssid = wpa_s->current_ssid;
  4293. if (!wpa_s->p2p_in_provisioning) {
  4294. wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
  4295. "provisioning not in progress");
  4296. return;
  4297. }
  4298. if (ssid && ssid->mode == WPAS_MODE_INFRA) {
  4299. u8 go_dev_addr[ETH_ALEN];
  4300. os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
  4301. wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
  4302. ssid->ssid_len);
  4303. /* Clear any stored provisioning info */
  4304. p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
  4305. }
  4306. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
  4307. NULL);
  4308. wpa_s->p2p_go_group_formation_completed = 1;
  4309. if (ssid && ssid->mode == WPAS_MODE_INFRA) {
  4310. /*
  4311. * Use a separate timeout for initial data connection to
  4312. * complete to allow the group to be removed automatically if
  4313. * something goes wrong in this step before the P2P group idle
  4314. * timeout mechanism is taken into use.
  4315. */
  4316. wpa_dbg(wpa_s, MSG_DEBUG,
  4317. "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
  4318. P2P_MAX_INITIAL_CONN_WAIT);
  4319. eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
  4320. wpas_p2p_group_formation_timeout,
  4321. wpa_s->parent, NULL);
  4322. } else if (ssid) {
  4323. /*
  4324. * Use a separate timeout for initial data connection to
  4325. * complete to allow the group to be removed automatically if
  4326. * the client does not complete data connection successfully.
  4327. */
  4328. wpa_dbg(wpa_s, MSG_DEBUG,
  4329. "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
  4330. P2P_MAX_INITIAL_CONN_WAIT_GO);
  4331. eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
  4332. wpas_p2p_group_formation_timeout,
  4333. wpa_s->parent, NULL);
  4334. /*
  4335. * Complete group formation on first successful data connection
  4336. */
  4337. wpa_s->p2p_go_group_formation_completed = 0;
  4338. }
  4339. if (wpa_s->global->p2p)
  4340. p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
  4341. wpas_group_formation_completed(wpa_s, 1);
  4342. }
  4343. void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
  4344. struct wps_event_fail *fail)
  4345. {
  4346. if (!wpa_s->p2p_in_provisioning) {
  4347. wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
  4348. "provisioning not in progress");
  4349. return;
  4350. }
  4351. if (wpa_s->go_params) {
  4352. p2p_clear_provisioning_info(
  4353. wpa_s->global->p2p,
  4354. wpa_s->go_params->peer_device_addr);
  4355. }
  4356. wpas_notify_p2p_wps_failed(wpa_s, fail);
  4357. }
  4358. int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  4359. const char *config_method,
  4360. enum wpas_p2p_prov_disc_use use)
  4361. {
  4362. u16 config_methods;
  4363. wpa_s->p2p_fallback_to_go_neg = 0;
  4364. wpa_s->pending_pd_use = NORMAL_PD;
  4365. if (os_strncmp(config_method, "display", 7) == 0)
  4366. config_methods = WPS_CONFIG_DISPLAY;
  4367. else if (os_strncmp(config_method, "keypad", 6) == 0)
  4368. config_methods = WPS_CONFIG_KEYPAD;
  4369. else if (os_strncmp(config_method, "pbc", 3) == 0 ||
  4370. os_strncmp(config_method, "pushbutton", 10) == 0)
  4371. config_methods = WPS_CONFIG_PUSHBUTTON;
  4372. else {
  4373. wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
  4374. return -1;
  4375. }
  4376. if (use == WPAS_P2P_PD_AUTO) {
  4377. os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
  4378. wpa_s->pending_pd_config_methods = config_methods;
  4379. wpa_s->p2p_auto_pd = 1;
  4380. wpa_s->p2p_auto_join = 0;
  4381. wpa_s->pending_pd_before_join = 0;
  4382. wpa_s->auto_pd_scan_retry = 0;
  4383. wpas_p2p_stop_find(wpa_s);
  4384. wpa_s->p2p_join_scan_count = 0;
  4385. os_get_reltime(&wpa_s->p2p_auto_started);
  4386. wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
  4387. wpa_s->p2p_auto_started.sec,
  4388. wpa_s->p2p_auto_started.usec);
  4389. wpas_p2p_join_scan(wpa_s, NULL);
  4390. return 0;
  4391. }
  4392. if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
  4393. return -1;
  4394. return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
  4395. config_methods, use == WPAS_P2P_PD_FOR_JOIN,
  4396. 0, 1);
  4397. }
  4398. int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
  4399. char *end)
  4400. {
  4401. return p2p_scan_result_text(ies, ies_len, buf, end);
  4402. }
  4403. static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
  4404. {
  4405. if (!offchannel_pending_action_tx(wpa_s))
  4406. return;
  4407. wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
  4408. "operation request");
  4409. offchannel_clear_pending_action_tx(wpa_s);
  4410. }
  4411. int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
  4412. enum p2p_discovery_type type,
  4413. unsigned int num_req_dev_types, const u8 *req_dev_types,
  4414. const u8 *dev_id, unsigned int search_delay)
  4415. {
  4416. wpas_p2p_clear_pending_action_tx(wpa_s);
  4417. wpa_s->p2p_long_listen = 0;
  4418. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
  4419. wpa_s->p2p_in_provisioning)
  4420. return -1;
  4421. wpa_supplicant_cancel_sched_scan(wpa_s);
  4422. return p2p_find(wpa_s->global->p2p, timeout, type,
  4423. num_req_dev_types, req_dev_types, dev_id,
  4424. search_delay);
  4425. }
  4426. static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
  4427. {
  4428. wpas_p2p_clear_pending_action_tx(wpa_s);
  4429. wpa_s->p2p_long_listen = 0;
  4430. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  4431. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  4432. if (wpa_s->global->p2p)
  4433. p2p_stop_find(wpa_s->global->p2p);
  4434. return 0;
  4435. }
  4436. void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
  4437. {
  4438. if (wpas_p2p_stop_find_oper(wpa_s) > 0)
  4439. return;
  4440. wpas_p2p_remove_pending_group_interface(wpa_s);
  4441. }
  4442. static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
  4443. {
  4444. struct wpa_supplicant *wpa_s = eloop_ctx;
  4445. wpa_s->p2p_long_listen = 0;
  4446. }
  4447. int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
  4448. {
  4449. int res;
  4450. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4451. return -1;
  4452. wpa_supplicant_cancel_sched_scan(wpa_s);
  4453. wpas_p2p_clear_pending_action_tx(wpa_s);
  4454. if (timeout == 0) {
  4455. /*
  4456. * This is a request for unlimited Listen state. However, at
  4457. * least for now, this is mapped to a Listen state for one
  4458. * hour.
  4459. */
  4460. timeout = 3600;
  4461. }
  4462. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  4463. wpa_s->p2p_long_listen = 0;
  4464. /*
  4465. * Stop previous find/listen operation to avoid trying to request a new
  4466. * remain-on-channel operation while the driver is still running the
  4467. * previous one.
  4468. */
  4469. if (wpa_s->global->p2p)
  4470. p2p_stop_find(wpa_s->global->p2p);
  4471. res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
  4472. if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
  4473. wpa_s->p2p_long_listen = timeout * 1000;
  4474. eloop_register_timeout(timeout, 0,
  4475. wpas_p2p_long_listen_timeout,
  4476. wpa_s, NULL);
  4477. }
  4478. return res;
  4479. }
  4480. int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  4481. u8 *buf, size_t len, int p2p_group)
  4482. {
  4483. struct wpabuf *p2p_ie;
  4484. int ret;
  4485. if (wpa_s->global->p2p_disabled)
  4486. return -1;
  4487. if (wpa_s->global->p2p == NULL)
  4488. return -1;
  4489. if (bss == NULL)
  4490. return -1;
  4491. p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  4492. ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
  4493. p2p_group, p2p_ie);
  4494. wpabuf_free(p2p_ie);
  4495. return ret;
  4496. }
  4497. int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
  4498. const u8 *dst, const u8 *bssid,
  4499. const u8 *ie, size_t ie_len, int ssi_signal)
  4500. {
  4501. if (wpa_s->global->p2p_disabled)
  4502. return 0;
  4503. if (wpa_s->global->p2p == NULL)
  4504. return 0;
  4505. switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
  4506. ie, ie_len)) {
  4507. case P2P_PREQ_NOT_P2P:
  4508. wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
  4509. ssi_signal);
  4510. /* fall through */
  4511. case P2P_PREQ_MALFORMED:
  4512. case P2P_PREQ_NOT_LISTEN:
  4513. case P2P_PREQ_NOT_PROCESSED:
  4514. default: /* make gcc happy */
  4515. return 0;
  4516. case P2P_PREQ_PROCESSED:
  4517. return 1;
  4518. }
  4519. }
  4520. void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
  4521. const u8 *sa, const u8 *bssid,
  4522. u8 category, const u8 *data, size_t len, int freq)
  4523. {
  4524. if (wpa_s->global->p2p_disabled)
  4525. return;
  4526. if (wpa_s->global->p2p == NULL)
  4527. return;
  4528. p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
  4529. freq);
  4530. }
  4531. void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
  4532. {
  4533. if (wpa_s->global->p2p_disabled)
  4534. return;
  4535. if (wpa_s->global->p2p == NULL)
  4536. return;
  4537. p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
  4538. }
  4539. void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
  4540. {
  4541. p2p_group_deinit(wpa_s->p2p_group);
  4542. wpa_s->p2p_group = NULL;
  4543. wpa_s->ap_configured_cb = NULL;
  4544. wpa_s->ap_configured_cb_ctx = NULL;
  4545. wpa_s->ap_configured_cb_data = NULL;
  4546. wpa_s->connect_without_scan = NULL;
  4547. }
  4548. int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
  4549. {
  4550. wpa_s->p2p_long_listen = 0;
  4551. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4552. return -1;
  4553. return p2p_reject(wpa_s->global->p2p, addr);
  4554. }
  4555. /* Invite to reinvoke a persistent group */
  4556. int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  4557. struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
  4558. int ht40, int vht, int pref_freq)
  4559. {
  4560. enum p2p_invite_role role;
  4561. u8 *bssid = NULL;
  4562. int force_freq = 0;
  4563. int res;
  4564. int no_pref_freq_given = pref_freq == 0;
  4565. wpa_s->global->p2p_invite_group = NULL;
  4566. if (peer_addr)
  4567. os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
  4568. else
  4569. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  4570. wpa_s->p2p_persistent_go_freq = freq;
  4571. wpa_s->p2p_go_ht40 = !!ht40;
  4572. if (ssid->mode == WPAS_MODE_P2P_GO) {
  4573. role = P2P_INVITE_ROLE_GO;
  4574. if (peer_addr == NULL) {
  4575. wpa_printf(MSG_DEBUG, "P2P: Missing peer "
  4576. "address in invitation command");
  4577. return -1;
  4578. }
  4579. if (wpas_p2p_create_iface(wpa_s)) {
  4580. if (wpas_p2p_add_group_interface(wpa_s,
  4581. WPA_IF_P2P_GO) < 0) {
  4582. wpa_printf(MSG_ERROR, "P2P: Failed to "
  4583. "allocate a new interface for the "
  4584. "group");
  4585. return -1;
  4586. }
  4587. bssid = wpa_s->pending_interface_addr;
  4588. } else
  4589. bssid = wpa_s->own_addr;
  4590. } else {
  4591. role = P2P_INVITE_ROLE_CLIENT;
  4592. peer_addr = ssid->bssid;
  4593. }
  4594. wpa_s->pending_invite_ssid_id = ssid->id;
  4595. res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
  4596. role == P2P_INVITE_ROLE_GO);
  4597. if (res)
  4598. return res;
  4599. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4600. return -1;
  4601. if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
  4602. no_pref_freq_given && pref_freq > 0 &&
  4603. wpa_s->num_multichan_concurrent > 1 &&
  4604. wpas_p2p_num_unused_channels(wpa_s) > 0) {
  4605. wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
  4606. pref_freq);
  4607. pref_freq = 0;
  4608. }
  4609. return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
  4610. ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
  4611. 1, pref_freq);
  4612. }
  4613. /* Invite to join an active group */
  4614. int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
  4615. const u8 *peer_addr, const u8 *go_dev_addr)
  4616. {
  4617. struct wpa_global *global = wpa_s->global;
  4618. enum p2p_invite_role role;
  4619. u8 *bssid = NULL;
  4620. struct wpa_ssid *ssid;
  4621. int persistent;
  4622. int freq = 0, force_freq = 0, pref_freq = 0;
  4623. int res;
  4624. wpa_s->p2p_persistent_go_freq = 0;
  4625. wpa_s->p2p_go_ht40 = 0;
  4626. wpa_s->p2p_go_vht = 0;
  4627. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  4628. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  4629. break;
  4630. }
  4631. if (wpa_s == NULL) {
  4632. wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
  4633. return -1;
  4634. }
  4635. ssid = wpa_s->current_ssid;
  4636. if (ssid == NULL) {
  4637. wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
  4638. "invitation");
  4639. return -1;
  4640. }
  4641. wpa_s->global->p2p_invite_group = wpa_s;
  4642. persistent = ssid->p2p_persistent_group &&
  4643. wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
  4644. ssid->ssid, ssid->ssid_len);
  4645. if (ssid->mode == WPAS_MODE_P2P_GO) {
  4646. role = P2P_INVITE_ROLE_ACTIVE_GO;
  4647. bssid = wpa_s->own_addr;
  4648. if (go_dev_addr == NULL)
  4649. go_dev_addr = wpa_s->global->p2p_dev_addr;
  4650. freq = ssid->frequency;
  4651. } else {
  4652. role = P2P_INVITE_ROLE_CLIENT;
  4653. if (wpa_s->wpa_state < WPA_ASSOCIATED) {
  4654. wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
  4655. "invite to current group");
  4656. return -1;
  4657. }
  4658. bssid = wpa_s->bssid;
  4659. if (go_dev_addr == NULL &&
  4660. !is_zero_ether_addr(wpa_s->go_dev_addr))
  4661. go_dev_addr = wpa_s->go_dev_addr;
  4662. freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
  4663. (int) wpa_s->assoc_freq;
  4664. }
  4665. wpa_s->parent->pending_invite_ssid_id = -1;
  4666. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4667. return -1;
  4668. res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
  4669. role == P2P_INVITE_ROLE_ACTIVE_GO);
  4670. if (res)
  4671. return res;
  4672. wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
  4673. return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
  4674. ssid->ssid, ssid->ssid_len, force_freq,
  4675. go_dev_addr, persistent, pref_freq);
  4676. }
  4677. void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
  4678. {
  4679. struct wpa_ssid *ssid = wpa_s->current_ssid;
  4680. const char *ssid_txt;
  4681. u8 go_dev_addr[ETH_ALEN];
  4682. int network_id = -1;
  4683. int persistent;
  4684. int freq;
  4685. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
  4686. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  4687. wpa_s->parent, NULL);
  4688. }
  4689. if (!wpa_s->show_group_started || !ssid)
  4690. return;
  4691. wpa_s->show_group_started = 0;
  4692. ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
  4693. os_memset(go_dev_addr, 0, ETH_ALEN);
  4694. if (ssid->bssid_set)
  4695. os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
  4696. persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
  4697. ssid->ssid_len);
  4698. os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
  4699. if (wpa_s->global->p2p_group_formation == wpa_s)
  4700. wpa_s->global->p2p_group_formation = NULL;
  4701. freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
  4702. (int) wpa_s->assoc_freq;
  4703. if (ssid->passphrase == NULL && ssid->psk_set) {
  4704. char psk[65];
  4705. wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
  4706. wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  4707. "%s client ssid=\"%s\" freq=%d psk=%s "
  4708. "go_dev_addr=" MACSTR "%s",
  4709. wpa_s->ifname, ssid_txt, freq, psk,
  4710. MAC2STR(go_dev_addr),
  4711. persistent ? " [PERSISTENT]" : "");
  4712. } else {
  4713. wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  4714. "%s client ssid=\"%s\" freq=%d "
  4715. "passphrase=\"%s\" go_dev_addr=" MACSTR "%s",
  4716. wpa_s->ifname, ssid_txt, freq,
  4717. ssid->passphrase ? ssid->passphrase : "",
  4718. MAC2STR(go_dev_addr),
  4719. persistent ? " [PERSISTENT]" : "");
  4720. }
  4721. if (persistent)
  4722. network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
  4723. ssid, go_dev_addr);
  4724. if (network_id < 0)
  4725. network_id = ssid->id;
  4726. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
  4727. }
  4728. int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
  4729. u32 interval1, u32 duration2, u32 interval2)
  4730. {
  4731. int ret;
  4732. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4733. return -1;
  4734. if (wpa_s->wpa_state < WPA_ASSOCIATED ||
  4735. wpa_s->current_ssid == NULL ||
  4736. wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
  4737. return -1;
  4738. ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
  4739. wpa_s->own_addr, wpa_s->assoc_freq,
  4740. duration1, interval1, duration2, interval2);
  4741. if (ret == 0)
  4742. wpa_s->waiting_presence_resp = 1;
  4743. return ret;
  4744. }
  4745. int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
  4746. unsigned int interval)
  4747. {
  4748. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4749. return -1;
  4750. return p2p_ext_listen(wpa_s->global->p2p, period, interval);
  4751. }
  4752. static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
  4753. {
  4754. if (wpa_s->current_ssid == NULL) {
  4755. /*
  4756. * current_ssid can be cleared when P2P client interface gets
  4757. * disconnected, so assume this interface was used as P2P
  4758. * client.
  4759. */
  4760. return 1;
  4761. }
  4762. return wpa_s->current_ssid->p2p_group &&
  4763. wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
  4764. }
  4765. static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
  4766. {
  4767. struct wpa_supplicant *wpa_s = eloop_ctx;
  4768. if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
  4769. wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
  4770. "disabled");
  4771. return;
  4772. }
  4773. wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
  4774. "group");
  4775. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
  4776. }
  4777. static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
  4778. {
  4779. int timeout;
  4780. if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
  4781. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
  4782. if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
  4783. return;
  4784. timeout = wpa_s->conf->p2p_group_idle;
  4785. if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
  4786. (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
  4787. timeout = P2P_MAX_CLIENT_IDLE;
  4788. if (timeout == 0)
  4789. return;
  4790. if (timeout < 0) {
  4791. if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
  4792. timeout = 0; /* special client mode no-timeout */
  4793. else
  4794. return;
  4795. }
  4796. if (wpa_s->p2p_in_provisioning) {
  4797. /*
  4798. * Use the normal group formation timeout during the
  4799. * provisioning phase to avoid terminating this process too
  4800. * early due to group idle timeout.
  4801. */
  4802. wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
  4803. "during provisioning");
  4804. return;
  4805. }
  4806. if (wpa_s->show_group_started) {
  4807. /*
  4808. * Use the normal group formation timeout between the end of
  4809. * the provisioning phase and completion of 4-way handshake to
  4810. * avoid terminating this process too early due to group idle
  4811. * timeout.
  4812. */
  4813. wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
  4814. "while waiting for initial 4-way handshake to "
  4815. "complete");
  4816. return;
  4817. }
  4818. wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
  4819. timeout);
  4820. eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
  4821. wpa_s, NULL);
  4822. }
  4823. /* Returns 1 if the interface was removed */
  4824. int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
  4825. u16 reason_code, const u8 *ie, size_t ie_len,
  4826. int locally_generated)
  4827. {
  4828. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4829. return 0;
  4830. if (!locally_generated)
  4831. p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
  4832. ie_len);
  4833. if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
  4834. wpa_s->current_ssid &&
  4835. wpa_s->current_ssid->p2p_group &&
  4836. wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
  4837. wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
  4838. "session is ending");
  4839. if (wpas_p2p_group_delete(wpa_s,
  4840. P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
  4841. > 0)
  4842. return 1;
  4843. }
  4844. return 0;
  4845. }
  4846. void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
  4847. u16 reason_code, const u8 *ie, size_t ie_len,
  4848. int locally_generated)
  4849. {
  4850. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4851. return;
  4852. if (!locally_generated)
  4853. p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
  4854. ie_len);
  4855. }
  4856. void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
  4857. {
  4858. struct p2p_data *p2p = wpa_s->global->p2p;
  4859. if (p2p == NULL)
  4860. return;
  4861. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
  4862. return;
  4863. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
  4864. p2p_set_dev_name(p2p, wpa_s->conf->device_name);
  4865. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
  4866. p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
  4867. if (wpa_s->wps &&
  4868. (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
  4869. p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
  4870. if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
  4871. p2p_set_uuid(p2p, wpa_s->wps->uuid);
  4872. if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
  4873. p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
  4874. p2p_set_model_name(p2p, wpa_s->conf->model_name);
  4875. p2p_set_model_number(p2p, wpa_s->conf->model_number);
  4876. p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
  4877. }
  4878. if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
  4879. p2p_set_sec_dev_types(p2p,
  4880. (void *) wpa_s->conf->sec_device_type,
  4881. wpa_s->conf->num_sec_device_types);
  4882. if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
  4883. int i;
  4884. p2p_remove_wps_vendor_extensions(p2p);
  4885. for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
  4886. if (wpa_s->conf->wps_vendor_ext[i] == NULL)
  4887. continue;
  4888. p2p_add_wps_vendor_extension(
  4889. p2p, wpa_s->conf->wps_vendor_ext[i]);
  4890. }
  4891. }
  4892. if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
  4893. wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  4894. char country[3];
  4895. country[0] = wpa_s->conf->country[0];
  4896. country[1] = wpa_s->conf->country[1];
  4897. country[2] = 0x04;
  4898. p2p_set_country(p2p, country);
  4899. }
  4900. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
  4901. p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
  4902. wpa_s->conf->p2p_ssid_postfix ?
  4903. os_strlen(wpa_s->conf->p2p_ssid_postfix) :
  4904. 0);
  4905. }
  4906. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
  4907. p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
  4908. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
  4909. u8 reg_class, channel;
  4910. int ret;
  4911. unsigned int r;
  4912. if (wpa_s->conf->p2p_listen_reg_class &&
  4913. wpa_s->conf->p2p_listen_channel) {
  4914. reg_class = wpa_s->conf->p2p_listen_reg_class;
  4915. channel = wpa_s->conf->p2p_listen_channel;
  4916. } else {
  4917. reg_class = 81;
  4918. /*
  4919. * Pick one of the social channels randomly as the
  4920. * listen channel.
  4921. */
  4922. os_get_random((u8 *) &r, sizeof(r));
  4923. channel = 1 + (r % 3) * 5;
  4924. }
  4925. ret = p2p_set_listen_channel(p2p, reg_class, channel);
  4926. if (ret)
  4927. wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
  4928. "failed: %d", ret);
  4929. }
  4930. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
  4931. u8 op_reg_class, op_channel, cfg_op_channel;
  4932. int ret = 0;
  4933. unsigned int r;
  4934. if (wpa_s->conf->p2p_oper_reg_class &&
  4935. wpa_s->conf->p2p_oper_channel) {
  4936. op_reg_class = wpa_s->conf->p2p_oper_reg_class;
  4937. op_channel = wpa_s->conf->p2p_oper_channel;
  4938. cfg_op_channel = 1;
  4939. } else {
  4940. op_reg_class = 81;
  4941. /*
  4942. * Use random operation channel from (1, 6, 11)
  4943. *if no other preference is indicated.
  4944. */
  4945. os_get_random((u8 *) &r, sizeof(r));
  4946. op_channel = 1 + (r % 3) * 5;
  4947. cfg_op_channel = 0;
  4948. }
  4949. ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
  4950. cfg_op_channel);
  4951. if (ret)
  4952. wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
  4953. "failed: %d", ret);
  4954. }
  4955. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
  4956. if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
  4957. wpa_s->conf->p2p_pref_chan) < 0) {
  4958. wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
  4959. "update failed");
  4960. }
  4961. if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
  4962. wpa_printf(MSG_ERROR, "P2P: No GO channel list "
  4963. "update failed");
  4964. }
  4965. }
  4966. }
  4967. int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
  4968. int duration)
  4969. {
  4970. if (!wpa_s->ap_iface)
  4971. return -1;
  4972. return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
  4973. duration);
  4974. }
  4975. int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
  4976. {
  4977. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4978. return -1;
  4979. wpa_s->global->cross_connection = enabled;
  4980. p2p_set_cross_connect(wpa_s->global->p2p, enabled);
  4981. if (!enabled) {
  4982. struct wpa_supplicant *iface;
  4983. for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
  4984. {
  4985. if (iface->cross_connect_enabled == 0)
  4986. continue;
  4987. iface->cross_connect_enabled = 0;
  4988. iface->cross_connect_in_use = 0;
  4989. wpa_msg_global(iface->parent, MSG_INFO,
  4990. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  4991. iface->ifname,
  4992. iface->cross_connect_uplink);
  4993. }
  4994. }
  4995. return 0;
  4996. }
  4997. static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
  4998. {
  4999. struct wpa_supplicant *iface;
  5000. if (!uplink->global->cross_connection)
  5001. return;
  5002. for (iface = uplink->global->ifaces; iface; iface = iface->next) {
  5003. if (!iface->cross_connect_enabled)
  5004. continue;
  5005. if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
  5006. 0)
  5007. continue;
  5008. if (iface->ap_iface == NULL)
  5009. continue;
  5010. if (iface->cross_connect_in_use)
  5011. continue;
  5012. iface->cross_connect_in_use = 1;
  5013. wpa_msg_global(iface->parent, MSG_INFO,
  5014. P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
  5015. iface->ifname, iface->cross_connect_uplink);
  5016. }
  5017. }
  5018. static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
  5019. {
  5020. struct wpa_supplicant *iface;
  5021. for (iface = uplink->global->ifaces; iface; iface = iface->next) {
  5022. if (!iface->cross_connect_enabled)
  5023. continue;
  5024. if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
  5025. 0)
  5026. continue;
  5027. if (!iface->cross_connect_in_use)
  5028. continue;
  5029. wpa_msg_global(iface->parent, MSG_INFO,
  5030. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  5031. iface->ifname, iface->cross_connect_uplink);
  5032. iface->cross_connect_in_use = 0;
  5033. }
  5034. }
  5035. void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
  5036. {
  5037. if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
  5038. wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
  5039. wpa_s->cross_connect_disallowed)
  5040. wpas_p2p_disable_cross_connect(wpa_s);
  5041. else
  5042. wpas_p2p_enable_cross_connect(wpa_s);
  5043. if (!wpa_s->ap_iface &&
  5044. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
  5045. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
  5046. }
  5047. void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
  5048. {
  5049. wpas_p2p_disable_cross_connect(wpa_s);
  5050. if (!wpa_s->ap_iface &&
  5051. !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
  5052. wpa_s, NULL))
  5053. wpas_p2p_set_group_idle_timeout(wpa_s);
  5054. }
  5055. static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
  5056. {
  5057. struct wpa_supplicant *iface;
  5058. if (!wpa_s->global->cross_connection)
  5059. return;
  5060. for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
  5061. if (iface == wpa_s)
  5062. continue;
  5063. if (iface->drv_flags &
  5064. WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
  5065. continue;
  5066. if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
  5067. continue;
  5068. wpa_s->cross_connect_enabled = 1;
  5069. os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
  5070. sizeof(wpa_s->cross_connect_uplink));
  5071. wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
  5072. "%s to %s whenever uplink is available",
  5073. wpa_s->ifname, wpa_s->cross_connect_uplink);
  5074. if (iface->ap_iface || iface->current_ssid == NULL ||
  5075. iface->current_ssid->mode != WPAS_MODE_INFRA ||
  5076. iface->cross_connect_disallowed ||
  5077. iface->wpa_state != WPA_COMPLETED)
  5078. break;
  5079. wpa_s->cross_connect_in_use = 1;
  5080. wpa_msg_global(wpa_s->parent, MSG_INFO,
  5081. P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
  5082. wpa_s->ifname, wpa_s->cross_connect_uplink);
  5083. break;
  5084. }
  5085. }
  5086. int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
  5087. {
  5088. if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
  5089. !wpa_s->p2p_in_provisioning)
  5090. return 0; /* not P2P client operation */
  5091. wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
  5092. "session overlap");
  5093. if (wpa_s != wpa_s->parent)
  5094. wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
  5095. wpas_p2p_group_formation_failed(wpa_s);
  5096. return 1;
  5097. }
  5098. void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
  5099. {
  5100. struct p2p_channels chan, cli_chan;
  5101. if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
  5102. return;
  5103. os_memset(&chan, 0, sizeof(chan));
  5104. os_memset(&cli_chan, 0, sizeof(cli_chan));
  5105. if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
  5106. wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
  5107. "channel list");
  5108. return;
  5109. }
  5110. p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
  5111. }
  5112. static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
  5113. struct wpa_scan_results *scan_res)
  5114. {
  5115. wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
  5116. }
  5117. int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
  5118. {
  5119. struct wpa_global *global = wpa_s->global;
  5120. int found = 0;
  5121. const u8 *peer;
  5122. if (global->p2p == NULL)
  5123. return -1;
  5124. wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
  5125. if (wpa_s->pending_interface_name[0] &&
  5126. !is_zero_ether_addr(wpa_s->pending_interface_addr))
  5127. found = 1;
  5128. peer = p2p_get_go_neg_peer(global->p2p);
  5129. if (peer) {
  5130. wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
  5131. MACSTR, MAC2STR(peer));
  5132. p2p_unauthorize(global->p2p, peer);
  5133. found = 1;
  5134. }
  5135. if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
  5136. wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
  5137. wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
  5138. found = 1;
  5139. }
  5140. if (wpa_s->pending_pd_before_join) {
  5141. wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
  5142. wpa_s->pending_pd_before_join = 0;
  5143. found = 1;
  5144. }
  5145. wpas_p2p_stop_find(wpa_s);
  5146. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  5147. if (wpa_s == global->p2p_group_formation &&
  5148. (wpa_s->p2p_in_provisioning ||
  5149. wpa_s->parent->pending_interface_type ==
  5150. WPA_IF_P2P_CLIENT)) {
  5151. wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
  5152. "formation found - cancelling",
  5153. wpa_s->ifname);
  5154. found = 1;
  5155. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  5156. wpa_s->parent, NULL);
  5157. if (wpa_s->p2p_in_provisioning) {
  5158. wpas_group_formation_completed(wpa_s, 0);
  5159. break;
  5160. }
  5161. wpas_p2p_group_delete(wpa_s,
  5162. P2P_GROUP_REMOVAL_REQUESTED);
  5163. break;
  5164. }
  5165. }
  5166. if (!found) {
  5167. wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
  5168. return -1;
  5169. }
  5170. return 0;
  5171. }
  5172. void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
  5173. {
  5174. if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
  5175. return;
  5176. wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
  5177. "being available anymore");
  5178. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
  5179. }
  5180. void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
  5181. int freq_24, int freq_5, int freq_overall)
  5182. {
  5183. struct p2p_data *p2p = wpa_s->global->p2p;
  5184. if (p2p == NULL)
  5185. return;
  5186. p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
  5187. }
  5188. int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
  5189. {
  5190. u8 peer[ETH_ALEN];
  5191. struct p2p_data *p2p = wpa_s->global->p2p;
  5192. if (p2p == NULL)
  5193. return -1;
  5194. if (hwaddr_aton(addr, peer))
  5195. return -1;
  5196. return p2p_unauthorize(p2p, peer);
  5197. }
  5198. /**
  5199. * wpas_p2p_disconnect - Disconnect from a P2P Group
  5200. * @wpa_s: Pointer to wpa_supplicant data
  5201. * Returns: 0 on success, -1 on failure
  5202. *
  5203. * This can be used to disconnect from a group in which the local end is a P2P
  5204. * Client or to end a P2P Group in case the local end is the Group Owner. If a
  5205. * virtual network interface was created for this group, that interface will be
  5206. * removed. Otherwise, only the configured P2P group network will be removed
  5207. * from the interface.
  5208. */
  5209. int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
  5210. {
  5211. if (wpa_s == NULL)
  5212. return -1;
  5213. return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
  5214. -1 : 0;
  5215. }
  5216. int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
  5217. {
  5218. int ret;
  5219. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5220. return 0;
  5221. ret = p2p_in_progress(wpa_s->global->p2p);
  5222. if (ret == 0) {
  5223. /*
  5224. * Check whether there is an ongoing WPS provisioning step (or
  5225. * other parts of group formation) on another interface since
  5226. * p2p_in_progress() does not report this to avoid issues for
  5227. * scans during such provisioning step.
  5228. */
  5229. if (wpa_s->global->p2p_group_formation &&
  5230. wpa_s->global->p2p_group_formation != wpa_s) {
  5231. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
  5232. "in group formation",
  5233. wpa_s->global->p2p_group_formation->ifname);
  5234. ret = 1;
  5235. }
  5236. }
  5237. if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
  5238. struct os_reltime now;
  5239. os_get_reltime(&now);
  5240. if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
  5241. P2P_MAX_INITIAL_CONN_WAIT_GO)) {
  5242. /* Wait for the first client has expired */
  5243. wpa_s->global->p2p_go_wait_client.sec = 0;
  5244. } else {
  5245. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
  5246. ret = 1;
  5247. }
  5248. }
  5249. return ret;
  5250. }
  5251. void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
  5252. struct wpa_ssid *ssid)
  5253. {
  5254. if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
  5255. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  5256. wpa_s->parent, NULL) > 0) {
  5257. /**
  5258. * Remove the network by scheduling the group formation
  5259. * timeout to happen immediately. The teardown code
  5260. * needs to be scheduled to run asynch later so that we
  5261. * don't delete data from under ourselves unexpectedly.
  5262. * Calling wpas_p2p_group_formation_timeout directly
  5263. * causes a series of crashes in WPS failure scenarios.
  5264. */
  5265. wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
  5266. "P2P group network getting removed");
  5267. eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
  5268. wpa_s->parent, NULL);
  5269. }
  5270. }
  5271. struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
  5272. const u8 *addr, const u8 *ssid,
  5273. size_t ssid_len)
  5274. {
  5275. struct wpa_ssid *s;
  5276. size_t i;
  5277. for (s = wpa_s->conf->ssid; s; s = s->next) {
  5278. if (s->disabled != 2)
  5279. continue;
  5280. if (ssid &&
  5281. (ssid_len != s->ssid_len ||
  5282. os_memcmp(ssid, s->ssid, ssid_len) != 0))
  5283. continue;
  5284. if (addr == NULL) {
  5285. if (s->mode == WPAS_MODE_P2P_GO)
  5286. return s;
  5287. continue;
  5288. }
  5289. if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
  5290. return s; /* peer is GO in the persistent group */
  5291. if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
  5292. continue;
  5293. for (i = 0; i < s->num_p2p_clients; i++) {
  5294. if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
  5295. addr, ETH_ALEN) == 0)
  5296. return s; /* peer is P2P client in persistent
  5297. * group */
  5298. }
  5299. }
  5300. return NULL;
  5301. }
  5302. void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
  5303. const u8 *addr)
  5304. {
  5305. if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  5306. wpa_s->parent, NULL) > 0) {
  5307. /*
  5308. * This can happen if WPS provisioning step is not terminated
  5309. * cleanly (e.g., P2P Client does not send WSC_Done). Since the
  5310. * peer was able to connect, there is no need to time out group
  5311. * formation after this, though. In addition, this is used with
  5312. * the initial connection wait on the GO as a separate formation
  5313. * timeout and as such, expected to be hit after the initial WPS
  5314. * provisioning step.
  5315. */
  5316. wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
  5317. }
  5318. if (!wpa_s->p2p_go_group_formation_completed) {
  5319. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
  5320. wpa_s->p2p_go_group_formation_completed = 1;
  5321. wpa_s->global->p2p_group_formation = NULL;
  5322. wpa_s->p2p_in_provisioning = 0;
  5323. }
  5324. wpa_s->global->p2p_go_wait_client.sec = 0;
  5325. if (addr == NULL)
  5326. return;
  5327. wpas_p2p_add_persistent_group_client(wpa_s, addr);
  5328. }
  5329. static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
  5330. int group_added)
  5331. {
  5332. struct wpa_supplicant *group = wpa_s;
  5333. if (wpa_s->global->p2p_group_formation)
  5334. group = wpa_s->global->p2p_group_formation;
  5335. wpa_s = wpa_s->parent;
  5336. offchannel_send_action_done(wpa_s);
  5337. if (group_added)
  5338. wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
  5339. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
  5340. wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
  5341. wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
  5342. 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
  5343. wpa_s->p2p_persistent_id,
  5344. wpa_s->p2p_pd_before_go_neg,
  5345. wpa_s->p2p_go_ht40,
  5346. wpa_s->p2p_go_vht);
  5347. }
  5348. int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
  5349. {
  5350. if (!wpa_s->p2p_fallback_to_go_neg ||
  5351. wpa_s->p2p_in_provisioning <= 5)
  5352. return 0;
  5353. if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
  5354. return 0; /* peer operating as a GO */
  5355. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
  5356. "fallback to GO Negotiation");
  5357. wpas_p2p_fallback_to_go_neg(wpa_s, 1);
  5358. return 1;
  5359. }
  5360. unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
  5361. {
  5362. struct wpa_supplicant *ifs;
  5363. if (wpa_s->wpa_state > WPA_SCANNING) {
  5364. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
  5365. "concurrent operation",
  5366. P2P_CONCURRENT_SEARCH_DELAY);
  5367. return P2P_CONCURRENT_SEARCH_DELAY;
  5368. }
  5369. dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
  5370. radio_list) {
  5371. if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
  5372. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
  5373. "delay due to concurrent operation on "
  5374. "interface %s",
  5375. P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
  5376. return P2P_CONCURRENT_SEARCH_DELAY;
  5377. }
  5378. }
  5379. return 0;
  5380. }
  5381. static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
  5382. struct wpa_ssid *s, const u8 *addr,
  5383. int iface_addr)
  5384. {
  5385. struct psk_list_entry *psk, *tmp;
  5386. int changed = 0;
  5387. dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
  5388. list) {
  5389. if ((iface_addr && !psk->p2p &&
  5390. os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
  5391. (!iface_addr && psk->p2p &&
  5392. os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
  5393. wpa_dbg(wpa_s, MSG_DEBUG,
  5394. "P2P: Remove persistent group PSK list entry for "
  5395. MACSTR " p2p=%u",
  5396. MAC2STR(psk->addr), psk->p2p);
  5397. dl_list_del(&psk->list);
  5398. os_free(psk);
  5399. changed++;
  5400. }
  5401. }
  5402. return changed;
  5403. }
  5404. void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
  5405. const u8 *p2p_dev_addr,
  5406. const u8 *psk, size_t psk_len)
  5407. {
  5408. struct wpa_ssid *ssid = wpa_s->current_ssid;
  5409. struct wpa_ssid *persistent;
  5410. struct psk_list_entry *p;
  5411. if (psk_len != sizeof(p->psk))
  5412. return;
  5413. if (p2p_dev_addr) {
  5414. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
  5415. " p2p_dev_addr=" MACSTR,
  5416. MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
  5417. if (is_zero_ether_addr(p2p_dev_addr))
  5418. p2p_dev_addr = NULL;
  5419. } else {
  5420. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
  5421. MAC2STR(mac_addr));
  5422. }
  5423. if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
  5424. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
  5425. /* To be added to persistent group once created */
  5426. if (wpa_s->global->add_psk == NULL) {
  5427. wpa_s->global->add_psk = os_zalloc(sizeof(*p));
  5428. if (wpa_s->global->add_psk == NULL)
  5429. return;
  5430. }
  5431. p = wpa_s->global->add_psk;
  5432. if (p2p_dev_addr) {
  5433. p->p2p = 1;
  5434. os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
  5435. } else {
  5436. p->p2p = 0;
  5437. os_memcpy(p->addr, mac_addr, ETH_ALEN);
  5438. }
  5439. os_memcpy(p->psk, psk, psk_len);
  5440. return;
  5441. }
  5442. if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
  5443. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
  5444. return;
  5445. }
  5446. persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
  5447. ssid->ssid_len);
  5448. if (!persistent) {
  5449. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
  5450. return;
  5451. }
  5452. p = os_zalloc(sizeof(*p));
  5453. if (p == NULL)
  5454. return;
  5455. if (p2p_dev_addr) {
  5456. p->p2p = 1;
  5457. os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
  5458. } else {
  5459. p->p2p = 0;
  5460. os_memcpy(p->addr, mac_addr, ETH_ALEN);
  5461. }
  5462. os_memcpy(p->psk, psk, psk_len);
  5463. if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS) {
  5464. struct psk_list_entry *last;
  5465. last = dl_list_last(&persistent->psk_list,
  5466. struct psk_list_entry, list);
  5467. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
  5468. MACSTR " (p2p=%u) to make room for a new one",
  5469. MAC2STR(last->addr), last->p2p);
  5470. dl_list_del(&last->list);
  5471. os_free(last);
  5472. }
  5473. wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
  5474. p2p_dev_addr ? p2p_dev_addr : mac_addr,
  5475. p2p_dev_addr == NULL);
  5476. if (p2p_dev_addr) {
  5477. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
  5478. MACSTR, MAC2STR(p2p_dev_addr));
  5479. } else {
  5480. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
  5481. MAC2STR(mac_addr));
  5482. }
  5483. dl_list_add(&persistent->psk_list, &p->list);
  5484. #ifndef CONFIG_NO_CONFIG_WRITE
  5485. if (wpa_s->parent->conf->update_config &&
  5486. wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
  5487. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  5488. #endif /* CONFIG_NO_CONFIG_WRITE */
  5489. }
  5490. static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
  5491. struct wpa_ssid *s, const u8 *addr,
  5492. int iface_addr)
  5493. {
  5494. int res;
  5495. res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
  5496. if (res > 0) {
  5497. #ifndef CONFIG_NO_CONFIG_WRITE
  5498. if (wpa_s->conf->update_config &&
  5499. wpa_config_write(wpa_s->confname, wpa_s->conf))
  5500. wpa_dbg(wpa_s, MSG_DEBUG,
  5501. "P2P: Failed to update configuration");
  5502. #endif /* CONFIG_NO_CONFIG_WRITE */
  5503. }
  5504. }
  5505. static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
  5506. const u8 *peer, int iface_addr)
  5507. {
  5508. struct hostapd_data *hapd;
  5509. struct hostapd_wpa_psk *psk, *prev, *rem;
  5510. struct sta_info *sta;
  5511. if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
  5512. wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
  5513. return;
  5514. /* Remove per-station PSK entry */
  5515. hapd = wpa_s->ap_iface->bss[0];
  5516. prev = NULL;
  5517. psk = hapd->conf->ssid.wpa_psk;
  5518. while (psk) {
  5519. if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
  5520. (!iface_addr &&
  5521. os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
  5522. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
  5523. MACSTR " iface_addr=%d",
  5524. MAC2STR(peer), iface_addr);
  5525. if (prev)
  5526. prev->next = psk->next;
  5527. else
  5528. hapd->conf->ssid.wpa_psk = psk->next;
  5529. rem = psk;
  5530. psk = psk->next;
  5531. os_free(rem);
  5532. } else {
  5533. prev = psk;
  5534. psk = psk->next;
  5535. }
  5536. }
  5537. /* Disconnect from group */
  5538. if (iface_addr)
  5539. sta = ap_get_sta(hapd, peer);
  5540. else
  5541. sta = ap_get_sta_p2p(hapd, peer);
  5542. if (sta) {
  5543. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
  5544. " (iface_addr=%d) from group",
  5545. MAC2STR(peer), iface_addr);
  5546. hostapd_drv_sta_deauth(hapd, sta->addr,
  5547. WLAN_REASON_DEAUTH_LEAVING);
  5548. ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
  5549. }
  5550. }
  5551. void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
  5552. int iface_addr)
  5553. {
  5554. struct wpa_ssid *s;
  5555. struct wpa_supplicant *w;
  5556. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
  5557. /* Remove from any persistent group */
  5558. for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
  5559. if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
  5560. continue;
  5561. if (!iface_addr)
  5562. wpas_remove_persistent_peer(wpa_s, s, peer, 0);
  5563. wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
  5564. }
  5565. /* Remove from any operating group */
  5566. for (w = wpa_s->global->ifaces; w; w = w->next)
  5567. wpas_p2p_remove_client_go(w, peer, iface_addr);
  5568. }
  5569. static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
  5570. {
  5571. struct wpa_supplicant *wpa_s = eloop_ctx;
  5572. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
  5573. }
  5574. static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
  5575. {
  5576. struct wpa_supplicant *wpa_s = eloop_ctx;
  5577. wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
  5578. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
  5579. }
  5580. int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
  5581. struct wpa_ssid *ssid)
  5582. {
  5583. struct wpa_supplicant *iface;
  5584. for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
  5585. if (!iface->current_ssid ||
  5586. iface->current_ssid->frequency == freq ||
  5587. (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
  5588. !iface->current_ssid->p2p_group))
  5589. continue;
  5590. /* Remove the connection with least priority */
  5591. if (!wpas_is_p2p_prioritized(iface)) {
  5592. /* STA connection has priority over existing
  5593. * P2P connection, so remove the interface. */
  5594. wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
  5595. eloop_register_timeout(0, 0,
  5596. wpas_p2p_group_freq_conflict,
  5597. iface, NULL);
  5598. /* If connection in progress is P2P connection, do not
  5599. * proceed for the connection. */
  5600. if (wpa_s == iface)
  5601. return -1;
  5602. else
  5603. return 0;
  5604. } else {
  5605. /* P2P connection has priority, disable the STA network
  5606. */
  5607. wpa_supplicant_disable_network(wpa_s->global->ifaces,
  5608. ssid);
  5609. wpa_msg(wpa_s->global->ifaces, MSG_INFO,
  5610. WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
  5611. os_memset(wpa_s->global->ifaces->pending_bssid, 0,
  5612. ETH_ALEN);
  5613. /* If P2P connection is in progress, continue
  5614. * connecting...*/
  5615. if (wpa_s == iface)
  5616. return 0;
  5617. else
  5618. return -1;
  5619. }
  5620. }
  5621. return 0;
  5622. }
  5623. int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
  5624. {
  5625. struct wpa_ssid *ssid = wpa_s->current_ssid;
  5626. if (ssid == NULL || !ssid->p2p_group)
  5627. return 0;
  5628. if (wpa_s->p2p_last_4way_hs_fail &&
  5629. wpa_s->p2p_last_4way_hs_fail == ssid) {
  5630. u8 go_dev_addr[ETH_ALEN];
  5631. struct wpa_ssid *persistent;
  5632. if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
  5633. ssid->ssid,
  5634. ssid->ssid_len) <= 0) {
  5635. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
  5636. goto disconnect;
  5637. }
  5638. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
  5639. MACSTR, MAC2STR(go_dev_addr));
  5640. persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
  5641. ssid->ssid,
  5642. ssid->ssid_len);
  5643. if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
  5644. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
  5645. goto disconnect;
  5646. }
  5647. wpa_msg_global(wpa_s->parent, MSG_INFO,
  5648. P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
  5649. persistent->id);
  5650. disconnect:
  5651. wpa_s->p2p_last_4way_hs_fail = NULL;
  5652. /*
  5653. * Remove the group from a timeout to avoid issues with caller
  5654. * continuing to use the interface if this is on a P2P group
  5655. * interface.
  5656. */
  5657. eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
  5658. wpa_s, NULL);
  5659. return 1;
  5660. }
  5661. wpa_s->p2p_last_4way_hs_fail = ssid;
  5662. return 0;
  5663. }