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