From: Andrew Zaborowski <andrew.zaborowski at intel.com>
To: ell at lists.01.org
Subject: [PATCH 05/17] netconfig: Handle DHCPv6 events
Date: Sat, 14 May 2022 00:47:01 +0200 [thread overview]
Message-ID: <20220513224713.1447773-5-andrew.zaborowski@intel.com> (raw)
In-Reply-To: 20220513224713.1447773-1-andrew.zaborowski@intel.com
[-- Attachment #1: Type: text/plain, Size: 4837 bytes --]
Use l_dhcp6_client to support IPv6 address assignment through DHCPv6.
---
ell/netconfig.c | 132 ++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 132 insertions(+)
diff --git a/ell/netconfig.c b/ell/netconfig.c
index 6ad6394..990b166 100644
--- a/ell/netconfig.c
+++ b/ell/netconfig.c
@@ -400,6 +400,135 @@ static void netconfig_dhcp_event_handler(struct l_dhcp_client *client,
}
}
+static void netconfig_add_dhcp6_address(struct l_netconfig *nc)
+{
+ const struct l_dhcp6_lease *lease =
+ l_dhcp6_client_get_lease(nc->dhcp6_client);
+ _auto_(l_free) char *ip = NULL;
+ uint32_t prefix_len;
+
+ if (L_WARN_ON(!lease))
+ return;
+
+ ip = l_dhcp6_lease_get_address(lease);
+ prefix_len = l_dhcp6_lease_get_prefix_length(lease);
+ nc->v6_address = l_rtnl_address_new(ip, prefix_len);
+
+ if (L_WARN_ON(!nc->v6_address))
+ return;
+
+ /*
+ * Assume we already have a route from a Router Advertisement
+ * covering the address from DHCPv6 + prefix length from DHCPv6.
+ * We might want to emit a warning of some sort or
+ * L_NETCONFIG_EVENT_FAILED if we don't since this would
+ * basically be fatal for IPv6 connectivity.
+ */
+ l_rtnl_address_set_noprefixroute(nc->v6_address, true);
+
+ l_queue_push_tail(nc->addresses.current, nc->v6_address);
+ l_queue_push_tail(nc->addresses.added, nc->v6_address);
+}
+
+static void netconfig_set_dhcp6_address_lifetimes(struct l_netconfig *nc,
+ bool updated)
+{
+ const struct l_dhcp6_lease *lease =
+ l_dhcp6_client_get_lease(nc->dhcp6_client);
+ uint32_t p, v;
+ uint64_t start_time;
+
+ if (L_WARN_ON(!lease))
+ return;
+
+ p = l_dhcp6_lease_get_preferred_lifetime(lease);
+ v = l_dhcp6_lease_get_valid_lifetime(lease);
+ start_time = l_dhcp6_lease_get_start_time(lease);
+
+ l_rtnl_address_set_lifetimes(nc->v6_address, p, v);
+ l_rtnl_address_set_expiry(nc->v6_address,
+ start_time + p * L_USEC_PER_SEC,
+ start_time + v * L_USEC_PER_SEC);
+
+ if (updated)
+ l_queue_push_tail(nc->addresses.updated, nc->v6_address);
+}
+
+static void netconfig_remove_dhcp6_address(struct l_netconfig *nc)
+{
+ l_queue_remove(nc->addresses.current, nc->v6_address);
+ l_queue_push_tail(nc->addresses.removed, nc->v6_address);
+ nc->v6_address = NULL;
+}
+
+static void netconfig_dhcp6_event_handler(struct l_dhcp6_client *client,
+ enum l_dhcp6_client_event event,
+ void *user_data)
+{
+ struct l_netconfig *nc = user_data;
+
+ switch (event) {
+ case L_DHCP6_CLIENT_EVENT_LEASE_OBTAINED:
+ if (L_WARN_ON(nc->v6_configured))
+ break;
+
+ netconfig_add_dhcp6_address(nc);
+ netconfig_set_dhcp6_address_lifetimes(nc, false);
+ nc->v6_configured = true;
+ netconfig_emit_event(nc, AF_INET6, L_NETCONFIG_EVENT_CONFIGURE);
+ break;
+ case L_DHCP6_CLIENT_EVENT_IP_CHANGED:
+ if (L_WARN_ON(!nc->v6_configured))
+ break;
+
+ netconfig_remove_dhcp6_address(nc);
+ netconfig_add_dhcp6_address(nc);
+ netconfig_set_dhcp6_address_lifetimes(nc, false);
+ netconfig_emit_event(nc, AF_INET6, L_NETCONFIG_EVENT_UPDATE);
+ break;
+ case L_DHCP6_CLIENT_EVENT_LEASE_EXPIRED:
+ if (L_WARN_ON(!nc->v6_configured))
+ break;
+
+ netconfig_remove_dhcp6_address(nc);
+ nc->v6_configured = false;
+
+ if (l_dhcp6_client_start(nc->dhcp6_client))
+ /* TODO: also start a new timeout */
+ netconfig_emit_event(nc, AF_INET6,
+ L_NETCONFIG_EVENT_UNCONFIGURE);
+ else
+ netconfig_emit_event(nc, AF_INET6,
+ L_NETCONFIG_EVENT_FAILED);
+
+ break;
+ case L_DHCP6_CLIENT_EVENT_LEASE_RENEWED:
+ if (L_WARN_ON(!nc->v6_configured))
+ break;
+
+ netconfig_set_dhcp6_address_lifetimes(nc, true);
+ netconfig_emit_event(nc, AF_INET6, L_NETCONFIG_EVENT_UPDATE);
+ break;
+ case L_DHCP6_CLIENT_EVENT_NO_LEASE:
+ if (L_WARN_ON(nc->v6_configured))
+ break;
+
+ /*
+ * The requested address is no longer available, try to restart
+ * the client.
+ *
+ * TODO: this may need to be delayed so we don't flood the
+ * network with SOLICITs and DECLINEs. Also add a retry limit
+ * or better yet a configurable timeout.
+ */
+ if (!l_dhcp6_client_start(nc->dhcp6_client))
+ netconfig_emit_event(nc, AF_INET6,
+ L_NETCONFIG_EVENT_FAILED);
+
+ break;
+ }
+}
+
static struct l_rtnl_route *netconfig_find_icmp6_route(
struct l_netconfig *nc,
const uint8_t *gateway,
@@ -621,6 +750,9 @@ LIB_EXPORT struct l_netconfig *l_netconfig_new(uint32_t ifindex)
nc, NULL);
nc->dhcp6_client = l_dhcp6_client_new(ifindex);
+ l_dhcp6_client_set_event_handler(nc->dhcp6_client,
+ netconfig_dhcp6_event_handler,
+ nc, NULL);
nc->icmp6_client = l_dhcp6_client_get_icmp6(nc->dhcp6_client);
l_icmp6_client_add_event_handler(nc->icmp6_client,
--
2.32.0
next reply other threads:[~2022-05-13 22:47 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-05-13 22:47 Andrew Zaborowski [this message]
-- strict thread matches above, loose matches on Subject: below --
2022-05-18 18:53 [PATCH 05/17] netconfig: Handle DHCPv6 events Denis Kenzior
2022-05-13 14:54 Andrew Zaborowski
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220513224713.1447773-5-andrew.zaborowski@intel.com \
--to=ell@lists.linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).