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