5a3797
diff -up dhcp-4.2.0/client/dhc6.c.sendDecline dhcp-4.2.0/client/dhc6.c
5a3797
--- dhcp-4.2.0/client/dhc6.c.sendDecline	2009-11-20 02:48:58.000000000 +0100
5a3797
+++ dhcp-4.2.0/client/dhc6.c	2010-07-21 16:18:51.000000000 +0200
21b8c4
@@ -95,6 +95,8 @@ void do_select6(void *input);
21b8c4
 void do_refresh6(void *input);
21b8c4
 static void do_release6(void *input);
21b8c4
 static void start_bound(struct client_state *client);
21b8c4
+static void start_decline6(struct client_state *client);
21b8c4
+static void do_decline6(void *input);
21b8c4
 static void start_informed(struct client_state *client);
21b8c4
 void informed_handler(struct packet *packet, struct client_state *client);
21b8c4
 void bound_handler(struct packet *packet, struct client_state *client);
5a3797
@@ -2140,6 +2142,7 @@ start_release6(struct client_state *clie
21b8c4
 	cancel_timeout(do_select6, client);
21b8c4
 	cancel_timeout(do_refresh6, client);
21b8c4
 	cancel_timeout(do_release6, client);
21b8c4
+	cancel_timeout(do_decline6, client);
21b8c4
 	client->state = S_STOPPED;
21b8c4
 
21b8c4
 	/*
5a3797
@@ -2790,6 +2793,7 @@ dhc6_check_reply(struct client_state *cl
21b8c4
 		break;
21b8c4
 
21b8c4
 	      case S_STOPPED:
21b8c4
+	      case S_DECLINED:
21b8c4
 		action = dhc6_stop_action;
21b8c4
 		break;
21b8c4
 
5a3797
@@ -2891,6 +2895,7 @@ dhc6_check_reply(struct client_state *cl
21b8c4
 		break;
21b8c4
 
21b8c4
 	      case S_STOPPED:
21b8c4
+	      case S_DECLINED:
21b8c4
 		/* Nothing critical to do at this stage. */
21b8c4
 		break;
21b8c4
 
5a3797
@@ -3933,17 +3938,23 @@ reply_handler(struct packet *packet, str
21b8c4
 	cancel_timeout(do_select6, client);
21b8c4
 	cancel_timeout(do_refresh6, client);
21b8c4
 	cancel_timeout(do_release6, client);
21b8c4
+	cancel_timeout(do_decline6, client);
21b8c4
 
21b8c4
 	/* If this is in response to a Release/Decline, clean up and return. */
21b8c4
-	if (client->state == S_STOPPED) {
21b8c4
-		if (client->active_lease == NULL)
21b8c4
-			return;
21b8c4
+	if ((client->state == S_STOPPED) ||
33f732
+	    (client->state == S_DECLINED)) {
21b8c4
+
21b8c4
+		if (client->active_lease != NULL) {
21b8c4
+			dhc6_lease_destroy(&client->active_lease, MDL);
21b8c4
+			client->active_lease = NULL;
21b8c4
+			/* We should never wait for nothing!? */
21b8c4
+			if (stopping_finished())
21b8c4
+				exit(0);
21b8c4
+		}
21b8c4
+
21b8c4
+		if (client->state == S_DECLINED)
21b8c4
+			start_init6(client);
21b8c4
 
21b8c4
-		dhc6_lease_destroy(&client->active_lease, MDL);
21b8c4
-		client->active_lease = NULL;
21b8c4
-		/* We should never wait for nothing!? */
21b8c4
-		if (stopping_finished())
21b8c4
-			exit(0);
21b8c4
 		return;
21b8c4
 	}
21b8c4
 
5a3797
@@ -4470,7 +4481,11 @@ start_bound(struct client_state *client)
21b8c4
 						     oldia, oldaddr);
21b8c4
 			dhc6_marshall_values("new_", client, lease, ia, addr);
21b8c4
 
21b8c4
-			script_go(client);
21b8c4
+			// when script returns 3, DAD failed
21b8c4
+			if (script_go(client) == 3) {
21b8c4
+				start_decline6(client);
21b8c4
+				return;
21b8c4
+			}
21b8c4
 		}
21b8c4
 
21b8c4
 		/* XXX: maybe we should loop on the old values instead? */
5a3797
@@ -4516,6 +4531,151 @@ start_bound(struct client_state *client)
21b8c4
 	dhc6_check_times(client);
21b8c4
 }
21b8c4
 
21b8c4
+/*
21b8c4
+ * Decline addresses.
21b8c4
+ */
21b8c4
+void
21b8c4
+start_decline6(struct client_state *client)
21b8c4
+{
21b8c4
+	/* Cancel any pending transmissions */
21b8c4
+	cancel_timeout(do_confirm6, client);
21b8c4
+	cancel_timeout(do_select6, client);
21b8c4
+	cancel_timeout(do_refresh6, client);
21b8c4
+	cancel_timeout(do_release6, client);
21b8c4
+	cancel_timeout(do_decline6, client);
21b8c4
+	client->state = S_DECLINED;
21b8c4
+
21b8c4
+	if (client->active_lease == NULL)
21b8c4
+		return;
21b8c4
+
21b8c4
+	/* Set timers per RFC3315 section 18.1.7. */
21b8c4
+	client->IRT = DEC_TIMEOUT * 100;
21b8c4
+	client->MRT = 0;
21b8c4
+	client->MRC = DEC_MAX_RC;
21b8c4
+	client->MRD = 0;
21b8c4
+
21b8c4
+	dhc6_retrans_init(client);
21b8c4
+	client->v6_handler = reply_handler;
21b8c4
+
21b8c4
+	client->refresh_type = DHCPV6_DECLINE;
21b8c4
+	do_decline6(client);
21b8c4
+}
21b8c4
+
21b8c4
+/*
21b8c4
+ * do_decline6() creates a Decline packet and transmits it.
21b8c4
+ */
21b8c4
+static void
21b8c4
+do_decline6(void *input)
21b8c4
+{
21b8c4
+	struct client_state *client;
21b8c4
+	struct data_string ds;
21b8c4
+	int send_ret;
33f732
+	struct timeval elapsed, tv;
21b8c4
+
21b8c4
+	client = input;
21b8c4
+
21b8c4
+	if ((client->active_lease == NULL) || !active_prefix(client))
21b8c4
+		return;
21b8c4
+
21b8c4
+	if ((client->MRC != 0) && (client->txcount > client->MRC))  {
21b8c4
+		log_info("Max retransmission count exceeded.");
21b8c4
+		goto decline_done;
21b8c4
+	}
21b8c4
+
21b8c4
+	/*
21b8c4
+	 * Start_time starts at the first transmission.
21b8c4
+	 */
21b8c4
+	if (client->txcount == 0) {
21b8c4
+		client->start_time.tv_sec = cur_tv.tv_sec;
21b8c4
+		client->start_time.tv_usec = cur_tv.tv_usec;
21b8c4
+	}
21b8c4
+
33f732
+	/* elapsed = cur - start */
33f732
+	elapsed.tv_sec = cur_tv.tv_sec - client->start_time.tv_sec;
33f732
+	elapsed.tv_usec = cur_tv.tv_usec - client->start_time.tv_usec;
33f732
+	if (elapsed.tv_usec < 0) {
33f732
+		elapsed.tv_sec -= 1;
33f732
+		elapsed.tv_usec += 1000000;
33f732
+	}
33f732
+
21b8c4
+	memset(&ds, 0, sizeof(ds));
21b8c4
+	if (!buffer_allocate(&ds.buffer, 4, MDL)) {
21b8c4
+		log_error("Unable to allocate memory for Decline.");
21b8c4
+		goto decline_done;
21b8c4
+	}
21b8c4
+
21b8c4
+	ds.data = ds.buffer->data;
21b8c4
+	ds.len = 4;
21b8c4
+	ds.buffer->data[0] = DHCPV6_DECLINE;
21b8c4
+	memcpy(ds.buffer->data + 1, client->dhcpv6_transaction_id, 3);
21b8c4
+
33f732
+	/* Form an elapsed option. */
33f732
+	/* Maximum value is 65535 1/100s coded as 0xffff. */
33f732
+	if ((elapsed.tv_sec < 0) || (elapsed.tv_sec > 655) ||
33f732
+	    ((elapsed.tv_sec == 655) && (elapsed.tv_usec > 350000))) {
33f732
+		client->elapsed = 0xffff;
33f732
+	} else {
33f732
+		client->elapsed = elapsed.tv_sec * 100;
33f732
+		client->elapsed += elapsed.tv_usec / 10000;
33f732
+	}
33f732
+
33f732
+	client->elapsed = htons(client->elapsed);
33f732
+
21b8c4
+	log_debug("XMT: Forming Decline.");
21b8c4
+	make_client6_options(client, &client->sent_options,
21b8c4
+			     client->active_lease, DHCPV6_DECLINE);
21b8c4
+	dhcpv6_universe.encapsulate(&ds, NULL, NULL, client, NULL,
21b8c4
+				    client->sent_options, &global_scope,
21b8c4
+				    &dhcpv6_universe);
21b8c4
+
21b8c4
+	/* Append IA's (but don't release temporary addresses). */
21b8c4
+	if (wanted_ia_na &&
21b8c4
+	    dhc6_add_ia_na(client, &ds, client->active_lease,
21b8c4
+			   DHCPV6_DECLINE) != ISC_R_SUCCESS) {
21b8c4
+		data_string_forget(&ds, MDL);
21b8c4
+		goto decline_done;
21b8c4
+	}
21b8c4
+	if (wanted_ia_pd &&
21b8c4
+	    dhc6_add_ia_pd(client, &ds, client->active_lease,
21b8c4
+			   DHCPV6_DECLINE) != ISC_R_SUCCESS) {
21b8c4
+		data_string_forget(&ds, MDL);
21b8c4
+		goto decline_done;
21b8c4
+	}
21b8c4
+
21b8c4
+	/* Transmit and wait. */
21b8c4
+	log_info("XMT: Decline on %s, interval %ld0ms.",
21b8c4
+		 client->name ? client->name : client->interface->name,
21b8c4
+		 (long int)client->RT);
21b8c4
+
21b8c4
+	send_ret = send_packet6(client->interface, ds.data, ds.len,
21b8c4
+				&DHCPv6DestAddr);
21b8c4
+	if (send_ret != ds.len) {
21b8c4
+		log_error("dhc6: sendpacket6() sent %d of %d bytes",
21b8c4
+			  send_ret, ds.len);
21b8c4
+	}
21b8c4
+
21b8c4
+	data_string_forget(&ds, MDL);
21b8c4
+
21b8c4
+	/* Wait RT */
21b8c4
+	tv.tv_sec = cur_tv.tv_sec + client->RT / 100;
21b8c4
+	tv.tv_usec = cur_tv.tv_usec + (client->RT % 100) * 10000;
21b8c4
+	if (tv.tv_usec >= 1000000) {
21b8c4
+		tv.tv_sec += 1;
21b8c4
+		tv.tv_usec -= 1000000;
21b8c4
+	}
21b8c4
+	add_timeout(&tv, do_decline6, client, NULL, NULL);
21b8c4
+	dhc6_retrans_advance(client);
21b8c4
+	return;
21b8c4
+
21b8c4
+decline_done:
21b8c4
+	if (client->active_lease != NULL) {
21b8c4
+		dhc6_lease_destroy(&client->active_lease, MDL);
21b8c4
+		client->active_lease = NULL;
21b8c4
+	}
21b8c4
+	start_init6(client);
21b8c4
+	return;
21b8c4
+}
21b8c4
+
21b8c4
 /* While bound, ignore packets.  In the future we'll want to answer
21b8c4
  * Reconfigure-Request messages and the like.
21b8c4
  */