p2p_supplicant.c 144 KB

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