p2p_supplicant.c 155 KB

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