p2p_supplicant.c 159 KB

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