| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
/* |
| 4 |
* Minimal mock of ModemManager's D-Bus interface for testing systemd-networkd |
| 5 |
* wwan/bearer support. |
| 6 |
* |
| 7 |
* Claims the org.freedesktop.ModemManager1 bus name and responds to: |
| 8 |
* - GetManagedObjects on /org/freedesktop/ModemManager1 |
| 9 |
* - GetAll on /org/freedesktop/ModemManager1/Bearer/0 |
| 10 |
* - Simple.Connect on /org/freedesktop/ModemManager1/Modem/0 |
| 11 |
*/ |
| 12 |
|
| 13 |
#include "sd-bus.h" |
| 14 |
#include "sd-daemon.h" |
| 15 |
#include "sd-event.h" |
| 16 |
|
| 17 |
#include "alloc-util.h" |
| 18 |
#include "build.h" |
| 19 |
#include "format-table.h" |
| 20 |
#include "help-util.h" |
| 21 |
#include "log.h" |
| 22 |
#include "main-func.h" |
| 23 |
#include "options.h" |
| 24 |
#include "parse-util.h" |
| 25 |
#include "string-util.h" |
| 26 |
|
| 27 |
static char *arg_ifname = NULL; |
| 28 |
static char *arg_ipv4_address = NULL; |
| 29 |
static char *arg_ipv4_gateway = NULL; |
| 30 |
static uint32_t arg_ipv4_prefix = 24; |
| 31 |
static char *arg_ipv6_address = NULL; |
| 32 |
static char *arg_ipv6_gateway = NULL; |
| 33 |
static uint32_t arg_ipv6_prefix = 64; |
| 34 |
|
| 35 |
STATIC_DESTRUCTOR_REGISTER(arg_ifname, freep); |
| 36 |
STATIC_DESTRUCTOR_REGISTER(arg_ipv4_address, freep); |
| 37 |
STATIC_DESTRUCTOR_REGISTER(arg_ipv4_gateway, freep); |
| 38 |
STATIC_DESTRUCTOR_REGISTER(arg_ipv6_address, freep); |
| 39 |
STATIC_DESTRUCTOR_REGISTER(arg_ipv6_gateway, freep); |
| 40 |
|
| 41 |
/* ModemManager enum values */ |
| 42 |
#define MM_BEARER_IP_METHOD_STATIC 2 |
| 43 |
#define MM_MODEM_PORT_TYPE_NET 2 |
| 44 |
#define MM_MODEM_STATE_CONNECTED 11 |
| 45 |
|
| 46 |
static int append_bearer_properties(sd_bus_message *reply) { |
| 47 |
int r; |
| 48 |
|
| 49 |
/* a{sv} of bearer properties */ |
| 50 |
r = sd_bus_message_open_container(reply, 'a', "{sv}"); |
| 51 |
if (r < 0) |
| 52 |
return r; |
| 53 |
|
| 54 |
/* Interface */ |
| 55 |
r = sd_bus_message_append(reply, "{sv}", "Interface", "s", arg_ifname); |
| 56 |
if (r < 0) |
| 57 |
return r; |
| 58 |
|
| 59 |
/* Connected */ |
| 60 |
r = sd_bus_message_append(reply, "{sv}", "Connected", "b", true); |
| 61 |
if (r < 0) |
| 62 |
return r; |
| 63 |
|
| 64 |
/* Ip4Config: a{sv} */ |
| 65 |
if (arg_ipv4_address) { |
| 66 |
r = sd_bus_message_open_container(reply, 'e', "sv"); |
| 67 |
if (r < 0) |
| 68 |
return r; |
| 69 |
r = sd_bus_message_append_basic(reply, 's', "Ip4Config"); |
| 70 |
if (r < 0) |
| 71 |
return r; |
| 72 |
r = sd_bus_message_open_container(reply, 'v', "a{sv}"); |
| 73 |
if (r < 0) |
| 74 |
return r; |
| 75 |
r = sd_bus_message_open_container(reply, 'a', "{sv}"); |
| 76 |
if (r < 0) |
| 77 |
return r; |
| 78 |
r = sd_bus_message_append(reply, "{sv}", "method", "u", (uint32_t) MM_BEARER_IP_METHOD_STATIC); |
| 79 |
if (r < 0) |
| 80 |
return r; |
| 81 |
r = sd_bus_message_append(reply, "{sv}", "address", "s", arg_ipv4_address); |
| 82 |
if (r < 0) |
| 83 |
return r; |
| 84 |
r = sd_bus_message_append(reply, "{sv}", "prefix", "u", arg_ipv4_prefix); |
| 85 |
if (r < 0) |
| 86 |
return r; |
| 87 |
r = sd_bus_message_append(reply, "{sv}", "gateway", "s", arg_ipv4_gateway); |
| 88 |
if (r < 0) |
| 89 |
return r; |
| 90 |
r = sd_bus_message_append(reply, "{sv}", "mtu", "u", (uint32_t) 1500); |
| 91 |
if (r < 0) |
| 92 |
return r; |
| 93 |
r = sd_bus_message_close_container(reply); /* a{sv} */ |
| 94 |
if (r < 0) |
| 95 |
return r; |
| 96 |
r = sd_bus_message_close_container(reply); /* v */ |
| 97 |
if (r < 0) |
| 98 |
return r; |
| 99 |
r = sd_bus_message_close_container(reply); /* e */ |
| 100 |
if (r < 0) |
| 101 |
return r; |
| 102 |
} |
| 103 |
|
| 104 |
/* Ip6Config: a{sv} */ |
| 105 |
if (arg_ipv6_address) { |
| 106 |
r = sd_bus_message_open_container(reply, 'e', "sv"); |
| 107 |
if (r < 0) |
| 108 |
return r; |
| 109 |
r = sd_bus_message_append_basic(reply, 's', "Ip6Config"); |
| 110 |
if (r < 0) |
| 111 |
return r; |
| 112 |
r = sd_bus_message_open_container(reply, 'v', "a{sv}"); |
| 113 |
if (r < 0) |
| 114 |
return r; |
| 115 |
r = sd_bus_message_open_container(reply, 'a', "{sv}"); |
| 116 |
if (r < 0) |
| 117 |
return r; |
| 118 |
r = sd_bus_message_append(reply, "{sv}", "method", "u", (uint32_t) MM_BEARER_IP_METHOD_STATIC); |
| 119 |
if (r < 0) |
| 120 |
return r; |
| 121 |
r = sd_bus_message_append(reply, "{sv}", "address", "s", arg_ipv6_address); |
| 122 |
if (r < 0) |
| 123 |
return r; |
| 124 |
r = sd_bus_message_append(reply, "{sv}", "prefix", "u", arg_ipv6_prefix); |
| 125 |
if (r < 0) |
| 126 |
return r; |
| 127 |
r = sd_bus_message_append(reply, "{sv}", "gateway", "s", arg_ipv6_gateway); |
| 128 |
if (r < 0) |
| 129 |
return r; |
| 130 |
r = sd_bus_message_append(reply, "{sv}", "mtu", "u", (uint32_t) 1500); |
| 131 |
if (r < 0) |
| 132 |
return r; |
| 133 |
r = sd_bus_message_close_container(reply); /* a{sv} */ |
| 134 |
if (r < 0) |
| 135 |
return r; |
| 136 |
r = sd_bus_message_close_container(reply); /* v */ |
| 137 |
if (r < 0) |
| 138 |
return r; |
| 139 |
r = sd_bus_message_close_container(reply); /* e */ |
| 140 |
if (r < 0) |
| 141 |
return r; |
| 142 |
} |
| 143 |
|
| 144 |
/* Properties: a{sv} with apn */ |
| 145 |
r = sd_bus_message_open_container(reply, 'e', "sv"); |
| 146 |
if (r < 0) |
| 147 |
return r; |
| 148 |
r = sd_bus_message_append_basic(reply, 's', "Properties"); |
| 149 |
if (r < 0) |
| 150 |
return r; |
| 151 |
r = sd_bus_message_open_container(reply, 'v', "a{sv}"); |
| 152 |
if (r < 0) |
| 153 |
return r; |
| 154 |
r = sd_bus_message_open_container(reply, 'a', "{sv}"); |
| 155 |
if (r < 0) |
| 156 |
return r; |
| 157 |
r = sd_bus_message_append(reply, "{sv}", "apn", "s", "internet.test"); |
| 158 |
if (r < 0) |
| 159 |
return r; |
| 160 |
r = sd_bus_message_close_container(reply); /* a{sv} */ |
| 161 |
if (r < 0) |
| 162 |
return r; |
| 163 |
r = sd_bus_message_close_container(reply); /* v */ |
| 164 |
if (r < 0) |
| 165 |
return r; |
| 166 |
r = sd_bus_message_close_container(reply); /* e */ |
| 167 |
if (r < 0) |
| 168 |
return r; |
| 169 |
|
| 170 |
r = sd_bus_message_close_container(reply); /* outer a{sv} */ |
| 171 |
if (r < 0) |
| 172 |
return r; |
| 173 |
|
| 174 |
return 0; |
| 175 |
} |
| 176 |
|
| 177 |
static int handle_get_managed_objects(sd_bus_message *msg, void *userdata, sd_bus_error *error) { |
| 178 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 179 |
int r; |
| 180 |
|
| 181 |
r = sd_bus_message_new_method_return(msg, &reply); |
| 182 |
if (r < 0) |
| 183 |
return r; |
| 184 |
|
| 185 |
/* a{oa{sa{sv}}} */ |
| 186 |
r = sd_bus_message_open_container(reply, 'a', "{oa{sa{sv}}}"); |
| 187 |
if (r < 0) |
| 188 |
return r; |
| 189 |
|
| 190 |
/* Modem object */ |
| 191 |
r = sd_bus_message_open_container(reply, 'e', "oa{sa{sv}}"); |
| 192 |
if (r < 0) |
| 193 |
return r; |
| 194 |
r = sd_bus_message_append_basic(reply, 'o', "/org/freedesktop/ModemManager1/Modem/0"); |
| 195 |
if (r < 0) |
| 196 |
return r; |
| 197 |
|
| 198 |
/* Array of interfaces */ |
| 199 |
r = sd_bus_message_open_container(reply, 'a', "{sa{sv}}"); |
| 200 |
if (r < 0) |
| 201 |
return r; |
| 202 |
|
| 203 |
/* org.freedesktop.ModemManager1.Modem interface */ |
| 204 |
r = sd_bus_message_open_container(reply, 'e', "sa{sv}"); |
| 205 |
if (r < 0) |
| 206 |
return r; |
| 207 |
r = sd_bus_message_append_basic(reply, 's', "org.freedesktop.ModemManager1.Modem"); |
| 208 |
if (r < 0) |
| 209 |
return r; |
| 210 |
|
| 211 |
/* Modem properties: a{sv} */ |
| 212 |
r = sd_bus_message_open_container(reply, 'a', "{sv}"); |
| 213 |
if (r < 0) |
| 214 |
return r; |
| 215 |
|
| 216 |
/* Bearers: ao */ |
| 217 |
r = sd_bus_message_append(reply, "{sv}", "Bearers", "ao", 1, "/org/freedesktop/ModemManager1/Bearer/0"); |
| 218 |
if (r < 0) |
| 219 |
return r; |
| 220 |
|
| 221 |
/* State: i (CONNECTED) */ |
| 222 |
r = sd_bus_message_append(reply, "{sv}", "State", "i", (int32_t) MM_MODEM_STATE_CONNECTED); |
| 223 |
if (r < 0) |
| 224 |
return r; |
| 225 |
|
| 226 |
/* StateFailedReason: u (NONE) */ |
| 227 |
r = sd_bus_message_append(reply, "{sv}", "StateFailedReason", "u", (uint32_t) 0); |
| 228 |
if (r < 0) |
| 229 |
return r; |
| 230 |
|
| 231 |
/* Manufacturer */ |
| 232 |
r = sd_bus_message_append(reply, "{sv}", "Manufacturer", "s", "MockModem"); |
| 233 |
if (r < 0) |
| 234 |
return r; |
| 235 |
|
| 236 |
/* Model */ |
| 237 |
r = sd_bus_message_append(reply, "{sv}", "Model", "s", "Virtual"); |
| 238 |
if (r < 0) |
| 239 |
return r; |
| 240 |
|
| 241 |
/* Ports: a(su) — array of structs with port name and type */ |
| 242 |
r = sd_bus_message_open_container(reply, 'e', "sv"); |
| 243 |
if (r < 0) |
| 244 |
return r; |
| 245 |
r = sd_bus_message_append_basic(reply, 's', "Ports"); |
| 246 |
if (r < 0) |
| 247 |
return r; |
| 248 |
r = sd_bus_message_open_container(reply, 'v', "a(su)"); |
| 249 |
if (r < 0) |
| 250 |
return r; |
| 251 |
r = sd_bus_message_open_container(reply, 'a', "(su)"); |
| 252 |
if (r < 0) |
| 253 |
return r; |
| 254 |
r = sd_bus_message_append(reply, "(su)", arg_ifname, (uint32_t) MM_MODEM_PORT_TYPE_NET); |
| 255 |
if (r < 0) |
| 256 |
return r; |
| 257 |
r = sd_bus_message_close_container(reply); /* a(su) */ |
| 258 |
if (r < 0) |
| 259 |
return r; |
| 260 |
r = sd_bus_message_close_container(reply); /* v */ |
| 261 |
if (r < 0) |
| 262 |
return r; |
| 263 |
r = sd_bus_message_close_container(reply); /* e */ |
| 264 |
if (r < 0) |
| 265 |
return r; |
| 266 |
|
| 267 |
r = sd_bus_message_close_container(reply); /* modem properties a{sv} */ |
| 268 |
if (r < 0) |
| 269 |
return r; |
| 270 |
r = sd_bus_message_close_container(reply); /* e sa{sv} */ |
| 271 |
if (r < 0) |
| 272 |
return r; |
| 273 |
r = sd_bus_message_close_container(reply); /* a{sa{sv}} */ |
| 274 |
if (r < 0) |
| 275 |
return r; |
| 276 |
r = sd_bus_message_close_container(reply); /* e oa{sa{sv}} */ |
| 277 |
if (r < 0) |
| 278 |
return r; |
| 279 |
r = sd_bus_message_close_container(reply); /* a{oa{sa{sv}}} */ |
| 280 |
if (r < 0) |
| 281 |
return r; |
| 282 |
|
| 283 |
r = sd_bus_send(NULL, reply, NULL); |
| 284 |
if (r < 0) |
| 285 |
return r; |
| 286 |
|
| 287 |
return 1; /* handled */ |
| 288 |
} |
| 289 |
|
| 290 |
static int handle_get_all(sd_bus_message *msg, void *userdata, sd_bus_error *error) { |
| 291 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 292 |
int r; |
| 293 |
|
| 294 |
r = sd_bus_message_new_method_return(msg, &reply); |
| 295 |
if (r < 0) |
| 296 |
return r; |
| 297 |
|
| 298 |
/* bearer_get_all_handler() in networkd expects a leading interface name string |
| 299 |
* before the a{sv} properties dict (it calls sd_bus_message_skip(message, "s")). */ |
| 300 |
r = sd_bus_message_append_basic(reply, 's', "org.freedesktop.ModemManager1.Bearer"); |
| 301 |
if (r < 0) |
| 302 |
return r; |
| 303 |
|
| 304 |
r = append_bearer_properties(reply); |
| 305 |
if (r < 0) |
| 306 |
return r; |
| 307 |
|
| 308 |
r = sd_bus_send(NULL, reply, NULL); |
| 309 |
if (r < 0) |
| 310 |
return r; |
| 311 |
|
| 312 |
return 1; /* handled */ |
| 313 |
} |
| 314 |
|
| 315 |
static int handle_simple_connect(sd_bus_message *msg, void *userdata, sd_bus_error *error) { |
| 316 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 317 |
int r; |
| 318 |
|
| 319 |
/* Return the bearer path */ |
| 320 |
r = sd_bus_message_new_method_return(msg, &reply); |
| 321 |
if (r < 0) |
| 322 |
return r; |
| 323 |
|
| 324 |
r = sd_bus_message_append(reply, "o", "/org/freedesktop/ModemManager1/Bearer/0"); |
| 325 |
if (r < 0) |
| 326 |
return r; |
| 327 |
|
| 328 |
r = sd_bus_send(NULL, reply, NULL); |
| 329 |
if (r < 0) |
| 330 |
return r; |
| 331 |
|
| 332 |
return 1; /* handled */ |
| 333 |
} |
| 334 |
|
| 335 |
static int filter_handler(sd_bus_message *m, void *userdata, sd_bus_error *error) { |
| 336 |
const char *path, *interface, *member; |
| 337 |
uint8_t type; |
| 338 |
|
| 339 |
if (sd_bus_message_get_type(m, &type) < 0 || type != SD_BUS_MESSAGE_METHOD_CALL) |
| 340 |
return 0; |
| 341 |
|
| 342 |
path = sd_bus_message_get_path(m); |
| 343 |
interface = sd_bus_message_get_interface(m); |
| 344 |
member = sd_bus_message_get_member(m); |
| 345 |
|
| 346 |
if (!path || !interface || !member) |
| 347 |
return 0; |
| 348 |
|
| 349 |
if (streq(path, "/org/freedesktop/ModemManager1") && |
| 350 |
streq(interface, "org.freedesktop.DBus.ObjectManager") && |
| 351 |
streq(member, "GetManagedObjects")) |
| 352 |
return handle_get_managed_objects(m, userdata, error); |
| 353 |
|
| 354 |
if (startswith(path, "/org/freedesktop/ModemManager1/Bearer/") && |
| 355 |
streq(interface, "org.freedesktop.DBus.Properties") && |
| 356 |
streq(member, "GetAll")) |
| 357 |
return handle_get_all(m, userdata, error); |
| 358 |
|
| 359 |
if (startswith(path, "/org/freedesktop/ModemManager1/Modem/") && |
| 360 |
streq(interface, "org.freedesktop.ModemManager1.Modem.Simple") && |
| 361 |
streq(member, "Connect")) |
| 362 |
return handle_simple_connect(m, userdata, error); |
| 363 |
|
| 364 |
return 0; |
| 365 |
} |
| 366 |
|
| 367 |
static int help(void) { |
| 368 |
_cleanup_(table_unrefp) Table *options = NULL; |
| 369 |
int r; |
| 370 |
|
| 371 |
r = option_parser_get_help_table(&options); |
| 372 |
if (r < 0) |
| 373 |
return r; |
| 374 |
|
| 375 |
help_cmdline("[OPTIONS...]"); |
| 376 |
help_abstract("Mock ModemManager D-Bus service for testing."); |
| 377 |
|
| 378 |
help_section("Options"); |
| 379 |
return table_print_or_warn(options); |
| 380 |
} |
| 381 |
|
| 382 |
static int parse_argv(int argc, char *argv[]) { |
| 383 |
int r; |
| 384 |
|
| 385 |
assert(argc >= 0); |
| 386 |
assert(argv); |
| 387 |
|
| 388 |
OptionParser opts = { argc, argv }; |
| 389 |
|
| 390 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 391 |
switch (c) { |
| 392 |
|
| 393 |
OPTION_COMMON_HELP: |
| 394 |
return help(); |
| 395 |
|
| 396 |
OPTION_COMMON_VERSION: |
| 397 |
return version(); |
| 398 |
|
| 399 |
OPTION_LONG("ifname", "NAME", "Interface name"): |
| 400 |
if (free_and_strdup(&arg_ifname, opts.arg) < 0) |
| 401 |
return log_oom(); |
| 402 |
break; |
| 403 |
|
| 404 |
OPTION_LONG("ipv4-address", "ADDR", "IPv4 address"): |
| 405 |
if (free_and_strdup(&arg_ipv4_address, opts.arg) < 0) |
| 406 |
return log_oom(); |
| 407 |
break; |
| 408 |
|
| 409 |
OPTION_LONG("ipv4-gateway", "ADDR", "IPv4 gateway"): |
| 410 |
if (free_and_strdup(&arg_ipv4_gateway, opts.arg) < 0) |
| 411 |
return log_oom(); |
| 412 |
break; |
| 413 |
|
| 414 |
OPTION_LONG("ipv4-prefix", "LEN", "IPv4 prefix length"): |
| 415 |
r = safe_atou32(opts.arg, &arg_ipv4_prefix); |
| 416 |
if (r < 0) |
| 417 |
return log_error_errno(r, "Failed to parse IPv4 prefix length: %m"); |
| 418 |
break; |
| 419 |
|
| 420 |
OPTION_LONG("ipv6-address", "ADDR", "IPv6 address"): |
| 421 |
if (free_and_strdup(&arg_ipv6_address, opts.arg) < 0) |
| 422 |
return log_oom(); |
| 423 |
break; |
| 424 |
|
| 425 |
OPTION_LONG("ipv6-gateway", "ADDR", "IPv6 gateway"): |
| 426 |
if (free_and_strdup(&arg_ipv6_gateway, opts.arg) < 0) |
| 427 |
return log_oom(); |
| 428 |
break; |
| 429 |
|
| 430 |
OPTION_LONG("ipv6-prefix", "LEN", "IPv6 prefix length"): |
| 431 |
r = safe_atou32(opts.arg, &arg_ipv6_prefix); |
| 432 |
if (r < 0) |
| 433 |
return log_error_errno(r, "Failed to parse IPv6 prefix length: %m"); |
| 434 |
break; |
| 435 |
} |
| 436 |
|
| 437 |
if (!arg_ifname) |
| 438 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--ifname is required"); |
| 439 |
|
| 440 |
return 1; /* work to do */ |
| 441 |
} |
| 442 |
|
| 443 |
static int run(int argc, char *argv[]) { |
| 444 |
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
| 445 |
_cleanup_(sd_event_unrefp) sd_event *event = NULL; |
| 446 |
int r; |
| 447 |
|
| 448 |
log_setup(); |
| 449 |
|
| 450 |
r = parse_argv(argc, argv); |
| 451 |
if (r <= 0) |
| 452 |
return r; |
| 453 |
|
| 454 |
r = sd_event_new(&event); |
| 455 |
if (r < 0) |
| 456 |
return log_error_errno(r, "Failed to create event loop: %m"); |
| 457 |
|
| 458 |
r = sd_bus_open_system(&bus); |
| 459 |
if (r < 0) |
| 460 |
return log_error_errno(r, "Failed to connect to system bus: %m"); |
| 461 |
|
| 462 |
r = sd_bus_add_filter(bus, NULL, filter_handler, NULL); |
| 463 |
if (r < 0) |
| 464 |
return log_error_errno(r, "Failed to add filter: %m"); |
| 465 |
|
| 466 |
r = sd_bus_request_name(bus, "org.freedesktop.ModemManager1", 0); |
| 467 |
if (r < 0) |
| 468 |
return log_error_errno(r, "Failed to acquire bus name: %m"); |
| 469 |
|
| 470 |
r = sd_bus_attach_event(bus, event, 0); |
| 471 |
if (r < 0) |
| 472 |
return log_error_errno(r, "Failed to attach bus to event loop: %m"); |
| 473 |
|
| 474 |
(void) sd_notify(0, "READY=1"); |
| 475 |
|
| 476 |
return sd_event_loop(event); |
| 477 |
} |
| 478 |
|
| 479 |
DEFINE_MAIN_FUNCTION(run); |