p2p_supplicant.c 105 KB

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