p2p_supplicant.c 135 KB

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