dbus_new_handlers.c 115 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234
  1. /*
  2. * WPA Supplicant / dbus-based control interface
  3. * Copyright (c) 2006, Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
  4. * Copyright (c) 2009-2010, Witold Sowa <witold.sowa@gmail.com>
  5. * Copyright (c) 2009-2015, Jouni Malinen <j@w1.fi>
  6. *
  7. * This software may be distributed under the terms of the BSD license.
  8. * See README for more details.
  9. */
  10. #include "includes.h"
  11. #include "common.h"
  12. #include "common/ieee802_11_defs.h"
  13. #include "eap_peer/eap_methods.h"
  14. #include "eapol_supp/eapol_supp_sm.h"
  15. #include "rsn_supp/wpa.h"
  16. #include "../config.h"
  17. #include "../wpa_supplicant_i.h"
  18. #include "../driver_i.h"
  19. #include "../notify.h"
  20. #include "../bss.h"
  21. #include "../scan.h"
  22. #include "../autoscan.h"
  23. #include "dbus_new_helpers.h"
  24. #include "dbus_new.h"
  25. #include "dbus_new_handlers.h"
  26. #include "dbus_dict_helpers.h"
  27. #include "dbus_common_i.h"
  28. #include "drivers/driver.h"
  29. static const char * const debug_strings[] = {
  30. "excessive", "msgdump", "debug", "info", "warning", "error", NULL
  31. };
  32. /**
  33. * wpas_dbus_error_unknown_error - Return a new UnknownError error message
  34. * @message: Pointer to incoming dbus message this error refers to
  35. * @arg: Optional string appended to error message
  36. * Returns: a dbus error message
  37. *
  38. * Convenience function to create and return an UnknownError
  39. */
  40. DBusMessage * wpas_dbus_error_unknown_error(DBusMessage *message,
  41. const char *arg)
  42. {
  43. return dbus_message_new_error(message, WPAS_DBUS_ERROR_UNKNOWN_ERROR,
  44. arg);
  45. }
  46. /**
  47. * wpas_dbus_error_iface_unknown - Return a new invalid interface error message
  48. * @message: Pointer to incoming dbus message this error refers to
  49. * Returns: A dbus error message
  50. *
  51. * Convenience function to create and return an invalid interface error
  52. */
  53. static DBusMessage * wpas_dbus_error_iface_unknown(DBusMessage *message)
  54. {
  55. return dbus_message_new_error(
  56. message, WPAS_DBUS_ERROR_IFACE_UNKNOWN,
  57. "wpa_supplicant knows nothing about this interface.");
  58. }
  59. /**
  60. * wpas_dbus_error_network_unknown - Return a new NetworkUnknown error message
  61. * @message: Pointer to incoming dbus message this error refers to
  62. * Returns: a dbus error message
  63. *
  64. * Convenience function to create and return an invalid network error
  65. */
  66. static DBusMessage * wpas_dbus_error_network_unknown(DBusMessage *message)
  67. {
  68. return dbus_message_new_error(
  69. message, WPAS_DBUS_ERROR_NETWORK_UNKNOWN,
  70. "There is no such a network in this interface.");
  71. }
  72. /**
  73. * wpas_dbus_error_invalid_args - Return a new InvalidArgs error message
  74. * @message: Pointer to incoming dbus message this error refers to
  75. * Returns: a dbus error message
  76. *
  77. * Convenience function to create and return an invalid options error
  78. */
  79. DBusMessage * wpas_dbus_error_invalid_args(DBusMessage *message,
  80. const char *arg)
  81. {
  82. DBusMessage *reply;
  83. reply = dbus_message_new_error(
  84. message, WPAS_DBUS_ERROR_INVALID_ARGS,
  85. "Did not receive correct message arguments.");
  86. if (arg != NULL)
  87. dbus_message_append_args(reply, DBUS_TYPE_STRING, &arg,
  88. DBUS_TYPE_INVALID);
  89. return reply;
  90. }
  91. /**
  92. * wpas_dbus_error_scan_error - Return a new ScanError error message
  93. * @message: Pointer to incoming dbus message this error refers to
  94. * @error: Optional string to be used as the error message
  95. * Returns: a dbus error message
  96. *
  97. * Convenience function to create and return a scan error
  98. */
  99. static DBusMessage * wpas_dbus_error_scan_error(DBusMessage *message,
  100. const char *error)
  101. {
  102. return dbus_message_new_error(message,
  103. WPAS_DBUS_ERROR_IFACE_SCAN_ERROR,
  104. error);
  105. }
  106. DBusMessage * wpas_dbus_error_no_memory(DBusMessage *message)
  107. {
  108. wpa_printf(MSG_DEBUG, "dbus: Failed to allocate memory");
  109. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY, NULL);
  110. }
  111. static const char * const dont_quote[] = {
  112. "key_mgmt", "proto", "pairwise", "auth_alg", "group", "eap",
  113. "opensc_engine_path", "pkcs11_engine_path", "pkcs11_module_path",
  114. "bssid", "scan_freq", "freq_list", NULL
  115. };
  116. static dbus_bool_t should_quote_opt(const char *key)
  117. {
  118. int i = 0;
  119. while (dont_quote[i] != NULL) {
  120. if (os_strcmp(key, dont_quote[i]) == 0)
  121. return FALSE;
  122. i++;
  123. }
  124. return TRUE;
  125. }
  126. /**
  127. * get_iface_by_dbus_path - Get a new network interface
  128. * @global: Pointer to global data from wpa_supplicant_init()
  129. * @path: Pointer to a dbus object path representing an interface
  130. * Returns: Pointer to the interface or %NULL if not found
  131. */
  132. static struct wpa_supplicant * get_iface_by_dbus_path(
  133. struct wpa_global *global, const char *path)
  134. {
  135. struct wpa_supplicant *wpa_s;
  136. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  137. if (wpa_s->dbus_new_path &&
  138. os_strcmp(wpa_s->dbus_new_path, path) == 0)
  139. return wpa_s;
  140. }
  141. return NULL;
  142. }
  143. /**
  144. * set_network_properties - Set properties of a configured network
  145. * @wpa_s: wpa_supplicant structure for a network interface
  146. * @ssid: wpa_ssid structure for a configured network
  147. * @iter: DBus message iterator containing dictionary of network
  148. * properties to set.
  149. * @error: On failure, an error describing the failure
  150. * Returns: TRUE if the request succeeds, FALSE if it failed
  151. *
  152. * Sets network configuration with parameters given id DBus dictionary
  153. */
  154. dbus_bool_t set_network_properties(struct wpa_supplicant *wpa_s,
  155. struct wpa_ssid *ssid,
  156. DBusMessageIter *iter,
  157. DBusError *error)
  158. {
  159. struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
  160. DBusMessageIter iter_dict;
  161. char *value = NULL;
  162. if (!wpa_dbus_dict_open_read(iter, &iter_dict, error))
  163. return FALSE;
  164. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  165. size_t size = 50;
  166. int ret;
  167. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  168. goto error;
  169. value = NULL;
  170. if (entry.type == DBUS_TYPE_ARRAY &&
  171. entry.array_type == DBUS_TYPE_BYTE) {
  172. if (entry.array_len <= 0)
  173. goto error;
  174. size = entry.array_len * 2 + 1;
  175. value = os_zalloc(size);
  176. if (value == NULL)
  177. goto error;
  178. ret = wpa_snprintf_hex(value, size,
  179. (u8 *) entry.bytearray_value,
  180. entry.array_len);
  181. if (ret <= 0)
  182. goto error;
  183. } else if (entry.type == DBUS_TYPE_STRING) {
  184. if (should_quote_opt(entry.key)) {
  185. size = os_strlen(entry.str_value);
  186. if (size == 0)
  187. goto error;
  188. size += 3;
  189. value = os_zalloc(size);
  190. if (value == NULL)
  191. goto error;
  192. ret = os_snprintf(value, size, "\"%s\"",
  193. entry.str_value);
  194. if (os_snprintf_error(size, ret))
  195. goto error;
  196. } else {
  197. value = os_strdup(entry.str_value);
  198. if (value == NULL)
  199. goto error;
  200. }
  201. } else if (entry.type == DBUS_TYPE_UINT32) {
  202. value = os_zalloc(size);
  203. if (value == NULL)
  204. goto error;
  205. ret = os_snprintf(value, size, "%u",
  206. entry.uint32_value);
  207. if (os_snprintf_error(size, ret))
  208. goto error;
  209. } else if (entry.type == DBUS_TYPE_INT32) {
  210. value = os_zalloc(size);
  211. if (value == NULL)
  212. goto error;
  213. ret = os_snprintf(value, size, "%d",
  214. entry.int32_value);
  215. if (os_snprintf_error(size, ret))
  216. goto error;
  217. } else
  218. goto error;
  219. if (wpa_config_set(ssid, entry.key, value, 0) < 0)
  220. goto error;
  221. if (os_strcmp(entry.key, "bssid") != 0 &&
  222. os_strcmp(entry.key, "priority") != 0)
  223. wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
  224. if (wpa_s->current_ssid == ssid ||
  225. wpa_s->current_ssid == NULL) {
  226. /*
  227. * Invalidate the EAP session cache if anything in the
  228. * current or previously used configuration changes.
  229. */
  230. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  231. }
  232. if ((os_strcmp(entry.key, "psk") == 0 &&
  233. value[0] == '"' && ssid->ssid_len) ||
  234. (os_strcmp(entry.key, "ssid") == 0 && ssid->passphrase))
  235. wpa_config_update_psk(ssid);
  236. else if (os_strcmp(entry.key, "priority") == 0)
  237. wpa_config_update_prio_list(wpa_s->conf);
  238. os_free(value);
  239. value = NULL;
  240. wpa_dbus_dict_entry_clear(&entry);
  241. }
  242. return TRUE;
  243. error:
  244. os_free(value);
  245. wpa_dbus_dict_entry_clear(&entry);
  246. dbus_set_error_const(error, DBUS_ERROR_INVALID_ARGS,
  247. "invalid message format");
  248. return FALSE;
  249. }
  250. /**
  251. * wpas_dbus_simple_property_getter - Get basic type property
  252. * @iter: Message iter to use when appending arguments
  253. * @type: DBus type of property (must be basic type)
  254. * @val: pointer to place holding property value
  255. * @error: On failure an error describing the failure
  256. * Returns: TRUE if the request was successful, FALSE if it failed
  257. *
  258. * Generic getter for basic type properties. Type is required to be basic.
  259. */
  260. dbus_bool_t wpas_dbus_simple_property_getter(DBusMessageIter *iter,
  261. const int type,
  262. const void *val,
  263. DBusError *error)
  264. {
  265. DBusMessageIter variant_iter;
  266. if (!dbus_type_is_basic(type)) {
  267. dbus_set_error(error, DBUS_ERROR_FAILED,
  268. "%s: given type is not basic", __func__);
  269. return FALSE;
  270. }
  271. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
  272. wpa_dbus_type_as_string(type),
  273. &variant_iter) ||
  274. !dbus_message_iter_append_basic(&variant_iter, type, val) ||
  275. !dbus_message_iter_close_container(iter, &variant_iter)) {
  276. dbus_set_error(error, DBUS_ERROR_FAILED,
  277. "%s: error constructing reply", __func__);
  278. return FALSE;
  279. }
  280. return TRUE;
  281. }
  282. /**
  283. * wpas_dbus_simple_property_setter - Set basic type property
  284. * @message: Pointer to incoming dbus message
  285. * @type: DBus type of property (must be basic type)
  286. * @val: pointer to place where value being set will be stored
  287. * Returns: TRUE if the request was successful, FALSE if it failed
  288. *
  289. * Generic setter for basic type properties. Type is required to be basic.
  290. */
  291. dbus_bool_t wpas_dbus_simple_property_setter(DBusMessageIter *iter,
  292. DBusError *error,
  293. const int type, void *val)
  294. {
  295. DBusMessageIter variant_iter;
  296. if (!dbus_type_is_basic(type)) {
  297. dbus_set_error(error, DBUS_ERROR_FAILED,
  298. "%s: given type is not basic", __func__);
  299. return FALSE;
  300. }
  301. /* Look at the new value */
  302. dbus_message_iter_recurse(iter, &variant_iter);
  303. if (dbus_message_iter_get_arg_type(&variant_iter) != type) {
  304. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  305. "wrong property type");
  306. return FALSE;
  307. }
  308. dbus_message_iter_get_basic(&variant_iter, val);
  309. return TRUE;
  310. }
  311. /**
  312. * wpas_dbus_simple_array_property_getter - Get array type property
  313. * @iter: Pointer to incoming dbus message iterator
  314. * @type: DBus type of property array elements (must be basic type)
  315. * @array: pointer to array of elements to put into response message
  316. * @array_len: length of above array
  317. * @error: a pointer to an error to fill on failure
  318. * Returns: TRUE if the request succeeded, FALSE if it failed
  319. *
  320. * Generic getter for array type properties. Array elements type is
  321. * required to be basic.
  322. */
  323. dbus_bool_t wpas_dbus_simple_array_property_getter(DBusMessageIter *iter,
  324. const int type,
  325. const void *array,
  326. size_t array_len,
  327. DBusError *error)
  328. {
  329. DBusMessageIter variant_iter, array_iter;
  330. char type_str[] = "a?"; /* ? will be replaced with subtype letter; */
  331. const char *sub_type_str;
  332. size_t element_size, i;
  333. if (!dbus_type_is_basic(type)) {
  334. dbus_set_error(error, DBUS_ERROR_FAILED,
  335. "%s: given type is not basic", __func__);
  336. return FALSE;
  337. }
  338. sub_type_str = wpa_dbus_type_as_string(type);
  339. type_str[1] = sub_type_str[0];
  340. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
  341. type_str, &variant_iter) ||
  342. !dbus_message_iter_open_container(&variant_iter, DBUS_TYPE_ARRAY,
  343. sub_type_str, &array_iter)) {
  344. dbus_set_error(error, DBUS_ERROR_FAILED,
  345. "%s: failed to construct message", __func__);
  346. return FALSE;
  347. }
  348. switch (type) {
  349. case DBUS_TYPE_BYTE:
  350. case DBUS_TYPE_BOOLEAN:
  351. element_size = 1;
  352. break;
  353. case DBUS_TYPE_INT16:
  354. case DBUS_TYPE_UINT16:
  355. element_size = sizeof(uint16_t);
  356. break;
  357. case DBUS_TYPE_INT32:
  358. case DBUS_TYPE_UINT32:
  359. element_size = sizeof(uint32_t);
  360. break;
  361. case DBUS_TYPE_INT64:
  362. case DBUS_TYPE_UINT64:
  363. element_size = sizeof(uint64_t);
  364. break;
  365. case DBUS_TYPE_DOUBLE:
  366. element_size = sizeof(double);
  367. break;
  368. case DBUS_TYPE_STRING:
  369. case DBUS_TYPE_OBJECT_PATH:
  370. element_size = sizeof(char *);
  371. break;
  372. default:
  373. dbus_set_error(error, DBUS_ERROR_FAILED,
  374. "%s: unknown element type %d", __func__, type);
  375. return FALSE;
  376. }
  377. for (i = 0; i < array_len; i++) {
  378. if (!dbus_message_iter_append_basic(&array_iter, type,
  379. array + i * element_size)) {
  380. dbus_set_error(error, DBUS_ERROR_FAILED,
  381. "%s: failed to construct message 2.5",
  382. __func__);
  383. return FALSE;
  384. }
  385. }
  386. if (!dbus_message_iter_close_container(&variant_iter, &array_iter) ||
  387. !dbus_message_iter_close_container(iter, &variant_iter)) {
  388. dbus_set_error(error, DBUS_ERROR_FAILED,
  389. "%s: failed to construct message 3", __func__);
  390. return FALSE;
  391. }
  392. return TRUE;
  393. }
  394. /**
  395. * wpas_dbus_simple_array_array_property_getter - Get array array type property
  396. * @iter: Pointer to incoming dbus message iterator
  397. * @type: DBus type of property array elements (must be basic type)
  398. * @array: pointer to array of elements to put into response message
  399. * @array_len: length of above array
  400. * @error: a pointer to an error to fill on failure
  401. * Returns: TRUE if the request succeeded, FALSE if it failed
  402. *
  403. * Generic getter for array type properties. Array elements type is
  404. * required to be basic.
  405. */
  406. dbus_bool_t wpas_dbus_simple_array_array_property_getter(DBusMessageIter *iter,
  407. const int type,
  408. struct wpabuf **array,
  409. size_t array_len,
  410. DBusError *error)
  411. {
  412. DBusMessageIter variant_iter, array_iter;
  413. char type_str[] = "aa?";
  414. char inner_type_str[] = "a?";
  415. const char *sub_type_str;
  416. size_t i;
  417. if (!dbus_type_is_basic(type)) {
  418. dbus_set_error(error, DBUS_ERROR_FAILED,
  419. "%s: given type is not basic", __func__);
  420. return FALSE;
  421. }
  422. sub_type_str = wpa_dbus_type_as_string(type);
  423. type_str[2] = sub_type_str[0];
  424. inner_type_str[1] = sub_type_str[0];
  425. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
  426. type_str, &variant_iter) ||
  427. !dbus_message_iter_open_container(&variant_iter, DBUS_TYPE_ARRAY,
  428. inner_type_str, &array_iter)) {
  429. dbus_set_error(error, DBUS_ERROR_FAILED,
  430. "%s: failed to construct message", __func__);
  431. return FALSE;
  432. }
  433. for (i = 0; i < array_len && array[i]; i++) {
  434. wpa_dbus_dict_bin_array_add_element(&array_iter,
  435. wpabuf_head(array[i]),
  436. wpabuf_len(array[i]));
  437. }
  438. if (!dbus_message_iter_close_container(&variant_iter, &array_iter) ||
  439. !dbus_message_iter_close_container(iter, &variant_iter)) {
  440. dbus_set_error(error, DBUS_ERROR_FAILED,
  441. "%s: failed to close message", __func__);
  442. return FALSE;
  443. }
  444. return TRUE;
  445. }
  446. /**
  447. * wpas_dbus_handler_create_interface - Request registration of a network iface
  448. * @message: Pointer to incoming dbus message
  449. * @global: %wpa_supplicant global data structure
  450. * Returns: The object path of the new interface object,
  451. * or a dbus error message with more information
  452. *
  453. * Handler function for "CreateInterface" method call. Handles requests
  454. * by dbus clients to register a network interface that wpa_supplicant
  455. * will manage.
  456. */
  457. DBusMessage * wpas_dbus_handler_create_interface(DBusMessage *message,
  458. struct wpa_global *global)
  459. {
  460. DBusMessageIter iter_dict;
  461. DBusMessage *reply = NULL;
  462. DBusMessageIter iter;
  463. struct wpa_dbus_dict_entry entry;
  464. char *driver = NULL;
  465. char *ifname = NULL;
  466. char *confname = NULL;
  467. char *bridge_ifname = NULL;
  468. dbus_message_iter_init(message, &iter);
  469. if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
  470. goto error;
  471. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  472. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  473. goto error;
  474. if (os_strcmp(entry.key, "Driver") == 0 &&
  475. entry.type == DBUS_TYPE_STRING) {
  476. os_free(driver);
  477. driver = os_strdup(entry.str_value);
  478. wpa_dbus_dict_entry_clear(&entry);
  479. if (driver == NULL)
  480. goto oom;
  481. } else if (os_strcmp(entry.key, "Ifname") == 0 &&
  482. entry.type == DBUS_TYPE_STRING) {
  483. os_free(ifname);
  484. ifname = os_strdup(entry.str_value);
  485. wpa_dbus_dict_entry_clear(&entry);
  486. if (ifname == NULL)
  487. goto oom;
  488. } else if (os_strcmp(entry.key, "ConfigFile") == 0 &&
  489. entry.type == DBUS_TYPE_STRING) {
  490. os_free(confname);
  491. confname = os_strdup(entry.str_value);
  492. wpa_dbus_dict_entry_clear(&entry);
  493. if (confname == NULL)
  494. goto oom;
  495. } else if (os_strcmp(entry.key, "BridgeIfname") == 0 &&
  496. entry.type == DBUS_TYPE_STRING) {
  497. os_free(bridge_ifname);
  498. bridge_ifname = os_strdup(entry.str_value);
  499. wpa_dbus_dict_entry_clear(&entry);
  500. if (bridge_ifname == NULL)
  501. goto oom;
  502. } else {
  503. wpa_dbus_dict_entry_clear(&entry);
  504. goto error;
  505. }
  506. }
  507. if (ifname == NULL)
  508. goto error; /* Required Ifname argument missing */
  509. /*
  510. * Try to get the wpa_supplicant record for this iface, return
  511. * an error if we already control it.
  512. */
  513. if (wpa_supplicant_get_iface(global, ifname) != NULL) {
  514. reply = dbus_message_new_error(
  515. message, WPAS_DBUS_ERROR_IFACE_EXISTS,
  516. "wpa_supplicant already controls this interface.");
  517. } else {
  518. struct wpa_supplicant *wpa_s;
  519. struct wpa_interface iface;
  520. os_memset(&iface, 0, sizeof(iface));
  521. iface.driver = driver;
  522. iface.ifname = ifname;
  523. iface.confname = confname;
  524. iface.bridge_ifname = bridge_ifname;
  525. /* Otherwise, have wpa_supplicant attach to it. */
  526. wpa_s = wpa_supplicant_add_iface(global, &iface, NULL);
  527. if (wpa_s && wpa_s->dbus_new_path) {
  528. const char *path = wpa_s->dbus_new_path;
  529. reply = dbus_message_new_method_return(message);
  530. dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH,
  531. &path, DBUS_TYPE_INVALID);
  532. } else {
  533. reply = wpas_dbus_error_unknown_error(
  534. message,
  535. "wpa_supplicant couldn't grab this interface.");
  536. }
  537. }
  538. out:
  539. os_free(driver);
  540. os_free(ifname);
  541. os_free(confname);
  542. os_free(bridge_ifname);
  543. return reply;
  544. error:
  545. reply = wpas_dbus_error_invalid_args(message, NULL);
  546. goto out;
  547. oom:
  548. reply = wpas_dbus_error_no_memory(message);
  549. goto out;
  550. }
  551. /**
  552. * wpas_dbus_handler_remove_interface - Request deregistration of an interface
  553. * @message: Pointer to incoming dbus message
  554. * @global: wpa_supplicant global data structure
  555. * Returns: a dbus message containing a UINT32 indicating success (1) or
  556. * failure (0), or returns a dbus error message with more information
  557. *
  558. * Handler function for "removeInterface" method call. Handles requests
  559. * by dbus clients to deregister a network interface that wpa_supplicant
  560. * currently manages.
  561. */
  562. DBusMessage * wpas_dbus_handler_remove_interface(DBusMessage *message,
  563. struct wpa_global *global)
  564. {
  565. struct wpa_supplicant *wpa_s;
  566. char *path;
  567. DBusMessage *reply = NULL;
  568. dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
  569. DBUS_TYPE_INVALID);
  570. wpa_s = get_iface_by_dbus_path(global, path);
  571. if (wpa_s == NULL)
  572. reply = wpas_dbus_error_iface_unknown(message);
  573. else if (wpa_supplicant_remove_iface(global, wpa_s, 0)) {
  574. reply = wpas_dbus_error_unknown_error(
  575. message,
  576. "wpa_supplicant couldn't remove this interface.");
  577. }
  578. return reply;
  579. }
  580. /**
  581. * wpas_dbus_handler_get_interface - Get the object path for an interface name
  582. * @message: Pointer to incoming dbus message
  583. * @global: %wpa_supplicant global data structure
  584. * Returns: The object path of the interface object,
  585. * or a dbus error message with more information
  586. *
  587. * Handler function for "getInterface" method call.
  588. */
  589. DBusMessage * wpas_dbus_handler_get_interface(DBusMessage *message,
  590. struct wpa_global *global)
  591. {
  592. DBusMessage *reply = NULL;
  593. const char *ifname;
  594. const char *path;
  595. struct wpa_supplicant *wpa_s;
  596. dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &ifname,
  597. DBUS_TYPE_INVALID);
  598. wpa_s = wpa_supplicant_get_iface(global, ifname);
  599. if (wpa_s == NULL || wpa_s->dbus_new_path == NULL)
  600. return wpas_dbus_error_iface_unknown(message);
  601. path = wpa_s->dbus_new_path;
  602. reply = dbus_message_new_method_return(message);
  603. if (reply == NULL)
  604. return wpas_dbus_error_no_memory(message);
  605. if (!dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path,
  606. DBUS_TYPE_INVALID)) {
  607. dbus_message_unref(reply);
  608. return wpas_dbus_error_no_memory(message);
  609. }
  610. return reply;
  611. }
  612. /**
  613. * wpas_dbus_getter_debug_level - Get debug level
  614. * @iter: Pointer to incoming dbus message iter
  615. * @error: Location to store error on failure
  616. * @user_data: Function specific data
  617. * Returns: TRUE on success, FALSE on failure
  618. *
  619. * Getter for "DebugLevel" property.
  620. */
  621. dbus_bool_t wpas_dbus_getter_debug_level(DBusMessageIter *iter,
  622. DBusError *error,
  623. void *user_data)
  624. {
  625. const char *str;
  626. int idx = wpa_debug_level;
  627. if (idx < 0)
  628. idx = 0;
  629. if (idx > 5)
  630. idx = 5;
  631. str = debug_strings[idx];
  632. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  633. &str, error);
  634. }
  635. /**
  636. * wpas_dbus_getter_debug_timestamp - Get debug timestamp
  637. * @iter: Pointer to incoming dbus message iter
  638. * @error: Location to store error on failure
  639. * @user_data: Function specific data
  640. * Returns: TRUE on success, FALSE on failure
  641. *
  642. * Getter for "DebugTimestamp" property.
  643. */
  644. dbus_bool_t wpas_dbus_getter_debug_timestamp(DBusMessageIter *iter,
  645. DBusError *error,
  646. void *user_data)
  647. {
  648. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_BOOLEAN,
  649. &wpa_debug_timestamp, error);
  650. }
  651. /**
  652. * wpas_dbus_getter_debug_show_keys - Get debug show keys
  653. * @iter: Pointer to incoming dbus message iter
  654. * @error: Location to store error on failure
  655. * @user_data: Function specific data
  656. * Returns: TRUE on success, FALSE on failure
  657. *
  658. * Getter for "DebugShowKeys" property.
  659. */
  660. dbus_bool_t wpas_dbus_getter_debug_show_keys(DBusMessageIter *iter,
  661. DBusError *error,
  662. void *user_data)
  663. {
  664. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_BOOLEAN,
  665. &wpa_debug_show_keys, error);
  666. }
  667. /**
  668. * wpas_dbus_setter_debug_level - Set debug level
  669. * @iter: Pointer to incoming dbus message iter
  670. * @error: Location to store error on failure
  671. * @user_data: Function specific data
  672. * Returns: TRUE on success, FALSE on failure
  673. *
  674. * Setter for "DebugLevel" property.
  675. */
  676. dbus_bool_t wpas_dbus_setter_debug_level(DBusMessageIter *iter,
  677. DBusError *error, void *user_data)
  678. {
  679. struct wpa_global *global = user_data;
  680. const char *str = NULL;
  681. int i, val = -1;
  682. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_STRING,
  683. &str))
  684. return FALSE;
  685. for (i = 0; debug_strings[i]; i++)
  686. if (os_strcmp(debug_strings[i], str) == 0) {
  687. val = i;
  688. break;
  689. }
  690. if (val < 0 ||
  691. wpa_supplicant_set_debug_params(global, val, wpa_debug_timestamp,
  692. wpa_debug_show_keys)) {
  693. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  694. "wrong debug level value");
  695. return FALSE;
  696. }
  697. return TRUE;
  698. }
  699. /**
  700. * wpas_dbus_setter_debug_timestamp - Set debug timestamp
  701. * @iter: Pointer to incoming dbus message iter
  702. * @error: Location to store error on failure
  703. * @user_data: Function specific data
  704. * Returns: TRUE on success, FALSE on failure
  705. *
  706. * Setter for "DebugTimestamp" property.
  707. */
  708. dbus_bool_t wpas_dbus_setter_debug_timestamp(DBusMessageIter *iter,
  709. DBusError *error,
  710. void *user_data)
  711. {
  712. struct wpa_global *global = user_data;
  713. dbus_bool_t val;
  714. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_BOOLEAN,
  715. &val))
  716. return FALSE;
  717. wpa_supplicant_set_debug_params(global, wpa_debug_level, val ? 1 : 0,
  718. wpa_debug_show_keys);
  719. return TRUE;
  720. }
  721. /**
  722. * wpas_dbus_setter_debug_show_keys - Set debug show keys
  723. * @iter: Pointer to incoming dbus message iter
  724. * @error: Location to store error on failure
  725. * @user_data: Function specific data
  726. * Returns: TRUE on success, FALSE on failure
  727. *
  728. * Setter for "DebugShowKeys" property.
  729. */
  730. dbus_bool_t wpas_dbus_setter_debug_show_keys(DBusMessageIter *iter,
  731. DBusError *error,
  732. void *user_data)
  733. {
  734. struct wpa_global *global = user_data;
  735. dbus_bool_t val;
  736. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_BOOLEAN,
  737. &val))
  738. return FALSE;
  739. wpa_supplicant_set_debug_params(global, wpa_debug_level,
  740. wpa_debug_timestamp,
  741. val ? 1 : 0);
  742. return TRUE;
  743. }
  744. /**
  745. * wpas_dbus_getter_interfaces - Request registered interfaces list
  746. * @iter: Pointer to incoming dbus message iter
  747. * @error: Location to store error on failure
  748. * @user_data: Function specific data
  749. * Returns: TRUE on success, FALSE on failure
  750. *
  751. * Getter for "Interfaces" property. Handles requests
  752. * by dbus clients to return list of registered interfaces objects
  753. * paths
  754. */
  755. dbus_bool_t wpas_dbus_getter_interfaces(DBusMessageIter *iter,
  756. DBusError *error,
  757. void *user_data)
  758. {
  759. struct wpa_global *global = user_data;
  760. struct wpa_supplicant *wpa_s;
  761. const char **paths;
  762. unsigned int i = 0, num = 0;
  763. dbus_bool_t success;
  764. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  765. if (wpa_s->dbus_new_path)
  766. num++;
  767. }
  768. paths = os_calloc(num, sizeof(char *));
  769. if (!paths) {
  770. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  771. return FALSE;
  772. }
  773. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  774. if (wpa_s->dbus_new_path)
  775. paths[i++] = wpa_s->dbus_new_path;
  776. }
  777. success = wpas_dbus_simple_array_property_getter(iter,
  778. DBUS_TYPE_OBJECT_PATH,
  779. paths, num, error);
  780. os_free(paths);
  781. return success;
  782. }
  783. /**
  784. * wpas_dbus_getter_eap_methods - Request supported EAP methods list
  785. * @iter: Pointer to incoming dbus message iter
  786. * @error: Location to store error on failure
  787. * @user_data: Function specific data
  788. * Returns: TRUE on success, FALSE on failure
  789. *
  790. * Getter for "EapMethods" property. Handles requests
  791. * by dbus clients to return list of strings with supported EAP methods
  792. */
  793. dbus_bool_t wpas_dbus_getter_eap_methods(DBusMessageIter *iter,
  794. DBusError *error, void *user_data)
  795. {
  796. char **eap_methods;
  797. size_t num_items = 0;
  798. dbus_bool_t success;
  799. eap_methods = eap_get_names_as_string_array(&num_items);
  800. if (!eap_methods) {
  801. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  802. return FALSE;
  803. }
  804. success = wpas_dbus_simple_array_property_getter(iter,
  805. DBUS_TYPE_STRING,
  806. eap_methods,
  807. num_items, error);
  808. while (num_items)
  809. os_free(eap_methods[--num_items]);
  810. os_free(eap_methods);
  811. return success;
  812. }
  813. /**
  814. * wpas_dbus_getter_global_capabilities - Request supported global capabilities
  815. * @iter: Pointer to incoming dbus message iter
  816. * @error: Location to store error on failure
  817. * @user_data: Function specific data
  818. * Returns: TRUE on success, FALSE on failure
  819. *
  820. * Getter for "Capabilities" property. Handles requests by dbus clients to
  821. * return a list of strings with supported capabilities like AP, RSN IBSS,
  822. * and P2P that are determined at compile time.
  823. */
  824. dbus_bool_t wpas_dbus_getter_global_capabilities(DBusMessageIter *iter,
  825. DBusError *error,
  826. void *user_data)
  827. {
  828. const char *capabilities[5] = { NULL, NULL, NULL, NULL, NULL };
  829. size_t num_items = 0;
  830. #ifdef CONFIG_AP
  831. capabilities[num_items++] = "ap";
  832. #endif /* CONFIG_AP */
  833. #ifdef CONFIG_IBSS_RSN
  834. capabilities[num_items++] = "ibss-rsn";
  835. #endif /* CONFIG_IBSS_RSN */
  836. #ifdef CONFIG_P2P
  837. capabilities[num_items++] = "p2p";
  838. #endif /* CONFIG_P2P */
  839. #ifdef CONFIG_INTERWORKING
  840. capabilities[num_items++] = "interworking";
  841. #endif /* CONFIG_INTERWORKING */
  842. return wpas_dbus_simple_array_property_getter(iter,
  843. DBUS_TYPE_STRING,
  844. capabilities,
  845. num_items, error);
  846. }
  847. static int wpas_dbus_get_scan_type(DBusMessage *message, DBusMessageIter *var,
  848. char **type, DBusMessage **reply)
  849. {
  850. if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_STRING) {
  851. wpa_printf(MSG_DEBUG, "%s[dbus]: Type must be a string",
  852. __func__);
  853. *reply = wpas_dbus_error_invalid_args(
  854. message, "Wrong Type value type. String required");
  855. return -1;
  856. }
  857. dbus_message_iter_get_basic(var, type);
  858. return 0;
  859. }
  860. static int wpas_dbus_get_scan_ssids(DBusMessage *message, DBusMessageIter *var,
  861. struct wpa_driver_scan_params *params,
  862. DBusMessage **reply)
  863. {
  864. struct wpa_driver_scan_ssid *ssids = params->ssids;
  865. size_t ssids_num = 0;
  866. u8 *ssid;
  867. DBusMessageIter array_iter, sub_array_iter;
  868. char *val;
  869. int len;
  870. if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_ARRAY) {
  871. wpa_printf(MSG_DEBUG,
  872. "%s[dbus]: ssids must be an array of arrays of bytes",
  873. __func__);
  874. *reply = wpas_dbus_error_invalid_args(
  875. message,
  876. "Wrong SSIDs value type. Array of arrays of bytes required");
  877. return -1;
  878. }
  879. dbus_message_iter_recurse(var, &array_iter);
  880. if (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_ARRAY ||
  881. dbus_message_iter_get_element_type(&array_iter) != DBUS_TYPE_BYTE) {
  882. wpa_printf(MSG_DEBUG,
  883. "%s[dbus]: ssids must be an array of arrays of bytes",
  884. __func__);
  885. *reply = wpas_dbus_error_invalid_args(
  886. message,
  887. "Wrong SSIDs value type. Array of arrays of bytes required");
  888. return -1;
  889. }
  890. while (dbus_message_iter_get_arg_type(&array_iter) == DBUS_TYPE_ARRAY) {
  891. if (ssids_num >= WPAS_MAX_SCAN_SSIDS) {
  892. wpa_printf(MSG_DEBUG,
  893. "%s[dbus]: Too many ssids specified on scan dbus call",
  894. __func__);
  895. *reply = wpas_dbus_error_invalid_args(
  896. message,
  897. "Too many ssids specified. Specify at most four");
  898. return -1;
  899. }
  900. dbus_message_iter_recurse(&array_iter, &sub_array_iter);
  901. dbus_message_iter_get_fixed_array(&sub_array_iter, &val, &len);
  902. if (len > SSID_MAX_LEN) {
  903. wpa_printf(MSG_DEBUG,
  904. "%s[dbus]: SSID too long (len=%d max_len=%d)",
  905. __func__, len, SSID_MAX_LEN);
  906. *reply = wpas_dbus_error_invalid_args(
  907. message, "Invalid SSID: too long");
  908. return -1;
  909. }
  910. if (len != 0) {
  911. ssid = os_malloc(len);
  912. if (ssid == NULL) {
  913. *reply = wpas_dbus_error_no_memory(message);
  914. return -1;
  915. }
  916. os_memcpy(ssid, val, len);
  917. } else {
  918. /* Allow zero-length SSIDs */
  919. ssid = NULL;
  920. }
  921. ssids[ssids_num].ssid = ssid;
  922. ssids[ssids_num].ssid_len = len;
  923. dbus_message_iter_next(&array_iter);
  924. ssids_num++;
  925. }
  926. params->num_ssids = ssids_num;
  927. return 0;
  928. }
  929. static int wpas_dbus_get_scan_ies(DBusMessage *message, DBusMessageIter *var,
  930. struct wpa_driver_scan_params *params,
  931. DBusMessage **reply)
  932. {
  933. u8 *ies = NULL, *nies;
  934. int ies_len = 0;
  935. DBusMessageIter array_iter, sub_array_iter;
  936. char *val;
  937. int len;
  938. if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_ARRAY) {
  939. wpa_printf(MSG_DEBUG,
  940. "%s[dbus]: ies must be an array of arrays of bytes",
  941. __func__);
  942. *reply = wpas_dbus_error_invalid_args(
  943. message,
  944. "Wrong IEs value type. Array of arrays of bytes required");
  945. return -1;
  946. }
  947. dbus_message_iter_recurse(var, &array_iter);
  948. if (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_ARRAY ||
  949. dbus_message_iter_get_element_type(&array_iter) != DBUS_TYPE_BYTE) {
  950. wpa_printf(MSG_DEBUG,
  951. "%s[dbus]: ies must be an array of arrays of bytes",
  952. __func__);
  953. *reply = wpas_dbus_error_invalid_args(
  954. message, "Wrong IEs value type. Array required");
  955. return -1;
  956. }
  957. while (dbus_message_iter_get_arg_type(&array_iter) == DBUS_TYPE_ARRAY) {
  958. dbus_message_iter_recurse(&array_iter, &sub_array_iter);
  959. dbus_message_iter_get_fixed_array(&sub_array_iter, &val, &len);
  960. if (len == 0) {
  961. dbus_message_iter_next(&array_iter);
  962. continue;
  963. }
  964. nies = os_realloc(ies, ies_len + len);
  965. if (nies == NULL) {
  966. os_free(ies);
  967. *reply = wpas_dbus_error_no_memory(message);
  968. return -1;
  969. }
  970. ies = nies;
  971. os_memcpy(ies + ies_len, val, len);
  972. ies_len += len;
  973. dbus_message_iter_next(&array_iter);
  974. }
  975. params->extra_ies = ies;
  976. params->extra_ies_len = ies_len;
  977. return 0;
  978. }
  979. static int wpas_dbus_get_scan_channels(DBusMessage *message,
  980. DBusMessageIter *var,
  981. struct wpa_driver_scan_params *params,
  982. DBusMessage **reply)
  983. {
  984. DBusMessageIter array_iter, sub_array_iter;
  985. int *freqs = NULL, *nfreqs;
  986. int freqs_num = 0;
  987. if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_ARRAY) {
  988. wpa_printf(MSG_DEBUG,
  989. "%s[dbus]: Channels must be an array of structs",
  990. __func__);
  991. *reply = wpas_dbus_error_invalid_args(
  992. message,
  993. "Wrong Channels value type. Array of structs required");
  994. return -1;
  995. }
  996. dbus_message_iter_recurse(var, &array_iter);
  997. if (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_STRUCT) {
  998. wpa_printf(MSG_DEBUG,
  999. "%s[dbus]: Channels must be an array of structs",
  1000. __func__);
  1001. *reply = wpas_dbus_error_invalid_args(
  1002. message,
  1003. "Wrong Channels value type. Array of structs required");
  1004. return -1;
  1005. }
  1006. while (dbus_message_iter_get_arg_type(&array_iter) == DBUS_TYPE_STRUCT)
  1007. {
  1008. int freq, width;
  1009. dbus_message_iter_recurse(&array_iter, &sub_array_iter);
  1010. if (dbus_message_iter_get_arg_type(&sub_array_iter) !=
  1011. DBUS_TYPE_UINT32) {
  1012. wpa_printf(MSG_DEBUG,
  1013. "%s[dbus]: Channel must by specified by struct of two UINT32s %c",
  1014. __func__,
  1015. dbus_message_iter_get_arg_type(
  1016. &sub_array_iter));
  1017. *reply = wpas_dbus_error_invalid_args(
  1018. message,
  1019. "Wrong Channel struct. Two UINT32s required");
  1020. os_free(freqs);
  1021. return -1;
  1022. }
  1023. dbus_message_iter_get_basic(&sub_array_iter, &freq);
  1024. if (!dbus_message_iter_next(&sub_array_iter) ||
  1025. dbus_message_iter_get_arg_type(&sub_array_iter) !=
  1026. DBUS_TYPE_UINT32) {
  1027. wpa_printf(MSG_DEBUG,
  1028. "%s[dbus]: Channel must by specified by struct of two UINT32s",
  1029. __func__);
  1030. *reply = wpas_dbus_error_invalid_args(
  1031. message,
  1032. "Wrong Channel struct. Two UINT32s required");
  1033. os_free(freqs);
  1034. return -1;
  1035. }
  1036. dbus_message_iter_get_basic(&sub_array_iter, &width);
  1037. #define FREQS_ALLOC_CHUNK 32
  1038. if (freqs_num % FREQS_ALLOC_CHUNK == 0) {
  1039. nfreqs = os_realloc_array(
  1040. freqs, freqs_num + FREQS_ALLOC_CHUNK,
  1041. sizeof(int));
  1042. if (nfreqs == NULL)
  1043. os_free(freqs);
  1044. freqs = nfreqs;
  1045. }
  1046. if (freqs == NULL) {
  1047. *reply = wpas_dbus_error_no_memory(message);
  1048. return -1;
  1049. }
  1050. freqs[freqs_num] = freq;
  1051. freqs_num++;
  1052. dbus_message_iter_next(&array_iter);
  1053. }
  1054. nfreqs = os_realloc_array(freqs, freqs_num + 1, sizeof(int));
  1055. if (nfreqs == NULL)
  1056. os_free(freqs);
  1057. freqs = nfreqs;
  1058. if (freqs == NULL) {
  1059. *reply = wpas_dbus_error_no_memory(message);
  1060. return -1;
  1061. }
  1062. freqs[freqs_num] = 0;
  1063. params->freqs = freqs;
  1064. return 0;
  1065. }
  1066. static int wpas_dbus_get_scan_allow_roam(DBusMessage *message,
  1067. DBusMessageIter *var,
  1068. dbus_bool_t *allow,
  1069. DBusMessage **reply)
  1070. {
  1071. if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_BOOLEAN) {
  1072. wpa_printf(MSG_DEBUG, "%s[dbus]: Type must be a boolean",
  1073. __func__);
  1074. *reply = wpas_dbus_error_invalid_args(
  1075. message, "Wrong Type value type. Boolean required");
  1076. return -1;
  1077. }
  1078. dbus_message_iter_get_basic(var, allow);
  1079. return 0;
  1080. }
  1081. /**
  1082. * wpas_dbus_handler_scan - Request a wireless scan on an interface
  1083. * @message: Pointer to incoming dbus message
  1084. * @wpa_s: wpa_supplicant structure for a network interface
  1085. * Returns: NULL indicating success or DBus error message on failure
  1086. *
  1087. * Handler function for "Scan" method call of a network device. Requests
  1088. * that wpa_supplicant perform a wireless scan as soon as possible
  1089. * on a particular wireless interface.
  1090. */
  1091. DBusMessage * wpas_dbus_handler_scan(DBusMessage *message,
  1092. struct wpa_supplicant *wpa_s)
  1093. {
  1094. DBusMessage *reply = NULL;
  1095. DBusMessageIter iter, dict_iter, entry_iter, variant_iter;
  1096. char *key = NULL, *type = NULL;
  1097. struct wpa_driver_scan_params params;
  1098. size_t i;
  1099. dbus_bool_t allow_roam = 1;
  1100. os_memset(&params, 0, sizeof(params));
  1101. dbus_message_iter_init(message, &iter);
  1102. dbus_message_iter_recurse(&iter, &dict_iter);
  1103. while (dbus_message_iter_get_arg_type(&dict_iter) ==
  1104. DBUS_TYPE_DICT_ENTRY) {
  1105. dbus_message_iter_recurse(&dict_iter, &entry_iter);
  1106. dbus_message_iter_get_basic(&entry_iter, &key);
  1107. dbus_message_iter_next(&entry_iter);
  1108. dbus_message_iter_recurse(&entry_iter, &variant_iter);
  1109. if (os_strcmp(key, "Type") == 0) {
  1110. if (wpas_dbus_get_scan_type(message, &variant_iter,
  1111. &type, &reply) < 0)
  1112. goto out;
  1113. } else if (os_strcmp(key, "SSIDs") == 0) {
  1114. if (wpas_dbus_get_scan_ssids(message, &variant_iter,
  1115. &params, &reply) < 0)
  1116. goto out;
  1117. } else if (os_strcmp(key, "IEs") == 0) {
  1118. if (wpas_dbus_get_scan_ies(message, &variant_iter,
  1119. &params, &reply) < 0)
  1120. goto out;
  1121. } else if (os_strcmp(key, "Channels") == 0) {
  1122. if (wpas_dbus_get_scan_channels(message, &variant_iter,
  1123. &params, &reply) < 0)
  1124. goto out;
  1125. } else if (os_strcmp(key, "AllowRoam") == 0) {
  1126. if (wpas_dbus_get_scan_allow_roam(message,
  1127. &variant_iter,
  1128. &allow_roam,
  1129. &reply) < 0)
  1130. goto out;
  1131. } else {
  1132. wpa_printf(MSG_DEBUG, "%s[dbus]: Unknown argument %s",
  1133. __func__, key);
  1134. reply = wpas_dbus_error_invalid_args(message, key);
  1135. goto out;
  1136. }
  1137. dbus_message_iter_next(&dict_iter);
  1138. }
  1139. if (!type) {
  1140. wpa_printf(MSG_DEBUG, "%s[dbus]: Scan type not specified",
  1141. __func__);
  1142. reply = wpas_dbus_error_invalid_args(message, key);
  1143. goto out;
  1144. }
  1145. if (os_strcmp(type, "passive") == 0) {
  1146. if (params.num_ssids || params.extra_ies_len) {
  1147. wpa_printf(MSG_DEBUG,
  1148. "%s[dbus]: SSIDs or IEs specified for passive scan.",
  1149. __func__);
  1150. reply = wpas_dbus_error_invalid_args(
  1151. message,
  1152. "You can specify only Channels in passive scan");
  1153. goto out;
  1154. } else {
  1155. if (wpa_s->sched_scanning) {
  1156. wpa_printf(MSG_DEBUG,
  1157. "%s[dbus]: Stop ongoing sched_scan to allow requested scan to proceed",
  1158. __func__);
  1159. wpa_supplicant_cancel_sched_scan(wpa_s);
  1160. }
  1161. if (params.freqs && params.freqs[0]) {
  1162. wpa_s->last_scan_req = MANUAL_SCAN_REQ;
  1163. if (wpa_supplicant_trigger_scan(wpa_s,
  1164. &params)) {
  1165. reply = wpas_dbus_error_scan_error(
  1166. message,
  1167. "Scan request rejected");
  1168. }
  1169. } else {
  1170. wpa_s->scan_req = MANUAL_SCAN_REQ;
  1171. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1172. }
  1173. }
  1174. } else if (os_strcmp(type, "active") == 0) {
  1175. if (!params.num_ssids) {
  1176. /* Add wildcard ssid */
  1177. params.num_ssids++;
  1178. }
  1179. #ifdef CONFIG_AUTOSCAN
  1180. autoscan_deinit(wpa_s);
  1181. #endif /* CONFIG_AUTOSCAN */
  1182. if (wpa_s->sched_scanning) {
  1183. wpa_printf(MSG_DEBUG,
  1184. "%s[dbus]: Stop ongoing sched_scan to allow requested scan to proceed",
  1185. __func__);
  1186. wpa_supplicant_cancel_sched_scan(wpa_s);
  1187. }
  1188. wpa_s->last_scan_req = MANUAL_SCAN_REQ;
  1189. if (wpa_supplicant_trigger_scan(wpa_s, &params)) {
  1190. reply = wpas_dbus_error_scan_error(
  1191. message, "Scan request rejected");
  1192. }
  1193. } else {
  1194. wpa_printf(MSG_DEBUG, "%s[dbus]: Unknown scan type: %s",
  1195. __func__, type);
  1196. reply = wpas_dbus_error_invalid_args(message,
  1197. "Wrong scan type");
  1198. goto out;
  1199. }
  1200. if (!allow_roam)
  1201. wpa_s->scan_res_handler = scan_only_handler;
  1202. out:
  1203. for (i = 0; i < WPAS_MAX_SCAN_SSIDS; i++)
  1204. os_free((u8 *) params.ssids[i].ssid);
  1205. os_free((u8 *) params.extra_ies);
  1206. os_free(params.freqs);
  1207. return reply;
  1208. }
  1209. /**
  1210. * wpas_dbus_handler_signal_poll - Request immediate signal properties
  1211. * @message: Pointer to incoming dbus message
  1212. * @wpa_s: wpa_supplicant structure for a network interface
  1213. * Returns: NULL indicating success or DBus error message on failure
  1214. *
  1215. * Handler function for "SignalPoll" method call of a network device. Requests
  1216. * that wpa_supplicant read signal properties like RSSI, noise, and link
  1217. * speed and return them.
  1218. */
  1219. DBusMessage * wpas_dbus_handler_signal_poll(DBusMessage *message,
  1220. struct wpa_supplicant *wpa_s)
  1221. {
  1222. struct wpa_signal_info si;
  1223. DBusMessage *reply = NULL;
  1224. DBusMessageIter iter, iter_dict, variant_iter;
  1225. int ret;
  1226. ret = wpa_drv_signal_poll(wpa_s, &si);
  1227. if (ret) {
  1228. return dbus_message_new_error(message, DBUS_ERROR_FAILED,
  1229. "Failed to read signal");
  1230. }
  1231. reply = dbus_message_new_method_return(message);
  1232. if (reply == NULL)
  1233. goto nomem;
  1234. dbus_message_iter_init_append(reply, &iter);
  1235. if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
  1236. "a{sv}", &variant_iter) ||
  1237. !wpa_dbus_dict_open_write(&variant_iter, &iter_dict) ||
  1238. !wpa_dbus_dict_append_int32(&iter_dict, "rssi",
  1239. si.current_signal) ||
  1240. !wpa_dbus_dict_append_int32(&iter_dict, "linkspeed",
  1241. si.current_txrate / 1000) ||
  1242. !wpa_dbus_dict_append_int32(&iter_dict, "noise",
  1243. si.current_noise) ||
  1244. !wpa_dbus_dict_append_uint32(&iter_dict, "frequency",
  1245. si.frequency) ||
  1246. (si.chanwidth != CHAN_WIDTH_UNKNOWN &&
  1247. !wpa_dbus_dict_append_string(
  1248. &iter_dict, "width",
  1249. channel_width_to_string(si.chanwidth))) ||
  1250. (si.center_frq1 > 0 && si.center_frq2 > 0 &&
  1251. (!wpa_dbus_dict_append_int32(&iter_dict, "center-frq1",
  1252. si.center_frq1) ||
  1253. !wpa_dbus_dict_append_int32(&iter_dict, "center-frq2",
  1254. si.center_frq2))) ||
  1255. (si.avg_signal &&
  1256. !wpa_dbus_dict_append_int32(&iter_dict, "avg-rssi",
  1257. si.avg_signal)) ||
  1258. !wpa_dbus_dict_close_write(&variant_iter, &iter_dict) ||
  1259. !dbus_message_iter_close_container(&iter, &variant_iter))
  1260. goto nomem;
  1261. return reply;
  1262. nomem:
  1263. if (reply)
  1264. dbus_message_unref(reply);
  1265. return wpas_dbus_error_no_memory(message);
  1266. }
  1267. /*
  1268. * wpas_dbus_handler_disconnect - Terminate the current connection
  1269. * @message: Pointer to incoming dbus message
  1270. * @wpa_s: wpa_supplicant structure for a network interface
  1271. * Returns: NotConnected DBus error message if already not connected
  1272. * or NULL otherwise.
  1273. *
  1274. * Handler function for "Disconnect" method call of network interface.
  1275. */
  1276. DBusMessage * wpas_dbus_handler_disconnect(DBusMessage *message,
  1277. struct wpa_supplicant *wpa_s)
  1278. {
  1279. if (wpa_s->current_ssid != NULL) {
  1280. wpa_s->disconnected = 1;
  1281. wpa_supplicant_deauthenticate(wpa_s,
  1282. WLAN_REASON_DEAUTH_LEAVING);
  1283. return NULL;
  1284. }
  1285. return dbus_message_new_error(message, WPAS_DBUS_ERROR_NOT_CONNECTED,
  1286. "This interface is not connected");
  1287. }
  1288. /**
  1289. * wpas_dbus_new_iface_add_network - Add a new configured network
  1290. * @message: Pointer to incoming dbus message
  1291. * @wpa_s: wpa_supplicant structure for a network interface
  1292. * Returns: A dbus message containing the object path of the new network
  1293. *
  1294. * Handler function for "AddNetwork" method call of a network interface.
  1295. */
  1296. DBusMessage * wpas_dbus_handler_add_network(DBusMessage *message,
  1297. struct wpa_supplicant *wpa_s)
  1298. {
  1299. DBusMessage *reply = NULL;
  1300. DBusMessageIter iter;
  1301. struct wpa_ssid *ssid = NULL;
  1302. char path_buf[WPAS_DBUS_OBJECT_PATH_MAX], *path = path_buf;
  1303. DBusError error;
  1304. dbus_message_iter_init(message, &iter);
  1305. if (wpa_s->dbus_new_path)
  1306. ssid = wpa_config_add_network(wpa_s->conf);
  1307. if (ssid == NULL) {
  1308. wpa_printf(MSG_ERROR, "%s[dbus]: can't add new interface.",
  1309. __func__);
  1310. reply = wpas_dbus_error_unknown_error(
  1311. message,
  1312. "wpa_supplicant could not add a network on this interface.");
  1313. goto err;
  1314. }
  1315. wpas_notify_network_added(wpa_s, ssid);
  1316. ssid->disabled = 1;
  1317. wpa_config_set_network_defaults(ssid);
  1318. dbus_error_init(&error);
  1319. if (!set_network_properties(wpa_s, ssid, &iter, &error)) {
  1320. wpa_printf(MSG_DEBUG,
  1321. "%s[dbus]: control interface couldn't set network properties",
  1322. __func__);
  1323. reply = wpas_dbus_reply_new_from_error(message, &error,
  1324. DBUS_ERROR_INVALID_ARGS,
  1325. "Failed to add network");
  1326. dbus_error_free(&error);
  1327. goto err;
  1328. }
  1329. /* Construct the object path for this network. */
  1330. os_snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
  1331. "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%d",
  1332. wpa_s->dbus_new_path, ssid->id);
  1333. reply = dbus_message_new_method_return(message);
  1334. if (reply == NULL) {
  1335. reply = wpas_dbus_error_no_memory(message);
  1336. goto err;
  1337. }
  1338. if (!dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path,
  1339. DBUS_TYPE_INVALID)) {
  1340. dbus_message_unref(reply);
  1341. reply = wpas_dbus_error_no_memory(message);
  1342. goto err;
  1343. }
  1344. return reply;
  1345. err:
  1346. if (ssid) {
  1347. wpas_notify_network_removed(wpa_s, ssid);
  1348. wpa_config_remove_network(wpa_s->conf, ssid->id);
  1349. }
  1350. return reply;
  1351. }
  1352. /**
  1353. * wpas_dbus_handler_reassociate - Reassociate
  1354. * @message: Pointer to incoming dbus message
  1355. * @wpa_s: wpa_supplicant structure for a network interface
  1356. * Returns: InterfaceDisabled DBus error message if disabled
  1357. * or NULL otherwise.
  1358. *
  1359. * Handler function for "Reassociate" method call of network interface.
  1360. */
  1361. DBusMessage * wpas_dbus_handler_reassociate(DBusMessage *message,
  1362. struct wpa_supplicant *wpa_s)
  1363. {
  1364. if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED) {
  1365. wpas_request_connection(wpa_s);
  1366. return NULL;
  1367. }
  1368. return dbus_message_new_error(message, WPAS_DBUS_ERROR_IFACE_DISABLED,
  1369. "This interface is disabled");
  1370. }
  1371. /**
  1372. * wpas_dbus_handler_expect_disconnect - ExpectDisconnect
  1373. * @message: Pointer to incoming dbus message
  1374. * @global: %wpa_supplicant global data structure
  1375. * Returns: NULL
  1376. *
  1377. * Handler function for notifying system there will be a expected disconnect.
  1378. * This will prevent wpa_supplicant from adding blacklists upon next disconnect..
  1379. */
  1380. DBusMessage * wpas_dbus_handler_expect_disconnect(DBusMessage *message,
  1381. struct wpa_global *global)
  1382. {
  1383. struct wpa_supplicant *wpa_s = global->ifaces;
  1384. for (; wpa_s; wpa_s = wpa_s->next)
  1385. if (wpa_s->wpa_state >= WPA_ASSOCIATED)
  1386. wpa_s->own_disconnect_req = 1;
  1387. return NULL;
  1388. }
  1389. /**
  1390. * wpas_dbus_handler_reattach - Reattach to current AP
  1391. * @message: Pointer to incoming dbus message
  1392. * @wpa_s: wpa_supplicant structure for a network interface
  1393. * Returns: NotConnected DBus error message if not connected
  1394. * or NULL otherwise.
  1395. *
  1396. * Handler function for "Reattach" method call of network interface.
  1397. */
  1398. DBusMessage * wpas_dbus_handler_reattach(DBusMessage *message,
  1399. struct wpa_supplicant *wpa_s)
  1400. {
  1401. if (wpa_s->current_ssid != NULL) {
  1402. wpa_s->reattach = 1;
  1403. wpas_request_connection(wpa_s);
  1404. return NULL;
  1405. }
  1406. return dbus_message_new_error(message, WPAS_DBUS_ERROR_NOT_CONNECTED,
  1407. "This interface is not connected");
  1408. }
  1409. /**
  1410. * wpas_dbus_handler_reconnect - Reconnect if disconnected
  1411. * @message: Pointer to incoming dbus message
  1412. * @wpa_s: wpa_supplicant structure for a network interface
  1413. * Returns: InterfaceDisabled DBus error message if disabled
  1414. * or NULL otherwise.
  1415. *
  1416. * Handler function for "Reconnect" method call of network interface.
  1417. */
  1418. DBusMessage * wpas_dbus_handler_reconnect(DBusMessage *message,
  1419. struct wpa_supplicant *wpa_s)
  1420. {
  1421. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  1422. return dbus_message_new_error(message,
  1423. WPAS_DBUS_ERROR_IFACE_DISABLED,
  1424. "This interface is disabled");
  1425. }
  1426. if (wpa_s->disconnected)
  1427. wpas_request_connection(wpa_s);
  1428. return NULL;
  1429. }
  1430. /**
  1431. * wpas_dbus_handler_remove_network - Remove a configured network
  1432. * @message: Pointer to incoming dbus message
  1433. * @wpa_s: wpa_supplicant structure for a network interface
  1434. * Returns: NULL on success or dbus error on failure
  1435. *
  1436. * Handler function for "RemoveNetwork" method call of a network interface.
  1437. */
  1438. DBusMessage * wpas_dbus_handler_remove_network(DBusMessage *message,
  1439. struct wpa_supplicant *wpa_s)
  1440. {
  1441. DBusMessage *reply = NULL;
  1442. const char *op;
  1443. char *iface, *net_id;
  1444. int id;
  1445. struct wpa_ssid *ssid;
  1446. int was_disabled;
  1447. dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &op,
  1448. DBUS_TYPE_INVALID);
  1449. /* Extract the network ID and ensure the network */
  1450. /* is actually a child of this interface */
  1451. iface = wpas_dbus_new_decompose_object_path(op,
  1452. WPAS_DBUS_NEW_NETWORKS_PART,
  1453. &net_id);
  1454. if (iface == NULL || net_id == NULL || !wpa_s->dbus_new_path ||
  1455. os_strcmp(iface, wpa_s->dbus_new_path) != 0) {
  1456. reply = wpas_dbus_error_invalid_args(message, op);
  1457. goto out;
  1458. }
  1459. errno = 0;
  1460. id = strtoul(net_id, NULL, 10);
  1461. if (errno != 0) {
  1462. reply = wpas_dbus_error_invalid_args(message, op);
  1463. goto out;
  1464. }
  1465. ssid = wpa_config_get_network(wpa_s->conf, id);
  1466. if (ssid == NULL) {
  1467. reply = wpas_dbus_error_network_unknown(message);
  1468. goto out;
  1469. }
  1470. was_disabled = ssid->disabled;
  1471. wpas_notify_network_removed(wpa_s, ssid);
  1472. if (ssid == wpa_s->current_ssid)
  1473. wpa_supplicant_deauthenticate(wpa_s,
  1474. WLAN_REASON_DEAUTH_LEAVING);
  1475. else if (!was_disabled && wpa_s->sched_scanning) {
  1476. wpa_printf(MSG_DEBUG,
  1477. "Stop ongoing sched_scan to remove network from filters");
  1478. wpa_supplicant_cancel_sched_scan(wpa_s);
  1479. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1480. }
  1481. if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
  1482. wpa_printf(MSG_ERROR,
  1483. "%s[dbus]: error occurred when removing network %d",
  1484. __func__, id);
  1485. reply = wpas_dbus_error_unknown_error(
  1486. message,
  1487. "error removing the specified network on is interface.");
  1488. goto out;
  1489. }
  1490. out:
  1491. os_free(iface);
  1492. return reply;
  1493. }
  1494. static void remove_network(void *arg, struct wpa_ssid *ssid)
  1495. {
  1496. struct wpa_supplicant *wpa_s = arg;
  1497. wpas_notify_network_removed(wpa_s, ssid);
  1498. if (wpa_config_remove_network(wpa_s->conf, ssid->id) < 0) {
  1499. wpa_printf(MSG_ERROR,
  1500. "%s[dbus]: error occurred when removing network %d",
  1501. __func__, ssid->id);
  1502. return;
  1503. }
  1504. if (ssid == wpa_s->current_ssid)
  1505. wpa_supplicant_deauthenticate(wpa_s,
  1506. WLAN_REASON_DEAUTH_LEAVING);
  1507. }
  1508. /**
  1509. * wpas_dbus_handler_remove_all_networks - Remove all configured networks
  1510. * @message: Pointer to incoming dbus message
  1511. * @wpa_s: wpa_supplicant structure for a network interface
  1512. * Returns: NULL on success or dbus error on failure
  1513. *
  1514. * Handler function for "RemoveAllNetworks" method call of a network interface.
  1515. */
  1516. DBusMessage * wpas_dbus_handler_remove_all_networks(
  1517. DBusMessage *message, struct wpa_supplicant *wpa_s)
  1518. {
  1519. if (wpa_s->sched_scanning)
  1520. wpa_supplicant_cancel_sched_scan(wpa_s);
  1521. /* NB: could check for failure and return an error */
  1522. wpa_config_foreach_network(wpa_s->conf, remove_network, wpa_s);
  1523. return NULL;
  1524. }
  1525. /**
  1526. * wpas_dbus_handler_select_network - Attempt association with a network
  1527. * @message: Pointer to incoming dbus message
  1528. * @wpa_s: wpa_supplicant structure for a network interface
  1529. * Returns: NULL on success or dbus error on failure
  1530. *
  1531. * Handler function for "SelectNetwork" method call of network interface.
  1532. */
  1533. DBusMessage * wpas_dbus_handler_select_network(DBusMessage *message,
  1534. struct wpa_supplicant *wpa_s)
  1535. {
  1536. DBusMessage *reply = NULL;
  1537. const char *op;
  1538. char *iface, *net_id;
  1539. int id;
  1540. struct wpa_ssid *ssid;
  1541. dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &op,
  1542. DBUS_TYPE_INVALID);
  1543. /* Extract the network ID and ensure the network */
  1544. /* is actually a child of this interface */
  1545. iface = wpas_dbus_new_decompose_object_path(op,
  1546. WPAS_DBUS_NEW_NETWORKS_PART,
  1547. &net_id);
  1548. if (iface == NULL || net_id == NULL || !wpa_s->dbus_new_path ||
  1549. os_strcmp(iface, wpa_s->dbus_new_path) != 0) {
  1550. reply = wpas_dbus_error_invalid_args(message, op);
  1551. goto out;
  1552. }
  1553. errno = 0;
  1554. id = strtoul(net_id, NULL, 10);
  1555. if (errno != 0) {
  1556. reply = wpas_dbus_error_invalid_args(message, op);
  1557. goto out;
  1558. }
  1559. ssid = wpa_config_get_network(wpa_s->conf, id);
  1560. if (ssid == NULL) {
  1561. reply = wpas_dbus_error_network_unknown(message);
  1562. goto out;
  1563. }
  1564. /* Finally, associate with the network */
  1565. wpa_supplicant_select_network(wpa_s, ssid);
  1566. out:
  1567. os_free(iface);
  1568. return reply;
  1569. }
  1570. /**
  1571. * wpas_dbus_handler_network_reply - Reply to a NetworkRequest signal
  1572. * @message: Pointer to incoming dbus message
  1573. * @wpa_s: wpa_supplicant structure for a network interface
  1574. * Returns: NULL on success or dbus error on failure
  1575. *
  1576. * Handler function for "NetworkReply" method call of network interface.
  1577. */
  1578. DBusMessage * wpas_dbus_handler_network_reply(DBusMessage *message,
  1579. struct wpa_supplicant *wpa_s)
  1580. {
  1581. #ifdef IEEE8021X_EAPOL
  1582. DBusMessage *reply = NULL;
  1583. const char *op, *field, *value;
  1584. char *iface, *net_id;
  1585. int id;
  1586. struct wpa_ssid *ssid;
  1587. if (!dbus_message_get_args(message, NULL,
  1588. DBUS_TYPE_OBJECT_PATH, &op,
  1589. DBUS_TYPE_STRING, &field,
  1590. DBUS_TYPE_STRING, &value,
  1591. DBUS_TYPE_INVALID))
  1592. return wpas_dbus_error_invalid_args(message, NULL);
  1593. /* Extract the network ID and ensure the network */
  1594. /* is actually a child of this interface */
  1595. iface = wpas_dbus_new_decompose_object_path(op,
  1596. WPAS_DBUS_NEW_NETWORKS_PART,
  1597. &net_id);
  1598. if (iface == NULL || net_id == NULL || !wpa_s->dbus_new_path ||
  1599. os_strcmp(iface, wpa_s->dbus_new_path) != 0) {
  1600. reply = wpas_dbus_error_invalid_args(message, op);
  1601. goto out;
  1602. }
  1603. errno = 0;
  1604. id = strtoul(net_id, NULL, 10);
  1605. if (errno != 0) {
  1606. reply = wpas_dbus_error_invalid_args(message, net_id);
  1607. goto out;
  1608. }
  1609. ssid = wpa_config_get_network(wpa_s->conf, id);
  1610. if (ssid == NULL) {
  1611. reply = wpas_dbus_error_network_unknown(message);
  1612. goto out;
  1613. }
  1614. if (wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid,
  1615. field, value) < 0)
  1616. reply = wpas_dbus_error_invalid_args(message, field);
  1617. else {
  1618. /* Tell EAP to retry immediately */
  1619. eapol_sm_notify_ctrl_response(wpa_s->eapol);
  1620. }
  1621. out:
  1622. os_free(iface);
  1623. return reply;
  1624. #else /* IEEE8021X_EAPOL */
  1625. wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
  1626. return wpas_dbus_error_unknown_error(message, "802.1X not included");
  1627. #endif /* IEEE8021X_EAPOL */
  1628. }
  1629. #ifndef CONFIG_NO_CONFIG_BLOBS
  1630. /**
  1631. * wpas_dbus_handler_add_blob - Store named binary blob (ie, for certificates)
  1632. * @message: Pointer to incoming dbus message
  1633. * @wpa_s: %wpa_supplicant data structure
  1634. * Returns: A dbus message containing an error on failure or NULL on success
  1635. *
  1636. * Asks wpa_supplicant to internally store a binary blobs.
  1637. */
  1638. DBusMessage * wpas_dbus_handler_add_blob(DBusMessage *message,
  1639. struct wpa_supplicant *wpa_s)
  1640. {
  1641. DBusMessage *reply = NULL;
  1642. DBusMessageIter iter, array_iter;
  1643. char *blob_name;
  1644. u8 *blob_data;
  1645. int blob_len;
  1646. struct wpa_config_blob *blob = NULL;
  1647. dbus_message_iter_init(message, &iter);
  1648. dbus_message_iter_get_basic(&iter, &blob_name);
  1649. if (wpa_config_get_blob(wpa_s->conf, blob_name)) {
  1650. return dbus_message_new_error(message,
  1651. WPAS_DBUS_ERROR_BLOB_EXISTS,
  1652. NULL);
  1653. }
  1654. dbus_message_iter_next(&iter);
  1655. dbus_message_iter_recurse(&iter, &array_iter);
  1656. dbus_message_iter_get_fixed_array(&array_iter, &blob_data, &blob_len);
  1657. blob = os_zalloc(sizeof(*blob));
  1658. if (!blob) {
  1659. reply = wpas_dbus_error_no_memory(message);
  1660. goto err;
  1661. }
  1662. blob->data = os_malloc(blob_len);
  1663. blob->name = os_strdup(blob_name);
  1664. if (!blob->data || !blob->name) {
  1665. reply = wpas_dbus_error_no_memory(message);
  1666. goto err;
  1667. }
  1668. os_memcpy(blob->data, blob_data, blob_len);
  1669. blob->len = blob_len;
  1670. wpa_config_set_blob(wpa_s->conf, blob);
  1671. wpas_notify_blob_added(wpa_s, blob->name);
  1672. return reply;
  1673. err:
  1674. if (blob) {
  1675. os_free(blob->name);
  1676. os_free(blob->data);
  1677. os_free(blob);
  1678. }
  1679. return reply;
  1680. }
  1681. /**
  1682. * wpas_dbus_handler_get_blob - Get named binary blob (ie, for certificates)
  1683. * @message: Pointer to incoming dbus message
  1684. * @wpa_s: %wpa_supplicant data structure
  1685. * Returns: A dbus message containing array of bytes (blob)
  1686. *
  1687. * Gets one wpa_supplicant's binary blobs.
  1688. */
  1689. DBusMessage * wpas_dbus_handler_get_blob(DBusMessage *message,
  1690. struct wpa_supplicant *wpa_s)
  1691. {
  1692. DBusMessage *reply = NULL;
  1693. DBusMessageIter iter, array_iter;
  1694. char *blob_name;
  1695. const struct wpa_config_blob *blob;
  1696. dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &blob_name,
  1697. DBUS_TYPE_INVALID);
  1698. blob = wpa_config_get_blob(wpa_s->conf, blob_name);
  1699. if (!blob) {
  1700. return dbus_message_new_error(message,
  1701. WPAS_DBUS_ERROR_BLOB_UNKNOWN,
  1702. "Blob id not set");
  1703. }
  1704. reply = dbus_message_new_method_return(message);
  1705. if (!reply)
  1706. return wpas_dbus_error_no_memory(message);
  1707. dbus_message_iter_init_append(reply, &iter);
  1708. if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
  1709. DBUS_TYPE_BYTE_AS_STRING,
  1710. &array_iter) ||
  1711. !dbus_message_iter_append_fixed_array(&array_iter, DBUS_TYPE_BYTE,
  1712. &(blob->data), blob->len) ||
  1713. !dbus_message_iter_close_container(&iter, &array_iter)) {
  1714. dbus_message_unref(reply);
  1715. reply = wpas_dbus_error_no_memory(message);
  1716. }
  1717. return reply;
  1718. }
  1719. /**
  1720. * wpas_remove_handler_remove_blob - Remove named binary blob
  1721. * @message: Pointer to incoming dbus message
  1722. * @wpa_s: %wpa_supplicant data structure
  1723. * Returns: NULL on success or dbus error
  1724. *
  1725. * Asks wpa_supplicant to internally remove a binary blobs.
  1726. */
  1727. DBusMessage * wpas_dbus_handler_remove_blob(DBusMessage *message,
  1728. struct wpa_supplicant *wpa_s)
  1729. {
  1730. DBusMessage *reply = NULL;
  1731. char *blob_name;
  1732. dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &blob_name,
  1733. DBUS_TYPE_INVALID);
  1734. if (wpa_config_remove_blob(wpa_s->conf, blob_name)) {
  1735. return dbus_message_new_error(message,
  1736. WPAS_DBUS_ERROR_BLOB_UNKNOWN,
  1737. "Blob id not set");
  1738. }
  1739. wpas_notify_blob_removed(wpa_s, blob_name);
  1740. return reply;
  1741. }
  1742. #endif /* CONFIG_NO_CONFIG_BLOBS */
  1743. /*
  1744. * wpas_dbus_handler_flush_bss - Flush the BSS cache
  1745. * @message: Pointer to incoming dbus message
  1746. * @wpa_s: wpa_supplicant structure for a network interface
  1747. * Returns: NULL
  1748. *
  1749. * Handler function for "FlushBSS" method call of network interface.
  1750. */
  1751. DBusMessage * wpas_dbus_handler_flush_bss(DBusMessage *message,
  1752. struct wpa_supplicant *wpa_s)
  1753. {
  1754. dbus_uint32_t age;
  1755. dbus_message_get_args(message, NULL, DBUS_TYPE_UINT32, &age,
  1756. DBUS_TYPE_INVALID);
  1757. if (age == 0)
  1758. wpa_bss_flush(wpa_s);
  1759. else
  1760. wpa_bss_flush_by_age(wpa_s, age);
  1761. return NULL;
  1762. }
  1763. #ifdef CONFIG_AUTOSCAN
  1764. /**
  1765. * wpas_dbus_handler_autoscan - Set autoscan parameters for the interface
  1766. * @message: Pointer to incoming dbus message
  1767. * @wpa_s: wpa_supplicant structure for a network interface
  1768. * Returns: NULL
  1769. *
  1770. * Handler function for "AutoScan" method call of network interface.
  1771. */
  1772. DBusMessage * wpas_dbus_handler_autoscan(DBusMessage *message,
  1773. struct wpa_supplicant *wpa_s)
  1774. {
  1775. DBusMessage *reply = NULL;
  1776. enum wpa_states state = wpa_s->wpa_state;
  1777. char *arg;
  1778. dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &arg,
  1779. DBUS_TYPE_INVALID);
  1780. if (arg != NULL && os_strlen(arg) > 0) {
  1781. char *tmp;
  1782. tmp = os_strdup(arg);
  1783. if (tmp == NULL) {
  1784. reply = wpas_dbus_error_no_memory(message);
  1785. } else {
  1786. os_free(wpa_s->conf->autoscan);
  1787. wpa_s->conf->autoscan = tmp;
  1788. if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
  1789. autoscan_init(wpa_s, 1);
  1790. else if (state == WPA_SCANNING)
  1791. wpa_supplicant_reinit_autoscan(wpa_s);
  1792. }
  1793. } else if (arg != NULL && os_strlen(arg) == 0) {
  1794. os_free(wpa_s->conf->autoscan);
  1795. wpa_s->conf->autoscan = NULL;
  1796. autoscan_deinit(wpa_s);
  1797. } else
  1798. reply = dbus_message_new_error(message,
  1799. DBUS_ERROR_INVALID_ARGS,
  1800. NULL);
  1801. return reply;
  1802. }
  1803. #endif /* CONFIG_AUTOSCAN */
  1804. /*
  1805. * wpas_dbus_handler_eap_logoff - IEEE 802.1X EAPOL state machine logoff
  1806. * @message: Pointer to incoming dbus message
  1807. * @wpa_s: wpa_supplicant structure for a network interface
  1808. * Returns: NULL
  1809. *
  1810. * Handler function for "EAPLogoff" method call of network interface.
  1811. */
  1812. DBusMessage * wpas_dbus_handler_eap_logoff(DBusMessage *message,
  1813. struct wpa_supplicant *wpa_s)
  1814. {
  1815. eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
  1816. return NULL;
  1817. }
  1818. /*
  1819. * wpas_dbus_handler_eap_logon - IEEE 802.1X EAPOL state machine logon
  1820. * @message: Pointer to incoming dbus message
  1821. * @wpa_s: wpa_supplicant structure for a network interface
  1822. * Returns: NULL
  1823. *
  1824. * Handler function for "EAPLogin" method call of network interface.
  1825. */
  1826. DBusMessage * wpas_dbus_handler_eap_logon(DBusMessage *message,
  1827. struct wpa_supplicant *wpa_s)
  1828. {
  1829. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  1830. return NULL;
  1831. }
  1832. #ifdef CONFIG_TDLS
  1833. static int get_peer_hwaddr_helper(DBusMessage *message, const char *func_name,
  1834. u8 *peer_address, DBusMessage **error)
  1835. {
  1836. const char *peer_string;
  1837. *error = NULL;
  1838. if (!dbus_message_get_args(message, NULL,
  1839. DBUS_TYPE_STRING, &peer_string,
  1840. DBUS_TYPE_INVALID)) {
  1841. *error = wpas_dbus_error_invalid_args(message, NULL);
  1842. return -1;
  1843. }
  1844. if (hwaddr_aton(peer_string, peer_address)) {
  1845. wpa_printf(MSG_DEBUG, "%s: invalid address '%s'",
  1846. func_name, peer_string);
  1847. *error = wpas_dbus_error_invalid_args(
  1848. message, "Invalid hardware address format");
  1849. return -1;
  1850. }
  1851. return 0;
  1852. }
  1853. /*
  1854. * wpas_dbus_handler_tdls_discover - Discover TDLS peer
  1855. * @message: Pointer to incoming dbus message
  1856. * @wpa_s: wpa_supplicant structure for a network interface
  1857. * Returns: NULL indicating success or DBus error message on failure
  1858. *
  1859. * Handler function for "TDLSDiscover" method call of network interface.
  1860. */
  1861. DBusMessage * wpas_dbus_handler_tdls_discover(DBusMessage *message,
  1862. struct wpa_supplicant *wpa_s)
  1863. {
  1864. u8 peer[ETH_ALEN];
  1865. DBusMessage *error_reply;
  1866. int ret;
  1867. if (get_peer_hwaddr_helper(message, __func__, peer, &error_reply) < 0)
  1868. return error_reply;
  1869. wpa_printf(MSG_DEBUG, "DBUS TDLS_DISCOVER " MACSTR, MAC2STR(peer));
  1870. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  1871. ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
  1872. else
  1873. ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
  1874. if (ret) {
  1875. return wpas_dbus_error_unknown_error(
  1876. message, "error performing TDLS discovery");
  1877. }
  1878. return NULL;
  1879. }
  1880. /*
  1881. * wpas_dbus_handler_tdls_setup - Setup TDLS session
  1882. * @message: Pointer to incoming dbus message
  1883. * @wpa_s: wpa_supplicant structure for a network interface
  1884. * Returns: NULL indicating success or DBus error message on failure
  1885. *
  1886. * Handler function for "TDLSSetup" method call of network interface.
  1887. */
  1888. DBusMessage * wpas_dbus_handler_tdls_setup(DBusMessage *message,
  1889. struct wpa_supplicant *wpa_s)
  1890. {
  1891. u8 peer[ETH_ALEN];
  1892. DBusMessage *error_reply;
  1893. int ret;
  1894. if (get_peer_hwaddr_helper(message, __func__, peer, &error_reply) < 0)
  1895. return error_reply;
  1896. wpa_printf(MSG_DEBUG, "DBUS TDLS_SETUP " MACSTR, MAC2STR(peer));
  1897. wpa_tdls_remove(wpa_s->wpa, peer);
  1898. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  1899. ret = wpa_tdls_start(wpa_s->wpa, peer);
  1900. else
  1901. ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
  1902. if (ret) {
  1903. return wpas_dbus_error_unknown_error(
  1904. message, "error performing TDLS setup");
  1905. }
  1906. return NULL;
  1907. }
  1908. /*
  1909. * wpas_dbus_handler_tdls_status - Return TDLS session status
  1910. * @message: Pointer to incoming dbus message
  1911. * @wpa_s: wpa_supplicant structure for a network interface
  1912. * Returns: A string representing the state of the link to this TDLS peer
  1913. *
  1914. * Handler function for "TDLSStatus" method call of network interface.
  1915. */
  1916. DBusMessage * wpas_dbus_handler_tdls_status(DBusMessage *message,
  1917. struct wpa_supplicant *wpa_s)
  1918. {
  1919. u8 peer[ETH_ALEN];
  1920. DBusMessage *reply;
  1921. const char *tdls_status;
  1922. if (get_peer_hwaddr_helper(message, __func__, peer, &reply) < 0)
  1923. return reply;
  1924. wpa_printf(MSG_DEBUG, "DBUS TDLS_STATUS " MACSTR, MAC2STR(peer));
  1925. tdls_status = wpa_tdls_get_link_status(wpa_s->wpa, peer);
  1926. reply = dbus_message_new_method_return(message);
  1927. dbus_message_append_args(reply, DBUS_TYPE_STRING,
  1928. &tdls_status, DBUS_TYPE_INVALID);
  1929. return reply;
  1930. }
  1931. /*
  1932. * wpas_dbus_handler_tdls_teardown - Teardown TDLS session
  1933. * @message: Pointer to incoming dbus message
  1934. * @wpa_s: wpa_supplicant structure for a network interface
  1935. * Returns: NULL indicating success or DBus error message on failure
  1936. *
  1937. * Handler function for "TDLSTeardown" method call of network interface.
  1938. */
  1939. DBusMessage * wpas_dbus_handler_tdls_teardown(DBusMessage *message,
  1940. struct wpa_supplicant *wpa_s)
  1941. {
  1942. u8 peer[ETH_ALEN];
  1943. DBusMessage *error_reply;
  1944. int ret;
  1945. if (get_peer_hwaddr_helper(message, __func__, peer, &error_reply) < 0)
  1946. return error_reply;
  1947. wpa_printf(MSG_DEBUG, "DBUS TDLS_TEARDOWN " MACSTR, MAC2STR(peer));
  1948. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  1949. ret = wpa_tdls_teardown_link(
  1950. wpa_s->wpa, peer,
  1951. WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
  1952. else
  1953. ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
  1954. if (ret) {
  1955. return wpas_dbus_error_unknown_error(
  1956. message, "error performing TDLS teardown");
  1957. }
  1958. return NULL;
  1959. }
  1960. #endif /* CONFIG_TDLS */
  1961. /**
  1962. * wpas_dbus_handler_set_pkcs11_engine_and_module_path - Set PKCS #11 engine and module path
  1963. * @message: Pointer to incoming dbus message
  1964. * @wpa_s: %wpa_supplicant data structure
  1965. * Returns: A dbus message containing an error on failure or NULL on success
  1966. *
  1967. * Sets the PKCS #11 engine and module path.
  1968. */
  1969. DBusMessage * wpas_dbus_handler_set_pkcs11_engine_and_module_path(
  1970. DBusMessage *message, struct wpa_supplicant *wpa_s)
  1971. {
  1972. DBusMessageIter iter;
  1973. char *value = NULL;
  1974. char *pkcs11_engine_path = NULL;
  1975. char *pkcs11_module_path = NULL;
  1976. dbus_message_iter_init(message, &iter);
  1977. dbus_message_iter_get_basic(&iter, &value);
  1978. if (value == NULL) {
  1979. return dbus_message_new_error(
  1980. message, DBUS_ERROR_INVALID_ARGS,
  1981. "Invalid pkcs11_engine_path argument");
  1982. }
  1983. /* Empty path defaults to NULL */
  1984. if (os_strlen(value))
  1985. pkcs11_engine_path = value;
  1986. dbus_message_iter_next(&iter);
  1987. dbus_message_iter_get_basic(&iter, &value);
  1988. if (value == NULL) {
  1989. os_free(pkcs11_engine_path);
  1990. return dbus_message_new_error(
  1991. message, DBUS_ERROR_INVALID_ARGS,
  1992. "Invalid pkcs11_module_path argument");
  1993. }
  1994. /* Empty path defaults to NULL */
  1995. if (os_strlen(value))
  1996. pkcs11_module_path = value;
  1997. if (wpas_set_pkcs11_engine_and_module_path(wpa_s, pkcs11_engine_path,
  1998. pkcs11_module_path))
  1999. return dbus_message_new_error(
  2000. message, DBUS_ERROR_FAILED,
  2001. "Reinit of the EAPOL state machine with the new PKCS #11 engine and module path failed.");
  2002. if (wpa_s->dbus_new_path) {
  2003. wpa_dbus_mark_property_changed(
  2004. wpa_s->global->dbus, wpa_s->dbus_new_path,
  2005. WPAS_DBUS_NEW_IFACE_INTERFACE, "PKCS11EnginePath");
  2006. wpa_dbus_mark_property_changed(
  2007. wpa_s->global->dbus, wpa_s->dbus_new_path,
  2008. WPAS_DBUS_NEW_IFACE_INTERFACE, "PKCS11ModulePath");
  2009. }
  2010. return NULL;
  2011. }
  2012. /**
  2013. * wpas_dbus_getter_capabilities - Return interface capabilities
  2014. * @iter: Pointer to incoming dbus message iter
  2015. * @error: Location to store error on failure
  2016. * @user_data: Function specific data
  2017. * Returns: TRUE on success, FALSE on failure
  2018. *
  2019. * Getter for "Capabilities" property of an interface.
  2020. */
  2021. dbus_bool_t wpas_dbus_getter_capabilities(DBusMessageIter *iter,
  2022. DBusError *error, void *user_data)
  2023. {
  2024. struct wpa_supplicant *wpa_s = user_data;
  2025. struct wpa_driver_capa capa;
  2026. int res;
  2027. DBusMessageIter iter_dict, iter_dict_entry, iter_dict_val, iter_array,
  2028. variant_iter;
  2029. const char *scans[] = { "active", "passive", "ssid" };
  2030. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
  2031. "a{sv}", &variant_iter) ||
  2032. !wpa_dbus_dict_open_write(&variant_iter, &iter_dict))
  2033. goto nomem;
  2034. res = wpa_drv_get_capa(wpa_s, &capa);
  2035. /***** pairwise cipher */
  2036. if (res < 0) {
  2037. const char *args[] = {"ccmp", "tkip", "none"};
  2038. if (!wpa_dbus_dict_append_string_array(
  2039. &iter_dict, "Pairwise", args,
  2040. ARRAY_SIZE(args)))
  2041. goto nomem;
  2042. } else {
  2043. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Pairwise",
  2044. &iter_dict_entry,
  2045. &iter_dict_val,
  2046. &iter_array) ||
  2047. ((capa.enc & WPA_DRIVER_CAPA_ENC_CCMP_256) &&
  2048. !wpa_dbus_dict_string_array_add_element(
  2049. &iter_array, "ccmp-256")) ||
  2050. ((capa.enc & WPA_DRIVER_CAPA_ENC_GCMP_256) &&
  2051. !wpa_dbus_dict_string_array_add_element(
  2052. &iter_array, "gcmp-256")) ||
  2053. ((capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) &&
  2054. !wpa_dbus_dict_string_array_add_element(
  2055. &iter_array, "ccmp")) ||
  2056. ((capa.enc & WPA_DRIVER_CAPA_ENC_GCMP) &&
  2057. !wpa_dbus_dict_string_array_add_element(
  2058. &iter_array, "gcmp")) ||
  2059. ((capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) &&
  2060. !wpa_dbus_dict_string_array_add_element(
  2061. &iter_array, "tkip")) ||
  2062. ((capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) &&
  2063. !wpa_dbus_dict_string_array_add_element(
  2064. &iter_array, "none")) ||
  2065. !wpa_dbus_dict_end_string_array(&iter_dict,
  2066. &iter_dict_entry,
  2067. &iter_dict_val,
  2068. &iter_array))
  2069. goto nomem;
  2070. }
  2071. /***** group cipher */
  2072. if (res < 0) {
  2073. const char *args[] = {
  2074. "ccmp", "tkip", "wep104", "wep40"
  2075. };
  2076. if (!wpa_dbus_dict_append_string_array(
  2077. &iter_dict, "Group", args,
  2078. ARRAY_SIZE(args)))
  2079. goto nomem;
  2080. } else {
  2081. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Group",
  2082. &iter_dict_entry,
  2083. &iter_dict_val,
  2084. &iter_array) ||
  2085. ((capa.enc & WPA_DRIVER_CAPA_ENC_CCMP_256) &&
  2086. !wpa_dbus_dict_string_array_add_element(
  2087. &iter_array, "ccmp-256")) ||
  2088. ((capa.enc & WPA_DRIVER_CAPA_ENC_GCMP_256) &&
  2089. !wpa_dbus_dict_string_array_add_element(
  2090. &iter_array, "gcmp-256")) ||
  2091. ((capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) &&
  2092. !wpa_dbus_dict_string_array_add_element(
  2093. &iter_array, "ccmp")) ||
  2094. ((capa.enc & WPA_DRIVER_CAPA_ENC_GCMP) &&
  2095. !wpa_dbus_dict_string_array_add_element(
  2096. &iter_array, "gcmp")) ||
  2097. ((capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) &&
  2098. !wpa_dbus_dict_string_array_add_element(
  2099. &iter_array, "tkip")) ||
  2100. ((capa.enc & WPA_DRIVER_CAPA_ENC_WEP104) &&
  2101. !wpa_dbus_dict_string_array_add_element(
  2102. &iter_array, "wep104")) ||
  2103. ((capa.enc & WPA_DRIVER_CAPA_ENC_WEP40) &&
  2104. !wpa_dbus_dict_string_array_add_element(
  2105. &iter_array, "wep40")) ||
  2106. !wpa_dbus_dict_end_string_array(&iter_dict,
  2107. &iter_dict_entry,
  2108. &iter_dict_val,
  2109. &iter_array))
  2110. goto nomem;
  2111. }
  2112. /***** key management */
  2113. if (res < 0) {
  2114. const char *args[] = {
  2115. "wpa-psk", "wpa-eap", "ieee8021x", "wpa-none",
  2116. #ifdef CONFIG_WPS
  2117. "wps",
  2118. #endif /* CONFIG_WPS */
  2119. "none"
  2120. };
  2121. if (!wpa_dbus_dict_append_string_array(
  2122. &iter_dict, "KeyMgmt", args,
  2123. ARRAY_SIZE(args)))
  2124. goto nomem;
  2125. } else {
  2126. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "KeyMgmt",
  2127. &iter_dict_entry,
  2128. &iter_dict_val,
  2129. &iter_array) ||
  2130. !wpa_dbus_dict_string_array_add_element(&iter_array,
  2131. "none") ||
  2132. !wpa_dbus_dict_string_array_add_element(&iter_array,
  2133. "ieee8021x"))
  2134. goto nomem;
  2135. if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2136. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  2137. if (!wpa_dbus_dict_string_array_add_element(
  2138. &iter_array, "wpa-eap") ||
  2139. ((capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT) &&
  2140. !wpa_dbus_dict_string_array_add_element(
  2141. &iter_array, "wpa-ft-eap")))
  2142. goto nomem;
  2143. /* TODO: Ensure that driver actually supports sha256 encryption. */
  2144. #ifdef CONFIG_IEEE80211W
  2145. if (!wpa_dbus_dict_string_array_add_element(
  2146. &iter_array, "wpa-eap-sha256"))
  2147. goto nomem;
  2148. #endif /* CONFIG_IEEE80211W */
  2149. }
  2150. if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  2151. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  2152. if (!wpa_dbus_dict_string_array_add_element(
  2153. &iter_array, "wpa-psk") ||
  2154. ((capa.key_mgmt &
  2155. WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK) &&
  2156. !wpa_dbus_dict_string_array_add_element(
  2157. &iter_array, "wpa-ft-psk")))
  2158. goto nomem;
  2159. /* TODO: Ensure that driver actually supports sha256 encryption. */
  2160. #ifdef CONFIG_IEEE80211W
  2161. if (!wpa_dbus_dict_string_array_add_element(
  2162. &iter_array, "wpa-psk-sha256"))
  2163. goto nomem;
  2164. #endif /* CONFIG_IEEE80211W */
  2165. }
  2166. if ((capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) &&
  2167. !wpa_dbus_dict_string_array_add_element(&iter_array,
  2168. "wpa-none"))
  2169. goto nomem;
  2170. #ifdef CONFIG_WPS
  2171. if (!wpa_dbus_dict_string_array_add_element(&iter_array,
  2172. "wps"))
  2173. goto nomem;
  2174. #endif /* CONFIG_WPS */
  2175. if (!wpa_dbus_dict_end_string_array(&iter_dict,
  2176. &iter_dict_entry,
  2177. &iter_dict_val,
  2178. &iter_array))
  2179. goto nomem;
  2180. }
  2181. /***** WPA protocol */
  2182. if (res < 0) {
  2183. const char *args[] = { "rsn", "wpa" };
  2184. if (!wpa_dbus_dict_append_string_array(
  2185. &iter_dict, "Protocol", args,
  2186. ARRAY_SIZE(args)))
  2187. goto nomem;
  2188. } else {
  2189. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Protocol",
  2190. &iter_dict_entry,
  2191. &iter_dict_val,
  2192. &iter_array) ||
  2193. ((capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  2194. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) &&
  2195. !wpa_dbus_dict_string_array_add_element(
  2196. &iter_array, "rsn")) ||
  2197. ((capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2198. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) &&
  2199. !wpa_dbus_dict_string_array_add_element(
  2200. &iter_array, "wpa")) ||
  2201. !wpa_dbus_dict_end_string_array(&iter_dict,
  2202. &iter_dict_entry,
  2203. &iter_dict_val,
  2204. &iter_array))
  2205. goto nomem;
  2206. }
  2207. /***** auth alg */
  2208. if (res < 0) {
  2209. const char *args[] = { "open", "shared", "leap" };
  2210. if (!wpa_dbus_dict_append_string_array(
  2211. &iter_dict, "AuthAlg", args,
  2212. ARRAY_SIZE(args)))
  2213. goto nomem;
  2214. } else {
  2215. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "AuthAlg",
  2216. &iter_dict_entry,
  2217. &iter_dict_val,
  2218. &iter_array))
  2219. goto nomem;
  2220. if (((capa.auth & WPA_DRIVER_AUTH_OPEN) &&
  2221. !wpa_dbus_dict_string_array_add_element(
  2222. &iter_array, "open")) ||
  2223. ((capa.auth & WPA_DRIVER_AUTH_SHARED) &&
  2224. !wpa_dbus_dict_string_array_add_element(
  2225. &iter_array, "shared")) ||
  2226. ((capa.auth & WPA_DRIVER_AUTH_LEAP) &&
  2227. !wpa_dbus_dict_string_array_add_element(
  2228. &iter_array, "leap")) ||
  2229. !wpa_dbus_dict_end_string_array(&iter_dict,
  2230. &iter_dict_entry,
  2231. &iter_dict_val,
  2232. &iter_array))
  2233. goto nomem;
  2234. }
  2235. /***** Scan */
  2236. if (!wpa_dbus_dict_append_string_array(&iter_dict, "Scan", scans,
  2237. ARRAY_SIZE(scans)))
  2238. goto nomem;
  2239. /***** Modes */
  2240. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Modes",
  2241. &iter_dict_entry,
  2242. &iter_dict_val,
  2243. &iter_array) ||
  2244. !wpa_dbus_dict_string_array_add_element(
  2245. &iter_array, "infrastructure") ||
  2246. !wpa_dbus_dict_string_array_add_element(
  2247. &iter_array, "ad-hoc") ||
  2248. (res >= 0 && (capa.flags & WPA_DRIVER_FLAGS_AP) &&
  2249. !wpa_dbus_dict_string_array_add_element(
  2250. &iter_array, "ap")) ||
  2251. (res >= 0 && (capa.flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
  2252. !wpa_dbus_dict_string_array_add_element(
  2253. &iter_array, "p2p")) ||
  2254. !wpa_dbus_dict_end_string_array(&iter_dict,
  2255. &iter_dict_entry,
  2256. &iter_dict_val,
  2257. &iter_array))
  2258. goto nomem;
  2259. /***** Modes end */
  2260. if (res >= 0) {
  2261. dbus_int32_t max_scan_ssid = capa.max_scan_ssids;
  2262. if (!wpa_dbus_dict_append_int32(&iter_dict, "MaxScanSSID",
  2263. max_scan_ssid))
  2264. goto nomem;
  2265. }
  2266. if (!wpa_dbus_dict_close_write(&variant_iter, &iter_dict) ||
  2267. !dbus_message_iter_close_container(iter, &variant_iter))
  2268. goto nomem;
  2269. return TRUE;
  2270. nomem:
  2271. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  2272. return FALSE;
  2273. }
  2274. /**
  2275. * wpas_dbus_getter_state - Get interface state
  2276. * @iter: Pointer to incoming dbus message iter
  2277. * @error: Location to store error on failure
  2278. * @user_data: Function specific data
  2279. * Returns: TRUE on success, FALSE on failure
  2280. *
  2281. * Getter for "State" property.
  2282. */
  2283. dbus_bool_t wpas_dbus_getter_state(DBusMessageIter *iter, DBusError *error,
  2284. void *user_data)
  2285. {
  2286. struct wpa_supplicant *wpa_s = user_data;
  2287. const char *str_state;
  2288. char *state_ls, *tmp;
  2289. dbus_bool_t success = FALSE;
  2290. str_state = wpa_supplicant_state_txt(wpa_s->wpa_state);
  2291. /* make state string lowercase to fit new DBus API convention
  2292. */
  2293. state_ls = tmp = os_strdup(str_state);
  2294. if (!tmp) {
  2295. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  2296. return FALSE;
  2297. }
  2298. while (*tmp) {
  2299. *tmp = tolower(*tmp);
  2300. tmp++;
  2301. }
  2302. success = wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  2303. &state_ls, error);
  2304. os_free(state_ls);
  2305. return success;
  2306. }
  2307. /**
  2308. * wpas_dbus_new_iface_get_scanning - Get interface scanning state
  2309. * @iter: Pointer to incoming dbus message iter
  2310. * @error: Location to store error on failure
  2311. * @user_data: Function specific data
  2312. * Returns: TRUE on success, FALSE on failure
  2313. *
  2314. * Getter for "scanning" property.
  2315. */
  2316. dbus_bool_t wpas_dbus_getter_scanning(DBusMessageIter *iter, DBusError *error,
  2317. void *user_data)
  2318. {
  2319. struct wpa_supplicant *wpa_s = user_data;
  2320. dbus_bool_t scanning = wpa_s->scanning ? TRUE : FALSE;
  2321. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_BOOLEAN,
  2322. &scanning, error);
  2323. }
  2324. /**
  2325. * wpas_dbus_getter_ap_scan - Control roaming mode
  2326. * @iter: Pointer to incoming dbus message iter
  2327. * @error: Location to store error on failure
  2328. * @user_data: Function specific data
  2329. * Returns: TRUE on success, FALSE on failure
  2330. *
  2331. * Getter function for "ApScan" property.
  2332. */
  2333. dbus_bool_t wpas_dbus_getter_ap_scan(DBusMessageIter *iter, DBusError *error,
  2334. void *user_data)
  2335. {
  2336. struct wpa_supplicant *wpa_s = user_data;
  2337. dbus_uint32_t ap_scan = wpa_s->conf->ap_scan;
  2338. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_UINT32,
  2339. &ap_scan, error);
  2340. }
  2341. /**
  2342. * wpas_dbus_setter_ap_scan - Control roaming mode
  2343. * @iter: Pointer to incoming dbus message iter
  2344. * @error: Location to store error on failure
  2345. * @user_data: Function specific data
  2346. * Returns: TRUE on success, FALSE on failure
  2347. *
  2348. * Setter function for "ApScan" property.
  2349. */
  2350. dbus_bool_t wpas_dbus_setter_ap_scan(DBusMessageIter *iter, DBusError *error,
  2351. void *user_data)
  2352. {
  2353. struct wpa_supplicant *wpa_s = user_data;
  2354. dbus_uint32_t ap_scan;
  2355. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_UINT32,
  2356. &ap_scan))
  2357. return FALSE;
  2358. if (wpa_supplicant_set_ap_scan(wpa_s, ap_scan)) {
  2359. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  2360. "ap_scan must be 0, 1, or 2");
  2361. return FALSE;
  2362. }
  2363. return TRUE;
  2364. }
  2365. /**
  2366. * wpas_dbus_getter_fast_reauth - Control fast
  2367. * reauthentication (TLS session resumption)
  2368. * @iter: Pointer to incoming dbus message iter
  2369. * @error: Location to store error on failure
  2370. * @user_data: Function specific data
  2371. * Returns: TRUE on success, FALSE on failure
  2372. *
  2373. * Getter function for "FastReauth" property.
  2374. */
  2375. dbus_bool_t wpas_dbus_getter_fast_reauth(DBusMessageIter *iter,
  2376. DBusError *error,
  2377. void *user_data)
  2378. {
  2379. struct wpa_supplicant *wpa_s = user_data;
  2380. dbus_bool_t fast_reauth = wpa_s->conf->fast_reauth ? TRUE : FALSE;
  2381. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_BOOLEAN,
  2382. &fast_reauth, error);
  2383. }
  2384. /**
  2385. * wpas_dbus_setter_fast_reauth - Control fast
  2386. * reauthentication (TLS session resumption)
  2387. * @iter: Pointer to incoming dbus message iter
  2388. * @error: Location to store error on failure
  2389. * @user_data: Function specific data
  2390. * Returns: TRUE on success, FALSE on failure
  2391. *
  2392. * Setter function for "FastReauth" property.
  2393. */
  2394. dbus_bool_t wpas_dbus_setter_fast_reauth(DBusMessageIter *iter,
  2395. DBusError *error,
  2396. void *user_data)
  2397. {
  2398. struct wpa_supplicant *wpa_s = user_data;
  2399. dbus_bool_t fast_reauth;
  2400. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_BOOLEAN,
  2401. &fast_reauth))
  2402. return FALSE;
  2403. wpa_s->conf->fast_reauth = fast_reauth;
  2404. return TRUE;
  2405. }
  2406. /**
  2407. * wpas_dbus_getter_disconnect_reason - Get most recent reason for disconnect
  2408. * @iter: Pointer to incoming dbus message iter
  2409. * @error: Location to store error on failure
  2410. * @user_data: Function specific data
  2411. * Returns: TRUE on success, FALSE on failure
  2412. *
  2413. * Getter for "DisconnectReason" property. The reason is negative if it is
  2414. * locally generated.
  2415. */
  2416. dbus_bool_t wpas_dbus_getter_disconnect_reason(DBusMessageIter *iter,
  2417. DBusError *error,
  2418. void *user_data)
  2419. {
  2420. struct wpa_supplicant *wpa_s = user_data;
  2421. dbus_int32_t reason = wpa_s->disconnect_reason;
  2422. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_INT32,
  2423. &reason, error);
  2424. }
  2425. /**
  2426. * wpas_dbus_getter_bss_expire_age - Get BSS entry expiration age
  2427. * @iter: Pointer to incoming dbus message iter
  2428. * @error: Location to store error on failure
  2429. * @user_data: Function specific data
  2430. * Returns: TRUE on success, FALSE on failure
  2431. *
  2432. * Getter function for "BSSExpireAge" property.
  2433. */
  2434. dbus_bool_t wpas_dbus_getter_bss_expire_age(DBusMessageIter *iter,
  2435. DBusError *error,
  2436. void *user_data)
  2437. {
  2438. struct wpa_supplicant *wpa_s = user_data;
  2439. dbus_uint32_t expire_age = wpa_s->conf->bss_expiration_age;
  2440. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_UINT32,
  2441. &expire_age, error);
  2442. }
  2443. /**
  2444. * wpas_dbus_setter_bss_expire_age - Control BSS entry expiration age
  2445. * @iter: Pointer to incoming dbus message iter
  2446. * @error: Location to store error on failure
  2447. * @user_data: Function specific data
  2448. * Returns: TRUE on success, FALSE on failure
  2449. *
  2450. * Setter function for "BSSExpireAge" property.
  2451. */
  2452. dbus_bool_t wpas_dbus_setter_bss_expire_age(DBusMessageIter *iter,
  2453. DBusError *error,
  2454. void *user_data)
  2455. {
  2456. struct wpa_supplicant *wpa_s = user_data;
  2457. dbus_uint32_t expire_age;
  2458. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_UINT32,
  2459. &expire_age))
  2460. return FALSE;
  2461. if (wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age)) {
  2462. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  2463. "BSSExpireAge must be >= 10");
  2464. return FALSE;
  2465. }
  2466. return TRUE;
  2467. }
  2468. /**
  2469. * wpas_dbus_getter_bss_expire_count - Get BSS entry expiration scan count
  2470. * @iter: Pointer to incoming dbus message iter
  2471. * @error: Location to store error on failure
  2472. * @user_data: Function specific data
  2473. * Returns: TRUE on success, FALSE on failure
  2474. *
  2475. * Getter function for "BSSExpireCount" property.
  2476. */
  2477. dbus_bool_t wpas_dbus_getter_bss_expire_count(DBusMessageIter *iter,
  2478. DBusError *error,
  2479. void *user_data)
  2480. {
  2481. struct wpa_supplicant *wpa_s = user_data;
  2482. dbus_uint32_t expire_count = wpa_s->conf->bss_expiration_scan_count;
  2483. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_UINT32,
  2484. &expire_count, error);
  2485. }
  2486. /**
  2487. * wpas_dbus_setter_bss_expire_count - Control BSS entry expiration scan count
  2488. * @iter: Pointer to incoming dbus message iter
  2489. * @error: Location to store error on failure
  2490. * @user_data: Function specific data
  2491. * Returns: TRUE on success, FALSE on failure
  2492. *
  2493. * Setter function for "BSSExpireCount" property.
  2494. */
  2495. dbus_bool_t wpas_dbus_setter_bss_expire_count(DBusMessageIter *iter,
  2496. DBusError *error,
  2497. void *user_data)
  2498. {
  2499. struct wpa_supplicant *wpa_s = user_data;
  2500. dbus_uint32_t expire_count;
  2501. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_UINT32,
  2502. &expire_count))
  2503. return FALSE;
  2504. if (wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count)) {
  2505. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  2506. "BSSExpireCount must be > 0");
  2507. return FALSE;
  2508. }
  2509. return TRUE;
  2510. }
  2511. /**
  2512. * wpas_dbus_getter_country - Control country code
  2513. * @iter: Pointer to incoming dbus message iter
  2514. * @error: Location to store error on failure
  2515. * @user_data: Function specific data
  2516. * Returns: TRUE on success, FALSE on failure
  2517. *
  2518. * Getter function for "Country" property.
  2519. */
  2520. dbus_bool_t wpas_dbus_getter_country(DBusMessageIter *iter, DBusError *error,
  2521. void *user_data)
  2522. {
  2523. struct wpa_supplicant *wpa_s = user_data;
  2524. char country[3];
  2525. char *str = country;
  2526. country[0] = wpa_s->conf->country[0];
  2527. country[1] = wpa_s->conf->country[1];
  2528. country[2] = '\0';
  2529. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  2530. &str, error);
  2531. }
  2532. /**
  2533. * wpas_dbus_setter_country - Control country code
  2534. * @iter: Pointer to incoming dbus message iter
  2535. * @error: Location to store error on failure
  2536. * @user_data: Function specific data
  2537. * Returns: TRUE on success, FALSE on failure
  2538. *
  2539. * Setter function for "Country" property.
  2540. */
  2541. dbus_bool_t wpas_dbus_setter_country(DBusMessageIter *iter, DBusError *error,
  2542. void *user_data)
  2543. {
  2544. struct wpa_supplicant *wpa_s = user_data;
  2545. const char *country;
  2546. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_STRING,
  2547. &country))
  2548. return FALSE;
  2549. if (!country[0] || !country[1]) {
  2550. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  2551. "invalid country code");
  2552. return FALSE;
  2553. }
  2554. if (wpa_s->drv_priv != NULL && wpa_drv_set_country(wpa_s, country)) {
  2555. wpa_printf(MSG_DEBUG, "Failed to set country");
  2556. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  2557. "failed to set country code");
  2558. return FALSE;
  2559. }
  2560. wpa_s->conf->country[0] = country[0];
  2561. wpa_s->conf->country[1] = country[1];
  2562. return TRUE;
  2563. }
  2564. /**
  2565. * wpas_dbus_getter_scan_interval - Get scan interval
  2566. * @iter: Pointer to incoming dbus message iter
  2567. * @error: Location to store error on failure
  2568. * @user_data: Function specific data
  2569. * Returns: TRUE on success, FALSE on failure
  2570. *
  2571. * Getter function for "ScanInterval" property.
  2572. */
  2573. dbus_bool_t wpas_dbus_getter_scan_interval(DBusMessageIter *iter,
  2574. DBusError *error,
  2575. void *user_data)
  2576. {
  2577. struct wpa_supplicant *wpa_s = user_data;
  2578. dbus_int32_t scan_interval = wpa_s->scan_interval;
  2579. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_INT32,
  2580. &scan_interval, error);
  2581. }
  2582. /**
  2583. * wpas_dbus_setter_scan_interval - Control scan interval
  2584. * @iter: Pointer to incoming dbus message iter
  2585. * @error: Location to store error on failure
  2586. * @user_data: Function specific data
  2587. * Returns: TRUE on success, FALSE on failure
  2588. *
  2589. * Setter function for "ScanInterval" property.
  2590. */
  2591. dbus_bool_t wpas_dbus_setter_scan_interval(DBusMessageIter *iter,
  2592. DBusError *error,
  2593. void *user_data)
  2594. {
  2595. struct wpa_supplicant *wpa_s = user_data;
  2596. dbus_int32_t scan_interval;
  2597. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_INT32,
  2598. &scan_interval))
  2599. return FALSE;
  2600. if (wpa_supplicant_set_scan_interval(wpa_s, scan_interval)) {
  2601. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  2602. "scan_interval must be >= 0");
  2603. return FALSE;
  2604. }
  2605. return TRUE;
  2606. }
  2607. /**
  2608. * wpas_dbus_getter_ifname - Get interface name
  2609. * @iter: Pointer to incoming dbus message iter
  2610. * @error: Location to store error on failure
  2611. * @user_data: Function specific data
  2612. * Returns: TRUE on success, FALSE on failure
  2613. *
  2614. * Getter for "Ifname" property.
  2615. */
  2616. dbus_bool_t wpas_dbus_getter_ifname(DBusMessageIter *iter, DBusError *error,
  2617. void *user_data)
  2618. {
  2619. struct wpa_supplicant *wpa_s = user_data;
  2620. const char *ifname = wpa_s->ifname;
  2621. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  2622. &ifname, error);
  2623. }
  2624. /**
  2625. * wpas_dbus_getter_driver - Get interface name
  2626. * @iter: Pointer to incoming dbus message iter
  2627. * @error: Location to store error on failure
  2628. * @user_data: Function specific data
  2629. * Returns: TRUE on success, FALSE on failure
  2630. *
  2631. * Getter for "Driver" property.
  2632. */
  2633. dbus_bool_t wpas_dbus_getter_driver(DBusMessageIter *iter, DBusError *error,
  2634. void *user_data)
  2635. {
  2636. struct wpa_supplicant *wpa_s = user_data;
  2637. const char *driver;
  2638. if (wpa_s->driver == NULL || wpa_s->driver->name == NULL) {
  2639. wpa_printf(MSG_DEBUG, "%s[dbus]: wpa_s has no driver set",
  2640. __func__);
  2641. dbus_set_error(error, DBUS_ERROR_FAILED, "%s: no driver set",
  2642. __func__);
  2643. return FALSE;
  2644. }
  2645. driver = wpa_s->driver->name;
  2646. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  2647. &driver, error);
  2648. }
  2649. /**
  2650. * wpas_dbus_getter_current_bss - Get current bss object path
  2651. * @iter: Pointer to incoming dbus message iter
  2652. * @error: Location to store error on failure
  2653. * @user_data: Function specific data
  2654. * Returns: TRUE on success, FALSE on failure
  2655. *
  2656. * Getter for "CurrentBSS" property.
  2657. */
  2658. dbus_bool_t wpas_dbus_getter_current_bss(DBusMessageIter *iter,
  2659. DBusError *error,
  2660. void *user_data)
  2661. {
  2662. struct wpa_supplicant *wpa_s = user_data;
  2663. char path_buf[WPAS_DBUS_OBJECT_PATH_MAX], *bss_obj_path = path_buf;
  2664. if (wpa_s->current_bss && wpa_s->dbus_new_path)
  2665. os_snprintf(bss_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
  2666. "%s/" WPAS_DBUS_NEW_BSSIDS_PART "/%u",
  2667. wpa_s->dbus_new_path, wpa_s->current_bss->id);
  2668. else
  2669. os_snprintf(bss_obj_path, WPAS_DBUS_OBJECT_PATH_MAX, "/");
  2670. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_OBJECT_PATH,
  2671. &bss_obj_path, error);
  2672. }
  2673. /**
  2674. * wpas_dbus_getter_current_network - Get current network object path
  2675. * @iter: Pointer to incoming dbus message iter
  2676. * @error: Location to store error on failure
  2677. * @user_data: Function specific data
  2678. * Returns: TRUE on success, FALSE on failure
  2679. *
  2680. * Getter for "CurrentNetwork" property.
  2681. */
  2682. dbus_bool_t wpas_dbus_getter_current_network(DBusMessageIter *iter,
  2683. DBusError *error,
  2684. void *user_data)
  2685. {
  2686. struct wpa_supplicant *wpa_s = user_data;
  2687. char path_buf[WPAS_DBUS_OBJECT_PATH_MAX], *net_obj_path = path_buf;
  2688. if (wpa_s->current_ssid && wpa_s->dbus_new_path)
  2689. os_snprintf(net_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
  2690. "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%u",
  2691. wpa_s->dbus_new_path, wpa_s->current_ssid->id);
  2692. else
  2693. os_snprintf(net_obj_path, WPAS_DBUS_OBJECT_PATH_MAX, "/");
  2694. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_OBJECT_PATH,
  2695. &net_obj_path, error);
  2696. }
  2697. /**
  2698. * wpas_dbus_getter_current_auth_mode - Get current authentication type
  2699. * @iter: Pointer to incoming dbus message iter
  2700. * @error: Location to store error on failure
  2701. * @user_data: Function specific data
  2702. * Returns: TRUE on success, FALSE on failure
  2703. *
  2704. * Getter for "CurrentAuthMode" property.
  2705. */
  2706. dbus_bool_t wpas_dbus_getter_current_auth_mode(DBusMessageIter *iter,
  2707. DBusError *error,
  2708. void *user_data)
  2709. {
  2710. struct wpa_supplicant *wpa_s = user_data;
  2711. const char *eap_mode;
  2712. const char *auth_mode;
  2713. char eap_mode_buf[WPAS_DBUS_AUTH_MODE_MAX];
  2714. if (wpa_s->wpa_state != WPA_COMPLETED) {
  2715. auth_mode = "INACTIVE";
  2716. } else if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X ||
  2717. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  2718. eap_mode = wpa_supplicant_get_eap_mode(wpa_s);
  2719. os_snprintf(eap_mode_buf, WPAS_DBUS_AUTH_MODE_MAX,
  2720. "EAP-%s", eap_mode);
  2721. auth_mode = eap_mode_buf;
  2722. } else {
  2723. auth_mode = wpa_key_mgmt_txt(wpa_s->key_mgmt,
  2724. wpa_s->current_ssid->proto);
  2725. }
  2726. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  2727. &auth_mode, error);
  2728. }
  2729. /**
  2730. * wpas_dbus_getter_bridge_ifname - Get interface name
  2731. * @iter: Pointer to incoming dbus message iter
  2732. * @error: Location to store error on failure
  2733. * @user_data: Function specific data
  2734. * Returns: TRUE on success, FALSE on failure
  2735. *
  2736. * Getter for "BridgeIfname" property.
  2737. */
  2738. dbus_bool_t wpas_dbus_getter_bridge_ifname(DBusMessageIter *iter,
  2739. DBusError *error,
  2740. void *user_data)
  2741. {
  2742. struct wpa_supplicant *wpa_s = user_data;
  2743. const char *bridge_ifname = wpa_s->bridge_ifname;
  2744. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  2745. &bridge_ifname, error);
  2746. }
  2747. /**
  2748. * wpas_dbus_getter_bsss - Get array of BSSs objects
  2749. * @iter: Pointer to incoming dbus message iter
  2750. * @error: Location to store error on failure
  2751. * @user_data: Function specific data
  2752. * Returns: TRUE on success, FALSE on failure
  2753. *
  2754. * Getter for "BSSs" property.
  2755. */
  2756. dbus_bool_t wpas_dbus_getter_bsss(DBusMessageIter *iter, DBusError *error,
  2757. void *user_data)
  2758. {
  2759. struct wpa_supplicant *wpa_s = user_data;
  2760. struct wpa_bss *bss;
  2761. char **paths;
  2762. unsigned int i = 0;
  2763. dbus_bool_t success = FALSE;
  2764. if (!wpa_s->dbus_new_path) {
  2765. dbus_set_error(error, DBUS_ERROR_FAILED,
  2766. "%s: no D-Bus interface", __func__);
  2767. return FALSE;
  2768. }
  2769. paths = os_calloc(wpa_s->num_bss, sizeof(char *));
  2770. if (!paths) {
  2771. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  2772. return FALSE;
  2773. }
  2774. /* Loop through scan results and append each result's object path */
  2775. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  2776. paths[i] = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  2777. if (paths[i] == NULL) {
  2778. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY,
  2779. "no memory");
  2780. goto out;
  2781. }
  2782. /* Construct the object path for this BSS. */
  2783. os_snprintf(paths[i++], WPAS_DBUS_OBJECT_PATH_MAX,
  2784. "%s/" WPAS_DBUS_NEW_BSSIDS_PART "/%u",
  2785. wpa_s->dbus_new_path, bss->id);
  2786. }
  2787. success = wpas_dbus_simple_array_property_getter(iter,
  2788. DBUS_TYPE_OBJECT_PATH,
  2789. paths, wpa_s->num_bss,
  2790. error);
  2791. out:
  2792. while (i)
  2793. os_free(paths[--i]);
  2794. os_free(paths);
  2795. return success;
  2796. }
  2797. /**
  2798. * wpas_dbus_getter_networks - Get array of networks objects
  2799. * @iter: Pointer to incoming dbus message iter
  2800. * @error: Location to store error on failure
  2801. * @user_data: Function specific data
  2802. * Returns: TRUE on success, FALSE on failure
  2803. *
  2804. * Getter for "Networks" property.
  2805. */
  2806. dbus_bool_t wpas_dbus_getter_networks(DBusMessageIter *iter, DBusError *error,
  2807. void *user_data)
  2808. {
  2809. struct wpa_supplicant *wpa_s = user_data;
  2810. struct wpa_ssid *ssid;
  2811. char **paths;
  2812. unsigned int i = 0, num = 0;
  2813. dbus_bool_t success = FALSE;
  2814. if (!wpa_s->dbus_new_path) {
  2815. dbus_set_error(error, DBUS_ERROR_FAILED,
  2816. "%s: no D-Bus interface", __func__);
  2817. return FALSE;
  2818. }
  2819. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
  2820. if (!network_is_persistent_group(ssid))
  2821. num++;
  2822. paths = os_calloc(num, sizeof(char *));
  2823. if (!paths) {
  2824. dbus_set_error(error, DBUS_ERROR_NO_MEMORY, "no memory");
  2825. return FALSE;
  2826. }
  2827. /* Loop through configured networks and append object path of each */
  2828. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  2829. if (network_is_persistent_group(ssid))
  2830. continue;
  2831. paths[i] = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  2832. if (paths[i] == NULL) {
  2833. dbus_set_error(error, DBUS_ERROR_NO_MEMORY,
  2834. "no memory");
  2835. goto out;
  2836. }
  2837. /* Construct the object path for this network. */
  2838. os_snprintf(paths[i++], WPAS_DBUS_OBJECT_PATH_MAX,
  2839. "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%d",
  2840. wpa_s->dbus_new_path, ssid->id);
  2841. }
  2842. success = wpas_dbus_simple_array_property_getter(iter,
  2843. DBUS_TYPE_OBJECT_PATH,
  2844. paths, num, error);
  2845. out:
  2846. while (i)
  2847. os_free(paths[--i]);
  2848. os_free(paths);
  2849. return success;
  2850. }
  2851. /**
  2852. * wpas_dbus_getter_pkcs11_engine_path - Get PKCS #11 engine path
  2853. * @iter: Pointer to incoming dbus message iter
  2854. * @error: Location to store error on failure
  2855. * @user_data: Function specific data
  2856. * Returns: A dbus message containing the PKCS #11 engine path
  2857. *
  2858. * Getter for "PKCS11EnginePath" property.
  2859. */
  2860. dbus_bool_t wpas_dbus_getter_pkcs11_engine_path(DBusMessageIter *iter,
  2861. DBusError *error,
  2862. void *user_data)
  2863. {
  2864. struct wpa_supplicant *wpa_s = user_data;
  2865. const char *pkcs11_engine_path;
  2866. if (wpa_s->conf->pkcs11_engine_path == NULL)
  2867. pkcs11_engine_path = "";
  2868. else
  2869. pkcs11_engine_path = wpa_s->conf->pkcs11_engine_path;
  2870. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  2871. &pkcs11_engine_path, error);
  2872. }
  2873. /**
  2874. * wpas_dbus_getter_pkcs11_module_path - Get PKCS #11 module path
  2875. * @iter: Pointer to incoming dbus message iter
  2876. * @error: Location to store error on failure
  2877. * @user_data: Function specific data
  2878. * Returns: A dbus message containing the PKCS #11 module path
  2879. *
  2880. * Getter for "PKCS11ModulePath" property.
  2881. */
  2882. dbus_bool_t wpas_dbus_getter_pkcs11_module_path(DBusMessageIter *iter,
  2883. DBusError *error,
  2884. void *user_data)
  2885. {
  2886. struct wpa_supplicant *wpa_s = user_data;
  2887. const char *pkcs11_module_path;
  2888. if (wpa_s->conf->pkcs11_module_path == NULL)
  2889. pkcs11_module_path = "";
  2890. else
  2891. pkcs11_module_path = wpa_s->conf->pkcs11_module_path;
  2892. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  2893. &pkcs11_module_path, error);
  2894. }
  2895. /**
  2896. * wpas_dbus_getter_blobs - Get all blobs defined for this interface
  2897. * @iter: Pointer to incoming dbus message iter
  2898. * @error: Location to store error on failure
  2899. * @user_data: Function specific data
  2900. * Returns: TRUE on success, FALSE on failure
  2901. *
  2902. * Getter for "Blobs" property.
  2903. */
  2904. dbus_bool_t wpas_dbus_getter_blobs(DBusMessageIter *iter, DBusError *error,
  2905. void *user_data)
  2906. {
  2907. struct wpa_supplicant *wpa_s = user_data;
  2908. DBusMessageIter variant_iter, dict_iter, entry_iter, array_iter;
  2909. struct wpa_config_blob *blob;
  2910. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
  2911. "a{say}", &variant_iter) ||
  2912. !dbus_message_iter_open_container(&variant_iter, DBUS_TYPE_ARRAY,
  2913. "{say}", &dict_iter)) {
  2914. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  2915. return FALSE;
  2916. }
  2917. blob = wpa_s->conf->blobs;
  2918. while (blob) {
  2919. if (!dbus_message_iter_open_container(&dict_iter,
  2920. DBUS_TYPE_DICT_ENTRY,
  2921. NULL, &entry_iter) ||
  2922. !dbus_message_iter_append_basic(&entry_iter,
  2923. DBUS_TYPE_STRING,
  2924. &(blob->name)) ||
  2925. !dbus_message_iter_open_container(&entry_iter,
  2926. DBUS_TYPE_ARRAY,
  2927. DBUS_TYPE_BYTE_AS_STRING,
  2928. &array_iter) ||
  2929. !dbus_message_iter_append_fixed_array(&array_iter,
  2930. DBUS_TYPE_BYTE,
  2931. &(blob->data),
  2932. blob->len) ||
  2933. !dbus_message_iter_close_container(&entry_iter,
  2934. &array_iter) ||
  2935. !dbus_message_iter_close_container(&dict_iter,
  2936. &entry_iter)) {
  2937. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY,
  2938. "no memory");
  2939. return FALSE;
  2940. }
  2941. blob = blob->next;
  2942. }
  2943. if (!dbus_message_iter_close_container(&variant_iter, &dict_iter) ||
  2944. !dbus_message_iter_close_container(iter, &variant_iter)) {
  2945. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  2946. return FALSE;
  2947. }
  2948. return TRUE;
  2949. }
  2950. static struct wpa_bss * get_bss_helper(struct bss_handler_args *args,
  2951. DBusError *error, const char *func_name)
  2952. {
  2953. struct wpa_bss *res = wpa_bss_get_id(args->wpa_s, args->id);
  2954. if (!res) {
  2955. wpa_printf(MSG_ERROR, "%s[dbus]: no bss with id %d found",
  2956. func_name, args->id);
  2957. dbus_set_error(error, DBUS_ERROR_FAILED,
  2958. "%s: BSS %d not found",
  2959. func_name, args->id);
  2960. }
  2961. return res;
  2962. }
  2963. /**
  2964. * wpas_dbus_getter_bss_bssid - Return the BSSID of a BSS
  2965. * @iter: Pointer to incoming dbus message iter
  2966. * @error: Location to store error on failure
  2967. * @user_data: Function specific data
  2968. * Returns: TRUE on success, FALSE on failure
  2969. *
  2970. * Getter for "BSSID" property.
  2971. */
  2972. dbus_bool_t wpas_dbus_getter_bss_bssid(DBusMessageIter *iter, DBusError *error,
  2973. void *user_data)
  2974. {
  2975. struct bss_handler_args *args = user_data;
  2976. struct wpa_bss *res;
  2977. res = get_bss_helper(args, error, __func__);
  2978. if (!res)
  2979. return FALSE;
  2980. return wpas_dbus_simple_array_property_getter(iter, DBUS_TYPE_BYTE,
  2981. res->bssid, ETH_ALEN,
  2982. error);
  2983. }
  2984. /**
  2985. * wpas_dbus_getter_bss_ssid - Return the SSID of a BSS
  2986. * @iter: Pointer to incoming dbus message iter
  2987. * @error: Location to store error on failure
  2988. * @user_data: Function specific data
  2989. * Returns: TRUE on success, FALSE on failure
  2990. *
  2991. * Getter for "SSID" property.
  2992. */
  2993. dbus_bool_t wpas_dbus_getter_bss_ssid(DBusMessageIter *iter, DBusError *error,
  2994. void *user_data)
  2995. {
  2996. struct bss_handler_args *args = user_data;
  2997. struct wpa_bss *res;
  2998. res = get_bss_helper(args, error, __func__);
  2999. if (!res)
  3000. return FALSE;
  3001. return wpas_dbus_simple_array_property_getter(iter, DBUS_TYPE_BYTE,
  3002. res->ssid, res->ssid_len,
  3003. error);
  3004. }
  3005. /**
  3006. * wpas_dbus_getter_bss_privacy - Return the privacy flag of a BSS
  3007. * @iter: Pointer to incoming dbus message iter
  3008. * @error: Location to store error on failure
  3009. * @user_data: Function specific data
  3010. * Returns: TRUE on success, FALSE on failure
  3011. *
  3012. * Getter for "Privacy" property.
  3013. */
  3014. dbus_bool_t wpas_dbus_getter_bss_privacy(DBusMessageIter *iter,
  3015. DBusError *error, void *user_data)
  3016. {
  3017. struct bss_handler_args *args = user_data;
  3018. struct wpa_bss *res;
  3019. dbus_bool_t privacy;
  3020. res = get_bss_helper(args, error, __func__);
  3021. if (!res)
  3022. return FALSE;
  3023. privacy = (res->caps & IEEE80211_CAP_PRIVACY) ? TRUE : FALSE;
  3024. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_BOOLEAN,
  3025. &privacy, error);
  3026. }
  3027. /**
  3028. * wpas_dbus_getter_bss_mode - Return the mode of a BSS
  3029. * @iter: Pointer to incoming dbus message iter
  3030. * @error: Location to store error on failure
  3031. * @user_data: Function specific data
  3032. * Returns: TRUE on success, FALSE on failure
  3033. *
  3034. * Getter for "Mode" property.
  3035. */
  3036. dbus_bool_t wpas_dbus_getter_bss_mode(DBusMessageIter *iter, DBusError *error,
  3037. void *user_data)
  3038. {
  3039. struct bss_handler_args *args = user_data;
  3040. struct wpa_bss *res;
  3041. const char *mode;
  3042. res = get_bss_helper(args, error, __func__);
  3043. if (!res)
  3044. return FALSE;
  3045. if (bss_is_dmg(res)) {
  3046. switch (res->caps & IEEE80211_CAP_DMG_MASK) {
  3047. case IEEE80211_CAP_DMG_PBSS:
  3048. case IEEE80211_CAP_DMG_IBSS:
  3049. mode = "ad-hoc";
  3050. break;
  3051. case IEEE80211_CAP_DMG_AP:
  3052. mode = "infrastructure";
  3053. break;
  3054. }
  3055. } else {
  3056. if (res->caps & IEEE80211_CAP_IBSS)
  3057. mode = "ad-hoc";
  3058. else
  3059. mode = "infrastructure";
  3060. }
  3061. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  3062. &mode, error);
  3063. }
  3064. /**
  3065. * wpas_dbus_getter_bss_level - Return the signal strength of a BSS
  3066. * @iter: Pointer to incoming dbus message iter
  3067. * @error: Location to store error on failure
  3068. * @user_data: Function specific data
  3069. * Returns: TRUE on success, FALSE on failure
  3070. *
  3071. * Getter for "Level" property.
  3072. */
  3073. dbus_bool_t wpas_dbus_getter_bss_signal(DBusMessageIter *iter,
  3074. DBusError *error, void *user_data)
  3075. {
  3076. struct bss_handler_args *args = user_data;
  3077. struct wpa_bss *res;
  3078. s16 level;
  3079. res = get_bss_helper(args, error, __func__);
  3080. if (!res)
  3081. return FALSE;
  3082. level = (s16) res->level;
  3083. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_INT16,
  3084. &level, error);
  3085. }
  3086. /**
  3087. * wpas_dbus_getter_bss_frequency - Return the frequency of a BSS
  3088. * @iter: Pointer to incoming dbus message iter
  3089. * @error: Location to store error on failure
  3090. * @user_data: Function specific data
  3091. * Returns: TRUE on success, FALSE on failure
  3092. *
  3093. * Getter for "Frequency" property.
  3094. */
  3095. dbus_bool_t wpas_dbus_getter_bss_frequency(DBusMessageIter *iter,
  3096. DBusError *error, void *user_data)
  3097. {
  3098. struct bss_handler_args *args = user_data;
  3099. struct wpa_bss *res;
  3100. u16 freq;
  3101. res = get_bss_helper(args, error, __func__);
  3102. if (!res)
  3103. return FALSE;
  3104. freq = (u16) res->freq;
  3105. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_UINT16,
  3106. &freq, error);
  3107. }
  3108. static int cmp_u8s_desc(const void *a, const void *b)
  3109. {
  3110. return (*(u8 *) b - *(u8 *) a);
  3111. }
  3112. /**
  3113. * wpas_dbus_getter_bss_rates - Return available bit rates of a BSS
  3114. * @iter: Pointer to incoming dbus message iter
  3115. * @error: Location to store error on failure
  3116. * @user_data: Function specific data
  3117. * Returns: TRUE on success, FALSE on failure
  3118. *
  3119. * Getter for "Rates" property.
  3120. */
  3121. dbus_bool_t wpas_dbus_getter_bss_rates(DBusMessageIter *iter,
  3122. DBusError *error, void *user_data)
  3123. {
  3124. struct bss_handler_args *args = user_data;
  3125. struct wpa_bss *res;
  3126. u8 *ie_rates = NULL;
  3127. u32 *real_rates;
  3128. int rates_num, i;
  3129. dbus_bool_t success = FALSE;
  3130. res = get_bss_helper(args, error, __func__);
  3131. if (!res)
  3132. return FALSE;
  3133. rates_num = wpa_bss_get_bit_rates(res, &ie_rates);
  3134. if (rates_num < 0)
  3135. return FALSE;
  3136. qsort(ie_rates, rates_num, 1, cmp_u8s_desc);
  3137. real_rates = os_malloc(sizeof(u32) * rates_num);
  3138. if (!real_rates) {
  3139. os_free(ie_rates);
  3140. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  3141. return FALSE;
  3142. }
  3143. for (i = 0; i < rates_num; i++)
  3144. real_rates[i] = ie_rates[i] * 500000;
  3145. success = wpas_dbus_simple_array_property_getter(iter, DBUS_TYPE_UINT32,
  3146. real_rates, rates_num,
  3147. error);
  3148. os_free(ie_rates);
  3149. os_free(real_rates);
  3150. return success;
  3151. }
  3152. static dbus_bool_t wpas_dbus_get_bss_security_prop(DBusMessageIter *iter,
  3153. struct wpa_ie_data *ie_data,
  3154. DBusError *error)
  3155. {
  3156. DBusMessageIter iter_dict, variant_iter;
  3157. const char *group;
  3158. const char *pairwise[5]; /* max 5 pairwise ciphers is supported */
  3159. const char *key_mgmt[9]; /* max 9 key managements may be supported */
  3160. int n;
  3161. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
  3162. "a{sv}", &variant_iter))
  3163. goto nomem;
  3164. if (!wpa_dbus_dict_open_write(&variant_iter, &iter_dict))
  3165. goto nomem;
  3166. /* KeyMgmt */
  3167. n = 0;
  3168. if (ie_data->key_mgmt & WPA_KEY_MGMT_PSK)
  3169. key_mgmt[n++] = "wpa-psk";
  3170. if (ie_data->key_mgmt & WPA_KEY_MGMT_FT_PSK)
  3171. key_mgmt[n++] = "wpa-ft-psk";
  3172. if (ie_data->key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
  3173. key_mgmt[n++] = "wpa-psk-sha256";
  3174. if (ie_data->key_mgmt & WPA_KEY_MGMT_IEEE8021X)
  3175. key_mgmt[n++] = "wpa-eap";
  3176. if (ie_data->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
  3177. key_mgmt[n++] = "wpa-ft-eap";
  3178. if (ie_data->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
  3179. key_mgmt[n++] = "wpa-eap-sha256";
  3180. #ifdef CONFIG_SUITEB
  3181. if (ie_data->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B)
  3182. key_mgmt[n++] = "wpa-eap-suite-b";
  3183. #endif /* CONFIG_SUITEB */
  3184. #ifdef CONFIG_SUITEB192
  3185. if (ie_data->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
  3186. key_mgmt[n++] = "wpa-eap-suite-b-192";
  3187. #endif /* CONFIG_SUITEB192 */
  3188. if (ie_data->key_mgmt & WPA_KEY_MGMT_NONE)
  3189. key_mgmt[n++] = "wpa-none";
  3190. if (!wpa_dbus_dict_append_string_array(&iter_dict, "KeyMgmt",
  3191. key_mgmt, n))
  3192. goto nomem;
  3193. /* Group */
  3194. switch (ie_data->group_cipher) {
  3195. case WPA_CIPHER_WEP40:
  3196. group = "wep40";
  3197. break;
  3198. case WPA_CIPHER_TKIP:
  3199. group = "tkip";
  3200. break;
  3201. case WPA_CIPHER_CCMP:
  3202. group = "ccmp";
  3203. break;
  3204. case WPA_CIPHER_GCMP:
  3205. group = "gcmp";
  3206. break;
  3207. case WPA_CIPHER_WEP104:
  3208. group = "wep104";
  3209. break;
  3210. case WPA_CIPHER_CCMP_256:
  3211. group = "ccmp-256";
  3212. break;
  3213. case WPA_CIPHER_GCMP_256:
  3214. group = "gcmp-256";
  3215. break;
  3216. default:
  3217. group = "";
  3218. break;
  3219. }
  3220. if (!wpa_dbus_dict_append_string(&iter_dict, "Group", group))
  3221. goto nomem;
  3222. /* Pairwise */
  3223. n = 0;
  3224. if (ie_data->pairwise_cipher & WPA_CIPHER_TKIP)
  3225. pairwise[n++] = "tkip";
  3226. if (ie_data->pairwise_cipher & WPA_CIPHER_CCMP)
  3227. pairwise[n++] = "ccmp";
  3228. if (ie_data->pairwise_cipher & WPA_CIPHER_GCMP)
  3229. pairwise[n++] = "gcmp";
  3230. if (ie_data->pairwise_cipher & WPA_CIPHER_CCMP_256)
  3231. pairwise[n++] = "ccmp-256";
  3232. if (ie_data->pairwise_cipher & WPA_CIPHER_GCMP_256)
  3233. pairwise[n++] = "gcmp-256";
  3234. if (!wpa_dbus_dict_append_string_array(&iter_dict, "Pairwise",
  3235. pairwise, n))
  3236. goto nomem;
  3237. /* Management group (RSN only) */
  3238. if (ie_data->proto == WPA_PROTO_RSN) {
  3239. switch (ie_data->mgmt_group_cipher) {
  3240. #ifdef CONFIG_IEEE80211W
  3241. case WPA_CIPHER_AES_128_CMAC:
  3242. group = "aes128cmac";
  3243. break;
  3244. #endif /* CONFIG_IEEE80211W */
  3245. default:
  3246. group = "";
  3247. break;
  3248. }
  3249. if (!wpa_dbus_dict_append_string(&iter_dict, "MgmtGroup",
  3250. group))
  3251. goto nomem;
  3252. }
  3253. if (!wpa_dbus_dict_close_write(&variant_iter, &iter_dict) ||
  3254. !dbus_message_iter_close_container(iter, &variant_iter))
  3255. goto nomem;
  3256. return TRUE;
  3257. nomem:
  3258. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  3259. return FALSE;
  3260. }
  3261. /**
  3262. * wpas_dbus_getter_bss_wpa - Return the WPA options of a BSS
  3263. * @iter: Pointer to incoming dbus message iter
  3264. * @error: Location to store error on failure
  3265. * @user_data: Function specific data
  3266. * Returns: TRUE on success, FALSE on failure
  3267. *
  3268. * Getter for "WPA" property.
  3269. */
  3270. dbus_bool_t wpas_dbus_getter_bss_wpa(DBusMessageIter *iter, DBusError *error,
  3271. void *user_data)
  3272. {
  3273. struct bss_handler_args *args = user_data;
  3274. struct wpa_bss *res;
  3275. struct wpa_ie_data wpa_data;
  3276. const u8 *ie;
  3277. res = get_bss_helper(args, error, __func__);
  3278. if (!res)
  3279. return FALSE;
  3280. os_memset(&wpa_data, 0, sizeof(wpa_data));
  3281. ie = wpa_bss_get_vendor_ie(res, WPA_IE_VENDOR_TYPE);
  3282. if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &wpa_data) < 0) {
  3283. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  3284. "failed to parse WPA IE");
  3285. return FALSE;
  3286. }
  3287. return wpas_dbus_get_bss_security_prop(iter, &wpa_data, error);
  3288. }
  3289. /**
  3290. * wpas_dbus_getter_bss_rsn - Return the RSN options of a BSS
  3291. * @iter: Pointer to incoming dbus message iter
  3292. * @error: Location to store error on failure
  3293. * @user_data: Function specific data
  3294. * Returns: TRUE on success, FALSE on failure
  3295. *
  3296. * Getter for "RSN" property.
  3297. */
  3298. dbus_bool_t wpas_dbus_getter_bss_rsn(DBusMessageIter *iter, DBusError *error,
  3299. void *user_data)
  3300. {
  3301. struct bss_handler_args *args = user_data;
  3302. struct wpa_bss *res;
  3303. struct wpa_ie_data wpa_data;
  3304. const u8 *ie;
  3305. res = get_bss_helper(args, error, __func__);
  3306. if (!res)
  3307. return FALSE;
  3308. os_memset(&wpa_data, 0, sizeof(wpa_data));
  3309. ie = wpa_bss_get_ie(res, WLAN_EID_RSN);
  3310. if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &wpa_data) < 0) {
  3311. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  3312. "failed to parse RSN IE");
  3313. return FALSE;
  3314. }
  3315. return wpas_dbus_get_bss_security_prop(iter, &wpa_data, error);
  3316. }
  3317. /**
  3318. * wpas_dbus_getter_bss_wps - Return the WPS options of a BSS
  3319. * @iter: Pointer to incoming dbus message iter
  3320. * @error: Location to store error on failure
  3321. * @user_data: Function specific data
  3322. * Returns: TRUE on success, FALSE on failure
  3323. *
  3324. * Getter for "WPS" property.
  3325. */
  3326. dbus_bool_t wpas_dbus_getter_bss_wps(DBusMessageIter *iter, DBusError *error,
  3327. void *user_data)
  3328. {
  3329. struct bss_handler_args *args = user_data;
  3330. struct wpa_bss *res;
  3331. #ifdef CONFIG_WPS
  3332. struct wpabuf *wps_ie;
  3333. #endif /* CONFIG_WPS */
  3334. DBusMessageIter iter_dict, variant_iter;
  3335. int wps_support = 0;
  3336. const char *type = "";
  3337. res = get_bss_helper(args, error, __func__);
  3338. if (!res)
  3339. return FALSE;
  3340. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
  3341. "a{sv}", &variant_iter) ||
  3342. !wpa_dbus_dict_open_write(&variant_iter, &iter_dict))
  3343. goto nomem;
  3344. #ifdef CONFIG_WPS
  3345. wps_ie = wpa_bss_get_vendor_ie_multi(res, WPS_IE_VENDOR_TYPE);
  3346. if (wps_ie) {
  3347. wps_support = 1;
  3348. if (wps_is_selected_pbc_registrar(wps_ie))
  3349. type = "pbc";
  3350. else if (wps_is_selected_pin_registrar(wps_ie))
  3351. type = "pin";
  3352. wpabuf_free(wps_ie);
  3353. }
  3354. #endif /* CONFIG_WPS */
  3355. if ((wps_support && !wpa_dbus_dict_append_string(&iter_dict, "Type", type)) ||
  3356. !wpa_dbus_dict_close_write(&variant_iter, &iter_dict) ||
  3357. !dbus_message_iter_close_container(iter, &variant_iter))
  3358. goto nomem;
  3359. return TRUE;
  3360. nomem:
  3361. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  3362. return FALSE;
  3363. }
  3364. /**
  3365. * wpas_dbus_getter_bss_ies - Return all IEs of a BSS
  3366. * @iter: Pointer to incoming dbus message iter
  3367. * @error: Location to store error on failure
  3368. * @user_data: Function specific data
  3369. * Returns: TRUE on success, FALSE on failure
  3370. *
  3371. * Getter for "IEs" property.
  3372. */
  3373. dbus_bool_t wpas_dbus_getter_bss_ies(DBusMessageIter *iter, DBusError *error,
  3374. void *user_data)
  3375. {
  3376. struct bss_handler_args *args = user_data;
  3377. struct wpa_bss *res;
  3378. res = get_bss_helper(args, error, __func__);
  3379. if (!res)
  3380. return FALSE;
  3381. return wpas_dbus_simple_array_property_getter(iter, DBUS_TYPE_BYTE,
  3382. res + 1, res->ie_len,
  3383. error);
  3384. }
  3385. /**
  3386. * wpas_dbus_getter_bss_age - Return time in seconds since BSS was last seen
  3387. * @iter: Pointer to incoming dbus message iter
  3388. * @error: Location to store error on failure
  3389. * @user_data: Function specific data
  3390. * Returns: TRUE on success, FALSE on failure
  3391. *
  3392. * Getter for BSS age
  3393. */
  3394. dbus_bool_t wpas_dbus_getter_bss_age(DBusMessageIter *iter, DBusError *error,
  3395. void *user_data)
  3396. {
  3397. struct bss_handler_args *args = user_data;
  3398. struct wpa_bss *res;
  3399. struct os_reltime now, diff = { 0, 0 };
  3400. u32 age;
  3401. res = get_bss_helper(args, error, __func__);
  3402. if (!res)
  3403. return FALSE;
  3404. os_get_reltime(&now);
  3405. os_reltime_sub(&now, &res->last_update, &diff);
  3406. age = diff.sec > 0 ? diff.sec : 0;
  3407. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_UINT32, &age,
  3408. error);
  3409. }
  3410. /**
  3411. * wpas_dbus_getter_enabled - Check whether network is enabled or disabled
  3412. * @iter: Pointer to incoming dbus message iter
  3413. * @error: Location to store error on failure
  3414. * @user_data: Function specific data
  3415. * Returns: TRUE on success, FALSE on failure
  3416. *
  3417. * Getter for "enabled" property of a configured network.
  3418. */
  3419. dbus_bool_t wpas_dbus_getter_enabled(DBusMessageIter *iter, DBusError *error,
  3420. void *user_data)
  3421. {
  3422. struct network_handler_args *net = user_data;
  3423. dbus_bool_t enabled = net->ssid->disabled ? FALSE : TRUE;
  3424. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_BOOLEAN,
  3425. &enabled, error);
  3426. }
  3427. /**
  3428. * wpas_dbus_setter_enabled - Mark a configured network as enabled or disabled
  3429. * @iter: Pointer to incoming dbus message iter
  3430. * @error: Location to store error on failure
  3431. * @user_data: Function specific data
  3432. * Returns: TRUE on success, FALSE on failure
  3433. *
  3434. * Setter for "Enabled" property of a configured network.
  3435. */
  3436. dbus_bool_t wpas_dbus_setter_enabled(DBusMessageIter *iter, DBusError *error,
  3437. void *user_data)
  3438. {
  3439. struct network_handler_args *net = user_data;
  3440. struct wpa_supplicant *wpa_s;
  3441. struct wpa_ssid *ssid;
  3442. dbus_bool_t enable;
  3443. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_BOOLEAN,
  3444. &enable))
  3445. return FALSE;
  3446. wpa_s = net->wpa_s;
  3447. ssid = net->ssid;
  3448. if (enable)
  3449. wpa_supplicant_enable_network(wpa_s, ssid);
  3450. else
  3451. wpa_supplicant_disable_network(wpa_s, ssid);
  3452. return TRUE;
  3453. }
  3454. /**
  3455. * wpas_dbus_getter_network_properties - Get options for a configured network
  3456. * @iter: Pointer to incoming dbus message iter
  3457. * @error: Location to store error on failure
  3458. * @user_data: Function specific data
  3459. * Returns: TRUE on success, FALSE on failure
  3460. *
  3461. * Getter for "Properties" property of a configured network.
  3462. */
  3463. dbus_bool_t wpas_dbus_getter_network_properties(DBusMessageIter *iter,
  3464. DBusError *error,
  3465. void *user_data)
  3466. {
  3467. struct network_handler_args *net = user_data;
  3468. DBusMessageIter variant_iter, dict_iter;
  3469. char **iterator;
  3470. char **props = wpa_config_get_all(net->ssid, 1);
  3471. dbus_bool_t success = FALSE;
  3472. if (!props) {
  3473. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  3474. return FALSE;
  3475. }
  3476. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, "a{sv}",
  3477. &variant_iter) ||
  3478. !wpa_dbus_dict_open_write(&variant_iter, &dict_iter)) {
  3479. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  3480. goto out;
  3481. }
  3482. iterator = props;
  3483. while (*iterator) {
  3484. if (!wpa_dbus_dict_append_string(&dict_iter, *iterator,
  3485. *(iterator + 1))) {
  3486. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY,
  3487. "no memory");
  3488. goto out;
  3489. }
  3490. iterator += 2;
  3491. }
  3492. if (!wpa_dbus_dict_close_write(&variant_iter, &dict_iter) ||
  3493. !dbus_message_iter_close_container(iter, &variant_iter)) {
  3494. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  3495. goto out;
  3496. }
  3497. success = TRUE;
  3498. out:
  3499. iterator = props;
  3500. while (*iterator) {
  3501. os_free(*iterator);
  3502. iterator++;
  3503. }
  3504. os_free(props);
  3505. return success;
  3506. }
  3507. /**
  3508. * wpas_dbus_setter_network_properties - Set options for a configured network
  3509. * @iter: Pointer to incoming dbus message iter
  3510. * @error: Location to store error on failure
  3511. * @user_data: Function specific data
  3512. * Returns: TRUE on success, FALSE on failure
  3513. *
  3514. * Setter for "Properties" property of a configured network.
  3515. */
  3516. dbus_bool_t wpas_dbus_setter_network_properties(DBusMessageIter *iter,
  3517. DBusError *error,
  3518. void *user_data)
  3519. {
  3520. struct network_handler_args *net = user_data;
  3521. struct wpa_ssid *ssid = net->ssid;
  3522. DBusMessageIter variant_iter;
  3523. dbus_message_iter_recurse(iter, &variant_iter);
  3524. return set_network_properties(net->wpa_s, ssid, &variant_iter, error);
  3525. }
  3526. #ifdef CONFIG_AP
  3527. DBusMessage * wpas_dbus_handler_subscribe_preq(
  3528. DBusMessage *message, struct wpa_supplicant *wpa_s)
  3529. {
  3530. struct wpas_dbus_priv *priv = wpa_s->global->dbus;
  3531. char *name;
  3532. if (wpa_s->preq_notify_peer != NULL) {
  3533. if (os_strcmp(dbus_message_get_sender(message),
  3534. wpa_s->preq_notify_peer) == 0)
  3535. return NULL;
  3536. return dbus_message_new_error(message,
  3537. WPAS_DBUS_ERROR_SUBSCRIPTION_IN_USE,
  3538. "Another application is already subscribed");
  3539. }
  3540. name = os_strdup(dbus_message_get_sender(message));
  3541. if (!name)
  3542. return wpas_dbus_error_no_memory(message);
  3543. wpa_s->preq_notify_peer = name;
  3544. /* Subscribe to clean up if application closes socket */
  3545. wpas_dbus_subscribe_noc(priv);
  3546. /*
  3547. * Double-check it's still alive to make sure that we didn't
  3548. * miss the NameOwnerChanged signal, e.g. while strdup'ing.
  3549. */
  3550. if (!dbus_bus_name_has_owner(priv->con, name, NULL)) {
  3551. /*
  3552. * Application no longer exists, clean up.
  3553. * The return value is irrelevant now.
  3554. *
  3555. * Need to check if the NameOwnerChanged handling
  3556. * already cleaned up because we have processed
  3557. * DBus messages while checking if the name still
  3558. * has an owner.
  3559. */
  3560. if (!wpa_s->preq_notify_peer)
  3561. return NULL;
  3562. os_free(wpa_s->preq_notify_peer);
  3563. wpa_s->preq_notify_peer = NULL;
  3564. wpas_dbus_unsubscribe_noc(priv);
  3565. }
  3566. return NULL;
  3567. }
  3568. DBusMessage * wpas_dbus_handler_unsubscribe_preq(
  3569. DBusMessage *message, struct wpa_supplicant *wpa_s)
  3570. {
  3571. struct wpas_dbus_priv *priv = wpa_s->global->dbus;
  3572. if (!wpa_s->preq_notify_peer)
  3573. return dbus_message_new_error(message,
  3574. WPAS_DBUS_ERROR_NO_SUBSCRIPTION,
  3575. "Not subscribed");
  3576. if (os_strcmp(wpa_s->preq_notify_peer,
  3577. dbus_message_get_sender(message)))
  3578. return dbus_message_new_error(message,
  3579. WPAS_DBUS_ERROR_SUBSCRIPTION_EPERM,
  3580. "Can't unsubscribe others");
  3581. os_free(wpa_s->preq_notify_peer);
  3582. wpa_s->preq_notify_peer = NULL;
  3583. wpas_dbus_unsubscribe_noc(priv);
  3584. return NULL;
  3585. }
  3586. void wpas_dbus_signal_preq(struct wpa_supplicant *wpa_s,
  3587. const u8 *addr, const u8 *dst, const u8 *bssid,
  3588. const u8 *ie, size_t ie_len, u32 ssi_signal)
  3589. {
  3590. DBusMessage *msg;
  3591. DBusMessageIter iter, dict_iter;
  3592. struct wpas_dbus_priv *priv = wpa_s->global->dbus;
  3593. /* Do nothing if the control interface is not turned on */
  3594. if (priv == NULL || !wpa_s->dbus_new_path)
  3595. return;
  3596. if (wpa_s->preq_notify_peer == NULL)
  3597. return;
  3598. msg = dbus_message_new_signal(wpa_s->dbus_new_path,
  3599. WPAS_DBUS_NEW_IFACE_INTERFACE,
  3600. "ProbeRequest");
  3601. if (msg == NULL)
  3602. return;
  3603. dbus_message_set_destination(msg, wpa_s->preq_notify_peer);
  3604. dbus_message_iter_init_append(msg, &iter);
  3605. if (!wpa_dbus_dict_open_write(&iter, &dict_iter) ||
  3606. (addr && !wpa_dbus_dict_append_byte_array(&dict_iter, "addr",
  3607. (const char *) addr,
  3608. ETH_ALEN)) ||
  3609. (dst && !wpa_dbus_dict_append_byte_array(&dict_iter, "dst",
  3610. (const char *) dst,
  3611. ETH_ALEN)) ||
  3612. (bssid && !wpa_dbus_dict_append_byte_array(&dict_iter, "bssid",
  3613. (const char *) bssid,
  3614. ETH_ALEN)) ||
  3615. (ie && ie_len && !wpa_dbus_dict_append_byte_array(&dict_iter, "ies",
  3616. (const char *) ie,
  3617. ie_len)) ||
  3618. (ssi_signal && !wpa_dbus_dict_append_int32(&dict_iter, "signal",
  3619. ssi_signal)) ||
  3620. !wpa_dbus_dict_close_write(&iter, &dict_iter))
  3621. goto fail;
  3622. dbus_connection_send(priv->con, msg, NULL);
  3623. goto out;
  3624. fail:
  3625. wpa_printf(MSG_ERROR, "dbus: Failed to construct signal");
  3626. out:
  3627. dbus_message_unref(msg);
  3628. }
  3629. #endif /* CONFIG_AP */