e83fb19
diff -up dhcp-4.3.0a1/server/dhcpv6.c.UseMulticast dhcp-4.3.0a1/server/dhcpv6.c
e83fb19
--- dhcp-4.3.0a1/server/dhcpv6.c.UseMulticast	2013-12-11 01:25:12.000000000 +0100
e83fb19
+++ dhcp-4.3.0a1/server/dhcpv6.c	2013-12-19 15:54:46.934932371 +0100
e83fb19
@@ -353,6 +353,48 @@ generate_new_server_duid(void) {
b08ba4e
 }
b08ba4e
 
b08ba4e
 /*
c402567
+ * Is the D6O_UNICAST option defined in dhcpd.conf ?
c402567
+ */
c402567
+static isc_boolean_t unicast_option_defined;
c402567
+
c402567
+/*
c402567
+ * Did we already search dhcpd.conf for D6O_UNICAST option ?
c402567
+ * We need to store it here to not parse dhcpd.conf repeatedly.
c402567
+ */
c402567
+static isc_boolean_t unicast_option_parsed = ISC_FALSE;
c402567
+
c402567
+
c402567
+/*
c402567
+ * Is the D6O_UNICAST option defined in dhcpd.conf ?
b08ba4e
+ */
b08ba4e
+isc_boolean_t
b08ba4e
+is_unicast_option_defined(void) {
b08ba4e
+	struct option_state *opt_state;
b08ba4e
+	struct option_cache *oc;
b08ba4e
+
c402567
+	/*
c402567
+	 * If we are looking for the unicast option for the first time
c402567
+	 */
c402567
+	if (unicast_option_parsed == ISC_FALSE) {
c402567
+		unicast_option_parsed = ISC_TRUE;
c402567
+		opt_state = NULL;
c402567
+		if (!option_state_allocate(&opt_state, MDL)) {
c402567
+			log_fatal("No memory for option state.");
c402567
+		}
b08ba4e
+
c402567
+		execute_statements_in_scope(NULL, NULL, NULL, NULL, NULL,
9ca3958
+			     opt_state, &global_scope, root_group, NULL, NULL);
b08ba4e
+
c402567
+		oc = lookup_option(&dhcpv6_universe, opt_state, D6O_UNICAST);
c402567
+		unicast_option_defined = (oc != NULL);
b08ba4e
+
c402567
+		option_state_dereference(&opt_state, MDL);
c402567
+	}
b08ba4e
+
c402567
+	return (unicast_option_defined);
b08ba4e
+}
b08ba4e
+
b08ba4e
+/*
c402567
  * Get the client identifier from the packet.
c402567
  */
c402567
 isc_result_t
e83fb19
@@ -1498,6 +1540,56 @@ lease_to_client(struct data_string *repl
e83fb19
 						    reply.shared->group, NULL);
b08ba4e
 	}
b08ba4e
 
b08ba4e
+	/* reject unicast message, unless we set unicast option */
b08ba4e
+	if ((packet->unicast == ISC_TRUE) && !is_unicast_option_defined())
b08ba4e
+	/*
b08ba4e
+	 * RFC3315 section 18.2.1 (Request):
b08ba4e
+	 *
b08ba4e
+	 * When the server receives a Request message via unicast from a client
b08ba4e
+	 * to which the server has not sent a unicast option, the server
b08ba4e
+	 * discards the Request message and responds with a Reply message
b08ba4e
+	 * containing a Status Code option with the value UseMulticast, a Server
b08ba4e
+	 * Identifier option containing the server's DUID, the Client Identifier
b08ba4e
+	 * option from the client message, and no other options.
b08ba4e
+	 *
b08ba4e
+	 * Section 18.2.3 (Renew):
b08ba4e
+	 *
b08ba4e
+	 * When the server receives a Renew message via unicast from a client to
b08ba4e
+	 * which the server has not sent a unicast option, the server discards
b08ba4e
+	 * the Renew message and responds with a Reply message containing a
b08ba4e
+	 * Status Code option with the value UseMulticast, a Server Identifier
b08ba4e
+	 * option containing the server's DUID, the Client Identifier option
b08ba4e
+	 * from the client message, and no other options.
b08ba4e
+	 */
b08ba4e
+	{
b08ba4e
+		/* Set the UseMulticast status code. */
b08ba4e
+		if (!set_status_code(STATUS_UseMulticast,
b08ba4e
+					"Unicast not allowed by server.",
b08ba4e
+					reply.opt_state)) {
b08ba4e
+			log_error("lease_to_client: Unable to set "
b08ba4e
+					"UseMulticast status code.");
b08ba4e
+			goto exit;
b08ba4e
+		}
b08ba4e
+
b08ba4e
+		/* Rewind the cursor to the start. */
b08ba4e
+		reply.cursor = REPLY_OPTIONS_INDEX;
b08ba4e
+
b08ba4e
+		/*
b08ba4e
+		 * Produce an reply that includes only:
b08ba4e
+		 *
b08ba4e
+		 * Status code.
b08ba4e
+		 * Server DUID.
b08ba4e
+		 * Client DUID.
b08ba4e
+		 */
b08ba4e
+		reply.cursor += store_options6((char *)reply.buf.data +
b08ba4e
+					reply.cursor,
b08ba4e
+					sizeof(reply.buf) -
b08ba4e
+					reply.cursor,
b08ba4e
+					reply.opt_state, reply.packet,
b08ba4e
+					required_opts_NAA,
b08ba4e
+					NULL);
d5d042f
+	}
d5d042f
+
b08ba4e
 	/*
b08ba4e
 	 * RFC3315 section 17.2.2 (Solicit):
b08ba4e
 	 *
e83fb19
@@ -1523,8 +1615,8 @@ lease_to_client(struct data_string *repl
b08ba4e
 	 * Sends a Renew/Rebind if the IA is not in the Reply message.
b08ba4e
 	 */
d5d042f
 #if defined (RFC3315_PRE_ERRATA_2010_08)
b08ba4e
-	if (no_resources_avail && (reply.ia_count != 0) &&
b08ba4e
-	    (reply.packet->dhcpv6_msg_type == DHCPV6_SOLICIT))
d5d042f
+	else if (no_resources_avail && (reply.ia_count != 0) &&
d5d042f
+		 (reply.packet->dhcpv6_msg_type == DHCPV6_SOLICIT))
b08ba4e
 	{
b08ba4e
 		/* Set the NoAddrsAvail status code. */
b08ba4e
 		if (!set_status_code(STATUS_NoAddrsAvail,
e83fb19
@@ -1571,6 +1663,7 @@ lease_to_client(struct data_string *repl
d5d042f
 	 * Having stored the client's IA's, store any options that
d5d042f
 	 * will fit in the remaining space.
d5d042f
 	 */
d5d042f
+	else 
d5d042f
 	reply.cursor += store_options6((char *)reply.buf.data + reply.cursor,
d5d042f
 				       sizeof(reply.buf) - reply.cursor,
d5d042f
 				       reply.opt_state, reply.packet,
e83fb19
@@ -4688,7 +4781,6 @@ dhcpv6_solicit(struct data_string *reply
b08ba4e
  * Very similar to Solicit handling, except the server DUID is required.
b08ba4e
  */
b08ba4e
 
b08ba4e
-/* TODO: reject unicast messages, unless we set unicast option */
b08ba4e
 static void
b08ba4e
 dhcpv6_request(struct data_string *reply_ret, struct packet *packet) {
b08ba4e
 	struct data_string client_id;
e83fb19
@@ -5018,7 +5110,6 @@ exit:
b08ba4e
  * except for the error code of when addresses don't match.
b08ba4e
  */
b08ba4e
 
b08ba4e
-/* TODO: reject unicast messages, unless we set unicast option */
b08ba4e
 static void
b08ba4e
 dhcpv6_renew(struct data_string *reply, struct packet *packet) {
b08ba4e
 	struct data_string client_id;
e83fb19
@@ -5262,18 +5353,60 @@ iterate_over_ia_na(struct data_string *r
b08ba4e
 		goto exit;
b08ba4e
 	}
b08ba4e
 
b08ba4e
-	snprintf(status_msg, sizeof(status_msg), "%s received.", packet_type);
b08ba4e
-	if (!set_status_code(STATUS_Success, status_msg, opt_state)) {
b08ba4e
-		goto exit;
b08ba4e
-	}
b08ba4e
+	/* reject unicast message, unless we set unicast option */
b08ba4e
+	if ((packet->unicast == ISC_TRUE) && !is_unicast_option_defined()) {
b08ba4e
+		/*
b08ba4e
+		 * RFC3315 section 18.2.6 (Release):
b08ba4e
+		 *
b08ba4e
+		 * When the server receives a Release message via unicast from a client
b08ba4e
+		 * to which the server has not sent a unicast option, the server
b08ba4e
+		 * discards the Release message and responds with a Reply message
b08ba4e
+		 * containing a Status Code option with value UseMulticast, a Server
b08ba4e
+		 * Identifier option containing the server's DUID, the Client Identifier
b08ba4e
+		 * option from the client message, and no other options.
b08ba4e
+		 *
b08ba4e
+		 * Section 18.2.7 (Decline):
b08ba4e
+		 *
b08ba4e
+		 * When the server receives a Decline message via unicast from a client
b08ba4e
+		 * to which the server has not sent a unicast option, the server
b08ba4e
+		 * discards the Decline message and responds with a Reply message
b08ba4e
+		 * containing a Status Code option with the value UseMulticast, a Server
b08ba4e
+		 * Identifier option containing the server's DUID, the Client Identifier
b08ba4e
+		 * option from the client message, and no other options.
b08ba4e
+		 */
b08ba4e
+		snprintf(status_msg, sizeof(status_msg),
b08ba4e
+				 "%s received unicast.", packet_type);
b08ba4e
+		if (!set_status_code(STATUS_UseMulticast, status_msg, opt_state)) {
b08ba4e
+			goto exit;
b08ba4e
+		}
b08ba4e
 
b08ba4e
-	/* 
b08ba4e
-	 * Add our options that are not associated with any IA_NA or IA_TA. 
b08ba4e
-	 */
b08ba4e
-	reply_ofs += store_options6(reply_data+reply_ofs,
b08ba4e
-				    sizeof(reply_data)-reply_ofs, 
b08ba4e
+		/*
b08ba4e
+		 * Produce an reply that includes only:
b08ba4e
+		 *
b08ba4e
+		 * Status code.
b08ba4e
+		 * Server DUID.
b08ba4e
+		 * Client DUID.
b08ba4e
+		 */
b08ba4e
+		reply_ofs += store_options6(reply_data+reply_ofs,
b08ba4e
+				    sizeof(reply_data)-reply_ofs,
b08ba4e
 				    opt_state, packet,
b08ba4e
-				    required_opts, NULL);
b08ba4e
+				    required_opts_NAA, NULL);
b08ba4e
+
b08ba4e
+		goto return_reply;
b08ba4e
+	} else {
b08ba4e
+		snprintf(status_msg, sizeof(status_msg), "%s received.", packet_type);
b08ba4e
+		if (!set_status_code(STATUS_Success, status_msg, opt_state)) {
b08ba4e
+			goto exit;
b08ba4e
+		}
b08ba4e
+
b08ba4e
+		/*
b08ba4e
+		 * Add our options that are not associated with any IA_NA or IA_TA.
b08ba4e
+		 */
b08ba4e
+		reply_ofs += store_options6(reply_data+reply_ofs,
b08ba4e
+					    sizeof(reply_data)-reply_ofs,
b08ba4e
+					    opt_state, packet,
b08ba4e
+					    required_opts, NULL);
b08ba4e
+	}
b08ba4e
 
b08ba4e
 	/*
b08ba4e
 	 * Loop through the IA_NA reported by the client, and deal with
e83fb19
@@ -5411,6 +5544,7 @@ iterate_over_ia_na(struct data_string *r
b08ba4e
 	/* 
b08ba4e
 	 * Return our reply to the caller.
b08ba4e
 	 */
b08ba4e
+return_reply:
b08ba4e
 	reply_ret->len = reply_ofs;
b08ba4e
 	reply_ret->buffer = NULL;
b08ba4e
 	if (!buffer_allocate(&reply_ret->buffer, reply_ofs, MDL)) {
e83fb19
@@ -5456,7 +5590,6 @@ exit:
b08ba4e
  * we still need to be aware of this possibility.
b08ba4e
  */
b08ba4e
 
b08ba4e
-/* TODO: reject unicast messages, unless we set unicast option */
b08ba4e
 /* TODO: IA_TA */
b08ba4e
 static void
b08ba4e
 dhcpv6_decline(struct data_string *reply, struct packet *packet) {
e83fb19
@@ -5926,7 +6059,6 @@ exit:
b08ba4e
  * Release means a client is done with the leases.
b08ba4e
  */
b08ba4e
 
b08ba4e
-/* TODO: reject unicast messages, unless we set unicast option */
b08ba4e
 static void
b08ba4e
 dhcpv6_release(struct data_string *reply, struct packet *packet) {
b08ba4e
 	struct data_string client_id;