4 * Copyright (c) 1993, 1994 Jeffrey C. Mogul, Digital Equipment Corporation,
5 * Western Research Laboratory. All rights reserved.
6 * Copyright (c) 2001 Compaq Computer Corporation. All rights reserved.
8 * Permission to use, copy, and modify this software and its
9 * documentation is hereby granted only under the following terms and
10 * conditions. Both the above copyright notice and this permission
11 * notice must appear in all copies of the software, derivative works
12 * or modified versions, and any portions thereof, and both notices
13 * must appear in supporting documentation.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in
22 * the documentation and/or other materials provided with the
25 * THE SOFTWARE IS PROVIDED "AS IS" AND COMPAQ COMPUTER CORPORATION
26 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
27 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
28 * EVENT SHALL COMPAQ COMPUTER CORPORATION BE LIABLE FOR ANY
29 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
30 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
31 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
32 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
37 * parsenfsfh.c - portable parser for NFS file handles
38 * uses all sorts of heuristics
41 * Digital Equipment Corporation
42 * Western Research Laboratory
45 #include <sys/cdefs.h>
48 static const char rcsid
[] _U_
=
49 "@(#) Header: /tcpdump/master/tcpdump/parsenfsfh.c,v 1.28.2.1 2007/06/15 19:15:04 guy Exp (LBL)";
51 __RCSID("$NetBSD: tcpdump2rcsid.ex,v 1.1 2001/06/25 20:09:58 itojun Exp $");
59 #include <tcpdump-stdinc.h>
64 #include "interface.h"
68 * This routine attempts to parse a file handle (in network byte order),
69 * using heuristics to guess what kind of format it is in. See the
70 * file "fhandle_layouts" for a detailed description of the various
71 * patterns we know about.
73 * The file handle is parsed into our internal representation of a
74 * file-system id, and an internal representation of an inode-number.
92 /* Nasty hack to keep the Ultrix C compiler from emitting bogus warnings */
93 #define XFF(x) ((u_int32_t)(x))
98 #define make_uint32(msb,b,c,lsb)\
99 (XFF(lsb) + (XFF(c)<<8) + (XFF(b)<<16) + (XFF(msb)<<24))
101 #define make_uint24(msb,b, lsb)\
102 (XFF(lsb) + (XFF(b)<<8) + (XFF(msb)<<16))
104 #define make_uint16(msb,lsb)\
105 (XFF(lsb) + (XFF(msb)<<8))
108 /* or other 64-bit systems */
109 #define make_uint48(msb,b,c,d,e,lsb)\
110 ((lsb) + ((e)<<8) + ((d)<<16) + ((c)<<24) + ((b)<<32) + ((msb)<<40))
112 /* on 32-bit systems ignore high-order bits */
113 #define make_uint48(msb,b,c,d,e,lsb)\
114 ((lsb) + ((e)<<8) + ((d)<<16) + ((c)<<24))
117 static int is_UCX(const unsigned char *);
120 Parse_fh(fh
, len
, fsidp
, inop
, osnamep
, fsnamep
, ourself
)
121 register const unsigned char *fh
;
125 const char **osnamep
; /* if non-NULL, return OS name here */
126 const char **fsnamep
; /* if non-NULL, return server fs name here (for VMS) */
127 int ourself
; /* true if file handle was generated on this host */
129 register const unsigned char *fhp
= fh
;
131 int fhtype
= FHT_UNKNOWN
;
135 /* File handle generated on this host, no need for guessing */
157 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__) \
158 || defined(__OpenBSD__)
163 * This is basically a big decision tree
165 else if ((fhp
[0] == 0) && (fhp
[1] == 0)) {
166 /* bytes[0,1] == (0,0); rules out Ultrix, IRIX5, SUNOS5 */
167 /* probably rules out HP-UX, AIX unless they allow major=0 */
168 if ((fhp
[2] == 0) && (fhp
[3] == 0)) {
169 /* bytes[2,3] == (0,0); must be Auspex */
170 /* XXX or could be Ultrix+MASSBUS "hp" disk? */
175 * bytes[2,3] != (0,0); rules out Auspex, could be
176 * DECOSF, SUNOS4, or IRIX4
178 if ((fhp
[4] != 0) && (fhp
[5] == 0) &&
179 (fhp
[8] == 12) && (fhp
[9] == 0)) {
180 /* seems to be DECOSF, with minor == 0 */
184 /* could be SUNOS4 or IRIX4 */
185 /* XXX the test of fhp[5] == 8 could be wrong */
186 if ((fhp
[4] == 0) && (fhp
[5] == 8) && (fhp
[6] == 0) &&
188 /* looks like a length, not a file system typecode */
200 * bytes[0,1] != (0,0); rules out Auspex, IRIX4, SUNOS4
201 * could be IRIX5, DECOSF, UCX, Ultrix, SUNOS5
202 * could be AIX, HP-UX
204 if ((fhp
[2] == 0) && (fhp
[3] == 0)) {
206 * bytes[2,3] == (0,0); rules out OSF, probably not UCX
207 * (unless the exported device name is just one letter!),
208 * could be Ultrix, IRIX5, AIX, or SUNOS5
209 * might be HP-UX (depends on their values for minor devs)
211 if ((fhp
[6] == 0) && (fhp
[7] == 0)) {
214 /*XXX we probably only need to test of these two bytes */
215 else if ((fhp
[21] == 0) && (fhp
[23] == 0)) {
219 /* Could be SUNOS5/IRIX5, maybe AIX */
220 /* XXX no obvious difference between SUNOS5 and IRIX5 */
223 /* XXX what about AIX? */
228 * bytes[2,3] != (0,0); rules out Ultrix, could be
229 * DECOSF, SUNOS5, IRIX5, AIX, HP-UX, or UCX
231 if ((fhp
[8] == 12) && (fhp
[9] == 0)) {
234 else if ((fhp
[8] == 0) && (fhp
[9] == 10)) {
235 /* could be SUNOS5/IRIX5, AIX, HP-UX */
236 if ((fhp
[7] == 0) && (fhp
[6] == 0) &&
237 (fhp
[5] == 0) && (fhp
[4] == 0)) {
238 /* XXX is this always true of HP-UX? */
241 else if (fhp
[7] == 2) {
242 /* This would be MNT_NFS on AIX, which is impossible */
243 fhtype
= FHT_SUNOS5
; /* or maybe IRIX5 */
247 * XXX Could be SUNOS5/IRIX5 or AIX. I don't
248 * XXX see any way to disambiguate these, so
249 * XXX I'm going with the more likely guess.
250 * XXX Sorry, Big Blue.
252 fhtype
= FHT_SUNOS5
; /* or maybe IRIX5 */
260 fhtype
= FHT_UNKNOWN
;
266 /* XXX still needs to handle SUNOS3 */
270 fsidp
->Fsid_dev
.Minor
= fhp
[7];
271 fsidp
->Fsid_dev
.Major
= fhp
[6];
272 fsidp
->fsid_code
= 0;
274 temp
= make_uint32(fhp
[12], fhp
[13], fhp
[14], fhp
[15]);
282 fsidp
->Fsid_dev
.Minor
= fhp
[0];
283 fsidp
->Fsid_dev
.Major
= fhp
[1];
284 fsidp
->fsid_code
= 0;
286 temp
= make_uint32(fhp
[15], fhp
[14], fhp
[13], fhp
[12]);
290 *osnamep
= "BSD 4.4";
294 fsidp
->fsid_code
= make_uint32(fhp
[7], fhp
[6], fhp
[5], fhp
[4]);
295 /* XXX could ignore 3 high-order bytes */
297 temp
= make_uint32(fhp
[3], fhp
[2], fhp
[1], fhp
[0]);
298 fsidp
->Fsid_dev
.Minor
= temp
& 0xFFFFF;
299 fsidp
->Fsid_dev
.Major
= (temp
>>20) & 0xFFF;
301 temp
= make_uint32(fhp
[15], fhp
[14], fhp
[13], fhp
[12]);
308 fsidp
->Fsid_dev
.Minor
= fhp
[3];
309 fsidp
->Fsid_dev
.Major
= fhp
[2];
310 fsidp
->fsid_code
= 0;
312 temp
= make_uint32(fhp
[8], fhp
[9], fhp
[10], fhp
[11]);
320 fsidp
->Fsid_dev
.Minor
= make_uint16(fhp
[2], fhp
[3]);
321 fsidp
->Fsid_dev
.Major
= make_uint16(fhp
[0], fhp
[1]);
322 fsidp
->fsid_code
= make_uint32(fhp
[4], fhp
[5], fhp
[6], fhp
[7]);
324 temp
= make_uint32(fhp
[12], fhp
[13], fhp
[14], fhp
[15]);
334 * XXX - none of the heuristics above return this.
335 * Are there any SunOS 3.x systems around to care about?
343 fsidp
->Fsid_dev
.Minor
= fhp
[3];
344 fsidp
->Fsid_dev
.Major
= fhp
[2];
345 fsidp
->fsid_code
= make_uint32(fhp
[4], fhp
[5], fhp
[6], fhp
[7]);
347 temp
= make_uint32(fhp
[12], fhp
[13], fhp
[14], fhp
[15]);
355 temp
= make_uint16(fhp
[0], fhp
[1]);
356 fsidp
->Fsid_dev
.Major
= (temp
>>2) & 0x3FFF;
357 temp
= make_uint24(fhp
[1], fhp
[2], fhp
[3]);
358 fsidp
->Fsid_dev
.Minor
= temp
& 0x3FFFF;
359 fsidp
->fsid_code
= make_uint32(fhp
[4], fhp
[5], fhp
[6], fhp
[7]);
361 temp
= make_uint32(fhp
[12], fhp
[13], fhp
[14], fhp
[15]);
369 fsidp
->fsid_code
= 0;
370 fsidp
->Fsid_dev
.Minor
= fhp
[0];
371 fsidp
->Fsid_dev
.Major
= fhp
[1];
373 temp
= make_uint32(fhp
[7], fhp
[6], fhp
[5], fhp
[4]);
380 /* No numeric file system ID, so hash on the device-name */
381 if (sizeof(*fsidp
) >= 14) {
382 if (sizeof(*fsidp
) > 14)
383 memset((char *)fsidp
, 0, sizeof(*fsidp
));
384 /* just use the whole thing */
385 memcpy((char *)fsidp
, (char *)fh
, 14);
388 u_int32_t tempa
[4]; /* at least 16 bytes, maybe more */
390 memset((char *)tempa
, 0, sizeof(tempa
));
391 memcpy((char *)tempa
, (char *)fh
, 14); /* ensure alignment */
392 fsidp
->Fsid_dev
.Minor
= tempa
[0] + (tempa
[1]<<1);
393 fsidp
->Fsid_dev
.Major
= tempa
[2] + (tempa
[3]<<1);
394 fsidp
->fsid_code
= 0;
397 /* VMS file ID is: (RVN, FidHi, FidLo) */
398 *inop
= make_uint32(fhp
[26], fhp
[27], fhp
[23], fhp
[22]);
400 /* Caller must save (and null-terminate?) this value */
402 *fsnamep
= (char *)&(fhp
[1]);
409 fsidp
->Fsid_dev
.Minor
= make_uint16(fhp
[2], fhp
[3]);
410 fsidp
->Fsid_dev
.Major
= make_uint16(fhp
[0], fhp
[1]);
411 fsidp
->fsid_code
= make_uint32(fhp
[4], fhp
[5], fhp
[6], fhp
[7]);
413 temp
= make_uint32(fhp
[12], fhp
[13], fhp
[14], fhp
[15]);
421 fsidp
->Fsid_dev
.Major
= fhp
[0];
422 temp
= make_uint24(fhp
[1], fhp
[2], fhp
[3]);
423 fsidp
->Fsid_dev
.Minor
= temp
;
424 fsidp
->fsid_code
= make_uint32(fhp
[4], fhp
[5], fhp
[6], fhp
[7]);
426 temp
= make_uint32(fhp
[12], fhp
[13], fhp
[14], fhp
[15]);
436 for (i
= 0; i
< 32; i
++)
437 (void)fprintf(stderr
, "%x.", fhp
[i
]);
438 (void)fprintf(stderr
, "\n");
440 /* Save the actual handle, so it can be display with -u */
441 for (i
= 0; i
< 32; i
++)
442 (void)snprintf(&(fsidp
->Opaque_Handle
[i
*2]), 3, "%.2X", fhp
[i
]);
444 /* XXX for now, give "bogus" values to aid debugging */
445 fsidp
->fsid_code
= 0;
446 fsidp
->Fsid_dev
.Minor
= 257;
447 fsidp
->Fsid_dev
.Major
= 257;
450 /* display will show this string instead of (257,257) */
452 *fsnamep
= "Unknown";
455 *osnamep
= "Unknown";
462 * Is this a VMS UCX file handle?
464 * (1) leading code byte [XXX not yet]
465 * (2) followed by string of printing chars & spaces
466 * (3) followed by string of nulls
470 const unsigned char *fhp
;
475 for (i
= 1; i
< 14; i
++) {
476 if (isprint(fhp
[i
])) {
482 else if (fhp
[i
] == 0) {