dbus_new_handlers.c 114 KB

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