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

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