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