p2p_supplicant.c 223 KB

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