dbus_new_handlers.c 118 KB

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