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

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