4 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that: (1) source code distributions
9 * retain the above copyright notice and this paragraph in its entirety, (2)
10 * distributions including binary code include the above copyright notice and
11 * this paragraph in its entirety in the documentation or other materials
12 * provided with the distribution, and (3) all advertising materials mentioning
13 * features or use of this software display the following acknowledgement:
14 * ``This product includes software developed by the University of California,
15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16 * the University nor the names of its contributors may be used to endorse
17 * or promote products derived from this software without specific prior
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
24 #include <sys/cdefs.h>
27 static const char rcsid
[] _U_
=
28 "@(#) Header: /tcpdump/master/tcpdump/print-nfs.c,v 1.106.2.4 2007/06/15 23:17:40 guy Exp (LBL)";
30 __RCSID("$NetBSD: tcpdump2rcsid.ex,v 1.1 2001/06/25 20:09:58 itojun Exp $");
38 #include <tcpdump-stdinc.h>
44 #include "interface.h"
45 #include "addrtoname.h"
58 static void nfs_printfh(const u_int32_t
*, const u_int
);
59 static void xid_map_enter(const struct sunrpc_msg
*, const u_char
*);
60 static int32_t xid_map_find(const struct sunrpc_msg
*, const u_char
*,
61 u_int32_t
*, u_int32_t
*);
62 static void interp_reply(const struct sunrpc_msg
*, u_int32_t
, u_int32_t
, int);
63 static const u_int32_t
*parse_post_op_attr(const u_int32_t
*, int);
64 static void print_sattr3(const struct nfsv3_sattr
*sa3
, int verbose
);
65 static void print_nfsaddr(const u_char
*, const char *, const char *);
68 * Mapping of old NFS Version 2 RPC numbers to generic numbers.
70 u_int32_t nfsv3_procid
[NFS_NPROCS
] = {
100 * NFS V2 and V3 status values.
102 * Some of these come from the RFCs for NFS V2 and V3, with the message
103 * strings taken from the FreeBSD C library "errlst.c".
105 * Others are errors that are not in the RFC but that I suspect some
106 * NFS servers could return; the values are FreeBSD errno values, as
107 * the first NFS server was the SunOS 2.0 one, and until 5.0 SunOS
108 * was primarily BSD-derived.
110 static struct tok status2str
[] = {
111 { 1, "Operation not permitted" }, /* EPERM */
112 { 2, "No such file or directory" }, /* ENOENT */
113 { 5, "Input/output error" }, /* EIO */
114 { 6, "Device not configured" }, /* ENXIO */
115 { 11, "Resource deadlock avoided" }, /* EDEADLK */
116 { 12, "Cannot allocate memory" }, /* ENOMEM */
117 { 13, "Permission denied" }, /* EACCES */
118 { 17, "File exists" }, /* EEXIST */
119 { 18, "Cross-device link" }, /* EXDEV */
120 { 19, "Operation not supported by device" }, /* ENODEV */
121 { 20, "Not a directory" }, /* ENOTDIR */
122 { 21, "Is a directory" }, /* EISDIR */
123 { 22, "Invalid argument" }, /* EINVAL */
124 { 26, "Text file busy" }, /* ETXTBSY */
125 { 27, "File too large" }, /* EFBIG */
126 { 28, "No space left on device" }, /* ENOSPC */
127 { 30, "Read-only file system" }, /* EROFS */
128 { 31, "Too many links" }, /* EMLINK */
129 { 45, "Operation not supported" }, /* EOPNOTSUPP */
130 { 62, "Too many levels of symbolic links" }, /* ELOOP */
131 { 63, "File name too long" }, /* ENAMETOOLONG */
132 { 66, "Directory not empty" }, /* ENOTEMPTY */
133 { 69, "Disc quota exceeded" }, /* EDQUOT */
134 { 70, "Stale NFS file handle" }, /* ESTALE */
135 { 71, "Too many levels of remote in path" }, /* EREMOTE */
136 { 99, "Write cache flushed to disk" }, /* NFSERR_WFLUSH (not used) */
137 { 10001, "Illegal NFS file handle" }, /* NFS3ERR_BADHANDLE */
138 { 10002, "Update synchronization mismatch" }, /* NFS3ERR_NOT_SYNC */
139 { 10003, "READDIR/READDIRPLUS cookie is stale" }, /* NFS3ERR_BAD_COOKIE */
140 { 10004, "Operation not supported" }, /* NFS3ERR_NOTSUPP */
141 { 10005, "Buffer or request is too small" }, /* NFS3ERR_TOOSMALL */
142 { 10006, "Unspecified error on server" }, /* NFS3ERR_SERVERFAULT */
143 { 10007, "Object of that type not supported" }, /* NFS3ERR_BADTYPE */
144 { 10008, "Request couldn't be completed in time" }, /* NFS3ERR_JUKEBOX */
148 static struct tok nfsv3_writemodes
[] = {
155 static struct tok type2str
[] = {
167 print_nfsaddr(const u_char
*bp
, const char *s
, const char *d
)
172 char srcaddr
[INET6_ADDRSTRLEN
], dstaddr
[INET6_ADDRSTRLEN
];
174 #ifndef INET_ADDRSTRLEN
175 #define INET_ADDRSTRLEN 16
177 char srcaddr
[INET_ADDRSTRLEN
], dstaddr
[INET_ADDRSTRLEN
];
180 srcaddr
[0] = dstaddr
[0] = '\0';
181 switch (IP_V((struct ip
*)bp
)) {
183 ip
= (struct ip
*)bp
;
184 strlcpy(srcaddr
, ipaddr_string(&ip
->ip_src
), sizeof(srcaddr
));
185 strlcpy(dstaddr
, ipaddr_string(&ip
->ip_dst
), sizeof(dstaddr
));
189 ip6
= (struct ip6_hdr
*)bp
;
190 strlcpy(srcaddr
, ip6addr_string(&ip6
->ip6_src
),
192 strlcpy(dstaddr
, ip6addr_string(&ip6
->ip6_dst
),
197 strlcpy(srcaddr
, "?", sizeof(srcaddr
));
198 strlcpy(dstaddr
, "?", sizeof(dstaddr
));
202 (void)printf("%s.%s > %s.%s: ", srcaddr
, s
, dstaddr
, d
);
205 static const u_int32_t
*
206 parse_sattr3(const u_int32_t
*dp
, struct nfsv3_sattr
*sa3
)
209 sa3
->sa_modeset
= EXTRACT_32BITS(dp
);
211 if (sa3
->sa_modeset
) {
213 sa3
->sa_mode
= EXTRACT_32BITS(dp
);
218 sa3
->sa_uidset
= EXTRACT_32BITS(dp
);
220 if (sa3
->sa_uidset
) {
222 sa3
->sa_uid
= EXTRACT_32BITS(dp
);
227 sa3
->sa_gidset
= EXTRACT_32BITS(dp
);
229 if (sa3
->sa_gidset
) {
231 sa3
->sa_gid
= EXTRACT_32BITS(dp
);
236 sa3
->sa_sizeset
= EXTRACT_32BITS(dp
);
238 if (sa3
->sa_sizeset
) {
240 sa3
->sa_size
= EXTRACT_32BITS(dp
);
245 sa3
->sa_atimetype
= EXTRACT_32BITS(dp
);
247 if (sa3
->sa_atimetype
== NFSV3SATTRTIME_TOCLIENT
) {
249 sa3
->sa_atime
.nfsv3_sec
= EXTRACT_32BITS(dp
);
251 sa3
->sa_atime
.nfsv3_nsec
= EXTRACT_32BITS(dp
);
256 sa3
->sa_mtimetype
= EXTRACT_32BITS(dp
);
258 if (sa3
->sa_mtimetype
== NFSV3SATTRTIME_TOCLIENT
) {
260 sa3
->sa_mtime
.nfsv3_sec
= EXTRACT_32BITS(dp
);
262 sa3
->sa_mtime
.nfsv3_nsec
= EXTRACT_32BITS(dp
);
271 static int nfserr
; /* true if we error rather than trunc */
274 print_sattr3(const struct nfsv3_sattr
*sa3
, int verbose
)
277 printf(" mode %o", sa3
->sa_mode
);
279 printf(" uid %u", sa3
->sa_uid
);
281 printf(" gid %u", sa3
->sa_gid
);
283 if (sa3
->sa_atimetype
== NFSV3SATTRTIME_TOCLIENT
)
284 printf(" atime %u.%06u", sa3
->sa_atime
.nfsv3_sec
,
285 sa3
->sa_atime
.nfsv3_nsec
);
286 if (sa3
->sa_mtimetype
== NFSV3SATTRTIME_TOCLIENT
)
287 printf(" mtime %u.%06u", sa3
->sa_mtime
.nfsv3_sec
,
288 sa3
->sa_mtime
.nfsv3_nsec
);
293 nfsreply_print(register const u_char
*bp
, u_int length
,
294 register const u_char
*bp2
)
296 register const struct sunrpc_msg
*rp
;
297 u_int32_t proc
, vers
, reply_stat
;
298 char srcid
[20], dstid
[20]; /*fits 32bit*/
299 enum sunrpc_reject_stat rstat
;
302 enum sunrpc_auth_stat rwhy
;
304 nfserr
= 0; /* assume no error */
305 rp
= (const struct sunrpc_msg
*)bp
;
308 strlcpy(srcid
, "nfs", sizeof(srcid
));
309 snprintf(dstid
, sizeof(dstid
), "%u",
310 EXTRACT_32BITS(&rp
->rm_xid
));
312 snprintf(srcid
, sizeof(srcid
), "%u", NFS_PORT
);
313 snprintf(dstid
, sizeof(dstid
), "%u",
314 EXTRACT_32BITS(&rp
->rm_xid
));
316 print_nfsaddr(bp2
, srcid
, dstid
);
317 reply_stat
= EXTRACT_32BITS(&rp
->rm_reply
.rp_stat
);
318 switch (reply_stat
) {
320 case SUNRPC_MSG_ACCEPTED
:
321 (void)printf("reply ok %u", length
);
322 if (xid_map_find(rp
, bp2
, &proc
, &vers
) >= 0)
323 interp_reply(rp
, proc
, vers
, length
);
326 case SUNRPC_MSG_DENIED
:
327 (void)printf("reply ERR %u: ", length
);
328 rstat
= EXTRACT_32BITS(&rp
->rm_reply
.rp_reject
.rj_stat
);
331 case SUNRPC_RPC_MISMATCH
:
332 rlow
= EXTRACT_32BITS(&rp
->rm_reply
.rp_reject
.rj_vers
.low
);
333 rhigh
= EXTRACT_32BITS(&rp
->rm_reply
.rp_reject
.rj_vers
.high
);
334 (void)printf("RPC Version mismatch (%u-%u)",
338 case SUNRPC_AUTH_ERROR
:
339 rwhy
= EXTRACT_32BITS(&rp
->rm_reply
.rp_reject
.rj_why
);
340 (void)printf("Auth ");
347 case SUNRPC_AUTH_BADCRED
:
348 (void)printf("Bogus Credentials (seal broken)");
351 case SUNRPC_AUTH_REJECTEDCRED
:
352 (void)printf("Rejected Credentials (client should begin new session)");
355 case SUNRPC_AUTH_BADVERF
:
356 (void)printf("Bogus Verifier (seal broken)");
359 case SUNRPC_AUTH_REJECTEDVERF
:
360 (void)printf("Verifier expired or was replayed");
363 case SUNRPC_AUTH_TOOWEAK
:
364 (void)printf("Credentials are too weak");
367 case SUNRPC_AUTH_INVALIDRESP
:
368 (void)printf("Bogus response verifier");
371 case SUNRPC_AUTH_FAILED
:
372 (void)printf("Unknown failure");
376 (void)printf("Invalid failure code %u",
383 (void)printf("Unknown reason for rejecting rpc message %u",
384 (unsigned int)rstat
);
390 (void)printf("reply Unknown rpc response code=%u %u",
397 * Return a pointer to the first file handle in the packet.
398 * If the packet was truncated, return 0.
400 static const u_int32_t
*
401 parsereq(register const struct sunrpc_msg
*rp
, register u_int length
)
403 register const u_int32_t
*dp
;
407 * find the start of the req data (if we captured it)
409 dp
= (u_int32_t
*)&rp
->rm_call
.cb_cred
;
411 len
= EXTRACT_32BITS(&dp
[1]);
413 dp
+= (len
+ (2 * sizeof(*dp
) + 3)) / sizeof(*dp
);
415 len
= EXTRACT_32BITS(&dp
[1]);
417 dp
+= (len
+ (2 * sizeof(*dp
) + 3)) / sizeof(*dp
);
427 * Print out an NFS file handle and return a pointer to following word.
428 * If packet was truncated, return 0.
430 static const u_int32_t
*
431 parsefh(register const u_int32_t
*dp
, int v3
)
437 len
= EXTRACT_32BITS(dp
) / 4;
442 if (TTEST2(*dp
, len
* sizeof(*dp
))) {
443 nfs_printfh(dp
, len
);
451 * Print out a file name and return pointer to 32-bit word past it.
452 * If packet was truncated, return 0.
454 static const u_int32_t
*
455 parsefn(register const u_int32_t
*dp
)
457 register u_int32_t len
;
458 register const u_char
*cp
;
460 /* Bail if we don't have the string length */
463 /* Fetch string length; convert to host order */
467 TCHECK2(*dp
, ((len
+ 3) & ~3));
470 /* Update 32-bit pointer (NFS filenames padded to 32-bit boundaries) */
471 dp
+= ((len
+ 3) & ~3) / sizeof(*dp
);
473 if (fn_printn(cp
, len
, snapend
)) {
485 * Print out file handle and file name.
486 * Return pointer to 32-bit word past file name.
487 * If packet was truncated (or there was some other error), return 0.
489 static const u_int32_t
*
490 parsefhn(register const u_int32_t
*dp
, int v3
)
492 dp
= parsefh(dp
, v3
);
496 return (parsefn(dp
));
500 nfsreq_print(register const u_char
*bp
, u_int length
,
501 register const u_char
*bp2
)
503 register const struct sunrpc_msg
*rp
;
504 register const u_int32_t
*dp
;
508 struct nfsv3_sattr sa3
;
509 char srcid
[20], dstid
[20]; /*fits 32bit*/
511 nfserr
= 0; /* assume no error */
512 rp
= (const struct sunrpc_msg
*)bp
;
514 snprintf(srcid
, sizeof(srcid
), "%u",
515 EXTRACT_32BITS(&rp
->rm_xid
));
516 strlcpy(dstid
, "nfs", sizeof(dstid
));
518 snprintf(srcid
, sizeof(srcid
), "%u",
519 EXTRACT_32BITS(&rp
->rm_xid
));
520 snprintf(dstid
, sizeof(dstid
), "%u", NFS_PORT
);
522 print_nfsaddr(bp2
, srcid
, dstid
);
523 (void)printf("%d", length
);
525 xid_map_enter(rp
, bp2
); /* record proc number for later on */
527 v3
= (EXTRACT_32BITS(&rp
->rm_call
.cb_vers
) == NFS_VER3
);
528 proc
= EXTRACT_32BITS(&rp
->rm_call
.cb_proc
);
530 if (!v3
&& proc
< NFS_NPROCS
)
531 proc
= nfsv3_procid
[proc
];
541 case NFSPROC_GETATTR
:
543 if ((dp
= parsereq(rp
, length
)) != NULL
&&
544 parsefh(dp
, v3
) != NULL
)
548 case NFSPROC_SETATTR
:
550 if ((dp
= parsereq(rp
, length
)) != NULL
&&
551 parsefh(dp
, v3
) != NULL
)
557 if ((dp
= parsereq(rp
, length
)) != NULL
&&
558 parsefhn(dp
, v3
) != NULL
)
564 if ((dp
= parsereq(rp
, length
)) != NULL
&&
565 (dp
= parsefh(dp
, v3
)) != NULL
) {
567 printf(" %04x", EXTRACT_32BITS(&dp
[0]));
572 case NFSPROC_READLINK
:
574 if ((dp
= parsereq(rp
, length
)) != NULL
&&
575 parsefh(dp
, v3
) != NULL
)
581 if ((dp
= parsereq(rp
, length
)) != NULL
&&
582 (dp
= parsefh(dp
, v3
)) != NULL
) {
585 printf(" %u bytes @ %" PRIu64
,
586 EXTRACT_32BITS(&dp
[2]),
587 EXTRACT_64BITS(&dp
[0]));
590 printf(" %u bytes @ %u",
591 EXTRACT_32BITS(&dp
[1]),
592 EXTRACT_32BITS(&dp
[0]));
600 if ((dp
= parsereq(rp
, length
)) != NULL
&&
601 (dp
= parsefh(dp
, v3
)) != NULL
) {
604 printf(" %u (%u) bytes @ %" PRIu64
,
605 EXTRACT_32BITS(&dp
[4]),
606 EXTRACT_32BITS(&dp
[2]),
607 EXTRACT_64BITS(&dp
[0]));
612 tok2str(nfsv3_writemodes
,
613 NULL
, EXTRACT_32BITS(dp
)));
617 printf(" %u (%u) bytes @ %u (%u)",
618 EXTRACT_32BITS(&dp
[3]),
619 EXTRACT_32BITS(&dp
[2]),
620 EXTRACT_32BITS(&dp
[1]),
621 EXTRACT_32BITS(&dp
[0]));
629 if ((dp
= parsereq(rp
, length
)) != NULL
&&
630 parsefhn(dp
, v3
) != NULL
)
636 if ((dp
= parsereq(rp
, length
)) != 0 && parsefhn(dp
, v3
) != 0)
640 case NFSPROC_SYMLINK
:
642 if ((dp
= parsereq(rp
, length
)) != 0 &&
643 (dp
= parsefhn(dp
, v3
)) != 0) {
644 fputs(" ->", stdout
);
645 if (v3
&& (dp
= parse_sattr3(dp
, &sa3
)) == 0)
647 if (parsefn(dp
) == 0)
650 print_sattr3(&sa3
, vflag
);
657 if ((dp
= parsereq(rp
, length
)) != 0 &&
658 (dp
= parsefhn(dp
, v3
)) != 0) {
660 type
= (nfs_type
)EXTRACT_32BITS(dp
);
662 if ((dp
= parse_sattr3(dp
, &sa3
)) == 0)
664 printf(" %s", tok2str(type2str
, "unk-ft %d", type
));
665 if (vflag
&& (type
== NFCHR
|| type
== NFBLK
)) {
668 EXTRACT_32BITS(&dp
[0]),
669 EXTRACT_32BITS(&dp
[1]));
673 print_sattr3(&sa3
, vflag
);
680 if ((dp
= parsereq(rp
, length
)) != NULL
&&
681 parsefhn(dp
, v3
) != NULL
)
687 if ((dp
= parsereq(rp
, length
)) != NULL
&&
688 parsefhn(dp
, v3
) != NULL
)
694 if ((dp
= parsereq(rp
, length
)) != NULL
&&
695 (dp
= parsefhn(dp
, v3
)) != NULL
) {
696 fputs(" ->", stdout
);
697 if (parsefhn(dp
, v3
) != NULL
)
704 if ((dp
= parsereq(rp
, length
)) != NULL
&&
705 (dp
= parsefh(dp
, v3
)) != NULL
) {
706 fputs(" ->", stdout
);
707 if (parsefhn(dp
, v3
) != NULL
)
712 case NFSPROC_READDIR
:
714 if ((dp
= parsereq(rp
, length
)) != NULL
&&
715 (dp
= parsefh(dp
, v3
)) != NULL
) {
719 * We shouldn't really try to interpret the
720 * offset cookie here.
722 printf(" %u bytes @ %" PRId64
,
723 EXTRACT_32BITS(&dp
[4]),
724 EXTRACT_64BITS(&dp
[0]));
726 printf(" verf %08x%08x", dp
[2],
731 * Print the offset as signed, since -1 is
732 * common, but offsets > 2^31 aren't.
734 printf(" %u bytes @ %d",
735 EXTRACT_32BITS(&dp
[1]),
736 EXTRACT_32BITS(&dp
[0]));
742 case NFSPROC_READDIRPLUS
:
743 printf(" readdirplus");
744 if ((dp
= parsereq(rp
, length
)) != NULL
&&
745 (dp
= parsefh(dp
, v3
)) != NULL
) {
748 * We don't try to interpret the offset
751 printf(" %u bytes @ %" PRId64
,
752 EXTRACT_32BITS(&dp
[4]),
753 EXTRACT_64BITS(&dp
[0]));
756 printf(" max %u verf %08x%08x",
757 EXTRACT_32BITS(&dp
[5]), dp
[2], dp
[3]);
765 if ((dp
= parsereq(rp
, length
)) != NULL
&&
766 parsefh(dp
, v3
) != NULL
)
772 if ((dp
= parsereq(rp
, length
)) != NULL
&&
773 parsefh(dp
, v3
) != NULL
)
777 case NFSPROC_PATHCONF
:
779 if ((dp
= parsereq(rp
, length
)) != NULL
&&
780 parsefh(dp
, v3
) != NULL
)
786 if ((dp
= parsereq(rp
, length
)) != NULL
&&
787 (dp
= parsefh(dp
, v3
)) != NULL
) {
789 printf(" %u bytes @ %" PRIu64
,
790 EXTRACT_32BITS(&dp
[2]),
791 EXTRACT_64BITS(&dp
[0]));
797 printf(" proc-%u", EXTRACT_32BITS(&rp
->rm_call
.cb_proc
));
803 fputs(" [|nfs]", stdout
);
807 * Print out an NFS file handle.
808 * We assume packet was not truncated before the end of the
809 * file handle pointed to by dp.
811 * Note: new version (using portable file-handle parser) doesn't produce
812 * generation number. It probably could be made to do that, with some
813 * additional hacking on the parser code.
816 nfs_printfh(register const u_int32_t
*dp
, const u_int len
)
820 const char *sfsname
= NULL
;
825 char const *sep
= "";
828 for (i
=0; i
<len
; i
++) {
829 (void)printf("%s%x", sep
, dp
[i
]);
836 Parse_fh((const u_char
*)dp
, len
, &fsid
, &ino
, NULL
, &sfsname
, 0);
839 /* file system ID is ASCII, not numeric, for this server OS */
840 static char temp
[NFSX_V3FHMAX
+1];
842 /* Make sure string is null-terminated */
843 strncpy(temp
, sfsname
, NFSX_V3FHMAX
);
844 temp
[sizeof(temp
) - 1] = '\0';
845 /* Remove trailing spaces */
846 spacep
= strchr(temp
, ' ');
850 (void)printf(" fh %s/", temp
);
852 (void)printf(" fh %d,%d/",
853 fsid
.Fsid_dev
.Major
, fsid
.Fsid_dev
.Minor
);
856 if(fsid
.Fsid_dev
.Minor
== 257)
857 /* Print the undecoded handle */
858 (void)printf("%s", fsid
.Opaque_Handle
);
860 (void)printf("%ld", (long) ino
);
864 * Maintain a small cache of recent client.XID.server/proc pairs, to allow
865 * us to match up replies with requests and thus to know how to parse
869 struct xid_map_entry
{
870 u_int32_t xid
; /* transaction ID (net order) */
871 int ipver
; /* IP version (4 or 6) */
873 struct in6_addr client
; /* client IP address (net order) */
874 struct in6_addr server
; /* server IP address (net order) */
876 struct in_addr client
; /* client IP address (net order) */
877 struct in_addr server
; /* server IP address (net order) */
879 u_int32_t proc
; /* call proc number (host order) */
880 u_int32_t vers
; /* program version (host order) */
884 * Map entries are kept in an array that we manage as a ring;
885 * new entries are always added at the tail of the ring. Initially,
886 * all the entries are zero and hence don't match anything.
889 #define XIDMAPSIZE 64
891 struct xid_map_entry xid_map
[XIDMAPSIZE
];
893 int xid_map_next
= 0;
894 int xid_map_hint
= 0;
897 xid_map_enter(const struct sunrpc_msg
*rp
, const u_char
*bp
)
899 struct ip
*ip
= NULL
;
901 struct ip6_hdr
*ip6
= NULL
;
903 struct xid_map_entry
*xmep
;
905 switch (IP_V((struct ip
*)bp
)) {
907 ip
= (struct ip
*)bp
;
911 ip6
= (struct ip6_hdr
*)bp
;
918 xmep
= &xid_map
[xid_map_next
];
920 if (++xid_map_next
>= XIDMAPSIZE
)
923 xmep
->xid
= rp
->rm_xid
;
926 memcpy(&xmep
->client
, &ip
->ip_src
, sizeof(ip
->ip_src
));
927 memcpy(&xmep
->server
, &ip
->ip_dst
, sizeof(ip
->ip_dst
));
932 memcpy(&xmep
->client
, &ip6
->ip6_src
, sizeof(ip6
->ip6_src
));
933 memcpy(&xmep
->server
, &ip6
->ip6_dst
, sizeof(ip6
->ip6_dst
));
936 xmep
->proc
= EXTRACT_32BITS(&rp
->rm_call
.cb_proc
);
937 xmep
->vers
= EXTRACT_32BITS(&rp
->rm_call
.cb_vers
);
941 * Returns 0 and puts NFSPROC_xxx in proc return and
942 * version in vers return, or returns -1 on failure
945 xid_map_find(const struct sunrpc_msg
*rp
, const u_char
*bp
, u_int32_t
*proc
,
949 struct xid_map_entry
*xmep
;
950 u_int32_t xid
= rp
->rm_xid
;
951 struct ip
*ip
= (struct ip
*)bp
;
953 struct ip6_hdr
*ip6
= (struct ip6_hdr
*)bp
;
957 /* Start searching from where we last left off */
962 if (xmep
->ipver
!= IP_V(ip
) || xmep
->xid
!= xid
)
964 switch (xmep
->ipver
) {
966 if (memcmp(&ip
->ip_src
, &xmep
->server
,
967 sizeof(ip
->ip_src
)) != 0 ||
968 memcmp(&ip
->ip_dst
, &xmep
->client
,
969 sizeof(ip
->ip_dst
)) != 0) {
975 if (memcmp(&ip6
->ip6_src
, &xmep
->server
,
976 sizeof(ip6
->ip6_src
)) != 0 ||
977 memcmp(&ip6
->ip6_dst
, &xmep
->client
,
978 sizeof(ip6
->ip6_dst
)) != 0) {
995 if (++i
>= XIDMAPSIZE
)
997 } while (i
!= xid_map_hint
);
1004 * Routines for parsing reply packets
1008 * Return a pointer to the beginning of the actual results.
1009 * If the packet was truncated, return 0.
1011 static const u_int32_t
*
1012 parserep(register const struct sunrpc_msg
*rp
, register u_int length
)
1014 register const u_int32_t
*dp
;
1016 enum sunrpc_accept_stat astat
;
1020 * Here we find the address of the ar_verf credentials.
1021 * Originally, this calculation was
1022 * dp = (u_int32_t *)&rp->rm_reply.rp_acpt.ar_verf
1023 * On the wire, the rp_acpt field starts immediately after
1024 * the (32 bit) rp_stat field. However, rp_acpt (which is a
1025 * "struct accepted_reply") contains a "struct opaque_auth",
1026 * whose internal representation contains a pointer, so on a
1027 * 64-bit machine the compiler inserts 32 bits of padding
1028 * before rp->rm_reply.rp_acpt.ar_verf. So, we cannot use
1029 * the internal representation to parse the on-the-wire
1030 * representation. Instead, we skip past the rp_stat field,
1031 * which is an "enum" and so occupies one 32-bit word.
1033 dp
= ((const u_int32_t
*)&rp
->rm_reply
) + 1;
1035 len
= EXTRACT_32BITS(&dp
[1]);
1039 * skip past the ar_verf credentials.
1041 dp
+= (len
+ (2*sizeof(u_int32_t
) + 3)) / sizeof(u_int32_t
);
1045 * now we can check the ar_stat field
1047 astat
= (enum sunrpc_accept_stat
) EXTRACT_32BITS(dp
);
1050 case SUNRPC_SUCCESS
:
1053 case SUNRPC_PROG_UNAVAIL
:
1054 printf(" PROG_UNAVAIL");
1055 nfserr
= 1; /* suppress trunc string */
1058 case SUNRPC_PROG_MISMATCH
:
1059 printf(" PROG_MISMATCH");
1060 nfserr
= 1; /* suppress trunc string */
1063 case SUNRPC_PROC_UNAVAIL
:
1064 printf(" PROC_UNAVAIL");
1065 nfserr
= 1; /* suppress trunc string */
1068 case SUNRPC_GARBAGE_ARGS
:
1069 printf(" GARBAGE_ARGS");
1070 nfserr
= 1; /* suppress trunc string */
1073 case SUNRPC_SYSTEM_ERR
:
1074 printf(" SYSTEM_ERR");
1075 nfserr
= 1; /* suppress trunc string */
1079 printf(" ar_stat %d", astat
);
1080 nfserr
= 1; /* suppress trunc string */
1083 /* successful return */
1084 TCHECK2(*dp
, sizeof(astat
));
1085 return ((u_int32_t
*) (sizeof(astat
) + ((char *)dp
)));
1090 static const u_int32_t
*
1091 parsestatus(const u_int32_t
*dp
, int *er
)
1097 errnum
= EXTRACT_32BITS(&dp
[0]);
1102 printf(" ERROR: %s",
1103 tok2str(status2str
, "unk %d", errnum
));
1111 static const u_int32_t
*
1112 parsefattr(const u_int32_t
*dp
, int verbose
, int v3
)
1114 const struct nfs_fattr
*fap
;
1116 fap
= (const struct nfs_fattr
*)dp
;
1117 TCHECK(fap
->fa_gid
);
1119 printf(" %s %o ids %d/%d",
1120 tok2str(type2str
, "unk-ft %d ",
1121 EXTRACT_32BITS(&fap
->fa_type
)),
1122 EXTRACT_32BITS(&fap
->fa_mode
),
1123 EXTRACT_32BITS(&fap
->fa_uid
),
1124 EXTRACT_32BITS(&fap
->fa_gid
));
1126 TCHECK(fap
->fa3_size
);
1127 printf(" sz %" PRIu64
,
1128 EXTRACT_64BITS((u_int32_t
*)&fap
->fa3_size
));
1130 TCHECK(fap
->fa2_size
);
1131 printf(" sz %d", EXTRACT_32BITS(&fap
->fa2_size
));
1134 /* print lots more stuff */
1137 TCHECK(fap
->fa3_ctime
);
1138 printf(" nlink %d rdev %d/%d",
1139 EXTRACT_32BITS(&fap
->fa_nlink
),
1140 EXTRACT_32BITS(&fap
->fa3_rdev
.specdata1
),
1141 EXTRACT_32BITS(&fap
->fa3_rdev
.specdata2
));
1142 printf(" fsid %" PRIx64
,
1143 EXTRACT_64BITS((u_int32_t
*)&fap
->fa3_fsid
));
1144 printf(" fileid %" PRIx64
,
1145 EXTRACT_64BITS((u_int32_t
*)&fap
->fa3_fileid
));
1146 printf(" a/m/ctime %u.%06u",
1147 EXTRACT_32BITS(&fap
->fa3_atime
.nfsv3_sec
),
1148 EXTRACT_32BITS(&fap
->fa3_atime
.nfsv3_nsec
));
1150 EXTRACT_32BITS(&fap
->fa3_mtime
.nfsv3_sec
),
1151 EXTRACT_32BITS(&fap
->fa3_mtime
.nfsv3_nsec
));
1153 EXTRACT_32BITS(&fap
->fa3_ctime
.nfsv3_sec
),
1154 EXTRACT_32BITS(&fap
->fa3_ctime
.nfsv3_nsec
));
1156 TCHECK(fap
->fa2_ctime
);
1157 printf(" nlink %d rdev %x fsid %x nodeid %x a/m/ctime",
1158 EXTRACT_32BITS(&fap
->fa_nlink
),
1159 EXTRACT_32BITS(&fap
->fa2_rdev
),
1160 EXTRACT_32BITS(&fap
->fa2_fsid
),
1161 EXTRACT_32BITS(&fap
->fa2_fileid
));
1163 EXTRACT_32BITS(&fap
->fa2_atime
.nfsv2_sec
),
1164 EXTRACT_32BITS(&fap
->fa2_atime
.nfsv2_usec
));
1166 EXTRACT_32BITS(&fap
->fa2_mtime
.nfsv2_sec
),
1167 EXTRACT_32BITS(&fap
->fa2_mtime
.nfsv2_usec
));
1169 EXTRACT_32BITS(&fap
->fa2_ctime
.nfsv2_sec
),
1170 EXTRACT_32BITS(&fap
->fa2_ctime
.nfsv2_usec
));
1173 return ((const u_int32_t
*)((unsigned char *)dp
+
1174 (v3
? NFSX_V3FATTR
: NFSX_V2FATTR
)));
1180 parseattrstat(const u_int32_t
*dp
, int verbose
, int v3
)
1184 dp
= parsestatus(dp
, &er
);
1190 return (parsefattr(dp
, verbose
, v3
) != NULL
);
1194 parsediropres(const u_int32_t
*dp
)
1198 if (!(dp
= parsestatus(dp
, &er
)))
1203 dp
= parsefh(dp
, 0);
1207 return (parsefattr(dp
, vflag
, 0) != NULL
);
1211 parselinkres(const u_int32_t
*dp
, int v3
)
1215 dp
= parsestatus(dp
, &er
);
1220 if (v3
&& !(dp
= parse_post_op_attr(dp
, vflag
)))
1223 return (parsefn(dp
) != NULL
);
1227 parsestatfs(const u_int32_t
*dp
, int v3
)
1229 const struct nfs_statfs
*sfsp
;
1232 dp
= parsestatus(dp
, &er
);
1244 if (!(dp
= parse_post_op_attr(dp
, vflag
)))
1248 TCHECK2(*dp
, (v3
? NFSX_V3STATFS
: NFSX_V2STATFS
));
1250 sfsp
= (const struct nfs_statfs
*)dp
;
1253 printf(" tbytes %" PRIu64
" fbytes %" PRIu64
" abytes %" PRIu64
,
1254 EXTRACT_64BITS((u_int32_t
*)&sfsp
->sf_tbytes
),
1255 EXTRACT_64BITS((u_int32_t
*)&sfsp
->sf_fbytes
),
1256 EXTRACT_64BITS((u_int32_t
*)&sfsp
->sf_abytes
));
1258 printf(" tfiles %" PRIu64
" ffiles %" PRIu64
" afiles %" PRIu64
" invar %u",
1259 EXTRACT_64BITS((u_int32_t
*)&sfsp
->sf_tfiles
),
1260 EXTRACT_64BITS((u_int32_t
*)&sfsp
->sf_ffiles
),
1261 EXTRACT_64BITS((u_int32_t
*)&sfsp
->sf_afiles
),
1262 EXTRACT_32BITS(&sfsp
->sf_invarsec
));
1265 printf(" tsize %d bsize %d blocks %d bfree %d bavail %d",
1266 EXTRACT_32BITS(&sfsp
->sf_tsize
),
1267 EXTRACT_32BITS(&sfsp
->sf_bsize
),
1268 EXTRACT_32BITS(&sfsp
->sf_blocks
),
1269 EXTRACT_32BITS(&sfsp
->sf_bfree
),
1270 EXTRACT_32BITS(&sfsp
->sf_bavail
));
1279 parserddires(const u_int32_t
*dp
)
1283 dp
= parsestatus(dp
, &er
);
1292 printf(" offset %x size %d ",
1293 EXTRACT_32BITS(&dp
[0]), EXTRACT_32BITS(&dp
[1]));
1302 static const u_int32_t
*
1303 parse_wcc_attr(const u_int32_t
*dp
)
1305 printf(" sz %" PRIu64
, EXTRACT_64BITS(&dp
[0]));
1306 printf(" mtime %u.%06u ctime %u.%06u",
1307 EXTRACT_32BITS(&dp
[2]), EXTRACT_32BITS(&dp
[3]),
1308 EXTRACT_32BITS(&dp
[4]), EXTRACT_32BITS(&dp
[5]));
1313 * Pre operation attributes. Print only if vflag > 1.
1315 static const u_int32_t
*
1316 parse_pre_op_attr(const u_int32_t
*dp
, int verbose
)
1319 if (!EXTRACT_32BITS(&dp
[0]))
1324 return parse_wcc_attr(dp
);
1326 /* If not verbose enough, just skip over wcc_attr */
1334 * Post operation attributes are printed if vflag >= 1
1336 static const u_int32_t
*
1337 parse_post_op_attr(const u_int32_t
*dp
, int verbose
)
1340 if (!EXTRACT_32BITS(&dp
[0]))
1344 return parsefattr(dp
, verbose
, 1);
1346 return (dp
+ (NFSX_V3FATTR
/ sizeof (u_int32_t
)));
1351 static const u_int32_t
*
1352 parse_wcc_data(const u_int32_t
*dp
, int verbose
)
1356 if (!(dp
= parse_pre_op_attr(dp
, verbose
)))
1361 return parse_post_op_attr(dp
, verbose
);
1364 static const u_int32_t
*
1365 parsecreateopres(const u_int32_t
*dp
, int verbose
)
1369 if (!(dp
= parsestatus(dp
, &er
)))
1372 dp
= parse_wcc_data(dp
, verbose
);
1375 if (!EXTRACT_32BITS(&dp
[0]))
1378 if (!(dp
= parsefh(dp
, 1)))
1381 if (!(dp
= parse_post_op_attr(dp
, verbose
)))
1384 printf(" dir attr:");
1385 dp
= parse_wcc_data(dp
, verbose
);
1395 parsewccres(const u_int32_t
*dp
, int verbose
)
1399 if (!(dp
= parsestatus(dp
, &er
)))
1401 return parse_wcc_data(dp
, verbose
) != 0;
1404 static const u_int32_t
*
1405 parsev3rddirres(const u_int32_t
*dp
, int verbose
)
1409 if (!(dp
= parsestatus(dp
, &er
)))
1413 if (!(dp
= parse_post_op_attr(dp
, verbose
)))
1419 printf(" verf %08x%08x", dp
[0], dp
[1]);
1428 parsefsinfo(const u_int32_t
*dp
)
1430 struct nfsv3_fsinfo
*sfp
;
1433 if (!(dp
= parsestatus(dp
, &er
)))
1437 if (!(dp
= parse_post_op_attr(dp
, vflag
)))
1442 sfp
= (struct nfsv3_fsinfo
*)dp
;
1444 printf(" rtmax %u rtpref %u wtmax %u wtpref %u dtpref %u",
1445 EXTRACT_32BITS(&sfp
->fs_rtmax
),
1446 EXTRACT_32BITS(&sfp
->fs_rtpref
),
1447 EXTRACT_32BITS(&sfp
->fs_wtmax
),
1448 EXTRACT_32BITS(&sfp
->fs_wtpref
),
1449 EXTRACT_32BITS(&sfp
->fs_dtpref
));
1451 printf(" rtmult %u wtmult %u maxfsz %" PRIu64
,
1452 EXTRACT_32BITS(&sfp
->fs_rtmult
),
1453 EXTRACT_32BITS(&sfp
->fs_wtmult
),
1454 EXTRACT_64BITS((u_int32_t
*)&sfp
->fs_maxfilesize
));
1455 printf(" delta %u.%06u ",
1456 EXTRACT_32BITS(&sfp
->fs_timedelta
.nfsv3_sec
),
1457 EXTRACT_32BITS(&sfp
->fs_timedelta
.nfsv3_nsec
));
1465 parsepathconf(const u_int32_t
*dp
)
1468 struct nfsv3_pathconf
*spp
;
1470 if (!(dp
= parsestatus(dp
, &er
)))
1474 if (!(dp
= parse_post_op_attr(dp
, vflag
)))
1479 spp
= (struct nfsv3_pathconf
*)dp
;
1482 printf(" linkmax %u namemax %u %s %s %s %s",
1483 EXTRACT_32BITS(&spp
->pc_linkmax
),
1484 EXTRACT_32BITS(&spp
->pc_namemax
),
1485 EXTRACT_32BITS(&spp
->pc_notrunc
) ? "notrunc" : "",
1486 EXTRACT_32BITS(&spp
->pc_chownrestricted
) ? "chownres" : "",
1487 EXTRACT_32BITS(&spp
->pc_caseinsensitive
) ? "igncase" : "",
1488 EXTRACT_32BITS(&spp
->pc_casepreserving
) ? "keepcase" : "");
1495 interp_reply(const struct sunrpc_msg
*rp
, u_int32_t proc
, u_int32_t vers
, int length
)
1497 register const u_int32_t
*dp
;
1501 v3
= (vers
== NFS_VER3
);
1503 if (!v3
&& proc
< NFS_NPROCS
)
1504 proc
= nfsv3_procid
[proc
];
1516 case NFSPROC_GETATTR
:
1518 dp
= parserep(rp
, length
);
1519 if (dp
!= NULL
&& parseattrstat(dp
, !qflag
, v3
) != 0)
1523 case NFSPROC_SETATTR
:
1525 if (!(dp
= parserep(rp
, length
)))
1528 if (parsewccres(dp
, vflag
))
1531 if (parseattrstat(dp
, !qflag
, 0) != 0)
1536 case NFSPROC_LOOKUP
:
1538 if (!(dp
= parserep(rp
, length
)))
1541 if (!(dp
= parsestatus(dp
, &er
)))
1545 printf(" post dattr:");
1546 dp
= parse_post_op_attr(dp
, vflag
);
1549 if (!(dp
= parsefh(dp
, v3
)))
1551 if ((dp
= parse_post_op_attr(dp
, vflag
)) &&
1553 printf(" post dattr:");
1554 dp
= parse_post_op_attr(dp
, vflag
);
1560 if (parsediropres(dp
) != 0)
1565 case NFSPROC_ACCESS
:
1567 if (!(dp
= parserep(rp
, length
)))
1569 if (!(dp
= parsestatus(dp
, &er
)))
1573 if (!(dp
= parse_post_op_attr(dp
, vflag
)))
1576 printf(" c %04x", EXTRACT_32BITS(&dp
[0]));
1579 case NFSPROC_READLINK
:
1580 printf(" readlink");
1581 dp
= parserep(rp
, length
);
1582 if (dp
!= NULL
&& parselinkres(dp
, v3
) != 0)
1588 if (!(dp
= parserep(rp
, length
)))
1591 if (!(dp
= parsestatus(dp
, &er
)))
1593 if (!(dp
= parse_post_op_attr(dp
, vflag
)))
1599 printf(" %u bytes", EXTRACT_32BITS(&dp
[0]));
1600 if (EXTRACT_32BITS(&dp
[1]))
1605 if (parseattrstat(dp
, vflag
, 0) != 0)
1612 if (!(dp
= parserep(rp
, length
)))
1615 if (!(dp
= parsestatus(dp
, &er
)))
1617 if (!(dp
= parse_wcc_data(dp
, vflag
)))
1623 printf(" %u bytes", EXTRACT_32BITS(&dp
[0]));
1627 tok2str(nfsv3_writemodes
,
1628 NULL
, EXTRACT_32BITS(&dp
[1])));
1633 if (parseattrstat(dp
, vflag
, v3
) != 0)
1638 case NFSPROC_CREATE
:
1640 if (!(dp
= parserep(rp
, length
)))
1643 if (parsecreateopres(dp
, vflag
) != 0)
1646 if (parsediropres(dp
) != 0)
1653 if (!(dp
= parserep(rp
, length
)))
1656 if (parsecreateopres(dp
, vflag
) != 0)
1659 if (parsediropres(dp
) != 0)
1664 case NFSPROC_SYMLINK
:
1666 if (!(dp
= parserep(rp
, length
)))
1669 if (parsecreateopres(dp
, vflag
) != 0)
1672 if (parsestatus(dp
, &er
) != 0)
1679 if (!(dp
= parserep(rp
, length
)))
1681 if (parsecreateopres(dp
, vflag
) != 0)
1685 case NFSPROC_REMOVE
:
1687 if (!(dp
= parserep(rp
, length
)))
1690 if (parsewccres(dp
, vflag
))
1693 if (parsestatus(dp
, &er
) != 0)
1700 if (!(dp
= parserep(rp
, length
)))
1703 if (parsewccres(dp
, vflag
))
1706 if (parsestatus(dp
, &er
) != 0)
1711 case NFSPROC_RENAME
:
1713 if (!(dp
= parserep(rp
, length
)))
1716 if (!(dp
= parsestatus(dp
, &er
)))
1720 if (!(dp
= parse_wcc_data(dp
, vflag
)))
1723 if (!(dp
= parse_wcc_data(dp
, vflag
)))
1728 if (parsestatus(dp
, &er
) != 0)
1735 if (!(dp
= parserep(rp
, length
)))
1738 if (!(dp
= parsestatus(dp
, &er
)))
1741 printf(" file POST:");
1742 if (!(dp
= parse_post_op_attr(dp
, vflag
)))
1745 if (!(dp
= parse_wcc_data(dp
, vflag
)))
1750 if (parsestatus(dp
, &er
) != 0)
1755 case NFSPROC_READDIR
:
1757 if (!(dp
= parserep(rp
, length
)))
1760 if (parsev3rddirres(dp
, vflag
))
1763 if (parserddires(dp
) != 0)
1768 case NFSPROC_READDIRPLUS
:
1769 printf(" readdirplus");
1770 if (!(dp
= parserep(rp
, length
)))
1772 if (parsev3rddirres(dp
, vflag
))
1776 case NFSPROC_FSSTAT
:
1778 dp
= parserep(rp
, length
);
1779 if (dp
!= NULL
&& parsestatfs(dp
, v3
) != 0)
1783 case NFSPROC_FSINFO
:
1785 dp
= parserep(rp
, length
);
1786 if (dp
!= NULL
&& parsefsinfo(dp
) != 0)
1790 case NFSPROC_PATHCONF
:
1791 printf(" pathconf");
1792 dp
= parserep(rp
, length
);
1793 if (dp
!= NULL
&& parsepathconf(dp
) != 0)
1797 case NFSPROC_COMMIT
:
1799 dp
= parserep(rp
, length
);
1800 if (dp
!= NULL
&& parsewccres(dp
, vflag
) != 0)
1805 printf(" proc-%u", proc
);
1810 fputs(" [|nfs]", stdout
);