p2p_supplicant.c 163 KB

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