philipp / rpms / dhcp

Forked from rpms/dhcp 4 years ago
Clone

Blame SOURCES/dhcp-4.2.5-sendDecline.patch

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