| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-netlink.h" |
| 4 |
|
| 5 |
#include "bus-util.h" |
| 6 |
#include "errno-util.h" |
| 7 |
#include "fd-util.h" |
| 8 |
#include "format-ifname.h" |
| 9 |
#include "log.h" |
| 10 |
#include "netlink-util.h" |
| 11 |
#include "networkctl.h" |
| 12 |
#include "networkctl-misc.h" |
| 13 |
#include "networkctl-util.h" |
| 14 |
#include "ordered-set.h" |
| 15 |
#include "parse-util.h" |
| 16 |
#include "polkit-agent.h" |
| 17 |
#include "string-util.h" |
| 18 |
#include "strv.h" |
| 19 |
#include "varlink-util.h" |
| 20 |
|
| 21 |
static int parse_interfaces(sd_netlink **rtnl, char *argv[], OrderedSet **ret) { |
| 22 |
_cleanup_(sd_netlink_unrefp) sd_netlink *our_rtnl = NULL; |
| 23 |
_cleanup_ordered_set_free_ OrderedSet *indexes = NULL; |
| 24 |
int r; |
| 25 |
|
| 26 |
assert(ret); |
| 27 |
|
| 28 |
if (!rtnl) |
| 29 |
rtnl = &our_rtnl; |
| 30 |
|
| 31 |
STRV_FOREACH(s, strv_skip(argv, 1)) { |
| 32 |
int index = rtnl_resolve_interface_or_warn(rtnl, *s); |
| 33 |
if (index < 0) |
| 34 |
return index; |
| 35 |
assert(index > 0); |
| 36 |
|
| 37 |
r = ordered_set_ensure_put(&indexes, /* ops= */ NULL, INT_TO_PTR(index)); |
| 38 |
if (r < 0) |
| 39 |
return log_oom(); |
| 40 |
} |
| 41 |
|
| 42 |
*ret = TAKE_PTR(indexes); |
| 43 |
return 0; |
| 44 |
} |
| 45 |
|
| 46 |
int verb_link_delete(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 47 |
int r, ret = 0; |
| 48 |
|
| 49 |
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
| 50 |
_cleanup_ordered_set_free_ OrderedSet *indexes = NULL; |
| 51 |
r = parse_interfaces(&rtnl, argv, &indexes); |
| 52 |
if (r < 0) |
| 53 |
return r; |
| 54 |
|
| 55 |
void *p; |
| 56 |
ORDERED_SET_FOREACH(p, indexes) { |
| 57 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
| 58 |
int index = PTR_TO_INT(p); |
| 59 |
|
| 60 |
r = sd_rtnl_message_new_link(rtnl, &req, RTM_DELLINK, index); |
| 61 |
if (r < 0) |
| 62 |
return rtnl_log_create_error(r); |
| 63 |
|
| 64 |
r = sd_netlink_call(rtnl, req, /* timeout= */ 0, /* ret= */ NULL); |
| 65 |
if (r < 0) { |
| 66 |
RET_GATHER(ret, r); |
| 67 |
log_error_errno(r, "Failed to delete interface %s: %m", |
| 68 |
FORMAT_IFNAME_FULL(index, FORMAT_IFNAME_IFINDEX)); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
return ret; |
| 73 |
} |
| 74 |
|
| 75 |
int verb_link_varlink_simple_method(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 76 |
int r, ret = 0; |
| 77 |
|
| 78 |
typedef struct LinkVarlinkAction { |
| 79 |
const char *verb; |
| 80 |
const char *method; |
| 81 |
} LinkVarlinkAction; |
| 82 |
|
| 83 |
static const LinkVarlinkAction link_varlink_action_table[] = { |
| 84 |
{ "up", "io.systemd.Network.Link.Up", }, |
| 85 |
{ "down", "io.systemd.Network.Link.Down", }, |
| 86 |
{ "renew", "io.systemd.Network.Link.Renew", }, |
| 87 |
{ "forcerenew", "io.systemd.Network.Link.ForceRenew", }, |
| 88 |
{ "reconfigure", "io.systemd.Network.Link.Reconfigure", }, |
| 89 |
}; |
| 90 |
|
| 91 |
/* Common implementation for 'simple' method calls that just take an ifindex, and nothing else. */ |
| 92 |
|
| 93 |
const LinkVarlinkAction *a = NULL; |
| 94 |
FOREACH_ELEMENT(i, link_varlink_action_table) |
| 95 |
if (streq(argv[0], i->verb)) { |
| 96 |
a = i; |
| 97 |
break; |
| 98 |
} |
| 99 |
assert(a); |
| 100 |
|
| 101 |
_cleanup_ordered_set_free_ OrderedSet *indexes = NULL; |
| 102 |
r = parse_interfaces(/* rtnl= */ NULL, argv, &indexes); |
| 103 |
if (r < 0) |
| 104 |
return r; |
| 105 |
|
| 106 |
_cleanup_(sd_varlink_flush_close_unrefp) sd_varlink *vl = NULL; |
| 107 |
r = varlink_connect_networkd(&vl); |
| 108 |
if (r < 0) |
| 109 |
return r; |
| 110 |
|
| 111 |
(void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password); |
| 112 |
|
| 113 |
void *p; |
| 114 |
ORDERED_SET_FOREACH(p, indexes) |
| 115 |
RET_GATHER(ret, varlink_callbo_and_log( |
| 116 |
vl, |
| 117 |
a->method, |
| 118 |
/* reply= */ NULL, |
| 119 |
SD_JSON_BUILD_PAIR_INTEGER("InterfaceIndex", PTR_TO_INT(p)), |
| 120 |
SD_JSON_BUILD_PAIR_BOOLEAN("allowInteractiveAuthentication", arg_ask_password))); |
| 121 |
|
| 122 |
return ret; |
| 123 |
} |
| 124 |
|
| 125 |
int verb_reload(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 126 |
return reload_networkd(); |
| 127 |
} |
| 128 |
|
| 129 |
int verb_persistent_storage(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 130 |
_cleanup_(sd_varlink_flush_close_unrefp) sd_varlink *vl = NULL; |
| 131 |
bool ready; |
| 132 |
int r; |
| 133 |
|
| 134 |
r = parse_boolean(argv[1]); |
| 135 |
if (r < 0) |
| 136 |
return log_error_errno(r, "Failed to parse argument: %s", argv[1]); |
| 137 |
ready = r; |
| 138 |
|
| 139 |
r = varlink_connect_networkd(&vl); |
| 140 |
if (r < 0) |
| 141 |
return r; |
| 142 |
|
| 143 |
if (ready) { |
| 144 |
_cleanup_close_ int fd = -EBADF; |
| 145 |
|
| 146 |
fd = open("/var/lib/systemd/network/", O_CLOEXEC | O_DIRECTORY); |
| 147 |
if (fd < 0) |
| 148 |
return log_error_errno(errno, "Failed to open %s: %m", "/var/lib/systemd/network/"); |
| 149 |
|
| 150 |
r = sd_varlink_push_fd(vl, fd); |
| 151 |
if (r < 0) |
| 152 |
return log_error_errno(r, "Failed to push file descriptor of /var/lib/systemd/network/ into varlink: %m"); |
| 153 |
|
| 154 |
TAKE_FD(fd); |
| 155 |
} |
| 156 |
|
| 157 |
return varlink_callbo_and_log( |
| 158 |
vl, |
| 159 |
"io.systemd.Network.SetPersistentStorage", |
| 160 |
/* reply= */ NULL, |
| 161 |
SD_JSON_BUILD_PAIR_BOOLEAN("Ready", ready)); |
| 162 |
} |