Reference:
https://src.fedoraproject.org/rpms/dhcp/blob/rawhide/f/0015-Add-GUID-DUID-to-dhcpd-logs-1064416.patch
From 3baf35269555e2223dbd1733cb1c475cb7f2ed7a Mon Sep 17 00:00:00 2001
From: Pavel Zhukov <pzhukov@redhat.com>
Date: Thu, 21 Feb 2019 10:35:47 +0100
Subject: [PATCH 15/28] Add GUID/DUID to dhcpd logs (#1064416)

---
 client/dhclient.c | 70 ++++++++++++++++++++++++++++++++++--------
 server/dhcp.c     | 78 ++++++++++++++++++++++++++++-------------------
 2 files changed, 105 insertions(+), 43 deletions(-)

diff --git a/client/dhclient.c b/client/dhclient.c
index 48edddf..181f6e1 100644
--- a/client/dhclient.c
+++ b/client/dhclient.c
@@ -1176,6 +1176,26 @@ main(int argc, char **argv) {
 		}
 	}
 
+	/* We create a backup seed before rediscovering interfaces in order to
+	   have a seed built using all of the available interfaces
+	   It's interesting if required interfaces doesn't let us defined
+	   a really unique seed due to a lack of valid HW addr later
+	   (this is the case with DHCP over IB)
+	   We only use the last device as using a sum could broke the
+	   uniqueness of the seed among multiple nodes
+	 */
+	unsigned backup_seed = 0;
+	for (ip = interfaces; ip; ip = ip -> next) {
+		int junk;
+		if ( ip -> hw_address.hlen <= sizeof seed )
+		  continue;
+		memcpy (&junk,
+			&ip -> hw_address.hbuf [ip -> hw_address.hlen -
+						sizeof seed], sizeof seed);
+		backup_seed = junk;
+	}
+
+
 	/* At this point, all the interfaces that the script thinks
 	   are relevant should be running, so now we once again call
 	   discover_interfaces(), and this time ask it to actually set
@@ -1204,14 +1224,34 @@ main(int argc, char **argv) {
 		   Not much entropy, but we're booting, so we're not likely to
 		   find anything better. */
 
+		int seed_flag = 0;
 		for (ip = interfaces; ip; ip = ip->next) {
 			int junk;
+			if ( ip -> hw_address.hlen <= sizeof seed )
+			  continue;
 			memcpy(&junk,
 			       &ip->hw_address.hbuf[ip->hw_address.hlen -
 						    sizeof seed], sizeof seed);
 			seed += junk;
+			seed_flag = 1;
 		}
-		seed += cur_time + (unsigned)getpid();
+		if ( seed_flag == 0 ) {
+			if ( backup_seed != 0 ) {
+			  seed = backup_seed;
+			  log_info ("xid: rand init seed (0x%x) built using all"
+				    " available interfaces",seed);
+			}
+			else {
+			  seed = cur_time^((unsigned) gethostid()) ;
+			  log_info ("xid: warning: no netdev with useable HWADDR found"
+				    " for seed's uniqueness enforcement");
+			  log_info ("xid: rand init seed (0x%x) built using gethostid",
+				    seed);
+			}
+			/* we only use seed and no current time as a broadcast reply */
+			/* will certainly be used by the hwaddrless interface */
+		}
+		seed += ((unsigned)(cur_tv.tv_usec * 1000000)) + (unsigned)getpid();
 	}
 	srandom(seed);
 
@@ -1869,9 +1909,10 @@ void dhcpack (packet)
 		return;
 	}
 
-	log_info ("DHCPACK of %s from %s",
+	log_info ("DHCPACK of %s from %s (xid=0x%x)",
 		  inet_ntoa(packet->raw->yiaddr),
-		  piaddr (packet->client_addr));
+		  piaddr (packet -> client_addr),
+		  ntohl(client -> xid));
 
 	/* Check v6only first. */
 	v6only_wait = check_v6only(packet, client);
@@ -2825,7 +2866,7 @@ void dhcpnak (packet)
 		return;
 	}
 
-	log_info ("DHCPNAK from %s", piaddr (packet -> client_addr));
+	log_info ("DHCPNAK from %s (xid=0x%x)", piaddr (packet -> client_addr), ntohl(client -> xid));
 
 	if (!client -> active) {
 #if defined (DEBUG)
@@ -2958,10 +2999,10 @@ void send_discover (cpp)
 			  (long)(client -> interval));
 	} else
 #endif
-	log_info ("DHCPDISCOVER on %s to %s port %d interval %ld",
+	log_info ("DHCPDISCOVER on %s to %s port %d interval %ld (xid=0x%x)",
 	      client -> name ? client -> name : client -> interface -> name,
 	      inet_ntoa (sockaddr_broadcast.sin_addr),
-	      ntohs (sockaddr_broadcast.sin_port), (long)(client -> interval));
+	      ntohs (sockaddr_broadcast.sin_port), (long)(client -> interval), ntohl(client -> xid));
 
 	/* Send out a packet. */
 #if defined(DHCPv6) && defined(DHCP4o6)
@@ -3355,10 +3396,12 @@ void send_request (cpp)
 	}
 
 	strncpy(rip_buf, rip_str, sizeof(rip_buf)-1);
-	log_info ("DHCPREQUEST for %s on %s to %s port %d", rip_buf,
+	log_info ("DHCPREQUEST for %s on %s to %s port %d (xid=0x%x)",
+                  rip_buf,
 		  client->name ? client->name : client->interface->name,
 		  inet_ntoa(destination.sin_addr),
-		  ntohs (destination.sin_port));
+		  ntohs (destination.sin_port),
+                  ntohl(client -> xid));
 
 #if defined(DHCPv6) && defined(DHCP4o6)
 	if (dhcpv4_over_dhcpv6) {
@@ -3415,11 +3458,13 @@ void send_decline (cpp)
 		log_info ("DHCPDECLINE");
 	} else
 #endif
-	log_info ("DHCPDECLINE of %s on %s to %s port %d",
+	log_info ("DHCPDECLINE of %s on %s to %s port %d (xid=0x%x)",                  
 		  piaddr(client->requested_address),
 		  (client->name ? client->name : client->interface->name),
 		  inet_ntoa(sockaddr_broadcast.sin_addr),
-		  ntohs(sockaddr_broadcast.sin_port));
+		  ntohs(sockaddr_broadcast.sin_port),
+                  ntohl(client -> xid));
+
 
 	/* Send out a packet. */
 #if defined(DHCPv6) && defined(DHCP4o6)
@@ -3478,11 +3523,12 @@ void send_release (cpp)
 		log_info ("DHCPRELEASE");
 	} else
 #endif
-	log_info ("DHCPRELEASE of %s on %s to %s port %d",
+	log_info ("DHCPRELEASE of %s on %s to %s port %d (xid=0x%x)",
 		  piaddr(client->active->address),
 		  client->name ? client->name : client->interface->name,
 		  inet_ntoa (destination.sin_addr),
-		  ntohs (destination.sin_port));
+		  ntohs (destination.sin_port),
+                  ntohl(client -> xid));
 
 #if defined(DHCPv6) && defined(DHCP4o6)
 	if (dhcpv4_over_dhcpv6) {
diff --git a/server/dhcp.c b/server/dhcp.c
index ae805a6..8363840 100644
--- a/server/dhcp.c
+++ b/server/dhcp.c
@@ -93,6 +93,42 @@ const int dhcp_type_name_max = ((sizeof dhcp_type_names) / sizeof (char *));
 
 static TIME leaseTimeCheck(TIME calculated, TIME alternate);
 
+char *print_client_identifier_from_packet (packet)
+	struct packet *packet;
+{
+	struct option_cache *oc;
+	struct data_string client_identifier;
+	char *ci;
+
+	memset (&client_identifier, 0, sizeof client_identifier);
+
+	oc = lookup_option (&dhcp_universe, packet -> options,
+			DHO_DHCP_CLIENT_IDENTIFIER);
+	if (oc &&
+	    evaluate_option_cache (&client_identifier,
+				    packet, (struct lease *)0,
+				    (struct client_state *)0,
+				    packet -> options,
+				    (struct option_state *)0,
+				    &global_scope, oc, MDL)) {
+		ci = print_hw_addr (HTYPE_INFINIBAND, client_identifier.len, client_identifier.data);
+		data_string_forget (&client_identifier, MDL);
+		return ci;
+	} else
+		return "\"no client id\"";
+}
+
+char *print_hw_addr_or_client_id (packet)
+	struct packet *packet;
+{
+	if (packet -> raw -> htype == HTYPE_INFINIBAND)
+		return print_client_identifier_from_packet (packet);
+	else
+		return print_hw_addr (packet -> raw -> htype,
+				      packet -> raw -> hlen,
+				      packet -> raw -> chaddr);
+}
+
 void
 dhcp (struct packet *packet) {
 	int ms_nulltp = 0;
@@ -135,9 +171,7 @@ dhcp (struct packet *packet) {
 
 		log_info("%s from %s via %s: %s", s,
 			 (packet->raw->htype
-			  ? print_hw_addr(packet->raw->htype,
-					  packet->raw->hlen,
-					  packet->raw->chaddr)
+			  ? print_hw_addr_or_client_id(packet)
 			  : "<no identifier>"),
 			 packet->raw->giaddr.s_addr
 			 ? inet_ntoa(packet->raw->giaddr)
@@ -334,9 +368,7 @@ void dhcpdiscover (packet, ms_nulltp)
 #endif
 	snprintf (msgbuf, sizeof msgbuf, "DHCPDISCOVER from %s %s%s%svia %s",
 		 (packet -> raw -> htype
-		  ? print_hw_addr (packet -> raw -> htype,
-				   packet -> raw -> hlen,
-				   packet -> raw -> chaddr)
+		  ? print_hw_addr_or_client_id (packet)
 		  : (lease
 		     ? print_hex_1(lease->uid_len, lease->uid, 60)
 		     : "<no identifier>")),
@@ -548,9 +580,7 @@ void dhcprequest (packet, ms_nulltp, ip_lease)
 		 "DHCPREQUEST for %s%s from %s %s%s%svia %s",
 		 piaddr (cip), smbuf,
 		 (packet -> raw -> htype
-		  ? print_hw_addr (packet -> raw -> htype,
-				   packet -> raw -> hlen,
-				   packet -> raw -> chaddr)
+		  ? print_hw_addr_or_client_id(packet)
 		  : (lease
 		     ? print_hex_1(lease->uid_len, lease->uid, 60)
 		     : "<no identifier>")),
@@ -791,9 +821,7 @@ void dhcprelease (packet, ms_nulltp)
 	if ((oc = lookup_option (&dhcp_universe, packet -> options,
 				 DHO_DHCP_REQUESTED_ADDRESS))) {
 		log_info ("DHCPRELEASE from %s specified requested-address.",
-		      print_hw_addr (packet -> raw -> htype,
-				     packet -> raw -> hlen,
-				     packet -> raw -> chaddr));
+		      print_hw_addr_or_client_id(packet));
 	}
 
 	oc = lookup_option (&dhcp_universe, packet -> options,
@@ -885,9 +913,7 @@ void dhcprelease (packet, ms_nulltp)
 		 "DHCPRELEASE of %s from %s %s%s%svia %s (%sfound)",
 		 cstr,
 		 (packet -> raw -> htype
-		  ? print_hw_addr (packet -> raw -> htype,
-				   packet -> raw -> hlen,
-				   packet -> raw -> chaddr)
+		  ? print_hw_addr_or_client_id(packet)
 		  : (lease
 		     ? print_hex_1(lease->uid_len, lease->uid, 60)
 		     : "<no identifier>")),
@@ -992,9 +1018,7 @@ void dhcpdecline (packet, ms_nulltp)
 		 "DHCPDECLINE of %s from %s %s%s%svia %s",
 		 piaddr (cip),
 		 (packet -> raw -> htype
-		  ? print_hw_addr (packet -> raw -> htype,
-				   packet -> raw -> hlen,
-				   packet -> raw -> chaddr)
+		  ? print_hw_addr_or_client_id(packet)
 		  : (lease
 		     ? print_hex_1(lease->uid_len, lease->uid, 60)
 		     : "<no identifier>")),
@@ -1740,8 +1764,7 @@ void dhcpinform (packet, ms_nulltp)
 	/* Report what we're sending. */
 	snprintf(msgbuf, sizeof msgbuf, "DHCPACK to %s (%s) via", piaddr(cip),
 		 (packet->raw->htype && packet->raw->hlen) ?
-			print_hw_addr(packet->raw->htype, packet->raw->hlen,
-				      packet->raw->chaddr) :
+			print_hw_addr_or_client_id(packet) :
 			"<no client hardware address>");
 	log_info("%s %s", msgbuf, gip.len ? piaddr(gip) :
 					    packet->interface->name);
@@ -1926,9 +1949,7 @@ void nak_lease (packet, cip, network_group)
 #endif
 	log_info ("DHCPNAK on %s to %s via %s",
 	      piaddr (*cip),
-	      print_hw_addr (packet -> raw -> htype,
-			     packet -> raw -> hlen,
-			     packet -> raw -> chaddr),
+	      print_hw_addr_or_client_id(packet),
 	      packet -> raw -> giaddr.s_addr
 	      ? inet_ntoa (packet -> raw -> giaddr)
 	      : packet -> interface -> name);
@@ -4044,7 +4065,7 @@ void dhcp_reply (lease)
 		   ? (state -> offer == DHCPACK ? "DHCPACK" : "DHCPOFFER")
 		   : "BOOTREPLY"),
 		  piaddr (lease -> ip_addr),
-		  (lease -> hardware_addr.hlen
+		  (lease -> hardware_addr.hlen > 1
 		   ? print_hw_addr (lease -> hardware_addr.hbuf [0],
 				    lease -> hardware_addr.hlen - 1,
 				    &lease -> hardware_addr.hbuf [1])
@@ -4605,10 +4626,7 @@ int find_lease (struct lease **lp,
 			if (uid_lease) {
 			    if (uid_lease->binding_state == FTS_ACTIVE) {
 				log_error ("client %s has duplicate%s on %s",
-					   (print_hw_addr
-					    (packet -> raw -> htype,
-					     packet -> raw -> hlen,
-					     packet -> raw -> chaddr)),
+					   (print_hw_addr_or_client_id(packet)),
 					   " leases",
 					   (ip_lease -> subnet ->
 					    shared_network -> name));
@@ -4775,9 +4793,7 @@ int find_lease (struct lease **lp,
 			log_error("uid lease %s for client %s is duplicate "
 				  "on %s",
 				  piaddr(uid_lease->ip_addr),
-				  print_hw_addr(packet->raw->htype,
-						packet->raw->hlen,
-						packet->raw->chaddr),
+				  print_hw_addr_or_client_id(packet),
 				  uid_lease->subnet->shared_network->name);
 
 			if (!packet -> raw -> ciaddr.s_addr &&
-- 
2.35.1

