581dc5f
diff -up dhcp-4.1.1/server/dhcpv6.c.UseMulticast dhcp-4.1.1/server/dhcpv6.c
581dc5f
--- dhcp-4.1.1/server/dhcpv6.c.UseMulticast	2009-09-30 23:01:20.000000000 +0200
0732bf5
+++ dhcp-4.1.1/server/dhcpv6.c	2010-03-15 12:29:32.000000000 +0100
0732bf5
@@ -345,6 +345,48 @@ generate_new_server_duid(void) {
581dc5f
 }
581dc5f
 
581dc5f
 /*
0732bf5
+ * Is the D6O_UNICAST option defined in dhcpd.conf ?
0732bf5
+ */
0732bf5
+static isc_boolean_t unicast_option_defined;
0732bf5
+
0732bf5
+/*
0732bf5
+ * Did we already search dhcpd.conf for D6O_UNICAST option ?
0732bf5
+ * We need to store it here to not parse dhcpd.conf repeatedly.
0732bf5
+ */
0732bf5
+static isc_boolean_t unicast_option_parsed = ISC_FALSE;
0732bf5
+
0732bf5
+
0732bf5
+/*
0732bf5
+ * Is the D6O_UNICAST option defined in dhcpd.conf ?
581dc5f
+ */
581dc5f
+isc_boolean_t
581dc5f
+is_unicast_option_defined(void) {
581dc5f
+	struct option_state *opt_state;
581dc5f
+	struct option_cache *oc;
581dc5f
+
0732bf5
+	/*
0732bf5
+	 * If we are looking for the unicast option for the first time
0732bf5
+	 */
0732bf5
+	if (unicast_option_parsed == ISC_FALSE) {
0732bf5
+		unicast_option_parsed = ISC_TRUE;
0732bf5
+		opt_state = NULL;
0732bf5
+		if (!option_state_allocate(&opt_state, MDL)) {
0732bf5
+			log_fatal("No memory for option state.");
0732bf5
+		}
581dc5f
+
0732bf5
+		execute_statements_in_scope(NULL, NULL, NULL, NULL, NULL,
0732bf5
+				opt_state, &global_scope, root_group, NULL);
581dc5f
+
0732bf5
+		oc = lookup_option(&dhcpv6_universe, opt_state, D6O_UNICAST);
0732bf5
+		unicast_option_defined = (oc != NULL);
581dc5f
+
0732bf5
+		option_state_dereference(&opt_state, MDL);
0732bf5
+	}
581dc5f
+
0732bf5
+	return (unicast_option_defined);
581dc5f
+}
581dc5f
+
581dc5f
+/*
0732bf5
  * Get the client identifier from the packet.
0732bf5
  */
0732bf5
 isc_result_t
0732bf5
@@ -1398,6 +1440,56 @@ lease_to_client(struct data_string *repl
581dc5f
 						    reply.shared->group);
581dc5f
 	}
581dc5f
 
581dc5f
+	/* reject unicast message, unless we set unicast option */
581dc5f
+	if ((packet->unicast == ISC_TRUE) && !is_unicast_option_defined())
581dc5f
+	/*
581dc5f
+	 * RFC3315 section 18.2.1 (Request):
581dc5f
+	 *
581dc5f
+	 * When the server receives a Request message via unicast from a client
581dc5f
+	 * to which the server has not sent a unicast option, the server
581dc5f
+	 * discards the Request message and responds with a Reply message
581dc5f
+	 * containing a Status Code option with the value UseMulticast, a Server
581dc5f
+	 * Identifier option containing the server's DUID, the Client Identifier
581dc5f
+	 * option from the client message, and no other options.
581dc5f
+	 *
581dc5f
+	 * Section 18.2.3 (Renew):
581dc5f
+	 *
581dc5f
+	 * When the server receives a Renew message via unicast from a client to
581dc5f
+	 * which the server has not sent a unicast option, the server discards
581dc5f
+	 * the Renew message and responds with a Reply message containing a
581dc5f
+	 * Status Code option with the value UseMulticast, a Server Identifier
581dc5f
+	 * option containing the server's DUID, the Client Identifier option
581dc5f
+	 * from the client message, and no other options.
581dc5f
+	 */
581dc5f
+	{
581dc5f
+		/* Set the UseMulticast status code. */
581dc5f
+		if (!set_status_code(STATUS_UseMulticast,
581dc5f
+					"Unicast not allowed by server.",
581dc5f
+					reply.opt_state)) {
581dc5f
+			log_error("lease_to_client: Unable to set "
581dc5f
+					"UseMulticast status code.");
581dc5f
+			goto exit;
581dc5f
+		}
581dc5f
+
581dc5f
+		/* Rewind the cursor to the start. */
581dc5f
+		reply.cursor = REPLY_OPTIONS_INDEX;
581dc5f
+
581dc5f
+		/*
581dc5f
+		 * Produce an reply that includes only:
581dc5f
+		 *
581dc5f
+		 * Status code.
581dc5f
+		 * Server DUID.
581dc5f
+		 * Client DUID.
581dc5f
+		 */
581dc5f
+		reply.cursor += store_options6((char *)reply.buf.data +
581dc5f
+					reply.cursor,
581dc5f
+					sizeof(reply.buf) -
581dc5f
+					reply.cursor,
581dc5f
+					reply.opt_state, reply.packet,
581dc5f
+					required_opts_NAA,
581dc5f
+					NULL);
581dc5f
+	} else if (no_resources_avail && (reply.ia_count != 0) &&
581dc5f
+	    (reply.packet->dhcpv6_msg_type == DHCPV6_SOLICIT))
581dc5f
 	/*
581dc5f
 	 * RFC3315 section 17.2.2 (Solicit):
581dc5f
 	 *
0732bf5
@@ -1422,8 +1514,6 @@ lease_to_client(struct data_string *repl
581dc5f
 	 * the server.
581dc5f
 	 * Sends a Renew/Rebind if the IA is not in the Reply message.
581dc5f
 	 */
581dc5f
-	if (no_resources_avail && (reply.ia_count != 0) &&
581dc5f
-	    (reply.packet->dhcpv6_msg_type == DHCPV6_SOLICIT))
581dc5f
 	{
581dc5f
 		/* Set the NoAddrsAvail status code. */
581dc5f
 		if (!set_status_code(STATUS_NoAddrsAvail,
0732bf5
@@ -4097,7 +4187,6 @@ dhcpv6_solicit(struct data_string *reply
581dc5f
  * Very similar to Solicit handling, except the server DUID is required.
581dc5f
  */
581dc5f
 
581dc5f
-/* TODO: reject unicast messages, unless we set unicast option */
581dc5f
 static void
581dc5f
 dhcpv6_request(struct data_string *reply_ret, struct packet *packet) {
581dc5f
 	struct data_string client_id;
0732bf5
@@ -4412,7 +4501,6 @@ exit:
581dc5f
  * except for the error code of when addresses don't match.
581dc5f
  */
581dc5f
 
581dc5f
-/* TODO: reject unicast messages, unless we set unicast option */
581dc5f
 static void
581dc5f
 dhcpv6_renew(struct data_string *reply, struct packet *packet) {
581dc5f
 	struct data_string client_id;
0732bf5
@@ -4653,18 +4741,60 @@ iterate_over_ia_na(struct data_string *r
581dc5f
 		goto exit;
581dc5f
 	}
581dc5f
 
581dc5f
-	snprintf(status_msg, sizeof(status_msg), "%s received.", packet_type);
581dc5f
-	if (!set_status_code(STATUS_Success, status_msg, opt_state)) {
581dc5f
-		goto exit;
581dc5f
-	}
581dc5f
+	/* reject unicast message, unless we set unicast option */
581dc5f
+	if ((packet->unicast == ISC_TRUE) && !is_unicast_option_defined()) {
581dc5f
+		/*
581dc5f
+		 * RFC3315 section 18.2.6 (Release):
581dc5f
+		 *
581dc5f
+		 * When the server receives a Release message via unicast from a client
581dc5f
+		 * to which the server has not sent a unicast option, the server
581dc5f
+		 * discards the Release message and responds with a Reply message
581dc5f
+		 * containing a Status Code option with value UseMulticast, a Server
581dc5f
+		 * Identifier option containing the server's DUID, the Client Identifier
581dc5f
+		 * option from the client message, and no other options.
581dc5f
+		 *
581dc5f
+		 * Section 18.2.7 (Decline):
581dc5f
+		 *
581dc5f
+		 * When the server receives a Decline message via unicast from a client
581dc5f
+		 * to which the server has not sent a unicast option, the server
581dc5f
+		 * discards the Decline message and responds with a Reply message
581dc5f
+		 * containing a Status Code option with the value UseMulticast, a Server
581dc5f
+		 * Identifier option containing the server's DUID, the Client Identifier
581dc5f
+		 * option from the client message, and no other options.
581dc5f
+		 */
581dc5f
+		snprintf(status_msg, sizeof(status_msg),
581dc5f
+				 "%s received unicast.", packet_type);
581dc5f
+		if (!set_status_code(STATUS_UseMulticast, status_msg, opt_state)) {
581dc5f
+			goto exit;
581dc5f
+		}
581dc5f
 
581dc5f
-	/* 
581dc5f
-	 * Add our options that are not associated with any IA_NA or IA_TA. 
581dc5f
-	 */
581dc5f
-	reply_ofs += store_options6(reply_data+reply_ofs,
581dc5f
-				    sizeof(reply_data)-reply_ofs, 
581dc5f
+		/*
581dc5f
+		 * Produce an reply that includes only:
581dc5f
+		 *
581dc5f
+		 * Status code.
581dc5f
+		 * Server DUID.
581dc5f
+		 * Client DUID.
581dc5f
+		 */
581dc5f
+		reply_ofs += store_options6(reply_data+reply_ofs,
581dc5f
+				    sizeof(reply_data)-reply_ofs,
581dc5f
 				    opt_state, packet,
581dc5f
-				    required_opts, NULL);
581dc5f
+				    required_opts_NAA, NULL);
581dc5f
+
581dc5f
+		goto return_reply;
581dc5f
+	} else {
581dc5f
+		snprintf(status_msg, sizeof(status_msg), "%s received.", packet_type);
581dc5f
+		if (!set_status_code(STATUS_Success, status_msg, opt_state)) {
581dc5f
+			goto exit;
581dc5f
+		}
581dc5f
+
581dc5f
+		/*
581dc5f
+		 * Add our options that are not associated with any IA_NA or IA_TA.
581dc5f
+		 */
581dc5f
+		reply_ofs += store_options6(reply_data+reply_ofs,
581dc5f
+					    sizeof(reply_data)-reply_ofs,
581dc5f
+					    opt_state, packet,
581dc5f
+					    required_opts, NULL);
581dc5f
+	}
581dc5f
 
581dc5f
 	/*
581dc5f
 	 * Loop through the IA_NA reported by the client, and deal with
0732bf5
@@ -4802,6 +4932,7 @@ iterate_over_ia_na(struct data_string *r
581dc5f
 	/* 
581dc5f
 	 * Return our reply to the caller.
581dc5f
 	 */
581dc5f
+return_reply:
581dc5f
 	reply_ret->len = reply_ofs;
581dc5f
 	reply_ret->buffer = NULL;
581dc5f
 	if (!buffer_allocate(&reply_ret->buffer, reply_ofs, MDL)) {
0732bf5
@@ -4847,7 +4978,6 @@ exit:
581dc5f
  * we still need to be aware of this possibility.
581dc5f
  */
581dc5f
 
581dc5f
-/* TODO: reject unicast messages, unless we set unicast option */
581dc5f
 /* TODO: IA_TA */
581dc5f
 static void
581dc5f
 dhcpv6_decline(struct data_string *reply, struct packet *packet) {
0732bf5
@@ -5314,7 +5444,6 @@ exit:
581dc5f
  * Release means a client is done with the leases.
581dc5f
  */
581dc5f
 
581dc5f
-/* TODO: reject unicast messages, unless we set unicast option */
581dc5f
 static void
581dc5f
 dhcpv6_release(struct data_string *reply, struct packet *packet) {
581dc5f
 	struct data_string client_id;