Blame SOURCES/0015-Add-GUID-DUID-to-dhcpd-logs-1064416.patch

f9ed25
From 3d3e442ed1316930a5360e4d5a56b46a42a29419 Mon Sep 17 00:00:00 2001
f9ed25
From: Pavel Zhukov <pzhukov@redhat.com>
f9ed25
Date: Thu, 21 Feb 2019 10:35:47 +0100
f9ed25
Subject: [PATCH 15/26] Add GUID/DUID to dhcpd logs (#1064416)
f9ed25
Cc: pzhukov@redhat.com
f9ed25
f9ed25
---
f9ed25
 client/dhclient.c | 75 ++++++++++++++++++++++++++++++++++++++++++----------
f9ed25
 server/dhcp.c     | 78 +++++++++++++++++++++++++++++++++----------------------
f9ed25
 2 files changed, 108 insertions(+), 45 deletions(-)
f9ed25
f9ed25
diff --git a/client/dhclient.c b/client/dhclient.c
f9ed25
index dc9080e..8e57da9 100644
f9ed25
--- a/client/dhclient.c
f9ed25
+++ b/client/dhclient.c
f9ed25
@@ -1170,6 +1170,26 @@ main(int argc, char **argv) {
f9ed25
 		}
f9ed25
 	}
f9ed25
 
f9ed25
+	/* We create a backup seed before rediscovering interfaces in order to
f9ed25
+	   have a seed built using all of the available interfaces
f9ed25
+	   It's interesting if required interfaces doesn't let us defined
f9ed25
+	   a really unique seed due to a lack of valid HW addr later
f9ed25
+	   (this is the case with DHCP over IB)
f9ed25
+	   We only use the last device as using a sum could broke the
f9ed25
+	   uniqueness of the seed among multiple nodes
f9ed25
+	 */
f9ed25
+	unsigned backup_seed = 0;
f9ed25
+	for (ip = interfaces; ip; ip = ip -> next) {
f9ed25
+		int junk;
f9ed25
+		if ( ip -> hw_address.hlen <= sizeof seed )
f9ed25
+		  continue;
f9ed25
+		memcpy (&junk,
f9ed25
+			&ip -> hw_address.hbuf [ip -> hw_address.hlen -
f9ed25
+						sizeof seed], sizeof seed);
f9ed25
+		backup_seed = junk;
f9ed25
+	}
f9ed25
+
f9ed25
+
f9ed25
 	/* At this point, all the interfaces that the script thinks
f9ed25
 	   are relevant should be running, so now we once again call
f9ed25
 	   discover_interfaces(), and this time ask it to actually set
f9ed25
@@ -1184,14 +1204,36 @@ main(int argc, char **argv) {
f9ed25
 	   Not much entropy, but we're booting, so we're not likely to
f9ed25
 	   find anything better. */
f9ed25
 	seed = 0;
f9ed25
+	int seed_flag = 0;
f9ed25
 	for (ip = interfaces; ip; ip = ip->next) {
f9ed25
 		int junk;
f9ed25
+		if ( ip -> hw_address.hlen <= sizeof seed )
f9ed25
+		  continue;
f9ed25
 		memcpy(&junk,
f9ed25
 		       &ip->hw_address.hbuf[ip->hw_address.hlen -
f9ed25
 					    sizeof seed], sizeof seed);
f9ed25
 		seed += junk;
f9ed25
+		seed_flag = 1;
f9ed25
 	}
f9ed25
-	srandom(seed + cur_time + (unsigned)getpid());
f9ed25
+	if ( seed_flag == 0 ) {
f9ed25
+		if ( backup_seed != 0 ) {
f9ed25
+		  seed = backup_seed;
f9ed25
+		  log_info ("xid: rand init seed (0x%x) built using all"
f9ed25
+			    " available interfaces",seed);
f9ed25
+		}
f9ed25
+		else {
f9ed25
+		  seed = cur_time^((unsigned) gethostid()) ;
f9ed25
+		  log_info ("xid: warning: no netdev with useable HWADDR found"
f9ed25
+			    " for seed's uniqueness enforcement");
f9ed25
+		  log_info ("xid: rand init seed (0x%x) built using gethostid",
f9ed25
+			    seed);
f9ed25
+		}
f9ed25
+		/* we only use seed and no current time as a broadcast reply */
f9ed25
+		/* will certainly be used by the hwaddrless interface */
f9ed25
+		srandom(seed + ((unsigned)(cur_tv.tv_usec * 1000000)) + (unsigned)getpid());
f9ed25
+	}
f9ed25
+	else
f9ed25
+	        srandom(seed + ((unsigned)(cur_tv.tv_usec * 1000000)) + (unsigned)getpid());
f9ed25
 
f9ed25
 	/* Setup specific Infiniband options */
f9ed25
 	for (ip = interfaces; ip; ip = ip->next) {
f9ed25
@@ -1746,10 +1788,10 @@ void dhcpack (packet)
f9ed25
 #endif
f9ed25
 		return;
f9ed25
 	}
f9ed25
-
f9ed25
-	log_info ("DHCPACK of %s from %s",
f9ed25
-		  inet_ntoa(packet->raw->yiaddr),
f9ed25
-		  piaddr (packet->client_addr));
f9ed25
+	log_info ("DHCPACK of %s from %s (xid=0x%x)",
f9ed25
+                  inet_ntoa(packet->raw->yiaddr),
f9ed25
+                  piaddr (packet -> client_addr),
f9ed25
+                  ntohl(client -> xid));
f9ed25
 
f9ed25
 	lease = packet_to_lease (packet, client);
f9ed25
 	if (!lease) {
f9ed25
@@ -2669,7 +2711,7 @@ void dhcpnak (packet)
f9ed25
 		return;
f9ed25
 	}
f9ed25
 
f9ed25
-	log_info ("DHCPNAK from %s", piaddr (packet -> client_addr));
f9ed25
+	log_info ("DHCPNAK from %s (xid=0x%x)", piaddr (packet -> client_addr), ntohl(client -> xid));
f9ed25
 
f9ed25
 	if (!client -> active) {
f9ed25
 #if defined (DEBUG)
f9ed25
@@ -2802,10 +2844,10 @@ void send_discover (cpp)
f9ed25
 			  (long)(client -> interval));
f9ed25
 	} else
f9ed25
 #endif
f9ed25
-	log_info ("DHCPDISCOVER on %s to %s port %d interval %ld",
f9ed25
+	log_info ("DHCPDISCOVER on %s to %s port %d interval %ld (xid=0x%x)",
f9ed25
 	      client -> name ? client -> name : client -> interface -> name,
f9ed25
 	      inet_ntoa (sockaddr_broadcast.sin_addr),
f9ed25
-	      ntohs (sockaddr_broadcast.sin_port), (long)(client -> interval));
f9ed25
+	      ntohs (sockaddr_broadcast.sin_port), (long)(client -> interval), ntohl(client -> xid));
f9ed25
 
f9ed25
 	/* Send out a packet. */
f9ed25
 #if defined(DHCPv6) && defined(DHCP4o6)
f9ed25
@@ -3108,10 +3150,12 @@ void send_request (cpp)
f9ed25
 	}
f9ed25
 
f9ed25
 	strncpy(rip_buf, rip_str, sizeof(rip_buf)-1);
f9ed25
-	log_info ("DHCPREQUEST for %s on %s to %s port %d", rip_buf,
f9ed25
+	log_info ("DHCPREQUEST for %s on %s to %s port %d (xid=0x%x)",
f9ed25
+                  rip_buf,
f9ed25
 		  client->name ? client->name : client->interface->name,
f9ed25
 		  inet_ntoa(destination.sin_addr),
f9ed25
-		  ntohs (destination.sin_port));
f9ed25
+		  ntohs (destination.sin_port),
f9ed25
+                  ntohl(client -> xid));
f9ed25
 
f9ed25
 #if defined(DHCPv6) && defined(DHCP4o6)
f9ed25
 	if (dhcpv4_over_dhcpv6) {
f9ed25
@@ -3168,11 +3212,13 @@ void send_decline (cpp)
f9ed25
 		log_info ("DHCPDECLINE");
f9ed25
 	} else
f9ed25
 #endif
f9ed25
-	log_info ("DHCPDECLINE of %s on %s to %s port %d",
f9ed25
+	log_info ("DHCPDECLINE of %s on %s to %s port %d (xid=0x%x)",                  
f9ed25
 		  piaddr(client->requested_address),
f9ed25
 		  (client->name ? client->name : client->interface->name),
f9ed25
 		  inet_ntoa(sockaddr_broadcast.sin_addr),
f9ed25
-		  ntohs(sockaddr_broadcast.sin_port));
f9ed25
+		  ntohs(sockaddr_broadcast.sin_port),
f9ed25
+                  ntohl(client -> xid));
f9ed25
+
f9ed25
 
f9ed25
 	/* Send out a packet. */
f9ed25
 #if defined(DHCPv6) && defined(DHCP4o6)
f9ed25
@@ -3231,11 +3277,12 @@ void send_release (cpp)
f9ed25
 		log_info ("DHCPRELEASE");
f9ed25
 	} else
f9ed25
 #endif
f9ed25
-	log_info ("DHCPRELEASE of %s on %s to %s port %d",
f9ed25
+	log_info ("DHCPRELEASE of %s on %s to %s port %d (xid=0x%x)",
f9ed25
 		  piaddr(client->active->address),
f9ed25
 		  client->name ? client->name : client->interface->name,
f9ed25
 		  inet_ntoa (destination.sin_addr),
f9ed25
-		  ntohs (destination.sin_port));
f9ed25
+		  ntohs (destination.sin_port),
f9ed25
+                  ntohl(client -> xid));
f9ed25
 
f9ed25
 #if defined(DHCPv6) && defined(DHCP4o6)
f9ed25
 	if (dhcpv4_over_dhcpv6) {
f9ed25
diff --git a/server/dhcp.c b/server/dhcp.c
f9ed25
index 20f2a62..0582c4c 100644
f9ed25
--- a/server/dhcp.c
f9ed25
+++ b/server/dhcp.c
f9ed25
@@ -87,6 +87,42 @@ const int dhcp_type_name_max = ((sizeof dhcp_type_names) / sizeof (char *));
f9ed25
 
f9ed25
 static TIME leaseTimeCheck(TIME calculated, TIME alternate);
f9ed25
 
f9ed25
+char *print_client_identifier_from_packet (packet)
f9ed25
+	struct packet *packet;
f9ed25
+{
f9ed25
+	struct option_cache *oc;
f9ed25
+	struct data_string client_identifier;
f9ed25
+	char *ci;
f9ed25
+
f9ed25
+	memset (&client_identifier, 0, sizeof client_identifier);
f9ed25
+
f9ed25
+	oc = lookup_option (&dhcp_universe, packet -> options,
f9ed25
+			DHO_DHCP_CLIENT_IDENTIFIER);
f9ed25
+	if (oc &&
f9ed25
+	    evaluate_option_cache (&client_identifier,
f9ed25
+				    packet, (struct lease *)0,
f9ed25
+				    (struct client_state *)0,
f9ed25
+				    packet -> options,
f9ed25
+				    (struct option_state *)0,
f9ed25
+				    &global_scope, oc, MDL)) {
f9ed25
+		ci = print_hw_addr (HTYPE_INFINIBAND, client_identifier.len, client_identifier.data);
f9ed25
+		data_string_forget (&client_identifier, MDL);
f9ed25
+		return ci;
f9ed25
+	} else
f9ed25
+		return "\"no client id\"";
f9ed25
+}
f9ed25
+
f9ed25
+char *print_hw_addr_or_client_id (packet)
f9ed25
+	struct packet *packet;
f9ed25
+{
f9ed25
+	if (packet -> raw -> htype == HTYPE_INFINIBAND)
f9ed25
+		return print_client_identifier_from_packet (packet);
f9ed25
+	else
f9ed25
+		return print_hw_addr (packet -> raw -> htype,
f9ed25
+				      packet -> raw -> hlen,
f9ed25
+				      packet -> raw -> chaddr);
f9ed25
+}
f9ed25
+
f9ed25
 void
f9ed25
 dhcp (struct packet *packet) {
f9ed25
 	int ms_nulltp = 0;
f9ed25
@@ -129,9 +165,7 @@ dhcp (struct packet *packet) {
f9ed25
 
f9ed25
 		log_info("%s from %s via %s: %s", s,
f9ed25
 			 (packet->raw->htype
f9ed25
-			  ? print_hw_addr(packet->raw->htype,
f9ed25
-					  packet->raw->hlen,
f9ed25
-					  packet->raw->chaddr)
f9ed25
+			  ? print_hw_addr_or_client_id(packet)
f9ed25
 			  : "<no identifier>"),
f9ed25
 			 packet->raw->giaddr.s_addr
f9ed25
 			 ? inet_ntoa(packet->raw->giaddr)
f9ed25
@@ -328,9 +362,7 @@ void dhcpdiscover (packet, ms_nulltp)
f9ed25
 #endif
f9ed25
 	snprintf (msgbuf, sizeof msgbuf, "DHCPDISCOVER from %s %s%s%svia %s",
f9ed25
 		 (packet -> raw -> htype
f9ed25
-		  ? print_hw_addr (packet -> raw -> htype,
f9ed25
-				   packet -> raw -> hlen,
f9ed25
-				   packet -> raw -> chaddr)
f9ed25
+		  ? print_hw_addr_or_client_id (packet)
f9ed25
 		  : (lease
f9ed25
 		     ? print_hex_1(lease->uid_len, lease->uid, 60)
f9ed25
 		     : "<no identifier>")),
f9ed25
@@ -542,9 +574,7 @@ void dhcprequest (packet, ms_nulltp, ip_lease)
f9ed25
 		 "DHCPREQUEST for %s%s from %s %s%s%svia %s",
f9ed25
 		 piaddr (cip), smbuf,
f9ed25
 		 (packet -> raw -> htype
f9ed25
-		  ? print_hw_addr (packet -> raw -> htype,
f9ed25
-				   packet -> raw -> hlen,
f9ed25
-				   packet -> raw -> chaddr)
f9ed25
+		  ? print_hw_addr_or_client_id(packet)
f9ed25
 		  : (lease
f9ed25
 		     ? print_hex_1(lease->uid_len, lease->uid, 60)
f9ed25
 		     : "<no identifier>")),
f9ed25
@@ -785,9 +815,7 @@ void dhcprelease (packet, ms_nulltp)
f9ed25
 	if ((oc = lookup_option (&dhcp_universe, packet -> options,
f9ed25
 				 DHO_DHCP_REQUESTED_ADDRESS))) {
f9ed25
 		log_info ("DHCPRELEASE from %s specified requested-address.",
f9ed25
-		      print_hw_addr (packet -> raw -> htype,
f9ed25
-				     packet -> raw -> hlen,
f9ed25
-				     packet -> raw -> chaddr));
f9ed25
+		      print_hw_addr_or_client_id(packet));
f9ed25
 	}
f9ed25
 
f9ed25
 	oc = lookup_option (&dhcp_universe, packet -> options,
f9ed25
@@ -879,9 +907,7 @@ void dhcprelease (packet, ms_nulltp)
f9ed25
 		 "DHCPRELEASE of %s from %s %s%s%svia %s (%sfound)",
f9ed25
 		 cstr,
f9ed25
 		 (packet -> raw -> htype
f9ed25
-		  ? print_hw_addr (packet -> raw -> htype,
f9ed25
-				   packet -> raw -> hlen,
f9ed25
-				   packet -> raw -> chaddr)
f9ed25
+		  ? print_hw_addr_or_client_id(packet)
f9ed25
 		  : (lease
f9ed25
 		     ? print_hex_1(lease->uid_len, lease->uid, 60)
f9ed25
 		     : "<no identifier>")),
f9ed25
@@ -986,9 +1012,7 @@ void dhcpdecline (packet, ms_nulltp)
f9ed25
 		 "DHCPDECLINE of %s from %s %s%s%svia %s",
f9ed25
 		 piaddr (cip),
f9ed25
 		 (packet -> raw -> htype
f9ed25
-		  ? print_hw_addr (packet -> raw -> htype,
f9ed25
-				   packet -> raw -> hlen,
f9ed25
-				   packet -> raw -> chaddr)
f9ed25
+		  ? print_hw_addr_or_client_id(packet)
f9ed25
 		  : (lease
f9ed25
 		     ? print_hex_1(lease->uid_len, lease->uid, 60)
f9ed25
 		     : "<no identifier>")),
f9ed25
@@ -1732,8 +1756,7 @@ void dhcpinform (packet, ms_nulltp)
f9ed25
 	/* Report what we're sending. */
f9ed25
 	snprintf(msgbuf, sizeof msgbuf, "DHCPACK to %s (%s) via", piaddr(cip),
f9ed25
 		 (packet->raw->htype && packet->raw->hlen) ?
f9ed25
-			print_hw_addr(packet->raw->htype, packet->raw->hlen,
f9ed25
-				      packet->raw->chaddr) :
f9ed25
+			print_hw_addr_or_client_id(packet) :
f9ed25
 			"<no client hardware address>");
f9ed25
 	log_info("%s %s", msgbuf, gip.len ? piaddr(gip) :
f9ed25
 					    packet->interface->name);
f9ed25
@@ -1918,9 +1941,7 @@ void nak_lease (packet, cip, network_group)
f9ed25
 #endif
f9ed25
 	log_info ("DHCPNAK on %s to %s via %s",
f9ed25
 	      piaddr (*cip),
f9ed25
-	      print_hw_addr (packet -> raw -> htype,
f9ed25
-			     packet -> raw -> hlen,
f9ed25
-			     packet -> raw -> chaddr),
f9ed25
+	      print_hw_addr_or_client_id(packet),
f9ed25
 	      packet -> raw -> giaddr.s_addr
f9ed25
 	      ? inet_ntoa (packet -> raw -> giaddr)
f9ed25
 	      : packet -> interface -> name);
f9ed25
@@ -3936,7 +3957,7 @@ void dhcp_reply (lease)
f9ed25
 		   ? (state -> offer == DHCPACK ? "DHCPACK" : "DHCPOFFER")
f9ed25
 		   : "BOOTREPLY"),
f9ed25
 		  piaddr (lease -> ip_addr),
f9ed25
-		  (lease -> hardware_addr.hlen
f9ed25
+		  (lease -> hardware_addr.hlen > 1
f9ed25
 		   ? print_hw_addr (lease -> hardware_addr.hbuf [0],
f9ed25
 				    lease -> hardware_addr.hlen - 1,
f9ed25
 				    &lease -> hardware_addr.hbuf [1])
f9ed25
@@ -4497,10 +4518,7 @@ int find_lease (struct lease **lp,
f9ed25
 			if (uid_lease) {
f9ed25
 			    if (uid_lease->binding_state == FTS_ACTIVE) {
f9ed25
 				log_error ("client %s has duplicate%s on %s",
f9ed25
-					   (print_hw_addr
f9ed25
-					    (packet -> raw -> htype,
f9ed25
-					     packet -> raw -> hlen,
f9ed25
-					     packet -> raw -> chaddr)),
f9ed25
+					   (print_hw_addr_or_client_id(packet)),
f9ed25
 					   " leases",
f9ed25
 					   (ip_lease -> subnet ->
f9ed25
 					    shared_network -> name));
f9ed25
@@ -4667,9 +4685,7 @@ int find_lease (struct lease **lp,
f9ed25
 			log_error("uid lease %s for client %s is duplicate "
f9ed25
 				  "on %s",
f9ed25
 				  piaddr(uid_lease->ip_addr),
f9ed25
-				  print_hw_addr(packet->raw->htype,
f9ed25
-						packet->raw->hlen,
f9ed25
-						packet->raw->chaddr),
f9ed25
+				  print_hw_addr_or_client_id(packet),
f9ed25
 				  uid_lease->subnet->shared_network->name);
f9ed25
 
f9ed25
 			if (!packet -> raw -> ciaddr.s_addr &&
f9ed25
-- 
f9ed25
2.14.5
f9ed25