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