git: 14105aae555c - main - nlm: Fix error messages for failed remote rpcbind contact

From: Mateusz Piotrowski <0mp_at_FreeBSD.org>
Date: Thu, 09 Nov 2023 21:01:57 UTC
The branch main has been updated by 0mp:

URL: https://cgit.FreeBSD.org/src/commit/?id=14105aae555cc22554d87ab041ee736c086f5ef1

commit 14105aae555cc22554d87ab041ee736c086f5ef1
Author:     Tom Jones <tom.jones@klarasystems.com>
AuthorDate: 2023-09-25 18:33:45 +0000
Commit:     Mateusz Piotrowski <0mp@FreeBSD.org>
CommitDate: 2023-11-09 20:54:28 +0000

    nlm: Fix error messages for failed remote rpcbind contact
    
    In case of a remote rpcbind connection timeout,
    the NFS kernel lock manager emits an error message
    along the lines of:
    
        NLM: failed to contact remote rpcbind, stat = 5, port = 28416
    
    In the Bugzilla PR, Garrett Wollman identified the following problems
    with that error message:
    
    - The error is in decimal, which can only be deciphered by reading the
      source code.
    - The port number is byte-swapped.
    - The error message does not identify the client the NLM is trying to
      communicate with.
    
    Fix the shortcomings of the current error message by:
    
    - Printing out the port number correctly.
    - Mentioning the remote client.
    
    The low-level decimal error remains an outstanding issue though.
    It seems like the error strings describing the error codes live outside
    of the kernel code currently.
    
    PR:             244698
    Reported by:    wollman
    Approved by:    allanjude
    Sponsored by:   National Bureau of Economic Research
    Sponsored by:   Klara, Inc.
    Co-authored-by: Mateusz Piotrowski <0mp@FreeBSD.org>
---
 sys/nlm/nlm_prot_impl.c | 28 ++++++++++++++++++++++++++--
 1 file changed, 26 insertions(+), 2 deletions(-)

diff --git a/sys/nlm/nlm_prot_impl.c b/sys/nlm/nlm_prot_impl.c
index a5b917b07f8b..216c4ac82b53 100644
--- a/sys/nlm/nlm_prot_impl.c
+++ b/sys/nlm/nlm_prot_impl.c
@@ -343,6 +343,12 @@ nlm_get_rpc(struct sockaddr *sa, rpcprog_t prog, rpcvers_t vers)
 	bool_t tryagain = FALSE;
 	struct portmap mapping;
 	u_short port = 0;
+	struct sockaddr_in *sin4;
+	char namebuf[INET_ADDRSTRLEN];
+#ifdef INET6
+	struct sockaddr_in6 *sin6;
+	char namebuf6[INET6_ADDRSTRLEN];
+#endif
 
 	/*
 	 * First we need to contact the remote RPCBIND service to find
@@ -489,8 +495,26 @@ again:
 		}
 
 		/* Otherwise, bad news. */
-		NLM_ERR("NLM: failed to contact remote rpcbind, "
-		    "stat = %d, port = %d\n", (int) stat, port);
+		switch (ss.ss_family) {
+			case AF_INET:
+				sin4 = (struct sockaddr_in *)&ss;
+				inet_ntop(ss.ss_family, &sin4->sin_addr,
+				    namebuf, sizeof namebuf);
+				NLM_ERR("NLM: failed to contact remote rpcbind, "
+				    "stat = %d, host = %s, port = %d\n",
+				    (int) stat, namebuf, htons(port));
+				break;
+#ifdef INET6
+			case AF_INET6:
+				sin6 = (struct sockaddr_in6 *)&ss;
+				inet_ntop(ss.ss_family, &sin6->sin6_addr,
+				    namebuf6, sizeof namebuf6);
+				NLM_ERR("NLM: failed to contact remote rpcbind, "
+				    "stat = %d, host = %s, port = %d\n",
+				    (int) stat, namebuf6, htons(port));
+				break;
+#endif
+		}
 		CLNT_DESTROY(rpcb);
 		return (NULL);
 	}