p2p_supplicant.c 144 KB

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