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