p2p_supplicant.c 84 KB

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