ell.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/3] dhcp-server: Fix debug messages in send_offer
@ 2021-07-09 11:47 Andrew Zaborowski
  2021-07-09 11:47 ` [PATCH 2/3] dhcp-server: Fill in netmask & gateway in lease objects Andrew Zaborowski
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Andrew Zaborowski @ 2021-07-09 11:47 UTC (permalink / raw)
  To: ell

[-- Attachment #1: Type: text/plain, Size: 1026 bytes --]

find_free_or_expired_ip() or check_requested_ip() should fail if no
addresses are left in the pool, rather than lease_add() so emit that
error message there.  The only thing lease_add() validates that has not
been validated before might be the origin MAC, treat the lease_add()
failure more as an internal error.
---
 ell/dhcp-server.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/ell/dhcp-server.c b/ell/dhcp-server.c
index 34512ae..b761833 100644
--- a/ell/dhcp-server.c
+++ b/ell/dhcp-server.c
@@ -451,13 +451,13 @@ static void send_offer(struct l_dhcp_server *server,
 							client_msg->chaddr);
 
 	if (!reply->yiaddr) {
-		SERVER_DEBUG("Could not find lease or send offer");
+		SERVER_DEBUG("No free IP addresses, OFFER abandoned");
 		return;
 	}
 
 	lease = add_lease(server, true, client_msg->chaddr, reply->yiaddr);
 	if (!lease) {
-		SERVER_DEBUG("No free IP addresses, OFFER abandoned");
+		SERVER_DEBUG("add_lease() failed");
 		return;
 	}
 
-- 
2.30.2

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/3] dhcp-server: Fill in netmask & gateway in lease objects
  2021-07-09 11:47 [PATCH 1/3] dhcp-server: Fix debug messages in send_offer Andrew Zaborowski
@ 2021-07-09 11:47 ` Andrew Zaborowski
  2021-07-09 11:47 ` [PATCH 3/3] dhcp-server: Add l_dhcp_server_{new,release}_lease Andrew Zaborowski
  2021-07-09 15:12 ` [PATCH 1/3] dhcp-server: Fix debug messages in send_offer Denis Kenzior
  2 siblings, 0 replies; 5+ messages in thread
From: Andrew Zaborowski @ 2021-07-09 11:47 UTC (permalink / raw)
  To: ell

[-- Attachment #1: Type: text/plain, Size: 707 bytes --]

When creating new l_dhcp_lease objects for client leases, also fill in
the netmask and the gateway fields for the benefit of the users of those
lease objects outside dhcp-server.c.
---
 ell/dhcp-server.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/ell/dhcp-server.c b/ell/dhcp-server.c
index b761833..759276e 100644
--- a/ell/dhcp-server.c
+++ b/ell/dhcp-server.c
@@ -262,6 +262,8 @@ static struct l_dhcp_lease *add_lease(struct l_dhcp_server *server,
 
 	memcpy(lease->mac, chaddr, ETH_ALEN);
 	lease->address = yiaddr;
+	lease->subnet_mask = server->netmask;
+	lease->router = server->gateway;
 
 	lease->offering = offering;
 	lease->bound_time = l_time_now();
-- 
2.30.2

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 3/3] dhcp-server: Add l_dhcp_server_{new,release}_lease
  2021-07-09 11:47 [PATCH 1/3] dhcp-server: Fix debug messages in send_offer Andrew Zaborowski
  2021-07-09 11:47 ` [PATCH 2/3] dhcp-server: Fill in netmask & gateway in lease objects Andrew Zaborowski
@ 2021-07-09 11:47 ` Andrew Zaborowski
  2021-07-09 15:12 ` [PATCH 1/3] dhcp-server: Fix debug messages in send_offer Denis Kenzior
  2 siblings, 0 replies; 5+ messages in thread
From: Andrew Zaborowski @ 2021-07-09 11:47 UTC (permalink / raw)
  To: ell

[-- Attachment #1: Type: text/plain, Size: 3550 bytes --]

Add API for obtaining and releasing DHCP leases directly, as if by
sending the appropriate DISCOVER, REQUEST and RELEASE messages but with
just method calls.  This is useful for implementing mechanisms like the
optional 802.11 4-Way Handshake IP Allocation (reduces the number of
roundtrips in a P2P connection setup).
---
 ell/dhcp-server.c | 52 +++++++++++++++++++++++++++++++++++++++++++++--
 ell/dhcp.h        |  5 +++++
 ell/ell.sym       |  2 ++
 3 files changed, 57 insertions(+), 2 deletions(-)

diff --git a/ell/dhcp-server.c b/ell/dhcp-server.c
index 759276e..e392bb7 100644
--- a/ell/dhcp-server.c
+++ b/ell/dhcp-server.c
@@ -129,12 +129,14 @@ static struct l_dhcp_lease *find_lease_by_mac(struct l_dhcp_server *server,
 	return l_queue_find(server->lease_list, match_lease_mac, mac);
 }
 
-static void remove_lease(struct l_dhcp_server *server,
+static bool remove_lease(struct l_dhcp_server *server,
 				struct l_dhcp_lease *lease)
 {
-	l_queue_remove(server->lease_list, lease);
+	if (!l_queue_remove(server->lease_list, lease))
+		return false;
 
 	_dhcp_lease_free(lease);
+	return true;
 }
 
 /* Clear the old lease and create the new one */
@@ -1043,3 +1045,49 @@ failed:
 	l_free(dns_list);
 	return false;
 }
+
+/*
+ * Create a new lease, like by sending a DISCOVER and a REQUEST.  The lease
+ * stays valid until the server is destroyed, l_dhcp_server_release_lease is
+ * called or L_DHCP_SERVER_EVENT_LEASE_EXPIRED is emitted for it.
+ */
+LIB_EXPORT struct l_dhcp_lease *l_dhcp_server_new_lease(
+						struct l_dhcp_server *server,
+						const uint8_t *mac)
+{
+	struct l_dhcp_lease *lease;
+	uint32_t ip = find_free_or_expired_ip(server, mac);
+
+	if (!ip) {
+		SERVER_DEBUG("Could not find any free addresses");
+		return NULL;
+	}
+
+	lease = add_lease(server, false, mac, ip);
+	if (!lease) {
+		SERVER_DEBUG("add_lease() failed");
+		return NULL;
+	}
+
+	SERVER_DEBUG("Allocated %s to " MAC, IP_STR(ip), MAC_STR(mac));
+
+	if (server->event_handler)
+		server->event_handler(server, L_DHCP_SERVER_EVENT_NEW_LEASE,
+					server->user_data, lease);
+
+	return lease;
+}
+
+LIB_EXPORT bool l_dhcp_server_release_lease(struct l_dhcp_server *server,
+						struct l_dhcp_lease *lease)
+{
+	/*
+	 * Don't emit event or put the lease in the expired list because
+	 * the lease is not expiring.
+	 */
+	if (!remove_lease(server, lease))
+		return false;
+
+	set_next_expire_timer(server, NULL);
+	return true;
+}
diff --git a/ell/dhcp.h b/ell/dhcp.h
index 2af75cc..4b5cd52 100644
--- a/ell/dhcp.h
+++ b/ell/dhcp.h
@@ -141,6 +141,11 @@ bool l_dhcp_server_set_ip_address(struct l_dhcp_server *server,
 bool l_dhcp_server_set_netmask(struct l_dhcp_server *server, const char *mask);
 bool l_dhcp_server_set_gateway(struct l_dhcp_server *server, const char *ip);
 bool l_dhcp_server_set_dns(struct l_dhcp_server *server, char **dns);
+
+struct l_dhcp_lease *l_dhcp_server_new_lease(struct l_dhcp_server *server,
+						const uint8_t *mac);
+bool l_dhcp_server_release_lease(struct l_dhcp_server *server,
+					struct l_dhcp_lease *lease);
 #ifdef __cplusplus
 }
 #endif
diff --git a/ell/ell.sym b/ell/ell.sym
index 4391051..e487f72 100644
--- a/ell/ell.sym
+++ b/ell/ell.sym
@@ -259,6 +259,8 @@ global:
 	l_dhcp_server_set_netmask;
 	l_dhcp_server_set_gateway;
 	l_dhcp_server_set_dns;
+	l_dhcp_server_new_lease;
+	l_dhcp_server_release_lease;
 	/* dhcp6 */
 	l_dhcp6_client_new;
 	l_dhcp6_client_destroy;
-- 
2.30.2

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/3] dhcp-server: Fix debug messages in send_offer
  2021-07-09 11:47 [PATCH 1/3] dhcp-server: Fix debug messages in send_offer Andrew Zaborowski
  2021-07-09 11:47 ` [PATCH 2/3] dhcp-server: Fill in netmask & gateway in lease objects Andrew Zaborowski
  2021-07-09 11:47 ` [PATCH 3/3] dhcp-server: Add l_dhcp_server_{new,release}_lease Andrew Zaborowski
@ 2021-07-09 15:12 ` Denis Kenzior
  2021-07-09 15:38   ` Denis Kenzior
  2 siblings, 1 reply; 5+ messages in thread
From: Denis Kenzior @ 2021-07-09 15:12 UTC (permalink / raw)
  To: ell

[-- Attachment #1: Type: text/plain, Size: 527 bytes --]

Hi Andrew,

On 7/9/21 6:47 AM, Andrew Zaborowski wrote:
> find_free_or_expired_ip() or check_requested_ip() should fail if no
> addresses are left in the pool, rather than lease_add() so emit that
> error message there.  The only thing lease_add() validates that has not
> been validated before might be the origin MAC, treat the lease_add()
> failure more as an internal error.
> ---
>   ell/dhcp-server.c | 4 ++--
>   1 file changed, 2 insertions(+), 2 deletions(-)
> 

All applied, thanks.

Regards,
-Denis

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/3] dhcp-server: Fix debug messages in send_offer
  2021-07-09 15:12 ` [PATCH 1/3] dhcp-server: Fix debug messages in send_offer Denis Kenzior
@ 2021-07-09 15:38   ` Denis Kenzior
  0 siblings, 0 replies; 5+ messages in thread
From: Denis Kenzior @ 2021-07-09 15:38 UTC (permalink / raw)
  To: ell

[-- Attachment #1: Type: text/plain, Size: 1836 bytes --]

Hi Andrew,

On 7/9/21 10:12 AM, Denis Kenzior wrote:
> Hi Andrew,
> 
> On 7/9/21 6:47 AM, Andrew Zaborowski wrote:
>> find_free_or_expired_ip() or check_requested_ip() should fail if no
>> addresses are left in the pool, rather than lease_add() so emit that
>> error message there.  The only thing lease_add() validates that has not
>> been validated before might be the origin MAC, treat the lease_add()
>> failure more as an internal error.
>> ---
>>   ell/dhcp-server.c | 4 ++--
>>   1 file changed, 2 insertions(+), 2 deletions(-)
>>
> 

Actually, I pulled patch 3 back.  It doesn't compile:

   CC       ell/dhcp-server.lo
ell/dhcp-server.c: In function ‘l_dhcp_server_new_lease’:
ell/dhcp-server.c:1071:39: error: implicit declaration of function ‘IP_STR’; did 
you mean ‘IP_TTL’? [-Werror=implicit-function-declaration]
  1071 |  SERVER_DEBUG("Allocated %s to " MAC, IP_STR(ip), MAC_STR(mac));
       |                                       ^~~~~~
ell/dhcp-server.c:103:41: note: in definition of macro ‘SERVER_DEBUG’
   103 |    "%s:%i " fmt, __func__, __LINE__, ## args)
       |                                         ^~~~
ell/dhcp-server.c:103:4: error: format ‘%s’ expects argument of type ‘char *’, 
but argument 6 has type ‘int’ [-Werror=format=]
   103 |    "%s:%i " fmt, __func__, __LINE__, ## args)
       |    ^~~~~~~~
......
  1071 |  SERVER_DEBUG("Allocated %s to " MAC, IP_STR(ip), MAC_STR(mac));
       |                                       ~~~~~~~~~~
       |                                       |
       |                                       int
ell/dhcp-server.c:1071:2: note: in expansion of macro ‘SERVER_DEBUG’
  1071 |  SERVER_DEBUG("Allocated %s to " MAC, IP_STR(ip), MAC_STR(mac));
       |  ^~~~~~~~~~~~

Regards,
-Denis

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-07-09 15:38 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-09 11:47 [PATCH 1/3] dhcp-server: Fix debug messages in send_offer Andrew Zaborowski
2021-07-09 11:47 ` [PATCH 2/3] dhcp-server: Fill in netmask & gateway in lease objects Andrew Zaborowski
2021-07-09 11:47 ` [PATCH 3/3] dhcp-server: Add l_dhcp_server_{new,release}_lease Andrew Zaborowski
2021-07-09 15:12 ` [PATCH 1/3] dhcp-server: Fix debug messages in send_offer Denis Kenzior
2021-07-09 15:38   ` Denis Kenzior

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 NNTP newsgroup(s).