750717
From 268080fc19990711a1d1e1acd68a50aa2f6cb5fb Mon Sep 17 00:00:00 2001
750717
From: =?UTF-8?q?Petr=20Men=C5=A1=C3=ADk?= <pemensik@redhat.com>
750717
Date: Fri, 17 Sep 2021 20:12:21 +0200
750717
Subject: [PATCH] Offer alternative DHCPv6 address if requested is taken
750717
750717
In some cases multiple requests might arrive from single DUID. It may
750717
happen just one address is offered to different IAID requests. When
750717
the first request confirms lease, another would be offered alternative
750717
address instead of address in use error.
750717
750717
Includes check on such Rapid commit equivalents and returns NotOnLink
750717
error, required by RFC 8145, if requested address were not on any
750717
supported prefix.
750717
---
750717
 src/rfc3315.c | 39 ++++++++++++++++++++++++++++-----------
750717
 1 file changed, 28 insertions(+), 11 deletions(-)
750717
750717
diff --git a/src/rfc3315.c b/src/rfc3315.c
750717
index 5c2ff97..d1534ad 100644
750717
--- a/src/rfc3315.c
750717
+++ b/src/rfc3315.c
750717
@@ -614,7 +614,7 @@ static int dhcp6_no_relay(struct state *state, int msg_type, void *inbuff, size_
750717
       
750717
     case DHCP6SOLICIT:
750717
       {
750717
-      	int address_assigned = 0;
750717
+	int address_assigned = 0, ia_invalid = 0;
750717
 	/* tags without all prefix-class tags */
750717
 	struct dhcp_netid *solicit_tags;
750717
 	struct dhcp_context *c;
750717
@@ -697,6 +697,8 @@ static int dhcp6_no_relay(struct state *state, int msg_type, void *inbuff, size_
750717
 		    get_context_tag(state, c);
750717
 		    address_assigned = 1;
750717
 		  }
750717
+		else
750717
+		  ia_invalid++;
750717
 	      }
750717
 	    
750717
 	    /* Suggest configured address(es) */
750717
@@ -782,11 +784,26 @@ static int dhcp6_no_relay(struct state *state, int msg_type, void *inbuff, size_
750717
 	    tagif = add_options(state, 0);
750717
 	  }
750717
 	else
750717
-	  { 
750717
+	  {
750717
+	    char *errmsg;
750717
 	    /* no address, return error */
750717
 	    o1 = new_opt6(OPTION6_STATUS_CODE);
750717
-	    put_opt6_short(DHCP6NOADDRS);
750717
-	    put_opt6_string(_("no addresses available"));
750717
+	    if (state->lease_allocate && ia_invalid)
750717
+	      {
750717
+		/* RFC 8415, Section 18.3.2:
750717
+		   If any of the prefixes of the included addresses are not
750717
+		   appropriate for the link to which the client is connected,
750717
+		   the server MUST return the IA to the client with a Status
750717
+		   Code option with the value NotOnLink. */
750717
+		put_opt6_short(DHCP6NOTONLINK);
750717
+		errmsg = _("not on link");
750717
+	      }
750717
+	    else
750717
+	      {
750717
+		put_opt6_short(DHCP6NOADDRS);
750717
+		errmsg = _("no addresses available");
750717
+	      }
750717
+	    put_opt6_string(errmsg);
750717
 	    end_opt6(o1);
750717
 
750717
 	    /* Some clients will ask repeatedly when we're not giving
750717
@@ -795,7 +812,7 @@ static int dhcp6_no_relay(struct state *state, int msg_type, void *inbuff, size_
750717
 	    for (c = state->context; c; c = c->current)
750717
 	      if (!(c->flags & CONTEXT_RA_STATELESS))
750717
 		{
750717
-		  log6_packet(state, state->lease_allocate ? "DHCPREPLY" : "DHCPADVERTISE", NULL, _("no addresses available"));
750717
+		  log6_packet(state, state->lease_allocate ? "DHCPREPLY" : "DHCPADVERTISE", NULL, errmsg);
750717
 		  break;
750717
 		}
750717
 	  }
750717
@@ -831,7 +848,7 @@ static int dhcp6_no_relay(struct state *state, int msg_type, void *inbuff, size_
750717
 		 /* If we get a request with an IA_*A without addresses, treat it exactly like
750717
 		    a SOLICT with rapid commit set. */
750717
 		 save_counter(start);
750717
-		 goto request_no_address; 
750717
+		 goto request_no_address;
750717
 	       }
750717
 
750717
 	    o = build_ia(state, &t1cntr);
750717
@@ -861,11 +878,11 @@ static int dhcp6_no_relay(struct state *state, int msg_type, void *inbuff, size_
750717
 		      }
750717
 		    else if (!check_address(state, &req_addr))
750717
 		      {
750717
-			/* Address leased to another DUID/IAID */
750717
-			o1 = new_opt6(OPTION6_STATUS_CODE);
750717
-			put_opt6_short(DHCP6UNSPEC);
750717
-			put_opt6_string(_("address in use"));
750717
-			end_opt6(o1);
750717
+			/* Address leased to another DUID/IAID.
750717
+			   Find another address for the client, treat it exactly like
750717
+			   a SOLICT with rapid commit set. */
750717
+			save_counter(start);
750717
+			goto request_no_address;
750717
 		      } 
750717
 		    else 
750717
 		      {
750717
-- 
750717
2.31.1
750717