p2p_supplicant.c 245 KB

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