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