p2p_supplicant.c 156 KB

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