Blame SOURCES/0016-Turn-on-creating-sending-of-DUID.patch

df4638
From 2f6b827e89305adcff45288c632785ac054adb8e Mon Sep 17 00:00:00 2001
df4638
From: Pavel Zhukov <pzhukov@redhat.com>
df4638
Date: Thu, 21 Feb 2019 10:36:30 +0100
df4638
Subject: [PATCH 16/26] Turn on creating/sending of DUID
df4638
Cc: pzhukov@redhat.com
df4638
df4638
as client identifier with DHCPv4 clients (#560361c#40, rfc4361)
df4638
---
df4638
 client/dhclient.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++---
df4638
 1 file changed, 70 insertions(+), 4 deletions(-)
df4638
df4638
diff --git a/client/dhclient.c b/client/dhclient.c
df4638
index 8e57da9..ccc98e4 100644
df4638
--- a/client/dhclient.c
df4638
+++ b/client/dhclient.c
df4638
@@ -4021,6 +4021,59 @@ write_options(struct client_state *client, struct option_state *options,
df4638
 	}
df4638
 }
df4638
 
df4638
+int unhexchar(char c) {
df4638
+
df4638
+	if (c >= '0' && c <= '9')
df4638
+		return c - '0';
df4638
+
df4638
+	if (c >= 'a' && c <= 'f')
df4638
+		return c - 'a' + 10;
df4638
+
df4638
+	if (c >= 'A' && c <= 'F')
df4638
+		return c - 'A' + 10;
df4638
+
df4638
+	return -1;
df4638
+}
df4638
+
df4638
+isc_result_t
df4638
+read_uuid(u_int8_t* uuid) {
df4638
+	const char *id_fname = "/etc/machine-id";
df4638
+	char id[32];
df4638
+	size_t nread;
df4638
+	FILE * file = fopen( id_fname , "r");
df4638
+	if (!file) {
df4638
+		log_debug("Cannot open %s", id_fname);
df4638
+		return ISC_R_IOERROR;
df4638
+	}
df4638
+	nread = fread(id, 1, sizeof id, file);
df4638
+	fclose(file);
df4638
+
df4638
+	if (nread < 32) {
df4638
+		log_debug("Not enough data in %s", id_fname);
df4638
+		return ISC_R_IOERROR;
df4638
+	}
df4638
+	int j;
df4638
+	for (j = 0; j < 16; j++) {
df4638
+		int a, b;
df4638
+
df4638
+		a = unhexchar(id[j*2]);
df4638
+		b = unhexchar(id[j*2+1]);
df4638
+
df4638
+		if (a < 0 || b < 0) {
df4638
+			log_debug("Wrong data in %s", id_fname);
df4638
+                        return ISC_R_IOERROR;
df4638
+		}
df4638
+		uuid[j] = a << 4 | b;
df4638
+	}
df4638
+
df4638
+	/* Set UUID version to 4 --- truly random generation */
df4638
+	uuid[6] = (uuid[6] & 0x0F) | 0x40;
df4638
+	/* Set the UUID variant to DCE */
df4638
+	uuid[8] = (uuid[8] & 0x3F) | 0x80;
df4638
+
df4638
+	return ISC_R_SUCCESS;
df4638
+}
df4638
+
df4638
 /*
df4638
  * The "best" default DUID, since we cannot predict any information
df4638
  * about the system (such as whether or not the hardware addresses are
df4638
@@ -4041,6 +4094,7 @@ form_duid(struct data_string *duid, const char *file, int line)
df4638
 	struct interface_info *ip;
df4638
 	int len;
df4638
 	char *str;
df4638
+	u_int8_t uuid[16];
df4638
 
df4638
 	/* For now, just use the first interface on the list. */
df4638
 	ip = interfaces;
df4638
@@ -4061,9 +4115,16 @@ form_duid(struct data_string *duid, const char *file, int line)
df4638
 	    (ip->hw_address.hlen > sizeof(ip->hw_address.hbuf)))
df4638
 		log_fatal("Impossible hardware address length at %s:%d.", MDL);
df4638
 
df4638
-	if (duid_type == 0)
df4638
-		duid_type = stateless ? DUID_LL : DUID_LLT;
df4638
-
df4638
+	if (duid_type == 0) {
df4638
+		if (read_uuid(uuid) == ISC_R_SUCCESS)
df4638
+		    duid_type = DUID_UUID;
df4638
+		else
df4638
+		    duid_type = stateless ? DUID_LL : DUID_LLT;
df4638
+	}
df4638
+	
df4638
+	if (duid_type == DUID_UUID)
df4638
+		len = 2 + sizeof (uuid);
df4638
+	else {
df4638
 	/*
df4638
 	 * 2 bytes for the 'duid type' field.
df4638
 	 * 2 bytes for the 'htype' field.
df4638
@@ -4074,13 +4135,18 @@ form_duid(struct data_string *duid, const char *file, int line)
df4638
 	len = 4 + (ip->hw_address.hlen - 1);
df4638
 	if (duid_type == DUID_LLT)
df4638
 		len += 4;
df4638
+	}
df4638
 	if (!buffer_allocate(&duid->buffer, len, MDL))
df4638
 		log_fatal("no memory for default DUID!");
df4638
 	duid->data = duid->buffer->data;
df4638
 	duid->len = len;
df4638
 
df4638
+	if (duid_type == DUID_UUID) {
df4638
+		putUShort(duid->buffer->data, DUID_UUID);
df4638
+		memcpy(duid->buffer->data + 2, uuid, sizeof(uuid));
df4638
+	}
df4638
 	/* Basic Link Local Address type of DUID. */
df4638
-	if (duid_type == DUID_LLT) {
df4638
+	else if (duid_type == DUID_LLT) {
df4638
 		putUShort(duid->buffer->data, DUID_LLT);
df4638
 		putUShort(duid->buffer->data + 2, ip->hw_address.hbuf[0]);
df4638
 		putULong(duid->buffer->data + 4, cur_time - DUID_TIME_EPOCH);
df4638
-- 
df4638
2.14.5
df4638