dbus_new_handlers.c 116 KB

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