p2p_supplicant.c 164 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968
  1. /*
  2. * wpa_supplicant - P2P
  3. * Copyright (c) 2009-2010, Atheros Communications
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "eloop.h"
  11. #include "common/ieee802_11_common.h"
  12. #include "common/ieee802_11_defs.h"
  13. #include "common/wpa_ctrl.h"
  14. #include "wps/wps_i.h"
  15. #include "p2p/p2p.h"
  16. #include "ap/hostapd.h"
  17. #include "ap/ap_config.h"
  18. #include "ap/p2p_hostapd.h"
  19. #include "eapol_supp/eapol_supp_sm.h"
  20. #include "rsn_supp/wpa.h"
  21. #include "wpa_supplicant_i.h"
  22. #include "driver_i.h"
  23. #include "ap.h"
  24. #include "config_ssid.h"
  25. #include "config.h"
  26. #include "notify.h"
  27. #include "scan.h"
  28. #include "bss.h"
  29. #include "offchannel.h"
  30. #include "wps_supplicant.h"
  31. #include "p2p_supplicant.h"
  32. /*
  33. * How many times to try to scan to find the GO before giving up on join
  34. * request.
  35. */
  36. #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
  37. #define P2P_AUTO_PD_SCAN_ATTEMPTS 5
  38. #ifndef P2P_MAX_CLIENT_IDLE
  39. /*
  40. * How many seconds to try to reconnect to the GO when connection in P2P client
  41. * role has been lost.
  42. */
  43. #define P2P_MAX_CLIENT_IDLE 10
  44. #endif /* P2P_MAX_CLIENT_IDLE */
  45. #ifndef P2P_MAX_INITIAL_CONN_WAIT
  46. /*
  47. * How many seconds to wait for initial 4-way handshake to get completed after
  48. * WPS provisioning step.
  49. */
  50. #define P2P_MAX_INITIAL_CONN_WAIT 10
  51. #endif /* P2P_MAX_INITIAL_CONN_WAIT */
  52. #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
  53. /*
  54. * How many seconds to wait for initial 4-way handshake to get completed after
  55. * WPS provisioning step on the GO. This controls the extra time the P2P
  56. * operation is considered to be in progress (e.g., to delay other scans) after
  57. * WPS provisioning has been completed on the GO during group formation.
  58. */
  59. #define P2P_MAX_INITIAL_CONN_WAIT_GO 10
  60. #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
  61. #ifndef P2P_CONCURRENT_SEARCH_DELAY
  62. #define P2P_CONCURRENT_SEARCH_DELAY 500
  63. #endif /* P2P_CONCURRENT_SEARCH_DELAY */
  64. #define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
  65. enum p2p_group_removal_reason {
  66. P2P_GROUP_REMOVAL_UNKNOWN,
  67. P2P_GROUP_REMOVAL_SILENT,
  68. P2P_GROUP_REMOVAL_FORMATION_FAILED,
  69. P2P_GROUP_REMOVAL_REQUESTED,
  70. P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
  71. P2P_GROUP_REMOVAL_UNAVAILABLE,
  72. P2P_GROUP_REMOVAL_GO_ENDING_SESSION
  73. };
  74. static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
  75. static struct wpa_supplicant *
  76. wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
  77. int go);
  78. static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
  79. static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
  80. static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
  81. static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
  82. const u8 *dev_addr, enum p2p_wps_method wps_method,
  83. int auto_join);
  84. static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
  85. static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
  86. static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
  87. static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
  88. static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
  89. void *timeout_ctx);
  90. static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
  91. int group_added);
  92. static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
  93. /*
  94. * Get the number of concurrent channels that the HW can operate, but that are
  95. * currently not in use by any of the wpa_supplicant interfaces.
  96. */
  97. static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
  98. {
  99. int *freqs;
  100. int num;
  101. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  102. if (!freqs)
  103. return -1;
  104. num = get_shared_radio_freqs(wpa_s, freqs,
  105. wpa_s->num_multichan_concurrent);
  106. os_free(freqs);
  107. return wpa_s->num_multichan_concurrent - num;
  108. }
  109. /*
  110. * Get the frequencies that are currently in use by one or more of the virtual
  111. * interfaces, and that are also valid for P2P operation.
  112. */
  113. static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
  114. int *p2p_freqs, unsigned int len)
  115. {
  116. int *freqs;
  117. unsigned int num, i, j;
  118. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  119. if (!freqs)
  120. return -1;
  121. num = get_shared_radio_freqs(wpa_s, freqs,
  122. wpa_s->num_multichan_concurrent);
  123. os_memset(p2p_freqs, 0, sizeof(int) * len);
  124. for (i = 0, j = 0; i < num && j < len; i++) {
  125. if (p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
  126. p2p_freqs[j++] = freqs[i];
  127. }
  128. os_free(freqs);
  129. return j;
  130. }
  131. static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
  132. int freq)
  133. {
  134. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  135. return;
  136. if (freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
  137. wpas_p2p_num_unused_channels(wpa_s) > 0 &&
  138. wpa_s->parent->conf->p2p_ignore_shared_freq)
  139. freq = 0;
  140. p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
  141. }
  142. static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
  143. struct wpa_scan_results *scan_res)
  144. {
  145. size_t i;
  146. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  147. return;
  148. wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
  149. (int) scan_res->num);
  150. for (i = 0; i < scan_res->num; i++) {
  151. struct wpa_scan_res *bss = scan_res->res[i];
  152. struct os_time time_tmp_age, entry_ts;
  153. time_tmp_age.sec = bss->age / 1000;
  154. time_tmp_age.usec = (bss->age % 1000) * 1000;
  155. os_time_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
  156. if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
  157. bss->freq, &entry_ts, bss->level,
  158. (const u8 *) (bss + 1),
  159. bss->ie_len) > 0)
  160. break;
  161. }
  162. p2p_scan_res_handled(wpa_s->global->p2p);
  163. }
  164. static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
  165. unsigned int num_req_dev_types,
  166. const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
  167. {
  168. struct wpa_supplicant *wpa_s = ctx;
  169. struct wpa_supplicant *ifs;
  170. struct wpa_driver_scan_params params;
  171. int ret;
  172. struct wpabuf *wps_ie, *ies;
  173. int social_channels[] = { 2412, 2437, 2462, 0, 0 };
  174. size_t ielen;
  175. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  176. return -1;
  177. for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
  178. if (ifs->sta_scan_pending &&
  179. (wpas_scan_scheduled(ifs) || ifs->scanning) &&
  180. wpas_p2p_in_progress(wpa_s) == 2) {
  181. wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
  182. "pending station mode scan to be "
  183. "completed on interface %s", ifs->ifname);
  184. wpa_s->global->p2p_cb_on_scan_complete = 1;
  185. wpa_supplicant_req_scan(ifs, 0, 0);
  186. return 1;
  187. }
  188. }
  189. os_memset(&params, 0, sizeof(params));
  190. /* P2P Wildcard SSID */
  191. params.num_ssids = 1;
  192. params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
  193. params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
  194. wpa_s->wps->dev.p2p = 1;
  195. wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
  196. wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
  197. num_req_dev_types, req_dev_types);
  198. if (wps_ie == NULL)
  199. return -1;
  200. ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
  201. ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
  202. if (ies == NULL) {
  203. wpabuf_free(wps_ie);
  204. return -1;
  205. }
  206. wpabuf_put_buf(ies, wps_ie);
  207. wpabuf_free(wps_ie);
  208. p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
  209. params.p2p_probe = 1;
  210. params.extra_ies = wpabuf_head(ies);
  211. params.extra_ies_len = wpabuf_len(ies);
  212. switch (type) {
  213. case P2P_SCAN_SOCIAL:
  214. params.freqs = social_channels;
  215. break;
  216. case P2P_SCAN_FULL:
  217. break;
  218. case P2P_SCAN_SOCIAL_PLUS_ONE:
  219. social_channels[3] = freq;
  220. params.freqs = social_channels;
  221. break;
  222. }
  223. ret = wpa_drv_scan(wpa_s, &params);
  224. wpabuf_free(ies);
  225. if (ret) {
  226. for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
  227. if (ifs->scanning ||
  228. ifs->scan_res_handler == wpas_p2p_scan_res_handler) {
  229. wpa_s->global->p2p_cb_on_scan_complete = 1;
  230. ret = 1;
  231. break;
  232. }
  233. }
  234. } else {
  235. os_get_time(&wpa_s->scan_trigger_time);
  236. wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
  237. }
  238. return ret;
  239. }
  240. static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
  241. {
  242. switch (p2p_group_interface) {
  243. case P2P_GROUP_INTERFACE_PENDING:
  244. return WPA_IF_P2P_GROUP;
  245. case P2P_GROUP_INTERFACE_GO:
  246. return WPA_IF_P2P_GO;
  247. case P2P_GROUP_INTERFACE_CLIENT:
  248. return WPA_IF_P2P_CLIENT;
  249. }
  250. return WPA_IF_P2P_GROUP;
  251. }
  252. static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
  253. const u8 *ssid,
  254. size_t ssid_len, int *go)
  255. {
  256. struct wpa_ssid *s;
  257. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  258. for (s = wpa_s->conf->ssid; s; s = s->next) {
  259. if (s->disabled != 0 || !s->p2p_group ||
  260. s->ssid_len != ssid_len ||
  261. os_memcmp(ssid, s->ssid, ssid_len) != 0)
  262. continue;
  263. if (s->mode == WPAS_MODE_P2P_GO &&
  264. s != wpa_s->current_ssid)
  265. continue;
  266. if (go)
  267. *go = s->mode == WPAS_MODE_P2P_GO;
  268. return wpa_s;
  269. }
  270. }
  271. return NULL;
  272. }
  273. static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
  274. enum p2p_group_removal_reason removal_reason)
  275. {
  276. struct wpa_ssid *ssid;
  277. char *gtype;
  278. const char *reason;
  279. ssid = wpa_s->current_ssid;
  280. if (ssid == NULL) {
  281. /*
  282. * The current SSID was not known, but there may still be a
  283. * pending P2P group interface waiting for provisioning or a
  284. * P2P group that is trying to reconnect.
  285. */
  286. ssid = wpa_s->conf->ssid;
  287. while (ssid) {
  288. if (ssid->p2p_group && ssid->disabled != 2)
  289. break;
  290. ssid = ssid->next;
  291. }
  292. if (ssid == NULL &&
  293. wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
  294. {
  295. wpa_printf(MSG_ERROR, "P2P: P2P group interface "
  296. "not found");
  297. return -1;
  298. }
  299. }
  300. if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
  301. gtype = "GO";
  302. else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
  303. (ssid && ssid->mode == WPAS_MODE_INFRA)) {
  304. wpa_s->reassociate = 0;
  305. wpa_s->disconnected = 1;
  306. wpa_supplicant_deauthenticate(wpa_s,
  307. WLAN_REASON_DEAUTH_LEAVING);
  308. gtype = "client";
  309. } else
  310. gtype = "GO";
  311. if (wpa_s->cross_connect_in_use) {
  312. wpa_s->cross_connect_in_use = 0;
  313. wpa_msg_global(wpa_s->parent, MSG_INFO,
  314. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  315. wpa_s->ifname, wpa_s->cross_connect_uplink);
  316. }
  317. switch (removal_reason) {
  318. case P2P_GROUP_REMOVAL_REQUESTED:
  319. reason = " reason=REQUESTED";
  320. break;
  321. case P2P_GROUP_REMOVAL_FORMATION_FAILED:
  322. reason = " reason=FORMATION_FAILED";
  323. break;
  324. case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
  325. reason = " reason=IDLE";
  326. break;
  327. case P2P_GROUP_REMOVAL_UNAVAILABLE:
  328. reason = " reason=UNAVAILABLE";
  329. break;
  330. case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
  331. reason = " reason=GO_ENDING_SESSION";
  332. break;
  333. default:
  334. reason = "";
  335. break;
  336. }
  337. if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
  338. wpa_msg_global(wpa_s->parent, MSG_INFO,
  339. P2P_EVENT_GROUP_REMOVED "%s %s%s",
  340. wpa_s->ifname, gtype, reason);
  341. }
  342. if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
  343. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
  344. if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  345. wpa_s->parent, NULL) > 0) {
  346. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
  347. "timeout");
  348. wpa_s->p2p_in_provisioning = 0;
  349. }
  350. if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
  351. wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
  352. if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
  353. struct wpa_global *global;
  354. char *ifname;
  355. enum wpa_driver_if_type type;
  356. wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
  357. wpa_s->ifname);
  358. global = wpa_s->global;
  359. ifname = os_strdup(wpa_s->ifname);
  360. type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
  361. wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
  362. wpa_s = global->ifaces;
  363. if (wpa_s && ifname)
  364. wpa_drv_if_remove(wpa_s, type, ifname);
  365. os_free(ifname);
  366. return 1;
  367. }
  368. wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
  369. if (ssid && (ssid->p2p_group ||
  370. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
  371. (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
  372. int id = ssid->id;
  373. if (ssid == wpa_s->current_ssid) {
  374. wpa_sm_set_config(wpa_s->wpa, NULL);
  375. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  376. wpa_s->current_ssid = NULL;
  377. }
  378. /*
  379. * Networks objects created during any P2P activities are not
  380. * exposed out as they might/will confuse certain non-P2P aware
  381. * applications since these network objects won't behave like
  382. * regular ones.
  383. *
  384. * Likewise, we don't send out network removed signals for such
  385. * network objects.
  386. */
  387. wpa_config_remove_network(wpa_s->conf, id);
  388. wpa_supplicant_clear_status(wpa_s);
  389. wpa_supplicant_cancel_sched_scan(wpa_s);
  390. wpa_s->sta_scan_pending = 0;
  391. } else {
  392. wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
  393. "found");
  394. }
  395. if (wpa_s->ap_iface)
  396. wpa_supplicant_ap_deinit(wpa_s);
  397. else
  398. wpa_drv_deinit_p2p_cli(wpa_s);
  399. return 0;
  400. }
  401. static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
  402. u8 *go_dev_addr,
  403. const u8 *ssid, size_t ssid_len)
  404. {
  405. struct wpa_bss *bss;
  406. const u8 *bssid;
  407. struct wpabuf *p2p;
  408. u8 group_capab;
  409. const u8 *addr;
  410. if (wpa_s->go_params)
  411. bssid = wpa_s->go_params->peer_interface_addr;
  412. else
  413. bssid = wpa_s->bssid;
  414. bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
  415. if (bss == NULL) {
  416. u8 iface_addr[ETH_ALEN];
  417. if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
  418. iface_addr) == 0)
  419. bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
  420. }
  421. if (bss == NULL) {
  422. wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
  423. "group is persistent - BSS " MACSTR " not found",
  424. MAC2STR(bssid));
  425. return 0;
  426. }
  427. p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  428. if (p2p == NULL) {
  429. wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
  430. "group is persistent - BSS " MACSTR
  431. " did not include P2P IE", MAC2STR(bssid));
  432. wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
  433. (u8 *) (bss + 1), bss->ie_len);
  434. wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
  435. ((u8 *) bss + 1) + bss->ie_len,
  436. bss->beacon_ie_len);
  437. return 0;
  438. }
  439. group_capab = p2p_get_group_capab(p2p);
  440. addr = p2p_get_go_dev_addr(p2p);
  441. wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
  442. "group_capab=0x%x", group_capab);
  443. if (addr) {
  444. os_memcpy(go_dev_addr, addr, ETH_ALEN);
  445. wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
  446. MAC2STR(addr));
  447. } else
  448. os_memset(go_dev_addr, 0, ETH_ALEN);
  449. wpabuf_free(p2p);
  450. wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
  451. "go_dev_addr=" MACSTR,
  452. MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
  453. return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
  454. }
  455. static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
  456. struct wpa_ssid *ssid,
  457. const u8 *go_dev_addr)
  458. {
  459. struct wpa_ssid *s;
  460. int changed = 0;
  461. wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
  462. "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
  463. for (s = wpa_s->conf->ssid; s; s = s->next) {
  464. if (s->disabled == 2 &&
  465. os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
  466. s->ssid_len == ssid->ssid_len &&
  467. os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
  468. break;
  469. }
  470. if (s) {
  471. wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
  472. "entry");
  473. if (ssid->passphrase && !s->passphrase)
  474. changed = 1;
  475. else if (ssid->passphrase && s->passphrase &&
  476. os_strcmp(ssid->passphrase, s->passphrase) != 0)
  477. changed = 1;
  478. } else {
  479. wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
  480. "entry");
  481. changed = 1;
  482. s = wpa_config_add_network(wpa_s->conf);
  483. if (s == NULL)
  484. return -1;
  485. /*
  486. * Instead of network_added we emit persistent_group_added
  487. * notification. Also to keep the defense checks in
  488. * persistent_group obj registration method, we set the
  489. * relevant flags in s to designate it as a persistent group.
  490. */
  491. s->p2p_group = 1;
  492. s->p2p_persistent_group = 1;
  493. wpas_notify_persistent_group_added(wpa_s, s);
  494. wpa_config_set_network_defaults(s);
  495. }
  496. s->p2p_group = 1;
  497. s->p2p_persistent_group = 1;
  498. s->disabled = 2;
  499. s->bssid_set = 1;
  500. os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
  501. s->mode = ssid->mode;
  502. s->auth_alg = WPA_AUTH_ALG_OPEN;
  503. s->key_mgmt = WPA_KEY_MGMT_PSK;
  504. s->proto = WPA_PROTO_RSN;
  505. s->pairwise_cipher = WPA_CIPHER_CCMP;
  506. s->export_keys = 1;
  507. if (ssid->passphrase) {
  508. os_free(s->passphrase);
  509. s->passphrase = os_strdup(ssid->passphrase);
  510. }
  511. if (ssid->psk_set) {
  512. s->psk_set = 1;
  513. os_memcpy(s->psk, ssid->psk, 32);
  514. }
  515. if (s->passphrase && !s->psk_set)
  516. wpa_config_update_psk(s);
  517. if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
  518. os_free(s->ssid);
  519. s->ssid = os_malloc(ssid->ssid_len);
  520. }
  521. if (s->ssid) {
  522. s->ssid_len = ssid->ssid_len;
  523. os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
  524. }
  525. #ifndef CONFIG_NO_CONFIG_WRITE
  526. if (changed && wpa_s->conf->update_config &&
  527. wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  528. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  529. }
  530. #endif /* CONFIG_NO_CONFIG_WRITE */
  531. return s->id;
  532. }
  533. static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
  534. const u8 *addr)
  535. {
  536. struct wpa_ssid *ssid, *s;
  537. u8 *n;
  538. size_t i;
  539. int found = 0;
  540. ssid = wpa_s->current_ssid;
  541. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  542. !ssid->p2p_persistent_group)
  543. return;
  544. for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
  545. if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
  546. continue;
  547. if (s->ssid_len == ssid->ssid_len &&
  548. os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
  549. break;
  550. }
  551. if (s == NULL)
  552. return;
  553. for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
  554. if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
  555. ETH_ALEN) != 0)
  556. continue;
  557. if (i == s->num_p2p_clients - 1)
  558. return; /* already the most recent entry */
  559. /* move the entry to mark it most recent */
  560. os_memmove(s->p2p_client_list + i * ETH_ALEN,
  561. s->p2p_client_list + (i + 1) * ETH_ALEN,
  562. (s->num_p2p_clients - i - 1) * ETH_ALEN);
  563. os_memcpy(s->p2p_client_list +
  564. (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
  565. found = 1;
  566. break;
  567. }
  568. if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
  569. n = os_realloc_array(s->p2p_client_list,
  570. s->num_p2p_clients + 1, ETH_ALEN);
  571. if (n == NULL)
  572. return;
  573. os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
  574. s->p2p_client_list = n;
  575. s->num_p2p_clients++;
  576. } else if (!found) {
  577. /* Not enough room for an additional entry - drop the oldest
  578. * entry */
  579. os_memmove(s->p2p_client_list,
  580. s->p2p_client_list + ETH_ALEN,
  581. (s->num_p2p_clients - 1) * ETH_ALEN);
  582. os_memcpy(s->p2p_client_list +
  583. (s->num_p2p_clients - 1) * ETH_ALEN,
  584. addr, ETH_ALEN);
  585. }
  586. #ifndef CONFIG_NO_CONFIG_WRITE
  587. if (wpa_s->parent->conf->update_config &&
  588. wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
  589. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  590. #endif /* CONFIG_NO_CONFIG_WRITE */
  591. }
  592. static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
  593. int success)
  594. {
  595. struct wpa_ssid *ssid;
  596. const char *ssid_txt;
  597. int client;
  598. int persistent;
  599. u8 go_dev_addr[ETH_ALEN];
  600. int network_id = -1;
  601. /*
  602. * This callback is likely called for the main interface. Update wpa_s
  603. * to use the group interface if a new interface was created for the
  604. * group.
  605. */
  606. if (wpa_s->global->p2p_group_formation)
  607. wpa_s = wpa_s->global->p2p_group_formation;
  608. wpa_s->global->p2p_group_formation = NULL;
  609. wpa_s->p2p_in_provisioning = 0;
  610. if (!success) {
  611. wpa_msg_global(wpa_s->parent, MSG_INFO,
  612. P2P_EVENT_GROUP_FORMATION_FAILURE);
  613. wpas_p2p_group_delete(wpa_s,
  614. P2P_GROUP_REMOVAL_FORMATION_FAILED);
  615. return;
  616. }
  617. wpa_msg_global(wpa_s->parent, MSG_INFO,
  618. P2P_EVENT_GROUP_FORMATION_SUCCESS);
  619. ssid = wpa_s->current_ssid;
  620. if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
  621. ssid->mode = WPAS_MODE_P2P_GO;
  622. p2p_group_notif_formation_done(wpa_s->p2p_group);
  623. wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
  624. }
  625. persistent = 0;
  626. if (ssid) {
  627. ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
  628. client = ssid->mode == WPAS_MODE_INFRA;
  629. if (ssid->mode == WPAS_MODE_P2P_GO) {
  630. persistent = ssid->p2p_persistent_group;
  631. os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
  632. ETH_ALEN);
  633. } else
  634. persistent = wpas_p2p_persistent_group(wpa_s,
  635. go_dev_addr,
  636. ssid->ssid,
  637. ssid->ssid_len);
  638. } else {
  639. ssid_txt = "";
  640. client = wpa_s->p2p_group_interface ==
  641. P2P_GROUP_INTERFACE_CLIENT;
  642. os_memset(go_dev_addr, 0, ETH_ALEN);
  643. }
  644. wpa_s->show_group_started = 0;
  645. if (client) {
  646. /*
  647. * Indicate event only after successfully completed 4-way
  648. * handshake, i.e., when the interface is ready for data
  649. * packets.
  650. */
  651. wpa_s->show_group_started = 1;
  652. } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
  653. char psk[65];
  654. wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
  655. wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  656. "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
  657. MACSTR "%s",
  658. wpa_s->ifname, ssid_txt, ssid->frequency, psk,
  659. MAC2STR(go_dev_addr),
  660. persistent ? " [PERSISTENT]" : "");
  661. wpas_p2p_cross_connect_setup(wpa_s);
  662. wpas_p2p_set_group_idle_timeout(wpa_s);
  663. } else {
  664. wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  665. "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
  666. "go_dev_addr=" MACSTR "%s",
  667. wpa_s->ifname, ssid_txt,
  668. ssid ? ssid->frequency : 0,
  669. ssid && ssid->passphrase ? ssid->passphrase : "",
  670. MAC2STR(go_dev_addr),
  671. persistent ? " [PERSISTENT]" : "");
  672. wpas_p2p_cross_connect_setup(wpa_s);
  673. wpas_p2p_set_group_idle_timeout(wpa_s);
  674. }
  675. if (persistent)
  676. network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
  677. ssid, go_dev_addr);
  678. if (network_id < 0 && ssid)
  679. network_id = ssid->id;
  680. if (!client) {
  681. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
  682. os_get_time(&wpa_s->global->p2p_go_wait_client);
  683. }
  684. }
  685. static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
  686. unsigned int freq,
  687. const u8 *dst, const u8 *src,
  688. const u8 *bssid,
  689. const u8 *data, size_t data_len,
  690. enum offchannel_send_action_result
  691. result)
  692. {
  693. enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
  694. if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
  695. return;
  696. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  697. return;
  698. switch (result) {
  699. case OFFCHANNEL_SEND_ACTION_SUCCESS:
  700. res = P2P_SEND_ACTION_SUCCESS;
  701. break;
  702. case OFFCHANNEL_SEND_ACTION_NO_ACK:
  703. res = P2P_SEND_ACTION_NO_ACK;
  704. break;
  705. case OFFCHANNEL_SEND_ACTION_FAILED:
  706. res = P2P_SEND_ACTION_FAILED;
  707. break;
  708. }
  709. p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
  710. if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
  711. wpa_s->pending_pd_before_join &&
  712. (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
  713. os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
  714. wpa_s->p2p_fallback_to_go_neg) {
  715. wpa_s->pending_pd_before_join = 0;
  716. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
  717. "during p2p_connect-auto");
  718. wpas_p2p_fallback_to_go_neg(wpa_s, 0);
  719. return;
  720. }
  721. }
  722. static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
  723. const u8 *src, const u8 *bssid, const u8 *buf,
  724. size_t len, unsigned int wait_time)
  725. {
  726. struct wpa_supplicant *wpa_s = ctx;
  727. return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
  728. wait_time,
  729. wpas_p2p_send_action_tx_status, 1);
  730. }
  731. static void wpas_send_action_done(void *ctx)
  732. {
  733. struct wpa_supplicant *wpa_s = ctx;
  734. offchannel_send_action_done(wpa_s);
  735. }
  736. static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
  737. struct p2p_go_neg_results *params)
  738. {
  739. if (wpa_s->go_params == NULL) {
  740. wpa_s->go_params = os_malloc(sizeof(*params));
  741. if (wpa_s->go_params == NULL)
  742. return -1;
  743. }
  744. os_memcpy(wpa_s->go_params, params, sizeof(*params));
  745. return 0;
  746. }
  747. static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
  748. struct p2p_go_neg_results *res)
  749. {
  750. wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
  751. MAC2STR(res->peer_interface_addr));
  752. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
  753. res->ssid, res->ssid_len);
  754. wpa_supplicant_ap_deinit(wpa_s);
  755. wpas_copy_go_neg_results(wpa_s, res);
  756. if (res->wps_method == WPS_PBC)
  757. wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
  758. else {
  759. u16 dev_pw_id = DEV_PW_DEFAULT;
  760. if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
  761. dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
  762. wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
  763. wpa_s->p2p_pin, 1, dev_pw_id);
  764. }
  765. }
  766. static void p2p_go_configured(void *ctx, void *data)
  767. {
  768. struct wpa_supplicant *wpa_s = ctx;
  769. struct p2p_go_neg_results *params = data;
  770. struct wpa_ssid *ssid;
  771. int network_id = -1;
  772. ssid = wpa_s->current_ssid;
  773. if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
  774. wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
  775. if (wpa_s->global->p2p_group_formation == wpa_s)
  776. wpa_s->global->p2p_group_formation = NULL;
  777. if (os_strlen(params->passphrase) > 0) {
  778. wpa_msg_global(wpa_s->parent, MSG_INFO,
  779. P2P_EVENT_GROUP_STARTED
  780. "%s GO ssid=\"%s\" freq=%d "
  781. "passphrase=\"%s\" go_dev_addr=" MACSTR
  782. "%s", wpa_s->ifname,
  783. wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
  784. ssid->frequency, params->passphrase,
  785. MAC2STR(wpa_s->global->p2p_dev_addr),
  786. params->persistent_group ?
  787. " [PERSISTENT]" : "");
  788. } else {
  789. char psk[65];
  790. wpa_snprintf_hex(psk, sizeof(psk), params->psk,
  791. sizeof(params->psk));
  792. wpa_msg_global(wpa_s->parent, MSG_INFO,
  793. P2P_EVENT_GROUP_STARTED
  794. "%s GO ssid=\"%s\" freq=%d psk=%s "
  795. "go_dev_addr=" MACSTR "%s",
  796. wpa_s->ifname,
  797. wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
  798. ssid->frequency, psk,
  799. MAC2STR(wpa_s->global->p2p_dev_addr),
  800. params->persistent_group ?
  801. " [PERSISTENT]" : "");
  802. }
  803. if (params->persistent_group)
  804. network_id = wpas_p2p_store_persistent_group(
  805. wpa_s->parent, ssid,
  806. wpa_s->global->p2p_dev_addr);
  807. if (network_id < 0)
  808. network_id = ssid->id;
  809. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
  810. wpas_p2p_cross_connect_setup(wpa_s);
  811. wpas_p2p_set_group_idle_timeout(wpa_s);
  812. return;
  813. }
  814. wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
  815. if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
  816. params->peer_interface_addr)) {
  817. wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
  818. "filtering");
  819. return;
  820. }
  821. if (params->wps_method == WPS_PBC)
  822. wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
  823. params->peer_device_addr);
  824. else if (wpa_s->p2p_pin[0])
  825. wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
  826. wpa_s->p2p_pin, NULL, 0, 0);
  827. os_free(wpa_s->go_params);
  828. wpa_s->go_params = NULL;
  829. }
  830. static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
  831. struct p2p_go_neg_results *params,
  832. int group_formation)
  833. {
  834. struct wpa_ssid *ssid;
  835. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
  836. if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
  837. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
  838. "results");
  839. return;
  840. }
  841. ssid = wpa_config_add_network(wpa_s->conf);
  842. if (ssid == NULL) {
  843. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
  844. return;
  845. }
  846. wpa_s->show_group_started = 0;
  847. wpa_config_set_network_defaults(ssid);
  848. ssid->temporary = 1;
  849. ssid->p2p_group = 1;
  850. ssid->p2p_persistent_group = params->persistent_group;
  851. ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
  852. WPAS_MODE_P2P_GO;
  853. ssid->frequency = params->freq;
  854. ssid->ht40 = params->ht40;
  855. ssid->ssid = os_zalloc(params->ssid_len + 1);
  856. if (ssid->ssid) {
  857. os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
  858. ssid->ssid_len = params->ssid_len;
  859. }
  860. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  861. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  862. ssid->proto = WPA_PROTO_RSN;
  863. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  864. if (os_strlen(params->passphrase) > 0) {
  865. ssid->passphrase = os_strdup(params->passphrase);
  866. if (ssid->passphrase == NULL) {
  867. wpa_msg_global(wpa_s, MSG_ERROR,
  868. "P2P: Failed to copy passphrase for GO");
  869. wpa_config_remove_network(wpa_s->conf, ssid->id);
  870. return;
  871. }
  872. } else
  873. ssid->passphrase = NULL;
  874. ssid->psk_set = params->psk_set;
  875. if (ssid->psk_set)
  876. os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
  877. else if (ssid->passphrase)
  878. wpa_config_update_psk(ssid);
  879. ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
  880. wpa_s->ap_configured_cb = p2p_go_configured;
  881. wpa_s->ap_configured_cb_ctx = wpa_s;
  882. wpa_s->ap_configured_cb_data = wpa_s->go_params;
  883. wpa_s->connect_without_scan = ssid;
  884. wpa_s->reassociate = 1;
  885. wpa_s->disconnected = 0;
  886. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
  887. "start GO)");
  888. wpa_supplicant_req_scan(wpa_s, 0, 0);
  889. }
  890. static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
  891. const struct wpa_supplicant *src)
  892. {
  893. struct wpa_config *d;
  894. const struct wpa_config *s;
  895. d = dst->conf;
  896. s = src->conf;
  897. #define C(n) if (s->n) d->n = os_strdup(s->n)
  898. C(device_name);
  899. C(manufacturer);
  900. C(model_name);
  901. C(model_number);
  902. C(serial_number);
  903. C(config_methods);
  904. #undef C
  905. os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
  906. os_memcpy(d->sec_device_type, s->sec_device_type,
  907. sizeof(d->sec_device_type));
  908. d->num_sec_device_types = s->num_sec_device_types;
  909. d->p2p_group_idle = s->p2p_group_idle;
  910. d->p2p_intra_bss = s->p2p_intra_bss;
  911. d->persistent_reconnect = s->persistent_reconnect;
  912. d->max_num_sta = s->max_num_sta;
  913. d->pbc_in_m1 = s->pbc_in_m1;
  914. d->ignore_old_scan_res = s->ignore_old_scan_res;
  915. d->beacon_int = s->beacon_int;
  916. d->disassoc_low_ack = s->disassoc_low_ack;
  917. }
  918. static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
  919. char *ifname, size_t len)
  920. {
  921. char *ifname_ptr = wpa_s->ifname;
  922. if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
  923. os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
  924. ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
  925. }
  926. os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
  927. if (os_strlen(ifname) >= IFNAMSIZ &&
  928. os_strlen(wpa_s->ifname) < IFNAMSIZ) {
  929. /* Try to avoid going over the IFNAMSIZ length limit */
  930. os_snprintf(ifname, sizeof(ifname), "p2p-%d",
  931. wpa_s->p2p_group_idx);
  932. }
  933. }
  934. static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
  935. enum wpa_driver_if_type type)
  936. {
  937. char ifname[120], force_ifname[120];
  938. if (wpa_s->pending_interface_name[0]) {
  939. wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
  940. "- skip creation of a new one");
  941. if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
  942. wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
  943. "unknown?! ifname='%s'",
  944. wpa_s->pending_interface_name);
  945. return -1;
  946. }
  947. return 0;
  948. }
  949. wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
  950. force_ifname[0] = '\0';
  951. wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
  952. ifname);
  953. wpa_s->p2p_group_idx++;
  954. wpa_s->pending_interface_type = type;
  955. if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
  956. wpa_s->pending_interface_addr, NULL) < 0) {
  957. wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
  958. "interface");
  959. return -1;
  960. }
  961. if (force_ifname[0]) {
  962. wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
  963. force_ifname);
  964. os_strlcpy(wpa_s->pending_interface_name, force_ifname,
  965. sizeof(wpa_s->pending_interface_name));
  966. } else
  967. os_strlcpy(wpa_s->pending_interface_name, ifname,
  968. sizeof(wpa_s->pending_interface_name));
  969. wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
  970. MACSTR, wpa_s->pending_interface_name,
  971. MAC2STR(wpa_s->pending_interface_addr));
  972. return 0;
  973. }
  974. static void wpas_p2p_remove_pending_group_interface(
  975. struct wpa_supplicant *wpa_s)
  976. {
  977. if (!wpa_s->pending_interface_name[0] ||
  978. is_zero_ether_addr(wpa_s->pending_interface_addr))
  979. return; /* No pending virtual interface */
  980. wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
  981. wpa_s->pending_interface_name);
  982. wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
  983. wpa_s->pending_interface_name);
  984. os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
  985. wpa_s->pending_interface_name[0] = '\0';
  986. }
  987. static struct wpa_supplicant *
  988. wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
  989. {
  990. struct wpa_interface iface;
  991. struct wpa_supplicant *group_wpa_s;
  992. if (!wpa_s->pending_interface_name[0]) {
  993. wpa_printf(MSG_ERROR, "P2P: No pending group interface");
  994. if (!wpas_p2p_create_iface(wpa_s))
  995. return NULL;
  996. /*
  997. * Something has forced us to remove the pending interface; try
  998. * to create a new one and hope for the best that we will get
  999. * the same local address.
  1000. */
  1001. if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
  1002. WPA_IF_P2P_CLIENT) < 0)
  1003. return NULL;
  1004. }
  1005. os_memset(&iface, 0, sizeof(iface));
  1006. iface.ifname = wpa_s->pending_interface_name;
  1007. iface.driver = wpa_s->driver->name;
  1008. if (wpa_s->conf->ctrl_interface == NULL &&
  1009. wpa_s->parent != wpa_s &&
  1010. wpa_s->p2p_mgmt &&
  1011. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
  1012. iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
  1013. else
  1014. iface.ctrl_interface = wpa_s->conf->ctrl_interface;
  1015. iface.driver_param = wpa_s->conf->driver_param;
  1016. group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
  1017. if (group_wpa_s == NULL) {
  1018. wpa_printf(MSG_ERROR, "P2P: Failed to create new "
  1019. "wpa_supplicant interface");
  1020. return NULL;
  1021. }
  1022. wpa_s->pending_interface_name[0] = '\0';
  1023. group_wpa_s->parent = wpa_s;
  1024. group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
  1025. P2P_GROUP_INTERFACE_CLIENT;
  1026. wpa_s->global->p2p_group_formation = group_wpa_s;
  1027. wpas_p2p_clone_config(group_wpa_s, wpa_s);
  1028. return group_wpa_s;
  1029. }
  1030. static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
  1031. void *timeout_ctx)
  1032. {
  1033. struct wpa_supplicant *wpa_s = eloop_ctx;
  1034. wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
  1035. wpas_p2p_group_formation_failed(wpa_s);
  1036. }
  1037. void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
  1038. {
  1039. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  1040. wpa_s->parent, NULL);
  1041. if (wpa_s->global->p2p)
  1042. p2p_group_formation_failed(wpa_s->global->p2p);
  1043. else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  1044. wpa_drv_p2p_group_formation_failed(wpa_s);
  1045. wpas_group_formation_completed(wpa_s, 0);
  1046. }
  1047. void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
  1048. {
  1049. struct wpa_supplicant *wpa_s = ctx;
  1050. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  1051. wpa_drv_cancel_remain_on_channel(wpa_s);
  1052. wpa_s->off_channel_freq = 0;
  1053. wpa_s->roc_waiting_drv_freq = 0;
  1054. }
  1055. if (res->status) {
  1056. wpa_msg_global(wpa_s, MSG_INFO,
  1057. P2P_EVENT_GO_NEG_FAILURE "status=%d",
  1058. res->status);
  1059. wpas_notify_p2p_go_neg_completed(wpa_s, res);
  1060. wpas_p2p_remove_pending_group_interface(wpa_s);
  1061. return;
  1062. }
  1063. if (wpa_s->p2p_go_ht40)
  1064. res->ht40 = 1;
  1065. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
  1066. "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
  1067. " wps_method=%s",
  1068. res->role_go ? "GO" : "client", res->freq, res->ht40,
  1069. MAC2STR(res->peer_device_addr),
  1070. MAC2STR(res->peer_interface_addr),
  1071. p2p_wps_method_text(res->wps_method));
  1072. wpas_notify_p2p_go_neg_completed(wpa_s, res);
  1073. if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
  1074. struct wpa_ssid *ssid;
  1075. ssid = wpa_config_get_network(wpa_s->conf,
  1076. wpa_s->p2p_persistent_id);
  1077. if (ssid && ssid->disabled == 2 &&
  1078. ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
  1079. size_t len = os_strlen(ssid->passphrase);
  1080. wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
  1081. "on requested persistent group");
  1082. os_memcpy(res->passphrase, ssid->passphrase, len);
  1083. res->passphrase[len] = '\0';
  1084. }
  1085. }
  1086. if (wpa_s->create_p2p_iface) {
  1087. struct wpa_supplicant *group_wpa_s =
  1088. wpas_p2p_init_group_interface(wpa_s, res->role_go);
  1089. if (group_wpa_s == NULL) {
  1090. wpas_p2p_remove_pending_group_interface(wpa_s);
  1091. return;
  1092. }
  1093. if (group_wpa_s != wpa_s) {
  1094. os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
  1095. sizeof(group_wpa_s->p2p_pin));
  1096. group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
  1097. }
  1098. os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
  1099. wpa_s->pending_interface_name[0] = '\0';
  1100. group_wpa_s->p2p_in_provisioning = 1;
  1101. if (res->role_go)
  1102. wpas_start_wps_go(group_wpa_s, res, 1);
  1103. else
  1104. wpas_start_wps_enrollee(group_wpa_s, res);
  1105. } else {
  1106. wpa_s->p2p_in_provisioning = 1;
  1107. wpa_s->global->p2p_group_formation = wpa_s;
  1108. if (res->role_go)
  1109. wpas_start_wps_go(wpa_s, res, 1);
  1110. else
  1111. wpas_start_wps_enrollee(ctx, res);
  1112. }
  1113. wpa_s->p2p_long_listen = 0;
  1114. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  1115. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  1116. eloop_register_timeout(15 + res->peer_config_timeout / 100,
  1117. (res->peer_config_timeout % 100) * 10000,
  1118. wpas_p2p_group_formation_timeout, wpa_s, NULL);
  1119. }
  1120. void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
  1121. {
  1122. struct wpa_supplicant *wpa_s = ctx;
  1123. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
  1124. " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
  1125. wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
  1126. }
  1127. void wpas_dev_found(void *ctx, const u8 *addr,
  1128. const struct p2p_peer_info *info,
  1129. int new_device)
  1130. {
  1131. #ifndef CONFIG_NO_STDOUT_DEBUG
  1132. struct wpa_supplicant *wpa_s = ctx;
  1133. char devtype[WPS_DEV_TYPE_BUFSIZE];
  1134. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
  1135. " p2p_dev_addr=" MACSTR
  1136. " pri_dev_type=%s name='%s' config_methods=0x%x "
  1137. "dev_capab=0x%x group_capab=0x%x",
  1138. MAC2STR(addr), MAC2STR(info->p2p_device_addr),
  1139. wps_dev_type_bin2str(info->pri_dev_type, devtype,
  1140. sizeof(devtype)),
  1141. info->device_name, info->config_methods,
  1142. info->dev_capab, info->group_capab);
  1143. #endif /* CONFIG_NO_STDOUT_DEBUG */
  1144. wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
  1145. }
  1146. static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
  1147. {
  1148. struct wpa_supplicant *wpa_s = ctx;
  1149. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
  1150. "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
  1151. wpas_notify_p2p_device_lost(wpa_s, dev_addr);
  1152. }
  1153. static void wpas_find_stopped(void *ctx)
  1154. {
  1155. struct wpa_supplicant *wpa_s = ctx;
  1156. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
  1157. }
  1158. static int wpas_start_listen(void *ctx, unsigned int freq,
  1159. unsigned int duration,
  1160. const struct wpabuf *probe_resp_ie)
  1161. {
  1162. struct wpa_supplicant *wpa_s = ctx;
  1163. wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
  1164. if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
  1165. wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
  1166. "report received Probe Request frames");
  1167. return -1;
  1168. }
  1169. wpa_s->pending_listen_freq = freq;
  1170. wpa_s->pending_listen_duration = duration;
  1171. if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
  1172. wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
  1173. "to remain on channel (%u MHz) for Listen "
  1174. "state", freq);
  1175. wpa_s->pending_listen_freq = 0;
  1176. return -1;
  1177. }
  1178. wpa_s->off_channel_freq = 0;
  1179. wpa_s->roc_waiting_drv_freq = freq;
  1180. return 0;
  1181. }
  1182. static void wpas_stop_listen(void *ctx)
  1183. {
  1184. struct wpa_supplicant *wpa_s = ctx;
  1185. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  1186. wpa_drv_cancel_remain_on_channel(wpa_s);
  1187. wpa_s->off_channel_freq = 0;
  1188. wpa_s->roc_waiting_drv_freq = 0;
  1189. }
  1190. wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
  1191. wpa_drv_probe_req_report(wpa_s, 0);
  1192. }
  1193. static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
  1194. {
  1195. struct wpa_supplicant *wpa_s = ctx;
  1196. return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
  1197. }
  1198. /*
  1199. * DNS Header section is used only to calculate compression pointers, so the
  1200. * contents of this data does not matter, but the length needs to be reserved
  1201. * in the virtual packet.
  1202. */
  1203. #define DNS_HEADER_LEN 12
  1204. /*
  1205. * 27-octet in-memory packet from P2P specification containing two implied
  1206. * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
  1207. */
  1208. #define P2P_SD_IN_MEMORY_LEN 27
  1209. static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
  1210. u8 **spos, const u8 *end)
  1211. {
  1212. while (*spos < end) {
  1213. u8 val = ((*spos)[0] & 0xc0) >> 6;
  1214. int len;
  1215. if (val == 1 || val == 2) {
  1216. /* These are reserved values in RFC 1035 */
  1217. wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
  1218. "sequence starting with 0x%x", val);
  1219. return -1;
  1220. }
  1221. if (val == 3) {
  1222. u16 offset;
  1223. u8 *spos_tmp;
  1224. /* Offset */
  1225. if (*spos + 2 > end) {
  1226. wpa_printf(MSG_DEBUG, "P2P: No room for full "
  1227. "DNS offset field");
  1228. return -1;
  1229. }
  1230. offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
  1231. if (offset >= *spos - start) {
  1232. wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
  1233. "pointer offset %u", offset);
  1234. return -1;
  1235. }
  1236. (*spos) += 2;
  1237. spos_tmp = start + offset;
  1238. return p2p_sd_dns_uncompress_label(upos, uend, start,
  1239. &spos_tmp,
  1240. *spos - 2);
  1241. }
  1242. /* Label */
  1243. len = (*spos)[0] & 0x3f;
  1244. if (len == 0)
  1245. return 0;
  1246. (*spos)++;
  1247. if (*spos + len > end) {
  1248. wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
  1249. "sequence - no room for label with length "
  1250. "%u", len);
  1251. return -1;
  1252. }
  1253. if (*upos + len + 2 > uend)
  1254. return -2;
  1255. os_memcpy(*upos, *spos, len);
  1256. *spos += len;
  1257. *upos += len;
  1258. (*upos)[0] = '.';
  1259. (*upos)++;
  1260. (*upos)[0] = '\0';
  1261. }
  1262. return 0;
  1263. }
  1264. /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
  1265. * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
  1266. * not large enough */
  1267. static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
  1268. size_t msg_len, size_t offset)
  1269. {
  1270. /* 27-octet in-memory packet from P2P specification */
  1271. const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
  1272. "\x04_udp\xC0\x11\x00\x0C\x00\x01";
  1273. u8 *tmp, *end, *spos;
  1274. char *upos, *uend;
  1275. int ret = 0;
  1276. if (buf_len < 2)
  1277. return -1;
  1278. if (offset > msg_len)
  1279. return -1;
  1280. tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
  1281. if (tmp == NULL)
  1282. return -1;
  1283. spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
  1284. end = spos + msg_len;
  1285. spos += offset;
  1286. os_memset(tmp, 0, DNS_HEADER_LEN);
  1287. os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
  1288. os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
  1289. upos = buf;
  1290. uend = buf + buf_len;
  1291. ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
  1292. if (ret) {
  1293. os_free(tmp);
  1294. return ret;
  1295. }
  1296. if (upos == buf) {
  1297. upos[0] = '.';
  1298. upos[1] = '\0';
  1299. } else if (upos[-1] == '.')
  1300. upos[-1] = '\0';
  1301. os_free(tmp);
  1302. return 0;
  1303. }
  1304. static struct p2p_srv_bonjour *
  1305. wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
  1306. const struct wpabuf *query)
  1307. {
  1308. struct p2p_srv_bonjour *bsrv;
  1309. size_t len;
  1310. len = wpabuf_len(query);
  1311. dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
  1312. struct p2p_srv_bonjour, list) {
  1313. if (len == wpabuf_len(bsrv->query) &&
  1314. os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
  1315. len) == 0)
  1316. return bsrv;
  1317. }
  1318. return NULL;
  1319. }
  1320. static struct p2p_srv_upnp *
  1321. wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
  1322. const char *service)
  1323. {
  1324. struct p2p_srv_upnp *usrv;
  1325. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  1326. struct p2p_srv_upnp, list) {
  1327. if (version == usrv->version &&
  1328. os_strcmp(service, usrv->service) == 0)
  1329. return usrv;
  1330. }
  1331. return NULL;
  1332. }
  1333. static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
  1334. u8 srv_trans_id)
  1335. {
  1336. u8 *len_pos;
  1337. if (wpabuf_tailroom(resp) < 5)
  1338. return;
  1339. /* Length (to be filled) */
  1340. len_pos = wpabuf_put(resp, 2);
  1341. wpabuf_put_u8(resp, srv_proto);
  1342. wpabuf_put_u8(resp, srv_trans_id);
  1343. /* Status Code */
  1344. wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
  1345. /* Response Data: empty */
  1346. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1347. }
  1348. static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
  1349. struct wpabuf *resp, u8 srv_trans_id)
  1350. {
  1351. struct p2p_srv_bonjour *bsrv;
  1352. u8 *len_pos;
  1353. wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
  1354. if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  1355. wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
  1356. return;
  1357. }
  1358. dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
  1359. struct p2p_srv_bonjour, list) {
  1360. if (wpabuf_tailroom(resp) <
  1361. 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
  1362. return;
  1363. /* Length (to be filled) */
  1364. len_pos = wpabuf_put(resp, 2);
  1365. wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
  1366. wpabuf_put_u8(resp, srv_trans_id);
  1367. /* Status Code */
  1368. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1369. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
  1370. wpabuf_head(bsrv->resp),
  1371. wpabuf_len(bsrv->resp));
  1372. /* Response Data */
  1373. wpabuf_put_buf(resp, bsrv->query); /* Key */
  1374. wpabuf_put_buf(resp, bsrv->resp); /* Value */
  1375. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  1376. 2);
  1377. }
  1378. }
  1379. static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
  1380. size_t query_len)
  1381. {
  1382. char str_rx[256], str_srv[256];
  1383. if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
  1384. return 0; /* Too short to include DNS Type and Version */
  1385. if (os_memcmp(query + query_len - 3,
  1386. wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
  1387. 3) != 0)
  1388. return 0; /* Mismatch in DNS Type or Version */
  1389. if (query_len == wpabuf_len(bsrv->query) &&
  1390. os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
  1391. return 1; /* Binary match */
  1392. if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
  1393. 0))
  1394. return 0; /* Failed to uncompress query */
  1395. if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
  1396. wpabuf_head(bsrv->query),
  1397. wpabuf_len(bsrv->query) - 3, 0))
  1398. return 0; /* Failed to uncompress service */
  1399. return os_strcmp(str_rx, str_srv) == 0;
  1400. }
  1401. static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
  1402. struct wpabuf *resp, u8 srv_trans_id,
  1403. const u8 *query, size_t query_len)
  1404. {
  1405. struct p2p_srv_bonjour *bsrv;
  1406. u8 *len_pos;
  1407. int matches = 0;
  1408. wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
  1409. query, query_len);
  1410. if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  1411. wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
  1412. wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
  1413. srv_trans_id);
  1414. return;
  1415. }
  1416. if (query_len == 0) {
  1417. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  1418. return;
  1419. }
  1420. dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
  1421. struct p2p_srv_bonjour, list) {
  1422. if (!match_bonjour_query(bsrv, query, query_len))
  1423. continue;
  1424. if (wpabuf_tailroom(resp) <
  1425. 5 + query_len + wpabuf_len(bsrv->resp))
  1426. return;
  1427. matches++;
  1428. /* Length (to be filled) */
  1429. len_pos = wpabuf_put(resp, 2);
  1430. wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
  1431. wpabuf_put_u8(resp, srv_trans_id);
  1432. /* Status Code */
  1433. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1434. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
  1435. wpabuf_head(bsrv->resp),
  1436. wpabuf_len(bsrv->resp));
  1437. /* Response Data */
  1438. wpabuf_put_data(resp, query, query_len); /* Key */
  1439. wpabuf_put_buf(resp, bsrv->resp); /* Value */
  1440. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1441. }
  1442. if (matches == 0) {
  1443. wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
  1444. "available");
  1445. if (wpabuf_tailroom(resp) < 5)
  1446. return;
  1447. /* Length (to be filled) */
  1448. len_pos = wpabuf_put(resp, 2);
  1449. wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
  1450. wpabuf_put_u8(resp, srv_trans_id);
  1451. /* Status Code */
  1452. wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
  1453. /* Response Data: empty */
  1454. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  1455. 2);
  1456. }
  1457. }
  1458. static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
  1459. struct wpabuf *resp, u8 srv_trans_id)
  1460. {
  1461. struct p2p_srv_upnp *usrv;
  1462. u8 *len_pos;
  1463. wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
  1464. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
  1465. wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
  1466. return;
  1467. }
  1468. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  1469. struct p2p_srv_upnp, list) {
  1470. if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
  1471. return;
  1472. /* Length (to be filled) */
  1473. len_pos = wpabuf_put(resp, 2);
  1474. wpabuf_put_u8(resp, P2P_SERV_UPNP);
  1475. wpabuf_put_u8(resp, srv_trans_id);
  1476. /* Status Code */
  1477. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1478. /* Response Data */
  1479. wpabuf_put_u8(resp, usrv->version);
  1480. wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
  1481. usrv->service);
  1482. wpabuf_put_str(resp, usrv->service);
  1483. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  1484. 2);
  1485. }
  1486. }
  1487. static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
  1488. struct wpabuf *resp, u8 srv_trans_id,
  1489. const u8 *query, size_t query_len)
  1490. {
  1491. struct p2p_srv_upnp *usrv;
  1492. u8 *len_pos;
  1493. u8 version;
  1494. char *str;
  1495. int count = 0;
  1496. wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
  1497. query, query_len);
  1498. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
  1499. wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
  1500. wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
  1501. srv_trans_id);
  1502. return;
  1503. }
  1504. if (query_len == 0) {
  1505. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  1506. return;
  1507. }
  1508. if (wpabuf_tailroom(resp) < 5)
  1509. return;
  1510. /* Length (to be filled) */
  1511. len_pos = wpabuf_put(resp, 2);
  1512. wpabuf_put_u8(resp, P2P_SERV_UPNP);
  1513. wpabuf_put_u8(resp, srv_trans_id);
  1514. version = query[0];
  1515. str = os_malloc(query_len);
  1516. if (str == NULL)
  1517. return;
  1518. os_memcpy(str, query + 1, query_len - 1);
  1519. str[query_len - 1] = '\0';
  1520. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  1521. struct p2p_srv_upnp, list) {
  1522. if (version != usrv->version)
  1523. continue;
  1524. if (os_strcmp(str, "ssdp:all") != 0 &&
  1525. os_strstr(usrv->service, str) == NULL)
  1526. continue;
  1527. if (wpabuf_tailroom(resp) < 2)
  1528. break;
  1529. if (count == 0) {
  1530. /* Status Code */
  1531. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1532. /* Response Data */
  1533. wpabuf_put_u8(resp, version);
  1534. } else
  1535. wpabuf_put_u8(resp, ',');
  1536. count++;
  1537. wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
  1538. usrv->service);
  1539. if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
  1540. break;
  1541. wpabuf_put_str(resp, usrv->service);
  1542. }
  1543. os_free(str);
  1544. if (count == 0) {
  1545. wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
  1546. "available");
  1547. /* Status Code */
  1548. wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
  1549. /* Response Data: empty */
  1550. }
  1551. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1552. }
  1553. #ifdef CONFIG_WIFI_DISPLAY
  1554. static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
  1555. struct wpabuf *resp, u8 srv_trans_id,
  1556. const u8 *query, size_t query_len)
  1557. {
  1558. const u8 *pos;
  1559. u8 role;
  1560. u8 *len_pos;
  1561. wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
  1562. if (!wpa_s->global->wifi_display) {
  1563. wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
  1564. wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
  1565. srv_trans_id);
  1566. return;
  1567. }
  1568. if (query_len < 1) {
  1569. wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
  1570. "Role");
  1571. return;
  1572. }
  1573. if (wpabuf_tailroom(resp) < 5)
  1574. return;
  1575. pos = query;
  1576. role = *pos++;
  1577. wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
  1578. /* TODO: role specific handling */
  1579. /* Length (to be filled) */
  1580. len_pos = wpabuf_put(resp, 2);
  1581. wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
  1582. wpabuf_put_u8(resp, srv_trans_id);
  1583. wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
  1584. while (pos < query + query_len) {
  1585. if (*pos < MAX_WFD_SUBELEMS &&
  1586. wpa_s->global->wfd_subelem[*pos] &&
  1587. wpabuf_tailroom(resp) >=
  1588. wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
  1589. wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
  1590. "subelement %u", *pos);
  1591. wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
  1592. }
  1593. pos++;
  1594. }
  1595. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1596. }
  1597. #endif /* CONFIG_WIFI_DISPLAY */
  1598. void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
  1599. u16 update_indic, const u8 *tlvs, size_t tlvs_len)
  1600. {
  1601. struct wpa_supplicant *wpa_s = ctx;
  1602. const u8 *pos = tlvs;
  1603. const u8 *end = tlvs + tlvs_len;
  1604. const u8 *tlv_end;
  1605. u16 slen;
  1606. struct wpabuf *resp;
  1607. u8 srv_proto, srv_trans_id;
  1608. size_t buf_len;
  1609. char *buf;
  1610. wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
  1611. tlvs, tlvs_len);
  1612. buf_len = 2 * tlvs_len + 1;
  1613. buf = os_malloc(buf_len);
  1614. if (buf) {
  1615. wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
  1616. wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
  1617. MACSTR " %u %u %s",
  1618. freq, MAC2STR(sa), dialog_token, update_indic,
  1619. buf);
  1620. os_free(buf);
  1621. }
  1622. if (wpa_s->p2p_sd_over_ctrl_iface) {
  1623. wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
  1624. update_indic, tlvs, tlvs_len);
  1625. return; /* to be processed by an external program */
  1626. }
  1627. resp = wpabuf_alloc(10000);
  1628. if (resp == NULL)
  1629. return;
  1630. while (pos + 1 < end) {
  1631. wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
  1632. slen = WPA_GET_LE16(pos);
  1633. pos += 2;
  1634. if (pos + slen > end || slen < 2) {
  1635. wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
  1636. "length");
  1637. wpabuf_free(resp);
  1638. return;
  1639. }
  1640. tlv_end = pos + slen;
  1641. srv_proto = *pos++;
  1642. wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
  1643. srv_proto);
  1644. srv_trans_id = *pos++;
  1645. wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
  1646. srv_trans_id);
  1647. wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
  1648. pos, tlv_end - pos);
  1649. if (wpa_s->force_long_sd) {
  1650. wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
  1651. "response");
  1652. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  1653. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  1654. goto done;
  1655. }
  1656. switch (srv_proto) {
  1657. case P2P_SERV_ALL_SERVICES:
  1658. wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
  1659. "for all services");
  1660. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
  1661. dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  1662. wpa_printf(MSG_DEBUG, "P2P: No service "
  1663. "discovery protocols available");
  1664. wpas_sd_add_proto_not_avail(
  1665. resp, P2P_SERV_ALL_SERVICES,
  1666. srv_trans_id);
  1667. break;
  1668. }
  1669. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  1670. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  1671. break;
  1672. case P2P_SERV_BONJOUR:
  1673. wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
  1674. pos, tlv_end - pos);
  1675. break;
  1676. case P2P_SERV_UPNP:
  1677. wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
  1678. pos, tlv_end - pos);
  1679. break;
  1680. #ifdef CONFIG_WIFI_DISPLAY
  1681. case P2P_SERV_WIFI_DISPLAY:
  1682. wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
  1683. pos, tlv_end - pos);
  1684. break;
  1685. #endif /* CONFIG_WIFI_DISPLAY */
  1686. default:
  1687. wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
  1688. "protocol %u", srv_proto);
  1689. wpas_sd_add_proto_not_avail(resp, srv_proto,
  1690. srv_trans_id);
  1691. break;
  1692. }
  1693. pos = tlv_end;
  1694. }
  1695. done:
  1696. wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
  1697. update_indic, tlvs, tlvs_len);
  1698. wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
  1699. wpabuf_free(resp);
  1700. }
  1701. void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
  1702. const u8 *tlvs, size_t tlvs_len)
  1703. {
  1704. struct wpa_supplicant *wpa_s = ctx;
  1705. const u8 *pos = tlvs;
  1706. const u8 *end = tlvs + tlvs_len;
  1707. const u8 *tlv_end;
  1708. u16 slen;
  1709. size_t buf_len;
  1710. char *buf;
  1711. wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
  1712. tlvs, tlvs_len);
  1713. if (tlvs_len > 1500) {
  1714. /* TODO: better way for handling this */
  1715. wpa_msg_ctrl(wpa_s, MSG_INFO,
  1716. P2P_EVENT_SERV_DISC_RESP MACSTR
  1717. " %u <long response: %u bytes>",
  1718. MAC2STR(sa), update_indic,
  1719. (unsigned int) tlvs_len);
  1720. } else {
  1721. buf_len = 2 * tlvs_len + 1;
  1722. buf = os_malloc(buf_len);
  1723. if (buf) {
  1724. wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
  1725. wpa_msg_ctrl(wpa_s, MSG_INFO,
  1726. P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
  1727. MAC2STR(sa), update_indic, buf);
  1728. os_free(buf);
  1729. }
  1730. }
  1731. while (pos < end) {
  1732. u8 srv_proto, srv_trans_id, status;
  1733. wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
  1734. slen = WPA_GET_LE16(pos);
  1735. pos += 2;
  1736. if (pos + slen > end || slen < 3) {
  1737. wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
  1738. "length");
  1739. return;
  1740. }
  1741. tlv_end = pos + slen;
  1742. srv_proto = *pos++;
  1743. wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
  1744. srv_proto);
  1745. srv_trans_id = *pos++;
  1746. wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
  1747. srv_trans_id);
  1748. status = *pos++;
  1749. wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
  1750. status);
  1751. wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
  1752. pos, tlv_end - pos);
  1753. pos = tlv_end;
  1754. }
  1755. wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
  1756. }
  1757. u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
  1758. const struct wpabuf *tlvs)
  1759. {
  1760. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  1761. return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
  1762. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  1763. return 0;
  1764. return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
  1765. }
  1766. u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
  1767. u8 version, const char *query)
  1768. {
  1769. struct wpabuf *tlvs;
  1770. u64 ret;
  1771. tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
  1772. if (tlvs == NULL)
  1773. return 0;
  1774. wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
  1775. wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
  1776. wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
  1777. wpabuf_put_u8(tlvs, version);
  1778. wpabuf_put_str(tlvs, query);
  1779. ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
  1780. wpabuf_free(tlvs);
  1781. return ret;
  1782. }
  1783. #ifdef CONFIG_WIFI_DISPLAY
  1784. static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
  1785. const struct wpabuf *tlvs)
  1786. {
  1787. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  1788. return 0;
  1789. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  1790. return 0;
  1791. return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
  1792. }
  1793. #define MAX_WFD_SD_SUBELEMS 20
  1794. static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
  1795. const char *subelems)
  1796. {
  1797. u8 *len;
  1798. const char *pos;
  1799. int val;
  1800. int count = 0;
  1801. len = wpabuf_put(tlvs, 2);
  1802. wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
  1803. wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
  1804. wpabuf_put_u8(tlvs, role);
  1805. pos = subelems;
  1806. while (*pos) {
  1807. val = atoi(pos);
  1808. if (val >= 0 && val < 256) {
  1809. wpabuf_put_u8(tlvs, val);
  1810. count++;
  1811. if (count == MAX_WFD_SD_SUBELEMS)
  1812. break;
  1813. }
  1814. pos = os_strchr(pos + 1, ',');
  1815. if (pos == NULL)
  1816. break;
  1817. pos++;
  1818. }
  1819. WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
  1820. }
  1821. u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
  1822. const u8 *dst, const char *role)
  1823. {
  1824. struct wpabuf *tlvs;
  1825. u64 ret;
  1826. const char *subelems;
  1827. u8 id = 1;
  1828. subelems = os_strchr(role, ' ');
  1829. if (subelems == NULL)
  1830. return 0;
  1831. subelems++;
  1832. tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
  1833. if (tlvs == NULL)
  1834. return 0;
  1835. if (os_strstr(role, "[source]"))
  1836. wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
  1837. if (os_strstr(role, "[pri-sink]"))
  1838. wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
  1839. if (os_strstr(role, "[sec-sink]"))
  1840. wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
  1841. if (os_strstr(role, "[source+sink]"))
  1842. wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
  1843. ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
  1844. wpabuf_free(tlvs);
  1845. return ret;
  1846. }
  1847. #endif /* CONFIG_WIFI_DISPLAY */
  1848. int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
  1849. {
  1850. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  1851. return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
  1852. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  1853. return -1;
  1854. return p2p_sd_cancel_request(wpa_s->global->p2p,
  1855. (void *) (uintptr_t) req);
  1856. }
  1857. void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
  1858. const u8 *dst, u8 dialog_token,
  1859. const struct wpabuf *resp_tlvs)
  1860. {
  1861. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  1862. wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
  1863. resp_tlvs);
  1864. return;
  1865. }
  1866. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  1867. return;
  1868. p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
  1869. resp_tlvs);
  1870. }
  1871. void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
  1872. {
  1873. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  1874. wpa_drv_p2p_service_update(wpa_s);
  1875. return;
  1876. }
  1877. if (wpa_s->global->p2p)
  1878. p2p_sd_service_update(wpa_s->global->p2p);
  1879. }
  1880. static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
  1881. {
  1882. dl_list_del(&bsrv->list);
  1883. wpabuf_free(bsrv->query);
  1884. wpabuf_free(bsrv->resp);
  1885. os_free(bsrv);
  1886. }
  1887. static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
  1888. {
  1889. dl_list_del(&usrv->list);
  1890. os_free(usrv->service);
  1891. os_free(usrv);
  1892. }
  1893. void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
  1894. {
  1895. struct p2p_srv_bonjour *bsrv, *bn;
  1896. struct p2p_srv_upnp *usrv, *un;
  1897. dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
  1898. struct p2p_srv_bonjour, list)
  1899. wpas_p2p_srv_bonjour_free(bsrv);
  1900. dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
  1901. struct p2p_srv_upnp, list)
  1902. wpas_p2p_srv_upnp_free(usrv);
  1903. wpas_p2p_sd_service_update(wpa_s);
  1904. }
  1905. int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
  1906. struct wpabuf *query, struct wpabuf *resp)
  1907. {
  1908. struct p2p_srv_bonjour *bsrv;
  1909. bsrv = os_zalloc(sizeof(*bsrv));
  1910. if (bsrv == NULL)
  1911. return -1;
  1912. bsrv->query = query;
  1913. bsrv->resp = resp;
  1914. dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
  1915. wpas_p2p_sd_service_update(wpa_s);
  1916. return 0;
  1917. }
  1918. int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
  1919. const struct wpabuf *query)
  1920. {
  1921. struct p2p_srv_bonjour *bsrv;
  1922. bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
  1923. if (bsrv == NULL)
  1924. return -1;
  1925. wpas_p2p_srv_bonjour_free(bsrv);
  1926. wpas_p2p_sd_service_update(wpa_s);
  1927. return 0;
  1928. }
  1929. int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
  1930. const char *service)
  1931. {
  1932. struct p2p_srv_upnp *usrv;
  1933. if (wpas_p2p_service_get_upnp(wpa_s, version, service))
  1934. return 0; /* Already listed */
  1935. usrv = os_zalloc(sizeof(*usrv));
  1936. if (usrv == NULL)
  1937. return -1;
  1938. usrv->version = version;
  1939. usrv->service = os_strdup(service);
  1940. if (usrv->service == NULL) {
  1941. os_free(usrv);
  1942. return -1;
  1943. }
  1944. dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
  1945. wpas_p2p_sd_service_update(wpa_s);
  1946. return 0;
  1947. }
  1948. int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
  1949. const char *service)
  1950. {
  1951. struct p2p_srv_upnp *usrv;
  1952. usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
  1953. if (usrv == NULL)
  1954. return -1;
  1955. wpas_p2p_srv_upnp_free(usrv);
  1956. wpas_p2p_sd_service_update(wpa_s);
  1957. return 0;
  1958. }
  1959. static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
  1960. const u8 *peer, const char *params,
  1961. unsigned int generated_pin)
  1962. {
  1963. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
  1964. " %08d%s", MAC2STR(peer), generated_pin, params);
  1965. }
  1966. static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
  1967. const u8 *peer, const char *params)
  1968. {
  1969. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
  1970. "%s", MAC2STR(peer), params);
  1971. }
  1972. void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
  1973. const u8 *dev_addr, const u8 *pri_dev_type,
  1974. const char *dev_name, u16 supp_config_methods,
  1975. u8 dev_capab, u8 group_capab, const u8 *group_id,
  1976. size_t group_id_len)
  1977. {
  1978. struct wpa_supplicant *wpa_s = ctx;
  1979. char devtype[WPS_DEV_TYPE_BUFSIZE];
  1980. char params[300];
  1981. u8 empty_dev_type[8];
  1982. unsigned int generated_pin = 0;
  1983. struct wpa_supplicant *group = NULL;
  1984. if (group_id) {
  1985. for (group = wpa_s->global->ifaces; group; group = group->next)
  1986. {
  1987. struct wpa_ssid *s = group->current_ssid;
  1988. if (s != NULL &&
  1989. s->mode == WPAS_MODE_P2P_GO &&
  1990. group_id_len - ETH_ALEN == s->ssid_len &&
  1991. os_memcmp(group_id + ETH_ALEN, s->ssid,
  1992. s->ssid_len) == 0)
  1993. break;
  1994. }
  1995. }
  1996. if (pri_dev_type == NULL) {
  1997. os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
  1998. pri_dev_type = empty_dev_type;
  1999. }
  2000. os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
  2001. " pri_dev_type=%s name='%s' config_methods=0x%x "
  2002. "dev_capab=0x%x group_capab=0x%x%s%s",
  2003. MAC2STR(dev_addr),
  2004. wps_dev_type_bin2str(pri_dev_type, devtype,
  2005. sizeof(devtype)),
  2006. dev_name, supp_config_methods, dev_capab, group_capab,
  2007. group ? " group=" : "",
  2008. group ? group->ifname : "");
  2009. params[sizeof(params) - 1] = '\0';
  2010. if (config_methods & WPS_CONFIG_DISPLAY) {
  2011. generated_pin = wps_generate_pin();
  2012. wpas_prov_disc_local_display(wpa_s, peer, params,
  2013. generated_pin);
  2014. } else if (config_methods & WPS_CONFIG_KEYPAD)
  2015. wpas_prov_disc_local_keypad(wpa_s, peer, params);
  2016. else if (config_methods & WPS_CONFIG_PUSHBUTTON)
  2017. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
  2018. MACSTR "%s", MAC2STR(peer), params);
  2019. wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
  2020. P2P_PROV_DISC_SUCCESS,
  2021. config_methods, generated_pin);
  2022. }
  2023. void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
  2024. {
  2025. struct wpa_supplicant *wpa_s = ctx;
  2026. unsigned int generated_pin = 0;
  2027. char params[20];
  2028. if (wpa_s->pending_pd_before_join &&
  2029. (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
  2030. os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
  2031. wpa_s->pending_pd_before_join = 0;
  2032. wpa_printf(MSG_DEBUG, "P2P: Starting pending "
  2033. "join-existing-group operation");
  2034. wpas_p2p_join_start(wpa_s);
  2035. return;
  2036. }
  2037. if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
  2038. wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
  2039. os_snprintf(params, sizeof(params), " peer_go=%d",
  2040. wpa_s->pending_pd_use == AUTO_PD_JOIN);
  2041. else
  2042. params[0] = '\0';
  2043. if (config_methods & WPS_CONFIG_DISPLAY)
  2044. wpas_prov_disc_local_keypad(wpa_s, peer, params);
  2045. else if (config_methods & WPS_CONFIG_KEYPAD) {
  2046. generated_pin = wps_generate_pin();
  2047. wpas_prov_disc_local_display(wpa_s, peer, params,
  2048. generated_pin);
  2049. } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
  2050. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
  2051. MACSTR "%s", MAC2STR(peer), params);
  2052. wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
  2053. P2P_PROV_DISC_SUCCESS,
  2054. config_methods, generated_pin);
  2055. }
  2056. static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
  2057. enum p2p_prov_disc_status status)
  2058. {
  2059. struct wpa_supplicant *wpa_s = ctx;
  2060. if (wpa_s->p2p_fallback_to_go_neg) {
  2061. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
  2062. "failed - fall back to GO Negotiation");
  2063. wpas_p2p_fallback_to_go_neg(wpa_s, 0);
  2064. return;
  2065. }
  2066. if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
  2067. wpa_s->pending_pd_before_join = 0;
  2068. wpa_printf(MSG_DEBUG, "P2P: Starting pending "
  2069. "join-existing-group operation (no ACK for PD "
  2070. "Req attempts)");
  2071. wpas_p2p_join_start(wpa_s);
  2072. return;
  2073. }
  2074. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
  2075. " p2p_dev_addr=" MACSTR " status=%d",
  2076. MAC2STR(peer), status);
  2077. wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
  2078. status, 0, 0);
  2079. }
  2080. static int freq_included(const struct p2p_channels *channels, unsigned int freq)
  2081. {
  2082. if (channels == NULL)
  2083. return 1; /* Assume no restrictions */
  2084. return p2p_channels_includes_freq(channels, freq);
  2085. }
  2086. static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
  2087. const u8 *go_dev_addr, const u8 *ssid,
  2088. size_t ssid_len, int *go, u8 *group_bssid,
  2089. int *force_freq, int persistent_group,
  2090. const struct p2p_channels *channels)
  2091. {
  2092. struct wpa_supplicant *wpa_s = ctx;
  2093. struct wpa_ssid *s;
  2094. int res;
  2095. struct wpa_supplicant *grp;
  2096. if (!persistent_group) {
  2097. wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
  2098. " to join an active group", MAC2STR(sa));
  2099. if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
  2100. (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
  2101. == 0 ||
  2102. os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
  2103. wpa_printf(MSG_DEBUG, "P2P: Accept previously "
  2104. "authorized invitation");
  2105. goto accept_inv;
  2106. }
  2107. /*
  2108. * Do not accept the invitation automatically; notify user and
  2109. * request approval.
  2110. */
  2111. return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  2112. }
  2113. grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
  2114. if (grp) {
  2115. wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
  2116. "running persistent group");
  2117. if (*go)
  2118. os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
  2119. goto accept_inv;
  2120. }
  2121. if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
  2122. os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
  2123. wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
  2124. "invitation to re-invoke a persistent group");
  2125. } else if (!wpa_s->conf->persistent_reconnect)
  2126. return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  2127. for (s = wpa_s->conf->ssid; s; s = s->next) {
  2128. if (s->disabled == 2 &&
  2129. os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
  2130. s->ssid_len == ssid_len &&
  2131. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  2132. break;
  2133. }
  2134. if (!s) {
  2135. wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
  2136. " requested reinvocation of an unknown group",
  2137. MAC2STR(sa));
  2138. return P2P_SC_FAIL_UNKNOWN_GROUP;
  2139. }
  2140. if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
  2141. *go = 1;
  2142. if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
  2143. wpa_printf(MSG_DEBUG, "P2P: The only available "
  2144. "interface is already in use - reject "
  2145. "invitation");
  2146. return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
  2147. }
  2148. os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
  2149. } else if (s->mode == WPAS_MODE_P2P_GO) {
  2150. *go = 1;
  2151. if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
  2152. {
  2153. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  2154. "interface address for the group");
  2155. return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
  2156. }
  2157. os_memcpy(group_bssid, wpa_s->pending_interface_addr,
  2158. ETH_ALEN);
  2159. }
  2160. accept_inv:
  2161. wpas_p2p_set_own_freq_preference(wpa_s, 0);
  2162. /* Get one of the frequencies currently in use */
  2163. if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) {
  2164. wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match a channel already used by one of the interfaces");
  2165. *force_freq = res;
  2166. wpas_p2p_set_own_freq_preference(wpa_s, res);
  2167. }
  2168. if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
  2169. wpas_p2p_num_unused_channels(wpa_s) > 0) {
  2170. if (*go == 0) {
  2171. /* We are the client */
  2172. wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
  2173. "running a GO but we are capable of MCC, "
  2174. "figure out the best channel to use");
  2175. *force_freq = 0;
  2176. } else if (!freq_included(channels, *force_freq)) {
  2177. /* We are the GO, and *force_freq is not in the
  2178. * intersection */
  2179. wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
  2180. "in intersection but we are capable of MCC, "
  2181. "figure out the best channel to use",
  2182. *force_freq);
  2183. *force_freq = 0;
  2184. }
  2185. }
  2186. return P2P_SC_SUCCESS;
  2187. }
  2188. static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
  2189. const u8 *ssid, size_t ssid_len,
  2190. const u8 *go_dev_addr, u8 status,
  2191. int op_freq)
  2192. {
  2193. struct wpa_supplicant *wpa_s = ctx;
  2194. struct wpa_ssid *s;
  2195. for (s = wpa_s->conf->ssid; s; s = s->next) {
  2196. if (s->disabled == 2 &&
  2197. s->ssid_len == ssid_len &&
  2198. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  2199. break;
  2200. }
  2201. if (status == P2P_SC_SUCCESS) {
  2202. wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
  2203. " was accepted; op_freq=%d MHz",
  2204. MAC2STR(sa), op_freq);
  2205. if (s) {
  2206. int go = s->mode == WPAS_MODE_P2P_GO;
  2207. wpas_p2p_group_add_persistent(
  2208. wpa_s, s, go, go ? op_freq : 0, 0, NULL);
  2209. } else if (bssid) {
  2210. wpa_s->user_initiated_pd = 0;
  2211. wpas_p2p_join(wpa_s, bssid, go_dev_addr,
  2212. wpa_s->p2p_wps_method, 0);
  2213. }
  2214. return;
  2215. }
  2216. if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
  2217. wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
  2218. " was rejected (status %u)", MAC2STR(sa), status);
  2219. return;
  2220. }
  2221. if (!s) {
  2222. if (bssid) {
  2223. wpa_msg_global(wpa_s, MSG_INFO,
  2224. P2P_EVENT_INVITATION_RECEIVED
  2225. "sa=" MACSTR " go_dev_addr=" MACSTR
  2226. " bssid=" MACSTR " unknown-network",
  2227. MAC2STR(sa), MAC2STR(go_dev_addr),
  2228. MAC2STR(bssid));
  2229. } else {
  2230. wpa_msg_global(wpa_s, MSG_INFO,
  2231. P2P_EVENT_INVITATION_RECEIVED
  2232. "sa=" MACSTR " go_dev_addr=" MACSTR
  2233. " unknown-network",
  2234. MAC2STR(sa), MAC2STR(go_dev_addr));
  2235. }
  2236. return;
  2237. }
  2238. if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
  2239. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
  2240. "sa=" MACSTR " persistent=%d freq=%d",
  2241. MAC2STR(sa), s->id, op_freq);
  2242. } else {
  2243. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
  2244. "sa=" MACSTR " persistent=%d",
  2245. MAC2STR(sa), s->id);
  2246. }
  2247. }
  2248. static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
  2249. struct wpa_ssid *ssid,
  2250. const u8 *peer)
  2251. {
  2252. size_t i;
  2253. if (ssid == NULL)
  2254. return;
  2255. for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
  2256. if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
  2257. ETH_ALEN) == 0)
  2258. break;
  2259. }
  2260. if (i >= ssid->num_p2p_clients) {
  2261. if (ssid->mode != WPAS_MODE_P2P_GO &&
  2262. os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
  2263. wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
  2264. "due to invitation result", ssid->id);
  2265. wpas_notify_network_removed(wpa_s, ssid);
  2266. wpa_config_remove_network(wpa_s->conf, ssid->id);
  2267. return;
  2268. }
  2269. return; /* Peer not found in client list */
  2270. }
  2271. wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
  2272. "group %d client list due to invitation result",
  2273. MAC2STR(peer), ssid->id);
  2274. os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
  2275. ssid->p2p_client_list + (i + 1) * ETH_ALEN,
  2276. (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
  2277. ssid->num_p2p_clients--;
  2278. #ifndef CONFIG_NO_CONFIG_WRITE
  2279. if (wpa_s->parent->conf->update_config &&
  2280. wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
  2281. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  2282. #endif /* CONFIG_NO_CONFIG_WRITE */
  2283. }
  2284. static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
  2285. const u8 *peer)
  2286. {
  2287. struct wpa_ssid *ssid;
  2288. wpa_s = wpa_s->global->p2p_invite_group;
  2289. if (wpa_s == NULL)
  2290. return; /* No known invitation group */
  2291. ssid = wpa_s->current_ssid;
  2292. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  2293. !ssid->p2p_persistent_group)
  2294. return; /* Not operating as a GO in persistent group */
  2295. ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
  2296. ssid->ssid, ssid->ssid_len);
  2297. wpas_remove_persistent_peer(wpa_s, ssid, peer);
  2298. }
  2299. static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
  2300. const struct p2p_channels *channels,
  2301. const u8 *peer)
  2302. {
  2303. struct wpa_supplicant *wpa_s = ctx;
  2304. struct wpa_ssid *ssid;
  2305. if (bssid) {
  2306. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
  2307. "status=%d " MACSTR,
  2308. status, MAC2STR(bssid));
  2309. } else {
  2310. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
  2311. "status=%d ", status);
  2312. }
  2313. wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
  2314. wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
  2315. status, MAC2STR(peer));
  2316. if (wpa_s->pending_invite_ssid_id == -1) {
  2317. if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
  2318. wpas_remove_persistent_client(wpa_s, peer);
  2319. return; /* Invitation to active group */
  2320. }
  2321. if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
  2322. wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
  2323. "invitation exchange to indicate readiness for "
  2324. "re-invocation");
  2325. }
  2326. if (status != P2P_SC_SUCCESS) {
  2327. if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
  2328. ssid = wpa_config_get_network(
  2329. wpa_s->conf, wpa_s->pending_invite_ssid_id);
  2330. wpas_remove_persistent_peer(wpa_s, ssid, peer);
  2331. }
  2332. wpas_p2p_remove_pending_group_interface(wpa_s);
  2333. return;
  2334. }
  2335. ssid = wpa_config_get_network(wpa_s->conf,
  2336. wpa_s->pending_invite_ssid_id);
  2337. if (ssid == NULL) {
  2338. wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
  2339. "data matching with invitation");
  2340. return;
  2341. }
  2342. /*
  2343. * The peer could have missed our ctrl::ack frame for Invitation
  2344. * Response and continue retransmitting the frame. To reduce the
  2345. * likelihood of the peer not getting successful TX status for the
  2346. * Invitation Response frame, wait a short time here before starting
  2347. * the persistent group so that we will remain on the current channel to
  2348. * acknowledge any possible retransmission from the peer.
  2349. */
  2350. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
  2351. "starting persistent group");
  2352. os_sleep(0, 50000);
  2353. wpas_p2p_group_add_persistent(wpa_s, ssid,
  2354. ssid->mode == WPAS_MODE_P2P_GO,
  2355. wpa_s->p2p_persistent_go_freq,
  2356. wpa_s->p2p_go_ht40, channels);
  2357. }
  2358. static int wpas_p2p_disallowed_freq(struct wpa_global *global,
  2359. unsigned int freq)
  2360. {
  2361. unsigned int i;
  2362. if (global->p2p_disallow_freq == NULL)
  2363. return 0;
  2364. for (i = 0; i < global->num_p2p_disallow_freq; i++) {
  2365. if (freq >= global->p2p_disallow_freq[i].min &&
  2366. freq <= global->p2p_disallow_freq[i].max)
  2367. return 1;
  2368. }
  2369. return 0;
  2370. }
  2371. static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
  2372. {
  2373. reg->channel[reg->channels] = chan;
  2374. reg->channels++;
  2375. }
  2376. static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
  2377. struct p2p_channels *chan)
  2378. {
  2379. int i, cla = 0;
  2380. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
  2381. "band");
  2382. /* Operating class 81 - 2.4 GHz band channels 1..13 */
  2383. chan->reg_class[cla].reg_class = 81;
  2384. chan->reg_class[cla].channels = 0;
  2385. for (i = 0; i < 11; i++) {
  2386. if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
  2387. wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
  2388. }
  2389. if (chan->reg_class[cla].channels)
  2390. cla++;
  2391. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
  2392. "band");
  2393. /* Operating class 115 - 5 GHz, channels 36-48 */
  2394. chan->reg_class[cla].reg_class = 115;
  2395. chan->reg_class[cla].channels = 0;
  2396. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
  2397. wpas_p2p_add_chan(&chan->reg_class[cla], 36);
  2398. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
  2399. wpas_p2p_add_chan(&chan->reg_class[cla], 40);
  2400. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
  2401. wpas_p2p_add_chan(&chan->reg_class[cla], 44);
  2402. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
  2403. wpas_p2p_add_chan(&chan->reg_class[cla], 48);
  2404. if (chan->reg_class[cla].channels)
  2405. cla++;
  2406. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
  2407. "band");
  2408. /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
  2409. chan->reg_class[cla].reg_class = 124;
  2410. chan->reg_class[cla].channels = 0;
  2411. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
  2412. wpas_p2p_add_chan(&chan->reg_class[cla], 149);
  2413. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
  2414. wpas_p2p_add_chan(&chan->reg_class[cla], 153);
  2415. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
  2416. wpas_p2p_add_chan(&chan->reg_class[cla], 157);
  2417. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
  2418. wpas_p2p_add_chan(&chan->reg_class[cla], 161);
  2419. if (chan->reg_class[cla].channels)
  2420. cla++;
  2421. chan->reg_classes = cla;
  2422. return 0;
  2423. }
  2424. static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
  2425. u16 num_modes,
  2426. enum hostapd_hw_mode mode)
  2427. {
  2428. u16 i;
  2429. for (i = 0; i < num_modes; i++) {
  2430. if (modes[i].mode == mode)
  2431. return &modes[i];
  2432. }
  2433. return NULL;
  2434. }
  2435. static int has_channel(struct wpa_global *global,
  2436. struct hostapd_hw_modes *mode, u8 chan, int *flags)
  2437. {
  2438. int i;
  2439. unsigned int freq;
  2440. freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
  2441. chan * 5;
  2442. if (wpas_p2p_disallowed_freq(global, freq))
  2443. return 0;
  2444. for (i = 0; i < mode->num_channels; i++) {
  2445. if (mode->channels[i].chan == chan) {
  2446. if (flags)
  2447. *flags = mode->channels[i].flag;
  2448. return !(mode->channels[i].flag &
  2449. (HOSTAPD_CHAN_DISABLED |
  2450. HOSTAPD_CHAN_PASSIVE_SCAN |
  2451. HOSTAPD_CHAN_NO_IBSS |
  2452. HOSTAPD_CHAN_RADAR));
  2453. }
  2454. }
  2455. return 0;
  2456. }
  2457. struct p2p_oper_class_map {
  2458. enum hostapd_hw_mode mode;
  2459. u8 op_class;
  2460. u8 min_chan;
  2461. u8 max_chan;
  2462. u8 inc;
  2463. enum { BW20, BW40PLUS, BW40MINUS } bw;
  2464. };
  2465. static struct p2p_oper_class_map op_class[] = {
  2466. { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
  2467. #if 0 /* Do not enable HT40 on 2 GHz for now */
  2468. { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
  2469. { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
  2470. #endif
  2471. { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
  2472. { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
  2473. { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
  2474. { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
  2475. { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
  2476. { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
  2477. { -1, 0, 0, 0, 0, BW20 }
  2478. };
  2479. static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
  2480. struct hostapd_hw_modes *mode,
  2481. u8 channel, u8 bw)
  2482. {
  2483. int flag;
  2484. if (!has_channel(wpa_s->global, mode, channel, &flag))
  2485. return -1;
  2486. if (bw == BW40MINUS &&
  2487. (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
  2488. !has_channel(wpa_s->global, mode, channel - 4, NULL)))
  2489. return 0;
  2490. if (bw == BW40PLUS &&
  2491. (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
  2492. !has_channel(wpa_s->global, mode, channel + 4, NULL)))
  2493. return 0;
  2494. return 1;
  2495. }
  2496. static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
  2497. struct p2p_channels *chan)
  2498. {
  2499. struct hostapd_hw_modes *mode;
  2500. int cla, op;
  2501. if (wpa_s->hw.modes == NULL) {
  2502. wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
  2503. "of all supported channels; assume dualband "
  2504. "support");
  2505. return wpas_p2p_default_channels(wpa_s, chan);
  2506. }
  2507. cla = 0;
  2508. for (op = 0; op_class[op].op_class; op++) {
  2509. struct p2p_oper_class_map *o = &op_class[op];
  2510. u8 ch;
  2511. struct p2p_reg_class *reg = NULL;
  2512. mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
  2513. if (mode == NULL)
  2514. continue;
  2515. for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
  2516. if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
  2517. continue;
  2518. if (reg == NULL) {
  2519. wpa_printf(MSG_DEBUG, "P2P: Add operating "
  2520. "class %u", o->op_class);
  2521. reg = &chan->reg_class[cla];
  2522. cla++;
  2523. reg->reg_class = o->op_class;
  2524. }
  2525. reg->channel[reg->channels] = ch;
  2526. reg->channels++;
  2527. }
  2528. if (reg) {
  2529. wpa_hexdump(MSG_DEBUG, "P2P: Channels",
  2530. reg->channel, reg->channels);
  2531. }
  2532. }
  2533. chan->reg_classes = cla;
  2534. return 0;
  2535. }
  2536. int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
  2537. struct hostapd_hw_modes *mode, u8 channel)
  2538. {
  2539. int op, ret;
  2540. for (op = 0; op_class[op].op_class; op++) {
  2541. struct p2p_oper_class_map *o = &op_class[op];
  2542. u8 ch;
  2543. for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
  2544. if (o->mode != HOSTAPD_MODE_IEEE80211A ||
  2545. o->bw == BW20 || ch != channel)
  2546. continue;
  2547. ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
  2548. if (ret < 0)
  2549. continue;
  2550. else if (ret > 0)
  2551. return (o->bw == BW40MINUS) ? -1 : 1;
  2552. else
  2553. return 0;
  2554. }
  2555. }
  2556. return 0;
  2557. }
  2558. static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
  2559. size_t buf_len)
  2560. {
  2561. struct wpa_supplicant *wpa_s = ctx;
  2562. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2563. if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
  2564. break;
  2565. }
  2566. if (wpa_s == NULL)
  2567. return -1;
  2568. return wpa_drv_get_noa(wpa_s, buf, buf_len);
  2569. }
  2570. static int wpas_go_connected(void *ctx, const u8 *dev_addr)
  2571. {
  2572. struct wpa_supplicant *wpa_s = ctx;
  2573. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2574. struct wpa_ssid *ssid = wpa_s->current_ssid;
  2575. if (ssid == NULL)
  2576. continue;
  2577. if (ssid->mode != WPAS_MODE_INFRA)
  2578. continue;
  2579. if (wpa_s->wpa_state != WPA_COMPLETED &&
  2580. wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
  2581. continue;
  2582. if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
  2583. return 1;
  2584. }
  2585. return 0;
  2586. }
  2587. static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
  2588. {
  2589. struct wpa_supplicant *wpa_s = ctx;
  2590. wpa_msg_global(wpa_s, level, "P2P: %s", msg);
  2591. }
  2592. int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
  2593. {
  2594. struct wpa_interface iface;
  2595. struct wpa_supplicant *p2pdev_wpa_s;
  2596. char ifname[100];
  2597. char force_name[100];
  2598. int ret;
  2599. os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
  2600. wpa_s->ifname);
  2601. force_name[0] = '\0';
  2602. wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
  2603. ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
  2604. force_name, wpa_s->pending_interface_addr, NULL);
  2605. if (ret < 0) {
  2606. wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
  2607. return ret;
  2608. }
  2609. os_strlcpy(wpa_s->pending_interface_name, ifname,
  2610. sizeof(wpa_s->pending_interface_name));
  2611. os_memset(&iface, 0, sizeof(iface));
  2612. iface.p2p_mgmt = 1;
  2613. iface.ifname = wpa_s->pending_interface_name;
  2614. iface.driver = wpa_s->driver->name;
  2615. iface.driver_param = wpa_s->conf->driver_param;
  2616. iface.confname = wpa_s->confname;
  2617. p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
  2618. if (!p2pdev_wpa_s) {
  2619. wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
  2620. return -1;
  2621. }
  2622. p2pdev_wpa_s->parent = wpa_s;
  2623. wpa_s->pending_interface_name[0] = '\0';
  2624. return 0;
  2625. }
  2626. /**
  2627. * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
  2628. * @global: Pointer to global data from wpa_supplicant_init()
  2629. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2630. * Returns: 0 on success, -1 on failure
  2631. */
  2632. int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
  2633. {
  2634. struct p2p_config p2p;
  2635. unsigned int r;
  2636. int i;
  2637. if (wpa_s->conf->p2p_disabled)
  2638. return 0;
  2639. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
  2640. return 0;
  2641. if (global->p2p)
  2642. return 0;
  2643. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  2644. struct p2p_params params;
  2645. wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
  2646. os_memset(&params, 0, sizeof(params));
  2647. params.dev_name = wpa_s->conf->device_name;
  2648. os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
  2649. WPS_DEV_TYPE_LEN);
  2650. params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
  2651. os_memcpy(params.sec_dev_type,
  2652. wpa_s->conf->sec_device_type,
  2653. params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
  2654. if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
  2655. return -1;
  2656. return 0;
  2657. }
  2658. os_memset(&p2p, 0, sizeof(p2p));
  2659. p2p.cb_ctx = wpa_s;
  2660. p2p.debug_print = wpas_p2p_debug_print;
  2661. p2p.p2p_scan = wpas_p2p_scan;
  2662. p2p.send_action = wpas_send_action;
  2663. p2p.send_action_done = wpas_send_action_done;
  2664. p2p.go_neg_completed = wpas_go_neg_completed;
  2665. p2p.go_neg_req_rx = wpas_go_neg_req_rx;
  2666. p2p.dev_found = wpas_dev_found;
  2667. p2p.dev_lost = wpas_dev_lost;
  2668. p2p.find_stopped = wpas_find_stopped;
  2669. p2p.start_listen = wpas_start_listen;
  2670. p2p.stop_listen = wpas_stop_listen;
  2671. p2p.send_probe_resp = wpas_send_probe_resp;
  2672. p2p.sd_request = wpas_sd_request;
  2673. p2p.sd_response = wpas_sd_response;
  2674. p2p.prov_disc_req = wpas_prov_disc_req;
  2675. p2p.prov_disc_resp = wpas_prov_disc_resp;
  2676. p2p.prov_disc_fail = wpas_prov_disc_fail;
  2677. p2p.invitation_process = wpas_invitation_process;
  2678. p2p.invitation_received = wpas_invitation_received;
  2679. p2p.invitation_result = wpas_invitation_result;
  2680. p2p.get_noa = wpas_get_noa;
  2681. p2p.go_connected = wpas_go_connected;
  2682. os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
  2683. os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
  2684. p2p.dev_name = wpa_s->conf->device_name;
  2685. p2p.manufacturer = wpa_s->conf->manufacturer;
  2686. p2p.model_name = wpa_s->conf->model_name;
  2687. p2p.model_number = wpa_s->conf->model_number;
  2688. p2p.serial_number = wpa_s->conf->serial_number;
  2689. if (wpa_s->wps) {
  2690. os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
  2691. p2p.config_methods = wpa_s->wps->config_methods;
  2692. }
  2693. if (wpa_s->conf->p2p_listen_reg_class &&
  2694. wpa_s->conf->p2p_listen_channel) {
  2695. p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
  2696. p2p.channel = wpa_s->conf->p2p_listen_channel;
  2697. } else {
  2698. p2p.reg_class = 81;
  2699. /*
  2700. * Pick one of the social channels randomly as the listen
  2701. * channel.
  2702. */
  2703. os_get_random((u8 *) &r, sizeof(r));
  2704. p2p.channel = 1 + (r % 3) * 5;
  2705. }
  2706. wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
  2707. if (wpa_s->conf->p2p_oper_reg_class &&
  2708. wpa_s->conf->p2p_oper_channel) {
  2709. p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
  2710. p2p.op_channel = wpa_s->conf->p2p_oper_channel;
  2711. p2p.cfg_op_channel = 1;
  2712. wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
  2713. "%d:%d", p2p.op_reg_class, p2p.op_channel);
  2714. } else {
  2715. p2p.op_reg_class = 81;
  2716. /*
  2717. * Use random operation channel from (1, 6, 11) if no other
  2718. * preference is indicated.
  2719. */
  2720. os_get_random((u8 *) &r, sizeof(r));
  2721. p2p.op_channel = 1 + (r % 3) * 5;
  2722. p2p.cfg_op_channel = 0;
  2723. wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
  2724. "%d:%d", p2p.op_reg_class, p2p.op_channel);
  2725. }
  2726. if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
  2727. p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
  2728. p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
  2729. }
  2730. if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  2731. os_memcpy(p2p.country, wpa_s->conf->country, 2);
  2732. p2p.country[2] = 0x04;
  2733. } else
  2734. os_memcpy(p2p.country, "XX\x04", 3);
  2735. if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
  2736. wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
  2737. "channel list");
  2738. return -1;
  2739. }
  2740. os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
  2741. WPS_DEV_TYPE_LEN);
  2742. p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
  2743. os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
  2744. p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
  2745. p2p.concurrent_operations = !!(wpa_s->drv_flags &
  2746. WPA_DRIVER_FLAGS_P2P_CONCURRENT);
  2747. p2p.max_peers = 100;
  2748. if (wpa_s->conf->p2p_ssid_postfix) {
  2749. p2p.ssid_postfix_len =
  2750. os_strlen(wpa_s->conf->p2p_ssid_postfix);
  2751. if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
  2752. p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
  2753. os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
  2754. p2p.ssid_postfix_len);
  2755. }
  2756. p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
  2757. p2p.max_listen = wpa_s->max_remain_on_chan;
  2758. global->p2p = p2p_init(&p2p);
  2759. if (global->p2p == NULL)
  2760. return -1;
  2761. global->p2p_init_wpa_s = wpa_s;
  2762. for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
  2763. if (wpa_s->conf->wps_vendor_ext[i] == NULL)
  2764. continue;
  2765. p2p_add_wps_vendor_extension(
  2766. global->p2p, wpa_s->conf->wps_vendor_ext[i]);
  2767. }
  2768. return 0;
  2769. }
  2770. /**
  2771. * wpas_p2p_deinit - Deinitialize per-interface P2P data
  2772. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2773. *
  2774. * This function deinitialize per-interface P2P data.
  2775. */
  2776. void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
  2777. {
  2778. if (wpa_s->driver && wpa_s->drv_priv)
  2779. wpa_drv_probe_req_report(wpa_s, 0);
  2780. if (wpa_s->go_params) {
  2781. /* Clear any stored provisioning info */
  2782. p2p_clear_provisioning_info(
  2783. wpa_s->global->p2p,
  2784. wpa_s->go_params->peer_device_addr);
  2785. }
  2786. os_free(wpa_s->go_params);
  2787. wpa_s->go_params = NULL;
  2788. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  2789. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2790. wpa_s->p2p_long_listen = 0;
  2791. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  2792. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  2793. wpas_p2p_remove_pending_group_interface(wpa_s);
  2794. /* TODO: remove group interface from the driver if this wpa_s instance
  2795. * is on top of a P2P group interface */
  2796. }
  2797. /**
  2798. * wpas_p2p_deinit_global - Deinitialize global P2P module
  2799. * @global: Pointer to global data from wpa_supplicant_init()
  2800. *
  2801. * This function deinitializes the global (per device) P2P module.
  2802. */
  2803. void wpas_p2p_deinit_global(struct wpa_global *global)
  2804. {
  2805. struct wpa_supplicant *wpa_s, *tmp;
  2806. wpa_s = global->ifaces;
  2807. if (wpa_s)
  2808. wpas_p2p_service_flush(wpa_s);
  2809. if (global->p2p == NULL)
  2810. return;
  2811. /* Remove remaining P2P group interfaces */
  2812. while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
  2813. wpa_s = wpa_s->next;
  2814. while (wpa_s) {
  2815. tmp = global->ifaces;
  2816. while (tmp &&
  2817. (tmp == wpa_s ||
  2818. tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
  2819. tmp = tmp->next;
  2820. }
  2821. if (tmp == NULL)
  2822. break;
  2823. /* Disconnect from the P2P group and deinit the interface */
  2824. wpas_p2p_disconnect(tmp);
  2825. }
  2826. /*
  2827. * Deinit GO data on any possibly remaining interface (if main
  2828. * interface is used as GO).
  2829. */
  2830. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2831. if (wpa_s->ap_iface)
  2832. wpas_p2p_group_deinit(wpa_s);
  2833. }
  2834. p2p_deinit(global->p2p);
  2835. global->p2p = NULL;
  2836. global->p2p_init_wpa_s = NULL;
  2837. }
  2838. static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
  2839. {
  2840. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
  2841. wpa_s->conf->p2p_no_group_iface)
  2842. return 0; /* separate interface disabled per configuration */
  2843. if (wpa_s->drv_flags &
  2844. (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
  2845. WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
  2846. return 1; /* P2P group requires a new interface in every case
  2847. */
  2848. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
  2849. return 0; /* driver does not support concurrent operations */
  2850. if (wpa_s->global->ifaces->next)
  2851. return 1; /* more that one interface already in use */
  2852. if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
  2853. return 1; /* this interface is already in use */
  2854. return 0;
  2855. }
  2856. static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
  2857. const u8 *peer_addr,
  2858. enum p2p_wps_method wps_method,
  2859. int go_intent, const u8 *own_interface_addr,
  2860. unsigned int force_freq, int persistent_group,
  2861. struct wpa_ssid *ssid, unsigned int pref_freq)
  2862. {
  2863. if (persistent_group && wpa_s->conf->persistent_reconnect)
  2864. persistent_group = 2;
  2865. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  2866. return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
  2867. go_intent, own_interface_addr,
  2868. force_freq, persistent_group);
  2869. }
  2870. /*
  2871. * Increase GO config timeout if HT40 is used since it takes some time
  2872. * to scan channels for coex purposes before the BSS can be started.
  2873. */
  2874. p2p_set_config_timeout(wpa_s->global->p2p,
  2875. wpa_s->p2p_go_ht40 ? 255 : 100, 20);
  2876. return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
  2877. go_intent, own_interface_addr, force_freq,
  2878. persistent_group, ssid ? ssid->ssid : NULL,
  2879. ssid ? ssid->ssid_len : 0,
  2880. wpa_s->p2p_pd_before_go_neg, pref_freq);
  2881. }
  2882. static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
  2883. const u8 *peer_addr,
  2884. enum p2p_wps_method wps_method,
  2885. int go_intent, const u8 *own_interface_addr,
  2886. unsigned int force_freq, int persistent_group,
  2887. struct wpa_ssid *ssid, unsigned int pref_freq)
  2888. {
  2889. if (persistent_group && wpa_s->conf->persistent_reconnect)
  2890. persistent_group = 2;
  2891. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  2892. return -1;
  2893. return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
  2894. go_intent, own_interface_addr, force_freq,
  2895. persistent_group, ssid ? ssid->ssid : NULL,
  2896. ssid ? ssid->ssid_len : 0, pref_freq);
  2897. }
  2898. static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
  2899. {
  2900. wpa_s->p2p_join_scan_count++;
  2901. wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
  2902. wpa_s->p2p_join_scan_count);
  2903. if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
  2904. wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
  2905. " for join operationg - stop join attempt",
  2906. MAC2STR(wpa_s->pending_join_iface_addr));
  2907. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2908. if (wpa_s->p2p_auto_pd) {
  2909. wpa_s->p2p_auto_pd = 0;
  2910. wpa_msg_global(wpa_s, MSG_INFO,
  2911. P2P_EVENT_PROV_DISC_FAILURE
  2912. " p2p_dev_addr=" MACSTR " status=N/A",
  2913. MAC2STR(wpa_s->pending_join_dev_addr));
  2914. return;
  2915. }
  2916. wpa_msg_global(wpa_s->parent, MSG_INFO,
  2917. P2P_EVENT_GROUP_FORMATION_FAILURE);
  2918. }
  2919. }
  2920. static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
  2921. {
  2922. int *freqs, res, num, i;
  2923. if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
  2924. /* Multiple channels are supported and not all are in use */
  2925. return 0;
  2926. }
  2927. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  2928. if (!freqs)
  2929. return 1;
  2930. num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
  2931. wpa_s->num_multichan_concurrent);
  2932. if (num < 0) {
  2933. res = 1;
  2934. goto exit_free;
  2935. }
  2936. for (i = 0; i < num; i++) {
  2937. if (freqs[i] == freq) {
  2938. wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
  2939. freq);
  2940. res = 0;
  2941. goto exit_free;
  2942. }
  2943. }
  2944. res = 1;
  2945. exit_free:
  2946. os_free(freqs);
  2947. return res;
  2948. }
  2949. static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
  2950. const u8 *peer_dev_addr)
  2951. {
  2952. struct wpa_bss *bss;
  2953. int updated;
  2954. bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
  2955. if (bss == NULL)
  2956. return -1;
  2957. if (bss->last_update_idx < wpa_s->bss_update_idx) {
  2958. wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
  2959. "last scan");
  2960. return 0;
  2961. }
  2962. updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
  2963. wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
  2964. "%ld.%06ld (%supdated in last scan)",
  2965. bss->last_update.sec, bss->last_update.usec,
  2966. updated ? "": "not ");
  2967. return updated;
  2968. }
  2969. static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
  2970. struct wpa_scan_results *scan_res)
  2971. {
  2972. struct wpa_bss *bss;
  2973. int freq;
  2974. u8 iface_addr[ETH_ALEN];
  2975. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2976. if (wpa_s->global->p2p_disabled)
  2977. return;
  2978. wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
  2979. scan_res ? (int) scan_res->num : -1,
  2980. wpa_s->p2p_auto_join ? "auto_" : "");
  2981. if (scan_res)
  2982. wpas_p2p_scan_res_handler(wpa_s, scan_res);
  2983. if (wpa_s->p2p_auto_pd) {
  2984. int join = wpas_p2p_peer_go(wpa_s,
  2985. wpa_s->pending_join_dev_addr);
  2986. if (join == 0 &&
  2987. wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
  2988. wpa_s->auto_pd_scan_retry++;
  2989. bss = wpa_bss_get_bssid_latest(
  2990. wpa_s, wpa_s->pending_join_dev_addr);
  2991. if (bss) {
  2992. freq = bss->freq;
  2993. wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
  2994. "the peer " MACSTR " at %d MHz",
  2995. wpa_s->auto_pd_scan_retry,
  2996. MAC2STR(wpa_s->
  2997. pending_join_dev_addr),
  2998. freq);
  2999. wpas_p2p_join_scan_req(wpa_s, freq);
  3000. return;
  3001. }
  3002. }
  3003. if (join < 0)
  3004. join = 0;
  3005. wpa_s->p2p_auto_pd = 0;
  3006. wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
  3007. wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
  3008. MAC2STR(wpa_s->pending_join_dev_addr), join);
  3009. if (p2p_prov_disc_req(wpa_s->global->p2p,
  3010. wpa_s->pending_join_dev_addr,
  3011. wpa_s->pending_pd_config_methods, join,
  3012. 0, wpa_s->user_initiated_pd) < 0) {
  3013. wpa_s->p2p_auto_pd = 0;
  3014. wpa_msg_global(wpa_s, MSG_INFO,
  3015. P2P_EVENT_PROV_DISC_FAILURE
  3016. " p2p_dev_addr=" MACSTR " status=N/A",
  3017. MAC2STR(wpa_s->pending_join_dev_addr));
  3018. }
  3019. return;
  3020. }
  3021. if (wpa_s->p2p_auto_join) {
  3022. int join = wpas_p2p_peer_go(wpa_s,
  3023. wpa_s->pending_join_dev_addr);
  3024. if (join < 0) {
  3025. wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
  3026. "running a GO -> use GO Negotiation");
  3027. wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
  3028. wpa_s->p2p_pin, wpa_s->p2p_wps_method,
  3029. wpa_s->p2p_persistent_group, 0, 0, 0,
  3030. wpa_s->p2p_go_intent,
  3031. wpa_s->p2p_connect_freq,
  3032. wpa_s->p2p_persistent_id,
  3033. wpa_s->p2p_pd_before_go_neg,
  3034. wpa_s->p2p_go_ht40);
  3035. return;
  3036. }
  3037. wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
  3038. "try to join the group", join ? "" :
  3039. " in older scan");
  3040. if (!join)
  3041. wpa_s->p2p_fallback_to_go_neg = 1;
  3042. }
  3043. freq = p2p_get_oper_freq(wpa_s->global->p2p,
  3044. wpa_s->pending_join_iface_addr);
  3045. if (freq < 0 &&
  3046. p2p_get_interface_addr(wpa_s->global->p2p,
  3047. wpa_s->pending_join_dev_addr,
  3048. iface_addr) == 0 &&
  3049. os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
  3050. {
  3051. wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
  3052. "address for join from " MACSTR " to " MACSTR
  3053. " based on newly discovered P2P peer entry",
  3054. MAC2STR(wpa_s->pending_join_iface_addr),
  3055. MAC2STR(iface_addr));
  3056. os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
  3057. ETH_ALEN);
  3058. freq = p2p_get_oper_freq(wpa_s->global->p2p,
  3059. wpa_s->pending_join_iface_addr);
  3060. }
  3061. if (freq >= 0) {
  3062. wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
  3063. "from P2P peer table: %d MHz", freq);
  3064. }
  3065. bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
  3066. if (bss) {
  3067. freq = bss->freq;
  3068. wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
  3069. "from BSS table: %d MHz", freq);
  3070. }
  3071. if (freq > 0) {
  3072. u16 method;
  3073. if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
  3074. wpa_msg_global(wpa_s->parent, MSG_INFO,
  3075. P2P_EVENT_GROUP_FORMATION_FAILURE
  3076. "reason=FREQ_CONFLICT");
  3077. return;
  3078. }
  3079. wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
  3080. "prior to joining an existing group (GO " MACSTR
  3081. " freq=%u MHz)",
  3082. MAC2STR(wpa_s->pending_join_dev_addr), freq);
  3083. wpa_s->pending_pd_before_join = 1;
  3084. switch (wpa_s->pending_join_wps_method) {
  3085. case WPS_PIN_DISPLAY:
  3086. method = WPS_CONFIG_KEYPAD;
  3087. break;
  3088. case WPS_PIN_KEYPAD:
  3089. method = WPS_CONFIG_DISPLAY;
  3090. break;
  3091. case WPS_PBC:
  3092. method = WPS_CONFIG_PUSHBUTTON;
  3093. break;
  3094. default:
  3095. method = 0;
  3096. break;
  3097. }
  3098. if ((p2p_get_provisioning_info(wpa_s->global->p2p,
  3099. wpa_s->pending_join_dev_addr) ==
  3100. method)) {
  3101. /*
  3102. * We have already performed provision discovery for
  3103. * joining the group. Proceed directly to join
  3104. * operation without duplicated provision discovery. */
  3105. wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
  3106. "with " MACSTR " already done - proceed to "
  3107. "join",
  3108. MAC2STR(wpa_s->pending_join_dev_addr));
  3109. wpa_s->pending_pd_before_join = 0;
  3110. goto start;
  3111. }
  3112. if (p2p_prov_disc_req(wpa_s->global->p2p,
  3113. wpa_s->pending_join_dev_addr, method, 1,
  3114. freq, wpa_s->user_initiated_pd) < 0) {
  3115. wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
  3116. "Discovery Request before joining an "
  3117. "existing group");
  3118. wpa_s->pending_pd_before_join = 0;
  3119. goto start;
  3120. }
  3121. return;
  3122. }
  3123. wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
  3124. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3125. eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
  3126. wpas_p2p_check_join_scan_limit(wpa_s);
  3127. return;
  3128. start:
  3129. /* Start join operation immediately */
  3130. wpas_p2p_join_start(wpa_s);
  3131. }
  3132. static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
  3133. {
  3134. int ret;
  3135. struct wpa_driver_scan_params params;
  3136. struct wpabuf *wps_ie, *ies;
  3137. size_t ielen;
  3138. int freqs[2] = { 0, 0 };
  3139. os_memset(&params, 0, sizeof(params));
  3140. /* P2P Wildcard SSID */
  3141. params.num_ssids = 1;
  3142. params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
  3143. params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
  3144. wpa_s->wps->dev.p2p = 1;
  3145. wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
  3146. wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
  3147. NULL);
  3148. if (wps_ie == NULL) {
  3149. wpas_p2p_scan_res_join(wpa_s, NULL);
  3150. return;
  3151. }
  3152. ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
  3153. ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
  3154. if (ies == NULL) {
  3155. wpabuf_free(wps_ie);
  3156. wpas_p2p_scan_res_join(wpa_s, NULL);
  3157. return;
  3158. }
  3159. wpabuf_put_buf(ies, wps_ie);
  3160. wpabuf_free(wps_ie);
  3161. p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
  3162. params.p2p_probe = 1;
  3163. params.extra_ies = wpabuf_head(ies);
  3164. params.extra_ies_len = wpabuf_len(ies);
  3165. if (freq > 0) {
  3166. freqs[0] = freq;
  3167. params.freqs = freqs;
  3168. }
  3169. /*
  3170. * Run a scan to update BSS table and start Provision Discovery once
  3171. * the new scan results become available.
  3172. */
  3173. ret = wpa_drv_scan(wpa_s, &params);
  3174. if (!ret) {
  3175. os_get_time(&wpa_s->scan_trigger_time);
  3176. wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
  3177. }
  3178. wpabuf_free(ies);
  3179. if (ret) {
  3180. wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
  3181. "try again later");
  3182. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3183. eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
  3184. wpas_p2p_check_join_scan_limit(wpa_s);
  3185. }
  3186. }
  3187. static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
  3188. {
  3189. struct wpa_supplicant *wpa_s = eloop_ctx;
  3190. wpas_p2p_join_scan_req(wpa_s, 0);
  3191. }
  3192. static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
  3193. const u8 *dev_addr, enum p2p_wps_method wps_method,
  3194. int auto_join)
  3195. {
  3196. wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
  3197. MACSTR " dev " MACSTR ")%s",
  3198. MAC2STR(iface_addr), MAC2STR(dev_addr),
  3199. auto_join ? " (auto_join)" : "");
  3200. wpa_s->p2p_auto_pd = 0;
  3201. wpa_s->p2p_auto_join = !!auto_join;
  3202. os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
  3203. os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
  3204. wpa_s->pending_join_wps_method = wps_method;
  3205. /* Make sure we are not running find during connection establishment */
  3206. wpas_p2p_stop_find(wpa_s);
  3207. wpa_s->p2p_join_scan_count = 0;
  3208. wpas_p2p_join_scan(wpa_s, NULL);
  3209. return 0;
  3210. }
  3211. static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
  3212. {
  3213. struct wpa_supplicant *group;
  3214. struct p2p_go_neg_results res;
  3215. struct wpa_bss *bss;
  3216. group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
  3217. if (group == NULL)
  3218. return -1;
  3219. if (group != wpa_s) {
  3220. os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
  3221. sizeof(group->p2p_pin));
  3222. group->p2p_wps_method = wpa_s->p2p_wps_method;
  3223. } else {
  3224. /*
  3225. * Need to mark the current interface for p2p_group_formation
  3226. * when a separate group interface is not used. This is needed
  3227. * to allow p2p_cancel stop a pending p2p_connect-join.
  3228. * wpas_p2p_init_group_interface() addresses this for the case
  3229. * where a separate group interface is used.
  3230. */
  3231. wpa_s->global->p2p_group_formation = wpa_s;
  3232. }
  3233. group->p2p_in_provisioning = 1;
  3234. group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
  3235. os_memset(&res, 0, sizeof(res));
  3236. os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
  3237. ETH_ALEN);
  3238. res.wps_method = wpa_s->pending_join_wps_method;
  3239. bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
  3240. if (bss) {
  3241. res.freq = bss->freq;
  3242. res.ssid_len = bss->ssid_len;
  3243. os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
  3244. }
  3245. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  3246. wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
  3247. "starting client");
  3248. wpa_drv_cancel_remain_on_channel(wpa_s);
  3249. wpa_s->off_channel_freq = 0;
  3250. wpa_s->roc_waiting_drv_freq = 0;
  3251. }
  3252. wpas_start_wps_enrollee(group, &res);
  3253. /*
  3254. * Allow a longer timeout for join-a-running-group than normal 15
  3255. * second group formation timeout since the GO may not have authorized
  3256. * our connection yet.
  3257. */
  3258. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  3259. eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
  3260. wpa_s, NULL);
  3261. return 0;
  3262. }
  3263. static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
  3264. int *force_freq, int *pref_freq)
  3265. {
  3266. int *freqs, res;
  3267. unsigned int freq_in_use = 0, num, i;
  3268. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  3269. if (!freqs)
  3270. return -1;
  3271. num = get_shared_radio_freqs(wpa_s, freqs,
  3272. wpa_s->num_multichan_concurrent);
  3273. wpa_printf(MSG_DEBUG,
  3274. "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
  3275. freq, wpa_s->num_multichan_concurrent, num);
  3276. if (freq > 0) {
  3277. if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
  3278. wpa_printf(MSG_DEBUG, "P2P: The forced channel "
  3279. "(%u MHz) is not supported for P2P uses",
  3280. freq);
  3281. res = -3;
  3282. goto exit_free;
  3283. }
  3284. for (i = 0; i < num; i++) {
  3285. if (freqs[i] == freq)
  3286. freq_in_use = 1;
  3287. }
  3288. if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
  3289. wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
  3290. freq);
  3291. res = -2;
  3292. goto exit_free;
  3293. }
  3294. wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
  3295. "requested channel (%u MHz)", freq);
  3296. *force_freq = freq;
  3297. goto exit_ok;
  3298. }
  3299. for (i = 0; i < num; i++) {
  3300. if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
  3301. continue;
  3302. wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
  3303. *force_freq);
  3304. *force_freq = freqs[i];
  3305. if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
  3306. wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency we are already using");
  3307. *pref_freq = *force_freq;
  3308. }
  3309. break;
  3310. }
  3311. if (i == num) {
  3312. if (num < wpa_s->num_multichan_concurrent) {
  3313. wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
  3314. *force_freq = 0;
  3315. } else {
  3316. wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
  3317. res = -2;
  3318. goto exit_free;
  3319. }
  3320. }
  3321. exit_ok:
  3322. res = 0;
  3323. exit_free:
  3324. os_free(freqs);
  3325. return res;
  3326. }
  3327. /**
  3328. * wpas_p2p_connect - Request P2P Group Formation to be started
  3329. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3330. * @peer_addr: Address of the peer P2P Device
  3331. * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
  3332. * @persistent_group: Whether to create a persistent group
  3333. * @auto_join: Whether to select join vs. GO Negotiation automatically
  3334. * @join: Whether to join an existing group (as a client) instead of starting
  3335. * Group Owner negotiation; @peer_addr is BSSID in that case
  3336. * @auth: Whether to only authorize the connection instead of doing that and
  3337. * initiating Group Owner negotiation
  3338. * @go_intent: GO Intent or -1 to use default
  3339. * @freq: Frequency for the group or 0 for auto-selection
  3340. * @persistent_id: Persistent group credentials to use for forcing GO
  3341. * parameters or -1 to generate new values (SSID/passphrase)
  3342. * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
  3343. * interoperability workaround when initiating group formation
  3344. * @ht40: Start GO with 40 MHz channel width
  3345. * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
  3346. * failure, -2 on failure due to channel not currently available,
  3347. * -3 if forced channel is not supported
  3348. */
  3349. int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  3350. const char *pin, enum p2p_wps_method wps_method,
  3351. int persistent_group, int auto_join, int join, int auth,
  3352. int go_intent, int freq, int persistent_id, int pd,
  3353. int ht40)
  3354. {
  3355. int force_freq = 0, pref_freq = 0;
  3356. int ret = 0, res;
  3357. enum wpa_driver_if_type iftype;
  3358. const u8 *if_addr;
  3359. struct wpa_ssid *ssid = NULL;
  3360. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3361. return -1;
  3362. if (persistent_id >= 0) {
  3363. ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
  3364. if (ssid == NULL || ssid->disabled != 2 ||
  3365. ssid->mode != WPAS_MODE_P2P_GO)
  3366. return -1;
  3367. }
  3368. if (go_intent < 0)
  3369. go_intent = wpa_s->conf->p2p_go_intent;
  3370. if (!auth)
  3371. wpa_s->p2p_long_listen = 0;
  3372. wpa_s->p2p_wps_method = wps_method;
  3373. wpa_s->p2p_persistent_group = !!persistent_group;
  3374. wpa_s->p2p_persistent_id = persistent_id;
  3375. wpa_s->p2p_go_intent = go_intent;
  3376. wpa_s->p2p_connect_freq = freq;
  3377. wpa_s->p2p_fallback_to_go_neg = 0;
  3378. wpa_s->p2p_pd_before_go_neg = !!pd;
  3379. wpa_s->p2p_go_ht40 = !!ht40;
  3380. if (pin)
  3381. os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
  3382. else if (wps_method == WPS_PIN_DISPLAY) {
  3383. ret = wps_generate_pin();
  3384. os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
  3385. ret);
  3386. wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
  3387. wpa_s->p2p_pin);
  3388. } else
  3389. wpa_s->p2p_pin[0] = '\0';
  3390. if (join || auto_join) {
  3391. u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
  3392. if (auth) {
  3393. wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
  3394. "connect a running group from " MACSTR,
  3395. MAC2STR(peer_addr));
  3396. os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
  3397. return ret;
  3398. }
  3399. os_memcpy(dev_addr, peer_addr, ETH_ALEN);
  3400. if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
  3401. iface_addr) < 0) {
  3402. os_memcpy(iface_addr, peer_addr, ETH_ALEN);
  3403. p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
  3404. dev_addr);
  3405. }
  3406. if (auto_join) {
  3407. os_get_time(&wpa_s->p2p_auto_started);
  3408. wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
  3409. "%ld.%06ld",
  3410. wpa_s->p2p_auto_started.sec,
  3411. wpa_s->p2p_auto_started.usec);
  3412. }
  3413. wpa_s->user_initiated_pd = 1;
  3414. if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
  3415. auto_join) < 0)
  3416. return -1;
  3417. return ret;
  3418. }
  3419. res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq);
  3420. if (res)
  3421. return res;
  3422. wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
  3423. wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
  3424. if (wpa_s->create_p2p_iface) {
  3425. /* Prepare to add a new interface for the group */
  3426. iftype = WPA_IF_P2P_GROUP;
  3427. if (go_intent == 15)
  3428. iftype = WPA_IF_P2P_GO;
  3429. if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
  3430. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  3431. "interface for the group");
  3432. return -1;
  3433. }
  3434. if_addr = wpa_s->pending_interface_addr;
  3435. } else
  3436. if_addr = wpa_s->own_addr;
  3437. if (auth) {
  3438. if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
  3439. go_intent, if_addr,
  3440. force_freq, persistent_group, ssid,
  3441. pref_freq) < 0)
  3442. return -1;
  3443. return ret;
  3444. }
  3445. if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
  3446. go_intent, if_addr, force_freq,
  3447. persistent_group, ssid, pref_freq) < 0) {
  3448. if (wpa_s->create_p2p_iface)
  3449. wpas_p2p_remove_pending_group_interface(wpa_s);
  3450. return -1;
  3451. }
  3452. return ret;
  3453. }
  3454. /**
  3455. * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
  3456. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3457. * @freq: Frequency of the channel in MHz
  3458. * @duration: Duration of the stay on the channel in milliseconds
  3459. *
  3460. * This callback is called when the driver indicates that it has started the
  3461. * requested remain-on-channel duration.
  3462. */
  3463. void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  3464. unsigned int freq, unsigned int duration)
  3465. {
  3466. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3467. return;
  3468. if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
  3469. p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
  3470. wpa_s->pending_listen_duration);
  3471. wpa_s->pending_listen_freq = 0;
  3472. }
  3473. }
  3474. static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
  3475. unsigned int timeout)
  3476. {
  3477. /* Limit maximum Listen state time based on driver limitation. */
  3478. if (timeout > wpa_s->max_remain_on_chan)
  3479. timeout = wpa_s->max_remain_on_chan;
  3480. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3481. return wpa_drv_p2p_listen(wpa_s, timeout);
  3482. return p2p_listen(wpa_s->global->p2p, timeout);
  3483. }
  3484. /**
  3485. * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
  3486. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3487. * @freq: Frequency of the channel in MHz
  3488. *
  3489. * This callback is called when the driver indicates that a remain-on-channel
  3490. * operation has been completed, i.e., the duration on the requested channel
  3491. * has timed out.
  3492. */
  3493. void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  3494. unsigned int freq)
  3495. {
  3496. wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
  3497. "(p2p_long_listen=%d ms pending_action_tx=%p)",
  3498. wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
  3499. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3500. return;
  3501. if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
  3502. return; /* P2P module started a new operation */
  3503. if (offchannel_pending_action_tx(wpa_s))
  3504. return;
  3505. if (wpa_s->p2p_long_listen > 0)
  3506. wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
  3507. if (wpa_s->p2p_long_listen > 0) {
  3508. wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
  3509. wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
  3510. } else {
  3511. /*
  3512. * When listen duration is over, stop listen & update p2p_state
  3513. * to IDLE.
  3514. */
  3515. p2p_stop_listen(wpa_s->global->p2p);
  3516. }
  3517. }
  3518. /**
  3519. * wpas_p2p_group_remove - Remove a P2P group
  3520. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3521. * @ifname: Network interface name of the group interface or "*" to remove all
  3522. * groups
  3523. * Returns: 0 on success, -1 on failure
  3524. *
  3525. * This function is used to remove a P2P group. This can be used to disconnect
  3526. * from a group in which the local end is a P2P Client or to end a P2P Group in
  3527. * case the local end is the Group Owner. If a virtual network interface was
  3528. * created for this group, that interface will be removed. Otherwise, only the
  3529. * configured P2P group network will be removed from the interface.
  3530. */
  3531. int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
  3532. {
  3533. struct wpa_global *global = wpa_s->global;
  3534. if (os_strcmp(ifname, "*") == 0) {
  3535. struct wpa_supplicant *prev;
  3536. wpa_s = global->ifaces;
  3537. while (wpa_s) {
  3538. prev = wpa_s;
  3539. wpa_s = wpa_s->next;
  3540. if (prev->p2p_group_interface !=
  3541. NOT_P2P_GROUP_INTERFACE ||
  3542. (prev->current_ssid &&
  3543. prev->current_ssid->p2p_group))
  3544. wpas_p2p_disconnect(prev);
  3545. }
  3546. return 0;
  3547. }
  3548. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3549. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  3550. break;
  3551. }
  3552. return wpas_p2p_disconnect(wpa_s);
  3553. }
  3554. static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
  3555. {
  3556. unsigned int r;
  3557. if (freq == 2) {
  3558. wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
  3559. "band");
  3560. if (wpa_s->best_24_freq > 0 &&
  3561. p2p_supported_freq(wpa_s->global->p2p,
  3562. wpa_s->best_24_freq)) {
  3563. freq = wpa_s->best_24_freq;
  3564. wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
  3565. "channel: %d MHz", freq);
  3566. } else {
  3567. os_get_random((u8 *) &r, sizeof(r));
  3568. freq = 2412 + (r % 3) * 25;
  3569. wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
  3570. "channel: %d MHz", freq);
  3571. }
  3572. }
  3573. if (freq == 5) {
  3574. wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
  3575. "band");
  3576. if (wpa_s->best_5_freq > 0 &&
  3577. p2p_supported_freq(wpa_s->global->p2p,
  3578. wpa_s->best_5_freq)) {
  3579. freq = wpa_s->best_5_freq;
  3580. wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
  3581. "channel: %d MHz", freq);
  3582. } else {
  3583. os_get_random((u8 *) &r, sizeof(r));
  3584. freq = 5180 + (r % 4) * 20;
  3585. if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
  3586. wpa_printf(MSG_DEBUG, "P2P: Could not select "
  3587. "5 GHz channel for P2P group");
  3588. return -1;
  3589. }
  3590. wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
  3591. "channel: %d MHz", freq);
  3592. }
  3593. }
  3594. if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
  3595. wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
  3596. "(%u MHz) is not supported for P2P uses",
  3597. freq);
  3598. return -1;
  3599. }
  3600. return freq;
  3601. }
  3602. static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
  3603. struct p2p_go_neg_results *params,
  3604. int freq, int ht40,
  3605. const struct p2p_channels *channels)
  3606. {
  3607. int res, *freqs;
  3608. unsigned int pref_freq;
  3609. unsigned int num, i;
  3610. os_memset(params, 0, sizeof(*params));
  3611. params->role_go = 1;
  3612. params->ht40 = ht40;
  3613. if (freq) {
  3614. if (!freq_included(channels, freq)) {
  3615. wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
  3616. "accepted", freq);
  3617. return -1;
  3618. }
  3619. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
  3620. "frequency %d MHz", freq);
  3621. params->freq = freq;
  3622. } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
  3623. wpa_s->conf->p2p_oper_channel >= 1 &&
  3624. wpa_s->conf->p2p_oper_channel <= 11 &&
  3625. freq_included(channels,
  3626. 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
  3627. params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
  3628. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
  3629. "frequency %d MHz", params->freq);
  3630. } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
  3631. wpa_s->conf->p2p_oper_reg_class == 116 ||
  3632. wpa_s->conf->p2p_oper_reg_class == 117 ||
  3633. wpa_s->conf->p2p_oper_reg_class == 124 ||
  3634. wpa_s->conf->p2p_oper_reg_class == 126 ||
  3635. wpa_s->conf->p2p_oper_reg_class == 127) &&
  3636. freq_included(channels,
  3637. 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
  3638. params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
  3639. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
  3640. "frequency %d MHz", params->freq);
  3641. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  3642. wpa_s->best_overall_freq > 0 &&
  3643. p2p_supported_freq(wpa_s->global->p2p,
  3644. wpa_s->best_overall_freq) &&
  3645. freq_included(channels, wpa_s->best_overall_freq)) {
  3646. params->freq = wpa_s->best_overall_freq;
  3647. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
  3648. "channel %d MHz", params->freq);
  3649. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  3650. wpa_s->best_24_freq > 0 &&
  3651. p2p_supported_freq(wpa_s->global->p2p,
  3652. wpa_s->best_24_freq) &&
  3653. freq_included(channels, wpa_s->best_24_freq)) {
  3654. params->freq = wpa_s->best_24_freq;
  3655. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
  3656. "channel %d MHz", params->freq);
  3657. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  3658. wpa_s->best_5_freq > 0 &&
  3659. p2p_supported_freq(wpa_s->global->p2p,
  3660. wpa_s->best_5_freq) &&
  3661. freq_included(channels, wpa_s->best_5_freq)) {
  3662. params->freq = wpa_s->best_5_freq;
  3663. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
  3664. "channel %d MHz", params->freq);
  3665. } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
  3666. channels))) {
  3667. params->freq = pref_freq;
  3668. wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
  3669. "channels", params->freq);
  3670. } else {
  3671. int chan;
  3672. for (chan = 0; chan < 11; chan++) {
  3673. params->freq = 2412 + chan * 5;
  3674. if (!wpas_p2p_disallowed_freq(wpa_s->global,
  3675. params->freq) &&
  3676. freq_included(channels, params->freq))
  3677. break;
  3678. }
  3679. if (chan == 11) {
  3680. wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
  3681. "allowed");
  3682. return -1;
  3683. }
  3684. wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
  3685. "known)", params->freq);
  3686. }
  3687. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  3688. if (!freqs)
  3689. return -1;
  3690. res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
  3691. wpa_s->num_multichan_concurrent);
  3692. if (res < 0) {
  3693. os_free(freqs);
  3694. return -1;
  3695. }
  3696. num = res;
  3697. if (!freq) {
  3698. for (i = 0; i < num; i++) {
  3699. if (freq_included(channels, freqs[i])) {
  3700. wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
  3701. freqs[i]);
  3702. params->freq = freqs[i];
  3703. break;
  3704. }
  3705. }
  3706. if (i == num) {
  3707. if (num == wpa_s->num_multichan_concurrent) {
  3708. wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
  3709. os_free(freqs);
  3710. return -1;
  3711. } else {
  3712. wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
  3713. }
  3714. }
  3715. } else {
  3716. for (i = 0; i < num; i++) {
  3717. if (freqs[i] == freq)
  3718. break;
  3719. }
  3720. if (i == num) {
  3721. if (num == wpa_s->num_multichan_concurrent) {
  3722. wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
  3723. os_free(freqs);
  3724. return -1;
  3725. } else {
  3726. wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
  3727. }
  3728. }
  3729. }
  3730. os_free(freqs);
  3731. return 0;
  3732. }
  3733. static struct wpa_supplicant *
  3734. wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
  3735. int go)
  3736. {
  3737. struct wpa_supplicant *group_wpa_s;
  3738. if (!wpas_p2p_create_iface(wpa_s)) {
  3739. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
  3740. "operations");
  3741. return wpa_s;
  3742. }
  3743. if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
  3744. WPA_IF_P2P_CLIENT) < 0) {
  3745. wpa_msg_global(wpa_s, MSG_ERROR,
  3746. "P2P: Failed to add group interface");
  3747. return NULL;
  3748. }
  3749. group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
  3750. if (group_wpa_s == NULL) {
  3751. wpa_msg_global(wpa_s, MSG_ERROR,
  3752. "P2P: Failed to initialize group interface");
  3753. wpas_p2p_remove_pending_group_interface(wpa_s);
  3754. return NULL;
  3755. }
  3756. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
  3757. group_wpa_s->ifname);
  3758. return group_wpa_s;
  3759. }
  3760. /**
  3761. * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
  3762. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3763. * @persistent_group: Whether to create a persistent group
  3764. * @freq: Frequency for the group or 0 to indicate no hardcoding
  3765. * Returns: 0 on success, -1 on failure
  3766. *
  3767. * This function creates a new P2P group with the local end as the Group Owner,
  3768. * i.e., without using Group Owner Negotiation.
  3769. */
  3770. int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
  3771. int freq, int ht40)
  3772. {
  3773. struct p2p_go_neg_results params;
  3774. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3775. return -1;
  3776. /* Make sure we are not running find during connection establishment */
  3777. wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
  3778. wpas_p2p_stop_find_oper(wpa_s);
  3779. freq = wpas_p2p_select_go_freq(wpa_s, freq);
  3780. if (freq < 0)
  3781. return -1;
  3782. if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, NULL))
  3783. return -1;
  3784. if (params.freq &&
  3785. !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
  3786. wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
  3787. "(%u MHz) is not supported for P2P uses",
  3788. params.freq);
  3789. return -1;
  3790. }
  3791. p2p_go_params(wpa_s->global->p2p, &params);
  3792. params.persistent_group = persistent_group;
  3793. wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
  3794. if (wpa_s == NULL)
  3795. return -1;
  3796. wpas_start_wps_go(wpa_s, &params, 0);
  3797. return 0;
  3798. }
  3799. static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
  3800. struct wpa_ssid *params, int addr_allocated)
  3801. {
  3802. struct wpa_ssid *ssid;
  3803. wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
  3804. if (wpa_s == NULL)
  3805. return -1;
  3806. wpa_supplicant_ap_deinit(wpa_s);
  3807. ssid = wpa_config_add_network(wpa_s->conf);
  3808. if (ssid == NULL)
  3809. return -1;
  3810. wpa_config_set_network_defaults(ssid);
  3811. ssid->temporary = 1;
  3812. ssid->proto = WPA_PROTO_RSN;
  3813. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  3814. ssid->group_cipher = WPA_CIPHER_CCMP;
  3815. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  3816. ssid->ssid = os_malloc(params->ssid_len);
  3817. if (ssid->ssid == NULL) {
  3818. wpa_config_remove_network(wpa_s->conf, ssid->id);
  3819. return -1;
  3820. }
  3821. os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
  3822. ssid->ssid_len = params->ssid_len;
  3823. ssid->p2p_group = 1;
  3824. ssid->export_keys = 1;
  3825. if (params->psk_set) {
  3826. os_memcpy(ssid->psk, params->psk, 32);
  3827. ssid->psk_set = 1;
  3828. }
  3829. if (params->passphrase)
  3830. ssid->passphrase = os_strdup(params->passphrase);
  3831. wpa_supplicant_select_network(wpa_s, ssid);
  3832. wpa_s->show_group_started = 1;
  3833. return 0;
  3834. }
  3835. int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
  3836. struct wpa_ssid *ssid, int addr_allocated,
  3837. int freq, int ht40,
  3838. const struct p2p_channels *channels)
  3839. {
  3840. struct p2p_go_neg_results params;
  3841. int go = 0;
  3842. if (ssid->disabled != 2 || ssid->ssid == NULL)
  3843. return -1;
  3844. if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
  3845. go == (ssid->mode == WPAS_MODE_P2P_GO)) {
  3846. wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
  3847. "already running");
  3848. return 0;
  3849. }
  3850. /* Make sure we are not running find during connection establishment */
  3851. wpas_p2p_stop_find_oper(wpa_s);
  3852. wpa_s->p2p_fallback_to_go_neg = 0;
  3853. if (ssid->mode == WPAS_MODE_INFRA)
  3854. return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
  3855. if (ssid->mode != WPAS_MODE_P2P_GO)
  3856. return -1;
  3857. freq = wpas_p2p_select_go_freq(wpa_s, freq);
  3858. if (freq < 0)
  3859. return -1;
  3860. if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, channels))
  3861. return -1;
  3862. params.role_go = 1;
  3863. params.psk_set = ssid->psk_set;
  3864. if (params.psk_set)
  3865. os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
  3866. if (ssid->passphrase) {
  3867. if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
  3868. wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
  3869. "persistent group");
  3870. return -1;
  3871. }
  3872. os_strlcpy(params.passphrase, ssid->passphrase,
  3873. sizeof(params.passphrase));
  3874. }
  3875. os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
  3876. params.ssid_len = ssid->ssid_len;
  3877. params.persistent_group = 1;
  3878. wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
  3879. if (wpa_s == NULL)
  3880. return -1;
  3881. wpas_start_wps_go(wpa_s, &params, 0);
  3882. return 0;
  3883. }
  3884. static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
  3885. struct wpabuf *proberesp_ies)
  3886. {
  3887. struct wpa_supplicant *wpa_s = ctx;
  3888. if (wpa_s->ap_iface) {
  3889. struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
  3890. if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
  3891. wpabuf_free(beacon_ies);
  3892. wpabuf_free(proberesp_ies);
  3893. return;
  3894. }
  3895. if (beacon_ies) {
  3896. wpabuf_free(hapd->p2p_beacon_ie);
  3897. hapd->p2p_beacon_ie = beacon_ies;
  3898. }
  3899. wpabuf_free(hapd->p2p_probe_resp_ie);
  3900. hapd->p2p_probe_resp_ie = proberesp_ies;
  3901. } else {
  3902. wpabuf_free(beacon_ies);
  3903. wpabuf_free(proberesp_ies);
  3904. }
  3905. wpa_supplicant_ap_update_beacon(wpa_s);
  3906. }
  3907. static void wpas_p2p_idle_update(void *ctx, int idle)
  3908. {
  3909. struct wpa_supplicant *wpa_s = ctx;
  3910. if (!wpa_s->ap_iface)
  3911. return;
  3912. wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
  3913. if (idle)
  3914. wpas_p2p_set_group_idle_timeout(wpa_s);
  3915. else
  3916. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  3917. }
  3918. struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
  3919. struct wpa_ssid *ssid)
  3920. {
  3921. struct p2p_group *group;
  3922. struct p2p_group_config *cfg;
  3923. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3924. return NULL;
  3925. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3926. return NULL;
  3927. cfg = os_zalloc(sizeof(*cfg));
  3928. if (cfg == NULL)
  3929. return NULL;
  3930. if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
  3931. cfg->persistent_group = 2;
  3932. else if (ssid->p2p_persistent_group)
  3933. cfg->persistent_group = 1;
  3934. os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
  3935. if (wpa_s->max_stations &&
  3936. wpa_s->max_stations < wpa_s->conf->max_num_sta)
  3937. cfg->max_clients = wpa_s->max_stations;
  3938. else
  3939. cfg->max_clients = wpa_s->conf->max_num_sta;
  3940. os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
  3941. cfg->ssid_len = ssid->ssid_len;
  3942. cfg->cb_ctx = wpa_s;
  3943. cfg->ie_update = wpas_p2p_ie_update;
  3944. cfg->idle_update = wpas_p2p_idle_update;
  3945. group = p2p_group_init(wpa_s->global->p2p, cfg);
  3946. if (group == NULL)
  3947. os_free(cfg);
  3948. if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
  3949. p2p_group_notif_formation_done(group);
  3950. wpa_s->p2p_group = group;
  3951. return group;
  3952. }
  3953. void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  3954. int registrar)
  3955. {
  3956. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3957. if (!wpa_s->p2p_in_provisioning) {
  3958. wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
  3959. "provisioning not in progress");
  3960. return;
  3961. }
  3962. if (ssid && ssid->mode == WPAS_MODE_INFRA) {
  3963. u8 go_dev_addr[ETH_ALEN];
  3964. os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
  3965. wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
  3966. ssid->ssid_len);
  3967. /* Clear any stored provisioning info */
  3968. p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
  3969. }
  3970. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
  3971. NULL);
  3972. if (ssid && ssid->mode == WPAS_MODE_INFRA) {
  3973. /*
  3974. * Use a separate timeout for initial data connection to
  3975. * complete to allow the group to be removed automatically if
  3976. * something goes wrong in this step before the P2P group idle
  3977. * timeout mechanism is taken into use.
  3978. */
  3979. eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
  3980. wpas_p2p_group_formation_timeout,
  3981. wpa_s->parent, NULL);
  3982. }
  3983. if (wpa_s->global->p2p)
  3984. p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
  3985. else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3986. wpa_drv_wps_success_cb(wpa_s, peer_addr);
  3987. wpas_group_formation_completed(wpa_s, 1);
  3988. }
  3989. void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
  3990. struct wps_event_fail *fail)
  3991. {
  3992. if (!wpa_s->p2p_in_provisioning) {
  3993. wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
  3994. "provisioning not in progress");
  3995. return;
  3996. }
  3997. if (wpa_s->go_params) {
  3998. p2p_clear_provisioning_info(
  3999. wpa_s->global->p2p,
  4000. wpa_s->go_params->peer_device_addr);
  4001. }
  4002. wpas_notify_p2p_wps_failed(wpa_s, fail);
  4003. }
  4004. int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  4005. const char *config_method,
  4006. enum wpas_p2p_prov_disc_use use)
  4007. {
  4008. u16 config_methods;
  4009. wpa_s->p2p_fallback_to_go_neg = 0;
  4010. wpa_s->pending_pd_use = NORMAL_PD;
  4011. if (os_strncmp(config_method, "display", 7) == 0)
  4012. config_methods = WPS_CONFIG_DISPLAY;
  4013. else if (os_strncmp(config_method, "keypad", 6) == 0)
  4014. config_methods = WPS_CONFIG_KEYPAD;
  4015. else if (os_strncmp(config_method, "pbc", 3) == 0 ||
  4016. os_strncmp(config_method, "pushbutton", 10) == 0)
  4017. config_methods = WPS_CONFIG_PUSHBUTTON;
  4018. else {
  4019. wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
  4020. return -1;
  4021. }
  4022. if (use == WPAS_P2P_PD_AUTO) {
  4023. os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
  4024. wpa_s->pending_pd_config_methods = config_methods;
  4025. wpa_s->p2p_auto_pd = 1;
  4026. wpa_s->p2p_auto_join = 0;
  4027. wpa_s->pending_pd_before_join = 0;
  4028. wpa_s->auto_pd_scan_retry = 0;
  4029. wpas_p2p_stop_find(wpa_s);
  4030. wpa_s->p2p_join_scan_count = 0;
  4031. os_get_time(&wpa_s->p2p_auto_started);
  4032. wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
  4033. wpa_s->p2p_auto_started.sec,
  4034. wpa_s->p2p_auto_started.usec);
  4035. wpas_p2p_join_scan(wpa_s, NULL);
  4036. return 0;
  4037. }
  4038. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  4039. return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
  4040. config_methods,
  4041. use == WPAS_P2P_PD_FOR_JOIN);
  4042. }
  4043. if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
  4044. return -1;
  4045. return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
  4046. config_methods, use == WPAS_P2P_PD_FOR_JOIN,
  4047. 0, 1);
  4048. }
  4049. int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
  4050. char *end)
  4051. {
  4052. return p2p_scan_result_text(ies, ies_len, buf, end);
  4053. }
  4054. static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
  4055. {
  4056. if (!offchannel_pending_action_tx(wpa_s))
  4057. return;
  4058. wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
  4059. "operation request");
  4060. offchannel_clear_pending_action_tx(wpa_s);
  4061. }
  4062. int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
  4063. enum p2p_discovery_type type,
  4064. unsigned int num_req_dev_types, const u8 *req_dev_types,
  4065. const u8 *dev_id, unsigned int search_delay)
  4066. {
  4067. wpas_p2p_clear_pending_action_tx(wpa_s);
  4068. wpa_s->p2p_long_listen = 0;
  4069. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4070. return wpa_drv_p2p_find(wpa_s, timeout, type);
  4071. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
  4072. wpa_s->p2p_in_provisioning)
  4073. return -1;
  4074. wpa_supplicant_cancel_sched_scan(wpa_s);
  4075. return p2p_find(wpa_s->global->p2p, timeout, type,
  4076. num_req_dev_types, req_dev_types, dev_id,
  4077. search_delay);
  4078. }
  4079. static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
  4080. {
  4081. wpas_p2p_clear_pending_action_tx(wpa_s);
  4082. wpa_s->p2p_long_listen = 0;
  4083. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  4084. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  4085. wpa_s->global->p2p_cb_on_scan_complete = 0;
  4086. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  4087. wpa_drv_p2p_stop_find(wpa_s);
  4088. return 1;
  4089. }
  4090. if (wpa_s->global->p2p)
  4091. p2p_stop_find(wpa_s->global->p2p);
  4092. return 0;
  4093. }
  4094. void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
  4095. {
  4096. if (wpas_p2p_stop_find_oper(wpa_s) > 0)
  4097. return;
  4098. wpas_p2p_remove_pending_group_interface(wpa_s);
  4099. }
  4100. static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
  4101. {
  4102. struct wpa_supplicant *wpa_s = eloop_ctx;
  4103. wpa_s->p2p_long_listen = 0;
  4104. }
  4105. int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
  4106. {
  4107. int res;
  4108. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4109. return -1;
  4110. wpa_supplicant_cancel_sched_scan(wpa_s);
  4111. wpas_p2p_clear_pending_action_tx(wpa_s);
  4112. if (timeout == 0) {
  4113. /*
  4114. * This is a request for unlimited Listen state. However, at
  4115. * least for now, this is mapped to a Listen state for one
  4116. * hour.
  4117. */
  4118. timeout = 3600;
  4119. }
  4120. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  4121. wpa_s->p2p_long_listen = 0;
  4122. /*
  4123. * Stop previous find/listen operation to avoid trying to request a new
  4124. * remain-on-channel operation while the driver is still running the
  4125. * previous one.
  4126. */
  4127. if (wpa_s->global->p2p)
  4128. p2p_stop_find(wpa_s->global->p2p);
  4129. res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
  4130. if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
  4131. wpa_s->p2p_long_listen = timeout * 1000;
  4132. eloop_register_timeout(timeout, 0,
  4133. wpas_p2p_long_listen_timeout,
  4134. wpa_s, NULL);
  4135. }
  4136. return res;
  4137. }
  4138. int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  4139. u8 *buf, size_t len, int p2p_group)
  4140. {
  4141. struct wpabuf *p2p_ie;
  4142. int ret;
  4143. if (wpa_s->global->p2p_disabled)
  4144. return -1;
  4145. if (wpa_s->global->p2p == NULL)
  4146. return -1;
  4147. if (bss == NULL)
  4148. return -1;
  4149. p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  4150. ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
  4151. p2p_group, p2p_ie);
  4152. wpabuf_free(p2p_ie);
  4153. return ret;
  4154. }
  4155. int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
  4156. const u8 *dst, const u8 *bssid,
  4157. const u8 *ie, size_t ie_len, int ssi_signal)
  4158. {
  4159. if (wpa_s->global->p2p_disabled)
  4160. return 0;
  4161. if (wpa_s->global->p2p == NULL)
  4162. return 0;
  4163. switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
  4164. ie, ie_len)) {
  4165. case P2P_PREQ_NOT_P2P:
  4166. wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
  4167. ssi_signal);
  4168. /* fall through */
  4169. case P2P_PREQ_MALFORMED:
  4170. case P2P_PREQ_NOT_LISTEN:
  4171. case P2P_PREQ_NOT_PROCESSED:
  4172. default: /* make gcc happy */
  4173. return 0;
  4174. case P2P_PREQ_PROCESSED:
  4175. return 1;
  4176. }
  4177. }
  4178. void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
  4179. const u8 *sa, const u8 *bssid,
  4180. u8 category, const u8 *data, size_t len, int freq)
  4181. {
  4182. if (wpa_s->global->p2p_disabled)
  4183. return;
  4184. if (wpa_s->global->p2p == NULL)
  4185. return;
  4186. p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
  4187. freq);
  4188. }
  4189. void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
  4190. {
  4191. if (wpa_s->global->p2p_disabled)
  4192. return;
  4193. if (wpa_s->global->p2p == NULL)
  4194. return;
  4195. p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
  4196. }
  4197. void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
  4198. {
  4199. p2p_group_deinit(wpa_s->p2p_group);
  4200. wpa_s->p2p_group = NULL;
  4201. wpa_s->ap_configured_cb = NULL;
  4202. wpa_s->ap_configured_cb_ctx = NULL;
  4203. wpa_s->ap_configured_cb_data = NULL;
  4204. wpa_s->connect_without_scan = NULL;
  4205. }
  4206. int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
  4207. {
  4208. wpa_s->p2p_long_listen = 0;
  4209. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4210. return wpa_drv_p2p_reject(wpa_s, addr);
  4211. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4212. return -1;
  4213. return p2p_reject(wpa_s->global->p2p, addr);
  4214. }
  4215. /* Invite to reinvoke a persistent group */
  4216. int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  4217. struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
  4218. int ht40, int pref_freq)
  4219. {
  4220. enum p2p_invite_role role;
  4221. u8 *bssid = NULL;
  4222. int force_freq = 0;
  4223. int res;
  4224. wpa_s->global->p2p_invite_group = NULL;
  4225. if (peer_addr)
  4226. os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
  4227. else
  4228. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  4229. wpa_s->p2p_persistent_go_freq = freq;
  4230. wpa_s->p2p_go_ht40 = !!ht40;
  4231. if (ssid->mode == WPAS_MODE_P2P_GO) {
  4232. role = P2P_INVITE_ROLE_GO;
  4233. if (peer_addr == NULL) {
  4234. wpa_printf(MSG_DEBUG, "P2P: Missing peer "
  4235. "address in invitation command");
  4236. return -1;
  4237. }
  4238. if (wpas_p2p_create_iface(wpa_s)) {
  4239. if (wpas_p2p_add_group_interface(wpa_s,
  4240. WPA_IF_P2P_GO) < 0) {
  4241. wpa_printf(MSG_ERROR, "P2P: Failed to "
  4242. "allocate a new interface for the "
  4243. "group");
  4244. return -1;
  4245. }
  4246. bssid = wpa_s->pending_interface_addr;
  4247. } else
  4248. bssid = wpa_s->own_addr;
  4249. } else {
  4250. role = P2P_INVITE_ROLE_CLIENT;
  4251. peer_addr = ssid->bssid;
  4252. }
  4253. wpa_s->pending_invite_ssid_id = ssid->id;
  4254. res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq);
  4255. if (res)
  4256. return res;
  4257. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4258. return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
  4259. ssid->ssid, ssid->ssid_len,
  4260. go_dev_addr, 1);
  4261. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4262. return -1;
  4263. return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
  4264. ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
  4265. 1, pref_freq);
  4266. }
  4267. /* Invite to join an active group */
  4268. int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
  4269. const u8 *peer_addr, const u8 *go_dev_addr)
  4270. {
  4271. struct wpa_global *global = wpa_s->global;
  4272. enum p2p_invite_role role;
  4273. u8 *bssid = NULL;
  4274. struct wpa_ssid *ssid;
  4275. int persistent;
  4276. int force_freq = 0, pref_freq = 0;
  4277. int res;
  4278. wpa_s->p2p_persistent_go_freq = 0;
  4279. wpa_s->p2p_go_ht40 = 0;
  4280. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  4281. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  4282. break;
  4283. }
  4284. if (wpa_s == NULL) {
  4285. wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
  4286. return -1;
  4287. }
  4288. ssid = wpa_s->current_ssid;
  4289. if (ssid == NULL) {
  4290. wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
  4291. "invitation");
  4292. return -1;
  4293. }
  4294. wpa_s->global->p2p_invite_group = wpa_s;
  4295. persistent = ssid->p2p_persistent_group &&
  4296. wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
  4297. ssid->ssid, ssid->ssid_len);
  4298. if (ssid->mode == WPAS_MODE_P2P_GO) {
  4299. role = P2P_INVITE_ROLE_ACTIVE_GO;
  4300. bssid = wpa_s->own_addr;
  4301. if (go_dev_addr == NULL)
  4302. go_dev_addr = wpa_s->global->p2p_dev_addr;
  4303. } else {
  4304. role = P2P_INVITE_ROLE_CLIENT;
  4305. if (wpa_s->wpa_state < WPA_ASSOCIATED) {
  4306. wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
  4307. "invite to current group");
  4308. return -1;
  4309. }
  4310. bssid = wpa_s->bssid;
  4311. if (go_dev_addr == NULL &&
  4312. !is_zero_ether_addr(wpa_s->go_dev_addr))
  4313. go_dev_addr = wpa_s->go_dev_addr;
  4314. }
  4315. wpa_s->parent->pending_invite_ssid_id = -1;
  4316. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4317. return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
  4318. ssid->ssid, ssid->ssid_len,
  4319. go_dev_addr, persistent);
  4320. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4321. return -1;
  4322. res = wpas_p2p_setup_freqs(wpa_s, 0, &force_freq, &pref_freq);
  4323. if (res)
  4324. return res;
  4325. wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
  4326. return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
  4327. ssid->ssid, ssid->ssid_len, force_freq,
  4328. go_dev_addr, persistent, pref_freq);
  4329. }
  4330. void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
  4331. {
  4332. struct wpa_ssid *ssid = wpa_s->current_ssid;
  4333. const char *ssid_txt;
  4334. u8 go_dev_addr[ETH_ALEN];
  4335. int network_id = -1;
  4336. int persistent;
  4337. int freq;
  4338. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
  4339. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  4340. wpa_s->parent, NULL);
  4341. }
  4342. if (!wpa_s->show_group_started || !ssid)
  4343. goto done;
  4344. wpa_s->show_group_started = 0;
  4345. ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
  4346. os_memset(go_dev_addr, 0, ETH_ALEN);
  4347. if (ssid->bssid_set)
  4348. os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
  4349. persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
  4350. ssid->ssid_len);
  4351. os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
  4352. if (wpa_s->global->p2p_group_formation == wpa_s)
  4353. wpa_s->global->p2p_group_formation = NULL;
  4354. freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
  4355. (int) wpa_s->assoc_freq;
  4356. if (ssid->passphrase == NULL && ssid->psk_set) {
  4357. char psk[65];
  4358. wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
  4359. wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  4360. "%s client ssid=\"%s\" freq=%d psk=%s "
  4361. "go_dev_addr=" MACSTR "%s",
  4362. wpa_s->ifname, ssid_txt, freq, psk,
  4363. MAC2STR(go_dev_addr),
  4364. persistent ? " [PERSISTENT]" : "");
  4365. } else {
  4366. wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  4367. "%s client ssid=\"%s\" freq=%d "
  4368. "passphrase=\"%s\" go_dev_addr=" MACSTR "%s",
  4369. wpa_s->ifname, ssid_txt, freq,
  4370. ssid->passphrase ? ssid->passphrase : "",
  4371. MAC2STR(go_dev_addr),
  4372. persistent ? " [PERSISTENT]" : "");
  4373. }
  4374. if (persistent)
  4375. network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
  4376. ssid, go_dev_addr);
  4377. if (network_id < 0)
  4378. network_id = ssid->id;
  4379. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
  4380. done:
  4381. wpas_p2p_continue_after_scan(wpa_s);
  4382. }
  4383. int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
  4384. u32 interval1, u32 duration2, u32 interval2)
  4385. {
  4386. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4387. return -1;
  4388. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4389. return -1;
  4390. if (wpa_s->wpa_state < WPA_ASSOCIATED ||
  4391. wpa_s->current_ssid == NULL ||
  4392. wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
  4393. return -1;
  4394. return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
  4395. wpa_s->own_addr, wpa_s->assoc_freq,
  4396. duration1, interval1, duration2, interval2);
  4397. }
  4398. int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
  4399. unsigned int interval)
  4400. {
  4401. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4402. return -1;
  4403. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4404. return -1;
  4405. return p2p_ext_listen(wpa_s->global->p2p, period, interval);
  4406. }
  4407. static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
  4408. {
  4409. if (wpa_s->current_ssid == NULL) {
  4410. /*
  4411. * current_ssid can be cleared when P2P client interface gets
  4412. * disconnected, so assume this interface was used as P2P
  4413. * client.
  4414. */
  4415. return 1;
  4416. }
  4417. return wpa_s->current_ssid->p2p_group &&
  4418. wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
  4419. }
  4420. static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
  4421. {
  4422. struct wpa_supplicant *wpa_s = eloop_ctx;
  4423. if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
  4424. wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
  4425. "disabled");
  4426. return;
  4427. }
  4428. wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
  4429. "group");
  4430. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
  4431. }
  4432. static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
  4433. {
  4434. int timeout;
  4435. if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
  4436. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
  4437. if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
  4438. return;
  4439. timeout = wpa_s->conf->p2p_group_idle;
  4440. if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
  4441. (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
  4442. timeout = P2P_MAX_CLIENT_IDLE;
  4443. if (timeout == 0)
  4444. return;
  4445. if (timeout < 0) {
  4446. if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
  4447. timeout = 0; /* special client mode no-timeout */
  4448. else
  4449. return;
  4450. }
  4451. if (wpa_s->p2p_in_provisioning) {
  4452. /*
  4453. * Use the normal group formation timeout during the
  4454. * provisioning phase to avoid terminating this process too
  4455. * early due to group idle timeout.
  4456. */
  4457. wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
  4458. "during provisioning");
  4459. return;
  4460. }
  4461. if (wpa_s->show_group_started) {
  4462. /*
  4463. * Use the normal group formation timeout between the end of
  4464. * the provisioning phase and completion of 4-way handshake to
  4465. * avoid terminating this process too early due to group idle
  4466. * timeout.
  4467. */
  4468. wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
  4469. "while waiting for initial 4-way handshake to "
  4470. "complete");
  4471. return;
  4472. }
  4473. wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
  4474. timeout);
  4475. eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
  4476. wpa_s, NULL);
  4477. }
  4478. /* Returns 1 if the interface was removed */
  4479. int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
  4480. u16 reason_code, const u8 *ie, size_t ie_len,
  4481. int locally_generated)
  4482. {
  4483. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4484. return 0;
  4485. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4486. return 0;
  4487. if (!locally_generated)
  4488. p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
  4489. ie_len);
  4490. if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
  4491. wpa_s->current_ssid &&
  4492. wpa_s->current_ssid->p2p_group &&
  4493. wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
  4494. wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
  4495. "session is ending");
  4496. if (wpas_p2p_group_delete(wpa_s,
  4497. P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
  4498. > 0)
  4499. return 1;
  4500. }
  4501. return 0;
  4502. }
  4503. void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
  4504. u16 reason_code, const u8 *ie, size_t ie_len,
  4505. int locally_generated)
  4506. {
  4507. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4508. return;
  4509. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4510. return;
  4511. if (!locally_generated)
  4512. p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
  4513. ie_len);
  4514. }
  4515. void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
  4516. {
  4517. struct p2p_data *p2p = wpa_s->global->p2p;
  4518. if (p2p == NULL)
  4519. return;
  4520. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
  4521. return;
  4522. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
  4523. p2p_set_dev_name(p2p, wpa_s->conf->device_name);
  4524. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
  4525. p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
  4526. if (wpa_s->wps &&
  4527. (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
  4528. p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
  4529. if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
  4530. p2p_set_uuid(p2p, wpa_s->wps->uuid);
  4531. if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
  4532. p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
  4533. p2p_set_model_name(p2p, wpa_s->conf->model_name);
  4534. p2p_set_model_number(p2p, wpa_s->conf->model_number);
  4535. p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
  4536. }
  4537. if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
  4538. p2p_set_sec_dev_types(p2p,
  4539. (void *) wpa_s->conf->sec_device_type,
  4540. wpa_s->conf->num_sec_device_types);
  4541. if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
  4542. int i;
  4543. p2p_remove_wps_vendor_extensions(p2p);
  4544. for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
  4545. if (wpa_s->conf->wps_vendor_ext[i] == NULL)
  4546. continue;
  4547. p2p_add_wps_vendor_extension(
  4548. p2p, wpa_s->conf->wps_vendor_ext[i]);
  4549. }
  4550. }
  4551. if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
  4552. wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  4553. char country[3];
  4554. country[0] = wpa_s->conf->country[0];
  4555. country[1] = wpa_s->conf->country[1];
  4556. country[2] = 0x04;
  4557. p2p_set_country(p2p, country);
  4558. }
  4559. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
  4560. p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
  4561. wpa_s->conf->p2p_ssid_postfix ?
  4562. os_strlen(wpa_s->conf->p2p_ssid_postfix) :
  4563. 0);
  4564. }
  4565. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
  4566. p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
  4567. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
  4568. u8 reg_class, channel;
  4569. int ret;
  4570. unsigned int r;
  4571. if (wpa_s->conf->p2p_listen_reg_class &&
  4572. wpa_s->conf->p2p_listen_channel) {
  4573. reg_class = wpa_s->conf->p2p_listen_reg_class;
  4574. channel = wpa_s->conf->p2p_listen_channel;
  4575. } else {
  4576. reg_class = 81;
  4577. /*
  4578. * Pick one of the social channels randomly as the
  4579. * listen channel.
  4580. */
  4581. os_get_random((u8 *) &r, sizeof(r));
  4582. channel = 1 + (r % 3) * 5;
  4583. }
  4584. ret = p2p_set_listen_channel(p2p, reg_class, channel);
  4585. if (ret)
  4586. wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
  4587. "failed: %d", ret);
  4588. }
  4589. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
  4590. u8 op_reg_class, op_channel, cfg_op_channel;
  4591. int ret = 0;
  4592. unsigned int r;
  4593. if (wpa_s->conf->p2p_oper_reg_class &&
  4594. wpa_s->conf->p2p_oper_channel) {
  4595. op_reg_class = wpa_s->conf->p2p_oper_reg_class;
  4596. op_channel = wpa_s->conf->p2p_oper_channel;
  4597. cfg_op_channel = 1;
  4598. } else {
  4599. op_reg_class = 81;
  4600. /*
  4601. * Use random operation channel from (1, 6, 11)
  4602. *if no other preference is indicated.
  4603. */
  4604. os_get_random((u8 *) &r, sizeof(r));
  4605. op_channel = 1 + (r % 3) * 5;
  4606. cfg_op_channel = 0;
  4607. }
  4608. ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
  4609. cfg_op_channel);
  4610. if (ret)
  4611. wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
  4612. "failed: %d", ret);
  4613. }
  4614. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
  4615. if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
  4616. wpa_s->conf->p2p_pref_chan) < 0) {
  4617. wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
  4618. "update failed");
  4619. }
  4620. }
  4621. }
  4622. int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
  4623. int duration)
  4624. {
  4625. if (!wpa_s->ap_iface)
  4626. return -1;
  4627. return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
  4628. duration);
  4629. }
  4630. int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
  4631. {
  4632. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4633. return -1;
  4634. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4635. return -1;
  4636. wpa_s->global->cross_connection = enabled;
  4637. p2p_set_cross_connect(wpa_s->global->p2p, enabled);
  4638. if (!enabled) {
  4639. struct wpa_supplicant *iface;
  4640. for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
  4641. {
  4642. if (iface->cross_connect_enabled == 0)
  4643. continue;
  4644. iface->cross_connect_enabled = 0;
  4645. iface->cross_connect_in_use = 0;
  4646. wpa_msg_global(iface->parent, MSG_INFO,
  4647. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  4648. iface->ifname,
  4649. iface->cross_connect_uplink);
  4650. }
  4651. }
  4652. return 0;
  4653. }
  4654. static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
  4655. {
  4656. struct wpa_supplicant *iface;
  4657. if (!uplink->global->cross_connection)
  4658. return;
  4659. for (iface = uplink->global->ifaces; iface; iface = iface->next) {
  4660. if (!iface->cross_connect_enabled)
  4661. continue;
  4662. if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
  4663. 0)
  4664. continue;
  4665. if (iface->ap_iface == NULL)
  4666. continue;
  4667. if (iface->cross_connect_in_use)
  4668. continue;
  4669. iface->cross_connect_in_use = 1;
  4670. wpa_msg_global(iface->parent, MSG_INFO,
  4671. P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
  4672. iface->ifname, iface->cross_connect_uplink);
  4673. }
  4674. }
  4675. static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
  4676. {
  4677. struct wpa_supplicant *iface;
  4678. for (iface = uplink->global->ifaces; iface; iface = iface->next) {
  4679. if (!iface->cross_connect_enabled)
  4680. continue;
  4681. if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
  4682. 0)
  4683. continue;
  4684. if (!iface->cross_connect_in_use)
  4685. continue;
  4686. wpa_msg_global(iface->parent, MSG_INFO,
  4687. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  4688. iface->ifname, iface->cross_connect_uplink);
  4689. iface->cross_connect_in_use = 0;
  4690. }
  4691. }
  4692. void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
  4693. {
  4694. if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
  4695. wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
  4696. wpa_s->cross_connect_disallowed)
  4697. wpas_p2p_disable_cross_connect(wpa_s);
  4698. else
  4699. wpas_p2p_enable_cross_connect(wpa_s);
  4700. if (!wpa_s->ap_iface &&
  4701. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
  4702. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
  4703. }
  4704. void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
  4705. {
  4706. wpas_p2p_disable_cross_connect(wpa_s);
  4707. if (!wpa_s->ap_iface &&
  4708. !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
  4709. wpa_s, NULL))
  4710. wpas_p2p_set_group_idle_timeout(wpa_s);
  4711. }
  4712. static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
  4713. {
  4714. struct wpa_supplicant *iface;
  4715. if (!wpa_s->global->cross_connection)
  4716. return;
  4717. for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
  4718. if (iface == wpa_s)
  4719. continue;
  4720. if (iface->drv_flags &
  4721. WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
  4722. continue;
  4723. if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
  4724. continue;
  4725. wpa_s->cross_connect_enabled = 1;
  4726. os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
  4727. sizeof(wpa_s->cross_connect_uplink));
  4728. wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
  4729. "%s to %s whenever uplink is available",
  4730. wpa_s->ifname, wpa_s->cross_connect_uplink);
  4731. if (iface->ap_iface || iface->current_ssid == NULL ||
  4732. iface->current_ssid->mode != WPAS_MODE_INFRA ||
  4733. iface->cross_connect_disallowed ||
  4734. iface->wpa_state != WPA_COMPLETED)
  4735. break;
  4736. wpa_s->cross_connect_in_use = 1;
  4737. wpa_msg_global(wpa_s->parent, MSG_INFO,
  4738. P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
  4739. wpa_s->ifname, wpa_s->cross_connect_uplink);
  4740. break;
  4741. }
  4742. }
  4743. int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
  4744. {
  4745. if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
  4746. !wpa_s->p2p_in_provisioning)
  4747. return 0; /* not P2P client operation */
  4748. wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
  4749. "session overlap");
  4750. if (wpa_s != wpa_s->parent)
  4751. wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
  4752. wpas_p2p_group_formation_failed(wpa_s);
  4753. return 1;
  4754. }
  4755. void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
  4756. {
  4757. struct p2p_channels chan;
  4758. if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
  4759. return;
  4760. os_memset(&chan, 0, sizeof(chan));
  4761. if (wpas_p2p_setup_channels(wpa_s, &chan)) {
  4762. wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
  4763. "channel list");
  4764. return;
  4765. }
  4766. p2p_update_channel_list(wpa_s->global->p2p, &chan);
  4767. }
  4768. static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
  4769. struct wpa_scan_results *scan_res)
  4770. {
  4771. wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
  4772. }
  4773. int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
  4774. {
  4775. struct wpa_global *global = wpa_s->global;
  4776. int found = 0;
  4777. const u8 *peer;
  4778. if (global->p2p == NULL)
  4779. return -1;
  4780. wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
  4781. if (wpa_s->pending_interface_name[0] &&
  4782. !is_zero_ether_addr(wpa_s->pending_interface_addr))
  4783. found = 1;
  4784. peer = p2p_get_go_neg_peer(global->p2p);
  4785. if (peer) {
  4786. wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
  4787. MACSTR, MAC2STR(peer));
  4788. p2p_unauthorize(global->p2p, peer);
  4789. found = 1;
  4790. }
  4791. if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
  4792. wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
  4793. wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
  4794. found = 1;
  4795. }
  4796. if (wpa_s->pending_pd_before_join) {
  4797. wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
  4798. wpa_s->pending_pd_before_join = 0;
  4799. found = 1;
  4800. }
  4801. wpas_p2p_stop_find(wpa_s);
  4802. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  4803. if (wpa_s == global->p2p_group_formation &&
  4804. (wpa_s->p2p_in_provisioning ||
  4805. wpa_s->parent->pending_interface_type ==
  4806. WPA_IF_P2P_CLIENT)) {
  4807. wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
  4808. "formation found - cancelling",
  4809. wpa_s->ifname);
  4810. found = 1;
  4811. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  4812. wpa_s->parent, NULL);
  4813. if (wpa_s->p2p_in_provisioning) {
  4814. wpas_group_formation_completed(wpa_s, 0);
  4815. break;
  4816. }
  4817. wpas_p2p_group_delete(wpa_s,
  4818. P2P_GROUP_REMOVAL_REQUESTED);
  4819. break;
  4820. }
  4821. }
  4822. if (!found) {
  4823. wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
  4824. return -1;
  4825. }
  4826. return 0;
  4827. }
  4828. void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
  4829. {
  4830. if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
  4831. return;
  4832. wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
  4833. "being available anymore");
  4834. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
  4835. }
  4836. void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
  4837. int freq_24, int freq_5, int freq_overall)
  4838. {
  4839. struct p2p_data *p2p = wpa_s->global->p2p;
  4840. if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
  4841. return;
  4842. p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
  4843. }
  4844. int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
  4845. {
  4846. u8 peer[ETH_ALEN];
  4847. struct p2p_data *p2p = wpa_s->global->p2p;
  4848. if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
  4849. return -1;
  4850. if (hwaddr_aton(addr, peer))
  4851. return -1;
  4852. return p2p_unauthorize(p2p, peer);
  4853. }
  4854. /**
  4855. * wpas_p2p_disconnect - Disconnect from a P2P Group
  4856. * @wpa_s: Pointer to wpa_supplicant data
  4857. * Returns: 0 on success, -1 on failure
  4858. *
  4859. * This can be used to disconnect from a group in which the local end is a P2P
  4860. * Client or to end a P2P Group in case the local end is the Group Owner. If a
  4861. * virtual network interface was created for this group, that interface will be
  4862. * removed. Otherwise, only the configured P2P group network will be removed
  4863. * from the interface.
  4864. */
  4865. int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
  4866. {
  4867. if (wpa_s == NULL)
  4868. return -1;
  4869. return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
  4870. -1 : 0;
  4871. }
  4872. int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
  4873. {
  4874. int ret;
  4875. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4876. return 0;
  4877. ret = p2p_in_progress(wpa_s->global->p2p);
  4878. if (ret == 0) {
  4879. /*
  4880. * Check whether there is an ongoing WPS provisioning step (or
  4881. * other parts of group formation) on another interface since
  4882. * p2p_in_progress() does not report this to avoid issues for
  4883. * scans during such provisioning step.
  4884. */
  4885. if (wpa_s->global->p2p_group_formation &&
  4886. wpa_s->global->p2p_group_formation != wpa_s) {
  4887. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
  4888. "in group formation",
  4889. wpa_s->global->p2p_group_formation->ifname);
  4890. ret = 1;
  4891. }
  4892. }
  4893. if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
  4894. struct os_time now;
  4895. os_get_time(&now);
  4896. if (now.sec > wpa_s->global->p2p_go_wait_client.sec +
  4897. P2P_MAX_INITIAL_CONN_WAIT_GO) {
  4898. /* Wait for the first client has expired */
  4899. wpa_s->global->p2p_go_wait_client.sec = 0;
  4900. } else {
  4901. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
  4902. ret = 1;
  4903. }
  4904. }
  4905. return ret;
  4906. }
  4907. void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
  4908. struct wpa_ssid *ssid)
  4909. {
  4910. if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
  4911. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  4912. wpa_s->parent, NULL) > 0) {
  4913. /**
  4914. * Remove the network by scheduling the group formation
  4915. * timeout to happen immediately. The teardown code
  4916. * needs to be scheduled to run asynch later so that we
  4917. * don't delete data from under ourselves unexpectedly.
  4918. * Calling wpas_p2p_group_formation_timeout directly
  4919. * causes a series of crashes in WPS failure scenarios.
  4920. */
  4921. wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
  4922. "P2P group network getting removed");
  4923. eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
  4924. wpa_s->parent, NULL);
  4925. }
  4926. }
  4927. struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
  4928. const u8 *addr, const u8 *ssid,
  4929. size_t ssid_len)
  4930. {
  4931. struct wpa_ssid *s;
  4932. size_t i;
  4933. for (s = wpa_s->conf->ssid; s; s = s->next) {
  4934. if (s->disabled != 2)
  4935. continue;
  4936. if (ssid &&
  4937. (ssid_len != s->ssid_len ||
  4938. os_memcmp(ssid, s->ssid, ssid_len) != 0))
  4939. continue;
  4940. if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
  4941. return s; /* peer is GO in the persistent group */
  4942. if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
  4943. continue;
  4944. for (i = 0; i < s->num_p2p_clients; i++) {
  4945. if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
  4946. addr, ETH_ALEN) == 0)
  4947. return s; /* peer is P2P client in persistent
  4948. * group */
  4949. }
  4950. }
  4951. return NULL;
  4952. }
  4953. void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
  4954. const u8 *addr)
  4955. {
  4956. wpa_s->global->p2p_go_wait_client.sec = 0;
  4957. if (addr == NULL)
  4958. return;
  4959. wpas_p2p_add_persistent_group_client(wpa_s, addr);
  4960. }
  4961. static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
  4962. int group_added)
  4963. {
  4964. struct wpa_supplicant *group = wpa_s;
  4965. if (wpa_s->global->p2p_group_formation)
  4966. group = wpa_s->global->p2p_group_formation;
  4967. wpa_s = wpa_s->parent;
  4968. offchannel_send_action_done(wpa_s);
  4969. if (group_added)
  4970. wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
  4971. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
  4972. wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
  4973. wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
  4974. 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
  4975. wpa_s->p2p_persistent_id,
  4976. wpa_s->p2p_pd_before_go_neg,
  4977. wpa_s->p2p_go_ht40);
  4978. }
  4979. int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
  4980. {
  4981. if (!wpa_s->p2p_fallback_to_go_neg ||
  4982. wpa_s->p2p_in_provisioning <= 5)
  4983. return 0;
  4984. if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
  4985. return 0; /* peer operating as a GO */
  4986. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
  4987. "fallback to GO Negotiation");
  4988. wpas_p2p_fallback_to_go_neg(wpa_s, 1);
  4989. return 1;
  4990. }
  4991. unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
  4992. {
  4993. const char *rn, *rn2;
  4994. struct wpa_supplicant *ifs;
  4995. if (wpa_s->wpa_state > WPA_SCANNING) {
  4996. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
  4997. "concurrent operation",
  4998. P2P_CONCURRENT_SEARCH_DELAY);
  4999. return P2P_CONCURRENT_SEARCH_DELAY;
  5000. }
  5001. if (!wpa_s->driver->get_radio_name)
  5002. return 0;
  5003. rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
  5004. if (rn == NULL || rn[0] == '\0')
  5005. return 0;
  5006. for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
  5007. if (ifs == wpa_s || !ifs->driver->get_radio_name)
  5008. continue;
  5009. rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
  5010. if (!rn2 || os_strcmp(rn, rn2) != 0)
  5011. continue;
  5012. if (ifs->wpa_state > WPA_SCANNING) {
  5013. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
  5014. "delay due to concurrent operation on "
  5015. "interface %s",
  5016. P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
  5017. return P2P_CONCURRENT_SEARCH_DELAY;
  5018. }
  5019. }
  5020. return 0;
  5021. }
  5022. void wpas_p2p_continue_after_scan(struct wpa_supplicant *wpa_s)
  5023. {
  5024. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Station mode scan operation not "
  5025. "pending anymore (sta_scan_pending=%d "
  5026. "p2p_cb_on_scan_complete=%d)", wpa_s->sta_scan_pending,
  5027. wpa_s->global->p2p_cb_on_scan_complete);
  5028. wpa_s->sta_scan_pending = 0;
  5029. if (!wpa_s->global->p2p_cb_on_scan_complete)
  5030. return;
  5031. wpa_s->global->p2p_cb_on_scan_complete = 0;
  5032. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5033. return;
  5034. if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
  5035. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
  5036. "continued after successful connection");
  5037. p2p_increase_search_delay(wpa_s->global->p2p,
  5038. wpas_p2p_search_delay(wpa_s));
  5039. }
  5040. }