p2p_supplicant.c 114 KB

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