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