]> The Tcpdump Group git mirrors - tcpdump/blob - print-nfs.c
Get rid of unneeded includes of <netinet/in_systm.h> and <netinet/ip.h>.
[tcpdump] / print-nfs.c
1 /*
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
16 * written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 */
21
22 #ifndef lint
23 static const char rcsid[] =
24 "@(#) $Header: /tcpdump/master/tcpdump/print-nfs.c,v 1.93 2002-06-01 23:50:32 guy Exp $ (LBL)";
25 #endif
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <sys/param.h>
32 #include <sys/time.h>
33 #include <sys/socket.h>
34
35 #include <netinet/in.h>
36
37 #include <rpc/rpc.h>
38
39 #include <ctype.h>
40 #include <pcap.h>
41 #include <stdio.h>
42 #include <string.h>
43
44 #include "interface.h"
45 #include "addrtoname.h"
46
47 #include "nfs.h"
48 #include "nfsfh.h"
49
50 #include "ip.h"
51 #ifdef INET6
52 #include "ip6.h"
53 #endif
54
55 static void nfs_printfh(const u_int32_t *, const u_int);
56 static void xid_map_enter(const struct rpc_msg *, const u_char *);
57 static int32_t xid_map_find(const struct rpc_msg *, const u_char *,
58 u_int32_t *, u_int32_t *);
59 static void interp_reply(const struct rpc_msg *, u_int32_t, u_int32_t, int);
60 static const u_int32_t *parse_post_op_attr(const u_int32_t *, int);
61 static void print_sattr3(const struct nfsv3_sattr *sa3, int verbose);
62 static int print_int64(const u_int32_t *dp, int how);
63 static void print_nfsaddr(const u_char *, const char *, const char *);
64
65 /*
66 * Mapping of old NFS Version 2 RPC numbers to generic numbers.
67 */
68 u_int32_t nfsv3_procid[NFS_NPROCS] = {
69 NFSPROC_NULL,
70 NFSPROC_GETATTR,
71 NFSPROC_SETATTR,
72 NFSPROC_NOOP,
73 NFSPROC_LOOKUP,
74 NFSPROC_READLINK,
75 NFSPROC_READ,
76 NFSPROC_NOOP,
77 NFSPROC_WRITE,
78 NFSPROC_CREATE,
79 NFSPROC_REMOVE,
80 NFSPROC_RENAME,
81 NFSPROC_LINK,
82 NFSPROC_SYMLINK,
83 NFSPROC_MKDIR,
84 NFSPROC_RMDIR,
85 NFSPROC_READDIR,
86 NFSPROC_FSSTAT,
87 NFSPROC_NOOP,
88 NFSPROC_NOOP,
89 NFSPROC_NOOP,
90 NFSPROC_NOOP,
91 NFSPROC_NOOP,
92 NFSPROC_NOOP,
93 NFSPROC_NOOP,
94 NFSPROC_NOOP
95 };
96
97 /*
98 * NFS V2 and V3 status values.
99 *
100 * Some of these come from the RFCs for NFS V2 and V3, with the message
101 * strings taken from the FreeBSD C library "errlst.c".
102 *
103 * Others are errors that are not in the RFC but that I suspect some
104 * NFS servers could return; the values are FreeBSD errno values, as
105 * the first NFS server was the SunOS 2.0 one, and until 5.0 SunOS
106 * was primarily BSD-derived.
107 */
108 static struct tok status2str[] = {
109 { 1, "Operation not permitted" }, /* EPERM */
110 { 2, "No such file or directory" }, /* ENOENT */
111 { 5, "Input/output error" }, /* EIO */
112 { 6, "Device not configured" }, /* ENXIO */
113 { 11, "Resource deadlock avoided" }, /* EDEADLK */
114 { 12, "Cannot allocate memory" }, /* ENOMEM */
115 { 13, "Permission denied" }, /* EACCES */
116 { 17, "File exists" }, /* EEXIST */
117 { 18, "Cross-device link" }, /* EXDEV */
118 { 19, "Operation not supported by device" }, /* ENODEV */
119 { 20, "Not a directory" }, /* ENOTDIR */
120 { 21, "Is a directory" }, /* EISDIR */
121 { 22, "Invalid argument" }, /* EINVAL */
122 { 26, "Text file busy" }, /* ETXTBSY */
123 { 27, "File too large" }, /* EFBIG */
124 { 28, "No space left on device" }, /* ENOSPC */
125 { 30, "Read-only file system" }, /* EROFS */
126 { 31, "Too many links" }, /* EMLINK */
127 { 45, "Operation not supported" }, /* EOPNOTSUPP */
128 { 62, "Too many levels of symbolic links" }, /* ELOOP */
129 { 63, "File name too long" }, /* ENAMETOOLONG */
130 { 66, "Directory not empty" }, /* ENOTEMPTY */
131 { 69, "Disc quota exceeded" }, /* EDQUOT */
132 { 70, "Stale NFS file handle" }, /* ESTALE */
133 { 71, "Too many levels of remote in path" }, /* EREMOTE */
134 { 99, "Write cache flushed to disk" }, /* NFSERR_WFLUSH (not used) */
135 { 10001, "Illegal NFS file handle" }, /* NFS3ERR_BADHANDLE */
136 { 10002, "Update synchronization mismatch" }, /* NFS3ERR_NOT_SYNC */
137 { 10003, "READDIR/READDIRPLUS cookie is stale" }, /* NFS3ERR_BAD_COOKIE */
138 { 10004, "Operation not supported" }, /* NFS3ERR_NOTSUPP */
139 { 10005, "Buffer or request is too small" }, /* NFS3ERR_TOOSMALL */
140 { 10006, "Unspecified error on server" }, /* NFS3ERR_SERVERFAULT */
141 { 10007, "Object of that type not supported" }, /* NFS3ERR_BADTYPE */
142 { 10008, "Request couldn't be completed in time" }, /* NFS3ERR_JUKEBOX */
143 { 0, NULL }
144 };
145
146 static struct tok nfsv3_writemodes[] = {
147 { 0, "unstable" },
148 { 1, "datasync" },
149 { 2, "filesync" },
150 { 0, NULL }
151 };
152
153 static struct tok type2str[] = {
154 { NFNON, "NON" },
155 { NFREG, "REG" },
156 { NFDIR, "DIR" },
157 { NFBLK, "BLK" },
158 { NFCHR, "CHR" },
159 { NFLNK, "LNK" },
160 { NFFIFO, "FIFO" },
161 { 0, NULL }
162 };
163
164 /*
165 * Print out a 64-bit integer. This appears to be different on each system,
166 * try to make the best of it. The integer stored as 2 consecutive XDR
167 * encoded 32-bit integers, to which a pointer is passed.
168 *
169 * Assume that a system that has INT64_FORMAT defined, has a 64-bit
170 * integer datatype and can print it.
171 */
172
173 #define UNSIGNED 0
174 #define SIGNED 1
175 #define HEX 2
176
177 static int print_int64(const u_int32_t *dp, int how)
178 {
179 #ifdef INT64_FORMAT
180 u_int64_t res;
181
182 res = ((u_int64_t)ntohl(dp[0]) << 32) | (u_int64_t)ntohl(dp[1]);
183 switch (how) {
184 case SIGNED:
185 printf(INT64_FORMAT, res);
186 break;
187 case UNSIGNED:
188 printf(U_INT64_FORMAT, res);
189 break;
190 case HEX:
191 printf(HEX_INT64_FORMAT, res);
192 break;
193 default:
194 return (0);
195 }
196 #else
197 switch (how) {
198 case SIGNED:
199 case UNSIGNED:
200 case HEX:
201 printf("0x%x%08x", (u_int32_t)ntohl(dp[0]),
202 (u_int32_t)ntohl(dp[1]));
203 break;
204 default:
205 return (0);
206 }
207 #endif
208 return 1;
209 }
210
211 static void
212 print_nfsaddr(const u_char *bp, const char *s, const char *d)
213 {
214 struct ip *ip;
215 #ifdef INET6
216 struct ip6_hdr *ip6;
217 char srcaddr[INET6_ADDRSTRLEN], dstaddr[INET6_ADDRSTRLEN];
218 #else
219 #ifndef INET_ADDRSTRLEN
220 #define INET_ADDRSTRLEN 16
221 #endif
222 char srcaddr[INET_ADDRSTRLEN], dstaddr[INET_ADDRSTRLEN];
223 #endif
224
225 srcaddr[0] = dstaddr[0] = '\0';
226 switch (IP_V((struct ip *)bp)) {
227 case 4:
228 ip = (struct ip *)bp;
229 strlcpy(srcaddr, ipaddr_string(&ip->ip_src), sizeof(srcaddr));
230 strlcpy(dstaddr, ipaddr_string(&ip->ip_dst), sizeof(dstaddr));
231 break;
232 #ifdef INET6
233 case 6:
234 ip6 = (struct ip6_hdr *)bp;
235 strlcpy(srcaddr, ip6addr_string(&ip6->ip6_src),
236 sizeof(srcaddr));
237 strlcpy(dstaddr, ip6addr_string(&ip6->ip6_dst),
238 sizeof(dstaddr));
239 break;
240 #endif
241 default:
242 strlcpy(srcaddr, "?", sizeof(srcaddr));
243 strlcpy(dstaddr, "?", sizeof(dstaddr));
244 break;
245 }
246
247 (void)printf("%s.%s > %s.%s: ", srcaddr, s, dstaddr, d);
248 }
249
250 static const u_int32_t *
251 parse_sattr3(const u_int32_t *dp, struct nfsv3_sattr *sa3)
252 {
253 TCHECK(dp[0]);
254 if ((sa3->sa_modeset = ntohl(*dp++))) {
255 TCHECK(dp[0]);
256 sa3->sa_mode = ntohl(*dp++);
257 }
258
259 TCHECK(dp[0]);
260 if ((sa3->sa_uidset = ntohl(*dp++))) {
261 TCHECK(dp[0]);
262 sa3->sa_uid = ntohl(*dp++);
263 }
264
265 TCHECK(dp[0]);
266 if ((sa3->sa_gidset = ntohl(*dp++))) {
267 TCHECK(dp[0]);
268 sa3->sa_gid = ntohl(*dp++);
269 }
270
271 TCHECK(dp[0]);
272 if ((sa3->sa_sizeset = ntohl(*dp++))) {
273 TCHECK(dp[0]);
274 sa3->sa_size = ntohl(*dp++);
275 }
276
277 TCHECK(dp[0]);
278 if ((sa3->sa_atimetype = ntohl(*dp++)) == NFSV3SATTRTIME_TOCLIENT) {
279 TCHECK(dp[1]);
280 sa3->sa_atime.nfsv3_sec = ntohl(*dp++);
281 sa3->sa_atime.nfsv3_nsec = ntohl(*dp++);
282 }
283
284 TCHECK(dp[0]);
285 if ((sa3->sa_mtimetype = ntohl(*dp++)) == NFSV3SATTRTIME_TOCLIENT) {
286 TCHECK(dp[1]);
287 sa3->sa_mtime.nfsv3_sec = ntohl(*dp++);
288 sa3->sa_mtime.nfsv3_nsec = ntohl(*dp++);
289 }
290
291 return dp;
292 trunc:
293 return NULL;
294 }
295
296 static int nfserr; /* true if we error rather than trunc */
297
298 static void
299 print_sattr3(const struct nfsv3_sattr *sa3, int verbose)
300 {
301 if (sa3->sa_modeset)
302 printf(" mode %o", sa3->sa_mode);
303 if (sa3->sa_uidset)
304 printf(" uid %u", sa3->sa_uid);
305 if (sa3->sa_gidset)
306 printf(" gid %u", sa3->sa_gid);
307 if (verbose > 1) {
308 if (sa3->sa_atimetype == NFSV3SATTRTIME_TOCLIENT)
309 printf(" atime %u.%06u", sa3->sa_atime.nfsv3_sec,
310 sa3->sa_atime.nfsv3_nsec);
311 if (sa3->sa_mtimetype == NFSV3SATTRTIME_TOCLIENT)
312 printf(" mtime %u.%06u", sa3->sa_mtime.nfsv3_sec,
313 sa3->sa_mtime.nfsv3_nsec);
314 }
315 }
316
317 void
318 nfsreply_print(register const u_char *bp, u_int length,
319 register const u_char *bp2)
320 {
321 register const struct rpc_msg *rp;
322 u_int32_t proc, vers;
323 char srcid[20], dstid[20]; /*fits 32bit*/
324
325 nfserr = 0; /* assume no error */
326 rp = (const struct rpc_msg *)bp;
327
328 if (!nflag) {
329 strlcpy(srcid, "nfs", sizeof(srcid));
330 snprintf(dstid, sizeof(dstid), "%u",
331 (u_int32_t)ntohl(rp->rm_xid));
332 } else {
333 snprintf(srcid, sizeof(srcid), "%u", NFS_PORT);
334 snprintf(dstid, sizeof(dstid), "%u",
335 (u_int32_t)ntohl(rp->rm_xid));
336 }
337 print_nfsaddr(bp2, srcid, dstid);
338 (void)printf("reply %s %d",
339 ntohl(rp->rm_reply.rp_stat) == MSG_ACCEPTED?
340 "ok":"ERR",
341 length);
342
343 if (xid_map_find(rp, bp2, &proc, &vers) >= 0)
344 interp_reply(rp, proc, vers, length);
345 }
346
347 /*
348 * Return a pointer to the first file handle in the packet.
349 * If the packet was truncated, return 0.
350 */
351 static const u_int32_t *
352 parsereq(register const struct rpc_msg *rp, register u_int length)
353 {
354 register const u_int32_t *dp;
355 register u_int len;
356
357 /*
358 * find the start of the req data (if we captured it)
359 */
360 dp = (u_int32_t *)&rp->rm_call.cb_cred;
361 TCHECK(dp[1]);
362 len = ntohl(dp[1]);
363 if (len < length) {
364 dp += (len + (2 * sizeof(*dp) + 3)) / sizeof(*dp);
365 TCHECK(dp[1]);
366 len = ntohl(dp[1]);
367 if (len < length) {
368 dp += (len + (2 * sizeof(*dp) + 3)) / sizeof(*dp);
369 TCHECK2(dp[0], 0);
370 return (dp);
371 }
372 }
373 trunc:
374 return (NULL);
375 }
376
377 /*
378 * Print out an NFS file handle and return a pointer to following word.
379 * If packet was truncated, return 0.
380 */
381 static const u_int32_t *
382 parsefh(register const u_int32_t *dp, int v3)
383 {
384 int len;
385
386 if (v3) {
387 TCHECK(dp[0]);
388 len = (int)ntohl(*dp) / 4;
389 dp++;
390 } else
391 len = NFSX_V2FH / 4;
392
393 if (TTEST2(*dp, len * sizeof(*dp))) {
394 nfs_printfh(dp, len);
395 return (dp + len);
396 }
397 trunc:
398 return (NULL);
399 }
400
401 /*
402 * Print out a file name and return pointer to 32-bit word past it.
403 * If packet was truncated, return 0.
404 */
405 static const u_int32_t *
406 parsefn(register const u_int32_t *dp)
407 {
408 register u_int32_t len;
409 register const u_char *cp;
410
411 /* Bail if we don't have the string length */
412 TCHECK(*dp);
413
414 /* Fetch string length; convert to host order */
415 len = *dp++;
416 NTOHL(len);
417
418 TCHECK2(*dp, ((len + 3) & ~3));
419
420 cp = (u_char *)dp;
421 /* Update 32-bit pointer (NFS filenames padded to 32-bit boundaries) */
422 dp += ((len + 3) & ~3) / sizeof(*dp);
423 /* XXX seems like we should be checking the length */
424 putchar('"');
425 (void) fn_printn(cp, len, NULL);
426 putchar('"');
427
428 return (dp);
429 trunc:
430 return NULL;
431 }
432
433 /*
434 * Print out file handle and file name.
435 * Return pointer to 32-bit word past file name.
436 * If packet was truncated (or there was some other error), return 0.
437 */
438 static const u_int32_t *
439 parsefhn(register const u_int32_t *dp, int v3)
440 {
441 dp = parsefh(dp, v3);
442 if (dp == NULL)
443 return (NULL);
444 putchar(' ');
445 return (parsefn(dp));
446 }
447
448 void
449 nfsreq_print(register const u_char *bp, u_int length,
450 register const u_char *bp2)
451 {
452 register const struct rpc_msg *rp;
453 register const u_int32_t *dp;
454 nfs_type type;
455 int v3;
456 u_int32_t proc;
457 struct nfsv3_sattr sa3;
458 char srcid[20], dstid[20]; /*fits 32bit*/
459
460 nfserr = 0; /* assume no error */
461 rp = (const struct rpc_msg *)bp;
462 if (!nflag) {
463 snprintf(srcid, sizeof(srcid), "%u",
464 (u_int32_t)ntohl(rp->rm_xid));
465 strlcpy(dstid, "nfs", sizeof(dstid));
466 } else {
467 snprintf(srcid, sizeof(srcid), "%u",
468 (u_int32_t)ntohl(rp->rm_xid));
469 snprintf(dstid, sizeof(dstid), "%u", NFS_PORT);
470 }
471 print_nfsaddr(bp2, srcid, dstid);
472 (void)printf("%d", length);
473
474 xid_map_enter(rp, bp2); /* record proc number for later on */
475
476 v3 = (ntohl(rp->rm_call.cb_vers) == NFS_VER3);
477 proc = ntohl(rp->rm_call.cb_proc);
478
479 if (!v3 && proc < NFS_NPROCS)
480 proc = nfsv3_procid[proc];
481
482 switch (proc) {
483 case NFSPROC_NOOP:
484 printf(" nop");
485 return;
486 case NFSPROC_NULL:
487 printf(" null");
488 return;
489
490 case NFSPROC_GETATTR:
491 printf(" getattr");
492 if ((dp = parsereq(rp, length)) != NULL &&
493 parsefh(dp, v3) != NULL)
494 return;
495 break;
496
497 case NFSPROC_SETATTR:
498 printf(" setattr");
499 if ((dp = parsereq(rp, length)) != NULL &&
500 parsefh(dp, v3) != NULL)
501 return;
502 break;
503
504 case NFSPROC_LOOKUP:
505 printf(" lookup");
506 if ((dp = parsereq(rp, length)) != NULL &&
507 parsefhn(dp, v3) != NULL)
508 return;
509 break;
510
511 case NFSPROC_ACCESS:
512 printf(" access");
513 if ((dp = parsereq(rp, length)) != NULL &&
514 (dp = parsefh(dp, v3)) != NULL) {
515 TCHECK(dp[0]);
516 printf(" %04x", (u_int32_t)ntohl(dp[0]));
517 return;
518 }
519 break;
520
521 case NFSPROC_READLINK:
522 printf(" readlink");
523 if ((dp = parsereq(rp, length)) != NULL &&
524 parsefh(dp, v3) != NULL)
525 return;
526 break;
527
528 case NFSPROC_READ:
529 printf(" read");
530 if ((dp = parsereq(rp, length)) != NULL &&
531 (dp = parsefh(dp, v3)) != NULL) {
532 if (v3) {
533 TCHECK(dp[2]);
534 printf(" %u bytes @ ",
535 (u_int32_t) ntohl(dp[2]));
536 print_int64(dp, UNSIGNED);
537 } else {
538 TCHECK(dp[1]);
539 printf(" %u bytes @ %u",
540 (u_int32_t)ntohl(dp[1]),
541 (u_int32_t)ntohl(dp[0]));
542 }
543 return;
544 }
545 break;
546
547 case NFSPROC_WRITE:
548 printf(" write");
549 if ((dp = parsereq(rp, length)) != NULL &&
550 (dp = parsefh(dp, v3)) != NULL) {
551 if (v3) {
552 TCHECK(dp[4]);
553 printf(" %u bytes @ ",
554 (u_int32_t) ntohl(dp[4]));
555 print_int64(dp, UNSIGNED);
556 if (vflag) {
557 dp += 3;
558 TCHECK(dp[0]);
559 printf(" <%s>",
560 tok2str(nfsv3_writemodes,
561 NULL, ntohl(*dp)));
562 }
563 } else {
564 TCHECK(dp[3]);
565 printf(" %u (%u) bytes @ %u (%u)",
566 (u_int32_t)ntohl(dp[3]),
567 (u_int32_t)ntohl(dp[2]),
568 (u_int32_t)ntohl(dp[1]),
569 (u_int32_t)ntohl(dp[0]));
570 }
571 return;
572 }
573 break;
574
575 case NFSPROC_CREATE:
576 printf(" create");
577 if ((dp = parsereq(rp, length)) != NULL &&
578 parsefhn(dp, v3) != NULL)
579 return;
580 break;
581
582 case NFSPROC_MKDIR:
583 printf(" mkdir");
584 if ((dp = parsereq(rp, length)) != 0 && parsefhn(dp, v3) != 0)
585 return;
586 break;
587
588 case NFSPROC_SYMLINK:
589 printf(" symlink");
590 if ((dp = parsereq(rp, length)) != 0 &&
591 (dp = parsefhn(dp, v3)) != 0) {
592 fputs(" ->", stdout);
593 if (v3 && (dp = parse_sattr3(dp, &sa3)) == 0)
594 break;
595 if (parsefn(dp) == 0)
596 break;
597 if (v3 && vflag)
598 print_sattr3(&sa3, vflag);
599 return;
600 }
601 break;
602
603 case NFSPROC_MKNOD:
604 printf(" mknod");
605 if ((dp = parsereq(rp, length)) != 0 &&
606 (dp = parsefhn(dp, v3)) != 0) {
607 TCHECK(*dp);
608 type = (nfs_type)ntohl(*dp++);
609 if ((dp = parse_sattr3(dp, &sa3)) == 0)
610 break;
611 printf(" %s", tok2str(type2str, "unk-ft %d", type));
612 if (vflag && (type == NFCHR || type == NFBLK)) {
613 TCHECK(dp[1]);
614 printf(" %u/%u",
615 (u_int32_t)ntohl(dp[0]),
616 (u_int32_t)ntohl(dp[1]));
617 dp += 2;
618 }
619 if (vflag)
620 print_sattr3(&sa3, vflag);
621 return;
622 }
623 break;
624
625 case NFSPROC_REMOVE:
626 printf(" remove");
627 if ((dp = parsereq(rp, length)) != NULL &&
628 parsefhn(dp, v3) != NULL)
629 return;
630 break;
631
632 case NFSPROC_RMDIR:
633 printf(" rmdir");
634 if ((dp = parsereq(rp, length)) != NULL &&
635 parsefhn(dp, v3) != NULL)
636 return;
637 break;
638
639 case NFSPROC_RENAME:
640 printf(" rename");
641 if ((dp = parsereq(rp, length)) != NULL &&
642 (dp = parsefhn(dp, v3)) != NULL) {
643 fputs(" ->", stdout);
644 if (parsefhn(dp, v3) != NULL)
645 return;
646 }
647 break;
648
649 case NFSPROC_LINK:
650 printf(" link");
651 if ((dp = parsereq(rp, length)) != NULL &&
652 (dp = parsefh(dp, v3)) != NULL) {
653 fputs(" ->", stdout);
654 if (parsefhn(dp, v3) != NULL)
655 return;
656 }
657 break;
658
659 case NFSPROC_READDIR:
660 printf(" readdir");
661 if ((dp = parsereq(rp, length)) != NULL &&
662 (dp = parsefh(dp, v3)) != NULL) {
663 if (v3) {
664 TCHECK(dp[4]);
665 /*
666 * We shouldn't really try to interpret the
667 * offset cookie here.
668 */
669 printf(" %u bytes @ ",
670 (u_int32_t) ntohl(dp[4]));
671 print_int64(dp, SIGNED);
672 if (vflag)
673 printf(" verf %08x%08x", dp[2],
674 dp[3]);
675 } else {
676 TCHECK(dp[1]);
677 /*
678 * Print the offset as signed, since -1 is
679 * common, but offsets > 2^31 aren't.
680 */
681 printf(" %u bytes @ %d",
682 (u_int32_t)ntohl(dp[1]),
683 (u_int32_t)ntohl(dp[0]));
684 }
685 return;
686 }
687 break;
688
689 case NFSPROC_READDIRPLUS:
690 printf(" readdirplus");
691 if ((dp = parsereq(rp, length)) != NULL &&
692 (dp = parsefh(dp, v3)) != NULL) {
693 TCHECK(dp[4]);
694 /*
695 * We don't try to interpret the offset
696 * cookie here.
697 */
698 printf(" %u bytes @ ", (u_int32_t) ntohl(dp[4]));
699 print_int64(dp, SIGNED);
700 if (vflag)
701 printf(" max %u verf %08x%08x",
702 (u_int32_t) ntohl(dp[5]), dp[2], dp[3]);
703 return;
704 }
705 break;
706
707 case NFSPROC_FSSTAT:
708 printf(" fsstat");
709 if ((dp = parsereq(rp, length)) != NULL &&
710 parsefh(dp, v3) != NULL)
711 return;
712 break;
713
714 case NFSPROC_FSINFO:
715 printf(" fsinfo");
716 if ((dp = parsereq(rp, length)) != NULL &&
717 parsefh(dp, v3) != NULL)
718 return;
719 break;
720
721 case NFSPROC_PATHCONF:
722 printf(" pathconf");
723 if ((dp = parsereq(rp, length)) != NULL &&
724 parsefh(dp, v3) != NULL)
725 return;
726 break;
727
728 case NFSPROC_COMMIT:
729 printf(" commit");
730 if ((dp = parsereq(rp, length)) != NULL &&
731 (dp = parsefh(dp, v3)) != NULL) {
732 printf(" %u bytes @ ", (u_int32_t) ntohl(dp[2]));
733 print_int64(dp, UNSIGNED);
734 return;
735 }
736 break;
737
738 default:
739 printf(" proc-%u", (u_int32_t)ntohl(rp->rm_call.cb_proc));
740 return;
741 }
742
743 trunc:
744 if (!nfserr)
745 fputs(" [|nfs]", stdout);
746 }
747
748 /*
749 * Print out an NFS file handle.
750 * We assume packet was not truncated before the end of the
751 * file handle pointed to by dp.
752 *
753 * Note: new version (using portable file-handle parser) doesn't produce
754 * generation number. It probably could be made to do that, with some
755 * additional hacking on the parser code.
756 */
757 static void
758 nfs_printfh(register const u_int32_t *dp, const u_int len)
759 {
760 my_fsid fsid;
761 ino_t ino;
762 const char *sfsname = NULL;
763 char *spacep;
764
765 Parse_fh((const u_char *)dp, len, &fsid, &ino, NULL, &sfsname, 0);
766
767 if (sfsname) {
768 /* file system ID is ASCII, not numeric, for this server OS */
769 static char temp[NFSX_V3FHMAX+1];
770
771 /* Make sure string is null-terminated */
772 strncpy(temp, sfsname, NFSX_V3FHMAX);
773 temp[sizeof(temp) - 1] = '\0';
774 /* Remove trailing spaces */
775 spacep = strchr(temp, ' ');
776 if (spacep)
777 *spacep = '\0';
778
779 (void)printf(" fh %s/", temp);
780 } else {
781 (void)printf(" fh %d,%d/",
782 fsid.Fsid_dev.Major, fsid.Fsid_dev.Minor);
783 }
784
785 if(fsid.Fsid_dev.Minor == 257 && uflag)
786 /* Print the undecoded handle */
787 (void)printf("%s", fsid.Opaque_Handle);
788 else
789 (void)printf("%ld", (long) ino);
790 }
791
792 /*
793 * Maintain a small cache of recent client.XID.server/proc pairs, to allow
794 * us to match up replies with requests and thus to know how to parse
795 * the reply.
796 */
797
798 struct xid_map_entry {
799 u_int32_t xid; /* transaction ID (net order) */
800 int ipver; /* IP version (4 or 6) */
801 #ifdef INET6
802 struct in6_addr client; /* client IP address (net order) */
803 struct in6_addr server; /* server IP address (net order) */
804 #else
805 struct in_addr client; /* client IP address (net order) */
806 struct in_addr server; /* server IP address (net order) */
807 #endif
808 u_int32_t proc; /* call proc number (host order) */
809 u_int32_t vers; /* program version (host order) */
810 };
811
812 /*
813 * Map entries are kept in an array that we manage as a ring;
814 * new entries are always added at the tail of the ring. Initially,
815 * all the entries are zero and hence don't match anything.
816 */
817
818 #define XIDMAPSIZE 64
819
820 struct xid_map_entry xid_map[XIDMAPSIZE];
821
822 int xid_map_next = 0;
823 int xid_map_hint = 0;
824
825 static void
826 xid_map_enter(const struct rpc_msg *rp, const u_char *bp)
827 {
828 struct ip *ip = NULL;
829 #ifdef INET6
830 struct ip6_hdr *ip6 = NULL;
831 #endif
832 struct xid_map_entry *xmep;
833
834 switch (IP_V((struct ip *)bp)) {
835 case 4:
836 ip = (struct ip *)bp;
837 break;
838 #ifdef INET6
839 case 6:
840 ip6 = (struct ip6_hdr *)bp;
841 break;
842 #endif
843 default:
844 return;
845 }
846
847 xmep = &xid_map[xid_map_next];
848
849 if (++xid_map_next >= XIDMAPSIZE)
850 xid_map_next = 0;
851
852 xmep->xid = rp->rm_xid;
853 if (ip) {
854 xmep->ipver = 4;
855 memcpy(&xmep->client, &ip->ip_src, sizeof(ip->ip_src));
856 memcpy(&xmep->server, &ip->ip_dst, sizeof(ip->ip_dst));
857 }
858 #ifdef INET6
859 else if (ip6) {
860 xmep->ipver = 6;
861 memcpy(&xmep->client, &ip6->ip6_src, sizeof(ip6->ip6_src));
862 memcpy(&xmep->server, &ip6->ip6_dst, sizeof(ip6->ip6_dst));
863 }
864 #endif
865 xmep->proc = ntohl(rp->rm_call.cb_proc);
866 xmep->vers = ntohl(rp->rm_call.cb_vers);
867 }
868
869 /*
870 * Returns 0 and puts NFSPROC_xxx in proc return and
871 * version in vers return, or returns -1 on failure
872 */
873 static int
874 xid_map_find(const struct rpc_msg *rp, const u_char *bp, u_int32_t *proc,
875 u_int32_t *vers)
876 {
877 int i;
878 struct xid_map_entry *xmep;
879 u_int32_t xid = rp->rm_xid;
880 struct ip *ip = (struct ip *)bp;
881 #ifdef INET6
882 struct ip6_hdr *ip6 = (struct ip6_hdr *)bp;
883 #endif
884 int cmp;
885
886 /* Start searching from where we last left off */
887 i = xid_map_hint;
888 do {
889 xmep = &xid_map[i];
890 cmp = 1;
891 if (xmep->ipver != IP_V(ip) || xmep->xid != xid)
892 goto nextitem;
893 switch (xmep->ipver) {
894 case 4:
895 if (memcmp(&ip->ip_src, &xmep->server,
896 sizeof(ip->ip_src)) != 0 ||
897 memcmp(&ip->ip_dst, &xmep->client,
898 sizeof(ip->ip_dst)) != 0) {
899 cmp = 0;
900 }
901 break;
902 #ifdef INET6
903 case 6:
904 if (memcmp(&ip6->ip6_src, &xmep->server,
905 sizeof(ip6->ip6_src)) != 0 ||
906 memcmp(&ip6->ip6_dst, &xmep->client,
907 sizeof(ip6->ip6_dst)) != 0) {
908 cmp = 0;
909 }
910 break;
911 #endif
912 default:
913 cmp = 0;
914 break;
915 }
916 if (cmp) {
917 /* match */
918 xid_map_hint = i;
919 *proc = xmep->proc;
920 *vers = xmep->vers;
921 return 0;
922 }
923 nextitem:
924 if (++i >= XIDMAPSIZE)
925 i = 0;
926 } while (i != xid_map_hint);
927
928 /* search failed */
929 return (-1);
930 }
931
932 /*
933 * Routines for parsing reply packets
934 */
935
936 /*
937 * Return a pointer to the beginning of the actual results.
938 * If the packet was truncated, return 0.
939 */
940 static const u_int32_t *
941 parserep(register const struct rpc_msg *rp, register u_int length)
942 {
943 register const u_int32_t *dp;
944 u_int len;
945 enum accept_stat astat;
946
947 /*
948 * Portability note:
949 * Here we find the address of the ar_verf credentials.
950 * Originally, this calculation was
951 * dp = (u_int32_t *)&rp->rm_reply.rp_acpt.ar_verf
952 * On the wire, the rp_acpt field starts immediately after
953 * the (32 bit) rp_stat field. However, rp_acpt (which is a
954 * "struct accepted_reply") contains a "struct opaque_auth",
955 * whose internal representation contains a pointer, so on a
956 * 64-bit machine the compiler inserts 32 bits of padding
957 * before rp->rm_reply.rp_acpt.ar_verf. So, we cannot use
958 * the internal representation to parse the on-the-wire
959 * representation. Instead, we skip past the rp_stat field,
960 * which is an "enum" and so occupies one 32-bit word.
961 */
962 dp = ((const u_int32_t *)&rp->rm_reply) + 1;
963 TCHECK(dp[1]);
964 len = ntohl(dp[1]);
965 if (len >= length)
966 return (NULL);
967 /*
968 * skip past the ar_verf credentials.
969 */
970 dp += (len + (2*sizeof(u_int32_t) + 3)) / sizeof(u_int32_t);
971 TCHECK2(dp[0], 0);
972
973 /*
974 * now we can check the ar_stat field
975 */
976 astat = ntohl(*(enum accept_stat *)dp);
977 switch (astat) {
978
979 case SUCCESS:
980 break;
981
982 case PROG_UNAVAIL:
983 printf(" PROG_UNAVAIL");
984 nfserr = 1; /* suppress trunc string */
985 return (NULL);
986
987 case PROG_MISMATCH:
988 printf(" PROG_MISMATCH");
989 nfserr = 1; /* suppress trunc string */
990 return (NULL);
991
992 case PROC_UNAVAIL:
993 printf(" PROC_UNAVAIL");
994 nfserr = 1; /* suppress trunc string */
995 return (NULL);
996
997 case GARBAGE_ARGS:
998 printf(" GARBAGE_ARGS");
999 nfserr = 1; /* suppress trunc string */
1000 return (NULL);
1001
1002 case SYSTEM_ERR:
1003 printf(" SYSTEM_ERR");
1004 nfserr = 1; /* suppress trunc string */
1005 return (NULL);
1006
1007 default:
1008 printf(" ar_stat %d", astat);
1009 nfserr = 1; /* suppress trunc string */
1010 return (NULL);
1011 }
1012 /* successful return */
1013 TCHECK2(*dp, sizeof(astat));
1014 return ((u_int32_t *) (sizeof(astat) + ((char *)dp)));
1015 trunc:
1016 return (0);
1017 }
1018
1019 static const u_int32_t *
1020 parsestatus(const u_int32_t *dp, int *er)
1021 {
1022 int errnum;
1023
1024 TCHECK(dp[0]);
1025
1026 errnum = ntohl(dp[0]);
1027 if (er)
1028 *er = errnum;
1029 if (errnum != 0) {
1030 if (!qflag)
1031 printf(" ERROR: %s",
1032 tok2str(status2str, "unk %d", errnum));
1033 nfserr = 1;
1034 }
1035 return (dp + 1);
1036 trunc:
1037 return NULL;
1038 }
1039
1040 static const u_int32_t *
1041 parsefattr(const u_int32_t *dp, int verbose, int v3)
1042 {
1043 const struct nfs_fattr *fap;
1044
1045 fap = (const struct nfs_fattr *)dp;
1046 TCHECK(fap->fa_gid);
1047 if (verbose) {
1048 printf(" %s %o ids %d/%d",
1049 tok2str(type2str, "unk-ft %d ",
1050 (u_int32_t)ntohl(fap->fa_type)),
1051 (u_int32_t)ntohl(fap->fa_mode),
1052 (u_int32_t)ntohl(fap->fa_uid),
1053 (u_int32_t) ntohl(fap->fa_gid));
1054 if (v3) {
1055 TCHECK(fap->fa3_size);
1056 printf(" sz ");
1057 print_int64((u_int32_t *)&fap->fa3_size, UNSIGNED);
1058 putchar(' ');
1059 } else {
1060 TCHECK(fap->fa2_size);
1061 printf(" sz %d ", (u_int32_t) ntohl(fap->fa2_size));
1062 }
1063 }
1064 /* print lots more stuff */
1065 if (verbose > 1) {
1066 if (v3) {
1067 TCHECK(fap->fa3_ctime);
1068 printf("nlink %d rdev %d/%d ",
1069 (u_int32_t)ntohl(fap->fa_nlink),
1070 (u_int32_t) ntohl(fap->fa3_rdev.specdata1),
1071 (u_int32_t) ntohl(fap->fa3_rdev.specdata2));
1072 printf("fsid ");
1073 print_int64((u_int32_t *)&fap->fa2_fsid, HEX);
1074 printf(" nodeid ");
1075 print_int64((u_int32_t *)&fap->fa2_fileid, HEX);
1076 printf(" a/m/ctime %u.%06u ",
1077 (u_int32_t) ntohl(fap->fa3_atime.nfsv3_sec),
1078 (u_int32_t) ntohl(fap->fa3_atime.nfsv3_nsec));
1079 printf("%u.%06u ",
1080 (u_int32_t) ntohl(fap->fa3_mtime.nfsv3_sec),
1081 (u_int32_t) ntohl(fap->fa3_mtime.nfsv3_nsec));
1082 printf("%u.%06u ",
1083 (u_int32_t) ntohl(fap->fa3_ctime.nfsv3_sec),
1084 (u_int32_t) ntohl(fap->fa3_ctime.nfsv3_nsec));
1085 } else {
1086 TCHECK(fap->fa2_ctime);
1087 printf("nlink %d rdev %x fsid %x nodeid %x a/m/ctime ",
1088 (u_int32_t) ntohl(fap->fa_nlink),
1089 (u_int32_t) ntohl(fap->fa2_rdev),
1090 (u_int32_t) ntohl(fap->fa2_fsid),
1091 (u_int32_t) ntohl(fap->fa2_fileid));
1092 printf("%u.%06u ",
1093 (u_int32_t) ntohl(fap->fa2_atime.nfsv2_sec),
1094 (u_int32_t) ntohl(fap->fa2_atime.nfsv2_usec));
1095 printf("%u.%06u ",
1096 (u_int32_t) ntohl(fap->fa2_mtime.nfsv2_sec),
1097 (u_int32_t) ntohl(fap->fa2_mtime.nfsv2_usec));
1098 printf("%u.%06u ",
1099 (u_int32_t) ntohl(fap->fa2_ctime.nfsv2_sec),
1100 (u_int32_t) ntohl(fap->fa2_ctime.nfsv2_usec));
1101 }
1102 }
1103 return ((const u_int32_t *)((unsigned char *)dp +
1104 (v3 ? NFSX_V3FATTR : NFSX_V2FATTR)));
1105 trunc:
1106 return (NULL);
1107 }
1108
1109 static int
1110 parseattrstat(const u_int32_t *dp, int verbose, int v3)
1111 {
1112 int er;
1113
1114 dp = parsestatus(dp, &er);
1115 if (dp == NULL)
1116 return (0);
1117 if (er)
1118 return (1);
1119
1120 return (parsefattr(dp, verbose, v3) != NULL);
1121 }
1122
1123 static int
1124 parsediropres(const u_int32_t *dp)
1125 {
1126 int er;
1127
1128 if (!(dp = parsestatus(dp, &er)))
1129 return (0);
1130 if (er)
1131 return (1);
1132
1133 dp = parsefh(dp, 0);
1134 if (dp == NULL)
1135 return (0);
1136
1137 return (parsefattr(dp, vflag, 0) != NULL);
1138 }
1139
1140 static int
1141 parselinkres(const u_int32_t *dp, int v3)
1142 {
1143 int er;
1144
1145 dp = parsestatus(dp, &er);
1146 if (dp == NULL)
1147 return(0);
1148 if (er)
1149 return(1);
1150 if (v3 && !(dp = parse_post_op_attr(dp, vflag)))
1151 return (0);
1152 putchar(' ');
1153 return (parsefn(dp) != NULL);
1154 }
1155
1156 static int
1157 parsestatfs(const u_int32_t *dp, int v3)
1158 {
1159 const struct nfs_statfs *sfsp;
1160 int er;
1161
1162 dp = parsestatus(dp, &er);
1163 if (dp == NULL)
1164 return (0);
1165 if (!v3 && er)
1166 return (1);
1167
1168 if (qflag)
1169 return(1);
1170
1171 if (v3) {
1172 if (vflag)
1173 printf(" POST:");
1174 if (!(dp = parse_post_op_attr(dp, vflag)))
1175 return (0);
1176 }
1177
1178 TCHECK2(*dp, (v3 ? NFSX_V3STATFS : NFSX_V2STATFS));
1179
1180 sfsp = (const struct nfs_statfs *)dp;
1181
1182 if (v3) {
1183 printf(" tbytes ");
1184 print_int64((u_int32_t *)&sfsp->sf_tbytes, UNSIGNED);
1185 printf(" fbytes ");
1186 print_int64((u_int32_t *)&sfsp->sf_fbytes, UNSIGNED);
1187 printf(" abytes ");
1188 print_int64((u_int32_t *)&sfsp->sf_abytes, UNSIGNED);
1189 if (vflag) {
1190 printf(" tfiles ");
1191 print_int64((u_int32_t *)&sfsp->sf_tfiles, UNSIGNED);
1192 printf(" ffiles ");
1193 print_int64((u_int32_t *)&sfsp->sf_ffiles, UNSIGNED);
1194 printf(" afiles ");
1195 print_int64((u_int32_t *)&sfsp->sf_afiles, UNSIGNED);
1196 printf(" invar %u",
1197 (u_int32_t) ntohl(sfsp->sf_invarsec));
1198 }
1199 } else {
1200 printf(" tsize %d bsize %d blocks %d bfree %d bavail %d",
1201 (u_int32_t)ntohl(sfsp->sf_tsize),
1202 (u_int32_t)ntohl(sfsp->sf_bsize),
1203 (u_int32_t)ntohl(sfsp->sf_blocks),
1204 (u_int32_t)ntohl(sfsp->sf_bfree),
1205 (u_int32_t)ntohl(sfsp->sf_bavail));
1206 }
1207
1208 return (1);
1209 trunc:
1210 return (0);
1211 }
1212
1213 static int
1214 parserddires(const u_int32_t *dp)
1215 {
1216 int er;
1217
1218 dp = parsestatus(dp, &er);
1219 if (dp == NULL)
1220 return (0);
1221 if (er)
1222 return (1);
1223 if (qflag)
1224 return (1);
1225
1226 TCHECK(dp[2]);
1227 printf(" offset %x size %d ",
1228 (u_int32_t)ntohl(dp[0]), (u_int32_t)ntohl(dp[1]));
1229 if (dp[2] != 0)
1230 printf(" eof");
1231
1232 return (1);
1233 trunc:
1234 return (0);
1235 }
1236
1237 static const u_int32_t *
1238 parse_wcc_attr(const u_int32_t *dp)
1239 {
1240 printf(" sz ");
1241 print_int64(dp, UNSIGNED);
1242 printf(" mtime %u.%06u ctime %u.%06u",
1243 (u_int32_t)ntohl(dp[2]), (u_int32_t)ntohl(dp[3]),
1244 (u_int32_t)ntohl(dp[4]), (u_int32_t)ntohl(dp[5]));
1245 return (dp + 6);
1246 }
1247
1248 /*
1249 * Pre operation attributes. Print only if vflag > 1.
1250 */
1251 static const u_int32_t *
1252 parse_pre_op_attr(const u_int32_t *dp, int verbose)
1253 {
1254 TCHECK(dp[0]);
1255 if (!ntohl(dp[0]))
1256 return (dp + 1);
1257 dp++;
1258 TCHECK2(*dp, 24);
1259 if (verbose > 1) {
1260 return parse_wcc_attr(dp);
1261 } else {
1262 /* If not verbose enough, just skip over wcc_attr */
1263 return (dp + 6);
1264 }
1265 trunc:
1266 return (NULL);
1267 }
1268
1269 /*
1270 * Post operation attributes are printed if vflag >= 1
1271 */
1272 static const u_int32_t *
1273 parse_post_op_attr(const u_int32_t *dp, int verbose)
1274 {
1275 TCHECK(dp[0]);
1276 if (!ntohl(dp[0]))
1277 return (dp + 1);
1278 dp++;
1279 if (verbose) {
1280 return parsefattr(dp, verbose, 1);
1281 } else
1282 return (dp + (NFSX_V3FATTR / sizeof (u_int32_t)));
1283 trunc:
1284 return (NULL);
1285 }
1286
1287 static const u_int32_t *
1288 parse_wcc_data(const u_int32_t *dp, int verbose)
1289 {
1290 if (verbose > 1)
1291 printf(" PRE:");
1292 if (!(dp = parse_pre_op_attr(dp, verbose)))
1293 return (0);
1294
1295 if (verbose)
1296 printf(" POST:");
1297 return parse_post_op_attr(dp, verbose);
1298 }
1299
1300 static const u_int32_t *
1301 parsecreateopres(const u_int32_t *dp, int verbose)
1302 {
1303 int er;
1304
1305 if (!(dp = parsestatus(dp, &er)))
1306 return (0);
1307 if (er)
1308 dp = parse_wcc_data(dp, verbose);
1309 else {
1310 TCHECK(dp[0]);
1311 if (!ntohl(dp[0]))
1312 return (dp + 1);
1313 dp++;
1314 if (!(dp = parsefh(dp, 1)))
1315 return (0);
1316 if (verbose) {
1317 if (!(dp = parse_post_op_attr(dp, verbose)))
1318 return (0);
1319 if (vflag > 1) {
1320 printf("dir attr:");
1321 dp = parse_wcc_data(dp, verbose);
1322 }
1323 }
1324 }
1325 return (dp);
1326 trunc:
1327 return (NULL);
1328 }
1329
1330 static int
1331 parsewccres(const u_int32_t *dp, int verbose)
1332 {
1333 int er;
1334
1335 if (!(dp = parsestatus(dp, &er)))
1336 return (0);
1337 return parse_wcc_data(dp, verbose) != 0;
1338 }
1339
1340 static const u_int32_t *
1341 parsev3rddirres(const u_int32_t *dp, int verbose)
1342 {
1343 int er;
1344
1345 if (!(dp = parsestatus(dp, &er)))
1346 return (0);
1347 if (vflag)
1348 printf(" POST:");
1349 if (!(dp = parse_post_op_attr(dp, verbose)))
1350 return (0);
1351 if (er)
1352 return dp;
1353 if (vflag) {
1354 TCHECK(dp[1]);
1355 printf(" verf %08x%08x", dp[0], dp[1]);
1356 dp += 2;
1357 }
1358 return dp;
1359 trunc:
1360 return (NULL);
1361 }
1362
1363 static int
1364 parsefsinfo(const u_int32_t *dp)
1365 {
1366 struct nfsv3_fsinfo *sfp;
1367 int er;
1368
1369 if (!(dp = parsestatus(dp, &er)))
1370 return (0);
1371 if (vflag)
1372 printf(" POST:");
1373 if (!(dp = parse_post_op_attr(dp, vflag)))
1374 return (0);
1375 if (er)
1376 return (1);
1377
1378 sfp = (struct nfsv3_fsinfo *)dp;
1379 TCHECK(*sfp);
1380 printf(" rtmax %u rtpref %u wtmax %u wtpref %u dtpref %u",
1381 (u_int32_t) ntohl(sfp->fs_rtmax),
1382 (u_int32_t) ntohl(sfp->fs_rtpref),
1383 (u_int32_t) ntohl(sfp->fs_wtmax),
1384 (u_int32_t) ntohl(sfp->fs_wtpref),
1385 (u_int32_t) ntohl(sfp->fs_dtpref));
1386 if (vflag) {
1387 printf(" rtmult %u wtmult %u maxfsz ",
1388 (u_int32_t) ntohl(sfp->fs_rtmult),
1389 (u_int32_t) ntohl(sfp->fs_wtmult));
1390 print_int64((u_int32_t *)&sfp->fs_maxfilesize, UNSIGNED);
1391 printf(" delta %u.%06u ",
1392 (u_int32_t) ntohl(sfp->fs_timedelta.nfsv3_sec),
1393 (u_int32_t) ntohl(sfp->fs_timedelta.nfsv3_nsec));
1394 }
1395 return (1);
1396 trunc:
1397 return (0);
1398 }
1399
1400 static int
1401 parsepathconf(const u_int32_t *dp)
1402 {
1403 int er;
1404 struct nfsv3_pathconf *spp;
1405
1406 if (!(dp = parsestatus(dp, &er)))
1407 return (0);
1408 if (vflag)
1409 printf(" POST:");
1410 if (!(dp = parse_post_op_attr(dp, vflag)))
1411 return (0);
1412 if (er)
1413 return (1);
1414
1415 spp = (struct nfsv3_pathconf *)dp;
1416 TCHECK(*spp);
1417
1418 printf(" linkmax %u namemax %u %s %s %s %s",
1419 (u_int32_t) ntohl(spp->pc_linkmax),
1420 (u_int32_t) ntohl(spp->pc_namemax),
1421 ntohl(spp->pc_notrunc) ? "notrunc" : "",
1422 ntohl(spp->pc_chownrestricted) ? "chownres" : "",
1423 ntohl(spp->pc_caseinsensitive) ? "igncase" : "",
1424 ntohl(spp->pc_casepreserving) ? "keepcase" : "");
1425 return (1);
1426 trunc:
1427 return (0);
1428 }
1429
1430 static void
1431 interp_reply(const struct rpc_msg *rp, u_int32_t proc, u_int32_t vers, int length)
1432 {
1433 register const u_int32_t *dp;
1434 register int v3;
1435 int er;
1436
1437 v3 = (vers == NFS_VER3);
1438
1439 if (!v3 && proc < NFS_NPROCS)
1440 proc = nfsv3_procid[proc];
1441
1442 switch (proc) {
1443
1444 case NFSPROC_NOOP:
1445 printf(" nop");
1446 return;
1447
1448 case NFSPROC_NULL:
1449 printf(" null");
1450 return;
1451
1452 case NFSPROC_GETATTR:
1453 printf(" getattr");
1454 dp = parserep(rp, length);
1455 if (dp != NULL && parseattrstat(dp, !qflag, v3) != 0)
1456 return;
1457 break;
1458
1459 case NFSPROC_SETATTR:
1460 printf(" setattr");
1461 if (!(dp = parserep(rp, length)))
1462 return;
1463 if (v3) {
1464 if (parsewccres(dp, vflag))
1465 return;
1466 } else {
1467 if (parseattrstat(dp, !qflag, 0) != 0)
1468 return;
1469 }
1470 break;
1471
1472 case NFSPROC_LOOKUP:
1473 printf(" lookup");
1474 if (!(dp = parserep(rp, length)))
1475 break;
1476 if (v3) {
1477 if (!(dp = parsestatus(dp, &er)))
1478 break;
1479 if (er) {
1480 if (vflag > 1) {
1481 printf(" post dattr:");
1482 dp = parse_post_op_attr(dp, vflag);
1483 }
1484 } else {
1485 if (!(dp = parsefh(dp, v3)))
1486 break;
1487 if ((dp = parse_post_op_attr(dp, vflag)) &&
1488 vflag > 1) {
1489 printf(" post dattr:");
1490 dp = parse_post_op_attr(dp, vflag);
1491 }
1492 }
1493 if (dp)
1494 return;
1495 } else {
1496 if (parsediropres(dp) != 0)
1497 return;
1498 }
1499 break;
1500
1501 case NFSPROC_ACCESS:
1502 printf(" access");
1503 if (!(dp = parserep(rp, length)))
1504 break;
1505 if (!(dp = parsestatus(dp, &er)))
1506 break;
1507 if (vflag)
1508 printf(" attr:");
1509 if (!(dp = parse_post_op_attr(dp, vflag)))
1510 break;
1511 if (!er)
1512 printf(" c %04x", (u_int32_t)ntohl(dp[0]));
1513 return;
1514
1515 case NFSPROC_READLINK:
1516 printf(" readlink");
1517 dp = parserep(rp, length);
1518 if (dp != NULL && parselinkres(dp, v3) != 0)
1519 return;
1520 break;
1521
1522 case NFSPROC_READ:
1523 printf(" read");
1524 if (!(dp = parserep(rp, length)))
1525 break;
1526 if (v3) {
1527 if (!(dp = parsestatus(dp, &er)))
1528 break;
1529 if (!(dp = parse_post_op_attr(dp, vflag)))
1530 break;
1531 if (er)
1532 return;
1533 if (vflag) {
1534 TCHECK(dp[1]);
1535 printf("%u bytes", (u_int32_t) ntohl(dp[0]));
1536 if (ntohl(dp[1]))
1537 printf(" EOF");
1538 }
1539 return;
1540 } else {
1541 if (parseattrstat(dp, vflag, 0) != 0)
1542 return;
1543 }
1544 break;
1545
1546 case NFSPROC_WRITE:
1547 printf(" write");
1548 if (!(dp = parserep(rp, length)))
1549 break;
1550 if (v3) {
1551 if (!(dp = parsestatus(dp, &er)))
1552 break;
1553 if (!(dp = parse_wcc_data(dp, vflag)))
1554 break;
1555 if (er)
1556 return;
1557 if (vflag) {
1558 TCHECK(dp[0]);
1559 printf("%u bytes", (u_int32_t) ntohl(dp[0]));
1560 if (vflag > 1) {
1561 TCHECK(dp[1]);
1562 printf(" <%s>",
1563 tok2str(nfsv3_writemodes,
1564 NULL, ntohl(dp[1])));
1565 }
1566 return;
1567 }
1568 } else {
1569 if (parseattrstat(dp, vflag, v3) != 0)
1570 return;
1571 }
1572 break;
1573
1574 case NFSPROC_CREATE:
1575 printf(" create");
1576 if (!(dp = parserep(rp, length)))
1577 break;
1578 if (v3) {
1579 if (parsecreateopres(dp, vflag) != 0)
1580 return;
1581 } else {
1582 if (parsediropres(dp) != 0)
1583 return;
1584 }
1585 break;
1586
1587 case NFSPROC_MKDIR:
1588 printf(" mkdir");
1589 if (!(dp = parserep(rp, length)))
1590 break;
1591 if (v3) {
1592 if (parsecreateopres(dp, vflag) != 0)
1593 return;
1594 } else {
1595 if (parsediropres(dp) != 0)
1596 return;
1597 }
1598 break;
1599
1600 case NFSPROC_SYMLINK:
1601 printf(" symlink");
1602 if (!(dp = parserep(rp, length)))
1603 break;
1604 if (v3) {
1605 if (parsecreateopres(dp, vflag) != 0)
1606 return;
1607 } else {
1608 if (parsestatus(dp, &er) != 0)
1609 return;
1610 }
1611 break;
1612
1613 case NFSPROC_MKNOD:
1614 printf(" mknod");
1615 if (!(dp = parserep(rp, length)))
1616 break;
1617 if (parsecreateopres(dp, vflag) != 0)
1618 return;
1619 break;
1620
1621 case NFSPROC_REMOVE:
1622 printf(" remove");
1623 if (!(dp = parserep(rp, length)))
1624 break;
1625 if (v3) {
1626 if (parsewccres(dp, vflag))
1627 return;
1628 } else {
1629 if (parsestatus(dp, &er) != 0)
1630 return;
1631 }
1632 break;
1633
1634 case NFSPROC_RMDIR:
1635 printf(" rmdir");
1636 if (!(dp = parserep(rp, length)))
1637 break;
1638 if (v3) {
1639 if (parsewccres(dp, vflag))
1640 return;
1641 } else {
1642 if (parsestatus(dp, &er) != 0)
1643 return;
1644 }
1645 break;
1646
1647 case NFSPROC_RENAME:
1648 printf(" rename");
1649 if (!(dp = parserep(rp, length)))
1650 break;
1651 if (v3) {
1652 if (!(dp = parsestatus(dp, &er)))
1653 break;
1654 if (vflag) {
1655 printf(" from:");
1656 if (!(dp = parse_wcc_data(dp, vflag)))
1657 break;
1658 printf(" to:");
1659 if (!(dp = parse_wcc_data(dp, vflag)))
1660 break;
1661 }
1662 return;
1663 } else {
1664 if (parsestatus(dp, &er) != 0)
1665 return;
1666 }
1667 break;
1668
1669 case NFSPROC_LINK:
1670 printf(" link");
1671 if (!(dp = parserep(rp, length)))
1672 break;
1673 if (v3) {
1674 if (!(dp = parsestatus(dp, &er)))
1675 break;
1676 if (vflag) {
1677 printf(" file POST:");
1678 if (!(dp = parse_post_op_attr(dp, vflag)))
1679 break;
1680 printf(" dir:");
1681 if (!(dp = parse_wcc_data(dp, vflag)))
1682 break;
1683 return;
1684 }
1685 } else {
1686 if (parsestatus(dp, &er) != 0)
1687 return;
1688 }
1689 break;
1690
1691 case NFSPROC_READDIR:
1692 printf(" readdir");
1693 if (!(dp = parserep(rp, length)))
1694 break;
1695 if (v3) {
1696 if (parsev3rddirres(dp, vflag))
1697 return;
1698 } else {
1699 if (parserddires(dp) != 0)
1700 return;
1701 }
1702 break;
1703
1704 case NFSPROC_READDIRPLUS:
1705 printf(" readdirplus");
1706 if (!(dp = parserep(rp, length)))
1707 break;
1708 if (parsev3rddirres(dp, vflag))
1709 return;
1710 break;
1711
1712 case NFSPROC_FSSTAT:
1713 printf(" fsstat");
1714 dp = parserep(rp, length);
1715 if (dp != NULL && parsestatfs(dp, v3) != 0)
1716 return;
1717 break;
1718
1719 case NFSPROC_FSINFO:
1720 printf(" fsinfo");
1721 dp = parserep(rp, length);
1722 if (dp != NULL && parsefsinfo(dp) != 0)
1723 return;
1724 break;
1725
1726 case NFSPROC_PATHCONF:
1727 printf(" pathconf");
1728 dp = parserep(rp, length);
1729 if (dp != NULL && parsepathconf(dp) != 0)
1730 return;
1731 break;
1732
1733 case NFSPROC_COMMIT:
1734 printf(" commit");
1735 dp = parserep(rp, length);
1736 if (dp != NULL && parsewccres(dp, vflag) != 0)
1737 return;
1738 break;
1739
1740 default:
1741 printf(" proc-%u", proc);
1742 return;
1743 }
1744 trunc:
1745 if (!nfserr)
1746 fputs(" [|nfs]", stdout);
1747 }