p2p_supplicant.c 224 KB

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