]> The Tcpdump Group git mirrors - tcpdump/blobdiff - print-nfs.c
When discussing the "gateway" keyword, don't say that the host must be
[tcpdump] / print-nfs.c
index 4f3afa58d0e0f055ab4f1243995169f83b82e983..d195ec759a2d3e7026a6f129a4e094009fd93073 100644 (file)
@@ -21,7 +21,7 @@
 
 #ifndef lint
 static const char rcsid[] =
-    "@(#) $Header: /tcpdump/master/tcpdump/print-nfs.c,v 1.72 2000-06-01 01:08:18 assar Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/tcpdump/print-nfs.c,v 1.87 2000-10-07 05:53:12 itojun Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -32,16 +32,10 @@ static const char rcsid[] =
 #include <sys/time.h>
 #include <sys/socket.h>
 
-#if __STDC__
 struct mbuf;
 struct rtentry;
-#endif
-#include <net/if.h>
 
 #include <netinet/in.h>
-#include <netinet/if_ether.h>
-#include <netinet/in_systm.h>
-#include <netinet/ip.h>
 
 #include <rpc/rpc.h>
 
@@ -56,14 +50,20 @@ struct rtentry;
 #include "nfs.h"
 #include "nfsfh.h"
 
+#include "ip.h"
+#ifdef INET6
+#include "ip6.h"
+#endif
+
 static void nfs_printfh(const u_int32_t *, const u_int);
-static void xid_map_enter(const struct rpc_msg *, const struct ip *);
-static int32_t xid_map_find(const struct rpc_msg *, const struct ip *,
+static void xid_map_enter(const struct rpc_msg *, const u_char *);
+static int32_t xid_map_find(const struct rpc_msg *, const u_char *,
                            u_int32_t *, u_int32_t *);
 static void interp_reply(const struct rpc_msg *, u_int32_t, u_int32_t, int);
 static const u_int32_t *parse_post_op_attr(const u_int32_t *, int);
 static void print_sattr3(const struct nfsv3_sattr *sa3, int verbose);
 static int print_int64(const u_int32_t *dp, int how);
+static void print_nfsaddr(const u_char *, const char *, const char *);
 
 /*
  * Mapping of old NFS Version 2 RPC numbers to generic numbers.
@@ -97,6 +97,55 @@ u_int32_t nfsv3_procid[NFS_NPROCS] = {
        NFSPROC_NOOP
 };
 
+/*
+ * NFS V2 and V3 status values.
+ *
+ * Some of these come from the RFCs for NFS V2 and V3, with the message
+ * strings taken from the FreeBSD C library "errlst.c".
+ *
+ * Others are errors that are not in the RFC but that I suspect some
+ * NFS servers could return; the values are FreeBSD errno values, as
+ * the first NFS server was the SunOS 2.0 one, and until 5.0 SunOS
+ * was primarily BSD-derived.
+ */
+static struct tok status2str[] = {
+       { 1,     "Operation not permitted" },   /* EPERM */
+       { 2,     "No such file or directory" }, /* ENOENT */
+       { 5,     "Input/output error" },        /* EIO */
+       { 6,     "Device not configured" },     /* ENXIO */
+       { 11,    "Resource deadlock avoided" }, /* EDEADLK */
+       { 12,    "Cannot allocate memory" },    /* ENOMEM */
+       { 13,    "Permission denied" },         /* EACCES */
+       { 17,    "File exists" },               /* EEXIST */
+       { 18,    "Cross-device link" },         /* EXDEV */
+       { 19,    "Operation not supported by device" }, /* ENODEV */
+       { 20,    "Not a directory" },           /* ENOTDIR */
+       { 21,    "Is a directory" },            /* EISDIR */
+       { 22,    "Invalid argument" },          /* EINVAL */
+       { 26,    "Text file busy" },            /* ETXTBSY */
+       { 27,    "File too large" },            /* EFBIG */
+       { 28,    "No space left on device" },   /* ENOSPC */
+       { 30,    "Read-only file system" },     /* EROFS */
+       { 31,    "Too many links" },            /* EMLINK */
+       { 45,    "Operation not supported" },   /* EOPNOTSUPP */
+       { 62,    "Too many levels of symbolic links" }, /* ELOOP */
+       { 63,    "File name too long" },        /* ENAMETOOLONG */
+       { 66,    "Directory not empty" },       /* ENOTEMPTY */
+       { 69,    "Disc quota exceeded" },       /* EDQUOT */
+       { 70,    "Stale NFS file handle" },     /* ESTALE */
+       { 71,    "Too many levels of remote in path" }, /* EREMOTE */
+       { 99,    "Write cache flushed to disk" }, /* NFSERR_WFLUSH (not used) */
+       { 10001, "Illegal NFS file handle" },   /* NFS3ERR_BADHANDLE */
+       { 10002, "Update synchronization mismatch" }, /* NFS3ERR_NOT_SYNC */
+       { 10003, "READDIR/READDIRPLUS cookie is stale" }, /* NFS3ERR_BAD_COOKIE */
+       { 10004, "Operation not supported" },   /* NFS3ERR_NOTSUPP */
+       { 10005, "Buffer or request is too small" }, /* NFS3ERR_TOOSMALL */
+       { 10006, "Unspecified error on server" }, /* NFS3ERR_SERVERFAULT */
+       { 10007, "Object of that type not supported" }, /* NFS3ERR_BADTYPE */
+       { 10008, "Request couldn't be completed in time" }, /* NFS3ERR_JUKEBOX */
+       { 0,     NULL }
+};
+
 static struct tok nfsv3_writemodes[] = {
        { 0,            "unstable" },
        { 1,            "datasync" },
@@ -149,18 +198,58 @@ static int print_int64(const u_int32_t *dp, int how)
        }
 #else
        switch (how) {
-       case SIGNED :
-       case UNSIGNED :
-       case HEX :
-           printf ("0x%x%08x", dp[0], dp[1]);
-           break;
-       default :
-           return (0);
+       case SIGNED:
+       case UNSIGNED:
+       case HEX:
+               printf("0x%x%08x", (u_int32_t)ntohl(dp[0]),
+                   (u_int32_t)ntohl(dp[1]));
+               break;
+       default:
+               return (0);
        }
 #endif
        return 1;
 }
 
+static void
+print_nfsaddr(const u_char *bp, const char *s, const char *d)
+{
+       struct ip *ip;
+#ifdef INET6
+       struct ip6_hdr *ip6;
+       char srcaddr[INET6_ADDRSTRLEN], dstaddr[INET6_ADDRSTRLEN];
+#else
+#ifndef INET_ADDRSTRLEN
+#define INET_ADDRSTRLEN        16
+#endif
+       char srcaddr[INET_ADDRSTRLEN], dstaddr[INET_ADDRSTRLEN];
+#endif
+
+       srcaddr[0] = dstaddr[0] = '\0';
+       switch (IP_V((struct ip *)bp)) {
+       case 4:
+               ip = (struct ip *)bp;
+               strlcpy(srcaddr, ipaddr_string(&ip->ip_src), sizeof(srcaddr));
+               strlcpy(dstaddr, ipaddr_string(&ip->ip_dst), sizeof(dstaddr));
+               break;
+#ifdef INET6
+       case 6:
+               ip6 = (struct ip6_hdr *)bp;
+               strlcpy(srcaddr, ip6addr_string(&ip6->ip6_src),
+                   sizeof(srcaddr));
+               strlcpy(dstaddr, ip6addr_string(&ip6->ip6_dst),
+                   sizeof(dstaddr));
+               break;
+#endif
+       default:
+               strlcpy(srcaddr, "?", sizeof(srcaddr));
+               strlcpy(dstaddr, "?", sizeof(dstaddr));
+               break;
+       }
+
+       (void)printf("%s.%s > %s.%s: ", srcaddr, s, dstaddr, d);
+}
+
 static const u_int32_t *
 parse_sattr3(const u_int32_t *dp, struct nfsv3_sattr *sa3)
 {
@@ -233,32 +322,28 @@ nfsreply_print(register const u_char *bp, u_int length,
               register const u_char *bp2)
 {
        register const struct rpc_msg *rp;
-       register const struct ip *ip;
        u_int32_t proc, vers;
+       char srcid[20], dstid[20];      /*fits 32bit*/
 
        nfserr = 0;             /* assume no error */
        rp = (const struct rpc_msg *)bp;
-       ip = (const struct ip *)bp2;
-
-       if (!nflag)
-               (void)printf("%s.nfs > %s.%u: reply %s %d",
-                            ipaddr_string(&ip->ip_src),
-                            ipaddr_string(&ip->ip_dst),
-                            (u_int32_t)ntohl(rp->rm_xid),
-                            ntohl(rp->rm_reply.rp_stat) == MSG_ACCEPTED?
-                                    "ok":"ERR",
-                            length);
-       else
-               (void)printf("%s.%u > %s.%u: reply %s %d",
-                            ipaddr_string(&ip->ip_src),
-                            NFS_PORT,
-                            ipaddr_string(&ip->ip_dst),
-                            (u_int32_t)ntohl(rp->rm_xid),
-                            ntohl(rp->rm_reply.rp_stat) == MSG_ACCEPTED?
-                               "ok":"ERR",
+
+       if (!nflag) {
+               strlcpy(srcid, "nfs", sizeof(srcid));
+               snprintf(dstid, sizeof(dstid), "%u",
+                   (u_int32_t)ntohl(rp->rm_xid));
+       } else {
+               snprintf(srcid, sizeof(srcid), "%u", NFS_PORT);
+               snprintf(dstid, sizeof(dstid), "%u",
+                   (u_int32_t)ntohl(rp->rm_xid));
+       }
+       print_nfsaddr(bp2, srcid, dstid);
+       (void)printf("reply %s %d",
+                    ntohl(rp->rm_reply.rp_stat) == MSG_ACCEPTED?
+                            "ok":"ERR",
                             length);
 
-       if (xid_map_find(rp, ip, &proc, &vers) >= 0)
+       if (xid_map_find(rp, bp2, &proc, &vers) >= 0)
                interp_reply(rp, proc, vers, length);
 }
 
@@ -368,31 +453,28 @@ nfsreq_print(register const u_char *bp, u_int length,
     register const u_char *bp2)
 {
        register const struct rpc_msg *rp;
-       register const struct ip *ip;
        register const u_int32_t *dp;
-       nfstype type;
+       nfs_type type;
        int v3;
        u_int32_t proc;
        struct nfsv3_sattr sa3;
+       char srcid[20], dstid[20];      /*fits 32bit*/
 
        nfserr = 0;             /* assume no error */
        rp = (const struct rpc_msg *)bp;
-       ip = (const struct ip *)bp2;
-       if (!nflag)
-               (void)printf("%s.%u > %s.nfs: %d",
-                            ipaddr_string(&ip->ip_src),
-                            (u_int32_t)ntohl(rp->rm_xid),
-                            ipaddr_string(&ip->ip_dst),
-                            length);
-       else
-               (void)printf("%s.%u > %s.%u: %d",
-                            ipaddr_string(&ip->ip_src),
-                            (u_int32_t)ntohl(rp->rm_xid),
-                            ipaddr_string(&ip->ip_dst),
-                            NFS_PORT,
-                            length);
+       if (!nflag) {
+               snprintf(srcid, sizeof(srcid), "%u",
+                   (u_int32_t)ntohl(rp->rm_xid));
+               strlcpy(dstid, "nfs", sizeof(dstid));
+       } else {
+               snprintf(srcid, sizeof(srcid), "%u",
+                   (u_int32_t)ntohl(rp->rm_xid));
+               snprintf(dstid, sizeof(dstid), "%u", NFS_PORT);
+       }
+       print_nfsaddr(bp2, srcid, dstid);
+       (void)printf("%d", length);
 
-       xid_map_enter(rp, ip);  /* record proc number for later on */
+       xid_map_enter(rp, bp2); /* record proc number for later on */
 
        v3 = (ntohl(rp->rm_call.cb_vers) == NFS_VER3);
        proc = ntohl(rp->rm_call.cb_proc);
@@ -434,7 +516,7 @@ nfsreq_print(register const u_char *bp, u_int length,
                if ((dp = parsereq(rp, length)) != NULL &&
                    (dp = parsefh(dp, v3)) != NULL) {
                        TCHECK(dp[0]);
-                       printf(" %04x", (unsigned)ntohl(dp[0]));
+                       printf(" %04x", (u_int32_t)ntohl(dp[0]));
                        return;
                }
                break;
@@ -526,15 +608,15 @@ nfsreq_print(register const u_char *bp, u_int length,
                if ((dp = parsereq(rp, length)) != 0 &&
                    (dp = parsefhn(dp, v3)) != 0) {
                        TCHECK(*dp);
-                       type = (nfstype)ntohl(*dp++);
+                       type = (nfs_type)ntohl(*dp++);
                        if ((dp = parse_sattr3(dp, &sa3)) == 0)
                                break;
                        printf(" %s", tok2str(type2str, "unk-ft %d", type));
                        if (vflag && (type == NFCHR || type == NFBLK)) {
                                TCHECK(dp[1]);
                                printf(" %u/%u",
-                                      (unsigned)ntohl(dp[0]),
-                                      (unsigned)ntohl(dp[1]));
+                                      (u_int32_t)ntohl(dp[0]),
+                                      (u_int32_t)ntohl(dp[1]));
                                dp += 2;
                        }
                        if (vflag)
@@ -684,6 +766,7 @@ nfs_printfh(register const u_int32_t *dp, const u_int len)
 
                /* Make sure string is null-terminated */
                strncpy(temp, sfsname, NFSX_V3FHMAX);
+               temp[sizeof(temp) - 1] = '\0';
                /* Remove trailing spaces */
                sfsname = strchr(temp, ' ');
                if (sfsname)
@@ -709,11 +792,17 @@ nfs_printfh(register const u_int32_t *dp, const u_int len)
  */
 
 struct xid_map_entry {
-       u_int32_t               xid;            /* transaction ID (net order) */
+       u_int32_t       xid;            /* transaction ID (net order) */
+       int ipver;                      /* IP version (4 or 6) */
+#ifdef INET6
+       struct in6_addr client;         /* client IP address (net order) */
+       struct in6_addr server;         /* server IP address (net order) */
+#else
        struct in_addr  client;         /* client IP address (net order) */
        struct in_addr  server;         /* server IP address (net order) */
-       u_int32_t               proc;           /* call proc number (host order) */
-       u_int32_t               vers;   /* program version (host order) */
+#endif
+       u_int32_t       proc;           /* call proc number (host order) */
+       u_int32_t       vers;           /* program version (host order) */
 };
 
 /*
@@ -730,18 +819,45 @@ int       xid_map_next = 0;
 int    xid_map_hint = 0;
 
 static void
-xid_map_enter(const struct rpc_msg *rp, const struct ip *ip)
+xid_map_enter(const struct rpc_msg *rp, const u_char *bp)
 {
+       struct ip *ip = NULL;
+#ifdef INET6
+       struct ip6_hdr *ip6 = NULL;
+#endif
        struct xid_map_entry *xmep;
 
+       switch (IP_V((struct ip *)bp)) {
+       case 4:
+               ip = (struct ip *)bp;
+               break;
+#ifdef INET6
+       case 6:
+               ip6 = (struct ip6_hdr *)bp;
+               break;
+#endif
+       default:
+               return;
+       }
+
        xmep = &xid_map[xid_map_next];
 
        if (++xid_map_next >= XIDMAPSIZE)
                xid_map_next = 0;
 
        xmep->xid = rp->rm_xid;
-       xmep->client = ip->ip_src;
-       xmep->server = ip->ip_dst;
+       if (ip) {
+               xmep->ipver = 4;
+               memcpy(&xmep->client, &ip->ip_src, sizeof(ip->ip_src));
+               memcpy(&xmep->server, &ip->ip_dst, sizeof(ip->ip_dst));
+       }
+#ifdef INET6
+       else if (ip6) {
+               xmep->ipver = 6;
+               memcpy(&xmep->client, &ip6->ip6_src, sizeof(ip6->ip6_src));
+               memcpy(&xmep->server, &ip6->ip6_dst, sizeof(ip6->ip6_dst));
+       }
+#endif
        xmep->proc = ntohl(rp->rm_call.cb_proc);
        xmep->vers = ntohl(rp->rm_call.cb_vers);
 }
@@ -751,27 +867,56 @@ xid_map_enter(const struct rpc_msg *rp, const struct ip *ip)
  * version in vers return, or returns -1 on failure
  */
 static int
-xid_map_find(const struct rpc_msg *rp, const struct ip *ip, u_int32_t *proc,
+xid_map_find(const struct rpc_msg *rp, const u_char *bp, u_int32_t *proc,
             u_int32_t *vers)
 {
        int i;
        struct xid_map_entry *xmep;
        u_int32_t xid = rp->rm_xid;
-       u_int32_t clip = ip->ip_dst.s_addr;
-       u_int32_t sip = ip->ip_src.s_addr;
+       struct ip *ip = (struct ip *)bp;
+#ifdef INET6
+       struct ip6_hdr *ip6 = (struct ip6_hdr *)bp;
+#endif
+       int cmp;
 
        /* Start searching from where we last left off */
-       i = xid_map_hint;
+       i = xid_map_hint; 
        do {
                xmep = &xid_map[i];
-               if (xmep->xid == xid && xmep->client.s_addr == clip &&
-                   xmep->server.s_addr == sip) {
+               cmp = 1;
+               if (xmep->ipver != IP_V(ip) || xmep->xid != xid)
+                       goto nextitem;
+               switch (xmep->ipver) {
+               case 4:
+                       if (memcmp(&ip->ip_src, &xmep->server,
+                                  sizeof(ip->ip_src)) != 0 ||
+                           memcmp(&ip->ip_dst, &xmep->client,
+                                  sizeof(ip->ip_dst)) != 0) {
+                               cmp = 0;
+                       }
+                       break;
+#ifdef INET6
+               case 6:
+                       if (memcmp(&ip6->ip6_src, &xmep->server,
+                                  sizeof(ip6->ip6_src)) != 0 ||
+                           memcmp(&ip6->ip6_dst, &xmep->client,
+                                  sizeof(ip6->ip6_dst)) != 0) {
+                               cmp = 0;
+                       }
+                       break;
+#endif
+               default:
+                       cmp = 0;
+                       break;
+               }
+               if (cmp) {
                        /* match */
                        xid_map_hint = i;
                        *proc = xmep->proc;
                        *vers = xmep->vers;
                        return 0;
                }
+       nextitem:
                if (++i >= XIDMAPSIZE)
                        i = 0;
        } while (i != xid_map_hint);
@@ -879,7 +1024,8 @@ parsestatus(const u_int32_t *dp, int *er)
                *er = errnum;
        if (errnum != 0) {
                if (!qflag)
-                       printf(" ERROR: %s", pcap_strerror(errnum));
+                       printf(" ERROR: %s",
+                           tok2str(status2str, "unk %d", errnum));
                nfserr = 1;
                return (NULL);
        }
@@ -1066,7 +1212,7 @@ parserddires(const u_int32_t *dp)
 
        TCHECK(dp[2]);
        printf(" offset %x size %d ",
-              (unsigned)ntohl(dp[0]), (unsigned)ntohl(dp[1]));
+              (u_int32_t)ntohl(dp[0]), (u_int32_t)ntohl(dp[1]));
        if (dp[2] != 0)
                printf(" eof");
 
@@ -1081,8 +1227,8 @@ parse_wcc_attr(const u_int32_t *dp)
        printf(" sz ");
        print_int64(dp, UNSIGNED);
        printf(" mtime %u.%06u ctime %u.%06u",
-              (unsigned)ntohl(dp[2]), (unsigned)ntohl(dp[3]),
-              (unsigned)ntohl(dp[4]), (unsigned)ntohl(dp[5]));
+              (u_int32_t)ntohl(dp[2]), (u_int32_t)ntohl(dp[3]),
+              (u_int32_t)ntohl(dp[4]), (u_int32_t)ntohl(dp[5]));
        return (dp + 6);
 }
 
@@ -1349,7 +1495,7 @@ interp_reply(const struct rpc_msg *rp, u_int32_t proc, u_int32_t vers, int lengt
                if (!(dp = parse_post_op_attr(dp, vflag)))
                        break;
                if (!er)
-                       printf(" c %04x", (unsigned)ntohl(dp[0]));
+                       printf(" c %04x", (u_int32_t)ntohl(dp[0]));
                return;
 
        case NFSPROC_READLINK: