netconfig-commit.c (21173B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2022 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 <arpa/inet.h> 28 #include <netinet/if_ether.h> 29 #include <linux/icmpv6.h> 30 31 #include <ell/ell.h> 32 33 #include "ell/useful.h" 34 #include "src/iwd.h" 35 #include "src/common.h" 36 #include "src/util.h" 37 #include "src/netdev.h" 38 #include "src/ie.h" 39 #include "src/resolve.h" 40 #include "src/dbus.h" 41 #include "src/netconfig.h" 42 43 struct netconfig_commit_ops { 44 bool (*init_data)(struct netconfig *netconfig); 45 void (*free_data)(struct netconfig *netconfig, const char *reasonstr); 46 void (*commit)(struct netconfig *netconfig, uint8_t family, 47 enum l_netconfig_event event); 48 }; 49 50 static struct l_netlink *rtnl; 51 52 static void netconfig_rtnl_commit(struct netconfig *netconfig, uint8_t family, 53 enum l_netconfig_event event); 54 static void netconfig_rtnl_free_data(struct netconfig *netconfig, 55 const char *reasonstr); 56 57 /* Default backend */ 58 static struct netconfig_commit_ops netconfig_rtnl_ops = { 59 .commit = netconfig_rtnl_commit, 60 .free_data = netconfig_rtnl_free_data, 61 }; 62 63 /* Same backend for all netconfig objects */ 64 static const struct netconfig_commit_ops *commit_ops = &netconfig_rtnl_ops; 65 66 static struct l_queue *netconfig_list; 67 68 void netconfig_commit_init(struct netconfig *netconfig) 69 { 70 if (!rtnl) 71 rtnl = l_rtnl_get(); 72 73 if (!netconfig_list) 74 netconfig_list = l_queue_new(); 75 76 l_queue_push_tail(netconfig_list, netconfig); 77 78 L_WARN_ON(netconfig->commit_data); 79 80 if (commit_ops->init_data) 81 commit_ops->init_data(netconfig); 82 } 83 84 void netconfig_commit_free(struct netconfig *netconfig, const char *reasonstr) 85 { 86 if (commit_ops->free_data) 87 commit_ops->free_data(netconfig, reasonstr); 88 89 L_WARN_ON(!l_queue_remove(netconfig_list, netconfig)); 90 91 if (l_queue_isempty(netconfig_list)) 92 l_queue_destroy(l_steal_ptr(netconfig_list), NULL); 93 } 94 95 static void netconfig_commit_print_addrs(const char *verb, 96 const struct l_queue_entry *addrs) 97 { 98 for (; addrs; addrs = addrs->next) { 99 const struct l_rtnl_address *addr = addrs->data; 100 char str[INET6_ADDRSTRLEN]; 101 102 if (l_rtnl_address_get_address(addr, str)) 103 l_debug("%s address: %s", verb, str); 104 } 105 } 106 107 void netconfig_commit(struct netconfig *netconfig, uint8_t family, 108 enum l_netconfig_event event) 109 { 110 const struct l_queue_entry *added; 111 const struct l_queue_entry *removed; 112 const struct l_queue_entry *expired; 113 114 l_netconfig_get_addresses(netconfig->nc, &added, NULL, 115 &removed, &expired); 116 117 /* Only print IP additions and removals to avoid cluttering the log */ 118 netconfig_commit_print_addrs("installing", added); 119 netconfig_commit_print_addrs("removing", removed); 120 netconfig_commit_print_addrs("expired", expired); 121 122 commit_ops->commit(netconfig, family, event); 123 124 if (event == L_NETCONFIG_EVENT_CONFIGURE) { 125 /* 126 * Done here instead of in ops->commit because the MACs are 127 * not considered part of the network configuration 128 * (particularly Network Manager's "level 3 config" or l3cfg) 129 * so we can handle this ourselves independent of the backend. 130 */ 131 if (family == AF_INET && 132 !netconfig->static_config[INDEX_FOR_AF(family)]) 133 netconfig_dhcp_gateway_to_arp(netconfig); 134 135 if (!netconfig->connected[INDEX_FOR_AF(family)] && 136 netconfig_use_fils_addr(netconfig, family)) 137 netconfig_commit_fils_macs(netconfig, family); 138 } 139 } 140 141 static void netconfig_switch_backend(const struct netconfig_commit_ops *new_ops) 142 { 143 const struct l_queue_entry *entry; 144 145 for (entry = l_queue_get_entries(netconfig_list); entry; 146 entry = entry->next) { 147 struct netconfig *netconfig = entry->data; 148 149 if (commit_ops->free_data) 150 commit_ops->free_data(netconfig, ""); 151 152 if (new_ops->init_data) 153 new_ops->init_data(netconfig); 154 } 155 156 commit_ops = new_ops; 157 } 158 159 /* 160 * Called by all backends when netconfig_commit finishes, synchronously or 161 * asynchronously. 162 */ 163 static void netconfig_commit_done(struct netconfig *netconfig, uint8_t family, 164 enum l_netconfig_event event, 165 bool success) 166 { 167 bool connected = netconfig->connected[INDEX_FOR_AF(family)]; 168 169 if (!success) { 170 netconfig->connected[INDEX_FOR_AF(family)] = false; 171 172 if (netconfig->notify && family == AF_INET) 173 netconfig->notify(NETCONFIG_EVENT_FAILED, 174 netconfig->user_data); 175 return; 176 } 177 178 switch (event) { 179 case L_NETCONFIG_EVENT_CONFIGURE: 180 case L_NETCONFIG_EVENT_UPDATE: 181 netconfig->connected[INDEX_FOR_AF(family)] = true; 182 183 if (family == AF_INET && !connected && netconfig->notify) 184 netconfig->notify(NETCONFIG_EVENT_CONNECTED, 185 netconfig->user_data); 186 187 break; 188 189 case L_NETCONFIG_EVENT_UNCONFIGURE: 190 case L_NETCONFIG_EVENT_FAILED: 191 break; 192 } 193 } 194 195 static void netconfig_set_neighbor_entry_cb(int error, 196 uint16_t type, const void *data, 197 uint32_t len, void *user_data) 198 { 199 if (error) 200 l_error("l_rtnl_neighbor_set_hwaddr failed: %s (%i)", 201 strerror(-error), error); 202 } 203 204 void netconfig_dhcp_gateway_to_arp(struct netconfig *netconfig) 205 { 206 struct l_dhcp_client *dhcp = l_netconfig_get_dhcp_client(netconfig->nc); 207 const struct l_dhcp_lease *lease; 208 _auto_(l_free) char *server_id = NULL; 209 _auto_(l_free) char *gw = NULL; 210 const uint8_t *server_mac; 211 struct in_addr in_gw; 212 uint32_t ifindex = netdev_get_ifindex(netconfig->netdev); 213 214 /* Can only do this for DHCP in certain network setups */ 215 if (netconfig->static_config[INDEX_FOR_AF(AF_INET)] || 216 netconfig->fils_override) 217 return; 218 219 lease = l_dhcp_client_get_lease(dhcp); 220 if (!lease) 221 return; 222 223 server_id = l_dhcp_lease_get_server_id(lease); 224 gw = l_dhcp_lease_get_gateway(lease); 225 server_mac = l_dhcp_lease_get_server_mac(lease); 226 227 if (!gw || strcmp(server_id, gw) || !server_mac) 228 return; 229 230 l_debug("Gateway MAC is known, setting into ARP cache"); 231 in_gw.s_addr = l_dhcp_lease_get_gateway_u32(lease); 232 233 if (!l_rtnl_neighbor_set_hwaddr(rtnl, ifindex, AF_INET, 234 &in_gw, server_mac, ETH_ALEN, 235 netconfig_set_neighbor_entry_cb, NULL, 236 NULL)) 237 l_debug("l_rtnl_neighbor_set_hwaddr failed"); 238 } 239 240 void netconfig_commit_fils_macs(struct netconfig *netconfig, uint8_t family) 241 { 242 const struct ie_fils_ip_addr_response_info *fils = 243 netconfig->fils_override; 244 const void *addr; 245 const void *hwaddr; 246 size_t addr_len = (family == AF_INET ? 4 : 16); 247 uint32_t ifindex = netdev_get_ifindex(netconfig->netdev); 248 249 if (!fils) 250 return; 251 252 /* 253 * Attempt to use the gateway/DNS MAC addressed received from the AP 254 * by writing the mapping directly into the netdev's ARP table so as 255 * to save one data frame roundtrip before first IP connections are 256 * established. This is very low-priority but print error messages 257 * just because they may indicate bigger problems. 258 */ 259 260 addr = (family == AF_INET ? (void *) &fils->ipv4_gateway : 261 (void *) &fils->ipv6_gateway); 262 hwaddr = (family == AF_INET ? 263 &fils->ipv4_gateway_mac : &fils->ipv6_gateway_mac); 264 265 if (!l_memeqzero(addr, addr_len) && !l_memeqzero(hwaddr, ETH_ALEN) && 266 unlikely(!l_rtnl_neighbor_set_hwaddr(rtnl, ifindex, 267 family, addr, hwaddr, ETH_ALEN, 268 netconfig_set_neighbor_entry_cb, 269 NULL, NULL))) 270 l_debug("l_rtnl_neighbor_set_hwaddr(%s, gateway) failed", 271 family == AF_INET ? "AF_INET" : "AF_INET6"); 272 273 addr = (family == AF_INET ? (void *) &fils->ipv4_dns : 274 (void *) &fils->ipv6_dns); 275 hwaddr = (family == AF_INET ? 276 &fils->ipv4_dns_mac : &fils->ipv6_dns_mac); 277 278 if (!l_memeqzero(addr, addr_len) && !l_memeqzero(hwaddr, ETH_ALEN) && 279 unlikely(!l_rtnl_neighbor_set_hwaddr(rtnl, ifindex, 280 family, addr, hwaddr, ETH_ALEN, 281 netconfig_set_neighbor_entry_cb, 282 NULL, NULL))) 283 l_debug("l_rtnl_neighbor_set_hwaddr(%s, DNS) failed", 284 family == AF_INET ? "AF_INET" : "AF_INET6"); 285 } 286 287 static void netconfig_dns_list_update(struct netconfig *netconfig) 288 { 289 _auto_(l_strv_free) char **dns_list = 290 l_netconfig_get_dns_list(netconfig->nc); 291 292 if (l_strv_eq(netconfig->dns_list, dns_list)) 293 return; 294 295 if (netconfig->resolve && dns_list) 296 resolve_set_dns(netconfig->resolve, dns_list); 297 298 l_strv_free(netconfig->dns_list); 299 netconfig->dns_list = l_steal_ptr(dns_list); 300 } 301 302 static void netconfig_domains_update(struct netconfig *netconfig) 303 { 304 _auto_(l_strv_free) char **domains = 305 l_netconfig_get_domain_names(netconfig->nc); 306 307 if (l_strv_eq(netconfig->domains, domains)) 308 return; 309 310 if (netconfig->resolve && domains) 311 resolve_set_domains(netconfig->resolve, domains); 312 313 l_strv_free(netconfig->domains); 314 netconfig->domains = l_steal_ptr(domains); 315 } 316 317 static void netconfig_rtnl_commit(struct netconfig *netconfig, uint8_t family, 318 enum l_netconfig_event event) 319 { 320 l_netconfig_apply_rtnl(netconfig->nc); 321 322 netconfig_dns_list_update(netconfig); 323 netconfig_domains_update(netconfig); 324 325 if (event == L_NETCONFIG_EVENT_CONFIGURE && family == AF_INET) 326 /* 327 * netconfig->mdns is currently only loaded in 328 * netconfig_load_settings() so we can set it once on 329 * the CONFIGURE event. 330 */ 331 resolve_set_mdns(netconfig->resolve, netconfig->mdns); 332 333 if (event == L_NETCONFIG_EVENT_UNCONFIGURE && family == AF_INET) { 334 l_strv_free(l_steal_ptr(netconfig->dns_list)); 335 l_strv_free(l_steal_ptr(netconfig->domains)); 336 resolve_revert(netconfig->resolve); 337 } 338 339 netconfig_commit_done(netconfig, family, event, true); 340 } 341 342 static void netconfig_rtnl_free_data(struct netconfig *netconfig, 343 const char *reasonstr) 344 { 345 l_strv_free(l_steal_ptr(netconfig->dns_list)); 346 l_strv_free(l_steal_ptr(netconfig->domains)); 347 } 348 349 350 struct netconfig_agent_data { 351 uint32_t pending_id[2]; 352 }; 353 354 struct netconfig_agent_call_data { 355 struct netconfig *netconfig; 356 uint8_t family; 357 enum l_netconfig_event event; 358 }; 359 360 static char *netconfig_agent_name; 361 static char *netconfig_agent_path; 362 static unsigned int netconfig_agent_watch; 363 364 static void netconfig_agent_cancel(struct netconfig *netconfig, uint8_t family, 365 const char *reasonstr) 366 { 367 struct netconfig_agent_data *data = netconfig->commit_data; 368 struct l_dbus *dbus = dbus_get_bus(); 369 const char *dev_path = netdev_get_path(netconfig->netdev); 370 struct l_dbus_message *message; 371 const char *method; 372 373 if (!data || !data->pending_id[INDEX_FOR_AF(family)]) 374 return; 375 376 l_dbus_cancel(dbus, data->pending_id[INDEX_FOR_AF(family)]); 377 data->pending_id[INDEX_FOR_AF(family)] = 0; 378 379 method = (family == AF_INET ? "CancelIPv4" : "CancelIPv6"); 380 l_debug("sending a %s(%s, %s) to %s %s", method, dev_path, reasonstr, 381 netconfig_agent_name, netconfig_agent_path); 382 383 message = l_dbus_message_new_method_call(dbus, netconfig_agent_name, 384 netconfig_agent_path, 385 IWD_NETCONFIG_AGENT_INTERFACE, 386 method); 387 l_dbus_message_set_arguments(message, "os", dev_path, reasonstr); 388 l_dbus_message_set_no_reply(message, true); 389 l_dbus_send(dbus, message); 390 } 391 392 static void netconfig_agent_receive_reply(struct l_dbus_message *reply, 393 void *user_data) 394 { 395 struct netconfig_agent_call_data *cd = user_data; 396 struct netconfig_agent_data *data = cd->netconfig->commit_data; 397 const char *error, *text; 398 bool success = true; 399 400 l_debug("agent reply from %s", l_dbus_message_get_sender(reply)); 401 402 data->pending_id[INDEX_FOR_AF(cd->family)] = 0; 403 404 if (!cd->netconfig->started) 405 return; 406 407 if (l_dbus_message_get_error(reply, &error, &text)) { 408 success = false; 409 l_error("netconfig agent call returned %s(\"%s\")", 410 error, text); 411 } else if (!l_dbus_message_get_arguments(reply, "")) { 412 success = false; 413 l_error("netconfig agent call reply signature wrong: %s", 414 l_dbus_message_get_signature(reply)); 415 } 416 417 netconfig_commit_done(cd->netconfig, cd->family, cd->event, success); 418 } 419 420 #define IS_IPV6_STR_FAST(str) (strchr(str, ':') != NULL) 421 422 typedef void (*netconfig_build_entry_fn)(struct l_dbus_message_builder *builder, 423 const void *data, uint8_t family); 424 425 static void netconfig_agent_append_dict_dict_array( 426 struct l_dbus_message_builder *builder, 427 const char *key, 428 const struct l_queue_entry *value, 429 netconfig_build_entry_fn build_entry, 430 uint8_t family) 431 { 432 l_dbus_message_builder_enter_dict(builder, "sv"); 433 l_dbus_message_builder_append_basic(builder, 's', key); 434 l_dbus_message_builder_enter_variant(builder, "aa{sv}"); 435 l_dbus_message_builder_enter_array(builder, "a{sv}"); 436 437 for (; value; value = value->next) 438 build_entry(builder, value->data, family); 439 440 l_dbus_message_builder_leave_array(builder); 441 l_dbus_message_builder_leave_variant(builder); 442 l_dbus_message_builder_leave_dict(builder); 443 } 444 445 static void netconfig_agent_append_dict_strv( 446 struct l_dbus_message_builder *builder, 447 const char *key, char **value, 448 uint8_t family) 449 { 450 if (!value) 451 return; 452 453 l_dbus_message_builder_enter_dict(builder, "sv"); 454 l_dbus_message_builder_append_basic(builder, 's', key); 455 l_dbus_message_builder_enter_variant(builder, "as"); 456 l_dbus_message_builder_enter_array(builder, "s"); 457 458 for (; *value; value++) { 459 uint8_t value_family = IS_IPV6_STR_FAST((char *) *value) ? 460 AF_INET6 : AF_INET; 461 462 if (family == AF_UNSPEC || value_family == family) 463 l_dbus_message_builder_append_basic(builder, 's', 464 *value); 465 } 466 467 l_dbus_message_builder_leave_array(builder); 468 l_dbus_message_builder_leave_variant(builder); 469 l_dbus_message_builder_leave_dict(builder); 470 } 471 472 static void netconfig_agent_append_address( 473 struct l_dbus_message_builder *builder, 474 const void *data, uint8_t family) 475 { 476 const struct l_rtnl_address *addr = data; 477 char addr_str[INET6_ADDRSTRLEN]; 478 uint64_t valid_expiry_time; 479 uint64_t preferred_expiry_time; 480 uint64_t now = l_time_now(); 481 482 if (l_rtnl_address_get_family(addr) != family) 483 return; 484 485 l_dbus_message_builder_enter_array(builder, "{sv}"); 486 487 l_rtnl_address_get_address(addr, addr_str); 488 dbus_append_dict_basic(builder, "Address", 's', addr_str); 489 490 if (family == AF_INET) { 491 uint8_t plen = l_rtnl_address_get_prefix_length(addr); 492 dbus_append_dict_basic(builder, "PrefixLength", 'y', &plen); 493 494 if (l_rtnl_address_get_broadcast(addr, addr_str) && 495 strcmp(addr_str, "0.0.0.0")) 496 dbus_append_dict_basic(builder, "Broadcast", 's', 497 addr_str); 498 } 499 500 l_rtnl_address_get_expiry(addr, &preferred_expiry_time, 501 &valid_expiry_time); 502 503 if (valid_expiry_time > now) { 504 uint32_t lt = l_time_to_secs(valid_expiry_time - now); 505 506 dbus_append_dict_basic(builder, "ValidLifetime", 'u', <); 507 } 508 509 if (preferred_expiry_time > now) { 510 uint32_t lt = l_time_to_secs(preferred_expiry_time - now); 511 512 dbus_append_dict_basic(builder, "PreferredLifetime", 'u', <); 513 } 514 515 l_dbus_message_builder_leave_array(builder); 516 } 517 518 static void netconfig_agent_append_route(struct l_dbus_message_builder *builder, 519 const void *data, 520 uint8_t family) 521 { 522 const struct l_rtnl_route *rt = data; 523 char addr_str[INET6_ADDRSTRLEN]; 524 uint8_t prefix_len; 525 uint64_t expiry_time; 526 uint64_t now = l_time_now(); 527 uint32_t priority; 528 uint8_t preference; 529 uint32_t mtu; 530 531 if (l_rtnl_route_get_family(rt) != family) 532 return; 533 534 l_dbus_message_builder_enter_array(builder, "{sv}"); 535 536 if (l_rtnl_route_get_dst(rt, addr_str, &prefix_len) && prefix_len) { 537 l_dbus_message_builder_enter_dict(builder, "sv"); 538 l_dbus_message_builder_append_basic(builder, 's', 539 "Destination"); 540 l_dbus_message_builder_enter_variant(builder, "(sy)"); 541 l_dbus_message_builder_enter_struct(builder, "sy"); 542 l_dbus_message_builder_append_basic(builder, 's', addr_str); 543 l_dbus_message_builder_append_basic(builder, 'y', &prefix_len); 544 l_dbus_message_builder_leave_struct(builder); 545 l_dbus_message_builder_leave_variant(builder); 546 l_dbus_message_builder_leave_dict(builder); 547 } 548 549 if (l_rtnl_route_get_gateway(rt, addr_str)) 550 dbus_append_dict_basic(builder, "Router", 's', addr_str); 551 552 if (l_rtnl_route_get_prefsrc(rt, addr_str)) 553 dbus_append_dict_basic(builder, "PreferredSource", 's', 554 addr_str); 555 556 expiry_time = l_rtnl_route_get_expiry(rt); 557 if (expiry_time > now) { 558 uint32_t lt = l_time_to_secs(expiry_time - now); 559 560 dbus_append_dict_basic(builder, "Lifetime", 'u', <); 561 } 562 563 priority = l_rtnl_route_get_priority(rt); 564 dbus_append_dict_basic(builder, "Priority", 'u', &priority); 565 566 /* 567 * ICMPV6_ROUTER_PREF_MEDIUM is returned by default even for IPv4 568 * routes where this property doesn't make sense so filter those out. 569 */ 570 preference = l_rtnl_route_get_preference(rt); 571 if (preference != ICMPV6_ROUTER_PREF_INVALID && family == AF_INET6) 572 dbus_append_dict_basic(builder, "Preference", 'y', &preference); 573 574 mtu = l_rtnl_route_get_mtu(rt); 575 if (mtu) 576 dbus_append_dict_basic(builder, "Priority", 'u', &mtu); 577 578 l_dbus_message_builder_leave_array(builder); 579 } 580 581 static void netconfig_agent_commit(struct netconfig *netconfig, uint8_t family, 582 enum l_netconfig_event event) 583 { 584 struct netconfig_agent_data *data; 585 struct netconfig_agent_call_data *cd; 586 struct l_dbus *dbus = dbus_get_bus(); 587 struct l_dbus_message *message; 588 struct l_dbus_message_builder *builder; 589 const char *dev_path = netdev_get_path(netconfig->netdev); 590 const char *dbus_method = 591 (family == AF_INET ? "ConfigureIPv4" : "ConfigureIPv6"); 592 const char *cfg_method = 593 netconfig->static_config[INDEX_FOR_AF(family)] ? 594 "static" : "auto"; 595 _auto_(l_strv_free) char **dns_list = NULL; 596 _auto_(l_strv_free) char **domains = NULL; 597 598 if (!netconfig->commit_data) 599 netconfig->commit_data = l_new(struct netconfig_agent_data, 1); 600 601 netconfig_agent_cancel(netconfig, family, "superseded"); 602 603 l_debug("sending a %s(%s, ...) to %s %s", dbus_method, dev_path, 604 netconfig_agent_name, netconfig_agent_path); 605 606 message = l_dbus_message_new_method_call(dbus, netconfig_agent_name, 607 netconfig_agent_path, 608 IWD_NETCONFIG_AGENT_INTERFACE, 609 dbus_method); 610 611 /* 612 * Build the call arguments: the Device object path and 613 * the complicated config dict. 614 */ 615 builder = l_dbus_message_builder_new(message); 616 l_dbus_message_builder_append_basic(builder, 'o', dev_path); 617 l_dbus_message_builder_enter_array(builder, "{sv}"); 618 dbus_append_dict_basic(builder, "Method", 's', cfg_method); 619 620 netconfig_agent_append_dict_dict_array(builder, "Addresses", 621 l_netconfig_get_addresses(netconfig->nc, 622 NULL, NULL, NULL, NULL), 623 netconfig_agent_append_address, family); 624 625 netconfig_agent_append_dict_dict_array(builder, "Routes", 626 l_netconfig_get_routes(netconfig->nc, 627 NULL, NULL, NULL, NULL), 628 netconfig_agent_append_route, family); 629 630 dns_list = l_netconfig_get_dns_list(netconfig->nc); 631 netconfig_agent_append_dict_strv(builder, "DomainNameServers", 632 dns_list, family); 633 634 domains = l_netconfig_get_domain_names(netconfig->nc); 635 netconfig_agent_append_dict_strv(builder, "DomainNames", 636 domains, AF_UNSPEC); 637 638 l_dbus_message_builder_leave_array(builder); 639 l_dbus_message_builder_finalize(builder); 640 l_dbus_message_builder_destroy(builder); 641 642 cd = l_new(struct netconfig_agent_call_data, 1); 643 cd->netconfig = netconfig; 644 cd->family = family; 645 cd->event = event; 646 data = netconfig->commit_data; 647 data->pending_id[INDEX_FOR_AF(family)] = 648 l_dbus_send_with_reply(dbus, message, 649 netconfig_agent_receive_reply, 650 cd, l_free); 651 } 652 653 static void netconfig_agent_free_data(struct netconfig *netconfig, 654 const char *reasonstr) 655 { 656 if (!netconfig->commit_data) 657 return; 658 659 netconfig_agent_cancel(netconfig, AF_INET, reasonstr); 660 netconfig_agent_cancel(netconfig, AF_INET6, reasonstr); 661 l_free(l_steal_ptr(netconfig->commit_data)); 662 } 663 664 static struct netconfig_commit_ops netconfig_agent_ops = { 665 .commit = netconfig_agent_commit, 666 .free_data = netconfig_agent_free_data, 667 }; 668 669 static void netconfig_agent_disconnect_handle(void *user_data) 670 { 671 netconfig_unregister_agent(netconfig_agent_name, netconfig_agent_path); 672 } 673 674 static void netconfig_agent_disconnect_cb(struct l_dbus *dbus, void *user_data) 675 { 676 l_debug(""); 677 l_idle_oneshot(netconfig_agent_disconnect_handle, NULL, NULL); 678 } 679 680 int netconfig_register_agent(const char *name, const char *path) 681 { 682 if (netconfig_agent_path) 683 return -EEXIST; 684 685 netconfig_agent_name = l_strdup(name); 686 netconfig_agent_path = l_strdup(path); 687 netconfig_agent_watch = l_dbus_add_disconnect_watch(dbus_get_bus(), 688 name, 689 netconfig_agent_disconnect_cb, 690 NULL, NULL); 691 692 netconfig_switch_backend(&netconfig_agent_ops); 693 694 return 0; 695 } 696 697 int netconfig_unregister_agent(const char *name, const char *path) 698 { 699 if (!netconfig_agent_path || strcmp(netconfig_agent_path, path)) 700 return -ENOENT; 701 702 if (strcmp(netconfig_agent_name, name)) 703 return -EPERM; 704 705 l_free(l_steal_ptr(netconfig_agent_name)); 706 l_free(l_steal_ptr(netconfig_agent_path)); 707 l_dbus_remove_watch(dbus_get_bus(), netconfig_agent_watch); 708 709 netconfig_switch_backend(&netconfig_rtnl_ops); 710 return 0; 711 }