diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2023-09-04 17:16:19 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-04 17:16:19 +0200 |
commit | a8320879790c71b1aa244a37078fb6f3ca04361d (patch) | |
tree | 4be1863faffd3671d83282533fd3594e31347cbe /src/network | |
parent | Merge pull request #29052 from yuwata/icmp6-util-cleanups (diff) | |
parent | Add dhcp client prefix lease information to networkd json output (diff) | |
download | systemd-a8320879790c71b1aa244a37078fb6f3ca04361d.tar.xz systemd-a8320879790c71b1aa244a37078fb6f3ca04361d.zip |
Merge pull request #29023 from pelaufer/dhcp-prefix-dbus
Add dhcp client prefix lease information to networkd json output
Diffstat (limited to 'src/network')
-rw-r--r-- | src/network/networkd-json.c | 151 |
1 files changed, 149 insertions, 2 deletions
diff --git a/src/network/networkd-json.c b/src/network/networkd-json.c index c6756a8738..7c3639d245 100644 --- a/src/network/networkd-json.c +++ b/src/network/networkd-json.c @@ -9,6 +9,7 @@ #include "netif-util.h" #include "networkd-address.h" #include "networkd-dhcp-common.h" +#include "networkd-dhcp-prefix-delegation.h" #include "networkd-json.h" #include "networkd-link.h" #include "networkd-manager.h" @@ -1044,6 +1045,7 @@ static int dhcp_server_append_json(Link *link, JsonVariant **v) { static int dhcp6_client_lease_append_json(Link *link, JsonVariant **v) { _cleanup_(json_variant_unrefp) JsonVariant *w = NULL; + uint64_t lease_timestamp_usec; int r; assert(link); @@ -1052,15 +1054,59 @@ static int dhcp6_client_lease_append_json(Link *link, JsonVariant **v) { if (!link->dhcp6_lease) return 0; + r = sd_dhcp6_lease_get_timestamp(link->dhcp6_lease, CLOCK_BOOTTIME, &lease_timestamp_usec); + if (r < 0) + return 0; + r = json_build(&w, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_FINITE_USEC("T1", link->dhcp6_lease->lifetime_t1), - JSON_BUILD_PAIR_FINITE_USEC("T2", link->dhcp6_lease->lifetime_t2))); + JSON_BUILD_PAIR_FINITE_USEC("Timeout1USec", + link->dhcp6_lease->lifetime_t1 + lease_timestamp_usec), + JSON_BUILD_PAIR_FINITE_USEC("Timeout2USec", + link->dhcp6_lease->lifetime_t2 + lease_timestamp_usec), + JSON_BUILD_PAIR_FINITE_USEC("LeaseTimestampUSec", + lease_timestamp_usec))); if (r < 0) return r; return json_append_one(v, "Lease", w); } +static int dhcp6_client_pd_append_json(Link *link, JsonVariant **v) { + _cleanup_(json_variant_unrefp) JsonVariant *array = NULL; + struct in6_addr prefix; + uint32_t lifetime_preferred, lifetime_valid; + uint8_t prefix_len; + uint64_t lease_timestamp_usec; + int r; + + assert(link); + assert(link->network); + assert(v); + + if (!link->network->dhcp6_use_pd_prefix || !link->dhcp6_lease || !dhcp6_lease_has_pd_prefix(link->dhcp6_lease)) + return 0; + + sd_dhcp6_lease_reset_pd_prefix_iter(link->dhcp6_lease); + + r = sd_dhcp6_lease_get_timestamp(link->dhcp6_lease, CLOCK_BOOTTIME, &lease_timestamp_usec); + if (r < 0) + return 0; + + while (sd_dhcp6_lease_get_pd(link->dhcp6_lease, &prefix, &prefix_len, &lifetime_preferred, &lifetime_valid) >= 0) { + r = json_variant_append_arrayb(&array, JSON_BUILD_OBJECT( + JSON_BUILD_PAIR_IN6_ADDR("Prefix", &prefix), + JSON_BUILD_PAIR_UNSIGNED("PrefixLength", prefix_len), + JSON_BUILD_PAIR_FINITE_USEC("PreferredLifetimeUSec", + sec_to_usec(lifetime_preferred, lease_timestamp_usec)), + JSON_BUILD_PAIR_FINITE_USEC("ValidLifetimeUSec", + sec_to_usec(lifetime_valid, lease_timestamp_usec)))); + if (r < 0) + return r; + } + + return json_append_one(v, "Prefixes", array); +} + static int dhcp6_client_append_json(Link *link, JsonVariant **v) { _cleanup_(json_variant_unrefp) JsonVariant *w = NULL; int r; @@ -1075,9 +1121,106 @@ static int dhcp6_client_append_json(Link *link, JsonVariant **v) { if (r < 0) return r; + r = dhcp6_client_pd_append_json(link, &w); + if (r < 0) + return r; + return json_append_one(v, "DHCPv6Client", w); } +static int dhcp_client_lease_append_json(Link *link, JsonVariant **v) { + _cleanup_(json_variant_unrefp) JsonVariant *w = NULL; + uint32_t t1, t2; + uint64_t lease_timestamp_usec; + int r; + + assert(link); + assert(v); + + if (!link->dhcp_client || !link->dhcp_lease) + return 0; + + r = sd_dhcp_lease_get_t1(link->dhcp_lease, &t1); + if (r < 0) + return 0; + + r = sd_dhcp_lease_get_t2(link->dhcp_lease, &t2); + if (r < 0) + return 0; + + r = sd_dhcp_client_get_lease_timestamp(link->dhcp_client, &lease_timestamp_usec); + if (r < 0) + return 0; + + r = json_build(&w, JSON_BUILD_OBJECT( + JSON_BUILD_PAIR_FINITE_USEC("Timeout1USec", + sec_to_usec(t1, lease_timestamp_usec)), + JSON_BUILD_PAIR_FINITE_USEC("Timeout2USec", + sec_to_usec(t2, lease_timestamp_usec)), + JSON_BUILD_PAIR_FINITE_USEC("LeaseTimestampUSec", lease_timestamp_usec))); + if (r < 0) + return r; + + return json_append_one(v, "Lease", w); +} + +static int dhcp_client_pd_append_json(Link *link, JsonVariant **v) { + _cleanup_(json_variant_unrefp) JsonVariant *addresses = NULL, *array = NULL; + uint8_t ipv4masklen, sixrd_prefixlen; + struct in6_addr sixrd_prefix; + const struct in_addr *br_addresses; + size_t n_br_addresses = 0; + int r; + + assert(link); + assert(link->network); + assert(v); + + if (!link->network->dhcp_use_6rd || !link->dhcp_lease || !dhcp4_lease_has_pd_prefix(link->dhcp_lease)) + return 0; + + r = sd_dhcp_lease_get_6rd(link->dhcp_lease, &ipv4masklen, &sixrd_prefixlen, &sixrd_prefix, &br_addresses, &n_br_addresses); + if (r < 0) + return r; + + FOREACH_ARRAY(br_address, br_addresses, n_br_addresses) { + r = json_variant_append_arrayb(&addresses, JSON_BUILD_IN4_ADDR(br_address)); + if (r < 0) + return r; + } + + r = json_build(&array, JSON_BUILD_OBJECT( + JSON_BUILD_PAIR_IN6_ADDR("Prefix", &sixrd_prefix), + JSON_BUILD_PAIR_UNSIGNED("PrefixLength", sixrd_prefixlen), + JSON_BUILD_PAIR_UNSIGNED("IPv4MaskLength", ipv4masklen), + JSON_BUILD_PAIR_VARIANT_NON_NULL("BorderRouters", addresses))); + if (r < 0) + return r; + + return json_append_one(v, "6rdPrefix", array); +} + +static int dhcp_client_append_json(Link *link, JsonVariant **v) { + _cleanup_(json_variant_unrefp) JsonVariant *w = NULL; + int r; + + assert(link); + assert(v); + + if (!link->dhcp_client) + return 0; + + r = dhcp_client_lease_append_json(link, &w); + if (r < 0) + return r; + + r = dhcp_client_pd_append_json(link, &w); + if (r < 0) + return r; + + return json_append_one(v, "DHCPv4Client", w); +} + int link_build_json(Link *link, JsonVariant **ret) { _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; _cleanup_free_ char *type = NULL, *flags = NULL; @@ -1197,6 +1340,10 @@ int link_build_json(Link *link, JsonVariant **ret) { if (r < 0) return r; + r = dhcp_client_append_json(link, &v); + if (r < 0) + return r; + r = dhcp6_client_append_json(link, &v); if (r < 0) return r; |