]> The Tcpdump Group git mirrors - tcpdump/blob - print-nfs.c
Get rid of casts of pointers-to-const to pointers-to-nonconst.
[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.91 2002-04-24 06:27:06 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 break;
720
721 case NFSPROC_PATHCONF:
722 printf(" pathconf");
723 break;
724
725 case NFSPROC_COMMIT:
726 printf(" commit");
727 if ((dp = parsereq(rp, length)) != NULL &&
728 (dp = parsefh(dp, v3)) != NULL) {
729 printf(" %u bytes @ ", (u_int32_t) ntohl(dp[2]));
730 print_int64(dp, UNSIGNED);
731 return;
732 }
733 break;
734
735 default:
736 printf(" proc-%u", (u_int32_t)ntohl(rp->rm_call.cb_proc));
737 return;
738 }
739
740 trunc:
741 if (!nfserr)
742 fputs(" [|nfs]", stdout);
743 }
744
745 /*
746 * Print out an NFS file handle.
747 * We assume packet was not truncated before the end of the
748 * file handle pointed to by dp.
749 *
750 * Note: new version (using portable file-handle parser) doesn't produce
751 * generation number. It probably could be made to do that, with some
752 * additional hacking on the parser code.
753 */
754 static void
755 nfs_printfh(register const u_int32_t *dp, const u_int len)
756 {
757 my_fsid fsid;
758 ino_t ino;
759 const char *sfsname = NULL;
760 char *spacep;
761
762 Parse_fh((const u_char *)dp, len, &fsid, &ino, NULL, &sfsname, 0);
763
764 if (sfsname) {
765 /* file system ID is ASCII, not numeric, for this server OS */
766 static char temp[NFSX_V3FHMAX+1];
767
768 /* Make sure string is null-terminated */
769 strncpy(temp, sfsname, NFSX_V3FHMAX);
770 temp[sizeof(temp) - 1] = '\0';
771 /* Remove trailing spaces */
772 spacep = strchr(temp, ' ');
773 if (spacep)
774 *spacep = '\0';
775
776 (void)printf(" fh %s/", temp);
777 } else {
778 (void)printf(" fh %d,%d/",
779 fsid.Fsid_dev.Major, fsid.Fsid_dev.Minor);
780 }
781
782 if(fsid.Fsid_dev.Minor == 257 && uflag)
783 /* Print the undecoded handle */
784 (void)printf("%s", fsid.Opaque_Handle);
785 else
786 (void)printf("%ld", (long) ino);
787 }
788
789 /*
790 * Maintain a small cache of recent client.XID.server/proc pairs, to allow
791 * us to match up replies with requests and thus to know how to parse
792 * the reply.
793 */
794
795 struct xid_map_entry {
796 u_int32_t xid; /* transaction ID (net order) */
797 int ipver; /* IP version (4 or 6) */
798 #ifdef INET6
799 struct in6_addr client; /* client IP address (net order) */
800 struct in6_addr server; /* server IP address (net order) */
801 #else
802 struct in_addr client; /* client IP address (net order) */
803 struct in_addr server; /* server IP address (net order) */
804 #endif
805 u_int32_t proc; /* call proc number (host order) */
806 u_int32_t vers; /* program version (host order) */
807 };
808
809 /*
810 * Map entries are kept in an array that we manage as a ring;
811 * new entries are always added at the tail of the ring. Initially,
812 * all the entries are zero and hence don't match anything.
813 */
814
815 #define XIDMAPSIZE 64
816
817 struct xid_map_entry xid_map[XIDMAPSIZE];
818
819 int xid_map_next = 0;
820 int xid_map_hint = 0;
821
822 static void
823 xid_map_enter(const struct rpc_msg *rp, const u_char *bp)
824 {
825 struct ip *ip = NULL;
826 #ifdef INET6
827 struct ip6_hdr *ip6 = NULL;
828 #endif
829 struct xid_map_entry *xmep;
830
831 switch (IP_V((struct ip *)bp)) {
832 case 4:
833 ip = (struct ip *)bp;
834 break;
835 #ifdef INET6
836 case 6:
837 ip6 = (struct ip6_hdr *)bp;
838 break;
839 #endif
840 default:
841 return;
842 }
843
844 xmep = &xid_map[xid_map_next];
845
846 if (++xid_map_next >= XIDMAPSIZE)
847 xid_map_next = 0;
848
849 xmep->xid = rp->rm_xid;
850 if (ip) {
851 xmep->ipver = 4;
852 memcpy(&xmep->client, &ip->ip_src, sizeof(ip->ip_src));
853 memcpy(&xmep->server, &ip->ip_dst, sizeof(ip->ip_dst));
854 }
855 #ifdef INET6
856 else if (ip6) {
857 xmep->ipver = 6;
858 memcpy(&xmep->client, &ip6->ip6_src, sizeof(ip6->ip6_src));
859 memcpy(&xmep->server, &ip6->ip6_dst, sizeof(ip6->ip6_dst));
860 }
861 #endif
862 xmep->proc = ntohl(rp->rm_call.cb_proc);
863 xmep->vers = ntohl(rp->rm_call.cb_vers);
864 }
865
866 /*
867 * Returns 0 and puts NFSPROC_xxx in proc return and
868 * version in vers return, or returns -1 on failure
869 */
870 static int
871 xid_map_find(const struct rpc_msg *rp, const u_char *bp, u_int32_t *proc,
872 u_int32_t *vers)
873 {
874 int i;
875 struct xid_map_entry *xmep;
876 u_int32_t xid = rp->rm_xid;
877 struct ip *ip = (struct ip *)bp;
878 #ifdef INET6
879 struct ip6_hdr *ip6 = (struct ip6_hdr *)bp;
880 #endif
881 int cmp;
882
883 /* Start searching from where we last left off */
884 i = xid_map_hint;
885 do {
886 xmep = &xid_map[i];
887 cmp = 1;
888 if (xmep->ipver != IP_V(ip) || xmep->xid != xid)
889 goto nextitem;
890 switch (xmep->ipver) {
891 case 4:
892 if (memcmp(&ip->ip_src, &xmep->server,
893 sizeof(ip->ip_src)) != 0 ||
894 memcmp(&ip->ip_dst, &xmep->client,
895 sizeof(ip->ip_dst)) != 0) {
896 cmp = 0;
897 }
898 break;
899 #ifdef INET6
900 case 6:
901 if (memcmp(&ip6->ip6_src, &xmep->server,
902 sizeof(ip6->ip6_src)) != 0 ||
903 memcmp(&ip6->ip6_dst, &xmep->client,
904 sizeof(ip6->ip6_dst)) != 0) {
905 cmp = 0;
906 }
907 break;
908 #endif
909 default:
910 cmp = 0;
911 break;
912 }
913 if (cmp) {
914 /* match */
915 xid_map_hint = i;
916 *proc = xmep->proc;
917 *vers = xmep->vers;
918 return 0;
919 }
920 nextitem:
921 if (++i >= XIDMAPSIZE)
922 i = 0;
923 } while (i != xid_map_hint);
924
925 /* search failed */
926 return (-1);
927 }
928
929 /*
930 * Routines for parsing reply packets
931 */
932
933 /*
934 * Return a pointer to the beginning of the actual results.
935 * If the packet was truncated, return 0.
936 */
937 static const u_int32_t *
938 parserep(register const struct rpc_msg *rp, register u_int length)
939 {
940 register const u_int32_t *dp;
941 u_int len;
942 enum accept_stat astat;
943
944 /*
945 * Portability note:
946 * Here we find the address of the ar_verf credentials.
947 * Originally, this calculation was
948 * dp = (u_int32_t *)&rp->rm_reply.rp_acpt.ar_verf
949 * On the wire, the rp_acpt field starts immediately after
950 * the (32 bit) rp_stat field. However, rp_acpt (which is a
951 * "struct accepted_reply") contains a "struct opaque_auth",
952 * whose internal representation contains a pointer, so on a
953 * 64-bit machine the compiler inserts 32 bits of padding
954 * before rp->rm_reply.rp_acpt.ar_verf. So, we cannot use
955 * the internal representation to parse the on-the-wire
956 * representation. Instead, we skip past the rp_stat field,
957 * which is an "enum" and so occupies one 32-bit word.
958 */
959 dp = ((const u_int32_t *)&rp->rm_reply) + 1;
960 TCHECK(dp[1]);
961 len = ntohl(dp[1]);
962 if (len >= length)
963 return (NULL);
964 /*
965 * skip past the ar_verf credentials.
966 */
967 dp += (len + (2*sizeof(u_int32_t) + 3)) / sizeof(u_int32_t);
968 TCHECK2(dp[0], 0);
969
970 /*
971 * now we can check the ar_stat field
972 */
973 astat = ntohl(*(enum accept_stat *)dp);
974 switch (astat) {
975
976 case SUCCESS:
977 break;
978
979 case PROG_UNAVAIL:
980 printf(" PROG_UNAVAIL");
981 nfserr = 1; /* suppress trunc string */
982 return (NULL);
983
984 case PROG_MISMATCH:
985 printf(" PROG_MISMATCH");
986 nfserr = 1; /* suppress trunc string */
987 return (NULL);
988
989 case PROC_UNAVAIL:
990 printf(" PROC_UNAVAIL");
991 nfserr = 1; /* suppress trunc string */
992 return (NULL);
993
994 case GARBAGE_ARGS:
995 printf(" GARBAGE_ARGS");
996 nfserr = 1; /* suppress trunc string */
997 return (NULL);
998
999 case SYSTEM_ERR:
1000 printf(" SYSTEM_ERR");
1001 nfserr = 1; /* suppress trunc string */
1002 return (NULL);
1003
1004 default:
1005 printf(" ar_stat %d", astat);
1006 nfserr = 1; /* suppress trunc string */
1007 return (NULL);
1008 }
1009 /* successful return */
1010 TCHECK2(*dp, sizeof(astat));
1011 return ((u_int32_t *) (sizeof(astat) + ((char *)dp)));
1012 trunc:
1013 return (0);
1014 }
1015
1016 static const u_int32_t *
1017 parsestatus(const u_int32_t *dp, int *er)
1018 {
1019 int errnum;
1020
1021 TCHECK(dp[0]);
1022
1023 errnum = ntohl(dp[0]);
1024 if (er)
1025 *er = errnum;
1026 if (errnum != 0) {
1027 if (!qflag)
1028 printf(" ERROR: %s",
1029 tok2str(status2str, "unk %d", errnum));
1030 nfserr = 1;
1031 return (NULL);
1032 }
1033 return (dp + 1);
1034 trunc:
1035 return NULL;
1036 }
1037
1038 static const u_int32_t *
1039 parsefattr(const u_int32_t *dp, int verbose, int v3)
1040 {
1041 const struct nfs_fattr *fap;
1042
1043 fap = (const struct nfs_fattr *)dp;
1044 TCHECK(fap->fa_gid);
1045 if (verbose) {
1046 printf(" %s %o ids %d/%d",
1047 tok2str(type2str, "unk-ft %d ",
1048 (u_int32_t)ntohl(fap->fa_type)),
1049 (u_int32_t)ntohl(fap->fa_mode),
1050 (u_int32_t)ntohl(fap->fa_uid),
1051 (u_int32_t) ntohl(fap->fa_gid));
1052 if (v3) {
1053 TCHECK(fap->fa3_size);
1054 printf(" sz ");
1055 print_int64((u_int32_t *)&fap->fa3_size, UNSIGNED);
1056 putchar(' ');
1057 } else {
1058 TCHECK(fap->fa2_size);
1059 printf(" sz %d ", (u_int32_t) ntohl(fap->fa2_size));
1060 }
1061 }
1062 /* print lots more stuff */
1063 if (verbose > 1) {
1064 if (v3) {
1065 TCHECK(fap->fa3_ctime);
1066 printf("nlink %d rdev %d/%d ",
1067 (u_int32_t)ntohl(fap->fa_nlink),
1068 (u_int32_t) ntohl(fap->fa3_rdev.specdata1),
1069 (u_int32_t) ntohl(fap->fa3_rdev.specdata2));
1070 printf("fsid ");
1071 print_int64((u_int32_t *)&fap->fa2_fsid, HEX);
1072 printf(" nodeid ");
1073 print_int64((u_int32_t *)&fap->fa2_fileid, HEX);
1074 printf(" a/m/ctime %u.%06u ",
1075 (u_int32_t) ntohl(fap->fa3_atime.nfsv3_sec),
1076 (u_int32_t) ntohl(fap->fa3_atime.nfsv3_nsec));
1077 printf("%u.%06u ",
1078 (u_int32_t) ntohl(fap->fa3_mtime.nfsv3_sec),
1079 (u_int32_t) ntohl(fap->fa3_mtime.nfsv3_nsec));
1080 printf("%u.%06u ",
1081 (u_int32_t) ntohl(fap->fa3_ctime.nfsv3_sec),
1082 (u_int32_t) ntohl(fap->fa3_ctime.nfsv3_nsec));
1083 } else {
1084 TCHECK(fap->fa2_ctime);
1085 printf("nlink %d rdev %x fsid %x nodeid %x a/m/ctime ",
1086 (u_int32_t) ntohl(fap->fa_nlink),
1087 (u_int32_t) ntohl(fap->fa2_rdev),
1088 (u_int32_t) ntohl(fap->fa2_fsid),
1089 (u_int32_t) ntohl(fap->fa2_fileid));
1090 printf("%u.%06u ",
1091 (u_int32_t) ntohl(fap->fa2_atime.nfsv2_sec),
1092 (u_int32_t) ntohl(fap->fa2_atime.nfsv2_usec));
1093 printf("%u.%06u ",
1094 (u_int32_t) ntohl(fap->fa2_mtime.nfsv2_sec),
1095 (u_int32_t) ntohl(fap->fa2_mtime.nfsv2_usec));
1096 printf("%u.%06u ",
1097 (u_int32_t) ntohl(fap->fa2_ctime.nfsv2_sec),
1098 (u_int32_t) ntohl(fap->fa2_ctime.nfsv2_usec));
1099 }
1100 }
1101 return ((const u_int32_t *)((unsigned char *)dp +
1102 (v3 ? NFSX_V3FATTR : NFSX_V2FATTR)));
1103 trunc:
1104 return (NULL);
1105 }
1106
1107 static int
1108 parseattrstat(const u_int32_t *dp, int verbose, int v3)
1109 {
1110 int er;
1111
1112 dp = parsestatus(dp, &er);
1113 if (dp == NULL || er)
1114 return (0);
1115
1116 return (parsefattr(dp, verbose, v3) != NULL);
1117 }
1118
1119 static int
1120 parsediropres(const u_int32_t *dp)
1121 {
1122 int er;
1123
1124 if (!(dp = parsestatus(dp, &er)) || er)
1125 return (0);
1126
1127 dp = parsefh(dp, 0);
1128 if (dp == NULL)
1129 return (0);
1130
1131 return (parsefattr(dp, vflag, 0) != NULL);
1132 }
1133
1134 static int
1135 parselinkres(const u_int32_t *dp, int v3)
1136 {
1137 int er;
1138
1139 dp = parsestatus(dp, &er);
1140 if (dp == NULL || er)
1141 return(0);
1142 if (v3 && !(dp = parse_post_op_attr(dp, vflag)))
1143 return (0);
1144 putchar(' ');
1145 return (parsefn(dp) != NULL);
1146 }
1147
1148 static int
1149 parsestatfs(const u_int32_t *dp, int v3)
1150 {
1151 const struct nfs_statfs *sfsp;
1152 int er;
1153
1154 dp = parsestatus(dp, &er);
1155 if (dp == NULL || (!v3 && er))
1156 return (0);
1157
1158 if (qflag)
1159 return(1);
1160
1161 if (v3) {
1162 if (vflag)
1163 printf(" POST:");
1164 if (!(dp = parse_post_op_attr(dp, vflag)))
1165 return (0);
1166 }
1167
1168 TCHECK2(dp, (v3 ? NFSX_V3STATFS : NFSX_V2STATFS));
1169
1170 sfsp = (const struct nfs_statfs *)dp;
1171
1172 if (v3) {
1173 printf(" tbytes ");
1174 print_int64((u_int32_t *)&sfsp->sf_tbytes, UNSIGNED);
1175 printf(" fbytes ");
1176 print_int64((u_int32_t *)&sfsp->sf_fbytes, UNSIGNED);
1177 printf(" abytes ");
1178 print_int64((u_int32_t *)&sfsp->sf_abytes, UNSIGNED);
1179 if (vflag) {
1180 printf(" tfiles ");
1181 print_int64((u_int32_t *)&sfsp->sf_tfiles, UNSIGNED);
1182 printf(" ffiles ");
1183 print_int64((u_int32_t *)&sfsp->sf_ffiles, UNSIGNED);
1184 printf(" afiles ");
1185 print_int64((u_int32_t *)&sfsp->sf_afiles, UNSIGNED);
1186 printf(" invar %u",
1187 (u_int32_t) ntohl(sfsp->sf_invarsec));
1188 }
1189 } else {
1190 printf(" tsize %d bsize %d blocks %d bfree %d bavail %d",
1191 (u_int32_t)ntohl(sfsp->sf_tsize),
1192 (u_int32_t)ntohl(sfsp->sf_bsize),
1193 (u_int32_t)ntohl(sfsp->sf_blocks),
1194 (u_int32_t)ntohl(sfsp->sf_bfree),
1195 (u_int32_t)ntohl(sfsp->sf_bavail));
1196 }
1197
1198 return (1);
1199 trunc:
1200 return (0);
1201 }
1202
1203 static int
1204 parserddires(const u_int32_t *dp)
1205 {
1206 int er;
1207
1208 dp = parsestatus(dp, &er);
1209 if (dp == NULL || er)
1210 return (0);
1211 if (qflag)
1212 return (1);
1213
1214 TCHECK(dp[2]);
1215 printf(" offset %x size %d ",
1216 (u_int32_t)ntohl(dp[0]), (u_int32_t)ntohl(dp[1]));
1217 if (dp[2] != 0)
1218 printf(" eof");
1219
1220 return (1);
1221 trunc:
1222 return (0);
1223 }
1224
1225 static const u_int32_t *
1226 parse_wcc_attr(const u_int32_t *dp)
1227 {
1228 printf(" sz ");
1229 print_int64(dp, UNSIGNED);
1230 printf(" mtime %u.%06u ctime %u.%06u",
1231 (u_int32_t)ntohl(dp[2]), (u_int32_t)ntohl(dp[3]),
1232 (u_int32_t)ntohl(dp[4]), (u_int32_t)ntohl(dp[5]));
1233 return (dp + 6);
1234 }
1235
1236 /*
1237 * Pre operation attributes. Print only if vflag > 1.
1238 */
1239 static const u_int32_t *
1240 parse_pre_op_attr(const u_int32_t *dp, int verbose)
1241 {
1242 TCHECK(dp[0]);
1243 if (!ntohl(dp[0]))
1244 return (dp + 1);
1245 dp++;
1246 TCHECK2(dp, 24);
1247 if (verbose > 1) {
1248 return parse_wcc_attr(dp);
1249 } else {
1250 /* If not verbose enough, just skip over wcc_attr */
1251 return (dp + 6);
1252 }
1253 trunc:
1254 return (NULL);
1255 }
1256
1257 /*
1258 * Post operation attributes are printed if vflag >= 1
1259 */
1260 static const u_int32_t *
1261 parse_post_op_attr(const u_int32_t *dp, int verbose)
1262 {
1263 TCHECK(dp[0]);
1264 if (!ntohl(dp[0]))
1265 return (dp + 1);
1266 dp++;
1267 if (verbose) {
1268 return parsefattr(dp, verbose, 1);
1269 } else
1270 return (dp + (NFSX_V3FATTR / sizeof (u_int32_t)));
1271 trunc:
1272 return (NULL);
1273 }
1274
1275 static const u_int32_t *
1276 parse_wcc_data(const u_int32_t *dp, int verbose)
1277 {
1278 if (verbose > 1)
1279 printf(" PRE:");
1280 if (!(dp = parse_pre_op_attr(dp, verbose)))
1281 return (0);
1282
1283 if (verbose)
1284 printf(" POST:");
1285 return parse_post_op_attr(dp, verbose);
1286 }
1287
1288 static const u_int32_t *
1289 parsecreateopres(const u_int32_t *dp, int verbose)
1290 {
1291 int er;
1292
1293 if (!(dp = parsestatus(dp, &er)))
1294 return (0);
1295 if (er)
1296 dp = parse_wcc_data(dp, verbose);
1297 else {
1298 TCHECK(dp[0]);
1299 if (!ntohl(dp[0]))
1300 return (dp + 1);
1301 dp++;
1302 if (!(dp = parsefh(dp, 1)))
1303 return (0);
1304 if (verbose) {
1305 if (!(dp = parse_post_op_attr(dp, verbose)))
1306 return (0);
1307 if (vflag > 1) {
1308 printf("dir attr:");
1309 dp = parse_wcc_data(dp, verbose);
1310 }
1311 }
1312 }
1313 return (dp);
1314 trunc:
1315 return (NULL);
1316 }
1317
1318 static int
1319 parsewccres(const u_int32_t *dp, int verbose)
1320 {
1321 int er;
1322
1323 if (!(dp = parsestatus(dp, &er)))
1324 return (0);
1325 return parse_wcc_data(dp, verbose) != 0;
1326 }
1327
1328 static const u_int32_t *
1329 parsev3rddirres(const u_int32_t *dp, int verbose)
1330 {
1331 int er;
1332
1333 if (!(dp = parsestatus(dp, &er)))
1334 return (0);
1335 if (vflag)
1336 printf(" POST:");
1337 if (!(dp = parse_post_op_attr(dp, verbose)))
1338 return (0);
1339 if (er)
1340 return dp;
1341 if (vflag) {
1342 TCHECK(dp[1]);
1343 printf(" verf %08x%08x", dp[0], dp[1]);
1344 dp += 2;
1345 }
1346 return dp;
1347 trunc:
1348 return (NULL);
1349 }
1350
1351 static int
1352 parsefsinfo(const u_int32_t *dp)
1353 {
1354 struct nfsv3_fsinfo *sfp;
1355 int er;
1356
1357 if (!(dp = parsestatus(dp, &er)))
1358 return (0);
1359 if (vflag)
1360 printf(" POST:");
1361 if (!(dp = parse_post_op_attr(dp, vflag)))
1362 return (0);
1363 if (er)
1364 return (1);
1365
1366 sfp = (struct nfsv3_fsinfo *)dp;
1367 TCHECK(*sfp);
1368 printf(" rtmax %u rtpref %u wtmax %u wtpref %u dtpref %u",
1369 (u_int32_t) ntohl(sfp->fs_rtmax),
1370 (u_int32_t) ntohl(sfp->fs_rtpref),
1371 (u_int32_t) ntohl(sfp->fs_wtmax),
1372 (u_int32_t) ntohl(sfp->fs_wtpref),
1373 (u_int32_t) ntohl(sfp->fs_dtpref));
1374 if (vflag) {
1375 printf(" rtmult %u wtmult %u maxfsz ",
1376 (u_int32_t) ntohl(sfp->fs_rtmult),
1377 (u_int32_t) ntohl(sfp->fs_wtmult));
1378 print_int64((u_int32_t *)&sfp->fs_maxfilesize, UNSIGNED);
1379 printf(" delta %u.%06u ",
1380 (u_int32_t) ntohl(sfp->fs_timedelta.nfsv3_sec),
1381 (u_int32_t) ntohl(sfp->fs_timedelta.nfsv3_nsec));
1382 }
1383 return (0);
1384 trunc:
1385 return (1);
1386 }
1387
1388 static int
1389 parsepathconf(const u_int32_t *dp)
1390 {
1391 int er;
1392 struct nfsv3_pathconf *spp;
1393
1394 if (!(dp = parsestatus(dp, &er)))
1395 return (0);
1396 if (vflag)
1397 printf(" POST:");
1398 if (!(dp = parse_post_op_attr(dp, vflag)))
1399 return (0);
1400 if (er)
1401 return (1);
1402
1403 spp = (struct nfsv3_pathconf *)dp;
1404 TCHECK(*spp);
1405
1406 printf(" linkmax %u namemax %u %s %s %s %s",
1407 (u_int32_t) ntohl(spp->pc_linkmax),
1408 (u_int32_t) ntohl(spp->pc_namemax),
1409 ntohl(spp->pc_notrunc) ? "notrunc" : "",
1410 ntohl(spp->pc_chownrestricted) ? "chownres" : "",
1411 ntohl(spp->pc_caseinsensitive) ? "igncase" : "",
1412 ntohl(spp->pc_casepreserving) ? "keepcase" : "");
1413 return (0);
1414 trunc:
1415 return (1);
1416 }
1417
1418 static void
1419 interp_reply(const struct rpc_msg *rp, u_int32_t proc, u_int32_t vers, int length)
1420 {
1421 register const u_int32_t *dp;
1422 register int v3;
1423 int er;
1424
1425 v3 = (vers == NFS_VER3);
1426
1427 if (!v3 && proc < NFS_NPROCS)
1428 proc = nfsv3_procid[proc];
1429
1430 switch (proc) {
1431
1432 case NFSPROC_NOOP:
1433 printf(" nop");
1434 return;
1435
1436 case NFSPROC_NULL:
1437 printf(" null");
1438 return;
1439
1440 case NFSPROC_GETATTR:
1441 printf(" getattr");
1442 dp = parserep(rp, length);
1443 if (dp != NULL && parseattrstat(dp, !qflag, v3) != 0)
1444 return;
1445 break;
1446
1447 case NFSPROC_SETATTR:
1448 printf(" setattr");
1449 if (!(dp = parserep(rp, length)))
1450 return;
1451 if (v3) {
1452 if (parsewccres(dp, vflag))
1453 return;
1454 } else {
1455 if (parseattrstat(dp, !qflag, 0) != 0)
1456 return;
1457 }
1458 break;
1459
1460 case NFSPROC_LOOKUP:
1461 printf(" lookup");
1462 if (!(dp = parserep(rp, length)))
1463 break;
1464 if (v3) {
1465 if (!(dp = parsestatus(dp, &er)))
1466 break;
1467 if (er) {
1468 if (vflag > 1) {
1469 printf(" post dattr:");
1470 dp = parse_post_op_attr(dp, vflag);
1471 }
1472 } else {
1473 if (!(dp = parsefh(dp, v3)))
1474 break;
1475 if ((dp = parse_post_op_attr(dp, vflag)) &&
1476 vflag > 1) {
1477 printf(" post dattr:");
1478 dp = parse_post_op_attr(dp, vflag);
1479 }
1480 }
1481 if (dp)
1482 return;
1483 } else {
1484 if (parsediropres(dp) != 0)
1485 return;
1486 }
1487 break;
1488
1489 case NFSPROC_ACCESS:
1490 printf(" access");
1491 if (!(dp = parserep(rp, length)))
1492 break;
1493 if (!(dp = parsestatus(dp, &er)))
1494 break;
1495 if (vflag)
1496 printf(" attr:");
1497 if (!(dp = parse_post_op_attr(dp, vflag)))
1498 break;
1499 if (!er)
1500 printf(" c %04x", (u_int32_t)ntohl(dp[0]));
1501 return;
1502
1503 case NFSPROC_READLINK:
1504 printf(" readlink");
1505 dp = parserep(rp, length);
1506 if (dp != NULL && parselinkres(dp, v3) != 0)
1507 return;
1508 break;
1509
1510 case NFSPROC_READ:
1511 printf(" read");
1512 if (!(dp = parserep(rp, length)))
1513 break;
1514 if (v3) {
1515 if (!(dp = parsestatus(dp, &er)))
1516 break;
1517 if (!(dp = parse_post_op_attr(dp, vflag)))
1518 break;
1519 if (er)
1520 return;
1521 if (vflag) {
1522 TCHECK(dp[1]);
1523 printf("%u bytes", (u_int32_t) ntohl(dp[0]));
1524 if (ntohl(dp[1]))
1525 printf(" EOF");
1526 }
1527 return;
1528 } else {
1529 if (parseattrstat(dp, vflag, 0) != 0)
1530 return;
1531 }
1532 break;
1533
1534 case NFSPROC_WRITE:
1535 printf(" write");
1536 if (!(dp = parserep(rp, length)))
1537 break;
1538 if (v3) {
1539 if (!(dp = parsestatus(dp, &er)))
1540 break;
1541 if (!(dp = parse_wcc_data(dp, vflag)))
1542 break;
1543 if (er)
1544 return;
1545 if (vflag) {
1546 TCHECK(dp[0]);
1547 printf("%u bytes", (u_int32_t) ntohl(dp[0]));
1548 if (vflag > 1) {
1549 TCHECK(dp[1]);
1550 printf(" <%s>",
1551 tok2str(nfsv3_writemodes,
1552 NULL, ntohl(dp[1])));
1553 }
1554 return;
1555 }
1556 } else {
1557 if (parseattrstat(dp, vflag, v3) != 0)
1558 return;
1559 }
1560 break;
1561
1562 case NFSPROC_CREATE:
1563 printf(" create");
1564 if (!(dp = parserep(rp, length)))
1565 break;
1566 if (v3) {
1567 if (parsecreateopres(dp, vflag) != 0)
1568 return;
1569 } else {
1570 if (parsediropres(dp) != 0)
1571 return;
1572 }
1573 break;
1574
1575 case NFSPROC_MKDIR:
1576 printf(" mkdir");
1577 if (!(dp = parserep(rp, length)))
1578 break;
1579 if (v3) {
1580 if (parsecreateopres(dp, vflag) != 0)
1581 return;
1582 } else {
1583 if (parsediropres(dp) != 0)
1584 return;
1585 }
1586 break;
1587
1588 case NFSPROC_SYMLINK:
1589 printf(" symlink");
1590 if (!(dp = parserep(rp, length)))
1591 break;
1592 if (v3) {
1593 if (parsecreateopres(dp, vflag) != 0)
1594 return;
1595 } else {
1596 if (parsestatus(dp, &er) != 0)
1597 return;
1598 }
1599 break;
1600
1601 case NFSPROC_MKNOD:
1602 printf(" mknod");
1603 if (!(dp = parserep(rp, length)))
1604 break;
1605 if (parsecreateopres(dp, vflag) != 0)
1606 return;
1607 break;
1608
1609 case NFSPROC_REMOVE:
1610 printf(" remove");
1611 if (!(dp = parserep(rp, length)))
1612 break;
1613 if (v3) {
1614 if (parsewccres(dp, vflag))
1615 return;
1616 } else {
1617 if (parsestatus(dp, &er) != 0)
1618 return;
1619 }
1620 break;
1621
1622 case NFSPROC_RMDIR:
1623 printf(" rmdir");
1624 if (!(dp = parserep(rp, length)))
1625 break;
1626 if (v3) {
1627 if (parsewccres(dp, vflag))
1628 return;
1629 } else {
1630 if (parsestatus(dp, &er) != 0)
1631 return;
1632 }
1633 break;
1634
1635 case NFSPROC_RENAME:
1636 printf(" rename");
1637 if (!(dp = parserep(rp, length)))
1638 break;
1639 if (v3) {
1640 if (!(dp = parsestatus(dp, &er)))
1641 break;
1642 if (vflag) {
1643 printf(" from:");
1644 if (!(dp = parse_wcc_data(dp, vflag)))
1645 break;
1646 printf(" to:");
1647 if (!(dp = parse_wcc_data(dp, vflag)))
1648 break;
1649 }
1650 return;
1651 } else {
1652 if (parsestatus(dp, &er) != 0)
1653 return;
1654 }
1655 break;
1656
1657 case NFSPROC_LINK:
1658 printf(" link");
1659 if (!(dp = parserep(rp, length)))
1660 break;
1661 if (v3) {
1662 if (!(dp = parsestatus(dp, &er)))
1663 break;
1664 if (vflag) {
1665 printf(" file POST:");
1666 if (!(dp = parse_post_op_attr(dp, vflag)))
1667 break;
1668 printf(" dir:");
1669 if (!(dp = parse_wcc_data(dp, vflag)))
1670 break;
1671 return;
1672 }
1673 } else {
1674 if (parsestatus(dp, &er) != 0)
1675 return;
1676 }
1677 break;
1678
1679 case NFSPROC_READDIR:
1680 printf(" readdir");
1681 if (!(dp = parserep(rp, length)))
1682 break;
1683 if (v3) {
1684 if (parsev3rddirres(dp, vflag))
1685 return;
1686 } else {
1687 if (parserddires(dp) != 0)
1688 return;
1689 }
1690 break;
1691
1692 case NFSPROC_READDIRPLUS:
1693 printf(" readdirplus");
1694 if (!(dp = parserep(rp, length)))
1695 break;
1696 if (parsev3rddirres(dp, vflag))
1697 return;
1698 break;
1699
1700 case NFSPROC_FSSTAT:
1701 printf(" fsstat");
1702 dp = parserep(rp, length);
1703 if (dp != NULL && parsestatfs(dp, v3) != 0)
1704 return;
1705 break;
1706
1707 case NFSPROC_FSINFO:
1708 printf(" fsinfo");
1709 dp = parserep(rp, length);
1710 if (dp != NULL && parsefsinfo(dp) != 0)
1711 return;
1712 break;
1713
1714 case NFSPROC_PATHCONF:
1715 printf(" pathconf");
1716 dp = parserep(rp, length);
1717 if (dp != NULL && parsepathconf(dp) != 0)
1718 return;
1719 break;
1720
1721 case NFSPROC_COMMIT:
1722 printf(" commit");
1723 dp = parserep(rp, length);
1724 if (dp != NULL && parsewccres(dp, vflag) != 0)
1725 return;
1726 break;
1727
1728 default:
1729 printf(" proc-%u", proc);
1730 return;
1731 }
1732 trunc:
1733 if (!nfserr)
1734 fputs(" [|nfs]", stdout);
1735 }