dbus_new_handlers.c 117 KB

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