p2p_supplicant.c 85 KB

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