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