From e7bfd556c079c8b5e7425aed44abc35925b24043 Mon Sep 17 00:00:00 2001
From: Simon Kelley <simon@thekelleys.org.uk>
Date: Mon, 31 Dec 2018 20:51:15 +0000
Subject: [PATCH 17/32] Alter DHCP address selection after DECLINE in
 consec-addr mode. Avoid offering the same address after a recieving a DECLINE
 message to stop an infinite protocol loop. This has long been done in default
 address allocation mode: this adds similar behaviour when allocaing addresses
 consecutively.

Signed-off-by: Kevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>
---
 src/dhcp.c  | 13 +++++++++++++
 src/dhcp6.c | 11 +++++++++--
 2 files changed, 22 insertions(+), 2 deletions(-)

--- a/src/dhcp.c
+++ b/src/dhcp.c
@@ -754,6 +754,19 @@ int address_allocate(struct dhcp_context
 	      if (addr.s_addr == d->router.s_addr)
 		break;
 
+	    /* in consec-ip mode, skip addresses equal to
+	       the number of addresses rejected by clients. This
+	       should avoid the same client being offered the same
+	       address after it has rjected it. */
+	    if (option_bool(OPT_CONSEC_ADDR))
+	      {
+		if (c->addr_epoch)
+		  {
+		    c->addr_epoch--;
+		    d = context; /* d non-NULL skips the address. */
+		  }
+	      }
+	    
 	    /* Addresses which end in .255 and .0 are broken in Windows even when using 
 	       supernetting. ie dhcp-range=192.168.0.1,192.168.1.254,255,255,254.0
 	       then 192.168.0.255 is a valid IP address, but not for Windows as it's
--- a/src/dhcp6.c
+++ b/src/dhcp6.c
@@ -431,8 +431,15 @@ struct dhcp_context *address6_allocate(s
       else
 	{ 
 	  if (!temp_addr && option_bool(OPT_CONSEC_ADDR))
-	    /* seed is largest extant lease addr in this context */
-	    start = lease_find_max_addr6(c) + serial;
+	    {
+	      /* seed is largest extant lease addr in this context,
+		 skip addresses equal to the number of addresses rejected
+		 by clients. This should avoid the same client being offered the same
+		 address after it has rjected it. */
+	      start = lease_find_max_addr6(c) + serial + c->addr_epoch;
+	      if (c->addr_epoch)
+		c->addr_epoch--;
+	    }
 	  else
 	    {
 	      u64 range = 1 + addr6part(&c->end6) - addr6part(&c->start6);
