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