Skip to contentSkip to conversation

rtnl: Track route & address expiry times

PATCH Applied Andrew Zaborowski 2 participants 18 messages Mar 7, 2022 – Mar 11, 2022 Kbuild

lore
Share & tools
lore

Andrew Zaborowski

author
#0 lore raw

PATCH 1/12 rtnl: Track route & address expiry times

Store expiry times of addresses and routes in addition to the lifetimes so that at any time the current lifetime left can be calculated. The expiry time is normally the creation or renewal time + lifetime.

---
 ell/ell.sym |  4 +++
 ell/rtnl.c  | 76 ++++++++++++++++++++++++++++++++++++++++++++++++-----
 ell/rtnl.h  | 12 +++++++--
 3 files changed, 83 insertions(+), 9 deletions(-)
ell/ell.sym +4 -0
index 01aec31..1641506 100644
@@ -630,6 +630,8 @@ global:
 	l_rtnl_address_get_valid_lifetime;
 	l_rtnl_address_get_preferred_lifetime;
 	l_rtnl_address_set_lifetimes;
+	l_rtnl_address_get_expiry;
+	l_rtnl_address_set_expiry;
 	l_rtnl_address_set_scope;
 	l_rtnl_route_new_gateway;
 	l_rtnl_route_new_prefix;
@@ -639,6 +641,8 @@ global:
 	l_rtnl_route_get_dst;
 	l_rtnl_route_get_lifetime;
 	l_rtnl_route_set_lifetime;
+	l_rtnl_route_get_expiry;
+	l_rtnl_route_set_expiry;
 	l_rtnl_route_get_mtu;
 	l_rtnl_route_set_mtu;
 	l_rtnl_route_get_preference;
ell/rtnl.c +69 -7

154 lines not on this page · raw message

ell/rtnl.h +10 -2
index ebc6da8..bbd410e 100644
@@ -60,6 +60,12 @@ uint32_t l_rtnl_address_get_preferred_lifetime(
 bool l_rtnl_address_set_lifetimes(struct l_rtnl_address *addr,
 						uint32_t preferred_lifetime,
 						uint32_t valid_lifetime);
+bool l_rtnl_address_get_expiry(const struct l_rtnl_address *addr,
+				uint64_t *preferred_expiry_time,
+				uint64_t *valid_expiry_time);
+bool l_rtnl_address_set_expiry(struct l_rtnl_address *addr,
+				uint64_t preferred_expiry_time,
+				uint64_t valid_expiry_time);
 bool l_rtnl_address_set_scope(struct l_rtnl_address *addr, uint8_t scope);
 
 struct l_rtnl_route *l_rtnl_route_new_gateway(const char *gw);
@@ -69,10 +75,12 @@ void l_rtnl_route_free(struct l_rtnl_route *rt);
 DEFINE_CLEANUP_FUNC(l_rtnl_route_free);
 uint8_t l_rtnl_route_get_family(const struct l_rtnl_route *rt);
 bool l_rtnl_route_get_gateway(const struct l_rtnl_route *rt, char *out_buf);
-uint32_t l_rtnl_route_get_lifetime(const struct l_rtnl_route *rt);
-bool l_rtnl_route_set_lifetime(struct l_rtnl_route *rt, uint32_t lt);
 bool l_rtnl_route_get_dst(const struct l_rtnl_route *rt, char *out_buf,
 				uint8_t *out_prefix_len);
+uint32_t l_rtnl_route_get_lifetime(const struct l_rtnl_route *rt);
+bool l_rtnl_route_set_lifetime(struct l_rtnl_route *rt, uint32_t lt);
+uint64_t l_rtnl_route_get_expiry(const struct l_rtnl_route *rt);
+bool l_rtnl_route_set_expiry(struct l_rtnl_route *rt, uint64_t expiry_time);
 uint32_t l_rtnl_route_get_mtu(const struct l_rtnl_route *rt);
 bool l_rtnl_route_set_mtu(struct l_rtnl_route *rt, uint32_t mtu);
 uint8_t l_rtnl_route_get_preference(const struct l_rtnl_route *rt);
Signature · 1 line
2.32.0
author
#1 lore raw

PATCH 2/12 dhcp: Set lease expiry based on frame reception times

Use the SO_TIMESTAMP socket option to request the packet rx timestamps from the kernel and pass those values from dhcp-transport to dhcp-client/server to use in lease expiry time calculation.

Apparently linux has a few different variants of SO_TIMESTAMP, use the most basic one. Additionally the rx times can be obtained without SO_TIMESTAMP using the SIOCGSTAMP ioctl on each packet. The advantage would be that we'd only parse the timestamps for the frames where we actually care about timing. The disadvantage would be an extra syscall for those frames, which we don't incur with SO_TIMESTAMP.

---
 ell/dhcp-private.h   |  2 +-
 ell/dhcp-server.c    | 29 +++++++++++++++++------------
 ell/dhcp-transport.c | 41 +++++++++++++++++++++++++++++++++++------
 ell/dhcp.c           | 12 ++++++++----
 4 files changed, 61 insertions(+), 23 deletions(-)
ell/dhcp-private.h +1 -1
index 1c12cbe..b695e4e 100644
@@ -108,7 +108,7 @@ uint16_t _dhcp_checksum(const void *buf, size_t len);
 uint16_t _dhcp_checksumv(const struct iovec *iov, size_t iov_cnt);
 
 typedef void (*dhcp_transport_rx_cb_t)(const void *, size_t, void *,
-					const uint8_t *);
+					const uint8_t *, uint64_t);
 
 struct dhcp_transport {
 	int (*open)(struct dhcp_transport *s, uint32_t xid);
ell/dhcp-server.c +17 -12
index ff0784f..5c0774b 100644
@@ -299,7 +299,8 @@ static void lease_expired_cb(struct l_timeout *timeout, void *user_data)
 
 static struct l_dhcp_lease *add_lease(struct l_dhcp_server *server,
 					bool offering, const uint8_t *client_id,
-					const uint8_t *chaddr, uint32_t yiaddr)
+					const uint8_t *chaddr, uint32_t yiaddr,
+					uint64_t timestamp)
 {
 	struct l_dhcp_lease *lease = NULL;
 	int ret;
@@ -328,7 +329,7 @@ static struct l_dhcp_lease *add_lease(struct l_dhcp_server *server,
 		lease->client_id = l_memdup(client_id, client_id[0] + 1);
 
 	lease->offering = offering;
-	lease->bound_time = l_time_now();
+	lease->bound_time = timestamp;
 
 	if (!offering) {
 		lease->lifetime = server->lease_seconds;
@@ -572,7 +573,7 @@ static void copy_client_id(struct dhcp_message_builder *builder,
 static void send_offer(struct l_dhcp_server *server,
 			const struct dhcp_message *client_msg,
 			struct l_dhcp_lease *lease, uint32_t requested_ip,
-			const uint8_t *client_id)
+			const uint8_t *client_id, uint64_t timestamp)
 {
 	struct dhcp_message_builder builder;
 	size_t len = sizeof(struct dhcp_message) + DHCP_MIN_OPTIONS_SIZE;
@@ -595,7 +596,7 @@ static void send_offer(struct l_dhcp_server *server,
 	}
 
 	lease = add_lease(server, true, client_id, client_msg->chaddr,
-				reply->yiaddr);
+				reply->yiaddr, timestamp);
 	if (!lease) {
 		SERVER_DEBUG("add_lease() failed");
 		return;
@@ -668,7 +669,8 @@ static void send_nak(struct l_dhcp_server *server,
 static void send_ack(struct l_dhcp_server *server,
 			const struct dhcp_message *client_msg,
 			struct l_dhcp_lease *lease,
-			bool rapid_commit)
+			bool rapid_commit,
+			uint64_t timestamp)
 {
 	struct dhcp_message_builder builder;
 	size_t len = sizeof(struct dhcp_message) + DHCP_MIN_OPTIONS_SIZE;
@@ -706,7 +708,7 @@ static void send_ack(struct l_dhcp_server *server,
 		return;
 
 	lease = add_lease(server, false, client_id, reply->chaddr,
-				reply->yiaddr);
+				reply->yiaddr, timestamp);
 
 	if (server->event_handler)
 		server->event_handler(server, L_DHCP_SERVER_EVENT_NEW_LEASE,
@@ -714,7 +716,7 @@ static void send_ack(struct l_dhcp_server *server,
 }
 
 static void listener_event(const void *data, size_t len, void *user_data,
-				const uint8_t *saddr)
+				const uint8_t *saddr, uint64_t timestamp)
 {
 	struct l_dhcp_server *server = user_data;
 	const struct dhcp_message *message = data;
@@ -803,12 +805,13 @@ static void listener_event(const void *data, size_t len, void *user_data,
 				break;
 			}
 
-			send_ack(server, message, lease, rapid_commit_opt);
+			send_ack(server, message, lease, rapid_commit_opt,
+					timestamp);
 			break;
 		}
 
 		send_offer(server, message, lease, requested_ip_opt,
-				client_id_opt);
+				client_id_opt, timestamp);
 		break;
 	case DHCP_MESSAGE_TYPE_REQUEST:
 		SERVER_DEBUG("Received REQUEST, requested IP "NIPQUAD_FMT,
@@ -903,7 +906,7 @@ static void listener_event(const void *data, size_t len, void *user_data,
 			}
 		}
 
-		send_ack(server, message, lease, false);
+		send_ack(server, message, lease, false, timestamp);
 		break;
 	case DHCP_MESSAGE_TYPE_DECLINE:
 		SERVER_DEBUG("Received DECLINE");
@@ -1350,7 +1353,8 @@ LIB_EXPORT struct l_dhcp_lease *l_dhcp_server_discover(
 		}
 	}
 
-	lease = add_lease(server, true, client_id, mac, requested_ip_opt);
+	lease = add_lease(server, true, client_id, mac, requested_ip_opt,
+				l_time_now());
 	if (unlikely(!lease)) {
 		SERVER_DEBUG("add_lease() failed");
 		return NULL;
@@ -1373,7 +1377,8 @@ LIB_EXPORT bool l_dhcp_server_request(struct l_dhcp_server *server,
 			NIPQUAD(lease->address), MAC_STR(lease->mac));
 
 	memcpy(mac, lease->mac, ETH_ALEN);
-	lease = add_lease(server, false, NULL, mac, lease->address);
+	lease = add_lease(server, false, NULL, mac, lease->address,
+				lease->bound_time);
 
 	if (server->event_handler)
 		server->event_handler(server, L_DHCP_SERVER_EVENT_NEW_LEASE,
ell/dhcp-transport.c +35 -6

77 lines not on this page · raw message

ell/dhcp.c +8 -4
index e2d18fc..2d04900 100644
@@ -675,7 +675,7 @@ static void dhcp_client_address_add_cb(int error, uint16_t type,
 static int dhcp_client_receive_ack(struct l_dhcp_client *client,
 					const uint8_t *saddr,
 					const struct dhcp_message *ack,
-					size_t len)
+					size_t len, uint64_t timestamp)
 {
 	struct dhcp_message_iter iter;
 	struct l_dhcp_lease *lease;
@@ -727,6 +727,7 @@ static int dhcp_client_receive_ack(struct l_dhcp_client *client,
 		uint32_t l = l_dhcp_lease_get_lifetime(client->lease);
 		L_AUTO_FREE_VAR(char *, broadcast) =
 				l_dhcp_lease_get_broadcast(client->lease);
+		uint64_t et = timestamp + l * L_USEC_PER_SEC;
 
 		prefix_len = l_dhcp_lease_get_prefix_length(client->lease);
 		if (!prefix_len)
@@ -735,6 +736,7 @@ static int dhcp_client_receive_ack(struct l_dhcp_client *client,
 		a = l_rtnl_address_new(ip, prefix_len);
 		l_rtnl_address_set_noprefixroute(a, true);
 		l_rtnl_address_set_lifetimes(a, l, l);
+		l_rtnl_address_set_expiry(a, et, et);
 		l_rtnl_address_set_broadcast(a, broadcast);
 
 		client->rtnl_add_cmdid =
@@ -817,7 +819,8 @@ static bool dhcp_client_handle_offer(struct l_dhcp_client *client,
 }
 
 static void dhcp_client_rx_message(const void *data, size_t len, void *userdata,
-					const uint8_t *saddr)
+					const uint8_t *saddr,
+					uint64_t timestamp)
 {
 	struct l_dhcp_client *client = userdata;
 	const struct dhcp_message *message = data;
@@ -898,7 +901,8 @@ static void dhcp_client_rx_message(const void *data, size_t len, void *userdata,
 		if (msg_type != DHCP_MESSAGE_TYPE_ACK)
 			return;
 
-		r = dhcp_client_receive_ack(client, saddr, message, len);
+		r = dhcp_client_receive_ack(client, saddr, message, len,
+						timestamp);
 		if (r < 0)
 			return;
 
@@ -917,7 +921,7 @@ static void dhcp_client_rx_message(const void *data, size_t len, void *userdata,
 
 		dhcp_client_event_notify(client, r);
 
-		client->lease->bound_time = l_time_now();
+		client->lease->bound_time = timestamp;
 
 		/*
 		 * Start T1, once it expires we will start the T2 timer.  If
Signature · 1 line
2.32.0
author
#2 lore raw

PATCH 3/12 dhcp: Add l_dhcp_lease_get_start_time

In reply to Andrew Zaborowski

Add a getter for the rx timestamp of the lease ACK, i.e. the time from which the lifetime period is counted.

---
 ell/dhcp-lease.c | 9 +++++++++
 ell/dhcp.h       | 1 +
 ell/ell.sym      | 1 +
 3 files changed, 11 insertions(+)
ell/dhcp-lease.c +9 -0
index bc4ff74..b618f11 100644
@@ -351,6 +351,15 @@ LIB_EXPORT uint32_t l_dhcp_lease_get_lifetime(const struct l_dhcp_lease *lease)
 	return lease->lifetime;
 }
 
+LIB_EXPORT uint64_t l_dhcp_lease_get_start_time(
+					const struct l_dhcp_lease *lease)
+{
+	if (unlikely(!lease))
+		return 0;
+
+	return lease->bound_time;
+}
+
 LIB_EXPORT const uint8_t *l_dhcp_lease_get_mac(const struct l_dhcp_lease *lease)
 {
 	if (unlikely(!lease))
ell/dhcp.h +1 -0
index 4aa4a0a..946bf63 100644
@@ -122,6 +122,7 @@ const uint8_t *l_dhcp_lease_get_mac(const struct l_dhcp_lease *lease);
 uint32_t l_dhcp_lease_get_t1(const struct l_dhcp_lease *lease);
 uint32_t l_dhcp_lease_get_t2(const struct l_dhcp_lease *lease);
 uint32_t l_dhcp_lease_get_lifetime(const struct l_dhcp_lease *lease);
+uint64_t l_dhcp_lease_get_start_time(const struct l_dhcp_lease *lease);
 
 struct l_dhcp_server *l_dhcp_server_new(int ifindex);
 void l_dhcp_server_destroy(struct l_dhcp_server *server);
ell/ell.sym +1 -0
index 1641506..2fb5c55 100644
@@ -240,6 +240,7 @@ global:
 	l_dhcp_lease_get_t1;
 	l_dhcp_lease_get_t2;
 	l_dhcp_lease_get_lifetime;
+	l_dhcp_lease_get_start_time;
 	l_dhcp_client_new;
 	l_dhcp_client_destroy;
 	l_dhcp_client_add_request_option;
Signature · 1 line
2.32.0
author
#3 lore raw

PATCH 4/12 dhcp6: Set lease expiry based on frame reception times

In reply to Andrew Zaborowski

Use the SO_TIMESTAMP socket option to request the packet rx timestamps from the kernel and pass those values from dhcp6-transport to dhcp6-client to use in lease expiry time calculation.

Apparently linux has a few different variants of SO_TIMESTAMP, use the most basic one. Additionally the rx times can be obtained without SO_TIMESTAMP using the SIOCGSTAMP ioctl on each packet. The advantage would be that we'd only parse the timestamps for the frames where we actually care about timing. The disadvantage would be an extra syscall for those frames, which we don't incur with SO_TIMESTAMP.

---
 ell/dhcp6-private.h   |  2 +-
 ell/dhcp6-transport.c | 36 ++++++++++++++++++++++++++++++++----
 ell/dhcp6.c           | 11 +++++++----
 3 files changed, 40 insertions(+), 9 deletions(-)
ell/dhcp6-private.h +1 -1
index 2bbd1d3..e27cb7e 100644
@@ -80,7 +80,7 @@ struct dhcp6_option_iter {
 	uint16_t max;
 };
 
-typedef void (*dhcp6_transport_rx_cb_t)(const void *, size_t, void *);
+typedef void (*dhcp6_transport_rx_cb_t)(const void *, size_t, uint64_t, void *);
 
 struct dhcp6_transport {
 	int (*open)(struct dhcp6_transport *s);
ell/dhcp6-transport.c +32 -4
index feb890f..04bf4fb 100644
@@ -39,6 +39,7 @@
 #include "private.h"
 #include "missing.h"
 #include "io.h"
+#include "time.h"
 #include "dhcp6-private.h"
 
 struct dhcp6_default_transport {
@@ -56,14 +57,38 @@ static bool _dhcp6_default_transport_read_handler(struct l_io *io,
 	int fd = l_io_get_fd(io);
 	char buf[2048];
 	ssize_t len;
-
-	len = read(fd, buf, sizeof(buf));
+	uint64_t timestamp = 0;
+	struct cmsghdr *cmsg;
+	struct iovec iov = { .iov_base = buf, .iov_len = sizeof(buf) };
+	struct msghdr msg = {};
+	unsigned char control[32];
+
+	msg.msg_iov = &iov;
+	msg.msg_iovlen = 1;
+	msg.msg_control = control;
+	msg.msg_controllen = sizeof(control);
+
+	len = recvmsg(fd, &msg, 0);
 	if (len < 0)
 		return false;
 
-	if (transport->super.rx_cb)
-		transport->super.rx_cb(&buf, len, transport->super.rx_data);
+	if (!transport->super.rx_cb)
+		return true;
+
+	for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
+					cmsg = CMSG_NXTHDR(&msg, cmsg)) {
+		if (cmsg->cmsg_level == SOL_SOCKET &&
+				cmsg->cmsg_type == SCM_TIMESTAMP) {
+			const struct timeval *tv = (void *) CMSG_DATA(cmsg);
+
+			timestamp = tv->tv_sec * L_USEC_PER_SEC + tv->tv_usec;
+		}
+	}
+
+	if (!timestamp)
+		timestamp = l_time_now();
 
+	transport->super.rx_cb(&buf, len, timestamp, transport->super.rx_data);
 	return true;
 }
 
@@ -146,6 +171,9 @@ static int kernel_raw_socket_open(uint32_t ifindex,
 			goto error;
 	}
 
+	if (setsockopt(s, SOL_SOCKET, SO_TIMESTAMP, &yes, sizeof(yes)) < 0)
+		goto error;
+
 	if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0)
 		goto error;
 
ell/dhcp6.c +7 -4
index 40c6f3a..34635fe 100644
@@ -962,13 +962,14 @@ static void dhcp6_client_address_add_cb(int error, uint16_t type,
 	}
 }
 
-static void dhcp6_client_setup_lease(struct l_dhcp6_client *client)
+static void dhcp6_client_setup_lease(struct l_dhcp6_client *client,
+					uint64_t timestamp)
 {
 	uint32_t t1 = _dhcp6_lease_get_t1(client->lease);
 	uint32_t t2 = _dhcp6_lease_get_t2(client->lease);
 	enum l_dhcp6_client_event event;
 
-	client->lease_start_t = l_time_now();
+	client->lease_start_t = timestamp;
 
 	/* TODO: Emit IP_CHANGED if any addresses were removed / added */
 	if (client->state == DHCP6_STATE_REQUESTING ||
@@ -1008,6 +1009,8 @@ static void dhcp6_client_setup_lease(struct l_dhcp6_client *client)
 		a = l_rtnl_address_new(ip, prefix_len);
 		l_rtnl_address_set_noprefixroute(a, true);
 		l_rtnl_address_set_lifetimes(a, p, v);
+		l_rtnl_address_set_expiry(a, timestamp + p * L_USEC_PER_SEC,
+						timestamp + v * L_USEC_PER_SEC);
 
 		client->rtnl_add_cmdid =
 			l_rtnl_ifaddr_add(client->rtnl, client->ifindex, a,
@@ -1353,7 +1356,7 @@ bad_lease:
 }
 
 static void dhcp6_client_rx_message(const void *data, size_t len,
-								void *userdata)
+					uint64_t timestamp, void *userdata)
 {
 	struct l_dhcp6_client *client = userdata;
 	const struct dhcp6_message *message = data;
@@ -1413,7 +1416,7 @@ static void dhcp6_client_rx_message(const void *data, size_t len,
 	if (r == DHCP6_STATE_BOUND) {
 		l_timeout_remove(client->timeout_send);
 		client->timeout_send = NULL;
-		dhcp6_client_setup_lease(client);
+		dhcp6_client_setup_lease(client, timestamp);
 		return;
 	}
 
Signature · 1 line
2.32.0
author
#4 lore raw

PATCH 5/12 dhcp6: Expose lease lifetime & start time getters

In reply to Andrew Zaborowski
---
 ell/dhcp6-lease.c   | 33 +++++++++++++++++++++++++++------
 ell/dhcp6-private.h |  6 ++++--
 ell/dhcp6.c         | 11 +++++------
 ell/dhcp6.h         |  3 +++
 ell/ell.sym         |  3 +++
 5 files changed, 42 insertions(+), 14 deletions(-)
ell/dhcp6-lease.c +27 -6
index c30c02b..827c71e 100644
@@ -408,14 +408,35 @@ uint32_t _dhcp6_lease_get_t2(struct l_dhcp6_lease *lease)
 	return ia->info.valid_lifetime /  10 * 8;
 }
 
-uint32_t _dhcp6_lease_get_valid_lifetime(struct l_dhcp6_lease *lease)
+LIB_EXPORT uint32_t l_dhcp6_lease_get_valid_lifetime(
+						struct l_dhcp6_lease *lease)
 {
-	PICK_IA();
-	return ia->info.valid_lifetime;
+	if (unlikely(!lease))
+		return 0;
+
+	{
+		PICK_IA();
+		return ia->info.valid_lifetime;
+	}
 }
 
-uint32_t _dhcp6_lease_get_preferred_lifetime(struct l_dhcp6_lease *lease)
+LIB_EXPORT uint32_t l_dhcp6_lease_get_preferred_lifetime(
+						struct l_dhcp6_lease *lease)
 {
-	PICK_IA();
-	return ia->info.preferred_lifetime;
+	if (unlikely(!lease))
+		return 0;
+
+	{
+		PICK_IA();
+		return ia->info.preferred_lifetime;
+	}
+}
+
+/* Get the reception timestamp, i.e. when lifetimes are counted from */
+LIB_EXPORT uint64_t l_dhcp6_lease_get_start_time(struct l_dhcp6_lease *lease)
+{
+	if (unlikely(!lease))
+		return 0;
+
+	return lease->start_time;
 }
ell/dhcp6-private.h +4 -2
index e27cb7e..52f4002 100644
@@ -130,6 +130,7 @@ struct l_dhcp6_lease {
 	uint8_t *server_id;
 	size_t server_id_len;
 	uint8_t preference;
+	uint64_t start_time;
 
 	struct dhcp6_ia ia_na;
 	struct dhcp6_ia ia_pd;
@@ -149,5 +150,6 @@ struct l_dhcp6_lease *_dhcp6_lease_parse_options(
 					const uint8_t expected_iaid[static 4]);
 uint32_t _dhcp6_lease_get_t1(struct l_dhcp6_lease *lease);
 uint32_t _dhcp6_lease_get_t2(struct l_dhcp6_lease *lease);
-uint32_t _dhcp6_lease_get_valid_lifetime(struct l_dhcp6_lease *lease);
-uint32_t _dhcp6_lease_get_preferred_lifetime(struct l_dhcp6_lease *lease);
+uint32_t dhcp6_lease_get_valid_lifetime(struct l_dhcp6_lease *lease);
+uint32_t dhcp6_lease_get_preferred_lifetime(struct l_dhcp6_lease *lease);
+uint32_t dhcp6_lease_get_start_time(struct l_dhcp6_lease *lease);
ell/dhcp6.c +5 -6
index 34635fe..424fad0 100644
@@ -394,7 +394,6 @@ struct l_dhcp6_client {
 	struct l_timeout *timeout_send;
 	struct l_dhcp6_lease *lease;
 	struct l_timeout *timeout_lease;
-	uint64_t lease_start_t;
 
 	struct l_icmp6_client *icmp6;
 
@@ -901,8 +900,8 @@ static void dhcp6_client_t2_expired(struct l_timeout *timeout, void *user_data)
 {
 	struct l_dhcp6_client *client = user_data;
 	uint32_t elapsed =
-		l_time_to_msecs(l_time_now() - client->lease_start_t);
-	uint32_t valid = _dhcp6_lease_get_valid_lifetime(client->lease) *
+		l_time_to_msecs(l_time_now() - client->lease->start_time);
+	uint32_t valid = l_dhcp6_lease_get_valid_lifetime(client->lease) *
 				L_MSEC_PER_SEC;
 
 	CLIENT_DEBUG("");
@@ -969,7 +968,7 @@ static void dhcp6_client_setup_lease(struct l_dhcp6_client *client,
 	uint32_t t2 = _dhcp6_lease_get_t2(client->lease);
 	enum l_dhcp6_client_event event;
 
-	client->lease_start_t = timestamp;
+	client->lease->start_time = timestamp;
 
 	/* TODO: Emit IP_CHANGED if any addresses were removed / added */
 	if (client->state == DHCP6_STATE_REQUESTING ||
@@ -1003,8 +1002,8 @@ static void dhcp6_client_setup_lease(struct l_dhcp6_client *client,
 			l_dhcp6_lease_get_address(client->lease);
 		uint8_t prefix_len =
 			l_dhcp6_lease_get_prefix_length(client->lease);
-		uint32_t p = _dhcp6_lease_get_preferred_lifetime(client->lease);
-		uint32_t v = _dhcp6_lease_get_valid_lifetime(client->lease);
+		uint32_t p = l_dhcp6_lease_get_preferred_lifetime(client->lease);
+		uint32_t v = l_dhcp6_lease_get_valid_lifetime(client->lease);
 
 		a = l_rtnl_address_new(ip, prefix_len);
 		l_rtnl_address_set_noprefixroute(a, true);
ell/dhcp6.h +3 -0
index 8503a5e..9819fad 100644
@@ -94,6 +94,9 @@ char *l_dhcp6_lease_get_address(const struct l_dhcp6_lease *lease);
 char **l_dhcp6_lease_get_dns(const struct l_dhcp6_lease *lease);
 char **l_dhcp6_lease_get_domains(const struct l_dhcp6_lease *lease);
 uint8_t l_dhcp6_lease_get_prefix_length(const struct l_dhcp6_lease *lease);
+uint32_t l_dhcp6_lease_get_valid_lifetime(struct l_dhcp6_lease *lease);
+uint32_t l_dhcp6_lease_get_preferred_lifetime(struct l_dhcp6_lease *lease);
+uint64_t l_dhcp6_lease_get_start_time(struct l_dhcp6_lease *lease);
 
 #ifdef __cplusplus
 }
ell/ell.sym +3 -0
index 2fb5c55..db29d9a 100644
@@ -296,6 +296,9 @@ global:
 	l_dhcp6_lease_get_dns;
 	l_dhcp6_lease_get_domains;
 	l_dhcp6_lease_get_prefix_length;
+	l_dhcp6_lease_get_valid_lifetime;
+	l_dhcp6_lease_get_preferred_lifetime;
+	l_dhcp6_lease_get_start_time;
 	/* dir */
 	l_dir_watch_new;
 	l_dir_watch_destroy;
Signature · 1 line
2.32.0
author
#5 lore raw

PATCH 6/12 icmp6: Use SO_TIMESTAMP

In reply to Andrew Zaborowski

Like in dhcp and dhcp6 set the SO_TIMESTAMP socket option to get kernel packet reception times for router advertisements and save it.

---
 ell/icmp6-private.h |  4 +++-
 ell/icmp6.c         | 29 +++++++++++++++++++++++------
 2 files changed, 26 insertions(+), 7 deletions(-)
ell/icmp6-private.h +3 -1
index 3846bad..2433087 100644
@@ -33,6 +33,7 @@ struct l_icmp6_router {
 	bool managed : 1;
 	bool other : 1;
 	uint8_t pref;
+	uint64_t start_time;
 	uint16_t lifetime;
 	uint32_t mtu;
 	uint32_t n_prefixes;
@@ -43,4 +44,5 @@ struct l_icmp6_router *_icmp6_router_new();
 void _icmp6_router_free(struct l_icmp6_router *r);
 struct l_icmp6_router *_icmp6_router_parse(const struct nd_router_advert *ra,
 						size_t len,
-						const uint8_t src[static 16]);
+						const uint8_t src[static 16],
+						uint64_t timestamp);
ell/icmp6.c +23 -6
index 3658408..b0f8727 100644
@@ -126,6 +126,10 @@ static int icmp6_open_router_common(const struct icmp6_filter *filter,
 				ifr.ifr_name, strlen(ifr.ifr_name) + 1);
 	}
 
+	if (r < 0)
+		goto fail;
+
+	r = setsockopt(s, SOL_SOCKET, SO_TIMESTAMP, &yes, sizeof(yes));
 	if (r < 0)
 		goto fail;
 
@@ -191,7 +195,8 @@ static int icmp6_send_router_solicitation(int s, const uint8_t mac[static 6])
 	return 0;
 }
 
-static int icmp6_receive(int s, void *buf, size_t buf_len, struct in6_addr *src)
+static int icmp6_receive(int s, void *buf, size_t buf_len, struct in6_addr *src,
+				uint64_t *out_timestamp)
 {
 	char c_msg_buf[CMSG_SPACE(sizeof(int))];
 	struct iovec iov = {
@@ -210,6 +215,7 @@ static int icmp6_receive(int s, void *buf, size_t buf_len, struct in6_addr *src)
 	};
 	struct cmsghdr *cmsg;
 	ssize_t l;
+	uint64_t timestamp = 0;
 
 	l = recvmsg(s, &msg, MSG_DONTWAIT);
 	if (l < 0)
@@ -230,10 +236,16 @@ static int icmp6_receive(int s, void *buf, size_t buf_len, struct in6_addr *src)
 
 			if (hops != 255)
 				return -EMULTIHOP;
+		} else if (cmsg->cmsg_level == SOL_SOCKET &&
+				cmsg->cmsg_type == SCM_TIMESTAMP) {
+			const struct timeval *tv = (void *) CMSG_DATA(cmsg);
+
+			timestamp = tv->tv_sec * L_USEC_PER_SEC + tv->tv_usec;
 		}
 	}
 
 	memcpy(src, &saddr.sin6_addr, sizeof(saddr.sin6_addr));
+	*out_timestamp = timestamp ?: l_time_now();
 	return 0;
 }
 
@@ -344,9 +356,11 @@ static void icmp6_client_setup_routes(struct l_icmp6_client *client)
 static int icmp6_client_handle_message(struct l_icmp6_client *client,
 						struct nd_router_advert *ra,
 						size_t len,
-						const struct in6_addr *src)
+						const struct in6_addr *src,
+						uint64_t timestamp)
 {
-	struct l_icmp6_router *r = _icmp6_router_parse(ra, len, src->s6_addr);
+	struct l_icmp6_router *r =
+		_icmp6_router_parse(ra, len, src->s6_addr, timestamp);
 
 	if (!r)
 		return -EBADMSG;
@@ -382,6 +396,7 @@ static bool icmp6_client_read_handler(struct l_io *io, void *userdata)
 	ssize_t l;
 	struct in6_addr src;
 	int r;
+	uint64_t timestamp = 0;
 
 	/* Poke to see how many bytes we need to read / alloc */
 	l = recv(s, NULL, 0, MSG_PEEK|MSG_TRUNC);
@@ -392,7 +407,7 @@ static bool icmp6_client_read_handler(struct l_io *io, void *userdata)
 	}
 
 	ra = l_malloc(l);
-	if (icmp6_receive(s, ra, l, &src) < 0)
+	if (icmp6_receive(s, ra, l, &src, &timestamp) < 0)
 		goto done;
 
 	if ((size_t) l < sizeof(struct nd_router_advert)) {
@@ -400,7 +415,7 @@ static bool icmp6_client_read_handler(struct l_io *io, void *userdata)
 		goto done;
 	}
 
-	r = icmp6_client_handle_message(client, ra, l, &src);
+	r = icmp6_client_handle_message(client, ra, l, &src, timestamp);
 	if (r < 0)
 		goto done;
 
@@ -661,7 +676,8 @@ void _icmp6_router_free(struct l_icmp6_router *r)
 
 struct l_icmp6_router *_icmp6_router_parse(const struct nd_router_advert *ra,
 						size_t len,
-						const uint8_t src[static 16])
+						const uint8_t src[static 16],
+						uint64_t timestamp)
 {
 	struct l_icmp6_router *r;
 	const uint8_t *opts;
@@ -726,6 +742,7 @@ struct l_icmp6_router *_icmp6_router_parse(const struct nd_router_advert *ra,
 	if (r->pref == 0x2) /* If invalid, reset to medium */
 		r->pref = 0;
 
+	r->start_time = timestamp;
 	r->lifetime = L_BE16_TO_CPU(ra->nd_ra_router_lifetime);
 
 	opts = (uint8_t *) (ra + 1);
Signature · 1 line
2.32.0
author
#6 lore raw

PATCH 7/12 icmp6: Add router lifetime and rx timestamp getters

In reply to Andrew Zaborowski

Add l_icmp6_router getters for the current lifetime and rx timestamp (when lifetime is counted from) so that netconfig.c can build route information from the l_icmp6 API. Indent a comment in ell.sym consistent with surrounding lines.

---
 ell/ell.sym |  4 +++-
 ell/icmp6.c | 17 +++++++++++++++++
 ell/icmp6.h |  2 ++
 3 files changed, 22 insertions(+), 1 deletion(-)
ell/ell.sym +3 -1
index db29d9a..e2a278f 100644
@@ -685,7 +685,7 @@ global:
 	l_rtnl_ifaddr_delete;
 	l_rtnl_neighbor_get_hwaddr;
 	l_rtnl_neighbor_set_hwaddr;
-/* icmp6 */
+	/* icmp6 */
 	l_icmp6_client_new;
 	l_icmp6_client_free;
 	l_icmp6_client_start;
@@ -700,6 +700,8 @@ global:
 	l_icmp6_router_get_address;
 	l_icmp6_router_get_managed;
 	l_icmp6_router_get_other;
+	l_icmp6_router_get_lifetime;
+	l_icmp6_router_get_start_time;
 	/* acd */
 	l_acd_new;
 	l_acd_start;
ell/icmp6.c +17 -0
index b0f8727..e74b6d6 100644
@@ -815,3 +815,20 @@ LIB_EXPORT bool l_icmp6_router_get_other(const struct l_icmp6_router *r)
 
 	return r->other;
 }
+
+LIB_EXPORT uint16_t l_icmp6_router_get_lifetime(const struct l_icmp6_router *r)
+{
+	if (unlikely(!r))
+		return false;
+
+	return r->lifetime;
+}
+
+/* Get the reception timestamp, i.e. when lifetime is counted from */
+LIB_EXPORT uint64_t l_icmp6_router_get_start_time(const struct l_icmp6_router *r)
+{
+	if (unlikely(!r))
+		return false;
+
+	return r->start_time;
+}
ell/icmp6.h +2 -0
index 9ce2e24..615dba4 100644
@@ -69,6 +69,8 @@ bool l_icmp6_client_set_route_priority(struct l_icmp6_client *client,
 char *l_icmp6_router_get_address(const struct l_icmp6_router *r);
 bool l_icmp6_router_get_managed(const struct l_icmp6_router *r);
 bool l_icmp6_router_get_other(const struct l_icmp6_router *r);
+uint16_t l_icmp6_router_get_lifetime(const struct l_icmp6_router *r);
+uint64_t l_icmp6_router_get_start_time(const struct l_icmp6_router *r);
 
 #ifdef __cplusplus
 }
Signature · 1 line
2.32.0
author
#7 lore raw

PATCH 8/12 unit: Update dhcp{,6} receive handler arguments

In reply to Andrew Zaborowski
---
 unit/test-dhcp.c  | 20 +++++++++++---------
 unit/test-dhcp6.c |  2 +-
 2 files changed, 12 insertions(+), 10 deletions(-)
unit/test-dhcp.c +11 -9
index 28f763a..e734e03 100644
@@ -721,7 +721,7 @@ static void test_discover(const void *data)
 	assert(dhcp_message_compare(discover_data_1, sizeof(discover_data_1),
 					client_packet, client_packet_len));
 
-	transport->rx_cb(offer_data_1, sizeof(offer_data_1), client, NULL);
+	transport->rx_cb(offer_data_1, sizeof(offer_data_1), client, NULL, 0);
 
 	assert(client_send_called);
 	client_send_called = false;
@@ -729,7 +729,7 @@ static void test_discover(const void *data)
 					client_packet, client_packet_len));
 
 	event_handler_called = false;
-	transport->rx_cb(ack_data_1, sizeof(ack_data_1), client, NULL);
+	transport->rx_cb(ack_data_1, sizeof(ack_data_1), client, NULL, 0);
 	assert(!client_send_called);
 	assert(event_handler_called);
 
@@ -839,26 +839,26 @@ static void client_connect(struct l_dhcp_client *client,
 
 	/* RX DISCOVER */
 	srv_transport->rx_cb(client_packet, client_packet_len, server,
-				cli_addr);
+				cli_addr, 0);
 	assert(l2_send_called);
 	l2_send_called = false;
 
 	if (!rapid_commit) {
 		/* RX OFFER */
 		cli_transport->rx_cb(server_packet, server_packet_len, client,
-					NULL);
+					NULL, 0);
 		assert(client_send_called);
 		client_send_called = false;
 
 		/* RX REQUEST */
 		srv_transport->rx_cb(client_packet, client_packet_len, server,
-					cli_addr);
+					cli_addr, 0);
 		assert(l2_send_called);
 		l2_send_called = false;
 	}
 
 	/* RX ACK */
-	cli_transport->rx_cb(server_packet, server_packet_len, client, NULL);
+	cli_transport->rx_cb(server_packet, server_packet_len, client, NULL, 0);
 	assert(!client_send_called);
 
 	assert(event_handler_called);
@@ -974,7 +974,8 @@ static void test_complete_run(const void *data)
 	l_dhcp_client_stop(client1);
 	assert(client_send_called);
 	client_send_called = false;
-	srv_transport->rx_cb(client_packet, client_packet_len, server, addr1);
+	srv_transport->rx_cb(client_packet, client_packet_len, server, addr1,
+				0);
 	assert(expired_client);
 	assert(!strcmp(expired_client, "192.168.1.2"));
 	l_free(expired_client);
@@ -983,7 +984,8 @@ static void test_complete_run(const void *data)
 	l_dhcp_client_stop(client2);
 	assert(client_send_called);
 	client_send_called = false;
-	srv_transport->rx_cb(client_packet, client_packet_len, server, addr2);
+	srv_transport->rx_cb(client_packet, client_packet_len, server, addr2,
+				0);
 	assert(expired_client);
 	assert(!strcmp(expired_client, "192.168.1.3"));
 	l_free(expired_client);
@@ -1032,7 +1034,7 @@ static void test_expired_ip_reuse(const void *data)
 		assert(client_send_called);
 		client_send_called = false;
 		srv_transport->rx_cb(client_packet, client_packet_len, server,
-					addr);
+					addr, 0);
 		l_free(expired_client);
 		expired_client = NULL;
 	}
unit/test-dhcp6.c +1 -1
index d7cea7c..1f6522b 100644
@@ -235,7 +235,7 @@ static void event_handler_lease_obtained(struct l_dhcp6_client *client,
 	memcpy(transaction_id, client_packet + 1, sizeof(transaction_id)); \
 	memcpy(client_packet, packet, sizeof(packet)); \
 	memcpy(client_packet + 1, transaction_id, sizeof(transaction_id)); \
-	transport->rx_cb(client_packet, sizeof(packet), client) \
+	transport->rx_cb(client_packet, sizeof(packet), 0, client) \
 
 static void test_obtain_lease(const void *data)
 {
Signature · 1 line
2.32.0
author
#8 lore raw

PATCH 9/12 netconfig: Add netconfig API

In reply to Andrew Zaborowski

This adds the netconfig class that will encapsulate static IP configuration, DHCP, DHCP6, ICMP6, SLAAC logic and free the user of manually handling DHCP/DHCP6 events to implement netconfig policies. The user will still need to handle committing received DNS and mdns information.

In this patch only IPv4 DHCP is implemented and no configurable settings are added.

When events are emitted for address and/or route changes, the changes are split into "added", "updated" and "removed" lists so that the user can decide apply those changes without first removing all addresses and routes from the interface and the re-adding the complete new list. They can also decide to let another entity apply the changes. The obvious downside is the proliferation of lists.

---
 Makefile.am     |   6 +-
 ell/ell.h       |   1 +
 ell/ell.sym     |   7 +
 ell/netconfig.c | 377 ++++++++++++++++++++++++++++++++++++++++++++++++
 ell/netconfig.h |  72 +++++++++
 5 files changed, 461 insertions(+), 2 deletions(-)
 create mode 100644 ell/netconfig.c
 create mode 100644 ell/netconfig.h
Makefile.am +4 -2
index 4e650c9..8aaefdf 100644
@@ -60,7 +60,8 @@ pkginclude_HEADERS = ell/ell.h \
 			ell/icmp6.h \
 			ell/acd.h \
 			ell/tester.h \
-			ell/cleanup.h
+			ell/cleanup.h \
+			ell/netconfig.h
 
 lib_LTLIBRARIES = ell/libell.la
 
@@ -146,7 +147,8 @@ ell_libell_la_SOURCES = $(linux_headers) \
 			ell/icmp6.c \
 			ell/icmp6-private.h \
 			ell/acd.c \
-			ell/tester.c
+			ell/tester.c \
+			ell/netconfig.c
 
 ell_libell_la_LDFLAGS = -Wl,--no-undefined \
 			-Wl,--version-script=$(top_srcdir)/ell/ell.sym \
ell/ell.h +1 -0
index d47d5cf..d4489ac 100644
@@ -65,3 +65,4 @@
 #include <ell/path.h>
 #include <ell/acd.h>
 #include <ell/tester.h>
+#include <ell/netconfig.h>
ell/ell.sym +7 -0
index e2a278f..1dcee15 100644
@@ -732,6 +732,13 @@ global:
 	l_tester_post_teardown_complete;
 	l_tester_post_teardown_failed;
 	l_tester_wait;
+	/* netconfig */
+	l_netconfig_new;
+	l_netconfig_destroy;
+	l_netconfig_start;
+	l_netconfig_stop;
+	l_netconfig_get_dhcp_client;
+	l_netconfig_set_event_handler;
 local:
 	*;
 };
ell/netconfig.c new file +377 -0

377 lines not on this page · raw message

ell/netconfig.h new file +72 -0

72 lines not on this page · raw message

Signature · 1 line
2.32.0
Applied
#9 lore raw

Re: [PATCH 09/12] netconfig: Add netconfig API

Hi Andrew,

Quoted 22 lines · Andrew Zaborowski

This adds the netconfig class that will encapsulate static IP configuration, DHCP, DHCP6, ICMP6, SLAAC logic and free the user of manually handling DHCP/DHCP6 events to implement netconfig policies. The user will still need to handle committing received DNS and mdns information.

In this patch only IPv4 DHCP is implemented and no configurable settings are added.

When events are emitted for address and/or route changes, the changes are split into "added", "updated" and "removed" lists so that the user can decide apply those changes without first removing all addresses and routes from the interface and the re-adding the complete new list. They can also decide to let another entity apply the changes. The obvious downside is the proliferation of lists.

---
  Makefile.am     |   6 +-
  ell/ell.h       |   1 +
  ell/ell.sym     |   7 +
  ell/netconfig.c | 377 ++++++++++++++++++++++++++++++++++++++++++++++++
  ell/netconfig.h |  72 +++++++++
  5 files changed, 461 insertions(+), 2 deletions(-)
Quoted 3 lines

create mode 100644 ell/netconfig.c
create mode 100644 ell/netconfig.h

Due to:

make --no-print-directory all-am
CC unit/test-unit.o
CC ell/netconfig.lo
In file included from ell/netconfig.c:32:
ell/netconfig.c: In function ‘l_netconfig_stop’:
ell/netconfig.c:339:39: error: ‘struct l_netconfig’ has no member named ‘started’
339 | if (unlikely(!netconfig || !netconfig->started))
| ^~
ell/useful.h:26:41: note: in definition of macro ‘unlikely’

    26 | #define unlikely(x) __builtin_expect(!!(x), 0)
       |                                         ^

I slightly amended this patch to move the 'started' variable introduction from patch 11 over to this patch.

Applied, thanks.

Regards,
-Denis

author
#10 lore raw

PATCH 10/12 netconfig: Add l_netconfig_apply_rtnl

In reply to Andrew Zaborowski

Add l_netconfig_apply_rtnl and the getters for current lists of routes and addresses for the netconfig user to be able to commit address and route changes.

---
 ell/ell.sym     |  3 ++
 ell/netconfig.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++
 ell/netconfig.h | 11 ++++++++
 3 files changed, 87 insertions(+)
ell/ell.sym +3 -0
index 1dcee15..a03dc69 100644
@@ -739,6 +739,9 @@ global:
 	l_netconfig_stop;
 	l_netconfig_get_dhcp_client;
 	l_netconfig_set_event_handler;
+	l_netconfig_apply_rtnl;
+	l_netconfig_get_addresses;
+	l_netconfig_get_routes;
 local:
 	*;
 };
ell/netconfig.c +73 -0

76 lines not on this page · raw message

ell/netconfig.h +11 -0
index 7773885..682f4aa 100644
@@ -65,6 +65,17 @@ void l_netconfig_set_event_handler(struct l_netconfig *netconfig,
 					void *user_data,
 					l_netconfig_destroy_cb_t destroy);
 
+void l_netconfig_apply_rtnl(struct l_netconfig *netconfig,
+				struct l_netlink *rtnl);
+struct l_queue *l_netconfig_get_addresses(struct l_netconfig *netconfig,
+					struct l_queue **out_added,
+					struct l_queue **out_updated,
+					struct l_queue **out_removed);
+struct l_queue *l_netconfig_get_routes(struct l_netconfig *netconfig,
+					struct l_queue **out_added,
+					struct l_queue **out_updated,
+					struct l_queue **out_removed);
+
 #ifdef __cplusplus
 }
 #endif
Signature · 1 line
2.32.0
Applied
#11 lore raw

Re: [PATCH 10/12] netconfig: Add l_netconfig_apply_rtnl

Hi Andrew,

Quoted 9 lines · Andrew Zaborowski

Add l_netconfig_apply_rtnl and the getters for current lists of routes and addresses for the netconfig user to be able to commit address and route changes.

---
  ell/ell.sym     |  3 ++
  ell/netconfig.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++
  ell/netconfig.h | 11 ++++++++
  3 files changed, 87 insertions(+)

Applied, thanks.

Regards,
-Denis

author
#12 lore raw

PATCH 11/12 netconfig: Add DHCP override and static IP API

In reply to Andrew Zaborowski

Add API to override things like DNS, gateway/router address, domain names, etc. received from DHCP or disable DHCP completely by setting a static address.

---
 ell/ell.sym     |   8 +
 ell/netconfig.c | 424 +++++++++++++++++++++++++++++++++++++++++++++++-
 ell/netconfig.h |  17 ++
 3 files changed, 445 insertions(+), 4 deletions(-)
ell/ell.sym +8 -0
index a03dc69..ec995fc 100644
@@ -735,6 +735,14 @@ global:
 	/* netconfig */
 	l_netconfig_new;
 	l_netconfig_destroy;
+	l_netconfig_set_af_enabled;
+	l_netconfig_set_hostname;
+	l_netconfig_set_route_priority;
+	l_netconfig_set_static_addr;
+	l_netconfig_set_gateway_override;
+	l_netconfig_set_dns_override;
+	l_netconfig_set_domain_names_override;
+	l_netconfig_check_config;
 	l_netconfig_start;
 	l_netconfig_stop;
 	l_netconfig_get_dhcp_client;
ell/netconfig.c +420 -4

468 lines not on this page · raw message

ell/netconfig.h +17 -0
index 682f4aa..6ec00ba 100644
@@ -53,6 +53,23 @@ typedef void (*l_netconfig_destroy_cb_t)(void *user_data);
 
 struct l_netconfig *l_netconfig_new(uint32_t ifindex);
 void l_netconfig_destroy(struct l_netconfig *netconfig);
+void l_netconfig_set_af_enabled(struct l_netconfig *netconfig, uint8_t family,
+				bool enabled);
+void l_netconfig_set_hostname(struct l_netconfig *netconfig,
+				const char *hostname);
+void l_netconfig_set_route_priority(struct l_netconfig *netconfig,
+					uint32_t priority);
+void l_netconfig_set_static_addr(struct l_netconfig *netconfig, uint8_t family,
+					const struct l_rtnl_address *addr);
+void l_netconfig_set_gateway_override(struct l_netconfig *netconfig,
+					uint8_t family,
+					const char *gateway_str);
+void l_netconfig_set_dns_override(struct l_netconfig *netconfig, uint8_t family,
+					const char **dns_list);
+void l_netconfig_set_domain_names_override(struct l_netconfig *netconfig,
+						uint8_t family,
+						const char **names);
+bool l_netconfig_check_config(struct l_netconfig *netconfig);
 
 bool l_netconfig_start(struct l_netconfig *netconfig);
 void l_netconfig_stop(struct l_netconfig *netconfig);
Signature · 1 line
2.32.0
#13 lore raw

Re: [PATCH 11/12] netconfig: Add DHCP override and static IP API

Hi Andrew,

Quoted 9 lines · Andrew Zaborowski

Add API to override things like DNS, gateway/router address, domain names, etc. received from DHCP or disable DHCP completely by setting a static address.

---
  ell/ell.sym     |   8 +
  ell/netconfig.c | 424 +++++++++++++++++++++++++++++++++++++++++++++++-
  ell/netconfig.h |  17 ++
  3 files changed, 445 insertions(+), 4 deletions(-)

<snip>

Quoted 19 lines
ell/netconfig.c +14 -1
@@ -322,15 +352,396 @@ LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig)
  	l_free(netconfig);
  }
 
-LIB_EXPORT bool l_netconfig_start(struct l_netconfig *netconfig)
+/*
+ * The following l_netconfig_set_* functions configure the l_netconfig's
+ * client settings.  The setters can be called independently, without
+ * following a specific order.  Most of the setters will not validate the
+ * values passed, l_netconfig_start() will fail if settings are incorrect
+ * or inconsistent between themselves, e.g. if the static local IP and
+ * gateway IP are not in the same subnet.  Alternatively
+ * l_netconfig_check_config() can be called at any point to validate the
+ * current configuration.  The configuration can only be changed while
+ * the l_netconfig state machine is stopped, i.e. before
+ * l_netconfig_start() and after l_netconfig_stop().
+ *
+ * l_netconfig_set_hostname, l_netconfig_set_static_addr,
+ * l_netconfig_set_gateway_override, l_netconfig_set_dns_override and

ell/netconfig.c LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig) · line 378

Quoted 10 lines
ell/netconfig.c +10 -0
+ * l_netconfig_set_domain_names_override can be passed NULL to unset a
+ * value that had been set before (revert to auto).  This is why the
+ * family parameter is needed even when it could otherwise be derived
+ * from the new value that is passed.
+ */
+LIB_EXPORT void l_netconfig_set_af_enabled(struct l_netconfig *netconfig,
+						uint8_t family, bool enabled)
+{
+	if (unlikely(!netconfig || netconfig->started))
+		return;

Should we at least return bool here? In case family is invalid for example?

Quoted 5 lines
ell/netconfig.c +5 -0
+
+	switch (family) {
+	case AF_INET:
+		netconfig->v4_enabled = enabled;
+		break;

ell/netconfig.c LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig) · line 393

Quoted 10 lines
ell/netconfig.c +10 -0
+	}
+}
+
+LIB_EXPORT void l_netconfig_set_hostname(struct l_netconfig *netconfig,
+						const char *hostname)
+{
+	if (unlikely(!netconfig || netconfig->started))
+		return;
+
+	l_dhcp_client_set_hostname(netconfig->dhcp_client, hostname);

Same with this? In theory set_hostname might fail.

Quoted 16 lines
ell/netconfig.c +16 -0
+}
+
+LIB_EXPORT void l_netconfig_set_route_priority(struct l_netconfig *netconfig,
+						uint32_t priority)
+{
+	if (unlikely(!netconfig || netconfig->started))
+		return;
+
+	netconfig->route_priority = priority;
+}
+
+LIB_EXPORT void l_netconfig_set_static_addr(struct l_netconfig *netconfig,
+					uint8_t family,
+					const struct l_rtnl_address *addr)
+{
+	if (unlikely(!netconfig || netconfig->started))

ell/netconfig.c LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig) · line 419

Quoted 10 lines
ell/netconfig.c +10 -0
+		return;
+
+	if (L_WARN_ON(addr && l_rtnl_address_get_family(addr) != family))
+		return;
+
+	switch (family) {
+	case AF_INET:
+		if (netconfig->v4_static_addr)
+			l_rtnl_address_free(l_steal_ptr(
+						netconfig->v4_static_addr));

Why not simply l_rtnl_address_free(l_steal_ptr...))?

ell/netconfig.c LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig) · line 431

Quoted 12 lines
ell/netconfig.c +12 -0
+
+		if (addr)
+			break;
+
+		netconfig->v4_static_addr = l_rtnl_address_clone(addr);
+		l_rtnl_address_set_lifetimes(netconfig->v4_static_addr, 0, 0);
+
+		/*
+		 * We could leave the decision about this flag up to the
+		 * caller but for simplicity override to true.
+		 */
+		l_rtnl_address_set_noprefixroute(netconfig->v4_static_addr, true);

This line > 80 chars

ell/netconfig.c LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig) · line 436

Quoted 5 lines
ell/netconfig.c +5 -0
+		break;
+	case AF_INET6:
+		if (netconfig->v6_static_addr)
+			l_rtnl_address_free(l_steal_ptr(
+						netconfig->v6_static_addr));

As above, the if seems unnecessary.

ell/netconfig.c LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig) · line 443

Quoted 7 lines
ell/netconfig.c +7 -0
+
+		if (addr)
+			break;
+
+		netconfig->v6_static_addr = l_rtnl_address_clone(addr);
+		l_rtnl_address_set_lifetimes(netconfig->v6_static_addr, 0, 0);
+		l_rtnl_address_set_noprefixroute(netconfig->v6_static_addr, true);

This line > 80 chars

Quoted 24 lines
ell/netconfig.c +24 -0
+		break;
+	}
+}
+
+LIB_EXPORT void l_netconfig_set_gateway_override(struct l_netconfig *netconfig,
+							uint8_t family,
+							const char *gateway_str)
+{
+	if (unlikely(!netconfig || netconfig->started))
+		return;
+
+	switch (family) {
+	case AF_INET:
+		l_free(l_steal_ptr(netconfig->v4_gateway_override));
+
+		if (!gateway_str)
+			break;
+
+		netconfig->v4_gateway_override = l_strdup(gateway_str);
+		break;
+	case AF_INET6:
+		l_free(l_steal_ptr(netconfig->v6_gateway_override));
+
+		if (!gateway_str)

ell/netconfig.c LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig) · line 477

Quoted 10 lines
ell/netconfig.c +10 -0
+			break;
+
+		netconfig->v6_gateway_override = l_strdup(gateway_str);
+		break;
+	}
+}
+
+LIB_EXPORT void l_netconfig_set_dns_override(struct l_netconfig *netconfig,
+						uint8_t family,
+						const char **dns_list)

Why const char **? For const-correctness you'd likely need some C++ thing like 'const char * const *'. We generally fall back to simply char ** for situations like this.

Quoted 18 lines
ell/netconfig.c +18 -0
+{
+	if (unlikely(!netconfig || netconfig->started))
+		return;
+
+	switch (family) {
+	case AF_INET:
+		l_strv_free(l_steal_ptr(netconfig->v4_dns_override));
+
+		if (!dns_list)
+			break;
+
+		netconfig->v4_dns_override = l_strv_copy((char **) dns_list);
+		break;
+	case AF_INET6:
+		l_strv_free(l_steal_ptr(netconfig->v6_dns_override));
+
+		if (!dns_list)
+			break;

ell/netconfig.c LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig) · line 505

Quoted 10 lines
ell/netconfig.c +10 -0
+
+		netconfig->v6_dns_override = l_strv_copy((char **) dns_list);
+		break;
+	}
+}
+
+LIB_EXPORT void l_netconfig_set_domain_names_override(
+						struct l_netconfig *netconfig,
+						uint8_t family,
+						const char **names)

Same as above

Quoted 29 lines
ell/netconfig.c +29 -0
+{
+	if (unlikely(!netconfig || netconfig->started))
+		return;
+
+	switch (family) {
+	case AF_INET:
+		l_strv_free(l_steal_ptr(netconfig->v4_domain_names_override));
+
+		if (!names)
+			break;
+
+		netconfig->v4_domain_names_override =
+			l_strv_copy((char **) names);
+		break;
+	case AF_INET6:
+		l_strv_free(l_steal_ptr(netconfig->v6_domain_names_override));
+
+		if (!names)
+			break;
+
+		netconfig->v6_domain_names_override =
+			l_strv_copy((char **) names);
+		break;
+	}
+}
+
+static bool netconfig_match_subnet(const void *a, const void *b,
+					uint8_t prefix_len)
+{

ell/netconfig.c LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig) · line 544

Quoted 10 lines
ell/netconfig.c +10 -0
+	uint8_t bytes = prefix_len / 8;
+	uint8_t bits = prefix_len & 7;
+
+	if (bytes && memcmp(a, b, bytes))
+		return false;
+
+	return !bits ||
+		((((const uint8_t *) a)[bytes] ^ ((const uint8_t *) b)[bytes]) &
+		 (0xff00u >> bits)) == 0;
+}

Do we not have a similar function in iwd? Perhaps this should be a utility function in l_net? Also, with all the magic operations happening here, a few comments might be nice.

Review LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig) · line 545

Quoted 1 line
(quoted diff) +1 -0
+

Regards,
-Denis

author
#14 lore raw

Re: [PATCH 11/12] netconfig: Add DHCP override and static IP API

Hi Denis,

Quoted 8 lines · Denis Kenzior

ell/netconfig.c LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig) · line 378

Quoted 5 lines · Andrew Zaborowski
ell/netconfig.c +5 -0
+LIB_EXPORT void l_netconfig_set_af_enabled(struct l_netconfig *netconfig,
+                                             uint8_t family, bool enabled)
+{
+     if (unlikely(!netconfig || netconfig->started))
+             return;

Should we at least return bool here? In case family is invalid for example?

I wanted to avoid forcing the user to perform too many checks, and instead only return bool from l_netconfig_start, but there may be no way around it.

Quoted 10 lines
Quoted 10 lines
ell/netconfig.c +9 -0
+
+     switch (family) {
+     case AF_INET:
+             netconfig->v4_enabled = enabled;
+             break;
+     }
+}
+
+LIB_EXPORT void l_netconfig_set_hostname(struct l_netconfig *netconfig,
Quoted 8 lines

ell/netconfig.c LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig) · line 393

Quoted 6 lines
ell/netconfig.c +6 -0
+                                             const char *hostname)
+{
+     if (unlikely(!netconfig || netconfig->started))
+             return;
+
+     l_dhcp_client_set_hostname(netconfig->dhcp_client, hostname);

Same with this?

Ok.

Quoted 1 line

In theory set_hostname might fail.

We ensure that netconfig->dhcp_client is in the right state but ok.

Quoted 21 lines
Quoted 21 lines
ell/netconfig.c +20 -0
+}
+
+LIB_EXPORT void l_netconfig_set_route_priority(struct l_netconfig *netconfig,
+                                             uint32_t priority)
+{
+     if (unlikely(!netconfig || netconfig->started))
+             return;
+
+     netconfig->route_priority = priority;
+}
+
+LIB_EXPORT void l_netconfig_set_static_addr(struct l_netconfig *netconfig,
+                                     uint8_t family,
+                                     const struct l_rtnl_address *addr)
+{
+     if (unlikely(!netconfig || netconfig->started))
+             return;
+
+     if (L_WARN_ON(addr && l_rtnl_address_get_family(addr) != family))
+             return;
Quoted 8 lines

ell/netconfig.c LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig) · line 419

Quoted 6 lines
ell/netconfig.c +6 -0
+
+     switch (family) {
+     case AF_INET:
+             if (netconfig->v4_static_addr)
+                     l_rtnl_address_free(l_steal_ptr(
+                                             netconfig->v4_static_addr));

Why not simply l_rtnl_address_free(l_steal_ptr...))?

True.

Quoted 15 lines

ell/netconfig.c LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig) · line 431

Quoted 13 lines
ell/netconfig.c +12 -0
+
+             if (addr)
+                     break;
+
+             netconfig->v4_static_addr = l_rtnl_address_clone(addr);
+             l_rtnl_address_set_lifetimes(netconfig->v4_static_addr, 0, 0);
+
+             /*
+              * We could leave the decision about this flag up to the
+              * caller but for simplicity override to true.
+              */
+             l_rtnl_address_set_noprefixroute(netconfig->v4_static_addr, true);

This line > 80 chars

Oops, will fix these too.

Quoted 49 lines

ell/netconfig.c LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig) · line 436

Quoted 6 lines
ell/netconfig.c +5 -0
+             break;
+     case AF_INET6:
+             if (netconfig->v6_static_addr)
+                     l_rtnl_address_free(l_steal_ptr(
+                                             netconfig->v6_static_addr));

As above, the if seems unnecessary.

ell/netconfig.c LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig) · line 443

Quoted 7 lines
ell/netconfig.c +7 -0
+
+             if (addr)
+                     break;
+
+             netconfig->v6_static_addr = l_rtnl_address_clone(addr);
+             l_rtnl_address_set_lifetimes(netconfig->v6_static_addr, 0, 0);
+             l_rtnl_address_set_noprefixroute(netconfig->v6_static_addr, true);

This line > 80 chars

Quoted 30 lines
ell/netconfig.c +30 -0
+             break;
+     }
+}
+
+LIB_EXPORT void l_netconfig_set_gateway_override(struct l_netconfig *netconfig,
+                                                     uint8_t family,
+                                                     const char *gateway_str)
+{
+     if (unlikely(!netconfig || netconfig->started))
+             return;
+
+     switch (family) {
+     case AF_INET:
+             l_free(l_steal_ptr(netconfig->v4_gateway_override));
+
+             if (!gateway_str)
+                     break;
+
+             netconfig->v4_gateway_override = l_strdup(gateway_str);
+             break;
+     case AF_INET6:
+             l_free(l_steal_ptr(netconfig->v6_gateway_override));
+
+             if (!gateway_str)
+                     break;
+
+             netconfig->v6_gateway_override = l_strdup(gateway_str);
+             break;
+     }
+}
Quoted 8 lines

ell/netconfig.c LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig) · line 477

Quoted 4 lines
ell/netconfig.c +4 -0
+
+LIB_EXPORT void l_netconfig_set_dns_override(struct l_netconfig *netconfig,
+                                             uint8_t family,
+                                             const char **dns_list)

Why const char **? For const-correctness you'd likely need some C++ thing like 'const char * const *'. We generally fall back to simply char ** for situations like this.

Right, I'll drop this.

const char ** is not as correct as const char * const *, but more than char ** ;) visually conveys the meaning though.

Quoted 67 lines
Quoted 19 lines
ell/netconfig.c +18 -0
+{
+     if (unlikely(!netconfig || netconfig->started))
+             return;
+
+     switch (family) {
+     case AF_INET:
+             l_strv_free(l_steal_ptr(netconfig->v4_dns_override));
+
+             if (!dns_list)
+                     break;
+
+             netconfig->v4_dns_override = l_strv_copy((char **) dns_list);
+             break;
+     case AF_INET6:
+             l_strv_free(l_steal_ptr(netconfig->v6_dns_override));
+
+             if (!dns_list)
+                     break;

ell/netconfig.c LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig) · line 505

Quoted 10 lines
ell/netconfig.c +10 -0
+
+             netconfig->v6_dns_override = l_strv_copy((char **) dns_list);
+             break;
+     }
+}
+
+LIB_EXPORT void l_netconfig_set_domain_names_override(
+                                             struct l_netconfig *netconfig,
+                                             uint8_t family,
+                                             const char **names)

Same as above

Quoted 35 lines
ell/netconfig.c +35 -0
+{
+     if (unlikely(!netconfig || netconfig->started))
+             return;
+
+     switch (family) {
+     case AF_INET:
+             l_strv_free(l_steal_ptr(netconfig->v4_domain_names_override));
+
+             if (!names)
+                     break;
+
+             netconfig->v4_domain_names_override =
+                     l_strv_copy((char **) names);
+             break;
+     case AF_INET6:
+             l_strv_free(l_steal_ptr(netconfig->v6_domain_names_override));
+
+             if (!names)
+                     break;
+
+             netconfig->v6_domain_names_override =
+                     l_strv_copy((char **) names);
+             break;
+     }
+}
+
+static bool netconfig_match_subnet(const void *a, const void *b,
+                                     uint8_t prefix_len)
+{
+     uint8_t bytes = prefix_len / 8;
+     uint8_t bits = prefix_len & 7;
+
+     if (bytes && memcmp(a, b, bytes))
+             return false;
+
Quoted 8 lines

ell/netconfig.c LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig) · line 544

Quoted 4 lines
ell/netconfig.c +4 -0
+     return !bits ||
+             ((((const uint8_t *) a)[bytes] ^ ((const uint8_t *) b)[bytes]) &
+              (0xff00u >> bits)) == 0;
+}

Do we not have a similar function in iwd? Perhaps this should be a utility function in l_net? Also, with all the magic operations happening here, a few comments might be nice.

True, let me move it from IWD's util.h to ell/net.h.

Best regards

author
#15 lore raw

PATCH 12/12 examples: Add a netconfig API example/test utility

In reply to Andrew Zaborowski

A test utility that simply starts netconfig on a given interface and prints events that occur. DNS/mdns changes aren't committed to the system.

---
 Makefile.am               |   4 +-
 examples/netconfig-test.c | 208 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 211 insertions(+), 1 deletion(-)
 create mode 100644 examples/netconfig-test.c
Makefile.am +3 -1
index 8aaefdf..8ccb35e 100644
@@ -347,7 +347,8 @@ unit_test_data_files = unit/settings.test unit/dbus.conf
 examples = examples/dbus-service examples/https-client-test \
 		examples/https-server-test examples/dbus-client \
 		examples/dhcp-client examples/dhcp6-client \
-		examples/dhcp-server examples/acd-client
+		examples/dhcp-server examples/acd-client \
+		examples/netconfig-test
 
 if GLIB
 examples += examples/glib-eventloop
@@ -367,6 +368,7 @@ examples_dhcp_client_LDADD = ell/libell-private.la
 examples_dhcp6_client_LDADD = ell/libell-private.la
 examples_dhcp_server_LDADD = ell/libell-private.la
 examples_acd_client_LDADD = ell/libell-private.la
+examples_netconfig_test_LDADD = ell/libell-private.la
 
 noinst_PROGRAMS += tools/certchain-verify tools/genl-discover \
 		   tools/genl-watch tools/genl-request tools/gpio
examples/netconfig-test.c new file +208 -0

208 lines not on this page · raw message

Signature · 1 line
2.32.0
#16 lore raw

Re: [PATCH 12/12] examples: Add a netconfig API example/test utility

Hi Andrew,

Quoted 9 lines · Andrew Zaborowski

A test utility that simply starts netconfig on a given interface and prints events that occur. DNS/mdns changes aren't committed to the system.

---
  Makefile.am               |   4 +-
  examples/netconfig-test.c | 208 ++++++++++++++++++++++++++++++++++++++
  2 files changed, 211 insertions(+), 1 deletion(-)

create mode 100644 examples/netconfig-test.c

Applied with a minor amend to get rid of a >80 char line.

Regards,
-Denis

#17 lore raw
In reply to Andrew Zaborowski

Hi Andrew,

Quoted 9 lines · Andrew Zaborowski

Store expiry times of addresses and routes in addition to the lifetimes so that at any time the current lifetime left can be calculated. The expiry time is normally the creation or renewal time + lifetime.

---
  ell/ell.sym |  4 +++
  ell/rtnl.c  | 76 ++++++++++++++++++++++++++++++++++++++++++++++++-----
  ell/rtnl.h  | 12 +++++++--
  3 files changed, 83 insertions(+), 9 deletions(-)

Patches 1-8 applied, thanks.

Regards,
-Denis