| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "af-list.h" |
| 4 |
#include "alloc-util.h" |
| 5 |
#include "daemon-util.h" |
| 6 |
#include "errno-util.h" |
| 7 |
#include "fd-util.h" |
| 8 |
#include "hashmap.h" |
| 9 |
#include "iovec-util.h" |
| 10 |
#include "json-util.h" |
| 11 |
#include "memfd-util.h" |
| 12 |
#include "networkd-address.h" |
| 13 |
#include "networkd-json.h" |
| 14 |
#include "networkd-link.h" |
| 15 |
#include "networkd-manager.h" |
| 16 |
#include "networkd-nexthop.h" |
| 17 |
#include "networkd-route.h" |
| 18 |
#include "networkd-serialize.h" |
| 19 |
#include "string-util.h" |
| 20 |
|
| 21 |
int manager_serialize(Manager *manager) { |
| 22 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL, *array = NULL; |
| 23 |
int r; |
| 24 |
|
| 25 |
assert(manager); |
| 26 |
|
| 27 |
log_debug("Serializing..."); |
| 28 |
|
| 29 |
Link *link; |
| 30 |
HASHMAP_FOREACH(link, manager->links_by_index) { |
| 31 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *e = NULL; |
| 32 |
|
| 33 |
/* ignore unmanaged, failed, or removed interfaces. */ |
| 34 |
if (!IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_INITIALIZED, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) |
| 35 |
continue; |
| 36 |
|
| 37 |
r = sd_json_buildo( |
| 38 |
&e, |
| 39 |
SD_JSON_BUILD_PAIR_INTEGER("Index", link->ifindex)); |
| 40 |
if (r < 0) |
| 41 |
return r; |
| 42 |
|
| 43 |
r = addresses_append_json(link, /* serializing= */ true, &e); |
| 44 |
if (r < 0) |
| 45 |
return r; |
| 46 |
|
| 47 |
r = sd_json_variant_append_array(&array, e); |
| 48 |
if (r < 0) |
| 49 |
return r; |
| 50 |
} |
| 51 |
|
| 52 |
r = json_variant_set_field_non_null(&v, "Interfaces", array); |
| 53 |
if (r < 0) |
| 54 |
return r; |
| 55 |
|
| 56 |
r = nexthops_append_json(manager, /* ifindex= */ -1, &v); |
| 57 |
if (r < 0) |
| 58 |
return r; |
| 59 |
|
| 60 |
r = routes_append_json(manager, /* ifindex= */ -1, &v); |
| 61 |
if (r < 0) |
| 62 |
return r; |
| 63 |
|
| 64 |
if (!v) { |
| 65 |
log_debug("There is nothing to serialize."); |
| 66 |
(void) notify_remove_fd_warn("manager-serialization"); |
| 67 |
return 0; |
| 68 |
} |
| 69 |
|
| 70 |
_cleanup_free_ char *dump = NULL; |
| 71 |
r = sd_json_variant_format(v, /* flags= */ 0, &dump); |
| 72 |
if (r < 0) |
| 73 |
return r; |
| 74 |
|
| 75 |
_cleanup_close_ int fd = -EBADF; |
| 76 |
fd = memfd_new_and_seal_string("serialization", dump); |
| 77 |
if (fd < 0) |
| 78 |
return fd; |
| 79 |
|
| 80 |
r = notify_push_fd(fd, "manager-serialization"); |
| 81 |
if (r < 0) |
| 82 |
return log_debug_errno(r, "Failed to push serialization file descriptor: %m"); |
| 83 |
|
| 84 |
log_debug("Serialization completed."); |
| 85 |
return 0; |
| 86 |
} |
| 87 |
|
| 88 |
int manager_set_serialization_fd(Manager *manager, int fd, const char *name) { |
| 89 |
assert(manager); |
| 90 |
assert(fd >= 0); |
| 91 |
assert(name); |
| 92 |
|
| 93 |
if (!startswith(name, "manager-serialization")) |
| 94 |
return -EINVAL; |
| 95 |
|
| 96 |
if (manager->serialization_fd >= 0) |
| 97 |
return -EEXIST; |
| 98 |
|
| 99 |
manager->serialization_fd = fd; |
| 100 |
return 0; |
| 101 |
} |
| 102 |
|
| 103 |
static JSON_DISPATCH_ENUM_DEFINE(json_dispatch_network_config_source, NetworkConfigSource, network_config_source_from_string); |
| 104 |
|
| 105 |
static int json_dispatch_address_family(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { |
| 106 |
int r, *i = ASSERT_PTR(userdata); |
| 107 |
int64_t i64; |
| 108 |
|
| 109 |
assert_return(variant, -EINVAL); |
| 110 |
|
| 111 |
if (FLAGS_SET(flags, SD_JSON_RELAX) && sd_json_variant_is_null(variant)) { |
| 112 |
*i = AF_UNSPEC; |
| 113 |
return 0; |
| 114 |
} |
| 115 |
|
| 116 |
r = sd_json_dispatch_int64(name, variant, flags, &i64); |
| 117 |
if (r < 0) |
| 118 |
return r; |
| 119 |
|
| 120 |
if (!IN_SET(i64, AF_INET, AF_INET6) && !(FLAGS_SET(flags, SD_JSON_RELAX) && i64 == AF_UNSPEC)) |
| 121 |
return json_log(variant, flags, SYNTHETIC_ERRNO(ERANGE), "JSON field '%s' out of bounds for an address family.", strna(name)); |
| 122 |
|
| 123 |
*i = (int) i64; |
| 124 |
return 0; |
| 125 |
} |
| 126 |
|
| 127 |
typedef struct AddressParam { |
| 128 |
int family; |
| 129 |
struct iovec address; |
| 130 |
struct iovec peer; |
| 131 |
uint8_t prefixlen; |
| 132 |
NetworkConfigSource source; |
| 133 |
struct iovec provider; |
| 134 |
} AddressParam; |
| 135 |
|
| 136 |
static void address_param_done(AddressParam *p) { |
| 137 |
assert(p); |
| 138 |
|
| 139 |
iovec_done(&p->address); |
| 140 |
iovec_done(&p->peer); |
| 141 |
iovec_done(&p->provider); |
| 142 |
} |
| 143 |
|
| 144 |
static int link_deserialize_address(Link *link, sd_json_variant *v) { |
| 145 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 146 |
{ "Family", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_address_family, offsetof(AddressParam, family), SD_JSON_MANDATORY }, |
| 147 |
{ "Address", SD_JSON_VARIANT_ARRAY, json_dispatch_byte_array_iovec, offsetof(AddressParam, address), SD_JSON_MANDATORY }, |
| 148 |
{ "Peer", SD_JSON_VARIANT_ARRAY, json_dispatch_byte_array_iovec, offsetof(AddressParam, peer), 0 }, |
| 149 |
{ "PrefixLength", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint8, offsetof(AddressParam, prefixlen), SD_JSON_MANDATORY }, |
| 150 |
{ "ConfigSource", SD_JSON_VARIANT_STRING, json_dispatch_network_config_source, offsetof(AddressParam, source), SD_JSON_MANDATORY }, |
| 151 |
{ "ConfigProvider", SD_JSON_VARIANT_ARRAY, json_dispatch_byte_array_iovec, offsetof(AddressParam, provider), 0 }, |
| 152 |
{}, |
| 153 |
}; |
| 154 |
|
| 155 |
int r; |
| 156 |
|
| 157 |
assert(link); |
| 158 |
assert(v); |
| 159 |
|
| 160 |
_cleanup_(address_param_done) AddressParam p = {}; |
| 161 |
r = sd_json_dispatch(v, dispatch_table, SD_JSON_ALLOW_EXTENSIONS, &p); |
| 162 |
if (r < 0) |
| 163 |
return log_link_debug_errno(link, r, "Failed to dispatch address from json variant: %m"); |
| 164 |
|
| 165 |
if (p.address.iov_len != FAMILY_ADDRESS_SIZE(p.family)) |
| 166 |
return log_link_debug_errno(link, SYNTHETIC_ERRNO(EINVAL), |
| 167 |
"Dispatched address size (%zu) is incompatible with the family (%s).", |
| 168 |
p.address.iov_len, af_to_ipv4_ipv6(p.family)); |
| 169 |
|
| 170 |
if (p.peer.iov_len != 0 && p.peer.iov_len != FAMILY_ADDRESS_SIZE(p.family)) |
| 171 |
return log_link_debug_errno(link, SYNTHETIC_ERRNO(EINVAL), |
| 172 |
"Dispatched peer address size (%zu) is incompatible with the family (%s).", |
| 173 |
p.peer.iov_len, af_to_ipv4_ipv6(p.family)); |
| 174 |
|
| 175 |
if (p.provider.iov_len != 0 && p.provider.iov_len != FAMILY_ADDRESS_SIZE(p.family)) |
| 176 |
return log_link_debug_errno(link, SYNTHETIC_ERRNO(EINVAL), |
| 177 |
"Dispatched provider address size (%zu) is incompatible with the family (%s).", |
| 178 |
p.provider.iov_len, af_to_ipv4_ipv6(p.family)); |
| 179 |
|
| 180 |
Address tmp = { |
| 181 |
.family = p.family, |
| 182 |
.prefixlen = p.prefixlen, |
| 183 |
}; |
| 184 |
|
| 185 |
memcpy_safe(&tmp.in_addr, p.address.iov_base, p.address.iov_len); |
| 186 |
memcpy_safe(&tmp.in_addr_peer, p.peer.iov_base, p.peer.iov_len); |
| 187 |
|
| 188 |
Address *address; |
| 189 |
r = address_get(link, &tmp, &address); |
| 190 |
if (r < 0) { |
| 191 |
log_link_debug_errno(link, r, "Cannot find deserialized address %s: %m", |
| 192 |
IN_ADDR_PREFIX_TO_STRING(tmp.family, &tmp.in_addr, tmp.prefixlen)); |
| 193 |
return 0; /* Already removed? */ |
| 194 |
} |
| 195 |
|
| 196 |
if (address->source != NETWORK_CONFIG_SOURCE_FOREIGN) |
| 197 |
return 0; /* Huh?? Already deserialized?? */ |
| 198 |
|
| 199 |
address->source = p.source; |
| 200 |
memcpy_safe(&address->provider, p.provider.iov_base, p.provider.iov_len); |
| 201 |
|
| 202 |
log_address_debug(address, "Deserialized", link); |
| 203 |
return 0; |
| 204 |
} |
| 205 |
|
| 206 |
static int manager_deserialize_link(Manager *manager, sd_json_variant *v) { |
| 207 |
typedef struct LinkParam { |
| 208 |
int ifindex; |
| 209 |
sd_json_variant *addresses; |
| 210 |
} LinkParam; |
| 211 |
|
| 212 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 213 |
{ "Index", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_ifindex, offsetof(LinkParam, ifindex), SD_JSON_MANDATORY | SD_JSON_REFUSE_NULL }, |
| 214 |
{ "Addresses", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_variant_noref, offsetof(LinkParam, addresses), 0 }, |
| 215 |
{}, |
| 216 |
}; |
| 217 |
|
| 218 |
int r, ret = 0; |
| 219 |
|
| 220 |
assert(manager); |
| 221 |
assert(v); |
| 222 |
|
| 223 |
LinkParam p = {}; |
| 224 |
r = sd_json_dispatch(v, dispatch_table, SD_JSON_ALLOW_EXTENSIONS, &p); |
| 225 |
if (r < 0) |
| 226 |
return log_debug_errno(r, "Failed to dispatch interface from json variant: %m"); |
| 227 |
|
| 228 |
Link *link; |
| 229 |
r = link_get_by_index(manager, p.ifindex, &link); |
| 230 |
if (r < 0) { |
| 231 |
log_debug_errno(r, "No interface with deserialized ifindex (%i) found: %m", p.ifindex); |
| 232 |
return 0; /* Already removed? */ |
| 233 |
} |
| 234 |
|
| 235 |
sd_json_variant *i; |
| 236 |
JSON_VARIANT_ARRAY_FOREACH(i, p.addresses) |
| 237 |
RET_GATHER(ret, link_deserialize_address(link, i)); |
| 238 |
|
| 239 |
return ret; |
| 240 |
} |
| 241 |
|
| 242 |
typedef struct NextHopParam { |
| 243 |
uint32_t id; |
| 244 |
int family; |
| 245 |
NetworkConfigSource source; |
| 246 |
struct iovec provider; |
| 247 |
} NextHopParam; |
| 248 |
|
| 249 |
static void nexthop_param_done(NextHopParam *p) { |
| 250 |
assert(p); |
| 251 |
|
| 252 |
iovec_done(&p->provider); |
| 253 |
} |
| 254 |
|
| 255 |
static int manager_deserialize_nexthop(Manager *manager, sd_json_variant *v) { |
| 256 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 257 |
{ "ID", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint32, offsetof(NextHopParam, id), SD_JSON_MANDATORY }, |
| 258 |
{ "Family", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_address_family, offsetof(NextHopParam, family), SD_JSON_MANDATORY }, |
| 259 |
{ "ConfigSource", SD_JSON_VARIANT_STRING, json_dispatch_network_config_source, offsetof(NextHopParam, source), SD_JSON_MANDATORY }, |
| 260 |
{ "ConfigProvider", SD_JSON_VARIANT_ARRAY, json_dispatch_byte_array_iovec, offsetof(NextHopParam, provider), 0 }, |
| 261 |
{}, |
| 262 |
}; |
| 263 |
|
| 264 |
int r; |
| 265 |
|
| 266 |
assert(manager); |
| 267 |
assert(v); |
| 268 |
|
| 269 |
_cleanup_(nexthop_param_done) NextHopParam p = {}; |
| 270 |
r = sd_json_dispatch(v, dispatch_table, SD_JSON_ALLOW_EXTENSIONS, &p); |
| 271 |
if (r < 0) |
| 272 |
return log_debug_errno(r, "Failed to dispatch nexthop from json variant: %m"); |
| 273 |
|
| 274 |
if (p.id == 0) |
| 275 |
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Dispatched nexthop ID is zero."); |
| 276 |
|
| 277 |
if (p.provider.iov_len != 0 && p.provider.iov_len != FAMILY_ADDRESS_SIZE(p.family)) |
| 278 |
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), |
| 279 |
"Dispatched provider address size (%zu) is incompatible with the family (%s).", |
| 280 |
p.provider.iov_len, af_to_ipv4_ipv6(p.family)); |
| 281 |
|
| 282 |
NextHop *nexthop; |
| 283 |
r = nexthop_get_by_id(manager, p.id, &nexthop); |
| 284 |
if (r < 0) { |
| 285 |
log_debug_errno(r, "Cannot find deserialized nexthop (ID=%"PRIu32"): %m", p.id); |
| 286 |
return 0; /* Already removed? */ |
| 287 |
} |
| 288 |
|
| 289 |
if (nexthop->source != NETWORK_CONFIG_SOURCE_FOREIGN) |
| 290 |
return 0; /* Huh?? Already deserialized?? */ |
| 291 |
|
| 292 |
nexthop->source = p.source; |
| 293 |
memcpy_safe(&nexthop->provider, p.provider.iov_base, p.provider.iov_len); |
| 294 |
|
| 295 |
log_nexthop_debug(nexthop, "Deserialized", manager); |
| 296 |
return 0; |
| 297 |
} |
| 298 |
|
| 299 |
typedef struct RouteParam { |
| 300 |
Route route; |
| 301 |
|
| 302 |
struct iovec dst; |
| 303 |
struct iovec src; |
| 304 |
struct iovec prefsrc; |
| 305 |
struct iovec gw; |
| 306 |
struct iovec metrics; |
| 307 |
struct iovec provider; |
| 308 |
} RouteParam; |
| 309 |
|
| 310 |
static void route_param_done(RouteParam *p) { |
| 311 |
assert(p); |
| 312 |
|
| 313 |
free(p->route.metric.metrics); |
| 314 |
|
| 315 |
iovec_done(&p->dst); |
| 316 |
iovec_done(&p->src); |
| 317 |
iovec_done(&p->prefsrc); |
| 318 |
iovec_done(&p->gw); |
| 319 |
iovec_done(&p->metrics); |
| 320 |
iovec_done(&p->provider); |
| 321 |
} |
| 322 |
|
| 323 |
static int manager_deserialize_route(Manager *manager, sd_json_variant *v) { |
| 324 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 325 |
/* rtmsg header */ |
| 326 |
{ "Family", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_address_family, offsetof(RouteParam, route.family), SD_JSON_MANDATORY }, |
| 327 |
{ "DestinationPrefixLength", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint8, offsetof(RouteParam, route.dst_prefixlen), SD_JSON_MANDATORY }, |
| 328 |
{ "SourcePrefixLength", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint8, offsetof(RouteParam, route.src_prefixlen), 0 }, |
| 329 |
{ "TOS", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint8, offsetof(RouteParam, route.tos), SD_JSON_MANDATORY }, |
| 330 |
{ "Protocol", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint8, offsetof(RouteParam, route.protocol), SD_JSON_MANDATORY }, |
| 331 |
{ "Scope", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint8, offsetof(RouteParam, route.scope), SD_JSON_MANDATORY }, |
| 332 |
{ "Type", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint8, offsetof(RouteParam, route.type), SD_JSON_MANDATORY }, |
| 333 |
{ "Flags", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint32, offsetof(RouteParam, route.flags), SD_JSON_MANDATORY }, |
| 334 |
/* attributes */ |
| 335 |
{ "Destination", SD_JSON_VARIANT_ARRAY, json_dispatch_byte_array_iovec, offsetof(RouteParam, dst), SD_JSON_MANDATORY }, |
| 336 |
{ "Source", SD_JSON_VARIANT_ARRAY, json_dispatch_byte_array_iovec, offsetof(RouteParam, src), 0 }, |
| 337 |
{ "Priority", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint32, offsetof(RouteParam, route.priority), SD_JSON_MANDATORY }, |
| 338 |
{ "PreferredSource", SD_JSON_VARIANT_ARRAY, json_dispatch_byte_array_iovec, offsetof(RouteParam, prefsrc), 0 }, |
| 339 |
{ "Table", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint32, offsetof(RouteParam, route.table), SD_JSON_MANDATORY }, |
| 340 |
/* nexthops */ |
| 341 |
{ "Gateway", SD_JSON_VARIANT_ARRAY, json_dispatch_byte_array_iovec, offsetof(RouteParam, gw), 0 }, |
| 342 |
{ "InterfaceIndex", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_ifindex, offsetof(RouteParam, route.nexthop.ifindex), SD_JSON_MANDATORY | SD_JSON_RELAX }, |
| 343 |
{ "NextHopID", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint32, offsetof(RouteParam, route.nexthop_id), 0 }, |
| 344 |
/* metrics */ |
| 345 |
{ "Metrics", SD_JSON_VARIANT_ARRAY, json_dispatch_byte_array_iovec, offsetof(RouteParam, metrics), 0 }, |
| 346 |
{ "TCPCongestionControlAlgorithm", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(RouteParam, route.metric.tcp_congestion_control_algo), 0 }, |
| 347 |
/* config */ |
| 348 |
{ "ConfigSource", SD_JSON_VARIANT_STRING, json_dispatch_network_config_source, offsetof(RouteParam, route.source), SD_JSON_MANDATORY }, |
| 349 |
{ "ConfigProvider", SD_JSON_VARIANT_ARRAY, json_dispatch_byte_array_iovec, offsetof(RouteParam, provider), 0 }, |
| 350 |
{}, |
| 351 |
}; |
| 352 |
|
| 353 |
int r; |
| 354 |
|
| 355 |
assert(manager); |
| 356 |
assert(v); |
| 357 |
|
| 358 |
_cleanup_(route_param_done) RouteParam p = {}; |
| 359 |
r = sd_json_dispatch(v, dispatch_table, SD_JSON_ALLOW_EXTENSIONS, &p); |
| 360 |
if (r < 0) |
| 361 |
return log_debug_errno(r, "Failed to dispatch route from json variant: %m"); |
| 362 |
|
| 363 |
if (p.dst.iov_len != FAMILY_ADDRESS_SIZE(p.route.family)) |
| 364 |
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), |
| 365 |
"Dispatched destination address size (%zu) is incompatible with the family (%s).", |
| 366 |
p.dst.iov_len, af_to_ipv4_ipv6(p.route.family)); |
| 367 |
|
| 368 |
if (p.src.iov_len != 0 && p.src.iov_len != FAMILY_ADDRESS_SIZE(p.route.family)) |
| 369 |
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), |
| 370 |
"Dispatched source address size (%zu) is incompatible with the family (%s).", |
| 371 |
p.src.iov_len, af_to_ipv4_ipv6(p.route.family)); |
| 372 |
|
| 373 |
if (p.prefsrc.iov_len != 0 && p.prefsrc.iov_len != FAMILY_ADDRESS_SIZE(p.route.family)) |
| 374 |
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), |
| 375 |
"Dispatched preferred source address size (%zu) is incompatible with the family (%s).", |
| 376 |
p.prefsrc.iov_len, af_to_ipv4_ipv6(p.route.family)); |
| 377 |
|
| 378 |
switch (p.gw.iov_len) { |
| 379 |
case 0: |
| 380 |
p.route.nexthop.family = AF_UNSPEC; |
| 381 |
break; |
| 382 |
case sizeof(struct in_addr): |
| 383 |
p.route.nexthop.family = AF_INET; |
| 384 |
break; |
| 385 |
case sizeof(struct in6_addr): |
| 386 |
p.route.nexthop.family = AF_INET6; |
| 387 |
break; |
| 388 |
default: |
| 389 |
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), |
| 390 |
"Dispatched gateway address size (%zu) is invalid.", |
| 391 |
p.prefsrc.iov_len); |
| 392 |
} |
| 393 |
|
| 394 |
if (p.metrics.iov_len % sizeof(uint32_t) != 0) |
| 395 |
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), |
| 396 |
"Dispatched route metric size (%zu) is invalid.", |
| 397 |
p.metrics.iov_len); |
| 398 |
|
| 399 |
if (p.provider.iov_len != 0 && p.provider.iov_len != FAMILY_ADDRESS_SIZE(p.route.family)) |
| 400 |
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), |
| 401 |
"Dispatched provider address size (%zu) is incompatible with the family (%s).", |
| 402 |
p.provider.iov_len, af_to_ipv4_ipv6(p.route.family)); |
| 403 |
|
| 404 |
memcpy_safe(&p.route.dst, p.dst.iov_base, p.dst.iov_len); |
| 405 |
memcpy_safe(&p.route.src, p.src.iov_base, p.src.iov_len); |
| 406 |
memcpy_safe(&p.route.prefsrc, p.prefsrc.iov_base, p.prefsrc.iov_len); |
| 407 |
memcpy_safe(&p.route.nexthop.gw, p.gw.iov_base, p.gw.iov_len); |
| 408 |
|
| 409 |
p.route.metric.n_metrics = p.metrics.iov_len / sizeof(uint32_t); |
| 410 |
p.route.metric.metrics = new(uint32_t, p.route.metric.n_metrics); |
| 411 |
if (!p.route.metric.metrics) |
| 412 |
return log_oom_debug(); |
| 413 |
|
| 414 |
memcpy_safe(p.route.metric.metrics, p.metrics.iov_base, p.metrics.iov_len); |
| 415 |
|
| 416 |
Route *route; |
| 417 |
r = route_get(manager, &p.route, &route); |
| 418 |
if (r < 0) { |
| 419 |
log_route_debug(&p.route, "Cannot find deserialized", manager); |
| 420 |
return 0; /* Already removed? */ |
| 421 |
} |
| 422 |
|
| 423 |
if (route->source != NETWORK_CONFIG_SOURCE_FOREIGN) |
| 424 |
return 0; /* Huh?? Already deserialized?? */ |
| 425 |
|
| 426 |
route->source = p.route.source; |
| 427 |
memcpy_safe(&route->provider, p.provider.iov_base, p.provider.iov_len); |
| 428 |
|
| 429 |
log_route_debug(route, "Deserialized", manager); |
| 430 |
return 0; |
| 431 |
} |
| 432 |
|
| 433 |
int manager_deserialize(Manager *manager) { |
| 434 |
int r, ret = 0; |
| 435 |
|
| 436 |
assert(manager); |
| 437 |
|
| 438 |
_cleanup_close_ int fd = TAKE_FD(manager->serialization_fd); |
| 439 |
if (fd < 0) |
| 440 |
return 0; |
| 441 |
|
| 442 |
log_debug("Deserializing..."); |
| 443 |
|
| 444 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 445 |
unsigned err_line = 0, err_column = 0; |
| 446 |
r = sd_json_parse_fd( |
| 447 |
/* path= */ NULL, |
| 448 |
TAKE_FD(fd), |
| 449 |
SD_JSON_PARSE_DONATE_FD, |
| 450 |
&v, |
| 451 |
&err_line, |
| 452 |
&err_column); |
| 453 |
if (r < 0) |
| 454 |
return log_debug_errno(r, "Failed to parse json (line=%u, column=%u): %m", err_line, err_column); |
| 455 |
|
| 456 |
sd_json_variant *i; |
| 457 |
JSON_VARIANT_ARRAY_FOREACH(i, sd_json_variant_by_key(v, "Interfaces")) |
| 458 |
RET_GATHER(ret, manager_deserialize_link(manager, i)); |
| 459 |
|
| 460 |
JSON_VARIANT_ARRAY_FOREACH(i, sd_json_variant_by_key(v, "NextHops")) |
| 461 |
RET_GATHER(ret, manager_deserialize_nexthop(manager, i)); |
| 462 |
|
| 463 |
JSON_VARIANT_ARRAY_FOREACH(i, sd_json_variant_by_key(v, "Routes")) |
| 464 |
RET_GATHER(ret, manager_deserialize_route(manager, i)); |
| 465 |
|
| 466 |
log_debug("Deserialization completed."); |
| 467 |
return ret; |
| 468 |
} |