HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/network/networkd-json.c
Git blob: 137653bd66754c6aed3337dcce9e420cc355e1be
Lines 1–1000 of 1,726
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <linux/nexthop.h>
#include "sd-device.h"
#include "sd-dhcp-client.h"
#include "sd-dhcp6-client.h"
#include "dhcp-lease-internal.h" /* IWYU pragma: keep */
#include "dhcp-server-internal.h"
#include "dhcp-server-lease-internal.h"
#include "dhcp6-lease-internal.h"
#include "extract-word.h"
#include "in-addr-util.h"
#include "iovec-util.h"
#include "ip-protocol-list.h"
#include "json-util.h"
#include "netif-util.h"
#include "networkd-address.h"
#include "networkd-dhcp-common.h"
#include "networkd-json.h"
#include "networkd-link.h"
#include "networkd-manager.h"
#include "networkd-neighbor.h"
#include "networkd-network.h"
#include "networkd-nexthop.h"
#include "networkd-ntp.h"
#include "networkd-route.h"
#include "networkd-route-util.h"
#include "networkd-routing-policy-rule.h"
#include "networkd-speed-meter.h"
#include "networkd-wwan.h"
#include "ordered-set.h"
#include "set.h"
#include "string-util.h"
#include "strv.h"
#include "udev-util.h"
#include "wifi-util.h"
static int address_append_json(Address *address, bool serializing, sd_json_variant **array) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
int r;
assert(address);
assert(array);
r = sd_json_buildo(
&v,
SD_JSON_BUILD_PAIR_INTEGER("Family", address->family),
JSON_BUILD_PAIR_IN_ADDR_WITH_STRING("Address", address->family, &address->in_addr),
JSON_BUILD_PAIR_IN_ADDR_WITH_STRING_NON_NULL("Peer", address->family, &address->in_addr_peer),
SD_JSON_BUILD_PAIR_UNSIGNED("PrefixLength", address->prefixlen),
SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(address->source)),
JSON_BUILD_PAIR_IN_ADDR_WITH_STRING_NON_NULL("ConfigProvider", address->family, &address->provider));
if (r < 0)
return r;
if (!serializing) {
_cleanup_free_ char *scope = NULL, *flags = NULL, *state = NULL;
r = route_scope_to_string_alloc(address->scope, &scope);
if (r < 0)
return r;
r = address_flags_to_string_alloc(address->flags, address->family, &flags);
if (r < 0)
return r;
r = network_config_state_to_string_alloc(address->state, &state);
if (r < 0)
return r;
r = sd_json_variant_merge_objectbo(
&v,
JSON_BUILD_PAIR_IN4_ADDR_WITH_STRING_NON_NULL("Broadcast", &address->broadcast),
SD_JSON_BUILD_PAIR_UNSIGNED("Scope", address->scope),
SD_JSON_BUILD_PAIR_STRING("ScopeString", scope),
SD_JSON_BUILD_PAIR_UNSIGNED("Flags", address->flags),
SD_JSON_BUILD_PAIR_STRING("FlagsString", flags),
JSON_BUILD_PAIR_STRING_NON_EMPTY("Label", address->label),
JSON_BUILD_PAIR_FINITE_USEC("PreferredLifetimeUSec", address->lifetime_preferred_usec),
JSON_BUILD_PAIR_FINITE_USEC("PreferredLifetimeUsec", address->lifetime_preferred_usec), /* for backward compat */
JSON_BUILD_PAIR_FINITE_USEC("ValidLifetimeUSec", address->lifetime_valid_usec),
JSON_BUILD_PAIR_FINITE_USEC("ValidLifetimeUsec", address->lifetime_valid_usec), /* for backward compat */
SD_JSON_BUILD_PAIR_STRING("ConfigState", state));
if (r < 0)
return r;
}
return sd_json_variant_append_array(array, v);
}
int addresses_append_json(Link *link, bool serializing, sd_json_variant **v) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
Address *address;
int r;
assert(link);
assert(v);
SET_FOREACH(address, link->addresses) {
if (serializing) {
if (address->source == NETWORK_CONFIG_SOURCE_FOREIGN)
continue;
if (!address_is_ready(address))
continue;
log_address_debug(address, "Serializing", link);
}
r = address_append_json(address, serializing, &array);
if (r < 0)
return r;
}
return json_variant_set_field_non_null(v, "Addresses", array);
}
static int neighbor_append_json(Neighbor *n, sd_json_variant **array) {
_cleanup_free_ char *state = NULL;
int r;
assert(n);
assert(array);
r = network_config_state_to_string_alloc(n->state, &state);
if (r < 0)
return r;
return sd_json_variant_append_arraybo(
array,
SD_JSON_BUILD_PAIR_INTEGER("Family", n->dst_addr.family),
JSON_BUILD_PAIR_IN_ADDR_WITH_STRING("Destination", n->dst_addr.family, &n->dst_addr.address),
JSON_BUILD_PAIR_HW_ADDR("LinkLayerAddress", &n->ll_addr),
SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(n->source)),
SD_JSON_BUILD_PAIR_STRING("ConfigState", state));
}
static int neighbors_append_json(Set *neighbors, sd_json_variant **v) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
Neighbor *neighbor;
int r;
assert(v);
SET_FOREACH(neighbor, neighbors) {
r = neighbor_append_json(neighbor, &array);
if (r < 0)
return r;
}
return json_variant_set_field_non_null(v, "Neighbors", array);
}
static int nexthop_group_build_json(NextHop *nexthop, sd_json_variant **ret) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
struct nexthop_grp *g;
int r;
assert(nexthop);
assert(ret);
HASHMAP_FOREACH(g, nexthop->group) {
r = sd_json_variant_append_arraybo(
&array,
SD_JSON_BUILD_PAIR_UNSIGNED("ID", g->id),
SD_JSON_BUILD_PAIR_UNSIGNED("Weight", g->weight+1));
if (r < 0)
return r;
}
*ret = TAKE_PTR(array);
return 0;
}
static int nexthop_append_json(NextHop *n, bool serializing, sd_json_variant **array) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
int r;
assert(n);
assert(array);
r = sd_json_buildo(
&v,
SD_JSON_BUILD_PAIR_UNSIGNED("ID", n->id),
SD_JSON_BUILD_PAIR_INTEGER("Family", n->family),
SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(n->source)),
JSON_BUILD_PAIR_IN_ADDR_WITH_STRING_NON_NULL("ConfigProvider", n->family, &n->provider));
if (r < 0)
return r;
if (!serializing) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *group = NULL;
_cleanup_free_ char *flags = NULL, *protocol = NULL, *state = NULL;
r = route_flags_to_string_alloc(n->flags, &flags);
if (r < 0)
return r;
r = route_protocol_to_string_alloc(n->protocol, &protocol);
if (r < 0)
return r;
r = network_config_state_to_string_alloc(n->state, &state);
if (r < 0)
return r;
r = nexthop_group_build_json(n, &group);
if (r < 0)
return r;
r = sd_json_variant_merge_objectbo(
&v,
JSON_BUILD_PAIR_IN_ADDR_WITH_STRING_NON_NULL("Gateway", n->family, &n->gw.address),
SD_JSON_BUILD_PAIR_UNSIGNED("Flags", n->flags),
SD_JSON_BUILD_PAIR_STRING("FlagsString", strempty(flags)),
SD_JSON_BUILD_PAIR_UNSIGNED("Protocol", n->protocol),
SD_JSON_BUILD_PAIR_STRING("ProtocolString", protocol),
SD_JSON_BUILD_PAIR_BOOLEAN("Blackhole", n->blackhole),
JSON_BUILD_PAIR_VARIANT_NON_NULL("Group", group),
SD_JSON_BUILD_PAIR_STRING("ConfigState", state));
}
return sd_json_variant_append_array(array, v);
}
int nexthops_append_json(Manager *manager, int ifindex, sd_json_variant **v) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
NextHop *nexthop;
int r;
assert(manager);
assert(v);
HASHMAP_FOREACH(nexthop, manager->nexthops_by_id) {
if (ifindex >= 0) {
if (nexthop->ifindex != ifindex)
continue;
} else {
/* negative ifindex means we are serializing now. */
if (nexthop->source == NETWORK_CONFIG_SOURCE_FOREIGN)
continue;
if (!nexthop_exists(nexthop))
continue;
log_nexthop_debug(nexthop, "Serializing", manager);
}
r = nexthop_append_json(nexthop, /* serializing= */ ifindex < 0, &array);
if (r < 0)
return r;
}
return json_variant_set_field_non_null(v, "NextHops", array);
}
static int route_append_json(Route *route, bool serializing, sd_json_variant **array) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
int r;
assert(route);
assert(array);
r = sd_json_buildo(
&v,
SD_JSON_BUILD_PAIR_INTEGER("Family", route->family),
JSON_BUILD_PAIR_IN_ADDR_WITH_STRING("Destination", route->family, &route->dst),
SD_JSON_BUILD_PAIR_UNSIGNED("DestinationPrefixLength", route->dst_prefixlen),
JSON_BUILD_PAIR_IN_ADDR_WITH_STRING_NON_NULL("Gateway", route->nexthop.family, &route->nexthop.gw),
JSON_BUILD_PAIR_IN_ADDR_WITH_STRING_NON_NULL("Source", route->family, route->src_prefixlen > 0 ? &route->src : NULL),
JSON_BUILD_PAIR_UNSIGNED_NON_ZERO("SourcePrefixLength", route->src_prefixlen),
JSON_BUILD_PAIR_IN_ADDR_WITH_STRING_NON_NULL("PreferredSource", route->family, &route->prefsrc),
SD_JSON_BUILD_PAIR_UNSIGNED("TOS", route->tos),
SD_JSON_BUILD_PAIR_UNSIGNED("Scope", route->scope),
SD_JSON_BUILD_PAIR_UNSIGNED("Protocol", route->protocol),
SD_JSON_BUILD_PAIR_UNSIGNED("Type", route->type),
SD_JSON_BUILD_PAIR_UNSIGNED("Priority", route->priority),
SD_JSON_BUILD_PAIR_UNSIGNED("Table", route->table),
SD_JSON_BUILD_PAIR_UNSIGNED("Flags", route->flags),
JSON_BUILD_PAIR_UNSIGNED_NON_ZERO("NextHopID", route->nexthop_id),
SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(route->source)),
JSON_BUILD_PAIR_IN_ADDR_WITH_STRING_NON_NULL("ConfigProvider", route->family, &route->provider));
if (r < 0)
return r;
if (serializing) {
r = sd_json_variant_merge_objectbo(
&v,
SD_JSON_BUILD_PAIR_INTEGER("InterfaceIndex", route->nexthop.ifindex),
JSON_BUILD_PAIR_BYTE_ARRAY_NON_EMPTY("Metrics", route->metric.metrics, route->metric.n_metrics),
JSON_BUILD_PAIR_STRING_NON_EMPTY("TCPCongestionControlAlgorithm", route->metric.tcp_congestion_control_algo));
if (r < 0)
return r;
} else {
_cleanup_free_ char *scope = NULL, *protocol = NULL, *table = NULL, *flags = NULL, *state = NULL;
r = route_scope_to_string_alloc(route->scope, &scope);
if (r < 0)
return r;
r = route_protocol_to_string_alloc(route->protocol, &protocol);
if (r < 0)
return r;
r = manager_get_route_table_to_string(route->manager, route->table, /* append_num= */ false, &table);
if (r < 0)
return r;
r = route_flags_to_string_alloc(route->flags, &flags);
if (r < 0)
return r;
r = network_config_state_to_string_alloc(route->state, &state);
if (r < 0)
return r;
r = sd_json_variant_merge_objectbo(
&v,
SD_JSON_BUILD_PAIR_STRING("ScopeString", scope),
SD_JSON_BUILD_PAIR_STRING("ProtocolString", protocol),
SD_JSON_BUILD_PAIR_STRING("TypeString", route_type_to_string(route->type)),
SD_JSON_BUILD_PAIR_STRING("TableString", table),
JSON_BUILD_PAIR_UNSIGNED_NON_ZERO("MTU", route_metric_get(&route->metric, RTAX_MTU)),
SD_JSON_BUILD_PAIR_UNSIGNED("Preference", route->pref),
SD_JSON_BUILD_PAIR_STRING("FlagsString", strempty(flags)),
JSON_BUILD_PAIR_FINITE_USEC("LifetimeUSec", route->lifetime_usec),
SD_JSON_BUILD_PAIR_STRING("ConfigState", state));
if (r < 0)
return r;
}
return sd_json_variant_append_array(array, v);
}
int routes_append_json(Manager *manager, int ifindex, sd_json_variant **v) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
Route *route;
int r;
assert(manager);
assert(v);
SET_FOREACH(route, manager->routes) {
if (ifindex >= 0) {
if (route->nexthop.ifindex != ifindex)
continue;
} else {
/* negative ifindex means we are serializing now. */
if (route->source == NETWORK_CONFIG_SOURCE_FOREIGN)
continue;
if (!route_exists(route))
continue;
log_route_debug(route, "Serializing", manager);
}
r = route_append_json(route, /* serializing= */ ifindex < 0, &array);
if (r < 0)
return r;
}
return json_variant_set_field_non_null(v, "Routes", array);
}
static int routing_policy_rule_append_json(RoutingPolicyRule *rule, sd_json_variant **array) {
_cleanup_free_ char *table = NULL, *protocol = NULL, *state = NULL;
int r;
assert(rule);
assert(rule->manager);
assert(array);
r = manager_get_route_table_to_string(rule->manager, rule->table, /* append_num= */ false, &table);
if (r < 0 && r != -EINVAL)
return r;
r = route_protocol_to_string_alloc(rule->protocol, &protocol);
if (r < 0)
return r;
r = network_config_state_to_string_alloc(rule->state, &state);
if (r < 0)
return r;
return sd_json_variant_append_arraybo(
array,
SD_JSON_BUILD_PAIR_INTEGER("Family", rule->family),
JSON_BUILD_PAIR_IN_ADDR_WITH_STRING_NON_NULL("FromPrefix", rule->family, &rule->from.address),
SD_JSON_BUILD_PAIR_CONDITION(in_addr_is_set(rule->family, &rule->from.address),
"FromPrefixLength", SD_JSON_BUILD_UNSIGNED(rule->from.prefixlen)),
JSON_BUILD_PAIR_IN_ADDR_WITH_STRING_NON_NULL("ToPrefix", rule->family, &rule->to.address),
SD_JSON_BUILD_PAIR_CONDITION(in_addr_is_set(rule->family, &rule->to.address),
"ToPrefixLength", SD_JSON_BUILD_UNSIGNED(rule->to.prefixlen)),
SD_JSON_BUILD_PAIR_UNSIGNED("Protocol", rule->protocol),
SD_JSON_BUILD_PAIR_STRING("ProtocolString", protocol),
SD_JSON_BUILD_PAIR_UNSIGNED("TOS", rule->tos),
SD_JSON_BUILD_PAIR_UNSIGNED("Type", rule->action),
SD_JSON_BUILD_PAIR_STRING("TypeString", fr_act_type_to_string(rule->action)),
SD_JSON_BUILD_PAIR_UNSIGNED("IPProtocol", rule->ipproto),
SD_JSON_BUILD_PAIR_STRING("IPProtocolString", ip_protocol_to_name(rule->ipproto)),
SD_JSON_BUILD_PAIR_UNSIGNED("Priority", rule->priority),
SD_JSON_BUILD_PAIR_UNSIGNED("FirewallMark", rule->fwmark),
SD_JSON_BUILD_PAIR_UNSIGNED("FirewallMask", rule->fwmask),
JSON_BUILD_PAIR_UNSIGNED_NON_ZERO("Table", rule->table),
JSON_BUILD_PAIR_STRING_NON_EMPTY("TableString", table),
SD_JSON_BUILD_PAIR_BOOLEAN("Invert", FLAGS_SET(rule->flags, FIB_RULE_INVERT)),
SD_JSON_BUILD_PAIR_CONDITION(rule->suppress_prefixlen >= 0,
"SuppressPrefixLength", SD_JSON_BUILD_UNSIGNED(rule->suppress_prefixlen)),
SD_JSON_BUILD_PAIR_CONDITION(rule->suppress_ifgroup >= 0,
"SuppressInterfaceGroup", SD_JSON_BUILD_UNSIGNED(rule->suppress_ifgroup)),
SD_JSON_BUILD_PAIR_CONDITION(rule->sport.start != 0 || rule->sport.end != 0, "SourcePort",
SD_JSON_BUILD_ARRAY(SD_JSON_BUILD_UNSIGNED(rule->sport.start), SD_JSON_BUILD_UNSIGNED(rule->sport.end))),
SD_JSON_BUILD_PAIR_CONDITION(rule->dport.start != 0 || rule->dport.end != 0, "DestinationPort",
SD_JSON_BUILD_ARRAY(SD_JSON_BUILD_UNSIGNED(rule->dport.start), SD_JSON_BUILD_UNSIGNED(rule->dport.end))),
SD_JSON_BUILD_PAIR_CONDITION(rule->uid_range.start != UID_INVALID && rule->uid_range.end != UID_INVALID, "User",
SD_JSON_BUILD_ARRAY(SD_JSON_BUILD_UNSIGNED(rule->uid_range.start), SD_JSON_BUILD_UNSIGNED(rule->uid_range.end))),
JSON_BUILD_PAIR_STRING_NON_EMPTY("IncomingInterface", rule->iif),
JSON_BUILD_PAIR_STRING_NON_EMPTY("OutgoingInterface", rule->oif),
SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(rule->source)),
SD_JSON_BUILD_PAIR_STRING("ConfigState", state));
}
static int routing_policy_rules_append_json(Set *rules, sd_json_variant **v) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
RoutingPolicyRule *rule;
int r;
assert(v);
SET_FOREACH(rule, rules) {
r = routing_policy_rule_append_json(rule, &array);
if (r < 0)
return r;
}
return json_variant_set_field_non_null(v, "RoutingPolicyRules", array);
}
static int network_append_json(Network *network, sd_json_variant **v) {
assert(v);
if (!network)
return 0;
return sd_json_variant_merge_objectbo(
v,
SD_JSON_BUILD_PAIR_STRING("NetworkFile", network->filename),
SD_JSON_BUILD_PAIR_STRV("NetworkFileDropins", network->dropins),
SD_JSON_BUILD_PAIR_BOOLEAN("RequiredForOnline", network->required_for_online > 0),
SD_JSON_BUILD_PAIR_CONDITION(
operational_state_range_is_valid(&network->required_operstate_for_online),
"RequiredOperationalStateForOnline",
SD_JSON_BUILD_ARRAY(
SD_JSON_BUILD_STRING(link_operstate_to_string(network->required_operstate_for_online.min)),
SD_JSON_BUILD_STRING(link_operstate_to_string(network->required_operstate_for_online.max)))),
SD_JSON_BUILD_PAIR_STRING("RequiredFamilyForOnline", link_required_address_family_to_string(network->required_family_for_online)),
SD_JSON_BUILD_PAIR_STRING("ActivationPolicy", activation_policy_to_string(network->activation_policy)));
}
static int netdev_append_json(NetDev *netdev, sd_json_variant **v) {
assert(v);
if (!netdev)
return 0;
return sd_json_variant_merge_objectbo(
v,
SD_JSON_BUILD_PAIR_STRING("NetDevFile", netdev->filename),
SD_JSON_BUILD_PAIR_STRV("NetDevFileDropins", netdev->dropins));
}
static int device_append_json(sd_device *device, sd_json_variant **v) {
_cleanup_strv_free_ char **link_dropins = NULL;
const char *link = NULL, *path = NULL, *vendor = NULL, *model = NULL, *joined;
int r;
assert(v);
if (!device)
return 0;
(void) sd_device_get_property_value(device, "ID_NET_LINK_FILE", &link);
if (sd_device_get_property_value(device, "ID_NET_LINK_FILE_DROPINS", &joined) >= 0) {
r = strv_split_full(&link_dropins, joined, ":", EXTRACT_CUNESCAPE);
if (r < 0)
return r;
}
(void) sd_device_get_property_value(device, "ID_PATH", &path);
(void) device_get_vendor_string(device, &vendor);
(void) device_get_model_string(device, &model);
return sd_json_variant_merge_objectbo(
v,
JSON_BUILD_PAIR_STRING_NON_EMPTY("LinkFile", link),
JSON_BUILD_PAIR_STRV_NON_EMPTY("LinkFileDropins", link_dropins),
JSON_BUILD_PAIR_STRING_NON_EMPTY("Path", path),
JSON_BUILD_PAIR_STRING_NON_EMPTY("Vendor", vendor),
JSON_BUILD_PAIR_STRING_NON_EMPTY("Model", model));
}
static int dns_append_json_one(Link *link, const struct in_addr_full *a, NetworkConfigSource s, const union in_addr_union *p, sd_json_variant **array) {
assert(link);
assert(a);
assert(array);
if (a->ifindex != 0 && a->ifindex != link->ifindex)
return 0;
return sd_json_variant_append_arraybo(
array,
SD_JSON_BUILD_PAIR_INTEGER("Family", a->family),
JSON_BUILD_PAIR_IN_ADDR_WITH_STRING("Address", a->family, &a->address),
JSON_BUILD_PAIR_UNSIGNED_NON_ZERO("Port", a->port),
SD_JSON_BUILD_PAIR_CONDITION(a->ifindex != 0, "InterfaceIndex", SD_JSON_BUILD_INTEGER(a->ifindex)),
JSON_BUILD_PAIR_STRING_NON_EMPTY("ServerName", a->server_name),
SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(s)),
JSON_BUILD_PAIR_IN_ADDR_WITH_STRING_NON_NULL("ConfigProvider", a->family, p));
}
static int dns_append_json(Link *link, sd_json_variant **v) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
int r;
assert(link);
assert(v);
if (!link->network)
return 0;
if (link->n_dns != UINT_MAX)
for (unsigned i = 0; i < link->n_dns; i++) {
r = dns_append_json_one(link, link->dns[i], NETWORK_CONFIG_SOURCE_RUNTIME, NULL, &array);
if (r < 0)
return r;
}
else {
for (unsigned i = 0; i < link->network->n_dns; i++) {
r = dns_append_json_one(link, link->network->dns[i], NETWORK_CONFIG_SOURCE_STATIC, NULL, &array);
if (r < 0)
return r;
}
Bearer *b;
if (link_get_bearer(link, &b) >= 0)
FOREACH_ARRAY(dns, b->dns, b->n_dns) {
r = dns_append_json_one(link, *dns, NETWORK_CONFIG_SOURCE_MODEM_MANAGER, NULL, &array);
if (r < 0)
return r;
}
if (link->dhcp_lease && link_get_use_dns(link, NETWORK_CONFIG_SOURCE_DHCP4)) {
const struct in_addr *dns;
union in_addr_union s;
int n_dns;
r = sd_dhcp_lease_get_server_identifier(link->dhcp_lease, &s.in);
if (r < 0)
return r;
n_dns = sd_dhcp_lease_get_dns(link->dhcp_lease, &dns);
for (int i = 0; i < n_dns; i++) {
r = dns_append_json_one(link,
&(struct in_addr_full) { .family = AF_INET, .address.in = dns[i], },
NETWORK_CONFIG_SOURCE_DHCP4,
&s,
&array);
if (r < 0)
return r;
}
}
if (link->dhcp6_lease && link_get_use_dns(link, NETWORK_CONFIG_SOURCE_DHCP6)) {
const struct in6_addr *dns;
union in_addr_union s;
int n_dns;
r = sd_dhcp6_lease_get_server_address(link->dhcp6_lease, &s.in6);
if (r < 0)
return r;
n_dns = sd_dhcp6_lease_get_dns(link->dhcp6_lease, &dns);
for (int i = 0; i < n_dns; i++) {
r = dns_append_json_one(link,
&(struct in_addr_full) { .family = AF_INET6, .address.in6 = dns[i], },
NETWORK_CONFIG_SOURCE_DHCP6,
&s,
&array);
if (r < 0)
return r;
}
}
if (link_get_use_dns(link, NETWORK_CONFIG_SOURCE_NDISC)) {
NDiscRDNSS *a;
SET_FOREACH(a, link->ndisc_rdnss) {
r = dns_append_json_one(link,
&(struct in_addr_full) { .family = AF_INET6, .address.in6 = a->address, },
NETWORK_CONFIG_SOURCE_NDISC,
&(union in_addr_union) { .in6 = a->router },
&array);
if (r < 0)
return r;
}
}
}
return json_variant_set_field_non_null(v, "DNS", array);
}
static int dnr_append_json_one(Link *link, const struct sd_dns_resolver *res, NetworkConfigSource s, const union in_addr_union *p, sd_json_variant **array) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *addrs_array = NULL;
_cleanup_strv_free_ char **transports = NULL;
int r;
assert(link);
assert(res);
assert(array);
FOREACH_ARRAY(addr, res->addrs, res->n_addrs) {
r = sd_json_variant_append_arrayb(
&addrs_array,
JSON_BUILD_IN_ADDR(res->family, addr));
if (r < 0)
return r;
}
r = dns_resolver_transports_to_strv(res->transports, &transports);
if (r < 0)
return r;
//FIXME ifindex?
return sd_json_variant_append_arrayb(
array,
SD_JSON_BUILD_OBJECT(
SD_JSON_BUILD_PAIR_INTEGER("Family", res->family),
SD_JSON_BUILD_PAIR_INTEGER("Priority", res->priority),
JSON_BUILD_PAIR_VARIANT_NON_NULL("Addresses", addrs_array),
JSON_BUILD_PAIR_UNSIGNED_NON_ZERO("Port", res->port),
JSON_BUILD_PAIR_STRING_NON_EMPTY("ServerName", res->auth_name),
JSON_BUILD_PAIR_STRING_NON_EMPTY("DoHPath", res->dohpath),
JSON_BUILD_PAIR_STRV_NON_EMPTY("Transports", transports),
SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(s)),
JSON_BUILD_PAIR_IN_ADDR_NON_NULL("ConfigProvider", res->family, p)));
}
static int dnr_append_json(Link *link, sd_json_variant **v) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
int r;
assert(link);
assert(v);
if (!link->network)
return 0;
if (link->dhcp_lease && link_get_use_dnr(link, NETWORK_CONFIG_SOURCE_DHCP4)) {
struct sd_dns_resolver *dnr;
union in_addr_union s;
int n_dnr;
r = sd_dhcp_lease_get_server_identifier(link->dhcp_lease, &s.in);
if (r < 0)
return r;
n_dnr = sd_dhcp_lease_get_dnr(link->dhcp_lease, &dnr);
if (n_dnr > 0)
FOREACH_ARRAY(res, dnr, n_dnr) {
r = dnr_append_json_one(link,
res,
NETWORK_CONFIG_SOURCE_DHCP4,
&s,
&array);
if (r < 0)
return r;
}
}
if (link->dhcp6_lease && link_get_use_dnr(link, NETWORK_CONFIG_SOURCE_DHCP6)) {
struct sd_dns_resolver *dnr;
union in_addr_union s;
int n_dnr;
r = sd_dhcp6_lease_get_server_address(link->dhcp6_lease, &s.in6);
if (r < 0)
return r;
n_dnr = sd_dhcp6_lease_get_dnr(link->dhcp6_lease, &dnr);
if (n_dnr > 0)
FOREACH_ARRAY(res, dnr, n_dnr) {
r = dnr_append_json_one(link,
res,
NETWORK_CONFIG_SOURCE_DHCP6,
&s,
&array);
if (r < 0)
return r;
}
}
if (link_get_use_dnr(link, NETWORK_CONFIG_SOURCE_NDISC)) {
NDiscDNR *a;
SET_FOREACH(a, link->ndisc_dnr) {
r = dnr_append_json_one(link,
&a->resolver,
NETWORK_CONFIG_SOURCE_NDISC,
&(union in_addr_union) { .in6 = a->router },
&array);
if (r < 0)
return r;
}
}
return json_variant_set_field_non_null(v, "DNR", array);
}
static int server_append_json_one_addr(int family, const union in_addr_union *a, NetworkConfigSource s, const union in_addr_union *p, sd_json_variant **array) {
_cleanup_free_ char *address_str = NULL;
int r;
assert(IN_SET(family, AF_INET, AF_INET6));
assert(a);
assert(array);
r = in_addr_to_string(family, a, &address_str);
if (r < 0)
return r;
return sd_json_variant_append_arraybo(
array,
SD_JSON_BUILD_PAIR_INTEGER("Family", family),
JSON_BUILD_PAIR_IN_ADDR("Address", family, a),
SD_JSON_BUILD_PAIR_STRING("AddressString", address_str),
SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(s)),
JSON_BUILD_PAIR_IN_ADDR_WITH_STRING_NON_NULL("ConfigProvider", family, p));
}
static int server_append_json_one_fqdn(int family, const char *fqdn, NetworkConfigSource s, const union in_addr_union *p, sd_json_variant **array) {
assert(IN_SET(family, AF_UNSPEC, AF_INET, AF_INET6));
assert(fqdn);
assert(array);
return sd_json_variant_append_arraybo(
array,
SD_JSON_BUILD_PAIR_STRING("Server", fqdn),
SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(s)),
JSON_BUILD_PAIR_IN_ADDR_WITH_STRING_NON_NULL("ConfigProvider", family, p));
}
static int server_append_json_one_string(const char *str, NetworkConfigSource s, sd_json_variant **array) {
union in_addr_union a;
int family;
assert(str);
if (in_addr_from_string_auto(str, &family, &a) >= 0)
return server_append_json_one_addr(family, &a, s, NULL, array);
return server_append_json_one_fqdn(AF_UNSPEC, str, s, NULL, array);
}
static int ntp_append_json(Link *link, sd_json_variant **v) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
int r;
assert(link);
assert(v);
if (!link->network)
return 0;
STRV_FOREACH(p, link->ntp ?: link->network->ntp) {
r = server_append_json_one_string(*p, NETWORK_CONFIG_SOURCE_RUNTIME, &array);
if (r < 0)
return r;
}
if (!link->ntp) {
if (link->dhcp_lease && link_get_use_ntp(link, NETWORK_CONFIG_SOURCE_DHCP4)) {
const struct in_addr *ntp;
union in_addr_union s;
int n_ntp;
r = sd_dhcp_lease_get_server_identifier(link->dhcp_lease, &s.in);
if (r < 0)
return r;
n_ntp = sd_dhcp_lease_get_ntp(link->dhcp_lease, &ntp);
for (int i = 0; i < n_ntp; i++) {
r = server_append_json_one_addr(AF_INET,
&(union in_addr_union) { .in = ntp[i], },
NETWORK_CONFIG_SOURCE_DHCP4,
&s,
&array);
if (r < 0)
return r;
}
}
if (link->dhcp6_lease && link_get_use_ntp(link, NETWORK_CONFIG_SOURCE_DHCP6)) {
const struct in6_addr *ntp_addr;
union in_addr_union s;
char **ntp_fqdn;
int n_ntp;
r = sd_dhcp6_lease_get_server_address(link->dhcp6_lease, &s.in6);
if (r < 0)
return r;
n_ntp = sd_dhcp6_lease_get_ntp_addrs(link->dhcp6_lease, &ntp_addr);
for (int i = 0; i < n_ntp; i++) {
r = server_append_json_one_addr(AF_INET6,
&(union in_addr_union) { .in6 = ntp_addr[i], },
NETWORK_CONFIG_SOURCE_DHCP6,
&s,
&array);
if (r < 0)
return r;
}
n_ntp = sd_dhcp6_lease_get_ntp_fqdn(link->dhcp6_lease, &ntp_fqdn);
for (int i = 0; i < n_ntp; i++) {
r = server_append_json_one_fqdn(AF_INET6,
ntp_fqdn[i],
NETWORK_CONFIG_SOURCE_DHCP6,
&s,
&array);
if (r < 0)
return r;
}
}
}
return json_variant_set_field_non_null(v, "NTP", array);
}
static int domain_append_json(int family, const char *domain, NetworkConfigSource s, const union in_addr_union *p, sd_json_variant **array) {
assert(IN_SET(family, AF_UNSPEC, AF_INET, AF_INET6));
assert(domain);
assert(array);
return sd_json_variant_append_arraybo(
array,
SD_JSON_BUILD_PAIR_STRING("Domain", domain),
SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(s)),
JSON_BUILD_PAIR_IN_ADDR_WITH_STRING_NON_NULL("ConfigProvider", family, p));
}
static int sip_append_json(Link *link, sd_json_variant **v) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
int r;
assert(link);
assert(v);
if (!link->network)
return 0;
if (link->dhcp_lease && link->network->dhcp_use_sip) {
const struct in_addr *sip;
union in_addr_union s;
int n_sip;
r = sd_dhcp_lease_get_server_identifier(link->dhcp_lease, &s.in);
if (r < 0)
return r;
n_sip = sd_dhcp_lease_get_sip(link->dhcp_lease, &sip);
for (int i = 0; i < n_sip; i++) {
r = server_append_json_one_addr(AF_INET,
&(union in_addr_union) { .in = sip[i], },
NETWORK_CONFIG_SOURCE_DHCP4,
&s,
&array);
if (r < 0)
return r;
}
}
if (link->dhcp6_lease && link->network->dhcp6_use_sip) {
const struct in6_addr *sip_addr;
union in_addr_union s;
char **domains;
int n_sip;
r = sd_dhcp6_lease_get_server_address(link->dhcp6_lease, &s.in6);
if (r < 0)
return r;
n_sip = sd_dhcp6_lease_get_sip_addrs(link->dhcp6_lease, &sip_addr);
for (int i = 0; i < n_sip; i++) {
r = server_append_json_one_addr(AF_INET6,
&(union in_addr_union) { .in6 = sip_addr[i], },
NETWORK_CONFIG_SOURCE_DHCP6,
&s,
&array);
if (r < 0)
return r;
}
if (sd_dhcp6_lease_get_sip_domains(link->dhcp6_lease, &domains) >= 0)
STRV_FOREACH(p, domains) {
r = domain_append_json(AF_INET6,
*p,
NETWORK_CONFIG_SOURCE_DHCP6,
&s,
&array);
if (r < 0)
return r;
}
}
return json_variant_set_field_non_null(v, "SIP", array);
}
static int domains_append_json(Link *link, bool is_route, sd_json_variant **v) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
OrderedSet *link_domains, *network_domains;
UseDomains use_domains;
union in_addr_union s;
char **domains;
const char *domain;
int r;
assert(link);
assert(v);
if (!link->network)
return 0;
link_domains = is_route ? link->route_domains : link->search_domains;
network_domains = is_route ? link->network->route_domains : link->network->search_domains;
use_domains = is_route ? USE_DOMAINS_ROUTE : USE_DOMAINS_YES;
ORDERED_SET_FOREACH(domain, link_domains ?: network_domains) {
r = domain_append_json(AF_UNSPEC, domain,
link_domains ? NETWORK_CONFIG_SOURCE_RUNTIME : NETWORK_CONFIG_SOURCE_STATIC,
NULL, &array);
if (r < 0)
return r;
}
if (!link_domains) {
if (link->dhcp_lease &&
link_get_use_domains(link, NETWORK_CONFIG_SOURCE_DHCP4) == use_domains) {
r = sd_dhcp_lease_get_server_identifier(link->dhcp_lease, &s.in);
if (r < 0)
return r;
if (sd_dhcp_lease_get_domainname(link->dhcp_lease, &domain) >= 0) {
r = domain_append_json(AF_INET, domain, NETWORK_CONFIG_SOURCE_DHCP4, &s, &array);
if (r < 0)
return r;
}
if (sd_dhcp_lease_get_search_domains(link->dhcp_lease, &domains) >= 0)
STRV_FOREACH(p, domains) {
r = domain_append_json(AF_INET, *p, NETWORK_CONFIG_SOURCE_DHCP4, &s, &array);
if (r < 0)
return r;
}
}
if (link->dhcp6_lease &&
link_get_use_domains(link, NETWORK_CONFIG_SOURCE_DHCP6) == use_domains) {
r = sd_dhcp6_lease_get_server_address(link->dhcp6_lease, &s.in6);
if (r < 0)
return r;
if (sd_dhcp6_lease_get_domains(link->dhcp6_lease, &domains) >= 0)
STRV_FOREACH(p, domains) {
r = domain_append_json(AF_INET6, *p, NETWORK_CONFIG_SOURCE_DHCP6, &s, &array);
if (r < 0)
return r;
}
}
if (link_get_use_domains(link, NETWORK_CONFIG_SOURCE_NDISC) == use_domains) {
NDiscDNSSL *a;
SET_FOREACH(a, link->ndisc_dnssl) {
r = domain_append_json(AF_INET6, ndisc_dnssl_domain(a), NETWORK_CONFIG_SOURCE_NDISC,
&(union in_addr_union) { .in6 = a->router },
&array);
if (r < 0)
return r;
}
}
}
return json_variant_set_field_non_null(v, is_route ? "RouteDomains" : "SearchDomains", array);