dbus_new_handlers.c 120 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364
  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, "dbus: 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. #ifndef CONFIG_NO_CONFIG_WRITE
  1964. /**
  1965. * wpas_dbus_handler_save_config - Save configuration to configuration file
  1966. * @message: Pointer to incoming dbus message
  1967. * @wpa_s: wpa_supplicant structure for a network interface
  1968. * Returns: NULL on Success, Otherwise errror message
  1969. *
  1970. * Handler function for "SaveConfig" method call of network interface.
  1971. */
  1972. DBusMessage * wpas_dbus_handler_save_config(DBusMessage *message,
  1973. struct wpa_supplicant *wpa_s)
  1974. {
  1975. int ret;
  1976. if (!wpa_s->conf->update_config) {
  1977. return wpas_dbus_error_unknown_error(
  1978. message,
  1979. "Not allowed to update configuration (update_config=0)");
  1980. }
  1981. ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
  1982. if (ret)
  1983. return wpas_dbus_error_unknown_error(
  1984. message, "Failed to update configuration");
  1985. return NULL;
  1986. }
  1987. #endif /* CONFIG_NO_CONFIG_WRITE */
  1988. /**
  1989. * wpas_dbus_handler_set_pkcs11_engine_and_module_path - Set PKCS #11 engine and module path
  1990. * @message: Pointer to incoming dbus message
  1991. * @wpa_s: %wpa_supplicant data structure
  1992. * Returns: A dbus message containing an error on failure or NULL on success
  1993. *
  1994. * Sets the PKCS #11 engine and module path.
  1995. */
  1996. DBusMessage * wpas_dbus_handler_set_pkcs11_engine_and_module_path(
  1997. DBusMessage *message, struct wpa_supplicant *wpa_s)
  1998. {
  1999. DBusMessageIter iter;
  2000. char *value = NULL;
  2001. char *pkcs11_engine_path = NULL;
  2002. char *pkcs11_module_path = NULL;
  2003. dbus_message_iter_init(message, &iter);
  2004. dbus_message_iter_get_basic(&iter, &value);
  2005. if (value == NULL) {
  2006. return dbus_message_new_error(
  2007. message, DBUS_ERROR_INVALID_ARGS,
  2008. "Invalid pkcs11_engine_path argument");
  2009. }
  2010. /* Empty path defaults to NULL */
  2011. if (os_strlen(value))
  2012. pkcs11_engine_path = value;
  2013. dbus_message_iter_next(&iter);
  2014. dbus_message_iter_get_basic(&iter, &value);
  2015. if (value == NULL) {
  2016. os_free(pkcs11_engine_path);
  2017. return dbus_message_new_error(
  2018. message, DBUS_ERROR_INVALID_ARGS,
  2019. "Invalid pkcs11_module_path argument");
  2020. }
  2021. /* Empty path defaults to NULL */
  2022. if (os_strlen(value))
  2023. pkcs11_module_path = value;
  2024. if (wpas_set_pkcs11_engine_and_module_path(wpa_s, pkcs11_engine_path,
  2025. pkcs11_module_path))
  2026. return dbus_message_new_error(
  2027. message, DBUS_ERROR_FAILED,
  2028. "Reinit of the EAPOL state machine with the new PKCS #11 engine and module path failed.");
  2029. if (wpa_s->dbus_new_path) {
  2030. wpa_dbus_mark_property_changed(
  2031. wpa_s->global->dbus, wpa_s->dbus_new_path,
  2032. WPAS_DBUS_NEW_IFACE_INTERFACE, "PKCS11EnginePath");
  2033. wpa_dbus_mark_property_changed(
  2034. wpa_s->global->dbus, wpa_s->dbus_new_path,
  2035. WPAS_DBUS_NEW_IFACE_INTERFACE, "PKCS11ModulePath");
  2036. }
  2037. return NULL;
  2038. }
  2039. /**
  2040. * wpas_dbus_getter_capabilities - Return interface capabilities
  2041. * @iter: Pointer to incoming dbus message iter
  2042. * @error: Location to store error on failure
  2043. * @user_data: Function specific data
  2044. * Returns: TRUE on success, FALSE on failure
  2045. *
  2046. * Getter for "Capabilities" property of an interface.
  2047. */
  2048. dbus_bool_t wpas_dbus_getter_capabilities(
  2049. const struct wpa_dbus_property_desc *property_desc,
  2050. DBusMessageIter *iter, DBusError *error, void *user_data)
  2051. {
  2052. struct wpa_supplicant *wpa_s = user_data;
  2053. struct wpa_driver_capa capa;
  2054. int res;
  2055. DBusMessageIter iter_dict, iter_dict_entry, iter_dict_val, iter_array,
  2056. variant_iter;
  2057. const char *scans[] = { "active", "passive", "ssid" };
  2058. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
  2059. "a{sv}", &variant_iter) ||
  2060. !wpa_dbus_dict_open_write(&variant_iter, &iter_dict))
  2061. goto nomem;
  2062. res = wpa_drv_get_capa(wpa_s, &capa);
  2063. /***** pairwise cipher */
  2064. if (res < 0) {
  2065. const char *args[] = {"ccmp", "tkip", "none"};
  2066. if (!wpa_dbus_dict_append_string_array(
  2067. &iter_dict, "Pairwise", args,
  2068. ARRAY_SIZE(args)))
  2069. goto nomem;
  2070. } else {
  2071. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Pairwise",
  2072. &iter_dict_entry,
  2073. &iter_dict_val,
  2074. &iter_array) ||
  2075. ((capa.enc & WPA_DRIVER_CAPA_ENC_CCMP_256) &&
  2076. !wpa_dbus_dict_string_array_add_element(
  2077. &iter_array, "ccmp-256")) ||
  2078. ((capa.enc & WPA_DRIVER_CAPA_ENC_GCMP_256) &&
  2079. !wpa_dbus_dict_string_array_add_element(
  2080. &iter_array, "gcmp-256")) ||
  2081. ((capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) &&
  2082. !wpa_dbus_dict_string_array_add_element(
  2083. &iter_array, "ccmp")) ||
  2084. ((capa.enc & WPA_DRIVER_CAPA_ENC_GCMP) &&
  2085. !wpa_dbus_dict_string_array_add_element(
  2086. &iter_array, "gcmp")) ||
  2087. ((capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) &&
  2088. !wpa_dbus_dict_string_array_add_element(
  2089. &iter_array, "tkip")) ||
  2090. ((capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) &&
  2091. !wpa_dbus_dict_string_array_add_element(
  2092. &iter_array, "none")) ||
  2093. !wpa_dbus_dict_end_string_array(&iter_dict,
  2094. &iter_dict_entry,
  2095. &iter_dict_val,
  2096. &iter_array))
  2097. goto nomem;
  2098. }
  2099. /***** group cipher */
  2100. if (res < 0) {
  2101. const char *args[] = {
  2102. "ccmp", "tkip", "wep104", "wep40"
  2103. };
  2104. if (!wpa_dbus_dict_append_string_array(
  2105. &iter_dict, "Group", args,
  2106. ARRAY_SIZE(args)))
  2107. goto nomem;
  2108. } else {
  2109. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Group",
  2110. &iter_dict_entry,
  2111. &iter_dict_val,
  2112. &iter_array) ||
  2113. ((capa.enc & WPA_DRIVER_CAPA_ENC_CCMP_256) &&
  2114. !wpa_dbus_dict_string_array_add_element(
  2115. &iter_array, "ccmp-256")) ||
  2116. ((capa.enc & WPA_DRIVER_CAPA_ENC_GCMP_256) &&
  2117. !wpa_dbus_dict_string_array_add_element(
  2118. &iter_array, "gcmp-256")) ||
  2119. ((capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) &&
  2120. !wpa_dbus_dict_string_array_add_element(
  2121. &iter_array, "ccmp")) ||
  2122. ((capa.enc & WPA_DRIVER_CAPA_ENC_GCMP) &&
  2123. !wpa_dbus_dict_string_array_add_element(
  2124. &iter_array, "gcmp")) ||
  2125. ((capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) &&
  2126. !wpa_dbus_dict_string_array_add_element(
  2127. &iter_array, "tkip")) ||
  2128. ((capa.enc & WPA_DRIVER_CAPA_ENC_WEP104) &&
  2129. !wpa_dbus_dict_string_array_add_element(
  2130. &iter_array, "wep104")) ||
  2131. ((capa.enc & WPA_DRIVER_CAPA_ENC_WEP40) &&
  2132. !wpa_dbus_dict_string_array_add_element(
  2133. &iter_array, "wep40")) ||
  2134. !wpa_dbus_dict_end_string_array(&iter_dict,
  2135. &iter_dict_entry,
  2136. &iter_dict_val,
  2137. &iter_array))
  2138. goto nomem;
  2139. }
  2140. /***** key management */
  2141. if (res < 0) {
  2142. const char *args[] = {
  2143. "wpa-psk", "wpa-eap", "ieee8021x", "wpa-none",
  2144. #ifdef CONFIG_WPS
  2145. "wps",
  2146. #endif /* CONFIG_WPS */
  2147. "none"
  2148. };
  2149. if (!wpa_dbus_dict_append_string_array(
  2150. &iter_dict, "KeyMgmt", args,
  2151. ARRAY_SIZE(args)))
  2152. goto nomem;
  2153. } else {
  2154. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "KeyMgmt",
  2155. &iter_dict_entry,
  2156. &iter_dict_val,
  2157. &iter_array) ||
  2158. !wpa_dbus_dict_string_array_add_element(&iter_array,
  2159. "none") ||
  2160. !wpa_dbus_dict_string_array_add_element(&iter_array,
  2161. "ieee8021x"))
  2162. goto nomem;
  2163. if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2164. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  2165. if (!wpa_dbus_dict_string_array_add_element(
  2166. &iter_array, "wpa-eap") ||
  2167. ((capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT) &&
  2168. !wpa_dbus_dict_string_array_add_element(
  2169. &iter_array, "wpa-ft-eap")))
  2170. goto nomem;
  2171. /* TODO: Ensure that driver actually supports sha256 encryption. */
  2172. #ifdef CONFIG_IEEE80211W
  2173. if (!wpa_dbus_dict_string_array_add_element(
  2174. &iter_array, "wpa-eap-sha256"))
  2175. goto nomem;
  2176. #endif /* CONFIG_IEEE80211W */
  2177. }
  2178. if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  2179. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  2180. if (!wpa_dbus_dict_string_array_add_element(
  2181. &iter_array, "wpa-psk") ||
  2182. ((capa.key_mgmt &
  2183. WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK) &&
  2184. !wpa_dbus_dict_string_array_add_element(
  2185. &iter_array, "wpa-ft-psk")))
  2186. goto nomem;
  2187. /* TODO: Ensure that driver actually supports sha256 encryption. */
  2188. #ifdef CONFIG_IEEE80211W
  2189. if (!wpa_dbus_dict_string_array_add_element(
  2190. &iter_array, "wpa-psk-sha256"))
  2191. goto nomem;
  2192. #endif /* CONFIG_IEEE80211W */
  2193. }
  2194. if ((capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) &&
  2195. !wpa_dbus_dict_string_array_add_element(&iter_array,
  2196. "wpa-none"))
  2197. goto nomem;
  2198. #ifdef CONFIG_WPS
  2199. if (!wpa_dbus_dict_string_array_add_element(&iter_array,
  2200. "wps"))
  2201. goto nomem;
  2202. #endif /* CONFIG_WPS */
  2203. if (!wpa_dbus_dict_end_string_array(&iter_dict,
  2204. &iter_dict_entry,
  2205. &iter_dict_val,
  2206. &iter_array))
  2207. goto nomem;
  2208. }
  2209. /***** WPA protocol */
  2210. if (res < 0) {
  2211. const char *args[] = { "rsn", "wpa" };
  2212. if (!wpa_dbus_dict_append_string_array(
  2213. &iter_dict, "Protocol", args,
  2214. ARRAY_SIZE(args)))
  2215. goto nomem;
  2216. } else {
  2217. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Protocol",
  2218. &iter_dict_entry,
  2219. &iter_dict_val,
  2220. &iter_array) ||
  2221. ((capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  2222. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) &&
  2223. !wpa_dbus_dict_string_array_add_element(
  2224. &iter_array, "rsn")) ||
  2225. ((capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2226. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) &&
  2227. !wpa_dbus_dict_string_array_add_element(
  2228. &iter_array, "wpa")) ||
  2229. !wpa_dbus_dict_end_string_array(&iter_dict,
  2230. &iter_dict_entry,
  2231. &iter_dict_val,
  2232. &iter_array))
  2233. goto nomem;
  2234. }
  2235. /***** auth alg */
  2236. if (res < 0) {
  2237. const char *args[] = { "open", "shared", "leap" };
  2238. if (!wpa_dbus_dict_append_string_array(
  2239. &iter_dict, "AuthAlg", args,
  2240. ARRAY_SIZE(args)))
  2241. goto nomem;
  2242. } else {
  2243. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "AuthAlg",
  2244. &iter_dict_entry,
  2245. &iter_dict_val,
  2246. &iter_array))
  2247. goto nomem;
  2248. if (((capa.auth & WPA_DRIVER_AUTH_OPEN) &&
  2249. !wpa_dbus_dict_string_array_add_element(
  2250. &iter_array, "open")) ||
  2251. ((capa.auth & WPA_DRIVER_AUTH_SHARED) &&
  2252. !wpa_dbus_dict_string_array_add_element(
  2253. &iter_array, "shared")) ||
  2254. ((capa.auth & WPA_DRIVER_AUTH_LEAP) &&
  2255. !wpa_dbus_dict_string_array_add_element(
  2256. &iter_array, "leap")) ||
  2257. !wpa_dbus_dict_end_string_array(&iter_dict,
  2258. &iter_dict_entry,
  2259. &iter_dict_val,
  2260. &iter_array))
  2261. goto nomem;
  2262. }
  2263. /***** Scan */
  2264. if (!wpa_dbus_dict_append_string_array(&iter_dict, "Scan", scans,
  2265. ARRAY_SIZE(scans)))
  2266. goto nomem;
  2267. /***** Modes */
  2268. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Modes",
  2269. &iter_dict_entry,
  2270. &iter_dict_val,
  2271. &iter_array) ||
  2272. !wpa_dbus_dict_string_array_add_element(
  2273. &iter_array, "infrastructure") ||
  2274. !wpa_dbus_dict_string_array_add_element(
  2275. &iter_array, "ad-hoc") ||
  2276. (res >= 0 && (capa.flags & WPA_DRIVER_FLAGS_AP) &&
  2277. !wpa_dbus_dict_string_array_add_element(
  2278. &iter_array, "ap")) ||
  2279. (res >= 0 && (capa.flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
  2280. !wpa_dbus_dict_string_array_add_element(
  2281. &iter_array, "p2p")) ||
  2282. !wpa_dbus_dict_end_string_array(&iter_dict,
  2283. &iter_dict_entry,
  2284. &iter_dict_val,
  2285. &iter_array))
  2286. goto nomem;
  2287. /***** Modes end */
  2288. if (res >= 0) {
  2289. dbus_int32_t max_scan_ssid = capa.max_scan_ssids;
  2290. if (!wpa_dbus_dict_append_int32(&iter_dict, "MaxScanSSID",
  2291. max_scan_ssid))
  2292. goto nomem;
  2293. }
  2294. if (!wpa_dbus_dict_close_write(&variant_iter, &iter_dict) ||
  2295. !dbus_message_iter_close_container(iter, &variant_iter))
  2296. goto nomem;
  2297. return TRUE;
  2298. nomem:
  2299. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  2300. return FALSE;
  2301. }
  2302. /**
  2303. * wpas_dbus_getter_state - Get interface state
  2304. * @iter: Pointer to incoming dbus message iter
  2305. * @error: Location to store error on failure
  2306. * @user_data: Function specific data
  2307. * Returns: TRUE on success, FALSE on failure
  2308. *
  2309. * Getter for "State" property.
  2310. */
  2311. dbus_bool_t wpas_dbus_getter_state(
  2312. const struct wpa_dbus_property_desc *property_desc,
  2313. DBusMessageIter *iter, DBusError *error, void *user_data)
  2314. {
  2315. struct wpa_supplicant *wpa_s = user_data;
  2316. const char *str_state;
  2317. char *state_ls, *tmp;
  2318. dbus_bool_t success = FALSE;
  2319. str_state = wpa_supplicant_state_txt(wpa_s->wpa_state);
  2320. /* make state string lowercase to fit new DBus API convention
  2321. */
  2322. state_ls = tmp = os_strdup(str_state);
  2323. if (!tmp) {
  2324. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  2325. return FALSE;
  2326. }
  2327. while (*tmp) {
  2328. *tmp = tolower(*tmp);
  2329. tmp++;
  2330. }
  2331. success = wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  2332. &state_ls, error);
  2333. os_free(state_ls);
  2334. return success;
  2335. }
  2336. /**
  2337. * wpas_dbus_new_iface_get_scanning - Get interface scanning state
  2338. * @iter: Pointer to incoming dbus message iter
  2339. * @error: Location to store error on failure
  2340. * @user_data: Function specific data
  2341. * Returns: TRUE on success, FALSE on failure
  2342. *
  2343. * Getter for "scanning" property.
  2344. */
  2345. dbus_bool_t wpas_dbus_getter_scanning(
  2346. const struct wpa_dbus_property_desc *property_desc,
  2347. DBusMessageIter *iter, DBusError *error, void *user_data)
  2348. {
  2349. struct wpa_supplicant *wpa_s = user_data;
  2350. dbus_bool_t scanning = wpa_s->scanning ? TRUE : FALSE;
  2351. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_BOOLEAN,
  2352. &scanning, error);
  2353. }
  2354. /**
  2355. * wpas_dbus_getter_ap_scan - Control roaming mode
  2356. * @iter: Pointer to incoming dbus message iter
  2357. * @error: Location to store error on failure
  2358. * @user_data: Function specific data
  2359. * Returns: TRUE on success, FALSE on failure
  2360. *
  2361. * Getter function for "ApScan" property.
  2362. */
  2363. dbus_bool_t wpas_dbus_getter_ap_scan(
  2364. const struct wpa_dbus_property_desc *property_desc,
  2365. DBusMessageIter *iter, DBusError *error, void *user_data)
  2366. {
  2367. struct wpa_supplicant *wpa_s = user_data;
  2368. dbus_uint32_t ap_scan = wpa_s->conf->ap_scan;
  2369. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_UINT32,
  2370. &ap_scan, error);
  2371. }
  2372. /**
  2373. * wpas_dbus_setter_ap_scan - Control roaming mode
  2374. * @iter: Pointer to incoming dbus message iter
  2375. * @error: Location to store error on failure
  2376. * @user_data: Function specific data
  2377. * Returns: TRUE on success, FALSE on failure
  2378. *
  2379. * Setter function for "ApScan" property.
  2380. */
  2381. dbus_bool_t wpas_dbus_setter_ap_scan(
  2382. const struct wpa_dbus_property_desc *property_desc,
  2383. DBusMessageIter *iter, DBusError *error, void *user_data)
  2384. {
  2385. struct wpa_supplicant *wpa_s = user_data;
  2386. dbus_uint32_t ap_scan;
  2387. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_UINT32,
  2388. &ap_scan))
  2389. return FALSE;
  2390. if (wpa_supplicant_set_ap_scan(wpa_s, ap_scan)) {
  2391. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  2392. "ap_scan must be 0, 1, or 2");
  2393. return FALSE;
  2394. }
  2395. return TRUE;
  2396. }
  2397. /**
  2398. * wpas_dbus_getter_fast_reauth - Control fast
  2399. * reauthentication (TLS session resumption)
  2400. * @iter: Pointer to incoming dbus message iter
  2401. * @error: Location to store error on failure
  2402. * @user_data: Function specific data
  2403. * Returns: TRUE on success, FALSE on failure
  2404. *
  2405. * Getter function for "FastReauth" property.
  2406. */
  2407. dbus_bool_t wpas_dbus_getter_fast_reauth(
  2408. const struct wpa_dbus_property_desc *property_desc,
  2409. DBusMessageIter *iter, DBusError *error, void *user_data)
  2410. {
  2411. struct wpa_supplicant *wpa_s = user_data;
  2412. dbus_bool_t fast_reauth = wpa_s->conf->fast_reauth ? TRUE : FALSE;
  2413. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_BOOLEAN,
  2414. &fast_reauth, error);
  2415. }
  2416. /**
  2417. * wpas_dbus_setter_fast_reauth - Control fast
  2418. * reauthentication (TLS session resumption)
  2419. * @iter: Pointer to incoming dbus message iter
  2420. * @error: Location to store error on failure
  2421. * @user_data: Function specific data
  2422. * Returns: TRUE on success, FALSE on failure
  2423. *
  2424. * Setter function for "FastReauth" property.
  2425. */
  2426. dbus_bool_t wpas_dbus_setter_fast_reauth(
  2427. const struct wpa_dbus_property_desc *property_desc,
  2428. DBusMessageIter *iter, DBusError *error, void *user_data)
  2429. {
  2430. struct wpa_supplicant *wpa_s = user_data;
  2431. dbus_bool_t fast_reauth;
  2432. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_BOOLEAN,
  2433. &fast_reauth))
  2434. return FALSE;
  2435. wpa_s->conf->fast_reauth = fast_reauth;
  2436. return TRUE;
  2437. }
  2438. /**
  2439. * wpas_dbus_getter_disconnect_reason - Get most recent reason for disconnect
  2440. * @iter: Pointer to incoming dbus message iter
  2441. * @error: Location to store error on failure
  2442. * @user_data: Function specific data
  2443. * Returns: TRUE on success, FALSE on failure
  2444. *
  2445. * Getter for "DisconnectReason" property. The reason is negative if it is
  2446. * locally generated.
  2447. */
  2448. dbus_bool_t wpas_dbus_getter_disconnect_reason(
  2449. const struct wpa_dbus_property_desc *property_desc,
  2450. DBusMessageIter *iter, DBusError *error, void *user_data)
  2451. {
  2452. struct wpa_supplicant *wpa_s = user_data;
  2453. dbus_int32_t reason = wpa_s->disconnect_reason;
  2454. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_INT32,
  2455. &reason, error);
  2456. }
  2457. /**
  2458. * wpas_dbus_getter_bss_expire_age - Get BSS entry expiration age
  2459. * @iter: Pointer to incoming dbus message iter
  2460. * @error: Location to store error on failure
  2461. * @user_data: Function specific data
  2462. * Returns: TRUE on success, FALSE on failure
  2463. *
  2464. * Getter function for "BSSExpireAge" property.
  2465. */
  2466. dbus_bool_t wpas_dbus_getter_bss_expire_age(
  2467. const struct wpa_dbus_property_desc *property_desc,
  2468. DBusMessageIter *iter, DBusError *error, void *user_data)
  2469. {
  2470. struct wpa_supplicant *wpa_s = user_data;
  2471. dbus_uint32_t expire_age = wpa_s->conf->bss_expiration_age;
  2472. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_UINT32,
  2473. &expire_age, error);
  2474. }
  2475. /**
  2476. * wpas_dbus_setter_bss_expire_age - Control BSS entry expiration age
  2477. * @iter: Pointer to incoming dbus message iter
  2478. * @error: Location to store error on failure
  2479. * @user_data: Function specific data
  2480. * Returns: TRUE on success, FALSE on failure
  2481. *
  2482. * Setter function for "BSSExpireAge" property.
  2483. */
  2484. dbus_bool_t wpas_dbus_setter_bss_expire_age(
  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_age;
  2490. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_UINT32,
  2491. &expire_age))
  2492. return FALSE;
  2493. if (wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age)) {
  2494. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  2495. "BSSExpireAge must be >= 10");
  2496. return FALSE;
  2497. }
  2498. return TRUE;
  2499. }
  2500. /**
  2501. * wpas_dbus_getter_bss_expire_count - Get BSS entry expiration scan count
  2502. * @iter: Pointer to incoming dbus message iter
  2503. * @error: Location to store error on failure
  2504. * @user_data: Function specific data
  2505. * Returns: TRUE on success, FALSE on failure
  2506. *
  2507. * Getter function for "BSSExpireCount" property.
  2508. */
  2509. dbus_bool_t wpas_dbus_getter_bss_expire_count(
  2510. const struct wpa_dbus_property_desc *property_desc,
  2511. DBusMessageIter *iter, DBusError *error, void *user_data)
  2512. {
  2513. struct wpa_supplicant *wpa_s = user_data;
  2514. dbus_uint32_t expire_count = wpa_s->conf->bss_expiration_scan_count;
  2515. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_UINT32,
  2516. &expire_count, error);
  2517. }
  2518. /**
  2519. * wpas_dbus_setter_bss_expire_count - Control BSS entry expiration scan count
  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. * Setter function for "BSSExpireCount" property.
  2526. */
  2527. dbus_bool_t wpas_dbus_setter_bss_expire_count(
  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. dbus_uint32_t expire_count;
  2533. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_UINT32,
  2534. &expire_count))
  2535. return FALSE;
  2536. if (wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count)) {
  2537. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  2538. "BSSExpireCount must be > 0");
  2539. return FALSE;
  2540. }
  2541. return TRUE;
  2542. }
  2543. /**
  2544. * wpas_dbus_getter_country - Control country code
  2545. * @iter: Pointer to incoming dbus message iter
  2546. * @error: Location to store error on failure
  2547. * @user_data: Function specific data
  2548. * Returns: TRUE on success, FALSE on failure
  2549. *
  2550. * Getter function for "Country" property.
  2551. */
  2552. dbus_bool_t wpas_dbus_getter_country(
  2553. const struct wpa_dbus_property_desc *property_desc,
  2554. DBusMessageIter *iter, DBusError *error, void *user_data)
  2555. {
  2556. struct wpa_supplicant *wpa_s = user_data;
  2557. char country[3];
  2558. char *str = country;
  2559. country[0] = wpa_s->conf->country[0];
  2560. country[1] = wpa_s->conf->country[1];
  2561. country[2] = '\0';
  2562. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  2563. &str, error);
  2564. }
  2565. /**
  2566. * wpas_dbus_setter_country - Control country code
  2567. * @iter: Pointer to incoming dbus message iter
  2568. * @error: Location to store error on failure
  2569. * @user_data: Function specific data
  2570. * Returns: TRUE on success, FALSE on failure
  2571. *
  2572. * Setter function for "Country" property.
  2573. */
  2574. dbus_bool_t wpas_dbus_setter_country(
  2575. const struct wpa_dbus_property_desc *property_desc,
  2576. DBusMessageIter *iter, DBusError *error, void *user_data)
  2577. {
  2578. struct wpa_supplicant *wpa_s = user_data;
  2579. const char *country;
  2580. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_STRING,
  2581. &country))
  2582. return FALSE;
  2583. if (!country[0] || !country[1]) {
  2584. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  2585. "invalid country code");
  2586. return FALSE;
  2587. }
  2588. if (wpa_s->drv_priv != NULL && wpa_drv_set_country(wpa_s, country)) {
  2589. wpa_printf(MSG_DEBUG, "Failed to set country");
  2590. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  2591. "failed to set country code");
  2592. return FALSE;
  2593. }
  2594. wpa_s->conf->country[0] = country[0];
  2595. wpa_s->conf->country[1] = country[1];
  2596. return TRUE;
  2597. }
  2598. /**
  2599. * wpas_dbus_getter_scan_interval - Get scan interval
  2600. * @iter: Pointer to incoming dbus message iter
  2601. * @error: Location to store error on failure
  2602. * @user_data: Function specific data
  2603. * Returns: TRUE on success, FALSE on failure
  2604. *
  2605. * Getter function for "ScanInterval" property.
  2606. */
  2607. dbus_bool_t wpas_dbus_getter_scan_interval(
  2608. const struct wpa_dbus_property_desc *property_desc,
  2609. DBusMessageIter *iter, DBusError *error, void *user_data)
  2610. {
  2611. struct wpa_supplicant *wpa_s = user_data;
  2612. dbus_int32_t scan_interval = wpa_s->scan_interval;
  2613. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_INT32,
  2614. &scan_interval, error);
  2615. }
  2616. /**
  2617. * wpas_dbus_setter_scan_interval - Control scan interval
  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. * Setter function for "ScanInterval" property.
  2624. */
  2625. dbus_bool_t wpas_dbus_setter_scan_interval(
  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. dbus_int32_t scan_interval;
  2631. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_INT32,
  2632. &scan_interval))
  2633. return FALSE;
  2634. if (wpa_supplicant_set_scan_interval(wpa_s, scan_interval)) {
  2635. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  2636. "scan_interval must be >= 0");
  2637. return FALSE;
  2638. }
  2639. return TRUE;
  2640. }
  2641. /**
  2642. * wpas_dbus_getter_ifname - Get interface name
  2643. * @iter: Pointer to incoming dbus message iter
  2644. * @error: Location to store error on failure
  2645. * @user_data: Function specific data
  2646. * Returns: TRUE on success, FALSE on failure
  2647. *
  2648. * Getter for "Ifname" property.
  2649. */
  2650. dbus_bool_t wpas_dbus_getter_ifname(
  2651. const struct wpa_dbus_property_desc *property_desc,
  2652. DBusMessageIter *iter, DBusError *error, void *user_data)
  2653. {
  2654. struct wpa_supplicant *wpa_s = user_data;
  2655. const char *ifname = wpa_s->ifname;
  2656. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  2657. &ifname, error);
  2658. }
  2659. /**
  2660. * wpas_dbus_getter_driver - Get interface name
  2661. * @iter: Pointer to incoming dbus message iter
  2662. * @error: Location to store error on failure
  2663. * @user_data: Function specific data
  2664. * Returns: TRUE on success, FALSE on failure
  2665. *
  2666. * Getter for "Driver" property.
  2667. */
  2668. dbus_bool_t wpas_dbus_getter_driver(
  2669. const struct wpa_dbus_property_desc *property_desc,
  2670. DBusMessageIter *iter, DBusError *error, void *user_data)
  2671. {
  2672. struct wpa_supplicant *wpa_s = user_data;
  2673. const char *driver;
  2674. if (wpa_s->driver == NULL || wpa_s->driver->name == NULL) {
  2675. wpa_printf(MSG_DEBUG, "%s[dbus]: wpa_s has no driver set",
  2676. __func__);
  2677. dbus_set_error(error, DBUS_ERROR_FAILED, "%s: no driver set",
  2678. __func__);
  2679. return FALSE;
  2680. }
  2681. driver = wpa_s->driver->name;
  2682. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  2683. &driver, error);
  2684. }
  2685. /**
  2686. * wpas_dbus_getter_current_bss - Get current bss object path
  2687. * @iter: Pointer to incoming dbus message iter
  2688. * @error: Location to store error on failure
  2689. * @user_data: Function specific data
  2690. * Returns: TRUE on success, FALSE on failure
  2691. *
  2692. * Getter for "CurrentBSS" property.
  2693. */
  2694. dbus_bool_t wpas_dbus_getter_current_bss(
  2695. const struct wpa_dbus_property_desc *property_desc,
  2696. DBusMessageIter *iter, DBusError *error, void *user_data)
  2697. {
  2698. struct wpa_supplicant *wpa_s = user_data;
  2699. char path_buf[WPAS_DBUS_OBJECT_PATH_MAX], *bss_obj_path = path_buf;
  2700. if (wpa_s->current_bss && wpa_s->dbus_new_path)
  2701. os_snprintf(bss_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
  2702. "%s/" WPAS_DBUS_NEW_BSSIDS_PART "/%u",
  2703. wpa_s->dbus_new_path, wpa_s->current_bss->id);
  2704. else
  2705. os_snprintf(bss_obj_path, WPAS_DBUS_OBJECT_PATH_MAX, "/");
  2706. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_OBJECT_PATH,
  2707. &bss_obj_path, error);
  2708. }
  2709. /**
  2710. * wpas_dbus_getter_current_network - Get current network object path
  2711. * @iter: Pointer to incoming dbus message iter
  2712. * @error: Location to store error on failure
  2713. * @user_data: Function specific data
  2714. * Returns: TRUE on success, FALSE on failure
  2715. *
  2716. * Getter for "CurrentNetwork" property.
  2717. */
  2718. dbus_bool_t wpas_dbus_getter_current_network(
  2719. const struct wpa_dbus_property_desc *property_desc,
  2720. DBusMessageIter *iter, DBusError *error, void *user_data)
  2721. {
  2722. struct wpa_supplicant *wpa_s = user_data;
  2723. char path_buf[WPAS_DBUS_OBJECT_PATH_MAX], *net_obj_path = path_buf;
  2724. if (wpa_s->current_ssid && wpa_s->dbus_new_path)
  2725. os_snprintf(net_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
  2726. "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%u",
  2727. wpa_s->dbus_new_path, wpa_s->current_ssid->id);
  2728. else
  2729. os_snprintf(net_obj_path, WPAS_DBUS_OBJECT_PATH_MAX, "/");
  2730. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_OBJECT_PATH,
  2731. &net_obj_path, error);
  2732. }
  2733. /**
  2734. * wpas_dbus_getter_current_auth_mode - Get current authentication type
  2735. * @iter: Pointer to incoming dbus message iter
  2736. * @error: Location to store error on failure
  2737. * @user_data: Function specific data
  2738. * Returns: TRUE on success, FALSE on failure
  2739. *
  2740. * Getter for "CurrentAuthMode" property.
  2741. */
  2742. dbus_bool_t wpas_dbus_getter_current_auth_mode(
  2743. const struct wpa_dbus_property_desc *property_desc,
  2744. DBusMessageIter *iter, DBusError *error, void *user_data)
  2745. {
  2746. struct wpa_supplicant *wpa_s = user_data;
  2747. const char *eap_mode;
  2748. const char *auth_mode;
  2749. char eap_mode_buf[WPAS_DBUS_AUTH_MODE_MAX];
  2750. if (wpa_s->wpa_state != WPA_COMPLETED) {
  2751. auth_mode = "INACTIVE";
  2752. } else if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X ||
  2753. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  2754. eap_mode = wpa_supplicant_get_eap_mode(wpa_s);
  2755. os_snprintf(eap_mode_buf, WPAS_DBUS_AUTH_MODE_MAX,
  2756. "EAP-%s", eap_mode);
  2757. auth_mode = eap_mode_buf;
  2758. } else {
  2759. auth_mode = wpa_key_mgmt_txt(wpa_s->key_mgmt,
  2760. wpa_s->current_ssid->proto);
  2761. }
  2762. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  2763. &auth_mode, error);
  2764. }
  2765. /**
  2766. * wpas_dbus_getter_bridge_ifname - Get interface name
  2767. * @iter: Pointer to incoming dbus message iter
  2768. * @error: Location to store error on failure
  2769. * @user_data: Function specific data
  2770. * Returns: TRUE on success, FALSE on failure
  2771. *
  2772. * Getter for "BridgeIfname" property.
  2773. */
  2774. dbus_bool_t wpas_dbus_getter_bridge_ifname(
  2775. const struct wpa_dbus_property_desc *property_desc,
  2776. DBusMessageIter *iter, DBusError *error, void *user_data)
  2777. {
  2778. struct wpa_supplicant *wpa_s = user_data;
  2779. const char *bridge_ifname = wpa_s->bridge_ifname;
  2780. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  2781. &bridge_ifname, error);
  2782. }
  2783. /**
  2784. * wpas_dbus_getter_bsss - Get array of BSSs objects
  2785. * @iter: Pointer to incoming dbus message iter
  2786. * @error: Location to store error on failure
  2787. * @user_data: Function specific data
  2788. * Returns: TRUE on success, FALSE on failure
  2789. *
  2790. * Getter for "BSSs" property.
  2791. */
  2792. dbus_bool_t wpas_dbus_getter_bsss(
  2793. const struct wpa_dbus_property_desc *property_desc,
  2794. DBusMessageIter *iter, DBusError *error, void *user_data)
  2795. {
  2796. struct wpa_supplicant *wpa_s = user_data;
  2797. struct wpa_bss *bss;
  2798. char **paths;
  2799. unsigned int i = 0;
  2800. dbus_bool_t success = FALSE;
  2801. if (!wpa_s->dbus_new_path) {
  2802. dbus_set_error(error, DBUS_ERROR_FAILED,
  2803. "%s: no D-Bus interface", __func__);
  2804. return FALSE;
  2805. }
  2806. paths = os_calloc(wpa_s->num_bss, sizeof(char *));
  2807. if (!paths) {
  2808. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  2809. return FALSE;
  2810. }
  2811. /* Loop through scan results and append each result's object path */
  2812. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  2813. paths[i] = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  2814. if (paths[i] == NULL) {
  2815. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY,
  2816. "no memory");
  2817. goto out;
  2818. }
  2819. /* Construct the object path for this BSS. */
  2820. os_snprintf(paths[i++], WPAS_DBUS_OBJECT_PATH_MAX,
  2821. "%s/" WPAS_DBUS_NEW_BSSIDS_PART "/%u",
  2822. wpa_s->dbus_new_path, bss->id);
  2823. }
  2824. success = wpas_dbus_simple_array_property_getter(iter,
  2825. DBUS_TYPE_OBJECT_PATH,
  2826. paths, wpa_s->num_bss,
  2827. error);
  2828. out:
  2829. while (i)
  2830. os_free(paths[--i]);
  2831. os_free(paths);
  2832. return success;
  2833. }
  2834. /**
  2835. * wpas_dbus_getter_networks - Get array of networks objects
  2836. * @iter: Pointer to incoming dbus message iter
  2837. * @error: Location to store error on failure
  2838. * @user_data: Function specific data
  2839. * Returns: TRUE on success, FALSE on failure
  2840. *
  2841. * Getter for "Networks" property.
  2842. */
  2843. dbus_bool_t wpas_dbus_getter_networks(
  2844. const struct wpa_dbus_property_desc *property_desc,
  2845. DBusMessageIter *iter, DBusError *error, void *user_data)
  2846. {
  2847. struct wpa_supplicant *wpa_s = user_data;
  2848. struct wpa_ssid *ssid;
  2849. char **paths;
  2850. unsigned int i = 0, num = 0;
  2851. dbus_bool_t success = FALSE;
  2852. if (!wpa_s->dbus_new_path) {
  2853. dbus_set_error(error, DBUS_ERROR_FAILED,
  2854. "%s: no D-Bus interface", __func__);
  2855. return FALSE;
  2856. }
  2857. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
  2858. if (!network_is_persistent_group(ssid))
  2859. num++;
  2860. paths = os_calloc(num, sizeof(char *));
  2861. if (!paths) {
  2862. dbus_set_error(error, DBUS_ERROR_NO_MEMORY, "no memory");
  2863. return FALSE;
  2864. }
  2865. /* Loop through configured networks and append object path of each */
  2866. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  2867. if (network_is_persistent_group(ssid))
  2868. continue;
  2869. paths[i] = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  2870. if (paths[i] == NULL) {
  2871. dbus_set_error(error, DBUS_ERROR_NO_MEMORY,
  2872. "no memory");
  2873. goto out;
  2874. }
  2875. /* Construct the object path for this network. */
  2876. os_snprintf(paths[i++], WPAS_DBUS_OBJECT_PATH_MAX,
  2877. "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%d",
  2878. wpa_s->dbus_new_path, ssid->id);
  2879. }
  2880. success = wpas_dbus_simple_array_property_getter(iter,
  2881. DBUS_TYPE_OBJECT_PATH,
  2882. paths, num, error);
  2883. out:
  2884. while (i)
  2885. os_free(paths[--i]);
  2886. os_free(paths);
  2887. return success;
  2888. }
  2889. /**
  2890. * wpas_dbus_getter_pkcs11_engine_path - Get PKCS #11 engine path
  2891. * @iter: Pointer to incoming dbus message iter
  2892. * @error: Location to store error on failure
  2893. * @user_data: Function specific data
  2894. * Returns: A dbus message containing the PKCS #11 engine path
  2895. *
  2896. * Getter for "PKCS11EnginePath" property.
  2897. */
  2898. dbus_bool_t wpas_dbus_getter_pkcs11_engine_path(
  2899. const struct wpa_dbus_property_desc *property_desc,
  2900. DBusMessageIter *iter, DBusError *error, void *user_data)
  2901. {
  2902. struct wpa_supplicant *wpa_s = user_data;
  2903. const char *pkcs11_engine_path;
  2904. if (wpa_s->conf->pkcs11_engine_path == NULL)
  2905. pkcs11_engine_path = "";
  2906. else
  2907. pkcs11_engine_path = wpa_s->conf->pkcs11_engine_path;
  2908. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  2909. &pkcs11_engine_path, error);
  2910. }
  2911. /**
  2912. * wpas_dbus_getter_pkcs11_module_path - Get PKCS #11 module path
  2913. * @iter: Pointer to incoming dbus message iter
  2914. * @error: Location to store error on failure
  2915. * @user_data: Function specific data
  2916. * Returns: A dbus message containing the PKCS #11 module path
  2917. *
  2918. * Getter for "PKCS11ModulePath" property.
  2919. */
  2920. dbus_bool_t wpas_dbus_getter_pkcs11_module_path(
  2921. const struct wpa_dbus_property_desc *property_desc,
  2922. DBusMessageIter *iter, DBusError *error, void *user_data)
  2923. {
  2924. struct wpa_supplicant *wpa_s = user_data;
  2925. const char *pkcs11_module_path;
  2926. if (wpa_s->conf->pkcs11_module_path == NULL)
  2927. pkcs11_module_path = "";
  2928. else
  2929. pkcs11_module_path = wpa_s->conf->pkcs11_module_path;
  2930. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  2931. &pkcs11_module_path, error);
  2932. }
  2933. /**
  2934. * wpas_dbus_getter_blobs - Get all blobs defined for this interface
  2935. * @iter: Pointer to incoming dbus message iter
  2936. * @error: Location to store error on failure
  2937. * @user_data: Function specific data
  2938. * Returns: TRUE on success, FALSE on failure
  2939. *
  2940. * Getter for "Blobs" property.
  2941. */
  2942. dbus_bool_t wpas_dbus_getter_blobs(
  2943. const struct wpa_dbus_property_desc *property_desc,
  2944. DBusMessageIter *iter, DBusError *error, void *user_data)
  2945. {
  2946. struct wpa_supplicant *wpa_s = user_data;
  2947. DBusMessageIter variant_iter, dict_iter, entry_iter, array_iter;
  2948. struct wpa_config_blob *blob;
  2949. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
  2950. "a{say}", &variant_iter) ||
  2951. !dbus_message_iter_open_container(&variant_iter, DBUS_TYPE_ARRAY,
  2952. "{say}", &dict_iter)) {
  2953. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  2954. return FALSE;
  2955. }
  2956. blob = wpa_s->conf->blobs;
  2957. while (blob) {
  2958. if (!dbus_message_iter_open_container(&dict_iter,
  2959. DBUS_TYPE_DICT_ENTRY,
  2960. NULL, &entry_iter) ||
  2961. !dbus_message_iter_append_basic(&entry_iter,
  2962. DBUS_TYPE_STRING,
  2963. &(blob->name)) ||
  2964. !dbus_message_iter_open_container(&entry_iter,
  2965. DBUS_TYPE_ARRAY,
  2966. DBUS_TYPE_BYTE_AS_STRING,
  2967. &array_iter) ||
  2968. !dbus_message_iter_append_fixed_array(&array_iter,
  2969. DBUS_TYPE_BYTE,
  2970. &(blob->data),
  2971. blob->len) ||
  2972. !dbus_message_iter_close_container(&entry_iter,
  2973. &array_iter) ||
  2974. !dbus_message_iter_close_container(&dict_iter,
  2975. &entry_iter)) {
  2976. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY,
  2977. "no memory");
  2978. return FALSE;
  2979. }
  2980. blob = blob->next;
  2981. }
  2982. if (!dbus_message_iter_close_container(&variant_iter, &dict_iter) ||
  2983. !dbus_message_iter_close_container(iter, &variant_iter)) {
  2984. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  2985. return FALSE;
  2986. }
  2987. return TRUE;
  2988. }
  2989. dbus_bool_t wpas_dbus_getter_iface_global(
  2990. const struct wpa_dbus_property_desc *property_desc,
  2991. DBusMessageIter *iter, DBusError *error, void *user_data)
  2992. {
  2993. struct wpa_supplicant *wpa_s = user_data;
  2994. int ret;
  2995. char buf[250];
  2996. char *p = buf;
  2997. if (!property_desc->data) {
  2998. dbus_set_error(error, DBUS_ERROR_INVALID_ARGS,
  2999. "Unhandled interface property %s",
  3000. property_desc->dbus_property);
  3001. return FALSE;
  3002. }
  3003. ret = wpa_config_get_value(property_desc->data, wpa_s->conf, buf,
  3004. sizeof(buf));
  3005. if (ret < 0)
  3006. *p = '\0';
  3007. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING, &p,
  3008. error);
  3009. }
  3010. dbus_bool_t wpas_dbus_setter_iface_global(
  3011. const struct wpa_dbus_property_desc *property_desc,
  3012. DBusMessageIter *iter, DBusError *error, void *user_data)
  3013. {
  3014. struct wpa_supplicant *wpa_s = user_data;
  3015. const char *new_value = NULL;
  3016. char buf[250];
  3017. size_t combined_len;
  3018. int ret;
  3019. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_STRING,
  3020. &new_value))
  3021. return FALSE;
  3022. combined_len = os_strlen(property_desc->data) + os_strlen(new_value) +
  3023. 3;
  3024. if (combined_len >= sizeof(buf)) {
  3025. dbus_set_error(error, DBUS_ERROR_INVALID_ARGS,
  3026. "Interface property %s value too large",
  3027. property_desc->dbus_property);
  3028. return FALSE;
  3029. }
  3030. if (!new_value[0])
  3031. new_value = "NULL";
  3032. ret = os_snprintf(buf, combined_len, "%s=%s", property_desc->data,
  3033. new_value);
  3034. if (os_snprintf_error(combined_len, ret)) {
  3035. dbus_set_error(error, WPAS_DBUS_ERROR_UNKNOWN_ERROR,
  3036. "Failed to construct new interface property %s",
  3037. property_desc->dbus_property);
  3038. return FALSE;
  3039. }
  3040. if (wpa_config_process_global(wpa_s->conf, buf, -1)) {
  3041. dbus_set_error(error, DBUS_ERROR_INVALID_ARGS,
  3042. "Failed to set interface property %s",
  3043. property_desc->dbus_property);
  3044. return FALSE;
  3045. }
  3046. wpa_supplicant_update_config(wpa_s);
  3047. return TRUE;
  3048. }
  3049. static struct wpa_bss * get_bss_helper(struct bss_handler_args *args,
  3050. DBusError *error, const char *func_name)
  3051. {
  3052. struct wpa_bss *res = wpa_bss_get_id(args->wpa_s, args->id);
  3053. if (!res) {
  3054. wpa_printf(MSG_ERROR, "%s[dbus]: no bss with id %d found",
  3055. func_name, args->id);
  3056. dbus_set_error(error, DBUS_ERROR_FAILED,
  3057. "%s: BSS %d not found",
  3058. func_name, args->id);
  3059. }
  3060. return res;
  3061. }
  3062. /**
  3063. * wpas_dbus_getter_bss_bssid - Return the BSSID of a BSS
  3064. * @iter: Pointer to incoming dbus message iter
  3065. * @error: Location to store error on failure
  3066. * @user_data: Function specific data
  3067. * Returns: TRUE on success, FALSE on failure
  3068. *
  3069. * Getter for "BSSID" property.
  3070. */
  3071. dbus_bool_t wpas_dbus_getter_bss_bssid(
  3072. const struct wpa_dbus_property_desc *property_desc,
  3073. DBusMessageIter *iter, DBusError *error, void *user_data)
  3074. {
  3075. struct bss_handler_args *args = user_data;
  3076. struct wpa_bss *res;
  3077. res = get_bss_helper(args, error, __func__);
  3078. if (!res)
  3079. return FALSE;
  3080. return wpas_dbus_simple_array_property_getter(iter, DBUS_TYPE_BYTE,
  3081. res->bssid, ETH_ALEN,
  3082. error);
  3083. }
  3084. /**
  3085. * wpas_dbus_getter_bss_ssid - Return the SSID of a BSS
  3086. * @iter: Pointer to incoming dbus message iter
  3087. * @error: Location to store error on failure
  3088. * @user_data: Function specific data
  3089. * Returns: TRUE on success, FALSE on failure
  3090. *
  3091. * Getter for "SSID" property.
  3092. */
  3093. dbus_bool_t wpas_dbus_getter_bss_ssid(
  3094. const struct wpa_dbus_property_desc *property_desc,
  3095. DBusMessageIter *iter, DBusError *error, void *user_data)
  3096. {
  3097. struct bss_handler_args *args = user_data;
  3098. struct wpa_bss *res;
  3099. res = get_bss_helper(args, error, __func__);
  3100. if (!res)
  3101. return FALSE;
  3102. return wpas_dbus_simple_array_property_getter(iter, DBUS_TYPE_BYTE,
  3103. res->ssid, res->ssid_len,
  3104. error);
  3105. }
  3106. /**
  3107. * wpas_dbus_getter_bss_privacy - Return the privacy flag of a BSS
  3108. * @iter: Pointer to incoming dbus message iter
  3109. * @error: Location to store error on failure
  3110. * @user_data: Function specific data
  3111. * Returns: TRUE on success, FALSE on failure
  3112. *
  3113. * Getter for "Privacy" property.
  3114. */
  3115. dbus_bool_t wpas_dbus_getter_bss_privacy(
  3116. const struct wpa_dbus_property_desc *property_desc,
  3117. DBusMessageIter *iter, DBusError *error, void *user_data)
  3118. {
  3119. struct bss_handler_args *args = user_data;
  3120. struct wpa_bss *res;
  3121. dbus_bool_t privacy;
  3122. res = get_bss_helper(args, error, __func__);
  3123. if (!res)
  3124. return FALSE;
  3125. privacy = (res->caps & IEEE80211_CAP_PRIVACY) ? TRUE : FALSE;
  3126. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_BOOLEAN,
  3127. &privacy, error);
  3128. }
  3129. /**
  3130. * wpas_dbus_getter_bss_mode - Return the mode of a BSS
  3131. * @iter: Pointer to incoming dbus message iter
  3132. * @error: Location to store error on failure
  3133. * @user_data: Function specific data
  3134. * Returns: TRUE on success, FALSE on failure
  3135. *
  3136. * Getter for "Mode" property.
  3137. */
  3138. dbus_bool_t wpas_dbus_getter_bss_mode(
  3139. const struct wpa_dbus_property_desc *property_desc,
  3140. DBusMessageIter *iter, DBusError *error, void *user_data)
  3141. {
  3142. struct bss_handler_args *args = user_data;
  3143. struct wpa_bss *res;
  3144. const char *mode;
  3145. res = get_bss_helper(args, error, __func__);
  3146. if (!res)
  3147. return FALSE;
  3148. if (bss_is_dmg(res)) {
  3149. switch (res->caps & IEEE80211_CAP_DMG_MASK) {
  3150. case IEEE80211_CAP_DMG_PBSS:
  3151. case IEEE80211_CAP_DMG_IBSS:
  3152. mode = "ad-hoc";
  3153. break;
  3154. case IEEE80211_CAP_DMG_AP:
  3155. mode = "infrastructure";
  3156. break;
  3157. }
  3158. } else {
  3159. if (res->caps & IEEE80211_CAP_IBSS)
  3160. mode = "ad-hoc";
  3161. else
  3162. mode = "infrastructure";
  3163. }
  3164. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  3165. &mode, error);
  3166. }
  3167. /**
  3168. * wpas_dbus_getter_bss_level - Return the signal strength of a BSS
  3169. * @iter: Pointer to incoming dbus message iter
  3170. * @error: Location to store error on failure
  3171. * @user_data: Function specific data
  3172. * Returns: TRUE on success, FALSE on failure
  3173. *
  3174. * Getter for "Level" property.
  3175. */
  3176. dbus_bool_t wpas_dbus_getter_bss_signal(
  3177. const struct wpa_dbus_property_desc *property_desc,
  3178. DBusMessageIter *iter, DBusError *error, void *user_data)
  3179. {
  3180. struct bss_handler_args *args = user_data;
  3181. struct wpa_bss *res;
  3182. s16 level;
  3183. res = get_bss_helper(args, error, __func__);
  3184. if (!res)
  3185. return FALSE;
  3186. level = (s16) res->level;
  3187. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_INT16,
  3188. &level, error);
  3189. }
  3190. /**
  3191. * wpas_dbus_getter_bss_frequency - Return the frequency of a BSS
  3192. * @iter: Pointer to incoming dbus message iter
  3193. * @error: Location to store error on failure
  3194. * @user_data: Function specific data
  3195. * Returns: TRUE on success, FALSE on failure
  3196. *
  3197. * Getter for "Frequency" property.
  3198. */
  3199. dbus_bool_t wpas_dbus_getter_bss_frequency(
  3200. const struct wpa_dbus_property_desc *property_desc,
  3201. DBusMessageIter *iter, DBusError *error, void *user_data)
  3202. {
  3203. struct bss_handler_args *args = user_data;
  3204. struct wpa_bss *res;
  3205. u16 freq;
  3206. res = get_bss_helper(args, error, __func__);
  3207. if (!res)
  3208. return FALSE;
  3209. freq = (u16) res->freq;
  3210. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_UINT16,
  3211. &freq, error);
  3212. }
  3213. static int cmp_u8s_desc(const void *a, const void *b)
  3214. {
  3215. return (*(u8 *) b - *(u8 *) a);
  3216. }
  3217. /**
  3218. * wpas_dbus_getter_bss_rates - Return available bit rates of a BSS
  3219. * @iter: Pointer to incoming dbus message iter
  3220. * @error: Location to store error on failure
  3221. * @user_data: Function specific data
  3222. * Returns: TRUE on success, FALSE on failure
  3223. *
  3224. * Getter for "Rates" property.
  3225. */
  3226. dbus_bool_t wpas_dbus_getter_bss_rates(
  3227. const struct wpa_dbus_property_desc *property_desc,
  3228. DBusMessageIter *iter, DBusError *error, void *user_data)
  3229. {
  3230. struct bss_handler_args *args = user_data;
  3231. struct wpa_bss *res;
  3232. u8 *ie_rates = NULL;
  3233. u32 *real_rates;
  3234. int rates_num, i;
  3235. dbus_bool_t success = FALSE;
  3236. res = get_bss_helper(args, error, __func__);
  3237. if (!res)
  3238. return FALSE;
  3239. rates_num = wpa_bss_get_bit_rates(res, &ie_rates);
  3240. if (rates_num < 0)
  3241. return FALSE;
  3242. qsort(ie_rates, rates_num, 1, cmp_u8s_desc);
  3243. real_rates = os_malloc(sizeof(u32) * rates_num);
  3244. if (!real_rates) {
  3245. os_free(ie_rates);
  3246. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  3247. return FALSE;
  3248. }
  3249. for (i = 0; i < rates_num; i++)
  3250. real_rates[i] = ie_rates[i] * 500000;
  3251. success = wpas_dbus_simple_array_property_getter(iter, DBUS_TYPE_UINT32,
  3252. real_rates, rates_num,
  3253. error);
  3254. os_free(ie_rates);
  3255. os_free(real_rates);
  3256. return success;
  3257. }
  3258. static dbus_bool_t wpas_dbus_get_bss_security_prop(
  3259. const struct wpa_dbus_property_desc *property_desc,
  3260. DBusMessageIter *iter, struct wpa_ie_data *ie_data, DBusError *error)
  3261. {
  3262. DBusMessageIter iter_dict, variant_iter;
  3263. const char *group;
  3264. const char *pairwise[5]; /* max 5 pairwise ciphers is supported */
  3265. const char *key_mgmt[9]; /* max 9 key managements may be supported */
  3266. int n;
  3267. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
  3268. "a{sv}", &variant_iter))
  3269. goto nomem;
  3270. if (!wpa_dbus_dict_open_write(&variant_iter, &iter_dict))
  3271. goto nomem;
  3272. /* KeyMgmt */
  3273. n = 0;
  3274. if (ie_data->key_mgmt & WPA_KEY_MGMT_PSK)
  3275. key_mgmt[n++] = "wpa-psk";
  3276. if (ie_data->key_mgmt & WPA_KEY_MGMT_FT_PSK)
  3277. key_mgmt[n++] = "wpa-ft-psk";
  3278. if (ie_data->key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
  3279. key_mgmt[n++] = "wpa-psk-sha256";
  3280. if (ie_data->key_mgmt & WPA_KEY_MGMT_IEEE8021X)
  3281. key_mgmt[n++] = "wpa-eap";
  3282. if (ie_data->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
  3283. key_mgmt[n++] = "wpa-ft-eap";
  3284. if (ie_data->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
  3285. key_mgmt[n++] = "wpa-eap-sha256";
  3286. #ifdef CONFIG_SUITEB
  3287. if (ie_data->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B)
  3288. key_mgmt[n++] = "wpa-eap-suite-b";
  3289. #endif /* CONFIG_SUITEB */
  3290. #ifdef CONFIG_SUITEB192
  3291. if (ie_data->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
  3292. key_mgmt[n++] = "wpa-eap-suite-b-192";
  3293. #endif /* CONFIG_SUITEB192 */
  3294. if (ie_data->key_mgmt & WPA_KEY_MGMT_NONE)
  3295. key_mgmt[n++] = "wpa-none";
  3296. if (!wpa_dbus_dict_append_string_array(&iter_dict, "KeyMgmt",
  3297. key_mgmt, n))
  3298. goto nomem;
  3299. /* Group */
  3300. switch (ie_data->group_cipher) {
  3301. case WPA_CIPHER_WEP40:
  3302. group = "wep40";
  3303. break;
  3304. case WPA_CIPHER_TKIP:
  3305. group = "tkip";
  3306. break;
  3307. case WPA_CIPHER_CCMP:
  3308. group = "ccmp";
  3309. break;
  3310. case WPA_CIPHER_GCMP:
  3311. group = "gcmp";
  3312. break;
  3313. case WPA_CIPHER_WEP104:
  3314. group = "wep104";
  3315. break;
  3316. case WPA_CIPHER_CCMP_256:
  3317. group = "ccmp-256";
  3318. break;
  3319. case WPA_CIPHER_GCMP_256:
  3320. group = "gcmp-256";
  3321. break;
  3322. default:
  3323. group = "";
  3324. break;
  3325. }
  3326. if (!wpa_dbus_dict_append_string(&iter_dict, "Group", group))
  3327. goto nomem;
  3328. /* Pairwise */
  3329. n = 0;
  3330. if (ie_data->pairwise_cipher & WPA_CIPHER_TKIP)
  3331. pairwise[n++] = "tkip";
  3332. if (ie_data->pairwise_cipher & WPA_CIPHER_CCMP)
  3333. pairwise[n++] = "ccmp";
  3334. if (ie_data->pairwise_cipher & WPA_CIPHER_GCMP)
  3335. pairwise[n++] = "gcmp";
  3336. if (ie_data->pairwise_cipher & WPA_CIPHER_CCMP_256)
  3337. pairwise[n++] = "ccmp-256";
  3338. if (ie_data->pairwise_cipher & WPA_CIPHER_GCMP_256)
  3339. pairwise[n++] = "gcmp-256";
  3340. if (!wpa_dbus_dict_append_string_array(&iter_dict, "Pairwise",
  3341. pairwise, n))
  3342. goto nomem;
  3343. /* Management group (RSN only) */
  3344. if (ie_data->proto == WPA_PROTO_RSN) {
  3345. switch (ie_data->mgmt_group_cipher) {
  3346. #ifdef CONFIG_IEEE80211W
  3347. case WPA_CIPHER_AES_128_CMAC:
  3348. group = "aes128cmac";
  3349. break;
  3350. #endif /* CONFIG_IEEE80211W */
  3351. default:
  3352. group = "";
  3353. break;
  3354. }
  3355. if (!wpa_dbus_dict_append_string(&iter_dict, "MgmtGroup",
  3356. group))
  3357. goto nomem;
  3358. }
  3359. if (!wpa_dbus_dict_close_write(&variant_iter, &iter_dict) ||
  3360. !dbus_message_iter_close_container(iter, &variant_iter))
  3361. goto nomem;
  3362. return TRUE;
  3363. nomem:
  3364. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  3365. return FALSE;
  3366. }
  3367. /**
  3368. * wpas_dbus_getter_bss_wpa - Return the WPA options of a BSS
  3369. * @iter: Pointer to incoming dbus message iter
  3370. * @error: Location to store error on failure
  3371. * @user_data: Function specific data
  3372. * Returns: TRUE on success, FALSE on failure
  3373. *
  3374. * Getter for "WPA" property.
  3375. */
  3376. dbus_bool_t wpas_dbus_getter_bss_wpa(
  3377. const struct wpa_dbus_property_desc *property_desc,
  3378. DBusMessageIter *iter, DBusError *error, void *user_data)
  3379. {
  3380. struct bss_handler_args *args = user_data;
  3381. struct wpa_bss *res;
  3382. struct wpa_ie_data wpa_data;
  3383. const u8 *ie;
  3384. res = get_bss_helper(args, error, __func__);
  3385. if (!res)
  3386. return FALSE;
  3387. os_memset(&wpa_data, 0, sizeof(wpa_data));
  3388. ie = wpa_bss_get_vendor_ie(res, WPA_IE_VENDOR_TYPE);
  3389. if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &wpa_data) < 0) {
  3390. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  3391. "failed to parse WPA IE");
  3392. return FALSE;
  3393. }
  3394. return wpas_dbus_get_bss_security_prop(property_desc, iter, &wpa_data, error);
  3395. }
  3396. /**
  3397. * wpas_dbus_getter_bss_rsn - Return the RSN options of a BSS
  3398. * @iter: Pointer to incoming dbus message iter
  3399. * @error: Location to store error on failure
  3400. * @user_data: Function specific data
  3401. * Returns: TRUE on success, FALSE on failure
  3402. *
  3403. * Getter for "RSN" property.
  3404. */
  3405. dbus_bool_t wpas_dbus_getter_bss_rsn(
  3406. const struct wpa_dbus_property_desc *property_desc,
  3407. DBusMessageIter *iter, DBusError *error, void *user_data)
  3408. {
  3409. struct bss_handler_args *args = user_data;
  3410. struct wpa_bss *res;
  3411. struct wpa_ie_data wpa_data;
  3412. const u8 *ie;
  3413. res = get_bss_helper(args, error, __func__);
  3414. if (!res)
  3415. return FALSE;
  3416. os_memset(&wpa_data, 0, sizeof(wpa_data));
  3417. ie = wpa_bss_get_ie(res, WLAN_EID_RSN);
  3418. if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &wpa_data) < 0) {
  3419. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  3420. "failed to parse RSN IE");
  3421. return FALSE;
  3422. }
  3423. return wpas_dbus_get_bss_security_prop(property_desc, iter, &wpa_data, error);
  3424. }
  3425. /**
  3426. * wpas_dbus_getter_bss_wps - Return the WPS options of a BSS
  3427. * @iter: Pointer to incoming dbus message iter
  3428. * @error: Location to store error on failure
  3429. * @user_data: Function specific data
  3430. * Returns: TRUE on success, FALSE on failure
  3431. *
  3432. * Getter for "WPS" property.
  3433. */
  3434. dbus_bool_t wpas_dbus_getter_bss_wps(
  3435. const struct wpa_dbus_property_desc *property_desc,
  3436. DBusMessageIter *iter, DBusError *error, void *user_data)
  3437. {
  3438. struct bss_handler_args *args = user_data;
  3439. struct wpa_bss *res;
  3440. #ifdef CONFIG_WPS
  3441. struct wpabuf *wps_ie;
  3442. #endif /* CONFIG_WPS */
  3443. DBusMessageIter iter_dict, variant_iter;
  3444. int wps_support = 0;
  3445. const char *type = "";
  3446. res = get_bss_helper(args, error, __func__);
  3447. if (!res)
  3448. return FALSE;
  3449. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
  3450. "a{sv}", &variant_iter) ||
  3451. !wpa_dbus_dict_open_write(&variant_iter, &iter_dict))
  3452. goto nomem;
  3453. #ifdef CONFIG_WPS
  3454. wps_ie = wpa_bss_get_vendor_ie_multi(res, WPS_IE_VENDOR_TYPE);
  3455. if (wps_ie) {
  3456. wps_support = 1;
  3457. if (wps_is_selected_pbc_registrar(wps_ie))
  3458. type = "pbc";
  3459. else if (wps_is_selected_pin_registrar(wps_ie))
  3460. type = "pin";
  3461. wpabuf_free(wps_ie);
  3462. }
  3463. #endif /* CONFIG_WPS */
  3464. if ((wps_support && !wpa_dbus_dict_append_string(&iter_dict, "Type", type)) ||
  3465. !wpa_dbus_dict_close_write(&variant_iter, &iter_dict) ||
  3466. !dbus_message_iter_close_container(iter, &variant_iter))
  3467. goto nomem;
  3468. return TRUE;
  3469. nomem:
  3470. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  3471. return FALSE;
  3472. }
  3473. /**
  3474. * wpas_dbus_getter_bss_ies - Return all IEs of a BSS
  3475. * @iter: Pointer to incoming dbus message iter
  3476. * @error: Location to store error on failure
  3477. * @user_data: Function specific data
  3478. * Returns: TRUE on success, FALSE on failure
  3479. *
  3480. * Getter for "IEs" property.
  3481. */
  3482. dbus_bool_t wpas_dbus_getter_bss_ies(
  3483. const struct wpa_dbus_property_desc *property_desc,
  3484. DBusMessageIter *iter, DBusError *error, void *user_data)
  3485. {
  3486. struct bss_handler_args *args = user_data;
  3487. struct wpa_bss *res;
  3488. res = get_bss_helper(args, error, __func__);
  3489. if (!res)
  3490. return FALSE;
  3491. return wpas_dbus_simple_array_property_getter(iter, DBUS_TYPE_BYTE,
  3492. res + 1, res->ie_len,
  3493. error);
  3494. }
  3495. /**
  3496. * wpas_dbus_getter_bss_age - Return time in seconds since BSS was last seen
  3497. * @iter: Pointer to incoming dbus message iter
  3498. * @error: Location to store error on failure
  3499. * @user_data: Function specific data
  3500. * Returns: TRUE on success, FALSE on failure
  3501. *
  3502. * Getter for BSS age
  3503. */
  3504. dbus_bool_t wpas_dbus_getter_bss_age(
  3505. const struct wpa_dbus_property_desc *property_desc,
  3506. DBusMessageIter *iter, DBusError *error, void *user_data)
  3507. {
  3508. struct bss_handler_args *args = user_data;
  3509. struct wpa_bss *res;
  3510. struct os_reltime now, diff = { 0, 0 };
  3511. u32 age;
  3512. res = get_bss_helper(args, error, __func__);
  3513. if (!res)
  3514. return FALSE;
  3515. os_get_reltime(&now);
  3516. os_reltime_sub(&now, &res->last_update, &diff);
  3517. age = diff.sec > 0 ? diff.sec : 0;
  3518. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_UINT32, &age,
  3519. error);
  3520. }
  3521. /**
  3522. * wpas_dbus_getter_enabled - Check whether network is enabled or disabled
  3523. * @iter: Pointer to incoming dbus message iter
  3524. * @error: Location to store error on failure
  3525. * @user_data: Function specific data
  3526. * Returns: TRUE on success, FALSE on failure
  3527. *
  3528. * Getter for "enabled" property of a configured network.
  3529. */
  3530. dbus_bool_t wpas_dbus_getter_enabled(
  3531. const struct wpa_dbus_property_desc *property_desc,
  3532. DBusMessageIter *iter, DBusError *error, void *user_data)
  3533. {
  3534. struct network_handler_args *net = user_data;
  3535. dbus_bool_t enabled = net->ssid->disabled ? FALSE : TRUE;
  3536. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_BOOLEAN,
  3537. &enabled, error);
  3538. }
  3539. /**
  3540. * wpas_dbus_setter_enabled - Mark a configured network as enabled or disabled
  3541. * @iter: Pointer to incoming dbus message iter
  3542. * @error: Location to store error on failure
  3543. * @user_data: Function specific data
  3544. * Returns: TRUE on success, FALSE on failure
  3545. *
  3546. * Setter for "Enabled" property of a configured network.
  3547. */
  3548. dbus_bool_t wpas_dbus_setter_enabled(
  3549. const struct wpa_dbus_property_desc *property_desc,
  3550. DBusMessageIter *iter, DBusError *error, void *user_data)
  3551. {
  3552. struct network_handler_args *net = user_data;
  3553. struct wpa_supplicant *wpa_s;
  3554. struct wpa_ssid *ssid;
  3555. dbus_bool_t enable;
  3556. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_BOOLEAN,
  3557. &enable))
  3558. return FALSE;
  3559. wpa_s = net->wpa_s;
  3560. ssid = net->ssid;
  3561. if (enable)
  3562. wpa_supplicant_enable_network(wpa_s, ssid);
  3563. else
  3564. wpa_supplicant_disable_network(wpa_s, ssid);
  3565. return TRUE;
  3566. }
  3567. /**
  3568. * wpas_dbus_getter_network_properties - Get options for a configured network
  3569. * @iter: Pointer to incoming dbus message iter
  3570. * @error: Location to store error on failure
  3571. * @user_data: Function specific data
  3572. * Returns: TRUE on success, FALSE on failure
  3573. *
  3574. * Getter for "Properties" property of a configured network.
  3575. */
  3576. dbus_bool_t wpas_dbus_getter_network_properties(
  3577. const struct wpa_dbus_property_desc *property_desc,
  3578. DBusMessageIter *iter, DBusError *error, void *user_data)
  3579. {
  3580. struct network_handler_args *net = user_data;
  3581. DBusMessageIter variant_iter, dict_iter;
  3582. char **iterator;
  3583. char **props = wpa_config_get_all(net->ssid, 1);
  3584. dbus_bool_t success = FALSE;
  3585. if (!props) {
  3586. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  3587. return FALSE;
  3588. }
  3589. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, "a{sv}",
  3590. &variant_iter) ||
  3591. !wpa_dbus_dict_open_write(&variant_iter, &dict_iter)) {
  3592. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  3593. goto out;
  3594. }
  3595. iterator = props;
  3596. while (*iterator) {
  3597. if (!wpa_dbus_dict_append_string(&dict_iter, *iterator,
  3598. *(iterator + 1))) {
  3599. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY,
  3600. "no memory");
  3601. goto out;
  3602. }
  3603. iterator += 2;
  3604. }
  3605. if (!wpa_dbus_dict_close_write(&variant_iter, &dict_iter) ||
  3606. !dbus_message_iter_close_container(iter, &variant_iter)) {
  3607. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  3608. goto out;
  3609. }
  3610. success = TRUE;
  3611. out:
  3612. iterator = props;
  3613. while (*iterator) {
  3614. os_free(*iterator);
  3615. iterator++;
  3616. }
  3617. os_free(props);
  3618. return success;
  3619. }
  3620. /**
  3621. * wpas_dbus_setter_network_properties - Set options for a configured network
  3622. * @iter: Pointer to incoming dbus message iter
  3623. * @error: Location to store error on failure
  3624. * @user_data: Function specific data
  3625. * Returns: TRUE on success, FALSE on failure
  3626. *
  3627. * Setter for "Properties" property of a configured network.
  3628. */
  3629. dbus_bool_t wpas_dbus_setter_network_properties(
  3630. const struct wpa_dbus_property_desc *property_desc,
  3631. DBusMessageIter *iter, DBusError *error, void *user_data)
  3632. {
  3633. struct network_handler_args *net = user_data;
  3634. struct wpa_ssid *ssid = net->ssid;
  3635. DBusMessageIter variant_iter;
  3636. dbus_message_iter_recurse(iter, &variant_iter);
  3637. return set_network_properties(net->wpa_s, ssid, &variant_iter, error);
  3638. }
  3639. #ifdef CONFIG_AP
  3640. DBusMessage * wpas_dbus_handler_subscribe_preq(
  3641. DBusMessage *message, struct wpa_supplicant *wpa_s)
  3642. {
  3643. struct wpas_dbus_priv *priv = wpa_s->global->dbus;
  3644. char *name;
  3645. if (wpa_s->preq_notify_peer != NULL) {
  3646. if (os_strcmp(dbus_message_get_sender(message),
  3647. wpa_s->preq_notify_peer) == 0)
  3648. return NULL;
  3649. return dbus_message_new_error(message,
  3650. WPAS_DBUS_ERROR_SUBSCRIPTION_IN_USE,
  3651. "Another application is already subscribed");
  3652. }
  3653. name = os_strdup(dbus_message_get_sender(message));
  3654. if (!name)
  3655. return wpas_dbus_error_no_memory(message);
  3656. wpa_s->preq_notify_peer = name;
  3657. /* Subscribe to clean up if application closes socket */
  3658. wpas_dbus_subscribe_noc(priv);
  3659. /*
  3660. * Double-check it's still alive to make sure that we didn't
  3661. * miss the NameOwnerChanged signal, e.g. while strdup'ing.
  3662. */
  3663. if (!dbus_bus_name_has_owner(priv->con, name, NULL)) {
  3664. /*
  3665. * Application no longer exists, clean up.
  3666. * The return value is irrelevant now.
  3667. *
  3668. * Need to check if the NameOwnerChanged handling
  3669. * already cleaned up because we have processed
  3670. * DBus messages while checking if the name still
  3671. * has an owner.
  3672. */
  3673. if (!wpa_s->preq_notify_peer)
  3674. return NULL;
  3675. os_free(wpa_s->preq_notify_peer);
  3676. wpa_s->preq_notify_peer = NULL;
  3677. wpas_dbus_unsubscribe_noc(priv);
  3678. }
  3679. return NULL;
  3680. }
  3681. DBusMessage * wpas_dbus_handler_unsubscribe_preq(
  3682. DBusMessage *message, struct wpa_supplicant *wpa_s)
  3683. {
  3684. struct wpas_dbus_priv *priv = wpa_s->global->dbus;
  3685. if (!wpa_s->preq_notify_peer)
  3686. return dbus_message_new_error(message,
  3687. WPAS_DBUS_ERROR_NO_SUBSCRIPTION,
  3688. "Not subscribed");
  3689. if (os_strcmp(wpa_s->preq_notify_peer,
  3690. dbus_message_get_sender(message)))
  3691. return dbus_message_new_error(message,
  3692. WPAS_DBUS_ERROR_SUBSCRIPTION_EPERM,
  3693. "Can't unsubscribe others");
  3694. os_free(wpa_s->preq_notify_peer);
  3695. wpa_s->preq_notify_peer = NULL;
  3696. wpas_dbus_unsubscribe_noc(priv);
  3697. return NULL;
  3698. }
  3699. void wpas_dbus_signal_preq(struct wpa_supplicant *wpa_s,
  3700. const u8 *addr, const u8 *dst, const u8 *bssid,
  3701. const u8 *ie, size_t ie_len, u32 ssi_signal)
  3702. {
  3703. DBusMessage *msg;
  3704. DBusMessageIter iter, dict_iter;
  3705. struct wpas_dbus_priv *priv = wpa_s->global->dbus;
  3706. /* Do nothing if the control interface is not turned on */
  3707. if (priv == NULL || !wpa_s->dbus_new_path)
  3708. return;
  3709. if (wpa_s->preq_notify_peer == NULL)
  3710. return;
  3711. msg = dbus_message_new_signal(wpa_s->dbus_new_path,
  3712. WPAS_DBUS_NEW_IFACE_INTERFACE,
  3713. "ProbeRequest");
  3714. if (msg == NULL)
  3715. return;
  3716. dbus_message_set_destination(msg, wpa_s->preq_notify_peer);
  3717. dbus_message_iter_init_append(msg, &iter);
  3718. if (!wpa_dbus_dict_open_write(&iter, &dict_iter) ||
  3719. (addr && !wpa_dbus_dict_append_byte_array(&dict_iter, "addr",
  3720. (const char *) addr,
  3721. ETH_ALEN)) ||
  3722. (dst && !wpa_dbus_dict_append_byte_array(&dict_iter, "dst",
  3723. (const char *) dst,
  3724. ETH_ALEN)) ||
  3725. (bssid && !wpa_dbus_dict_append_byte_array(&dict_iter, "bssid",
  3726. (const char *) bssid,
  3727. ETH_ALEN)) ||
  3728. (ie && ie_len && !wpa_dbus_dict_append_byte_array(&dict_iter, "ies",
  3729. (const char *) ie,
  3730. ie_len)) ||
  3731. (ssi_signal && !wpa_dbus_dict_append_int32(&dict_iter, "signal",
  3732. ssi_signal)) ||
  3733. !wpa_dbus_dict_close_write(&iter, &dict_iter))
  3734. goto fail;
  3735. dbus_connection_send(priv->con, msg, NULL);
  3736. goto out;
  3737. fail:
  3738. wpa_printf(MSG_ERROR, "dbus: Failed to construct signal");
  3739. out:
  3740. dbus_message_unref(msg);
  3741. }
  3742. #endif /* CONFIG_AP */