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