5a3797e
diff -up dhcp-4.2.0/server/dhcpv6.c.UseMulticast dhcp-4.2.0/server/dhcpv6.c
5a3797e
--- dhcp-4.2.0/server/dhcpv6.c.UseMulticast	2010-06-01 19:30:00.000000000 +0200
5a3797e
+++ dhcp-4.2.0/server/dhcpv6.c	2010-07-21 16:17:30.000000000 +0200
5a3797e
@@ -346,6 +346,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,
c402567
+				opt_state, &global_scope, root_group, 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
5a3797e
@@ -1405,6 +1447,56 @@ lease_to_client(struct data_string *repl
b08ba4e
 						    reply.shared->group);
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);
b08ba4e
+	} else if (no_resources_avail && (reply.ia_count != 0) &&
b08ba4e
+	    (reply.packet->dhcpv6_msg_type == DHCPV6_SOLICIT))
b08ba4e
 	/*
b08ba4e
 	 * RFC3315 section 17.2.2 (Solicit):
b08ba4e
 	 *
5a3797e
@@ -1429,8 +1521,6 @@ lease_to_client(struct data_string *repl
b08ba4e
 	 * the server.
b08ba4e
 	 * Sends a Renew/Rebind if the IA is not in the Reply message.
b08ba4e
 	 */
b08ba4e
-	if (no_resources_avail && (reply.ia_count != 0) &&
b08ba4e
-	    (reply.packet->dhcpv6_msg_type == DHCPV6_SOLICIT))
b08ba4e
 	{
b08ba4e
 		/* Set the NoAddrsAvail status code. */
b08ba4e
 		if (!set_status_code(STATUS_NoAddrsAvail,
5a3797e
@@ -4128,7 +4218,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;
5a3797e
@@ -4443,7 +4532,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;
5a3797e
@@ -4688,18 +4776,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
5a3797e
@@ -4838,6 +4968,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)) {
5a3797e
@@ -4883,7 +5014,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) {
5a3797e
@@ -5355,7 +5485,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;