p2p_supplicant.c 254 KB

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