p2p_supplicant.c 220 KB

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