45d60a
diff -up dhcp-4.2.4b1/server/dhcpv6.c.UseMulticast dhcp-4.2.4b1/server/dhcpv6.c
45d60a
--- dhcp-4.2.4b1/server/dhcpv6.c.UseMulticast	2012-04-11 00:14:04.000000000 +0200
45d60a
+++ dhcp-4.2.4b1/server/dhcpv6.c	2012-04-16 19:21:43.575923732 +0200
45d60a
@@ -346,6 +346,48 @@ generate_new_server_duid(void) {
45d60a
 }
45d60a
 
45d60a
 /*
45d60a
+ * Is the D6O_UNICAST option defined in dhcpd.conf ?
45d60a
+ */
45d60a
+static isc_boolean_t unicast_option_defined;
45d60a
+
45d60a
+/*
45d60a
+ * Did we already search dhcpd.conf for D6O_UNICAST option ?
45d60a
+ * We need to store it here to not parse dhcpd.conf repeatedly.
45d60a
+ */
45d60a
+static isc_boolean_t unicast_option_parsed = ISC_FALSE;
45d60a
+
45d60a
+
45d60a
+/*
45d60a
+ * Is the D6O_UNICAST option defined in dhcpd.conf ?
45d60a
+ */
45d60a
+isc_boolean_t
45d60a
+is_unicast_option_defined(void) {
45d60a
+	struct option_state *opt_state;
45d60a
+	struct option_cache *oc;
45d60a
+
45d60a
+	/*
45d60a
+	 * If we are looking for the unicast option for the first time
45d60a
+	 */
45d60a
+	if (unicast_option_parsed == ISC_FALSE) {
45d60a
+		unicast_option_parsed = ISC_TRUE;
45d60a
+		opt_state = NULL;
45d60a
+		if (!option_state_allocate(&opt_state, MDL)) {
45d60a
+			log_fatal("No memory for option state.");
45d60a
+		}
45d60a
+
45d60a
+		execute_statements_in_scope(NULL, NULL, NULL, NULL, NULL,
45d60a
+				opt_state, &global_scope, root_group, NULL);
45d60a
+
45d60a
+		oc = lookup_option(&dhcpv6_universe, opt_state, D6O_UNICAST);
45d60a
+		unicast_option_defined = (oc != NULL);
45d60a
+
45d60a
+		option_state_dereference(&opt_state, MDL);
45d60a
+	}
45d60a
+
45d60a
+	return (unicast_option_defined);
45d60a
+}
45d60a
+
45d60a
+/*
45d60a
  * Get the client identifier from the packet.
45d60a
  */
45d60a
 isc_result_t
45d60a
@@ -1404,6 +1446,56 @@ lease_to_client(struct data_string *repl
45d60a
 						    reply.shared->group);
45d60a
 	}
45d60a
 
45d60a
+	/* reject unicast message, unless we set unicast option */
45d60a
+	if ((packet->unicast == ISC_TRUE) && !is_unicast_option_defined())
45d60a
+	/*
45d60a
+	 * RFC3315 section 18.2.1 (Request):
45d60a
+	 *
45d60a
+	 * When the server receives a Request message via unicast from a client
45d60a
+	 * to which the server has not sent a unicast option, the server
45d60a
+	 * discards the Request message and responds with a Reply message
45d60a
+	 * containing a Status Code option with the value UseMulticast, a Server
45d60a
+	 * Identifier option containing the server's DUID, the Client Identifier
45d60a
+	 * option from the client message, and no other options.
45d60a
+	 *
45d60a
+	 * Section 18.2.3 (Renew):
45d60a
+	 *
45d60a
+	 * When the server receives a Renew message via unicast from a client to
45d60a
+	 * which the server has not sent a unicast option, the server discards
45d60a
+	 * the Renew message and responds with a Reply message containing a
45d60a
+	 * Status Code option with the value UseMulticast, a Server Identifier
45d60a
+	 * option containing the server's DUID, the Client Identifier option
45d60a
+	 * from the client message, and no other options.
45d60a
+	 */
45d60a
+	{
45d60a
+		/* Set the UseMulticast status code. */
45d60a
+		if (!set_status_code(STATUS_UseMulticast,
45d60a
+					"Unicast not allowed by server.",
45d60a
+					reply.opt_state)) {
45d60a
+			log_error("lease_to_client: Unable to set "
45d60a
+					"UseMulticast status code.");
45d60a
+			goto exit;
45d60a
+		}
45d60a
+
45d60a
+		/* Rewind the cursor to the start. */
45d60a
+		reply.cursor = REPLY_OPTIONS_INDEX;
45d60a
+
45d60a
+		/*
45d60a
+		 * Produce an reply that includes only:
45d60a
+		 *
45d60a
+		 * Status code.
45d60a
+		 * Server DUID.
45d60a
+		 * Client DUID.
45d60a
+		 */
45d60a
+		reply.cursor += store_options6((char *)reply.buf.data +
45d60a
+					reply.cursor,
45d60a
+					sizeof(reply.buf) -
45d60a
+					reply.cursor,
45d60a
+					reply.opt_state, reply.packet,
45d60a
+					required_opts_NAA,
45d60a
+					NULL);
45d60a
+	}
45d60a
+
45d60a
 	/*
45d60a
 	 * RFC3315 section 17.2.2 (Solicit):
45d60a
 	 *
45d60a
@@ -1429,8 +1521,8 @@ lease_to_client(struct data_string *repl
45d60a
 	 * Sends a Renew/Rebind if the IA is not in the Reply message.
45d60a
 	 */
45d60a
 #if defined (RFC3315_PRE_ERRATA_2010_08)
45d60a
-	if (no_resources_avail && (reply.ia_count != 0) &&
45d60a
-	    (reply.packet->dhcpv6_msg_type == DHCPV6_SOLICIT))
45d60a
+	else if (no_resources_avail && (reply.ia_count != 0) &&
45d60a
+		 (reply.packet->dhcpv6_msg_type == DHCPV6_SOLICIT))
45d60a
 	{
45d60a
 		/* Set the NoAddrsAvail status code. */
45d60a
 		if (!set_status_code(STATUS_NoAddrsAvail,
45d60a
@@ -1477,6 +1569,7 @@ lease_to_client(struct data_string *repl
45d60a
 	 * Having stored the client's IA's, store any options that
45d60a
 	 * will fit in the remaining space.
45d60a
 	 */
45d60a
+	else 
45d60a
 	reply.cursor += store_options6((char *)reply.buf.data + reply.cursor,
45d60a
 				       sizeof(reply.buf) - reply.cursor,
45d60a
 				       reply.opt_state, reply.packet,
45d60a
@@ -4126,7 +4219,6 @@ dhcpv6_solicit(struct data_string *reply
45d60a
  * Very similar to Solicit handling, except the server DUID is required.
45d60a
  */
45d60a
 
45d60a
-/* TODO: reject unicast messages, unless we set unicast option */
45d60a
 static void
45d60a
 dhcpv6_request(struct data_string *reply_ret, struct packet *packet) {
45d60a
 	struct data_string client_id;
45d60a
@@ -4456,7 +4548,6 @@ exit:
45d60a
  * except for the error code of when addresses don't match.
45d60a
  */
45d60a
 
45d60a
-/* TODO: reject unicast messages, unless we set unicast option */
45d60a
 static void
45d60a
 dhcpv6_renew(struct data_string *reply, struct packet *packet) {
45d60a
 	struct data_string client_id;
45d60a
@@ -4700,18 +4791,60 @@ iterate_over_ia_na(struct data_string *r
45d60a
 		goto exit;
45d60a
 	}
45d60a
 
45d60a
-	snprintf(status_msg, sizeof(status_msg), "%s received.", packet_type);
45d60a
-	if (!set_status_code(STATUS_Success, status_msg, opt_state)) {
45d60a
-		goto exit;
45d60a
-	}
45d60a
+	/* reject unicast message, unless we set unicast option */
45d60a
+	if ((packet->unicast == ISC_TRUE) && !is_unicast_option_defined()) {
45d60a
+		/*
45d60a
+		 * RFC3315 section 18.2.6 (Release):
45d60a
+		 *
45d60a
+		 * When the server receives a Release message via unicast from a client
45d60a
+		 * to which the server has not sent a unicast option, the server
45d60a
+		 * discards the Release message and responds with a Reply message
45d60a
+		 * containing a Status Code option with value UseMulticast, a Server
45d60a
+		 * Identifier option containing the server's DUID, the Client Identifier
45d60a
+		 * option from the client message, and no other options.
45d60a
+		 *
45d60a
+		 * Section 18.2.7 (Decline):
45d60a
+		 *
45d60a
+		 * When the server receives a Decline message via unicast from a client
45d60a
+		 * to which the server has not sent a unicast option, the server
45d60a
+		 * discards the Decline message and responds with a Reply message
45d60a
+		 * containing a Status Code option with the value UseMulticast, a Server
45d60a
+		 * Identifier option containing the server's DUID, the Client Identifier
45d60a
+		 * option from the client message, and no other options.
45d60a
+		 */
45d60a
+		snprintf(status_msg, sizeof(status_msg),
45d60a
+				 "%s received unicast.", packet_type);
45d60a
+		if (!set_status_code(STATUS_UseMulticast, status_msg, opt_state)) {
45d60a
+			goto exit;
45d60a
+		}
45d60a
 
45d60a
-	/* 
45d60a
-	 * Add our options that are not associated with any IA_NA or IA_TA. 
45d60a
-	 */
45d60a
-	reply_ofs += store_options6(reply_data+reply_ofs,
45d60a
-				    sizeof(reply_data)-reply_ofs, 
45d60a
+		/*
45d60a
+		 * Produce an reply that includes only:
45d60a
+		 *
45d60a
+		 * Status code.
45d60a
+		 * Server DUID.
45d60a
+		 * Client DUID.
45d60a
+		 */
45d60a
+		reply_ofs += store_options6(reply_data+reply_ofs,
45d60a
+				    sizeof(reply_data)-reply_ofs,
45d60a
 				    opt_state, packet,
45d60a
-				    required_opts, NULL);
45d60a
+				    required_opts_NAA, NULL);
45d60a
+
45d60a
+		goto return_reply;
45d60a
+	} else {
45d60a
+		snprintf(status_msg, sizeof(status_msg), "%s received.", packet_type);
45d60a
+		if (!set_status_code(STATUS_Success, status_msg, opt_state)) {
45d60a
+			goto exit;
45d60a
+		}
45d60a
+
45d60a
+		/*
45d60a
+		 * Add our options that are not associated with any IA_NA or IA_TA.
45d60a
+		 */
45d60a
+		reply_ofs += store_options6(reply_data+reply_ofs,
45d60a
+					    sizeof(reply_data)-reply_ofs,
45d60a
+					    opt_state, packet,
45d60a
+					    required_opts, NULL);
45d60a
+	}
45d60a
 
45d60a
 	/*
45d60a
 	 * Loop through the IA_NA reported by the client, and deal with
45d60a
@@ -4849,6 +4982,7 @@ iterate_over_ia_na(struct data_string *r
45d60a
 	/* 
45d60a
 	 * Return our reply to the caller.
45d60a
 	 */
45d60a
+return_reply:
45d60a
 	reply_ret->len = reply_ofs;
45d60a
 	reply_ret->buffer = NULL;
45d60a
 	if (!buffer_allocate(&reply_ret->buffer, reply_ofs, MDL)) {
45d60a
@@ -4894,7 +5028,6 @@ exit:
45d60a
  * we still need to be aware of this possibility.
45d60a
  */
45d60a
 
45d60a
-/* TODO: reject unicast messages, unless we set unicast option */
45d60a
 /* TODO: IA_TA */
45d60a
 static void
45d60a
 dhcpv6_decline(struct data_string *reply, struct packet *packet) {
45d60a
@@ -5364,7 +5497,6 @@ exit:
45d60a
  * Release means a client is done with the leases.
45d60a
  */
45d60a
 
45d60a
-/* TODO: reject unicast messages, unless we set unicast option */
45d60a
 static void
45d60a
 dhcpv6_release(struct data_string *reply, struct packet *packet) {
45d60a
 	struct data_string client_id;