Skip to contentSkip to conversation

SUNRPC: Add a function to format the address in an svc_rqst for printing

PATCH In review Chuck Lever 3 participants 4 messages Nov 1, 2006 – Nov 10, 2006

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 5/12 SUNRPC: Add a function to format the address in an svc_rqst for printing

There are loads of places where the RPC server assumes that the rq_addr fields contains an IPv4 address. Top among these are error and debugging messages that display the server's IP address.

Let's refactor the address printing into a separate function that's smart enough to figure out the difference between IPv4 and IPv6 addresses.

Cc Aurelien Charbon <[email protected]>

---

 fs/lockd/svc.c             |    6 ++--
 fs/lockd/svc4proc.c        |    7 ++--
 fs/lockd/svcproc.c         |    7 ++--
 fs/nfs/callback.c          |   12 +++++--
 fs/nfsd/nfsfh.c            |    8 ++---
 fs/nfsd/nfsproc.c          |    9 +++--
 include/linux/sunrpc/svc.h |    3 ++
 net/sunrpc/svcsock.c       |   74 ++++++++++++++++++++++++++++++++------------
 8 files changed, 84 insertions(+), 42 deletions(-)
fs/lockd/svc.c +3 -3
index 6341392..de5d221 100644
@@ -141,6 +141,7 @@ lockd(struct svc_rqst *rqstp)
 	 */
 	while ((nlmsvc_users || !signalled()) && nlmsvc_pid == current->pid) {
 		long timeout = MAX_SCHEDULE_TIMEOUT;
+		char buf[RPC_MAX_ADDRBUFLEN];
 
 		if (signalled()) {
 			flush_signals(current);
@@ -175,11 +176,10 @@ lockd(struct svc_rqst *rqstp)
 			break;
 		}
 
-		dprintk("lockd: request from %08x\n",
-			(unsigned)ntohl(rqstp->rq_addr.sin_addr.s_addr));
+		svc_print_addr(rqstp, buf, sizeof(buf));
+		dprintk("lockd: request from %s\n", buf);
 
 		svc_process(rqstp);
-
 	}
 
 	flush_signals(current);
fs/lockd/svc4proc.c +3 -4
index f67146a..a66f9d5 100644
@@ -426,10 +426,9 @@ nlm4svc_proc_sm_notify(struct svc_rqst *
 	dprintk("lockd: SM_NOTIFY     called\n");
 	if (saddr.sin_addr.s_addr != htonl(INADDR_LOOPBACK)
 	 || ntohs(saddr.sin_port) >= 1024) {
-		printk(KERN_WARNING
-			"lockd: rejected NSM callback from %08x:%d\n",
-			ntohl(rqstp->rq_addr.sin_addr.s_addr),
-			ntohs(rqstp->rq_addr.sin_port));
+		char buf[RPC_MAX_ADDRBUFLEN];
+		svc_print_addr(rqstp, buf, sizeof(buf));
+		printk(KERN_WARNING "lockd: rejected NSM callback from %s\n", buf);
 		return rpc_system_err;
 	}
 
fs/lockd/svcproc.c +3 -4
index 3707c3a..19e1ac1 100644
@@ -457,10 +457,9 @@ nlmsvc_proc_sm_notify(struct svc_rqst *r
 	dprintk("lockd: SM_NOTIFY     called\n");
 	if (saddr.sin_addr.s_addr != htonl(INADDR_LOOPBACK)
 	 || ntohs(saddr.sin_port) >= 1024) {
-		printk(KERN_WARNING
-			"lockd: rejected NSM callback from %08x:%d\n",
-			ntohl(rqstp->rq_addr.sin_addr.s_addr),
-			ntohs(rqstp->rq_addr.sin_port));
+		char buf[RPC_MAX_ADDRBUFLEN];
+		svc_print_addr(rqstp, buf, sizeof(buf));
+		printk(KERN_WARNING "lockd: rejected NSM callback from %s\n", buf);
 		return rpc_system_err;
 	}
 
fs/nfs/callback.c +9 -3
index 9bdf766..f29ce6a 100644
@@ -71,6 +71,8 @@ static void nfs_callback_svc(struct svc_
 	complete(&nfs_callback_info.started);
 
 	for(;;) {
+		char buf[RPC_MAX_ADDRBUFLEN];
+
 		if (signalled()) {
 			if (nfs_callback_info.users == 0)
 				break;
@@ -88,8 +90,8 @@ static void nfs_callback_svc(struct svc_
 					__FUNCTION__, -err);
 			break;
 		}
-		dprintk("%s: request from %u.%u.%u.%u\n", __FUNCTION__,
-				NIPQUAD(rqstp->rq_addr.sin_addr.s_addr));
+		svc_print_addr(rqstp, buf, sizeof(buf));
+		dprintk("%s: request from %s\n", __FUNCTION__, buf);
 		svc_process(rqstp);
 	}
 
@@ -170,13 +172,17 @@ static int nfs_callback_authenticate(str
 {
 	struct sockaddr_in *addr = &rqstp->rq_addr;
 	struct nfs_client *clp;
+	char buf[RPC_MAX_ADDRBUFLEN];
 
 	/* Don't talk to strangers */
 	clp = nfs_find_client(addr, 4);
 	if (clp == NULL)
 		return SVC_DROP;
-	dprintk("%s: %u.%u.%u.%u NFSv4 callback!\n", __FUNCTION__, NIPQUAD(addr->sin_addr));
+
+	svc_print_addr(rqstp, buf, sizeof(buf));
+	dprintk("%s: %s NFSv4 callback!\n", __FUNCTION__, buf);
 	nfs_put_client(clp);
+
 	switch (rqstp->rq_authop->flavour) {
 		case RPC_AUTH_NULL:
 			if (rqstp->rq_proc != CB_NULL)
fs/nfsd/nfsfh.c +4 -4
index 727ab3b..19e7bc3 100644
@@ -20,6 +20,7 @@ #include <linux/dcache.h>
 #include <linux/mount.h>
 #include <asm/pgtable.h>
 
+#include <linux/sunrpc/clnt.h>
 #include <linux/sunrpc/svc.h>
 #include <linux/nfsd/nfsd.h>
 
@@ -180,10 +181,9 @@ fh_verify(struct svc_rqst *rqstp, struct
 		/* Check if the request originated from a secure port. */
 		error = nfserr_perm;
 		if (!rqstp->rq_secure && EX_SECURE(exp)) {
-			printk(KERN_WARNING
-			       "nfsd: request from insecure port (%u.%u.%u.%u:%d)!\n",
-			       NIPQUAD(rqstp->rq_addr.sin_addr.s_addr),
-			       ntohs(rqstp->rq_addr.sin_port));
+			char buf[RPC_MAX_ADDRBUFLEN];
+			svc_print_addr(rqstp, buf, sizeof(buf));
+			printk(KERN_WARNING "nfsd: request from insecure port %s!\n", buf);
 			goto out;
 		}
 
fs/nfsd/nfsproc.c +5 -4
index ec983b7..5612e00 100644
@@ -19,6 +19,7 @@ #include <linux/namei.h>
 #include <linux/unistd.h>
 #include <linux/slab.h>
 
+#include <linux/sunrpc/clnt.h>
 #include <linux/sunrpc/svc.h>
 #include <linux/nfsd/nfsd.h>
 #include <linux/nfsd/cache.h>
@@ -147,11 +148,11 @@ nfsd_proc_read(struct svc_rqst *rqstp, s
 	 */
 
 	if (NFSSVC_MAXBLKSIZE_V2 < argp->count) {
+		char buf[RPC_MAX_ADDRBUFLEN];
+		svc_print_addr(rqstp, buf, sizeof(buf));
 		printk(KERN_NOTICE
-			"oversized read request from %u.%u.%u.%u:%d (%d bytes)\n",
-				NIPQUAD(rqstp->rq_addr.sin_addr.s_addr),
-				ntohs(rqstp->rq_addr.sin_port),
-				argp->count);
+			"oversized read request from %s (%d bytes)\n",
+				buf, argp->count);
 		argp->count = NFSSVC_MAXBLKSIZE_V2;
 	}
 	svc_reserve(rqstp, (19<<2) + argp->count + 4);
include/linux/sunrpc/svc.h +3 -0
index 6fc2684..44e15ca 100644
@@ -366,5 +366,8 @@ int		   svc_register(struct svc_serv *, 
 void		   svc_wake_up(struct svc_serv *);
 void		   svc_reserve(struct svc_rqst *rqstp, int space);
 struct svc_pool *  svc_pool_for_cpu(struct svc_serv *serv, int cpu);
+void		   svc_print_addr(struct svc_rqst *, char *, size_t);
+
+#define	RPC_MAX_ADDRBUFLEN	(63U)
 
 #endif /* SUNRPC_SVC_H */
net/sunrpc/svcsock.c +54 -20

127 lines not on this page · raw message

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
NFS maillist - [email protected]
https://lists.sourceforge.net/lists/listinfo/nfs

#1 lore raw
Quoted 14 lines · On Wednesday November 1, [email protected] wrote:
fs/lockd/svc.c +3 -2
 		long timeout = MAX_SCHEDULE_TIMEOUT;
+		char buf[RPC_MAX_ADDRBUFLEN];
 
 		if (signalled()) {
 			flush_signals(current);
@@ -175,11 +176,10 @@ lockd(struct svc_rqst *rqstp)
 			break;
 		}
 
-		dprintk("lockd: request from %08x\n",
-			(unsigned)ntohl(rqstp->rq_addr.sin_addr.s_addr));
+		svc_print_addr(rqstp, buf, sizeof(buf));
+		dprintk("lockd: request from %s\n", buf);
 

....

net/sunrpc/svcsock.c static struct cache_deferred_req *svc_de · line 120

Quoted 12 lines
net/sunrpc/svcsock.c +12 -0
+/**
+ * svc_print_addr - Format rq_addr field for printing
+ * @rqstp: svc_rqst struct containing address to print
+ * @buf: target buffer for formatted address
+ * @len: length of target buffer
+ *
+ */
+void svc_print_addr(struct svc_rqst *rqstp, char *buf, size_t len)
+{
+	__svc_print_addr((struct sockaddr *) &rqstp->rq_addr, buf, len);
+
+}

Can we have svc_print_addr take a
char buf[RPC_MAX_ADDRBUFLEN]
rather than a char* and a len? The buffer is always the same size, so best to hard code it..

Also can svc_print_addr return a 'char *' being 'buf'. Then we can

Review

Quoted 2 lines
(quoted diff) +2 -0
+		dprintk("lockd: request from %s\n",
+			svc_print_addr(rqstp, buf));

This makes it less verbose and similar to bdevname().

Thanks,
NeilBrown

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
NFS maillist - [email protected]
https://lists.sourceforge.net/lists/listinfo/nfs

#2 lore raw
Quoted 10 lines · Neil Brown

net/sunrpc/svcsock.c static struct cache_deferred_req *svc_de · line 120

Quoted 5 lines
net/sunrpc/svcsock.c +5 -0
+void svc_print_addr(struct svc_rqst *rqstp, char *buf, size_t len)
+{
+	__svc_print_addr((struct sockaddr *) &rqstp->rq_addr, buf, len);
+
+}

Can we have svc_print_addr take a
char buf[RPC_MAX_ADDRBUFLEN]
rather than a char* and a len? The buffer is always the same size, so best to hard code it..

The compiler will still let you pass a buf[6] without complaining. Assumptions that buffer arguments are "big enough" make for great security bugs two years down the road because people forget. There's a reason why getwd and gets have been deprecated for 18 years :)

If you want to make it easy, why not have

#define RPC_SVC_ADDR(rqstp, buf) \
(svc_print_addr(rqstp, buf, sizeof(buf)))

Quoted 1 line

Also can svc_print_addr return a 'char *' being 'buf'.

That'd be nice.

Olaf

Signature · 10 lines
Walks like a duck. Quacks like a duck. Must be a chicken.

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
NFS maillist  -  [email protected]
https://lists.sourceforge.net/lists/listinfo/nfs
#3 lore raw
Quoted 12 lines · On Thursday November 9, [email protected] wrote:
Quoted 10 lines · Neil Brown

net/sunrpc/svcsock.c static struct cache_deferred_req *svc_de · line 120

Quoted 5 lines
net/sunrpc/svcsock.c +5 -0
+void svc_print_addr(struct svc_rqst *rqstp, char *buf, size_t len)
+{
+	__svc_print_addr((struct sockaddr *) &rqstp->rq_addr, buf, len);
+
+}

Can we have svc_print_addr take a
char buf[RPC_MAX_ADDRBUFLEN]
rather than a char* and a len? The buffer is always the same size, so best to hard code it..

Quoted 4 lines

The compiler will still let you pass a buf[6] without complaining. Assumptions that buffer arguments are "big enough" make for great security bugs two years down the road because people forget. There's a reason why getwd and gets have been deprecated for 18 years :)

I had hoped that sparse would pick it up, but it seems now.

Quoted 5 lines

If you want to make it easy, why not have

#define RPC_SVC_ADDR(rqstp, buf) \
(svc_print_addr(rqstp, buf, sizeof(buf)))

That's one option.
Another would be

  struct rpc_addr { char buf[RPC_MAX_ADDRBUFLEN]; };

  char *svc_print_addr(struct svc_rqst rqstp, struct rpc_addr *buf)
  {
       ....
       return buf->buf;
  }

so we force the compiler to check the allocated space.

NeilBrown

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
NFS maillist - [email protected]
https://lists.sourceforge.net/lists/listinfo/nfs