p2p_supplicant.c 254 KB

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