dbus_new_handlers.c 111 KB

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