p2p_supplicant.c 94 KB

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