p2p_supplicant.c 101 KB

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