eiwd

no dbus iwd with iwctl functionality
git clone https://git.daat.foo/repos/eiwd.git
Log | Files | Refs | Submodules | README | LICENSE

ofono.c (18834B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2017-2019  Intel Corporation. All rights reserved.
      6  *
      7  *  This library is free software; you can redistribute it and/or
      8  *  modify it under the terms of the GNU Lesser General Public
      9  *  License as published by the Free Software Foundation; either
     10  *  version 2.1 of the License, or (at your option) any later version.
     11  *
     12  *  This library is distributed in the hope that it will be useful,
     13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  *  Lesser General Public License for more details.
     16  *
     17  *  You should have received a copy of the GNU Lesser General Public
     18  *  License along with this library; if not, write to the Free Software
     19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     20  *
     21  */
     22 
     23 #ifdef HAVE_CONFIG_H
     24 #include <config.h>
     25 #endif
     26 
     27 #include <stdio.h>
     28 #include <ctype.h>
     29 #include <stdio.h>
     30 #include <errno.h>
     31 
     32 #include <ell/ell.h>
     33 #include <ell/dbus.h>
     34 
     35 #include "src/dbus.h"
     36 #include "src/simauth.h"
     37 #include "src/module.h"
     38 #include "src/iwd.h"
     39 
     40 /*
     41  * This plugin takes care of all the communication with ofono in order to
     42  * provide the needed algorithms for EAP-SIM/AKA/AKA'. Once this plugin is
     43  * started it will start the initial "discovery" stage i.e.
     44  *
     45  * 1. Find ofono DBus service
     46  * 2. Find all modems (and listen for Added/Removed signal)
     47  * 3. For each modem see if SimAuthentication interface exists and get apps
     48  *    and NAI
     49  * 4. Create simauth provider for modem if the above succeeds
     50  *
     51  * These steps are chained, as to avoid the complexity of concurrent method
     52  * calls. Once the chain of methods has completed, a new simauth provider is
     53  * created for the modem. Only then will the EAP methods be able to run the
     54  * authentication algorithms ofono provides.
     55  *
     56  * If at any time the above conditions change e.g. SimAuthentication disappears,
     57  * ofono disappears, the modem simauth provider will unregister itself from
     58  * simauth, not allowing any future authentication algorithms to be run until
     59  * the start conditions are met again.
     60  */
     61 
     62 #define OFONO_SIM_AUTHENTICATION_IFACE	"org.ofono.SimAuthentication"
     63 #define OFONO_SIM_MANAGER_IFACE		"org.ofono.SimManager"
     64 #define OFONO_MODEM_IFACE		"org.ofono.Modem"
     65 #define OFONO_USIM_APPLICATION_IFACE	"org.ofono.USimApplication"
     66 #define OFONO_ISIM_APPLICATION_IFACE	"org.ofono.ISimApplication"
     67 
     68 struct sa_data {
     69 	char *umts_app_path;
     70 	char *ims_app_path;
     71 
     72 	struct user_cb *pending;
     73 	uint32_t serial;
     74 };
     75 
     76 struct ofono_modem {
     77 	char *path;
     78 	int props_watch;
     79 
     80 	uint32_t props_serial;
     81 	uint32_t apps_serial;
     82 
     83 	bool sim_auth_found : 1;
     84 
     85 	struct iwd_sim_auth *auth;
     86 };
     87 
     88 struct user_cb {
     89 	void *cb;
     90 	void *data;
     91 	bool is_gsm : 1;
     92 };
     93 
     94 static uint32_t ofono_watch;
     95 static uint32_t modem_add_watch;
     96 static uint32_t modem_removed_watch;
     97 static struct l_queue *modems;
     98 
     99 static struct user_cb *new_cb(void *func, void *data, bool is_gsm)
    100 {
    101 	struct user_cb *cbd = l_new(struct user_cb, 1);
    102 
    103 	cbd->cb = func;
    104 	cbd->data = data;
    105 	cbd->is_gsm = is_gsm;
    106 
    107 	return cbd;
    108 }
    109 
    110 static void free_cb(void *ptr)
    111 {
    112 	struct sa_data *sa_data = ptr;
    113 
    114 	l_free(sa_data->pending);
    115 	sa_data->pending = NULL;
    116 	sa_data->serial = 0;
    117 }
    118 
    119 /*
    120  * Copy a byte array ("ay") from array into buf
    121  */
    122 static bool get_byte_array(struct l_dbus_message_iter *array, uint8_t *buf,
    123 		int len)
    124 {
    125 	int i;
    126 
    127 	for (i = 0; i < len; i++) {
    128 		if (!l_dbus_message_iter_next_entry(array, buf + i))
    129 			return false;
    130 	}
    131 
    132 	return true;
    133 }
    134 
    135 /*
    136  * Append a byte array ("ay") to a DBus message builder
    137  */
    138 static bool append_byte_array(struct l_dbus_message_builder *builder,
    139 		const uint8_t *data, int len)
    140 {
    141 	int i;
    142 
    143 	if (!l_dbus_message_builder_enter_array(builder, "y"))
    144 		return false;
    145 
    146 	for (i = 0; i < len; i++)
    147 		if (!l_dbus_message_builder_append_basic(builder, 'y',
    148 				data + i))
    149 			return false;
    150 
    151 	if (!l_dbus_message_builder_leave_array(builder))
    152 		return false;
    153 
    154 	return true;
    155 }
    156 
    157 static void ims_auth_cb(struct l_dbus_message *reply, void *user_data)
    158 {
    159 	struct sa_data *sa_data = user_data;
    160 	struct user_cb *cbd = sa_data->pending;
    161 	sim_auth_check_milenage_cb_t cb = cbd->cb;
    162 	struct l_dbus_message_iter properties;
    163 	struct l_dbus_message_iter value;
    164 	const char *prop;
    165 	uint8_t res[8];
    166 	uint8_t ck[16];
    167 	uint8_t ik[16];
    168 	uint8_t auts[16];
    169 
    170 	if (l_dbus_message_is_error(reply)) {
    171 		l_debug("ImsAuthenticate error");
    172 		goto end;
    173 	}
    174 
    175 	if (!l_dbus_message_get_arguments(reply, "a{say}", &properties))
    176 		goto end;
    177 
    178 	while (l_dbus_message_iter_next_entry(&properties, &prop, &value)) {
    179 		if (!strcmp(prop, "RES")) {
    180 			if (!get_byte_array(&value, res, 8))
    181 				goto end;
    182 		} else if (!strcmp(prop, "CK")) {
    183 			if (!get_byte_array(&value, ck, 16))
    184 				goto end;
    185 		} else if (!strcmp(prop, "IK")) {
    186 			if (!get_byte_array(&value, ik, 16))
    187 				goto end;
    188 		} else if (!strcmp(prop, "AUTS")) {
    189 			if (!get_byte_array(&value, auts, 14))
    190 				goto end;
    191 
    192 			cb(NULL, NULL, NULL, auts, cbd->data);
    193 
    194 			return;
    195 		}
    196 	}
    197 
    198 	cb(res, ck, ik, NULL, cbd->data);
    199 
    200 	return;
    201 
    202 end:
    203 	cb(NULL, NULL, NULL, NULL, cbd->data);
    204 }
    205 
    206 static void gsm_auth_cb(struct l_dbus_message *reply, void *user_data)
    207 {
    208 	struct sa_data *sa_data = user_data;
    209 	struct user_cb *cbd = sa_data->pending;
    210 	sim_auth_run_gsm_cb_t cb = cbd->cb;
    211 	struct l_dbus_message_iter array;
    212 	struct l_dbus_message_iter val;
    213 	struct l_dbus_message_iter dict;
    214 	const char *prop;
    215 	int sres_pos = 0;
    216 	int kc_pos = 0;
    217 	uint8_t kc[NUM_RANDS_MAX][EAP_SIM_KC_LEN];
    218 	uint8_t sres[NUM_RANDS_MAX][EAP_SIM_SRES_LEN];
    219 
    220 	if (l_dbus_message_is_error(reply)) {
    221 		l_debug("GsmAuthenticate error");
    222 		goto end;
    223 	}
    224 
    225 	if (!l_dbus_message_get_arguments(reply, "aa{say}", &array))
    226 		goto end;
    227 
    228 	while (l_dbus_message_iter_next_entry(&array, &dict)) {
    229 		while (l_dbus_message_iter_next_entry(&dict, &prop, &val)) {
    230 			if (sres_pos > NUM_RANDS_MAX || kc_pos > NUM_RANDS_MAX)
    231 				goto end;
    232 
    233 			if (!strcmp(prop, "SRES")) {
    234 				if (!get_byte_array(&val, sres[sres_pos++],
    235 						EAP_SIM_SRES_LEN))
    236 					goto end;
    237 			} else if (!strcmp(prop, "Kc")) {
    238 				if (!get_byte_array(&val, kc[kc_pos++],
    239 						EAP_SIM_KC_LEN))
    240 					goto end;
    241 			}
    242 		}
    243 	}
    244 
    245 	cb((const uint8_t *)sres, (const uint8_t *)kc, cbd->data);
    246 
    247 	return;
    248 
    249 end:
    250 	cb(NULL, NULL, cbd->data);
    251 }
    252 
    253 static int ofono_sim_auth_run_gsm(struct iwd_sim_auth *auth,
    254 		const uint8_t *rands, int num_rands, sim_auth_run_gsm_cb_t cb,
    255 		void *data)
    256 {
    257 	struct sa_data *sa_data = iwd_sim_auth_get_data(auth);
    258 	struct l_dbus *dbus = dbus_get_bus();
    259 	struct l_dbus_message *message;
    260 	struct l_dbus_message_builder *builder;
    261 	int i;
    262 
    263 	if (num_rands > NUM_RANDS_MAX) {
    264 		l_debug("Max number of RAND's is %d", NUM_RANDS_MAX);
    265 		return -EINVAL;
    266 	}
    267 
    268 	if (sa_data->pending) {
    269 		l_debug("Modem already has outstanding auth request");
    270 		return -EBUSY;
    271 	}
    272 
    273 	sa_data->pending = new_cb(cb, data, true);
    274 
    275 	message = l_dbus_message_new_method_call(dbus, "org.ofono",
    276 			sa_data->umts_app_path, OFONO_USIM_APPLICATION_IFACE,
    277 			"GsmAuthenticate");
    278 
    279 	builder = l_dbus_message_builder_new(message);
    280 
    281 	if (!l_dbus_message_builder_enter_array(builder, "ay"))
    282 		goto error;
    283 
    284 	for (i = 0; i < num_rands; i++) {
    285 		if (!append_byte_array(builder, rands + (i * 16), 16))
    286 			goto error;
    287 	}
    288 
    289 	if (!l_dbus_message_builder_leave_array(builder))
    290 		goto error;
    291 
    292 	if (!l_dbus_message_builder_finalize(builder))
    293 		goto error;
    294 
    295 	sa_data->serial = l_dbus_send_with_reply(dbus, message, gsm_auth_cb,
    296 			sa_data, free_cb);
    297 	if (!sa_data->serial)
    298 		goto error;
    299 
    300 	l_dbus_message_builder_destroy(builder);
    301 
    302 	return 0;
    303 
    304 error:
    305 	l_dbus_message_builder_destroy(builder);
    306 	l_free(sa_data->pending);
    307 	sa_data->pending = NULL;
    308 
    309 	return -EIO;
    310 }
    311 
    312 static int ofono_sim_auth_check_milenage(struct iwd_sim_auth *auth,
    313 		const uint8_t *rand, const uint8_t *autn,
    314 		sim_auth_check_milenage_cb_t cb, void *data)
    315 {
    316 	struct sa_data *sa_data = iwd_sim_auth_get_data(auth);
    317 	const char *iface = OFONO_ISIM_APPLICATION_IFACE;
    318 	const char *method = "ImsAuthenticate";
    319 	const char *path;
    320 	struct l_dbus *dbus = dbus_get_bus();
    321 	struct l_dbus_message *message;
    322 	struct l_dbus_message_builder *builder;
    323 
    324 	if (sa_data->pending) {
    325 		l_debug("Modem already has outstanding auth request");
    326 		return -EBUSY;
    327 	}
    328 
    329 	sa_data->pending = new_cb(cb, data, false);
    330 
    331 	/*
    332 	 * If ISIM is not available, run on USIM application
    333 	 */
    334 	if (!sa_data->ims_app_path && sa_data->umts_app_path) {
    335 		iface = OFONO_USIM_APPLICATION_IFACE;
    336 		method = "UmtsAuthenticate";
    337 		path = sa_data->umts_app_path;
    338 	} else {
    339 		path = sa_data->ims_app_path;
    340 	}
    341 
    342 	message = l_dbus_message_new_method_call(dbus, "org.ofono", path,
    343 			iface, method);
    344 
    345 	builder = l_dbus_message_builder_new(message);
    346 
    347 	if (!append_byte_array(builder, rand, 16))
    348 		goto error;
    349 
    350 	if (!append_byte_array(builder, autn, 16))
    351 		goto error;
    352 
    353 	if (!l_dbus_message_builder_finalize(builder))
    354 		goto error;
    355 
    356 	sa_data->serial = l_dbus_send_with_reply(dbus, message, ims_auth_cb,
    357 			sa_data, free_cb);
    358 	if (!sa_data->serial)
    359 		goto error;
    360 
    361 	l_dbus_message_builder_destroy(builder);
    362 
    363 	return 0;
    364 
    365 error:
    366 	l_dbus_message_builder_destroy(builder);
    367 	l_free(sa_data->pending);
    368 	sa_data->pending = NULL;
    369 
    370 	return -EIO;
    371 }
    372 
    373 static void ofono_sim_auth_cancel_request(struct iwd_sim_auth *auth, int id)
    374 {
    375 	struct sa_data *sa_data = iwd_sim_auth_get_data(auth);
    376 
    377 	if (sa_data->pending) {
    378 		l_dbus_cancel(dbus_get_bus(), sa_data->serial);
    379 		sa_data->pending = NULL;
    380 	}
    381 }
    382 
    383 static void ofono_sim_auth_remove(struct iwd_sim_auth *auth)
    384 {
    385 	struct sa_data *sa_data = iwd_sim_auth_get_data(auth);
    386 
    387 	l_debug("removing auth data %p", sa_data);
    388 
    389 	if (sa_data->pending) {
    390 		struct user_cb *cbd = sa_data->pending;
    391 
    392 		if (cbd->is_gsm) {
    393 			sim_auth_run_gsm_cb_t cb = cbd->cb;
    394 
    395 			cb(NULL, NULL, cbd->data);
    396 		} else {
    397 			sim_auth_check_milenage_cb_t cb = cbd->cb;
    398 
    399 			cb(NULL, NULL, NULL, NULL, cbd->data);
    400 		}
    401 
    402 		l_dbus_cancel(dbus_get_bus(), sa_data->serial);
    403 	}
    404 
    405 	l_free(sa_data->ims_app_path);
    406 	l_free(sa_data->umts_app_path);
    407 	l_free(sa_data);
    408 }
    409 
    410 static struct iwd_sim_auth_driver ofono_driver = {
    411 		.name = "oFono SimAuth driver",
    412 		.check_milenage = ofono_sim_auth_check_milenage,
    413 		.run_gsm = ofono_sim_auth_run_gsm,
    414 		.cancel_request = ofono_sim_auth_cancel_request,
    415 		.remove = ofono_sim_auth_remove
    416 };
    417 
    418 static void modem_destroy(void *data)
    419 {
    420 	struct l_dbus *dbus = dbus_get_bus();
    421 	struct ofono_modem *modem = data;
    422 
    423 	if (modem->auth) {
    424 		/*
    425 		 * If an auth instance has been created, simauth will call
    426 		 * the driver's remove which cleanups the sa_data object
    427 		 */
    428 		iwd_sim_auth_remove(modem->auth);
    429 	}
    430 
    431 	l_debug("removing modem %s", modem->path);
    432 
    433 	if (modem->apps_serial)
    434 		l_dbus_cancel(dbus, modem->apps_serial);
    435 
    436 	if (modem->props_serial)
    437 		l_dbus_cancel(dbus, modem->props_serial);
    438 
    439 	l_free(modem->path);
    440 	l_dbus_remove_watch(dbus, modem->props_watch);
    441 	l_free(modem);
    442 }
    443 
    444 static void get_auth_apps_cb(struct l_dbus_message *reply,
    445 		void *user_data)
    446 {
    447 	struct ofono_modem *modem = user_data;
    448 	struct sa_data *sa_data = iwd_sim_auth_get_data(modem->auth);
    449 	struct l_dbus_message_iter array;
    450 	struct l_dbus_message_iter dict;
    451 	struct l_dbus_message_iter variant;
    452 	bool sim_supported = false;
    453 	bool aka_supported = false;
    454 
    455 	const char *path;
    456 
    457 	modem->apps_serial = 0;
    458 
    459 	if (l_dbus_message_is_error(reply)) {
    460 		l_debug("GetApplications error");
    461 		goto error;
    462 	}
    463 
    464 	if (!l_dbus_message_get_arguments(reply, "a{oa{sv}}", &array))
    465 		goto error;
    466 
    467 	while (l_dbus_message_iter_next_entry(&array, &path, &dict)) {
    468 		const char *type;
    469 		const char *label;
    470 		const char *key;
    471 
    472 		while (l_dbus_message_iter_next_entry(&dict, &key, &variant)) {
    473 			if (strcmp(key, "Type"))
    474 				continue;
    475 
    476 			if (!l_dbus_message_iter_get_variant(&variant, "s",
    477 					&type, &label))
    478 				goto error;
    479 
    480 			if (!strcmp(type, "Umts")) {
    481 				sa_data->umts_app_path = l_strdup(path);
    482 				sim_supported = true;
    483 				aka_supported = true;
    484 			} else if (!strcmp(type, "Ims")) {
    485 				sa_data->ims_app_path = l_strdup(path);
    486 				aka_supported = true;
    487 			}
    488 		}
    489 	}
    490 
    491 	if (sa_data->umts_app_path || sa_data->ims_app_path) {
    492 		iwd_sim_auth_set_capabilities(modem->auth, sim_supported,
    493 				aka_supported);
    494 
    495 		iwd_sim_auth_register(modem->auth);
    496 
    497 		l_debug("modem %s successfully loaded, sim=%u, aka=%u",
    498 				modem->path, sim_supported, aka_supported);
    499 		return;
    500 	}
    501 
    502 	/* non supported type */
    503 	l_debug("unsupported modem auth capabilities");
    504 
    505 error:
    506 	iwd_sim_auth_remove(modem->auth);
    507 }
    508 
    509 static void get_applications(struct ofono_modem *modem)
    510 {
    511 	struct l_dbus *dbus = dbus_get_bus();
    512 	struct l_dbus_message *message;
    513 
    514 	message = l_dbus_message_new_method_call(dbus,
    515 			"org.ofono", modem->path,
    516 			OFONO_SIM_AUTHENTICATION_IFACE,
    517 			"GetApplications");
    518 
    519 	l_dbus_message_set_arguments(message, "");
    520 
    521 	modem->apps_serial = l_dbus_send_with_reply(dbus, message,
    522 			get_auth_apps_cb, modem, NULL);
    523 }
    524 
    525 static void get_auth_props_cb(struct l_dbus_message *reply,
    526 		void *user_data)
    527 {
    528 	struct ofono_modem *modem = user_data;
    529 	struct l_dbus_message_iter array;
    530 	struct l_dbus_message_iter variant;
    531 	const char *key;
    532 
    533 	modem->props_serial = 0;
    534 
    535 	if (l_dbus_message_is_error(reply)) {
    536 		l_debug("GetProperties error");
    537 		goto error;
    538 	}
    539 
    540 	if (!l_dbus_message_get_arguments(reply, "a{sv}", &array))
    541 		goto error;
    542 
    543 	while (l_dbus_message_iter_next_entry(&array, &key, &variant)) {
    544 		if (!strcmp(key, "NetworkAccessIdentity")) {
    545 			struct sa_data *sa_data;
    546 			const char *id;
    547 
    548 			if (!l_dbus_message_iter_get_variant(&variant,
    549 					"s", &id))
    550 				goto error;
    551 
    552 			modem->auth = iwd_sim_auth_create(&ofono_driver);
    553 
    554 			sa_data = l_new(struct sa_data, 1);
    555 
    556 			iwd_sim_auth_set_data(modem->auth, sa_data);
    557 			iwd_sim_auth_set_nai(modem->auth, id);
    558 
    559 			get_applications(modem);
    560 
    561 			return;
    562 		}
    563 	}
    564 
    565 error:
    566 	if (modem->auth)
    567 		iwd_sim_auth_remove(modem->auth);
    568 }
    569 
    570 static void get_properties(struct ofono_modem *modem)
    571 {
    572 	struct l_dbus *dbus = dbus_get_bus();
    573 	struct l_dbus_message *message;
    574 
    575 	message = l_dbus_message_new_method_call(dbus,
    576 			"org.ofono", modem->path,
    577 			OFONO_SIM_AUTHENTICATION_IFACE,
    578 			"GetProperties");
    579 
    580 	l_dbus_message_set_arguments(message, "");
    581 
    582 	modem->props_serial = l_dbus_send_with_reply(dbus, message,
    583 			get_auth_props_cb, modem, NULL);
    584 }
    585 
    586 static void parse_interfaces(struct l_dbus_message_iter *prop,
    587 		struct ofono_modem *modem)
    588 {
    589 	struct l_dbus_message_iter ifaces;
    590 	const char *str;
    591 
    592 	if (!l_dbus_message_iter_get_variant(prop, "as", &ifaces)) {
    593 		l_warn("error parsing modem %s interfaces", modem->path);
    594 		return;
    595 	}
    596 
    597 	while (l_dbus_message_iter_next_entry(&ifaces, &str)) {
    598 		if (!strcmp(OFONO_SIM_AUTHENTICATION_IFACE, str)) {
    599 			if (modem->sim_auth_found)
    600 				return;
    601 
    602 			modem->sim_auth_found = true;
    603 
    604 			get_properties(modem);
    605 
    606 			return;
    607 		}
    608 	}
    609 
    610 	/* SimAuthentication disappeared */
    611 	if (modem->sim_auth_found) {
    612 		/* Remove auth provider, this will free the sa_data object */
    613 		if (modem->auth)
    614 			iwd_sim_auth_remove(modem->auth);
    615 
    616 		/* put modem back into a 'discovery' state */
    617 		modem->sim_auth_found = false;
    618 	}
    619 }
    620 
    621 static void interfaces_changed_cb(struct l_dbus_message *message,
    622 		void *user_data)
    623 {
    624 	struct ofono_modem *modem = user_data;
    625 	struct l_dbus_message_iter value;
    626 	const char *key;
    627 
    628 	if (!l_dbus_message_get_arguments(message, "sv", &key, &value))
    629 		return;
    630 
    631 	if (!strcmp(key, "Interfaces"))
    632 		parse_interfaces(&value, modem);
    633 }
    634 
    635 static bool match_modem_by_path(const void *a, const void *b)
    636 {
    637 	struct ofono_modem *modem = (struct ofono_modem *)a;
    638 
    639 	if (strcmp(modem->path, (const char *)b))
    640 		return false;
    641 
    642 	return true;
    643 }
    644 
    645 static void parse_modem(const char *path, struct l_dbus_message_iter *props)
    646 {
    647 	struct l_dbus_message_iter value;
    648 	const char *key;
    649 	struct ofono_modem *modem;
    650 
    651 	l_debug("modem found: %s", path);
    652 
    653 	if (l_queue_find(modems, match_modem_by_path, path)) {
    654 		/* should never happen */
    655 		l_error("modem %s already found", path);
    656 
    657 		return;
    658 	}
    659 
    660 	modem = l_new(struct ofono_modem, 1);
    661 	modem->path = l_strdup(path);
    662 
    663 	while (l_dbus_message_iter_next_entry(props, &key, &value)) {
    664 		if (!strcmp(key, "Interfaces"))
    665 			parse_interfaces(&value, modem);
    666 	}
    667 
    668 	/*
    669 	 * add watch in case SimAuthentication goes away
    670 	 */
    671 	modem->props_watch = l_dbus_add_signal_watch(dbus_get_bus(),
    672 			"org.ofono", path, OFONO_MODEM_IFACE,
    673 			"PropertyChanged", L_DBUS_MATCH_ARGUMENT(0),
    674 			"Interfaces", L_DBUS_MATCH_NONE,
    675 			interfaces_changed_cb, modem);
    676 
    677 	l_queue_push_tail(modems, modem);
    678 }
    679 
    680 static void modem_added_cb(struct l_dbus_message *message, void *user_data)
    681 {
    682 	struct l_dbus_message_iter props;
    683 	const char *path;
    684 
    685 	l_debug("");
    686 
    687 	if (!l_dbus_message_get_arguments(message, "oa{sv}", &path, &props))
    688 		return;
    689 
    690 	parse_modem(path, &props);
    691 }
    692 
    693 static void modem_removed_cb(struct l_dbus_message *message, void *user_data)
    694 {
    695 	const char *path;
    696 	struct ofono_modem *modem;
    697 
    698 	if (!l_dbus_message_get_arguments(message, "o", &path))
    699 		return;
    700 
    701 	modem = l_queue_remove_if(modems, match_modem_by_path, path);
    702 	if (!modem) {
    703 		l_warn("Cannot remove modem %s, not found", path);
    704 		return;
    705 	}
    706 
    707 	modem_destroy(modem);
    708 }
    709 
    710 static void get_modems_cb(struct l_dbus_message *reply, void *user_data)
    711 {
    712 	struct l_dbus_message_iter props;
    713 	struct l_dbus_message_iter modem_list;
    714 	const char *path = NULL;
    715 
    716 	if (l_dbus_message_is_error(reply)) {
    717 		l_debug("Error discovering modems");
    718 		return;
    719 	}
    720 
    721 	modems = l_queue_new();
    722 
    723 	if (l_dbus_message_get_arguments(reply, "a(oa{sv})", &modem_list)) {
    724 		while (l_dbus_message_iter_next_entry(&modem_list, &path,
    725 									&props))
    726 			parse_modem(path, &props);
    727 	}
    728 
    729 	/* watch for modems being added/removed */
    730 	modem_add_watch = l_dbus_add_signal_watch(dbus_get_bus(),
    731 			"org.ofono", "/", "org.ofono.Manager", "ModemAdded",
    732 			L_DBUS_MATCH_NONE, modem_added_cb, NULL);
    733 
    734 	modem_removed_watch = l_dbus_add_signal_watch(dbus_get_bus(),
    735 			"org.ofono", "/", "org.ofono.Manager", "ModemRemoved",
    736 			L_DBUS_MATCH_NONE, modem_removed_cb, NULL);
    737 }
    738 
    739 static void ofono_found(struct l_dbus *dbus, void *user_data)
    740 {
    741 	struct l_dbus_message *message;
    742 
    743 	l_debug("");
    744 
    745 	/* start by getting all current modems */
    746 	message = l_dbus_message_new_method_call(dbus, "org.ofono", "/",
    747 			"org.ofono.Manager", "GetModems");
    748 
    749 	l_dbus_message_set_arguments(message, "");
    750 
    751 	l_dbus_send_with_reply(dbus, message, get_modems_cb, NULL, NULL);
    752 }
    753 
    754 static void ofono_disappeared(struct l_dbus *dbus, void *user_data)
    755 {
    756 	l_debug("");
    757 
    758 	if (modems) {
    759 		l_queue_destroy(modems, modem_destroy);
    760 		modems = NULL;
    761 
    762 		l_dbus_remove_watch(dbus, modem_add_watch);
    763 		l_dbus_remove_watch(dbus, modem_removed_watch);
    764 	}
    765 }
    766 
    767 static int ofono_init(void)
    768 {
    769 	struct l_dbus *dbus = dbus_get_bus();
    770 
    771 	ofono_watch = l_dbus_add_service_watch(dbus, "org.ofono", ofono_found,
    772 			ofono_disappeared, NULL, NULL);
    773 
    774 	return 0;
    775 }
    776 
    777 static void ofono_exit(void)
    778 {
    779 	struct l_dbus *dbus = dbus_get_bus();
    780 
    781 	if (modems)
    782 		ofono_disappeared(dbus, NULL);
    783 
    784 	l_dbus_remove_watch(dbus, ofono_watch);
    785 }
    786 
    787 IWD_MODULE(ofono, ofono_init, ofono_exit);
    788 IWD_MODULE_DEPENDS(ofono, simauth);