| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <linux/rtnetlink.h> |
| 4 |
|
| 5 |
#include "sd-event.h" |
| 6 |
#include "sd-id128.h" |
| 7 |
|
| 8 |
#include "conf-parser.h" |
| 9 |
#include "dhcp-relay-internal.h" |
| 10 |
#include "hashmap.h" |
| 11 |
#include "iovec-util.h" |
| 12 |
#include "networkd-address.h" |
| 13 |
#include "networkd-dhcp-relay.h" |
| 14 |
#include "networkd-link.h" |
| 15 |
#include "networkd-manager.h" |
| 16 |
#include "networkd-network.h" |
| 17 |
#include "networkd-queue.h" |
| 18 |
#include "string-table.h" |
| 19 |
#include "string-util.h" /* IWYU pragma: keep */ |
| 20 |
|
| 21 |
#define DHCP_RELAY_APP_REMOTE_ID SD_ID128_MAKE(85,bb,eb,d2,b8,56,47,0b,b0,86,4c,f3,d3,9b,c1,b5) |
| 22 |
|
| 23 |
void network_adjust_dhcp_relay(Network *network) { |
| 24 |
assert(network); |
| 25 |
assert(network->manager); |
| 26 |
|
| 27 |
if (network->dhcp_relay_interface_mode < 0) |
| 28 |
return; |
| 29 |
|
| 30 |
if (network->bond) { |
| 31 |
log_warning("%s: DHCPRelay= is enabled for bond slave. Disabling DHCP relay agent.", |
| 32 |
network->filename); |
| 33 |
network->dhcp_relay_interface_mode = _DHCP_RELAY_INTERFACE_INVALID; |
| 34 |
return; |
| 35 |
} |
| 36 |
|
| 37 |
if (network->dhcp_server) { |
| 38 |
log_warning("%s: DHCPRelay= cannot be enabled when DHCPServer= is enabled. Disabling DHCP relay agent.", |
| 39 |
network->filename); |
| 40 |
network->dhcp_relay_interface_mode = _DHCP_RELAY_INTERFACE_INVALID; |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
if (FLAGS_SET(network->dhcp, ADDRESS_FAMILY_IPV4)) { |
| 45 |
log_warning("%s: DHCPRelay= cannot be enabled when DHCPv4 client is enabled. Disabling DHCP relay agent.", |
| 46 |
network->filename); |
| 47 |
network->dhcp_relay_interface_mode = _DHCP_RELAY_INTERFACE_INVALID; |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
if (in4_addr_is_null(&network->manager->dhcp_relay_server_address)) { |
| 52 |
log_warning("%s: DHCPRelay= is enabled, but [DHCPRelay] ServerAddress= in networkd.conf is not configured. Disabling DHCP relay agent.", |
| 53 |
network->filename); |
| 54 |
network->dhcp_relay_interface_mode = _DHCP_RELAY_INTERFACE_INVALID; |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
Address *a; |
| 59 |
ORDERED_HASHMAP_FOREACH(a, network->addresses_by_section) { |
| 60 |
assert(!section_is_invalid(a->section)); |
| 61 |
|
| 62 |
if (a->family != AF_INET) |
| 63 |
continue; |
| 64 |
|
| 65 |
if (in4_addr_is_set(&a->in_addr_peer.in)) |
| 66 |
continue; |
| 67 |
|
| 68 |
if (in4_addr_is_set(&network->dhcp_relay_agent_address_in_addr)) { |
| 69 |
if (!in4_addr_equal(&a->in_addr.in, &network->dhcp_relay_agent_address_in_addr)) |
| 70 |
continue; |
| 71 |
|
| 72 |
} else { |
| 73 |
if (in4_addr_is_localhost(&a->in_addr.in)) |
| 74 |
continue; |
| 75 |
|
| 76 |
if (in4_addr_is_link_local(&a->in_addr.in)) |
| 77 |
continue; |
| 78 |
|
| 79 |
if (a->scope != RT_SCOPE_UNIVERSE) |
| 80 |
continue; |
| 81 |
} |
| 82 |
|
| 83 |
network->dhcp_relay_agent_address = a; |
| 84 |
break; |
| 85 |
} |
| 86 |
|
| 87 |
if (!network->dhcp_relay_agent_address) { |
| 88 |
if (in4_addr_is_set(&network->dhcp_relay_agent_address_in_addr)) |
| 89 |
log_warning("%s: Configured AgentAddress=%s not found among static addresses. Disabling DHCP relay agent.", |
| 90 |
network->filename, IN4_ADDR_TO_STRING(&network->dhcp_relay_agent_address_in_addr)); |
| 91 |
else |
| 92 |
log_warning("%s: DHCPRelay= is enabled, but no suitable static address configured. Disabling DHCP relay agent.", |
| 93 |
network->filename); |
| 94 |
network->dhcp_relay_interface_mode = _DHCP_RELAY_INTERFACE_INVALID; |
| 95 |
return; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
static int manager_configure_dhcp_relay(Manager *manager) { |
| 100 |
int r; |
| 101 |
|
| 102 |
assert(manager); |
| 103 |
assert(manager->event); |
| 104 |
|
| 105 |
if (manager->dhcp_relay) |
| 106 |
return 0; |
| 107 |
|
| 108 |
if (in4_addr_is_null(&manager->dhcp_relay_server_address)) |
| 109 |
return -EADDRNOTAVAIL; |
| 110 |
|
| 111 |
_cleanup_(sd_dhcp_relay_unrefp) sd_dhcp_relay *relay = NULL; |
| 112 |
r = sd_dhcp_relay_new(&relay); |
| 113 |
if (r < 0) |
| 114 |
return r; |
| 115 |
|
| 116 |
r = sd_dhcp_relay_attach_event(relay, manager->event, SD_EVENT_PRIORITY_NORMAL); |
| 117 |
if (r < 0) |
| 118 |
return r; |
| 119 |
|
| 120 |
r = sd_dhcp_relay_set_server_address(relay, &manager->dhcp_relay_server_address); |
| 121 |
if (r < 0) |
| 122 |
return r; |
| 123 |
|
| 124 |
if (iovec_is_set(&manager->dhcp_relay_remote_id)) { |
| 125 |
r = sd_dhcp_relay_set_remote_id(relay, &manager->dhcp_relay_remote_id); |
| 126 |
if (r < 0) |
| 127 |
return r; |
| 128 |
} else { |
| 129 |
sd_id128_t id; |
| 130 |
r = sd_id128_get_machine_app_specific(DHCP_RELAY_APP_REMOTE_ID, &id); |
| 131 |
if (r < 0) |
| 132 |
return r; |
| 133 |
|
| 134 |
r = sd_dhcp_relay_set_remote_id(relay, &IOVEC_MAKE_STRING(SD_ID128_TO_STRING(id))); |
| 135 |
if (r < 0) |
| 136 |
return r; |
| 137 |
} |
| 138 |
|
| 139 |
r = sd_dhcp_relay_set_server_identifier_override(relay, manager->dhcp_relay_override_server_id); |
| 140 |
if (r < 0) |
| 141 |
return r; |
| 142 |
|
| 143 |
r = dhcp_relay_set_extra_options(relay, &manager->dhcp_relay_extra_options); |
| 144 |
if (r < 0) |
| 145 |
return r; |
| 146 |
|
| 147 |
manager->dhcp_relay = TAKE_PTR(relay); |
| 148 |
return 0; |
| 149 |
} |
| 150 |
|
| 151 |
static int link_configure_dhcp_relay(Link *link) { |
| 152 |
int r; |
| 153 |
|
| 154 |
assert(link); |
| 155 |
assert(link->manager); |
| 156 |
assert(link->network); |
| 157 |
assert(!link->dhcp_relay_interface); |
| 158 |
assert(link->network->dhcp_relay_agent_address); |
| 159 |
assert(link->network->dhcp_relay_interface_mode >= 0 && link->network->dhcp_relay_interface_mode < _DHCP_RELAY_INTERFACE_MAX); |
| 160 |
|
| 161 |
r = manager_configure_dhcp_relay(link->manager); |
| 162 |
if (r < 0) |
| 163 |
return r; |
| 164 |
|
| 165 |
bool upstream = link->network->dhcp_relay_interface_mode == DHCP_RELAY_INTERFACE_UPSTREAM; |
| 166 |
|
| 167 |
_cleanup_(sd_dhcp_relay_interface_unrefp) sd_dhcp_relay_interface *interface = NULL; |
| 168 |
r = sd_dhcp_relay_add_interface(link->manager->dhcp_relay, link->ifindex, upstream, &interface); |
| 169 |
if (r < 0) |
| 170 |
return r; |
| 171 |
|
| 172 |
r = sd_dhcp_relay_interface_set_ifname(interface, link->ifname); |
| 173 |
if (r < 0) |
| 174 |
return r; |
| 175 |
|
| 176 |
r = sd_dhcp_relay_interface_set_address( |
| 177 |
interface, |
| 178 |
&link->network->dhcp_relay_agent_address->in_addr.in, |
| 179 |
link->network->dhcp_relay_agent_address->prefixlen); |
| 180 |
if (r < 0) |
| 181 |
return r; |
| 182 |
|
| 183 |
if (upstream) { |
| 184 |
r = sd_dhcp_relay_upstream_set_priority(interface, link->network->dhcp_relay_interface_priority); |
| 185 |
if (r < 0) |
| 186 |
return r; |
| 187 |
} else { |
| 188 |
if (in4_addr_is_set(&link->network->dhcp_relay_gateway_address)) |
| 189 |
r = sd_dhcp_relay_downstream_set_gateway_address(interface, &link->network->dhcp_relay_gateway_address); |
| 190 |
else |
| 191 |
r = sd_dhcp_relay_downstream_set_gateway_address(interface, &link->network->dhcp_relay_agent_address->in_addr.in); |
| 192 |
if (r < 0) |
| 193 |
return r; |
| 194 |
|
| 195 |
if (iovec_is_set(&link->network->dhcp_relay_circuit_id)) |
| 196 |
r = sd_dhcp_relay_downstream_set_circuit_id(interface, &link->network->dhcp_relay_circuit_id); |
| 197 |
else |
| 198 |
r = sd_dhcp_relay_downstream_set_circuit_id(interface, &IOVEC_MAKE_STRING(link->ifname)); |
| 199 |
if (r < 0) |
| 200 |
return r; |
| 201 |
|
| 202 |
r = sd_dhcp_relay_downstream_set_virtual_subnet_selection(interface, &link->network->dhcp_relay_vss); |
| 203 |
if (r < 0) |
| 204 |
return r; |
| 205 |
|
| 206 |
r = downstream_set_extra_options(interface, &link->network->dhcp_relay_extra_options); |
| 207 |
if (r < 0) |
| 208 |
return r; |
| 209 |
} |
| 210 |
|
| 211 |
link->dhcp_relay_interface = TAKE_PTR(interface); |
| 212 |
|
| 213 |
if (link->network->dhcp_relay_interface_mode == DHCP_RELAY_INTERFACE_COMPAT && |
| 214 |
!link->dhcp_relay_interface_compat) { |
| 215 |
r = sd_dhcp_relay_add_interface( |
| 216 |
link->manager->dhcp_relay, |
| 217 |
DHCP_RELAY_IFINDEX_UNBOUND, |
| 218 |
/* is_upstream= */ true, |
| 219 |
&link->dhcp_relay_interface_compat); |
| 220 |
if (r < 0) |
| 221 |
return r; |
| 222 |
} |
| 223 |
|
| 224 |
return 0; |
| 225 |
} |
| 226 |
|
| 227 |
static bool dhcp_relay_is_ready_to_configure(Link *link) { |
| 228 |
assert(link); |
| 229 |
assert(link->network); |
| 230 |
|
| 231 |
if (!link_is_ready_to_configure(link, /* allow_unmanaged= */ false)) |
| 232 |
return false; |
| 233 |
|
| 234 |
if (!link_has_carrier(link)) |
| 235 |
return false; |
| 236 |
|
| 237 |
if (!link->static_addresses_configured) |
| 238 |
return false; |
| 239 |
|
| 240 |
Address *a; |
| 241 |
if (address_get(link, link->network->dhcp_relay_agent_address, &a) < 0) |
| 242 |
return false; |
| 243 |
|
| 244 |
if (!address_is_ready(a)) |
| 245 |
return false; |
| 246 |
|
| 247 |
return true; |
| 248 |
} |
| 249 |
|
| 250 |
static int dhcp_relay_process_request(Request *req, Link *link, void *userdata) { |
| 251 |
int r; |
| 252 |
|
| 253 |
assert(link); |
| 254 |
|
| 255 |
if (!dhcp_relay_is_ready_to_configure(link)) |
| 256 |
return 0; |
| 257 |
|
| 258 |
r = link_configure_dhcp_relay(link); |
| 259 |
if (r < 0) |
| 260 |
return log_link_warning_errno(link, r, "Failed to configure DHCP relay agent: %m"); |
| 261 |
|
| 262 |
log_link_debug(link, "DHCP relay agent is configured."); |
| 263 |
|
| 264 |
r = link_start_dhcp_relay(link); |
| 265 |
if (r < 0) |
| 266 |
return log_link_warning_errno(link, r, "Failed to start DHCP relay agent: %m"); |
| 267 |
|
| 268 |
return 1; |
| 269 |
} |
| 270 |
|
| 271 |
int link_request_dhcp_relay(Link *link) { |
| 272 |
int r; |
| 273 |
|
| 274 |
assert(link); |
| 275 |
assert(link->manager); |
| 276 |
|
| 277 |
if (link->manager->state != MANAGER_RUNNING) |
| 278 |
return 0; |
| 279 |
|
| 280 |
if (!link->network) |
| 281 |
return 0; |
| 282 |
|
| 283 |
if (link->network->dhcp_relay_interface_mode < 0) |
| 284 |
return 0; |
| 285 |
|
| 286 |
if (link->dhcp_relay_interface) |
| 287 |
return 0; |
| 288 |
|
| 289 |
r = link_queue_request(link, REQUEST_TYPE_DHCP_RELAY, dhcp_relay_process_request, NULL); |
| 290 |
if (r < 0) |
| 291 |
return log_link_warning_errno(link, r, "Failed to request configuring of the DHCP relay agent: %m"); |
| 292 |
|
| 293 |
log_link_debug(link, "Requested configuring of the DHCP relay agent."); |
| 294 |
return 0; |
| 295 |
} |
| 296 |
|
| 297 |
int link_start_dhcp_relay(Link *link) { |
| 298 |
int r; |
| 299 |
|
| 300 |
assert(link); |
| 301 |
assert(link->manager); |
| 302 |
|
| 303 |
if (!link->dhcp_relay_interface) |
| 304 |
return 0; /* Not configured yet. */ |
| 305 |
|
| 306 |
if (!link_has_carrier(link)) |
| 307 |
return 0; |
| 308 |
|
| 309 |
r = sd_dhcp_relay_interface_start(link->dhcp_relay_interface); |
| 310 |
if (r < 0) |
| 311 |
return r; |
| 312 |
|
| 313 |
if (link->dhcp_relay_interface_compat) { |
| 314 |
r = sd_dhcp_relay_interface_start(link->dhcp_relay_interface_compat); |
| 315 |
if (r < 0) |
| 316 |
return r; |
| 317 |
} |
| 318 |
|
| 319 |
log_link_debug(link, "Relaying DHCPv4 messages."); |
| 320 |
return 0; |
| 321 |
} |
| 322 |
|
| 323 |
int link_dhcp_relay_address_dropped(Link *link, const Address *address) { |
| 324 |
int r; |
| 325 |
|
| 326 |
assert(link); |
| 327 |
assert(link->manager); |
| 328 |
assert(address); |
| 329 |
|
| 330 |
/* This is called when an address is removed from the interface. */ |
| 331 |
|
| 332 |
if (link->manager->state != MANAGER_RUNNING) |
| 333 |
return 0; |
| 334 |
|
| 335 |
if (!link->network) |
| 336 |
return 0; |
| 337 |
|
| 338 |
if (!link->dhcp_relay_interface) |
| 339 |
return 0; |
| 340 |
|
| 341 |
if (address->family != AF_INET) |
| 342 |
return 0; |
| 343 |
|
| 344 |
struct in_addr a; |
| 345 |
uint8_t prefixlen; |
| 346 |
r = sd_dhcp_relay_interface_get_address(link->dhcp_relay_interface, &a, &prefixlen); |
| 347 |
if (r <= 0) |
| 348 |
return r; |
| 349 |
|
| 350 |
if (!in4_addr_equal(&address->in_addr.in, &a)) |
| 351 |
return 0; |
| 352 |
|
| 353 |
if (address->prefixlen != prefixlen) |
| 354 |
return 0; |
| 355 |
|
| 356 |
r = sd_dhcp_relay_interface_stop(link->dhcp_relay_interface); |
| 357 |
if (r < 0) |
| 358 |
return r; |
| 359 |
|
| 360 |
link->dhcp_relay_interface = sd_dhcp_relay_interface_unref(link->dhcp_relay_interface); |
| 361 |
|
| 362 |
if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) |
| 363 |
return 0; |
| 364 |
|
| 365 |
/* The address may be reconfigured later. Let's reconfigure DHCP relay interface when the address comes back. */ |
| 366 |
return link_request_dhcp_relay(link); |
| 367 |
} |
| 368 |
|
| 369 |
static const char * const dhcp_relay_interface_mode_table[_DHCP_RELAY_INTERFACE_MAX] = { |
| 370 |
[DHCP_RELAY_INTERFACE_UPSTREAM] = "upstream", |
| 371 |
[DHCP_RELAY_INTERFACE_DOWNSTREAM] = "downstream", |
| 372 |
}; |
| 373 |
|
| 374 |
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(dhcp_relay_interface_mode, DHCPRelayInterfaceMode); |
| 375 |
|
| 376 |
DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT( |
| 377 |
config_parse_dhcp_relay_interface_mode, |
| 378 |
dhcp_relay_interface_mode, |
| 379 |
DHCPRelayInterfaceMode, |
| 380 |
_DHCP_RELAY_INTERFACE_INVALID); |