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