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