]> The Tcpdump Group git mirrors - tcpdump/blob - print-nfs.c
IS-IS: Use more ND_TCHECK_n() macros
[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 nfserr = 0; /* assume no error */
330 rp = (const struct sunrpc_msg *)bp;
331
332 ND_TCHECK_4(rp->rm_xid);
333 if (!ndo->ndo_nflag) {
334 strlcpy(srcid, "nfs", sizeof(srcid));
335 nd_snprintf(dstid, sizeof(dstid), "%u",
336 EXTRACT_BE_U_4(rp->rm_xid));
337 } else {
338 nd_snprintf(srcid, sizeof(srcid), "%u", NFS_PORT);
339 nd_snprintf(dstid, sizeof(dstid), "%u",
340 EXTRACT_BE_U_4(rp->rm_xid));
341 }
342 print_nfsaddr(ndo, bp2, srcid, dstid);
343
344 nfsreply_noaddr_print(ndo, bp, length, bp2);
345 return;
346
347 trunc:
348 if (!nfserr)
349 ND_PRINT("%s", tstr);
350 }
351
352 void
353 nfsreply_noaddr_print(netdissect_options *ndo,
354 const u_char *bp, u_int length,
355 const u_char *bp2)
356 {
357 const struct sunrpc_msg *rp;
358 uint32_t proc, vers, reply_stat;
359 enum sunrpc_reject_stat rstat;
360 uint32_t rlow;
361 uint32_t rhigh;
362 enum sunrpc_auth_stat rwhy;
363
364 nfserr = 0; /* assume no error */
365 rp = (const struct sunrpc_msg *)bp;
366
367 ND_TCHECK_4(rp->rm_reply.rp_stat);
368 reply_stat = EXTRACT_BE_U_4(&rp->rm_reply.rp_stat);
369 switch (reply_stat) {
370
371 case SUNRPC_MSG_ACCEPTED:
372 ND_PRINT("reply ok %u", length);
373 if (xid_map_find(rp, bp2, &proc, &vers) >= 0)
374 interp_reply(ndo, rp, proc, vers, length);
375 break;
376
377 case SUNRPC_MSG_DENIED:
378 ND_PRINT("reply ERR %u: ", length);
379 ND_TCHECK_4(rp->rm_reply.rp_reject.rj_stat);
380 rstat = EXTRACT_BE_U_4(&rp->rm_reply.rp_reject.rj_stat);
381 switch (rstat) {
382
383 case SUNRPC_RPC_MISMATCH:
384 ND_TCHECK_4(rp->rm_reply.rp_reject.rj_vers.high);
385 rlow = EXTRACT_BE_U_4(&rp->rm_reply.rp_reject.rj_vers.low);
386 rhigh = EXTRACT_BE_U_4(&rp->rm_reply.rp_reject.rj_vers.high);
387 ND_PRINT("RPC Version mismatch (%u-%u)", rlow, rhigh);
388 break;
389
390 case SUNRPC_AUTH_ERROR:
391 ND_TCHECK_4(rp->rm_reply.rp_reject.rj_why);
392 rwhy = EXTRACT_BE_U_4(&rp->rm_reply.rp_reject.rj_why);
393 ND_PRINT("Auth %s", tok2str(sunrpc_auth_str, "Invalid failure code %u", rwhy));
394 break;
395
396 default:
397 ND_PRINT("Unknown reason for rejecting rpc message %u", (unsigned int)rstat);
398 break;
399 }
400 break;
401
402 default:
403 ND_PRINT("reply Unknown rpc response code=%u %u", reply_stat, length);
404 break;
405 }
406 return;
407
408 trunc:
409 if (!nfserr)
410 ND_PRINT("%s", tstr);
411 }
412
413 /*
414 * Return a pointer to the first file handle in the packet.
415 * If the packet was truncated, return 0.
416 */
417 static const uint32_t *
418 parsereq(netdissect_options *ndo,
419 const struct sunrpc_msg *rp, u_int length)
420 {
421 const uint32_t *dp;
422 u_int len;
423
424 /*
425 * find the start of the req data (if we captured it)
426 */
427 dp = (const uint32_t *)&rp->rm_call.cb_cred;
428 ND_TCHECK_4(dp + 1);
429 len = EXTRACT_BE_U_4(dp + 1);
430 if (len < length) {
431 dp += (len + (2 * sizeof(*dp) + 3)) / sizeof(*dp);
432 ND_TCHECK_4(dp + 1);
433 len = EXTRACT_BE_U_4(dp + 1);
434 if (len < length) {
435 dp += (len + (2 * sizeof(*dp) + 3)) / sizeof(*dp);
436 ND_TCHECK_LEN(dp, 0);
437 return (dp);
438 }
439 }
440 trunc:
441 return (NULL);
442 }
443
444 /*
445 * Print out an NFS file handle and return a pointer to following word.
446 * If packet was truncated, return 0.
447 */
448 static const uint32_t *
449 parsefh(netdissect_options *ndo,
450 const uint32_t *dp, int v3)
451 {
452 u_int len;
453
454 if (v3) {
455 ND_TCHECK_4(dp);
456 len = EXTRACT_BE_U_4(dp) / 4;
457 dp++;
458 } else
459 len = NFSX_V2FH / 4;
460
461 if (ND_TTEST_LEN(dp, len * sizeof(*dp))) {
462 nfs_printfh(ndo, dp, len);
463 return (dp + len);
464 }
465 trunc:
466 return (NULL);
467 }
468
469 /*
470 * Print out a file name and return pointer to 32-bit word past it.
471 * If packet was truncated, return 0.
472 */
473 static const uint32_t *
474 parsefn(netdissect_options *ndo,
475 const uint32_t *dp)
476 {
477 uint32_t len;
478 const u_char *cp;
479
480 /* Bail if we don't have the string length */
481 ND_TCHECK_4(dp);
482
483 /* Fetch big-endian string length */
484 len = EXTRACT_BE_U_4(dp);
485 dp++;
486
487 ND_TCHECK_LEN(dp, ((len + 3) & ~3));
488
489 cp = (const u_char *)dp;
490 /* Update 32-bit pointer (NFS filenames padded to 32-bit boundaries) */
491 dp += ((len + 3) & ~3) / sizeof(*dp);
492 ND_PRINT("\"");
493 if (fn_printn(ndo, cp, len, ndo->ndo_snapend)) {
494 ND_PRINT("\"");
495 goto trunc;
496 }
497 ND_PRINT("\"");
498
499 return (dp);
500 trunc:
501 return NULL;
502 }
503
504 /*
505 * Print out file handle and file name.
506 * Return pointer to 32-bit word past file name.
507 * If packet was truncated (or there was some other error), return 0.
508 */
509 static const uint32_t *
510 parsefhn(netdissect_options *ndo,
511 const uint32_t *dp, int v3)
512 {
513 dp = parsefh(ndo, dp, v3);
514 if (dp == NULL)
515 return (NULL);
516 ND_PRINT(" ");
517 return (parsefn(ndo, dp));
518 }
519
520 void
521 nfsreq_noaddr_print(netdissect_options *ndo,
522 const u_char *bp, u_int length,
523 const u_char *bp2)
524 {
525 const struct sunrpc_msg *rp;
526 const uint32_t *dp;
527 nfs_type type;
528 int v3;
529 uint32_t proc;
530 uint32_t access_flags;
531 struct nfsv3_sattr sa3;
532
533 ND_PRINT("%u", length);
534 nfserr = 0; /* assume no error */
535 rp = (const struct sunrpc_msg *)bp;
536
537 if (!xid_map_enter(ndo, rp, bp2)) /* record proc number for later on */
538 goto trunc;
539
540 v3 = (EXTRACT_BE_U_4(&rp->rm_call.cb_vers) == NFS_VER3);
541 proc = EXTRACT_BE_U_4(&rp->rm_call.cb_proc);
542
543 if (!v3 && proc < NFS_NPROCS)
544 proc = nfsv3_procid[proc];
545
546 ND_PRINT(" %s", tok2str(nfsproc_str, "proc-%u", proc));
547 switch (proc) {
548
549 case NFSPROC_GETATTR:
550 case NFSPROC_SETATTR:
551 case NFSPROC_READLINK:
552 case NFSPROC_FSSTAT:
553 case NFSPROC_FSINFO:
554 case NFSPROC_PATHCONF:
555 if ((dp = parsereq(ndo, rp, length)) != NULL &&
556 parsefh(ndo, dp, v3) != NULL)
557 return;
558 break;
559
560 case NFSPROC_LOOKUP:
561 case NFSPROC_CREATE:
562 case NFSPROC_MKDIR:
563 case NFSPROC_REMOVE:
564 case NFSPROC_RMDIR:
565 if ((dp = parsereq(ndo, rp, length)) != NULL &&
566 parsefhn(ndo, dp, v3) != NULL)
567 return;
568 break;
569
570 case NFSPROC_ACCESS:
571 if ((dp = parsereq(ndo, rp, length)) != NULL &&
572 (dp = parsefh(ndo, dp, v3)) != NULL) {
573 ND_TCHECK_4(dp);
574 access_flags = EXTRACT_BE_U_4(dp);
575 if (access_flags & ~NFSV3ACCESS_FULL) {
576 /* NFSV3ACCESS definitions aren't up to date */
577 ND_PRINT(" %04x", access_flags);
578 } else if ((access_flags & NFSV3ACCESS_FULL) == NFSV3ACCESS_FULL) {
579 ND_PRINT(" NFS_ACCESS_FULL");
580 } else {
581 char separator = ' ';
582 if (access_flags & NFSV3ACCESS_READ) {
583 ND_PRINT(" NFS_ACCESS_READ");
584 separator = '|';
585 }
586 if (access_flags & NFSV3ACCESS_LOOKUP) {
587 ND_PRINT("%cNFS_ACCESS_LOOKUP", separator);
588 separator = '|';
589 }
590 if (access_flags & NFSV3ACCESS_MODIFY) {
591 ND_PRINT("%cNFS_ACCESS_MODIFY", separator);
592 separator = '|';
593 }
594 if (access_flags & NFSV3ACCESS_EXTEND) {
595 ND_PRINT("%cNFS_ACCESS_EXTEND", separator);
596 separator = '|';
597 }
598 if (access_flags & NFSV3ACCESS_DELETE) {
599 ND_PRINT("%cNFS_ACCESS_DELETE", separator);
600 separator = '|';
601 }
602 if (access_flags & NFSV3ACCESS_EXECUTE)
603 ND_PRINT("%cNFS_ACCESS_EXECUTE", separator);
604 }
605 return;
606 }
607 break;
608
609 case NFSPROC_READ:
610 if ((dp = parsereq(ndo, rp, length)) != NULL &&
611 (dp = parsefh(ndo, dp, v3)) != NULL) {
612 if (v3) {
613 ND_TCHECK_4(dp + 2);
614 ND_PRINT(" %u bytes @ %" PRIu64,
615 EXTRACT_BE_U_4(dp + 2),
616 EXTRACT_BE_U_8(dp));
617 } else {
618 ND_TCHECK_4(dp + 1);
619 ND_PRINT(" %u bytes @ %u",
620 EXTRACT_BE_U_4(dp + 1),
621 EXTRACT_BE_U_4(dp));
622 }
623 return;
624 }
625 break;
626
627 case NFSPROC_WRITE:
628 if ((dp = parsereq(ndo, rp, length)) != NULL &&
629 (dp = parsefh(ndo, dp, v3)) != NULL) {
630 if (v3) {
631 ND_TCHECK_4(dp + 4);
632 ND_PRINT(" %u (%u) bytes @ %" PRIu64,
633 EXTRACT_BE_U_4(dp + 4),
634 EXTRACT_BE_U_4(dp + 2),
635 EXTRACT_BE_U_8(dp));
636 if (ndo->ndo_vflag) {
637 ND_PRINT(" <%s>",
638 tok2str(nfsv3_writemodes,
639 NULL, EXTRACT_BE_U_4(dp + 3)));
640 }
641 } else {
642 ND_TCHECK_4(dp + 3);
643 ND_PRINT(" %u (%u) bytes @ %u (%u)",
644 EXTRACT_BE_U_4(dp + 3),
645 EXTRACT_BE_U_4(dp + 2),
646 EXTRACT_BE_U_4(dp + 1),
647 EXTRACT_BE_U_4(dp));
648 }
649 return;
650 }
651 break;
652
653 case NFSPROC_SYMLINK:
654 if ((dp = parsereq(ndo, rp, length)) != NULL &&
655 (dp = parsefhn(ndo, dp, v3)) != NULL) {
656 ND_PRINT(" ->");
657 if (v3 && (dp = parse_sattr3(ndo, dp, &sa3)) == NULL)
658 break;
659 if (parsefn(ndo, dp) == NULL)
660 break;
661 if (v3 && ndo->ndo_vflag)
662 print_sattr3(ndo, &sa3, ndo->ndo_vflag);
663 return;
664 }
665 break;
666
667 case NFSPROC_MKNOD:
668 if ((dp = parsereq(ndo, rp, length)) != NULL &&
669 (dp = parsefhn(ndo, dp, v3)) != NULL) {
670 ND_TCHECK_4(dp);
671 type = (nfs_type) EXTRACT_BE_U_4(dp);
672 dp++;
673 if ((dp = parse_sattr3(ndo, dp, &sa3)) == NULL)
674 break;
675 ND_PRINT(" %s", tok2str(type2str, "unk-ft %u", type));
676 if (ndo->ndo_vflag && (type == NFCHR || type == NFBLK)) {
677 ND_TCHECK_4(dp + 1);
678 ND_PRINT(" %u/%u",
679 EXTRACT_BE_U_4(dp),
680 EXTRACT_BE_U_4(dp + 1));
681 dp += 2;
682 }
683 if (ndo->ndo_vflag)
684 print_sattr3(ndo, &sa3, ndo->ndo_vflag);
685 return;
686 }
687 break;
688
689 case NFSPROC_RENAME:
690 if ((dp = parsereq(ndo, rp, length)) != NULL &&
691 (dp = parsefhn(ndo, dp, v3)) != NULL) {
692 ND_PRINT(" ->");
693 if (parsefhn(ndo, dp, v3) != NULL)
694 return;
695 }
696 break;
697
698 case NFSPROC_LINK:
699 if ((dp = parsereq(ndo, rp, length)) != NULL &&
700 (dp = parsefh(ndo, dp, v3)) != NULL) {
701 ND_PRINT(" ->");
702 if (parsefhn(ndo, dp, v3) != NULL)
703 return;
704 }
705 break;
706
707 case NFSPROC_READDIR:
708 if ((dp = parsereq(ndo, rp, length)) != NULL &&
709 (dp = parsefh(ndo, dp, v3)) != NULL) {
710 if (v3) {
711 ND_TCHECK_4(dp + 4);
712 /*
713 * We shouldn't really try to interpret the
714 * offset cookie here.
715 */
716 ND_PRINT(" %u bytes @ %" PRId64,
717 EXTRACT_BE_U_4(dp + 4),
718 EXTRACT_BE_U_8(dp));
719 if (ndo->ndo_vflag) {
720 /*
721 * This displays the 8 bytes
722 * of the verifier in order,
723 * from the low-order byte
724 * to the high-order byte.
725 */
726 ND_PRINT(" verf %08x%08x",
727 EXTRACT_BE_U_4(dp + 2),
728 EXTRACT_BE_U_4(dp + 3));
729 }
730 } else {
731 ND_TCHECK_4(dp + 1);
732 /*
733 * Print the offset as signed, since -1 is
734 * common, but offsets > 2^31 aren't.
735 */
736 ND_PRINT(" %u bytes @ %u",
737 EXTRACT_BE_U_4(dp + 1),
738 EXTRACT_BE_U_4(dp));
739 }
740 return;
741 }
742 break;
743
744 case NFSPROC_READDIRPLUS:
745 if ((dp = parsereq(ndo, rp, length)) != NULL &&
746 (dp = parsefh(ndo, dp, v3)) != NULL) {
747 ND_TCHECK_4(dp + 4);
748 /*
749 * We don't try to interpret the offset
750 * cookie here.
751 */
752 ND_PRINT(" %u bytes @ %" PRId64,
753 EXTRACT_BE_U_4(dp + 4),
754 EXTRACT_BE_U_8(dp));
755 if (ndo->ndo_vflag) {
756 ND_TCHECK_4(dp + 5);
757 /*
758 * This displays the 8 bytes
759 * of the verifier in order,
760 * from the low-order byte
761 * to the high-order byte.
762 */
763 ND_PRINT(" max %u verf %08x%08x",
764 EXTRACT_BE_U_4(dp + 5),
765 EXTRACT_BE_U_4(dp + 2),
766 EXTRACT_BE_U_4(dp + 3));
767 }
768 return;
769 }
770 break;
771
772 case NFSPROC_COMMIT:
773 if ((dp = parsereq(ndo, rp, length)) != NULL &&
774 (dp = parsefh(ndo, dp, v3)) != NULL) {
775 ND_TCHECK_4(dp + 2);
776 ND_PRINT(" %u bytes @ %" PRIu64,
777 EXTRACT_BE_U_4(dp + 2),
778 EXTRACT_BE_U_8(dp));
779 return;
780 }
781 break;
782
783 default:
784 return;
785 }
786
787 trunc:
788 if (!nfserr)
789 ND_PRINT("%s", tstr);
790 }
791
792 /*
793 * Print out an NFS file handle.
794 * We assume packet was not truncated before the end of the
795 * file handle pointed to by dp.
796 *
797 * Note: new version (using portable file-handle parser) doesn't produce
798 * generation number. It probably could be made to do that, with some
799 * additional hacking on the parser code.
800 */
801 static void
802 nfs_printfh(netdissect_options *ndo,
803 const uint32_t *dp, const u_int len)
804 {
805 my_fsid fsid;
806 uint32_t ino;
807 const char *sfsname = NULL;
808 char *spacep;
809
810 if (ndo->ndo_uflag) {
811 u_int i;
812 char const *sep = "";
813
814 ND_PRINT(" fh[");
815 for (i=0; i<len; i++) {
816 /*
817 * This displays 4 bytes in big-endian byte
818 * order. That's as good a choice as little-
819 * endian, as there's no guarantee that the
820 * server is big-endian or little-endian or
821 * that the file handle contains 4-byte
822 * integral fields, and is better than "the
823 * byte order of the host running tcpdump", as
824 * the latter means that different hosts
825 * running tcpdump may show the same file
826 * handle in different ways.
827 */
828 ND_PRINT("%s%x", sep, EXTRACT_BE_U_4(dp + i));
829 sep = ":";
830 }
831 ND_PRINT("]");
832 return;
833 }
834
835 Parse_fh((const u_char *)dp, len, &fsid, &ino, NULL, &sfsname, 0);
836
837 if (sfsname) {
838 /* file system ID is ASCII, not numeric, for this server OS */
839 char temp[NFSX_V3FHMAX+1];
840 u_int stringlen;
841
842 /* Make sure string is null-terminated */
843 stringlen = len;
844 if (stringlen > NFSX_V3FHMAX)
845 stringlen = NFSX_V3FHMAX;
846 strncpy(temp, sfsname, stringlen);
847 temp[stringlen] = '\0';
848 /* Remove trailing spaces */
849 spacep = strchr(temp, ' ');
850 if (spacep)
851 *spacep = '\0';
852
853 ND_PRINT(" fh %s/", temp);
854 } else {
855 ND_PRINT(" fh %u,%u/",
856 fsid.Fsid_dev.Major, fsid.Fsid_dev.Minor);
857 }
858
859 if(fsid.Fsid_dev.Minor == 257)
860 /* Print the undecoded handle */
861 ND_PRINT("%s", fsid.Opaque_Handle);
862 else
863 ND_PRINT("%ld", (long) ino);
864 }
865
866 /*
867 * Maintain a small cache of recent client.XID.server/proc pairs, to allow
868 * us to match up replies with requests and thus to know how to parse
869 * the reply.
870 */
871
872 struct xid_map_entry {
873 uint32_t xid; /* transaction ID (net order) */
874 int ipver; /* IP version (4 or 6) */
875 struct in6_addr client; /* client IP address (net order) */
876 struct in6_addr server; /* server IP address (net order) */
877 uint32_t proc; /* call proc number (host order) */
878 uint32_t vers; /* program version (host order) */
879 };
880
881 /*
882 * Map entries are kept in an array that we manage as a ring;
883 * new entries are always added at the tail of the ring. Initially,
884 * all the entries are zero and hence don't match anything.
885 */
886
887 #define XIDMAPSIZE 64
888
889 static struct xid_map_entry xid_map[XIDMAPSIZE];
890
891 static int xid_map_next = 0;
892 static int xid_map_hint = 0;
893
894 static int
895 xid_map_enter(netdissect_options *ndo,
896 const struct sunrpc_msg *rp, const u_char *bp)
897 {
898 const struct ip *ip = NULL;
899 const struct ip6_hdr *ip6 = NULL;
900 struct xid_map_entry *xmep;
901
902 if (!ND_TTEST_4(rp->rm_call.cb_proc))
903 return (0);
904 switch (IP_V((const struct ip *)bp)) {
905 case 4:
906 ip = (const struct ip *)bp;
907 break;
908 case 6:
909 ip6 = (const struct ip6_hdr *)bp;
910 break;
911 default:
912 return (1);
913 }
914
915 xmep = &xid_map[xid_map_next];
916
917 if (++xid_map_next >= XIDMAPSIZE)
918 xid_map_next = 0;
919
920 UNALIGNED_MEMCPY(&xmep->xid, &rp->rm_xid, sizeof(xmep->xid));
921 if (ip) {
922 xmep->ipver = 4;
923 UNALIGNED_MEMCPY(&xmep->client, ip->ip_src,
924 sizeof(ip->ip_src));
925 UNALIGNED_MEMCPY(&xmep->server, ip->ip_dst,
926 sizeof(ip->ip_dst));
927 }
928 else if (ip6) {
929 xmep->ipver = 6;
930 UNALIGNED_MEMCPY(&xmep->client, ip6->ip6_src,
931 sizeof(ip6->ip6_src));
932 UNALIGNED_MEMCPY(&xmep->server, ip6->ip6_dst,
933 sizeof(ip6->ip6_dst));
934 }
935 xmep->proc = EXTRACT_BE_U_4(&rp->rm_call.cb_proc);
936 xmep->vers = EXTRACT_BE_U_4(&rp->rm_call.cb_vers);
937 return (1);
938 }
939
940 /*
941 * Returns 0 and puts NFSPROC_xxx in proc return and
942 * version in vers return, or returns -1 on failure
943 */
944 static int
945 xid_map_find(const struct sunrpc_msg *rp, const u_char *bp, uint32_t *proc,
946 uint32_t *vers)
947 {
948 int i;
949 struct xid_map_entry *xmep;
950 uint32_t xid;
951 const struct ip *ip = (const struct ip *)bp;
952 const struct ip6_hdr *ip6 = (const struct ip6_hdr *)bp;
953 int cmp;
954
955 UNALIGNED_MEMCPY(&xid, &rp->rm_xid, sizeof(xmep->xid));
956 /* Start searching from where we last left off */
957 i = xid_map_hint;
958 do {
959 xmep = &xid_map[i];
960 cmp = 1;
961 if (xmep->ipver != IP_V(ip) || xmep->xid != xid)
962 goto nextitem;
963 switch (xmep->ipver) {
964 case 4:
965 if (UNALIGNED_MEMCMP(ip->ip_src, &xmep->server,
966 sizeof(ip->ip_src)) != 0 ||
967 UNALIGNED_MEMCMP(ip->ip_dst, &xmep->client,
968 sizeof(ip->ip_dst)) != 0) {
969 cmp = 0;
970 }
971 break;
972 case 6:
973 if (UNALIGNED_MEMCMP(ip6->ip6_src, &xmep->server,
974 sizeof(ip6->ip6_src)) != 0 ||
975 UNALIGNED_MEMCMP(ip6->ip6_dst, &xmep->client,
976 sizeof(ip6->ip6_dst)) != 0) {
977 cmp = 0;
978 }
979 break;
980 default:
981 cmp = 0;
982 break;
983 }
984 if (cmp) {
985 /* match */
986 xid_map_hint = i;
987 *proc = xmep->proc;
988 *vers = xmep->vers;
989 return 0;
990 }
991 nextitem:
992 if (++i >= XIDMAPSIZE)
993 i = 0;
994 } while (i != xid_map_hint);
995
996 /* search failed */
997 return (-1);
998 }
999
1000 /*
1001 * Routines for parsing reply packets
1002 */
1003
1004 /*
1005 * Return a pointer to the beginning of the actual results.
1006 * If the packet was truncated, return 0.
1007 */
1008 static const uint32_t *
1009 parserep(netdissect_options *ndo,
1010 const struct sunrpc_msg *rp, u_int length)
1011 {
1012 const uint32_t *dp;
1013 u_int len;
1014 enum sunrpc_accept_stat astat;
1015
1016 /*
1017 * Portability note:
1018 * Here we find the address of the ar_verf credentials.
1019 * Originally, this calculation was
1020 * dp = (uint32_t *)&rp->rm_reply.rp_acpt.ar_verf
1021 * On the wire, the rp_acpt field starts immediately after
1022 * the (32 bit) rp_stat field. However, rp_acpt (which is a
1023 * "struct accepted_reply") contains a "struct opaque_auth",
1024 * whose internal representation contains a pointer, so on a
1025 * 64-bit machine the compiler inserts 32 bits of padding
1026 * before rp->rm_reply.rp_acpt.ar_verf. So, we cannot use
1027 * the internal representation to parse the on-the-wire
1028 * representation. Instead, we skip past the rp_stat field,
1029 * which is an "enum" and so occupies one 32-bit word.
1030 */
1031 dp = ((const uint32_t *)&rp->rm_reply) + 1;
1032 ND_TCHECK_4(dp + 1);
1033 len = EXTRACT_BE_U_4(dp + 1);
1034 if (len >= length)
1035 return (NULL);
1036 /*
1037 * skip past the ar_verf credentials.
1038 */
1039 dp += (len + (2*sizeof(uint32_t) + 3)) / sizeof(uint32_t);
1040
1041 /*
1042 * now we can check the ar_stat field
1043 */
1044 ND_TCHECK_4(dp);
1045 astat = (enum sunrpc_accept_stat) EXTRACT_BE_U_4(dp);
1046 if (astat != SUNRPC_SUCCESS) {
1047 ND_PRINT(" %s", tok2str(sunrpc_str, "ar_stat %u", astat));
1048 nfserr = 1; /* suppress trunc string */
1049 return (NULL);
1050 }
1051 /* successful return */
1052 ND_TCHECK_LEN(dp, sizeof(astat));
1053 return ((const uint32_t *) (sizeof(astat) + ((const char *)dp)));
1054 trunc:
1055 return (0);
1056 }
1057
1058 static const uint32_t *
1059 parsestatus(netdissect_options *ndo,
1060 const uint32_t *dp, u_int *er)
1061 {
1062 u_int errnum;
1063
1064 ND_TCHECK_4(dp);
1065
1066 errnum = EXTRACT_BE_U_4(dp);
1067 if (er)
1068 *er = errnum;
1069 if (errnum != 0) {
1070 if (!ndo->ndo_qflag)
1071 ND_PRINT(" ERROR: %s",
1072 tok2str(status2str, "unk %u", errnum));
1073 nfserr = 1;
1074 }
1075 return (dp + 1);
1076 trunc:
1077 return NULL;
1078 }
1079
1080 static const uint32_t *
1081 parsefattr(netdissect_options *ndo,
1082 const uint32_t *dp, int verbose, int v3)
1083 {
1084 const struct nfs_fattr *fap;
1085
1086 fap = (const struct nfs_fattr *)dp;
1087 ND_TCHECK_4(&fap->fa_gid);
1088 if (verbose) {
1089 /*
1090 * XXX - UIDs and GIDs are unsigned in NFS and in
1091 * at least some UN*Xes, but we'll show them as
1092 * signed because -2 has traditionally been the
1093 * UID for "nobody", rather than 4294967294.
1094 */
1095 ND_PRINT(" %s %o ids %d/%d",
1096 tok2str(type2str, "unk-ft %u ",
1097 EXTRACT_BE_U_4(&fap->fa_type)),
1098 EXTRACT_BE_U_4(&fap->fa_mode),
1099 EXTRACT_BE_S_4(&fap->fa_uid),
1100 EXTRACT_BE_S_4(&fap->fa_gid));
1101 if (v3) {
1102 ND_TCHECK_8(&fap->fa3_size);
1103 ND_PRINT(" sz %" PRIu64,
1104 EXTRACT_BE_U_8((const uint32_t *)&fap->fa3_size));
1105 } else {
1106 ND_TCHECK_4(&fap->fa2_size);
1107 ND_PRINT(" sz %u", EXTRACT_BE_U_4(&fap->fa2_size));
1108 }
1109 }
1110 /* print lots more stuff */
1111 if (verbose > 1) {
1112 if (v3) {
1113 ND_TCHECK_8(&fap->fa3_ctime);
1114 ND_PRINT(" nlink %u rdev %u/%u",
1115 EXTRACT_BE_U_4(&fap->fa_nlink),
1116 EXTRACT_BE_U_4(&fap->fa3_rdev.specdata1),
1117 EXTRACT_BE_U_4(&fap->fa3_rdev.specdata2));
1118 ND_PRINT(" fsid %" PRIx64,
1119 EXTRACT_BE_U_8((const uint32_t *)&fap->fa3_fsid));
1120 ND_PRINT(" fileid %" PRIx64,
1121 EXTRACT_BE_U_8((const uint32_t *)&fap->fa3_fileid));
1122 ND_PRINT(" a/m/ctime %u.%06u",
1123 EXTRACT_BE_U_4(&fap->fa3_atime.nfsv3_sec),
1124 EXTRACT_BE_U_4(&fap->fa3_atime.nfsv3_nsec));
1125 ND_PRINT(" %u.%06u",
1126 EXTRACT_BE_U_4(&fap->fa3_mtime.nfsv3_sec),
1127 EXTRACT_BE_U_4(&fap->fa3_mtime.nfsv3_nsec));
1128 ND_PRINT(" %u.%06u",
1129 EXTRACT_BE_U_4(&fap->fa3_ctime.nfsv3_sec),
1130 EXTRACT_BE_U_4(&fap->fa3_ctime.nfsv3_nsec));
1131 } else {
1132 ND_TCHECK_8(&fap->fa2_ctime);
1133 ND_PRINT(" nlink %u rdev 0x%x fsid 0x%x nodeid 0x%x a/m/ctime",
1134 EXTRACT_BE_U_4(&fap->fa_nlink),
1135 EXTRACT_BE_U_4(&fap->fa2_rdev),
1136 EXTRACT_BE_U_4(&fap->fa2_fsid),
1137 EXTRACT_BE_U_4(&fap->fa2_fileid));
1138 ND_PRINT(" %u.%06u",
1139 EXTRACT_BE_U_4(&fap->fa2_atime.nfsv2_sec),
1140 EXTRACT_BE_U_4(&fap->fa2_atime.nfsv2_usec));
1141 ND_PRINT(" %u.%06u",
1142 EXTRACT_BE_U_4(&fap->fa2_mtime.nfsv2_sec),
1143 EXTRACT_BE_U_4(&fap->fa2_mtime.nfsv2_usec));
1144 ND_PRINT(" %u.%06u",
1145 EXTRACT_BE_U_4(&fap->fa2_ctime.nfsv2_sec),
1146 EXTRACT_BE_U_4(&fap->fa2_ctime.nfsv2_usec));
1147 }
1148 }
1149 return ((const uint32_t *)((const unsigned char *)dp +
1150 (v3 ? NFSX_V3FATTR : NFSX_V2FATTR)));
1151 trunc:
1152 return (NULL);
1153 }
1154
1155 static int
1156 parseattrstat(netdissect_options *ndo,
1157 const uint32_t *dp, int verbose, int v3)
1158 {
1159 u_int er;
1160
1161 dp = parsestatus(ndo, dp, &er);
1162 if (dp == NULL)
1163 return (0);
1164 if (er)
1165 return (1);
1166
1167 return (parsefattr(ndo, dp, verbose, v3) != NULL);
1168 }
1169
1170 static int
1171 parsediropres(netdissect_options *ndo,
1172 const uint32_t *dp)
1173 {
1174 u_int er;
1175
1176 if (!(dp = parsestatus(ndo, dp, &er)))
1177 return (0);
1178 if (er)
1179 return (1);
1180
1181 dp = parsefh(ndo, dp, 0);
1182 if (dp == NULL)
1183 return (0);
1184
1185 return (parsefattr(ndo, dp, ndo->ndo_vflag, 0) != NULL);
1186 }
1187
1188 static int
1189 parselinkres(netdissect_options *ndo,
1190 const uint32_t *dp, int v3)
1191 {
1192 u_int er;
1193
1194 dp = parsestatus(ndo, dp, &er);
1195 if (dp == NULL)
1196 return(0);
1197 if (er)
1198 return(1);
1199 if (v3 && !(dp = parse_post_op_attr(ndo, dp, ndo->ndo_vflag)))
1200 return (0);
1201 ND_PRINT(" ");
1202 return (parsefn(ndo, dp) != NULL);
1203 }
1204
1205 static int
1206 parsestatfs(netdissect_options *ndo,
1207 const uint32_t *dp, int v3)
1208 {
1209 const struct nfs_statfs *sfsp;
1210 u_int er;
1211
1212 dp = parsestatus(ndo, dp, &er);
1213 if (dp == NULL)
1214 return (0);
1215 if (!v3 && er)
1216 return (1);
1217
1218 if (ndo->ndo_qflag)
1219 return(1);
1220
1221 if (v3) {
1222 if (ndo->ndo_vflag)
1223 ND_PRINT(" POST:");
1224 if (!(dp = parse_post_op_attr(ndo, dp, ndo->ndo_vflag)))
1225 return (0);
1226 }
1227
1228 ND_TCHECK_LEN(dp, (v3 ? NFSX_V3STATFS : NFSX_V2STATFS));
1229
1230 sfsp = (const struct nfs_statfs *)dp;
1231
1232 if (v3) {
1233 ND_PRINT(" tbytes %" PRIu64 " fbytes %" PRIu64 " abytes %" PRIu64,
1234 EXTRACT_BE_U_8((const uint32_t *)&sfsp->sf_tbytes),
1235 EXTRACT_BE_U_8((const uint32_t *)&sfsp->sf_fbytes),
1236 EXTRACT_BE_U_8((const uint32_t *)&sfsp->sf_abytes));
1237 if (ndo->ndo_vflag) {
1238 ND_PRINT(" tfiles %" PRIu64 " ffiles %" PRIu64 " afiles %" PRIu64 " invar %u",
1239 EXTRACT_BE_U_8((const uint32_t *)&sfsp->sf_tfiles),
1240 EXTRACT_BE_U_8((const uint32_t *)&sfsp->sf_ffiles),
1241 EXTRACT_BE_U_8((const uint32_t *)&sfsp->sf_afiles),
1242 EXTRACT_BE_U_4(&sfsp->sf_invarsec));
1243 }
1244 } else {
1245 ND_PRINT(" tsize %u bsize %u blocks %u bfree %u bavail %u",
1246 EXTRACT_BE_U_4(&sfsp->sf_tsize),
1247 EXTRACT_BE_U_4(&sfsp->sf_bsize),
1248 EXTRACT_BE_U_4(&sfsp->sf_blocks),
1249 EXTRACT_BE_U_4(&sfsp->sf_bfree),
1250 EXTRACT_BE_U_4(&sfsp->sf_bavail));
1251 }
1252
1253 return (1);
1254 trunc:
1255 return (0);
1256 }
1257
1258 static int
1259 parserddires(netdissect_options *ndo,
1260 const uint32_t *dp)
1261 {
1262 u_int er;
1263
1264 dp = parsestatus(ndo, dp, &er);
1265 if (dp == NULL)
1266 return (0);
1267 if (er)
1268 return (1);
1269 if (ndo->ndo_qflag)
1270 return (1);
1271
1272 ND_TCHECK_4(dp + 2);
1273 ND_PRINT(" offset 0x%x size %u ",
1274 EXTRACT_BE_U_4(dp), EXTRACT_BE_U_4(dp + 1));
1275 if (EXTRACT_BE_U_4(dp + 2) != 0)
1276 ND_PRINT(" eof");
1277
1278 return (1);
1279 trunc:
1280 return (0);
1281 }
1282
1283 static const uint32_t *
1284 parse_wcc_attr(netdissect_options *ndo,
1285 const uint32_t *dp)
1286 {
1287 /* Our caller has already checked this */
1288 ND_PRINT(" sz %" PRIu64, EXTRACT_BE_U_8(dp));
1289 ND_PRINT(" mtime %u.%06u ctime %u.%06u",
1290 EXTRACT_BE_U_4(dp + 2), EXTRACT_BE_U_4(dp + 3),
1291 EXTRACT_BE_U_4(dp + 4), EXTRACT_BE_U_4(dp + 5));
1292 return (dp + 6);
1293 }
1294
1295 /*
1296 * Pre operation attributes. Print only if vflag > 1.
1297 */
1298 static const uint32_t *
1299 parse_pre_op_attr(netdissect_options *ndo,
1300 const uint32_t *dp, int verbose)
1301 {
1302 ND_TCHECK_4(dp);
1303 if (!EXTRACT_BE_U_4(dp))
1304 return (dp + 1);
1305 dp++;
1306 ND_TCHECK_LEN(dp, 24);
1307 if (verbose > 1) {
1308 return parse_wcc_attr(ndo, dp);
1309 } else {
1310 /* If not verbose enough, just skip over wcc_attr */
1311 return (dp + 6);
1312 }
1313 trunc:
1314 return (NULL);
1315 }
1316
1317 /*
1318 * Post operation attributes are printed if vflag >= 1
1319 */
1320 static const uint32_t *
1321 parse_post_op_attr(netdissect_options *ndo,
1322 const uint32_t *dp, int verbose)
1323 {
1324 ND_TCHECK_4(dp);
1325 if (!EXTRACT_BE_U_4(dp))
1326 return (dp + 1);
1327 dp++;
1328 if (verbose) {
1329 return parsefattr(ndo, dp, verbose, 1);
1330 } else
1331 return (dp + (NFSX_V3FATTR / sizeof (uint32_t)));
1332 trunc:
1333 return (NULL);
1334 }
1335
1336 static const uint32_t *
1337 parse_wcc_data(netdissect_options *ndo,
1338 const uint32_t *dp, int verbose)
1339 {
1340 if (verbose > 1)
1341 ND_PRINT(" PRE:");
1342 if (!(dp = parse_pre_op_attr(ndo, dp, verbose)))
1343 return (0);
1344
1345 if (verbose)
1346 ND_PRINT(" POST:");
1347 return parse_post_op_attr(ndo, dp, verbose);
1348 }
1349
1350 static const uint32_t *
1351 parsecreateopres(netdissect_options *ndo,
1352 const uint32_t *dp, int verbose)
1353 {
1354 u_int er;
1355
1356 if (!(dp = parsestatus(ndo, dp, &er)))
1357 return (0);
1358 if (er)
1359 dp = parse_wcc_data(ndo, dp, verbose);
1360 else {
1361 ND_TCHECK_4(dp);
1362 if (!EXTRACT_BE_U_4(dp))
1363 return (dp + 1);
1364 dp++;
1365 if (!(dp = parsefh(ndo, dp, 1)))
1366 return (0);
1367 if (verbose) {
1368 if (!(dp = parse_post_op_attr(ndo, dp, verbose)))
1369 return (0);
1370 if (ndo->ndo_vflag > 1) {
1371 ND_PRINT(" dir attr:");
1372 dp = parse_wcc_data(ndo, dp, verbose);
1373 }
1374 }
1375 }
1376 return (dp);
1377 trunc:
1378 return (NULL);
1379 }
1380
1381 static int
1382 parsewccres(netdissect_options *ndo,
1383 const uint32_t *dp, int verbose)
1384 {
1385 u_int er;
1386
1387 if (!(dp = parsestatus(ndo, dp, &er)))
1388 return (0);
1389 return parse_wcc_data(ndo, dp, verbose) != NULL;
1390 }
1391
1392 static const uint32_t *
1393 parsev3rddirres(netdissect_options *ndo,
1394 const uint32_t *dp, int verbose)
1395 {
1396 u_int er;
1397
1398 if (!(dp = parsestatus(ndo, dp, &er)))
1399 return (0);
1400 if (ndo->ndo_vflag)
1401 ND_PRINT(" POST:");
1402 if (!(dp = parse_post_op_attr(ndo, dp, verbose)))
1403 return (0);
1404 if (er)
1405 return dp;
1406 if (ndo->ndo_vflag) {
1407 ND_TCHECK_4(dp + 1);
1408 /*
1409 * This displays the 8 bytes of the verifier in order,
1410 * from the low-order byte to the high-order byte.
1411 */
1412 ND_PRINT(" verf %08x%08x",
1413 EXTRACT_BE_U_4(dp), EXTRACT_BE_U_4(dp + 1));
1414 dp += 2;
1415 }
1416 return dp;
1417 trunc:
1418 return (NULL);
1419 }
1420
1421 static int
1422 parsefsinfo(netdissect_options *ndo,
1423 const uint32_t *dp)
1424 {
1425 const struct nfsv3_fsinfo *sfp;
1426 u_int er;
1427
1428 if (!(dp = parsestatus(ndo, dp, &er)))
1429 return (0);
1430 if (ndo->ndo_vflag)
1431 ND_PRINT(" POST:");
1432 if (!(dp = parse_post_op_attr(ndo, dp, ndo->ndo_vflag)))
1433 return (0);
1434 if (er)
1435 return (1);
1436
1437 sfp = (const struct nfsv3_fsinfo *)dp;
1438 ND_TCHECK_SIZE(sfp);
1439 ND_PRINT(" rtmax %u rtpref %u wtmax %u wtpref %u dtpref %u",
1440 EXTRACT_BE_U_4(&sfp->fs_rtmax),
1441 EXTRACT_BE_U_4(&sfp->fs_rtpref),
1442 EXTRACT_BE_U_4(&sfp->fs_wtmax),
1443 EXTRACT_BE_U_4(&sfp->fs_wtpref),
1444 EXTRACT_BE_U_4(&sfp->fs_dtpref));
1445 if (ndo->ndo_vflag) {
1446 ND_PRINT(" rtmult %u wtmult %u maxfsz %" PRIu64,
1447 EXTRACT_BE_U_4(&sfp->fs_rtmult),
1448 EXTRACT_BE_U_4(&sfp->fs_wtmult),
1449 EXTRACT_BE_U_8((const uint32_t *)&sfp->fs_maxfilesize));
1450 ND_PRINT(" delta %u.%06u ",
1451 EXTRACT_BE_U_4(&sfp->fs_timedelta.nfsv3_sec),
1452 EXTRACT_BE_U_4(&sfp->fs_timedelta.nfsv3_nsec));
1453 }
1454 return (1);
1455 trunc:
1456 return (0);
1457 }
1458
1459 static int
1460 parsepathconf(netdissect_options *ndo,
1461 const uint32_t *dp)
1462 {
1463 u_int er;
1464 const struct nfsv3_pathconf *spp;
1465
1466 if (!(dp = parsestatus(ndo, dp, &er)))
1467 return (0);
1468 if (ndo->ndo_vflag)
1469 ND_PRINT(" POST:");
1470 if (!(dp = parse_post_op_attr(ndo, dp, ndo->ndo_vflag)))
1471 return (0);
1472 if (er)
1473 return (1);
1474
1475 spp = (const struct nfsv3_pathconf *)dp;
1476 ND_TCHECK_SIZE(spp);
1477
1478 ND_PRINT(" linkmax %u namemax %u %s %s %s %s",
1479 EXTRACT_BE_U_4(&spp->pc_linkmax),
1480 EXTRACT_BE_U_4(&spp->pc_namemax),
1481 EXTRACT_BE_U_4(&spp->pc_notrunc) ? "notrunc" : "",
1482 EXTRACT_BE_U_4(&spp->pc_chownrestricted) ? "chownres" : "",
1483 EXTRACT_BE_U_4(&spp->pc_caseinsensitive) ? "igncase" : "",
1484 EXTRACT_BE_U_4(&spp->pc_casepreserving) ? "keepcase" : "");
1485 return (1);
1486 trunc:
1487 return (0);
1488 }
1489
1490 static void
1491 interp_reply(netdissect_options *ndo,
1492 const struct sunrpc_msg *rp, uint32_t proc, uint32_t vers, int length)
1493 {
1494 const uint32_t *dp;
1495 int v3;
1496 u_int er;
1497
1498 v3 = (vers == NFS_VER3);
1499
1500 if (!v3 && proc < NFS_NPROCS)
1501 proc = nfsv3_procid[proc];
1502
1503 ND_PRINT(" %s", tok2str(nfsproc_str, "proc-%u", proc));
1504 switch (proc) {
1505
1506 case NFSPROC_GETATTR:
1507 dp = parserep(ndo, rp, length);
1508 if (dp != NULL && parseattrstat(ndo, dp, !ndo->ndo_qflag, v3) != 0)
1509 return;
1510 break;
1511
1512 case NFSPROC_SETATTR:
1513 if (!(dp = parserep(ndo, rp, length)))
1514 return;
1515 if (v3) {
1516 if (parsewccres(ndo, dp, ndo->ndo_vflag))
1517 return;
1518 } else {
1519 if (parseattrstat(ndo, dp, !ndo->ndo_qflag, 0) != 0)
1520 return;
1521 }
1522 break;
1523
1524 case NFSPROC_LOOKUP:
1525 if (!(dp = parserep(ndo, rp, length)))
1526 break;
1527 if (v3) {
1528 if (!(dp = parsestatus(ndo, dp, &er)))
1529 break;
1530 if (er) {
1531 if (ndo->ndo_vflag > 1) {
1532 ND_PRINT(" post dattr:");
1533 dp = parse_post_op_attr(ndo, dp, ndo->ndo_vflag);
1534 }
1535 } else {
1536 if (!(dp = parsefh(ndo, dp, v3)))
1537 break;
1538 if ((dp = parse_post_op_attr(ndo, dp, ndo->ndo_vflag)) &&
1539 ndo->ndo_vflag > 1) {
1540 ND_PRINT(" post dattr:");
1541 dp = parse_post_op_attr(ndo, dp, ndo->ndo_vflag);
1542 }
1543 }
1544 if (dp)
1545 return;
1546 } else {
1547 if (parsediropres(ndo, dp) != 0)
1548 return;
1549 }
1550 break;
1551
1552 case NFSPROC_ACCESS:
1553 if (!(dp = parserep(ndo, rp, length)))
1554 break;
1555 if (!(dp = parsestatus(ndo, dp, &er)))
1556 break;
1557 if (ndo->ndo_vflag)
1558 ND_PRINT(" attr:");
1559 if (!(dp = parse_post_op_attr(ndo, dp, ndo->ndo_vflag)))
1560 break;
1561 if (!er) {
1562 ND_TCHECK_4(dp);
1563 ND_PRINT(" c %04x", EXTRACT_BE_U_4(dp));
1564 }
1565 return;
1566
1567 case NFSPROC_READLINK:
1568 dp = parserep(ndo, rp, length);
1569 if (dp != NULL && parselinkres(ndo, dp, v3) != 0)
1570 return;
1571 break;
1572
1573 case NFSPROC_READ:
1574 if (!(dp = parserep(ndo, rp, length)))
1575 break;
1576 if (v3) {
1577 if (!(dp = parsestatus(ndo, dp, &er)))
1578 break;
1579 if (!(dp = parse_post_op_attr(ndo, dp, ndo->ndo_vflag)))
1580 break;
1581 if (er)
1582 return;
1583 if (ndo->ndo_vflag) {
1584 ND_TCHECK_4(dp + 1);
1585 ND_PRINT(" %u bytes", EXTRACT_BE_U_4(dp));
1586 if (EXTRACT_BE_U_4(dp + 1))
1587 ND_PRINT(" EOF");
1588 }
1589 return;
1590 } else {
1591 if (parseattrstat(ndo, dp, ndo->ndo_vflag, 0) != 0)
1592 return;
1593 }
1594 break;
1595
1596 case NFSPROC_WRITE:
1597 if (!(dp = parserep(ndo, rp, length)))
1598 break;
1599 if (v3) {
1600 if (!(dp = parsestatus(ndo, dp, &er)))
1601 break;
1602 if (!(dp = parse_wcc_data(ndo, dp, ndo->ndo_vflag)))
1603 break;
1604 if (er)
1605 return;
1606 if (ndo->ndo_vflag) {
1607 ND_TCHECK_4(dp);
1608 ND_PRINT(" %u bytes", EXTRACT_BE_U_4(dp));
1609 if (ndo->ndo_vflag > 1) {
1610 ND_TCHECK_4(dp + 1);
1611 ND_PRINT(" <%s>",
1612 tok2str(nfsv3_writemodes,
1613 NULL, EXTRACT_BE_U_4(dp + 1)));
1614 }
1615 return;
1616 }
1617 } else {
1618 if (parseattrstat(ndo, dp, ndo->ndo_vflag, v3) != 0)
1619 return;
1620 }
1621 break;
1622
1623 case NFSPROC_CREATE:
1624 case NFSPROC_MKDIR:
1625 if (!(dp = parserep(ndo, rp, length)))
1626 break;
1627 if (v3) {
1628 if (parsecreateopres(ndo, dp, ndo->ndo_vflag) != NULL)
1629 return;
1630 } else {
1631 if (parsediropres(ndo, dp) != 0)
1632 return;
1633 }
1634 break;
1635
1636 case NFSPROC_SYMLINK:
1637 if (!(dp = parserep(ndo, rp, length)))
1638 break;
1639 if (v3) {
1640 if (parsecreateopres(ndo, dp, ndo->ndo_vflag) != NULL)
1641 return;
1642 } else {
1643 if (parsestatus(ndo, dp, &er) != NULL)
1644 return;
1645 }
1646 break;
1647
1648 case NFSPROC_MKNOD:
1649 if (!(dp = parserep(ndo, rp, length)))
1650 break;
1651 if (parsecreateopres(ndo, dp, ndo->ndo_vflag) != NULL)
1652 return;
1653 break;
1654
1655 case NFSPROC_REMOVE:
1656 case NFSPROC_RMDIR:
1657 if (!(dp = parserep(ndo, rp, length)))
1658 break;
1659 if (v3) {
1660 if (parsewccres(ndo, dp, ndo->ndo_vflag))
1661 return;
1662 } else {
1663 if (parsestatus(ndo, dp, &er) != NULL)
1664 return;
1665 }
1666 break;
1667
1668 case NFSPROC_RENAME:
1669 if (!(dp = parserep(ndo, rp, length)))
1670 break;
1671 if (v3) {
1672 if (!(dp = parsestatus(ndo, dp, &er)))
1673 break;
1674 if (ndo->ndo_vflag) {
1675 ND_PRINT(" from:");
1676 if (!(dp = parse_wcc_data(ndo, dp, ndo->ndo_vflag)))
1677 break;
1678 ND_PRINT(" to:");
1679 if (!(dp = parse_wcc_data(ndo, dp, ndo->ndo_vflag)))
1680 break;
1681 }
1682 return;
1683 } else {
1684 if (parsestatus(ndo, dp, &er) != NULL)
1685 return;
1686 }
1687 break;
1688
1689 case NFSPROC_LINK:
1690 if (!(dp = parserep(ndo, rp, length)))
1691 break;
1692 if (v3) {
1693 if (!(dp = parsestatus(ndo, dp, &er)))
1694 break;
1695 if (ndo->ndo_vflag) {
1696 ND_PRINT(" file POST:");
1697 if (!(dp = parse_post_op_attr(ndo, dp, ndo->ndo_vflag)))
1698 break;
1699 ND_PRINT(" dir:");
1700 if (!(dp = parse_wcc_data(ndo, dp, ndo->ndo_vflag)))
1701 break;
1702 return;
1703 }
1704 } else {
1705 if (parsestatus(ndo, dp, &er) != NULL)
1706 return;
1707 }
1708 break;
1709
1710 case NFSPROC_READDIR:
1711 if (!(dp = parserep(ndo, rp, length)))
1712 break;
1713 if (v3) {
1714 if (parsev3rddirres(ndo, dp, ndo->ndo_vflag))
1715 return;
1716 } else {
1717 if (parserddires(ndo, dp) != 0)
1718 return;
1719 }
1720 break;
1721
1722 case NFSPROC_READDIRPLUS:
1723 if (!(dp = parserep(ndo, rp, length)))
1724 break;
1725 if (parsev3rddirres(ndo, dp, ndo->ndo_vflag))
1726 return;
1727 break;
1728
1729 case NFSPROC_FSSTAT:
1730 dp = parserep(ndo, rp, length);
1731 if (dp != NULL && parsestatfs(ndo, dp, v3) != 0)
1732 return;
1733 break;
1734
1735 case NFSPROC_FSINFO:
1736 dp = parserep(ndo, rp, length);
1737 if (dp != NULL && parsefsinfo(ndo, dp) != 0)
1738 return;
1739 break;
1740
1741 case NFSPROC_PATHCONF:
1742 dp = parserep(ndo, rp, length);
1743 if (dp != NULL && parsepathconf(ndo, dp) != 0)
1744 return;
1745 break;
1746
1747 case NFSPROC_COMMIT:
1748 dp = parserep(ndo, rp, length);
1749 if (dp != NULL && parsewccres(ndo, dp, ndo->ndo_vflag) != 0)
1750 return;
1751 break;
1752
1753 default:
1754 return;
1755 }
1756 trunc:
1757 if (!nfserr)
1758 ND_PRINT("%s", tstr);
1759 }