]> The Tcpdump Group git mirrors - tcpdump/blob - print-nfs.c
395107bfecac36e956c04eea5e5578a7e9cedaa3
[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.82 2000-09-23 08:03:38 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 #include <net/if.h>
38
39 #include <netinet/in.h>
40 #include <netinet/in_systm.h>
41 #include <netinet/ip.h>
42 #ifdef INET6
43 #include <netinet/ip6.h>
44 #endif
45
46 #include <rpc/rpc.h>
47
48 #include <ctype.h>
49 #include <pcap.h>
50 #include <stdio.h>
51 #include <string.h>
52
53 #include "interface.h"
54 #include "addrtoname.h"
55
56 #include "nfs.h"
57 #include "nfsfh.h"
58
59 static void nfs_printfh(const u_int32_t *, const u_int);
60 static void xid_map_enter(const struct rpc_msg *, const u_char *);
61 static int32_t xid_map_find(const struct rpc_msg *, const u_char *,
62 u_int32_t *, u_int32_t *);
63 static void interp_reply(const struct rpc_msg *, u_int32_t, u_int32_t, int);
64 static const u_int32_t *parse_post_op_attr(const u_int32_t *, int);
65 static void print_sattr3(const struct nfsv3_sattr *sa3, int verbose);
66 static int print_int64(const u_int32_t *dp, int how);
67 static void print_nfsaddr(const u_char *, const char *, const char *);
68
69 /*
70 * Mapping of old NFS Version 2 RPC numbers to generic numbers.
71 */
72 u_int32_t nfsv3_procid[NFS_NPROCS] = {
73 NFSPROC_NULL,
74 NFSPROC_GETATTR,
75 NFSPROC_SETATTR,
76 NFSPROC_NOOP,
77 NFSPROC_LOOKUP,
78 NFSPROC_READLINK,
79 NFSPROC_READ,
80 NFSPROC_NOOP,
81 NFSPROC_WRITE,
82 NFSPROC_CREATE,
83 NFSPROC_REMOVE,
84 NFSPROC_RENAME,
85 NFSPROC_LINK,
86 NFSPROC_SYMLINK,
87 NFSPROC_MKDIR,
88 NFSPROC_RMDIR,
89 NFSPROC_READDIR,
90 NFSPROC_FSSTAT,
91 NFSPROC_NOOP,
92 NFSPROC_NOOP,
93 NFSPROC_NOOP,
94 NFSPROC_NOOP,
95 NFSPROC_NOOP,
96 NFSPROC_NOOP,
97 NFSPROC_NOOP,
98 NFSPROC_NOOP
99 };
100
101 /*
102 * NFS V2 and V3 status values.
103 *
104 * Some of these come from the RFCs for NFS V2 and V3, with the message
105 * strings taken from the FreeBSD C library "errlst.c".
106 *
107 * Others are errors that are not in the RFC but that I suspect some
108 * NFS servers could return; the values are FreeBSD errno values, as
109 * the first NFS server was the SunOS 2.0 one, and until 5.0 SunOS
110 * was primarily BSD-derived.
111 */
112 static struct tok status2str[] = {
113 { 1, "Operation not permitted" }, /* EPERM */
114 { 2, "No such file or directory" }, /* ENOENT */
115 { 5, "Input/output error" }, /* EIO */
116 { 6, "Device not configured" }, /* ENXIO */
117 { 11, "Resource deadlock avoided" }, /* EDEADLK */
118 { 12, "Cannot allocate memory" }, /* ENOMEM */
119 { 13, "Permission denied" }, /* EACCES */
120 { 17, "File exists" }, /* EEXIST */
121 { 18, "Cross-device link" }, /* EXDEV */
122 { 19, "Operation not supported by device" }, /* ENODEV */
123 { 20, "Not a directory" }, /* ENOTDIR */
124 { 21, "Is a directory" }, /* EISDIR */
125 { 22, "Invalid argument" }, /* EINVAL */
126 { 26, "Text file busy" }, /* ETXTBSY */
127 { 27, "File too large" }, /* EFBIG */
128 { 28, "No space left on device" }, /* ENOSPC */
129 { 30, "Read-only file system" }, /* EROFS */
130 { 31, "Too many links" }, /* EMLINK */
131 { 45, "Operation not supported" }, /* EOPNOTSUPP */
132 { 62, "Too many levels of symbolic links" }, /* ELOOP */
133 { 63, "File name too long" }, /* ENAMETOOLONG */
134 { 66, "Directory not empty" }, /* ENOTEMPTY */
135 { 69, "Disc quota exceeded" }, /* EDQUOT */
136 { 70, "Stale NFS file handle" }, /* ESTALE */
137 { 71, "Too many levels of remote in path" }, /* EREMOTE */
138 { 99, "Write cache flushed to disk" }, /* NFSERR_WFLUSH (not used) */
139 { 10001, "Illegal NFS file handle" }, /* NFS3ERR_BADHANDLE */
140 { 10002, "Update synchronization mismatch" }, /* NFS3ERR_NOT_SYNC */
141 { 10003, "READDIR/READDIRPLUS cookie is stale" }, /* NFS3ERR_BAD_COOKIE */
142 { 10004, "Operation not supported" }, /* NFS3ERR_NOTSUPP */
143 { 10005, "Buffer or request is too small" }, /* NFS3ERR_TOOSMALL */
144 { 10006, "Unspecified error on server" }, /* NFS3ERR_SERVERFAULT */
145 { 10007, "Object of that type not supported" }, /* NFS3ERR_BADTYPE */
146 { 10008, "Request couldn't be completed in time" }, /* NFS3ERR_JUKEBOX */
147 { 0, NULL }
148 };
149
150 static struct tok nfsv3_writemodes[] = {
151 { 0, "unstable" },
152 { 1, "datasync" },
153 { 2, "filesync" },
154 { 0, NULL }
155 };
156
157 static struct tok type2str[] = {
158 { NFNON, "NON" },
159 { NFREG, "REG" },
160 { NFDIR, "DIR" },
161 { NFBLK, "BLK" },
162 { NFCHR, "CHR" },
163 { NFLNK, "LNK" },
164 { NFFIFO, "FIFO" },
165 { 0, NULL }
166 };
167
168 /*
169 * Print out a 64-bit integer. This appears to be different on each system,
170 * try to make the best of it. The integer stored as 2 consecutive XDR
171 * encoded 32-bit integers, to which a pointer is passed.
172 *
173 * Assume that a system that has INT64_FORMAT defined, has a 64-bit
174 * integer datatype and can print it.
175 */
176
177 #define UNSIGNED 0
178 #define SIGNED 1
179 #define HEX 2
180
181 static int print_int64(const u_int32_t *dp, int how)
182 {
183 #ifdef INT64_FORMAT
184 u_int64_t res;
185
186 res = ((u_int64_t)ntohl(dp[0]) << 32) | (u_int64_t)ntohl(dp[1]);
187 switch (how) {
188 case SIGNED:
189 printf(INT64_FORMAT, res);
190 break;
191 case UNSIGNED:
192 printf(U_INT64_FORMAT, res);
193 break;
194 case HEX:
195 printf(HEX_INT64_FORMAT, res);
196 break;
197 default:
198 return (0);
199 }
200 #else
201 switch (how) {
202 case SIGNED:
203 case UNSIGNED:
204 case HEX:
205 printf("0x%x%08x", (u_int32_t)ntohl(dp[0]),
206 (u_int32_t)ntohl(dp[1]));
207 break;
208 default:
209 return (0);
210 }
211 #endif
212 return 1;
213 }
214
215 static void
216 print_nfsaddr(const u_char *bp, const char *s, const char *d)
217 {
218 struct ip *ip;
219 #ifdef INET6
220 struct ip6_hdr *ip6;
221 char srcaddr[INET6_ADDRSTRLEN], dstaddr[INET6_ADDRSTRLEN];
222 #else
223 #ifndef INET_ADDRSTRLEN
224 #define INET_ADDRSTRLEN 16
225 #endif
226 char srcaddr[INET_ADDRSTRLEN], dstaddr[INET_ADDRSTRLEN];
227 #endif
228
229 srcaddr[0] = dstaddr[0] = '\0';
230 switch (((struct ip *)bp)->ip_v) {
231 case 4:
232 ip = (struct ip *)bp;
233 strlcpy(srcaddr, ipaddr_string(&ip->ip_src), sizeof(srcaddr));
234 strlcpy(dstaddr, ipaddr_string(&ip->ip_dst), sizeof(dstaddr));
235 break;
236 #ifdef INET6
237 case 6:
238 ip6 = (struct ip6_hdr *)bp;
239 strlcpy(srcaddr, ip6addr_string(&ip6->ip6_src),
240 sizeof(srcaddr));
241 strlcpy(dstaddr, ip6addr_string(&ip6->ip6_dst),
242 sizeof(dstaddr));
243 break;
244 #endif
245 default:
246 strlcpy(srcaddr, "?", sizeof(srcaddr));
247 strlcpy(dstaddr, "?", sizeof(dstaddr));
248 break;
249 }
250
251 (void)printf("%s.%s > %s.%s: ", srcaddr, s, dstaddr, d);
252 }
253
254 static const u_int32_t *
255 parse_sattr3(const u_int32_t *dp, struct nfsv3_sattr *sa3)
256 {
257 TCHECK(dp[0]);
258 if ((sa3->sa_modeset = ntohl(*dp++))) {
259 TCHECK(dp[0]);
260 sa3->sa_mode = ntohl(*dp++);
261 }
262
263 TCHECK(dp[0]);
264 if ((sa3->sa_uidset = ntohl(*dp++))) {
265 TCHECK(dp[0]);
266 sa3->sa_uid = ntohl(*dp++);
267 }
268
269 TCHECK(dp[0]);
270 if ((sa3->sa_gidset = ntohl(*dp++))) {
271 TCHECK(dp[0]);
272 sa3->sa_gid = ntohl(*dp++);
273 }
274
275 TCHECK(dp[0]);
276 if ((sa3->sa_sizeset = ntohl(*dp++))) {
277 TCHECK(dp[0]);
278 sa3->sa_size = ntohl(*dp++);
279 }
280
281 TCHECK(dp[0]);
282 if ((sa3->sa_atimetype = ntohl(*dp++)) == NFSV3SATTRTIME_TOCLIENT) {
283 TCHECK(dp[1]);
284 sa3->sa_atime.nfsv3_sec = ntohl(*dp++);
285 sa3->sa_atime.nfsv3_nsec = ntohl(*dp++);
286 }
287
288 TCHECK(dp[0]);
289 if ((sa3->sa_mtimetype = ntohl(*dp++)) == NFSV3SATTRTIME_TOCLIENT) {
290 TCHECK(dp[1]);
291 sa3->sa_mtime.nfsv3_sec = ntohl(*dp++);
292 sa3->sa_mtime.nfsv3_nsec = ntohl(*dp++);
293 }
294
295 return dp;
296 trunc:
297 return NULL;
298 }
299
300 static int nfserr; /* true if we error rather than trunc */
301
302 static void
303 print_sattr3(const struct nfsv3_sattr *sa3, int verbose)
304 {
305 if (sa3->sa_modeset)
306 printf(" mode %o", sa3->sa_mode);
307 if (sa3->sa_uidset)
308 printf(" uid %u", sa3->sa_uid);
309 if (sa3->sa_gidset)
310 printf(" gid %u", sa3->sa_gid);
311 if (verbose > 1) {
312 if (sa3->sa_atimetype == NFSV3SATTRTIME_TOCLIENT)
313 printf(" atime %u.%06u", sa3->sa_atime.nfsv3_sec,
314 sa3->sa_atime.nfsv3_nsec);
315 if (sa3->sa_mtimetype == NFSV3SATTRTIME_TOCLIENT)
316 printf(" mtime %u.%06u", sa3->sa_mtime.nfsv3_sec,
317 sa3->sa_mtime.nfsv3_nsec);
318 }
319 }
320
321 void
322 nfsreply_print(register const u_char *bp, u_int length,
323 register const u_char *bp2)
324 {
325 register const struct rpc_msg *rp;
326 u_int32_t proc, vers;
327 char srcid[20], dstid[20]; /*fits 32bit*/
328
329 nfserr = 0; /* assume no error */
330 rp = (const struct rpc_msg *)bp;
331
332 if (!nflag) {
333 strlcpy(srcid, "nfs", sizeof(srcid));
334 snprintf(dstid, sizeof(dstid), "%u",
335 (u_int32_t)ntohl(rp->rm_xid));
336 } else {
337 snprintf(srcid, sizeof(srcid), "%u", NFS_PORT);
338 snprintf(dstid, sizeof(dstid), "%u",
339 (u_int32_t)ntohl(rp->rm_xid));
340 }
341 print_nfsaddr(bp2, srcid, dstid);
342 (void)printf("reply %s %d",
343 ntohl(rp->rm_reply.rp_stat) == MSG_ACCEPTED?
344 "ok":"ERR",
345 length);
346
347 if (xid_map_find(rp, bp2, &proc, &vers) >= 0)
348 interp_reply(rp, proc, vers, length);
349 }
350
351 /*
352 * Return a pointer to the first file handle in the packet.
353 * If the packet was truncated, return 0.
354 */
355 static const u_int32_t *
356 parsereq(register const struct rpc_msg *rp, register u_int length)
357 {
358 register const u_int32_t *dp;
359 register u_int len;
360
361 /*
362 * find the start of the req data (if we captured it)
363 */
364 dp = (u_int32_t *)&rp->rm_call.cb_cred;
365 TCHECK(dp[1]);
366 len = ntohl(dp[1]);
367 if (len < length) {
368 dp += (len + (2 * sizeof(*dp) + 3)) / sizeof(*dp);
369 TCHECK(dp[1]);
370 len = ntohl(dp[1]);
371 if (len < length) {
372 dp += (len + (2 * sizeof(*dp) + 3)) / sizeof(*dp);
373 TCHECK2(dp[0], 0);
374 return (dp);
375 }
376 }
377 trunc:
378 return (NULL);
379 }
380
381 /*
382 * Print out an NFS file handle and return a pointer to following word.
383 * If packet was truncated, return 0.
384 */
385 static const u_int32_t *
386 parsefh(register const u_int32_t *dp, int v3)
387 {
388 int len;
389
390 if (v3) {
391 TCHECK(dp[0]);
392 len = (int)ntohl(*dp) / 4;
393 dp++;
394 } else
395 len = NFSX_V2FH / 4;
396
397 if (TTEST2(*dp, len * sizeof(*dp))) {
398 nfs_printfh(dp, len);
399 return (dp + len);
400 }
401 trunc:
402 return (NULL);
403 }
404
405 /*
406 * Print out a file name and return pointer to 32-bit word past it.
407 * If packet was truncated, return 0.
408 */
409 static const u_int32_t *
410 parsefn(register const u_int32_t *dp)
411 {
412 register u_int32_t len;
413 register const u_char *cp;
414
415 /* Bail if we don't have the string length */
416 TCHECK(*dp);
417
418 /* Fetch string length; convert to host order */
419 len = *dp++;
420 NTOHL(len);
421
422 TCHECK2(*dp, ((len + 3) & ~3));
423
424 cp = (u_char *)dp;
425 /* Update 32-bit pointer (NFS filenames padded to 32-bit boundaries) */
426 dp += ((len + 3) & ~3) / sizeof(*dp);
427 /* XXX seems like we should be checking the length */
428 putchar('"');
429 (void) fn_printn(cp, len, NULL);
430 putchar('"');
431
432 return (dp);
433 trunc:
434 return NULL;
435 }
436
437 /*
438 * Print out file handle and file name.
439 * Return pointer to 32-bit word past file name.
440 * If packet was truncated (or there was some other error), return 0.
441 */
442 static const u_int32_t *
443 parsefhn(register const u_int32_t *dp, int v3)
444 {
445 dp = parsefh(dp, v3);
446 if (dp == NULL)
447 return (NULL);
448 putchar(' ');
449 return (parsefn(dp));
450 }
451
452 void
453 nfsreq_print(register const u_char *bp, u_int length,
454 register const u_char *bp2)
455 {
456 register const struct rpc_msg *rp;
457 register const u_int32_t *dp;
458 nfs_type type;
459 int v3;
460 u_int32_t proc;
461 struct nfsv3_sattr sa3;
462 char srcid[20], dstid[20]; /*fits 32bit*/
463
464 nfserr = 0; /* assume no error */
465 rp = (const struct rpc_msg *)bp;
466 if (!nflag) {
467 snprintf(srcid, sizeof(srcid), "%u",
468 (u_int32_t)ntohl(rp->rm_xid));
469 strlcpy(dstid, "nfs", sizeof(dstid));
470 } else {
471 snprintf(srcid, sizeof(srcid), "%u",
472 (u_int32_t)ntohl(rp->rm_xid));
473 snprintf(dstid, sizeof(dstid), "%u", NFS_PORT);
474 }
475 print_nfsaddr(bp2, srcid, dstid);
476 (void)printf("%d", length);
477
478 xid_map_enter(rp, bp2); /* record proc number for later on */
479
480 v3 = (ntohl(rp->rm_call.cb_vers) == NFS_VER3);
481 proc = ntohl(rp->rm_call.cb_proc);
482
483 if (!v3 && proc < NFS_NPROCS)
484 proc = nfsv3_procid[proc];
485
486 switch (proc) {
487 case NFSPROC_NOOP:
488 printf(" nop");
489 return;
490 case NFSPROC_NULL:
491 printf(" null");
492 return;
493
494 case NFSPROC_GETATTR:
495 printf(" getattr");
496 if ((dp = parsereq(rp, length)) != NULL &&
497 parsefh(dp, v3) != NULL)
498 return;
499 break;
500
501 case NFSPROC_SETATTR:
502 printf(" setattr");
503 if ((dp = parsereq(rp, length)) != NULL &&
504 parsefh(dp, v3) != NULL)
505 return;
506 break;
507
508 case NFSPROC_LOOKUP:
509 printf(" lookup");
510 if ((dp = parsereq(rp, length)) != NULL &&
511 parsefhn(dp, v3) != NULL)
512 return;
513 break;
514
515 case NFSPROC_ACCESS:
516 printf(" access");
517 if ((dp = parsereq(rp, length)) != NULL &&
518 (dp = parsefh(dp, v3)) != NULL) {
519 TCHECK(dp[0]);
520 printf(" %04x", (u_int32_t)ntohl(dp[0]));
521 return;
522 }
523 break;
524
525 case NFSPROC_READLINK:
526 printf(" readlink");
527 if ((dp = parsereq(rp, length)) != NULL &&
528 parsefh(dp, v3) != NULL)
529 return;
530 break;
531
532 case NFSPROC_READ:
533 printf(" read");
534 if ((dp = parsereq(rp, length)) != NULL &&
535 (dp = parsefh(dp, v3)) != NULL) {
536 if (v3) {
537 TCHECK(dp[2]);
538 printf(" %u bytes @ ",
539 (u_int32_t) ntohl(dp[2]));
540 print_int64(dp, UNSIGNED);
541 } else {
542 TCHECK(dp[1]);
543 printf(" %u bytes @ %u",
544 (u_int32_t)ntohl(dp[1]),
545 (u_int32_t)ntohl(dp[0]));
546 }
547 return;
548 }
549 break;
550
551 case NFSPROC_WRITE:
552 printf(" write");
553 if ((dp = parsereq(rp, length)) != NULL &&
554 (dp = parsefh(dp, v3)) != NULL) {
555 if (v3) {
556 TCHECK(dp[4]);
557 printf(" %u bytes @ ",
558 (u_int32_t) ntohl(dp[4]));
559 print_int64(dp, UNSIGNED);
560 if (vflag) {
561 dp += 3;
562 TCHECK(dp[0]);
563 printf(" <%s>",
564 tok2str(nfsv3_writemodes,
565 NULL, ntohl(*dp)));
566 }
567 } else {
568 TCHECK(dp[3]);
569 printf(" %u (%u) bytes @ %u (%u)",
570 (u_int32_t)ntohl(dp[3]),
571 (u_int32_t)ntohl(dp[2]),
572 (u_int32_t)ntohl(dp[1]),
573 (u_int32_t)ntohl(dp[0]));
574 }
575 return;
576 }
577 break;
578
579 case NFSPROC_CREATE:
580 printf(" create");
581 if ((dp = parsereq(rp, length)) != NULL &&
582 parsefhn(dp, v3) != NULL)
583 return;
584 break;
585
586 case NFSPROC_MKDIR:
587 printf(" mkdir");
588 if ((dp = parsereq(rp, length)) != 0 && parsefhn(dp, v3) != 0)
589 return;
590 break;
591
592 case NFSPROC_SYMLINK:
593 printf(" symlink");
594 if ((dp = parsereq(rp, length)) != 0 &&
595 (dp = parsefhn(dp, v3)) != 0) {
596 fputs(" ->", stdout);
597 if (v3 && (dp = parse_sattr3(dp, &sa3)) == 0)
598 break;
599 if (parsefn(dp) == 0)
600 break;
601 if (v3 && vflag)
602 print_sattr3(&sa3, vflag);
603 return;
604 }
605 break;
606
607 case NFSPROC_MKNOD:
608 printf(" mknod");
609 if ((dp = parsereq(rp, length)) != 0 &&
610 (dp = parsefhn(dp, v3)) != 0) {
611 TCHECK(*dp);
612 type = (nfs_type)ntohl(*dp++);
613 if ((dp = parse_sattr3(dp, &sa3)) == 0)
614 break;
615 printf(" %s", tok2str(type2str, "unk-ft %d", type));
616 if (vflag && (type == NFCHR || type == NFBLK)) {
617 TCHECK(dp[1]);
618 printf(" %u/%u",
619 (u_int32_t)ntohl(dp[0]),
620 (u_int32_t)ntohl(dp[1]));
621 dp += 2;
622 }
623 if (vflag)
624 print_sattr3(&sa3, vflag);
625 return;
626 }
627 break;
628
629 case NFSPROC_REMOVE:
630 printf(" remove");
631 if ((dp = parsereq(rp, length)) != NULL &&
632 parsefhn(dp, v3) != NULL)
633 return;
634 break;
635
636 case NFSPROC_RMDIR:
637 printf(" rmdir");
638 if ((dp = parsereq(rp, length)) != NULL &&
639 parsefhn(dp, v3) != NULL)
640 return;
641 break;
642
643 case NFSPROC_RENAME:
644 printf(" rename");
645 if ((dp = parsereq(rp, length)) != NULL &&
646 (dp = parsefhn(dp, v3)) != NULL) {
647 fputs(" ->", stdout);
648 if (parsefhn(dp, v3) != NULL)
649 return;
650 }
651 break;
652
653 case NFSPROC_LINK:
654 printf(" link");
655 if ((dp = parsereq(rp, length)) != NULL &&
656 (dp = parsefh(dp, v3)) != NULL) {
657 fputs(" ->", stdout);
658 if (parsefhn(dp, v3) != NULL)
659 return;
660 }
661 break;
662
663 case NFSPROC_READDIR:
664 printf(" readdir");
665 if ((dp = parsereq(rp, length)) != NULL &&
666 (dp = parsefh(dp, v3)) != NULL) {
667 if (v3) {
668 TCHECK(dp[4]);
669 /*
670 * We shouldn't really try to interpret the
671 * offset cookie here.
672 */
673 printf(" %u bytes @ ",
674 (u_int32_t) ntohl(dp[4]));
675 print_int64(dp, SIGNED);
676 if (vflag)
677 printf(" verf %08x%08x", dp[2],
678 dp[3]);
679 } else {
680 TCHECK(dp[1]);
681 /*
682 * Print the offset as signed, since -1 is
683 * common, but offsets > 2^31 aren't.
684 */
685 printf(" %u bytes @ %d",
686 (u_int32_t)ntohl(dp[1]),
687 (u_int32_t)ntohl(dp[0]));
688 }
689 return;
690 }
691 break;
692
693 case NFSPROC_READDIRPLUS:
694 printf(" readdirplus");
695 if ((dp = parsereq(rp, length)) != NULL &&
696 (dp = parsefh(dp, v3)) != NULL) {
697 TCHECK(dp[4]);
698 /*
699 * We don't try to interpret the offset
700 * cookie here.
701 */
702 printf(" %u bytes @ ", (u_int32_t) ntohl(dp[4]));
703 print_int64(dp, SIGNED);
704 if (vflag)
705 printf(" max %u verf %08x%08x",
706 (u_int32_t) ntohl(dp[5]), dp[2], dp[3]);
707 return;
708 }
709 break;
710
711 case NFSPROC_FSSTAT:
712 printf(" fsstat");
713 if ((dp = parsereq(rp, length)) != NULL &&
714 parsefh(dp, v3) != NULL)
715 return;
716 break;
717
718 case NFSPROC_FSINFO:
719 printf(" fsinfo");
720 break;
721
722 case NFSPROC_PATHCONF:
723 printf(" pathconf");
724 break;
725
726 case NFSPROC_COMMIT:
727 printf(" commit");
728 if ((dp = parsereq(rp, length)) != NULL &&
729 (dp = parsefh(dp, v3)) != NULL) {
730 printf(" %u bytes @ ", (u_int32_t) ntohl(dp[2]));
731 print_int64(dp, UNSIGNED);
732 return;
733 }
734 break;
735
736 default:
737 printf(" proc-%u", (u_int32_t)ntohl(rp->rm_call.cb_proc));
738 return;
739 }
740
741 trunc:
742 if (!nfserr)
743 fputs(" [|nfs]", stdout);
744 }
745
746 /*
747 * Print out an NFS file handle.
748 * We assume packet was not truncated before the end of the
749 * file handle pointed to by dp.
750 *
751 * Note: new version (using portable file-handle parser) doesn't produce
752 * generation number. It probably could be made to do that, with some
753 * additional hacking on the parser code.
754 */
755 static void
756 nfs_printfh(register const u_int32_t *dp, const u_int len)
757 {
758 my_fsid fsid;
759 ino_t ino;
760 char *sfsname = NULL;
761
762 Parse_fh((caddr_t*)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 sfsname = strchr(temp, ' ');
773 if (sfsname)
774 *sfsname = 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 (((struct ip *)bp)->ip_v) {
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->ip_v || 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 == 0 || 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 dp = parserep(rp, length);
1492 if (!(dp = parsestatus(dp, &er)))
1493 break;
1494 if (vflag)
1495 printf(" attr:");
1496 if (!(dp = parse_post_op_attr(dp, vflag)))
1497 break;
1498 if (!er)
1499 printf(" c %04x", (u_int32_t)ntohl(dp[0]));
1500 return;
1501
1502 case NFSPROC_READLINK:
1503 printf(" readlink");
1504 dp = parserep(rp, length);
1505 if (dp != NULL && parselinkres(dp, v3) != 0)
1506 return;
1507 break;
1508
1509 case NFSPROC_READ:
1510 printf(" read");
1511 if (!(dp = parserep(rp, length)))
1512 break;
1513 if (v3) {
1514 if (!(dp = parsestatus(dp, &er)))
1515 break;
1516 if (!(dp = parse_post_op_attr(dp, vflag)))
1517 break;
1518 if (er)
1519 return;
1520 if (vflag) {
1521 TCHECK(dp[1]);
1522 printf("%u bytes", (u_int32_t) ntohl(dp[0]));
1523 if (ntohl(dp[1]))
1524 printf(" EOF");
1525 }
1526 return;
1527 } else {
1528 if (parseattrstat(dp, vflag, 0) != 0)
1529 return;
1530 }
1531 break;
1532
1533 case NFSPROC_WRITE:
1534 printf(" write");
1535 if (!(dp = parserep(rp, length)))
1536 break;
1537 if (v3) {
1538 if (!(dp = parsestatus(dp, &er)))
1539 break;
1540 if (!(dp = parse_wcc_data(dp, vflag)))
1541 break;
1542 if (er)
1543 return;
1544 if (vflag) {
1545 TCHECK(dp[0]);
1546 printf("%u bytes", (u_int32_t) ntohl(dp[0]));
1547 if (vflag > 1) {
1548 TCHECK(dp[1]);
1549 printf(" <%s>",
1550 tok2str(nfsv3_writemodes,
1551 NULL, ntohl(dp[1])));
1552 }
1553 return;
1554 }
1555 } else {
1556 if (parseattrstat(dp, vflag, v3) != 0)
1557 return;
1558 }
1559 break;
1560
1561 case NFSPROC_CREATE:
1562 printf(" create");
1563 if (!(dp = parserep(rp, length)))
1564 break;
1565 if (v3) {
1566 if (parsecreateopres(dp, vflag) != 0)
1567 return;
1568 } else {
1569 if (parsediropres(dp) != 0)
1570 return;
1571 }
1572 break;
1573
1574 case NFSPROC_MKDIR:
1575 printf(" mkdir");
1576 if (!(dp = parserep(rp, length)))
1577 break;
1578 if (v3) {
1579 if (parsecreateopres(dp, vflag) != 0)
1580 return;
1581 } else {
1582 if (parsediropres(dp) != 0)
1583 return;
1584 }
1585 break;
1586
1587 case NFSPROC_SYMLINK:
1588 printf(" symlink");
1589 if (!(dp = parserep(rp, length)))
1590 break;
1591 if (v3) {
1592 if (parsecreateopres(dp, vflag) != 0)
1593 return;
1594 } else {
1595 if (parsestatus(dp, &er) != 0)
1596 return;
1597 }
1598 break;
1599
1600 case NFSPROC_MKNOD:
1601 printf(" mknod");
1602 if (!(dp = parserep(rp, length)))
1603 break;
1604 if (parsecreateopres(dp, vflag) != 0)
1605 return;
1606 break;
1607
1608 case NFSPROC_REMOVE:
1609 printf(" remove");
1610 if (!(dp = parserep(rp, length)))
1611 break;
1612 if (v3) {
1613 if (parsewccres(dp, vflag))
1614 return;
1615 } else {
1616 if (parsestatus(dp, &er) != 0)
1617 return;
1618 }
1619 break;
1620
1621 case NFSPROC_RMDIR:
1622 printf(" rmdir");
1623 if (!(dp = parserep(rp, length)))
1624 break;
1625 if (v3) {
1626 if (parsewccres(dp, vflag))
1627 return;
1628 } else {
1629 if (parsestatus(dp, &er) != 0)
1630 return;
1631 }
1632 break;
1633
1634 case NFSPROC_RENAME:
1635 printf(" rename");
1636 if (!(dp = parserep(rp, length)))
1637 break;
1638 if (v3) {
1639 if (!(dp = parsestatus(dp, &er)))
1640 break;
1641 if (vflag) {
1642 printf(" from:");
1643 if (!(dp = parse_wcc_data(dp, vflag)))
1644 break;
1645 printf(" to:");
1646 if (!(dp = parse_wcc_data(dp, vflag)))
1647 break;
1648 }
1649 return;
1650 } else {
1651 if (parsestatus(dp, &er) != 0)
1652 return;
1653 }
1654 break;
1655
1656 case NFSPROC_LINK:
1657 printf(" link");
1658 if (!(dp = parserep(rp, length)))
1659 break;
1660 if (v3) {
1661 if (!(dp = parsestatus(dp, &er)))
1662 break;
1663 if (vflag) {
1664 printf(" file POST:");
1665 if (!(dp = parse_post_op_attr(dp, vflag)))
1666 break;
1667 printf(" dir:");
1668 if (!(dp = parse_wcc_data(dp, vflag)))
1669 break;
1670 return;
1671 }
1672 } else {
1673 if (parsestatus(dp, &er) != 0)
1674 return;
1675 }
1676 break;
1677
1678 case NFSPROC_READDIR:
1679 printf(" readdir");
1680 if (!(dp = parserep(rp, length)))
1681 break;
1682 if (v3) {
1683 if (parsev3rddirres(dp, vflag))
1684 return;
1685 } else {
1686 if (parserddires(dp) != 0)
1687 return;
1688 }
1689 break;
1690
1691 case NFSPROC_READDIRPLUS:
1692 printf(" readdirplus");
1693 if (!(dp = parserep(rp, length)))
1694 break;
1695 if (parsev3rddirres(dp, vflag))
1696 return;
1697 break;
1698
1699 case NFSPROC_FSSTAT:
1700 printf(" fsstat");
1701 dp = parserep(rp, length);
1702 if (dp != NULL && parsestatfs(dp, v3) != 0)
1703 return;
1704 break;
1705
1706 case NFSPROC_FSINFO:
1707 printf(" fsinfo");
1708 dp = parserep(rp, length);
1709 if (dp != NULL && parsefsinfo(dp) != 0)
1710 return;
1711 break;
1712
1713 case NFSPROC_PATHCONF:
1714 printf(" pathconf");
1715 dp = parserep(rp, length);
1716 if (dp != NULL && parsepathconf(dp) != 0)
1717 return;
1718 break;
1719
1720 case NFSPROC_COMMIT:
1721 printf(" commit");
1722 dp = parserep(rp, length);
1723 if (dp != NULL && parsewccres(dp, vflag) != 0)
1724 return;
1725 break;
1726
1727 default:
1728 printf(" proc-%u", proc);
1729 return;
1730 }
1731 trunc:
1732 if (!nfserr)
1733 fputs(" [|nfs]", stdout);
1734 }