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