Blame SOURCES/dhcp-4.2.5-sendDecline.patch

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