xref: /dragonfly/contrib/tcpdump/parsenfsfh.c (revision 25a2db75)
1 /*
2  * Copyright (c) 1993, 1994 Jeffrey C. Mogul, Digital Equipment Corporation,
3  * Western Research Laboratory. All rights reserved.
4  * Copyright (c) 2001 Compaq Computer Corporation. All rights reserved.
5  *
6  *  Permission to use, copy, and modify this software and its
7  *  documentation is hereby granted only under the following terms and
8  *  conditions.  Both the above copyright notice and this permission
9  *  notice must appear in all copies of the software, derivative works
10  *  or modified versions, and any portions thereof, and both notices
11  *  must appear in supporting documentation.
12  *
13  *  Redistribution and use in source and binary forms, with or without
14  *  modification, are permitted provided that the following conditions
15  *  are met:
16  *    1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  *    2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in
20  *    the documentation and/or other materials provided with the
21  *    distribution.
22  *
23  *  THE SOFTWARE IS PROVIDED "AS IS" AND COMPAQ COMPUTER CORPORATION
24  *  DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
25  *  ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.   IN NO
26  *  EVENT SHALL COMPAQ COMPUTER CORPORATION BE LIABLE FOR ANY
27  *  SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
28  *  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
29  *  AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
30  *  OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
31  *  SOFTWARE.
32  */
33 
34 /*
35  * parsenfsfh.c - portable parser for NFS file handles
36  *			uses all sorts of heuristics
37  *
38  * Jeffrey C. Mogul
39  * Digital Equipment Corporation
40  * Western Research Laboratory
41  */
42 
43 #ifndef lint
44 static const char rcsid[] _U_ =
45     "@(#) $Header: /tcpdump/master/tcpdump/parsenfsfh.c,v 1.29 2006-06-13 22:21:38 guy Exp $ (LBL)";
46 #endif
47 
48 #ifdef HAVE_CONFIG_H
49 #include "config.h"
50 #endif
51 
52 #include <tcpdump-stdinc.h>
53 
54 #include <stdio.h>
55 #include <string.h>
56 
57 #include "interface.h"
58 #include "nfsfh.h"
59 
60 /*
61  * This routine attempts to parse a file handle (in network byte order),
62  * using heuristics to guess what kind of format it is in.  See the
63  * file "fhandle_layouts" for a detailed description of the various
64  * patterns we know about.
65  *
66  * The file handle is parsed into our internal representation of a
67  * file-system id, and an internal representation of an inode-number.
68  */
69 
70 #define	FHT_UNKNOWN	0
71 #define	FHT_AUSPEX	1
72 #define	FHT_DECOSF	2
73 #define	FHT_IRIX4	3
74 #define	FHT_IRIX5	4
75 #define	FHT_SUNOS3	5
76 #define	FHT_SUNOS4	6
77 #define	FHT_ULTRIX	7
78 #define	FHT_VMSUCX	8
79 #define	FHT_SUNOS5	9
80 #define	FHT_AIX32	10
81 #define	FHT_HPUX9	11
82 #define	FHT_BSD44	12
83 
84 #ifdef	ultrix
85 /* Nasty hack to keep the Ultrix C compiler from emitting bogus warnings */
86 #define	XFF(x)	((u_int32_t)(x))
87 #else
88 #define	XFF(x)	(x)
89 #endif
90 
91 #define	make_uint32(msb,b,c,lsb)\
92 	(XFF(lsb) + (XFF(c)<<8) + (XFF(b)<<16) + (XFF(msb)<<24))
93 
94 #define	make_uint24(msb,b, lsb)\
95 	(XFF(lsb) + (XFF(b)<<8) + (XFF(msb)<<16))
96 
97 #define	make_uint16(msb,lsb)\
98 	(XFF(lsb) + (XFF(msb)<<8))
99 
100 #ifdef	__alpha
101 	/* or other 64-bit systems */
102 #define	make_uint48(msb,b,c,d,e,lsb)\
103 	((lsb) + ((e)<<8) + ((d)<<16) + ((c)<<24) + ((b)<<32) + ((msb)<<40))
104 #else
105 	/* on 32-bit systems ignore high-order bits */
106 #define	make_uint48(msb,b,c,d,e,lsb)\
107 	((lsb) + ((e)<<8) + ((d)<<16) + ((c)<<24))
108 #endif
109 
110 static int is_UCX(const unsigned char *);
111 
112 void
113 Parse_fh(fh, len, fsidp, inop, osnamep, fsnamep, ourself)
114 register const unsigned char *fh;
115 int len _U_;
116 my_fsid *fsidp;
117 ino_t *inop;
118 const char **osnamep;		/* if non-NULL, return OS name here */
119 const char **fsnamep;		/* if non-NULL, return server fs name here (for VMS) */
120 int ourself;		/* true if file handle was generated on this host */
121 {
122 	register const unsigned char *fhp = fh;
123 	u_int32_t temp;
124 	int fhtype = FHT_UNKNOWN;
125 	int i;
126 
127 	if (ourself) {
128 	    /* File handle generated on this host, no need for guessing */
129 #if	defined(IRIX40)
130 	    fhtype = FHT_IRIX4;
131 #endif
132 #if	defined(IRIX50)
133 	    fhtype = FHT_IRIX5;
134 #endif
135 #if	defined(IRIX51)
136 	    fhtype = FHT_IRIX5;
137 #endif
138 #if	defined(SUNOS4)
139 	    fhtype = FHT_SUNOS4;
140 #endif
141 #if	defined(SUNOS5)
142 	    fhtype = FHT_SUNOS5;
143 #endif
144 #if	defined(ultrix)
145 	    fhtype = FHT_ULTRIX;
146 #endif
147 #if	defined(__osf__)
148 	    fhtype = FHT_DECOSF;
149 #endif
150 #if	defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__) \
151      || defined(__OpenBSD__)
152 	    fhtype = FHT_BSD44;
153 #endif
154 	}
155 	/*
156 	 * This is basically a big decision tree
157 	 */
158 	else if ((fhp[0] == 0) && (fhp[1] == 0)) {
159 	    /* bytes[0,1] == (0,0); rules out Ultrix, IRIX5, SUNOS5 */
160 	    /* probably rules out HP-UX, AIX unless they allow major=0 */
161 	    if ((fhp[2] == 0) && (fhp[3] == 0)) {
162 		/* bytes[2,3] == (0,0); must be Auspex */
163 		/* XXX or could be Ultrix+MASSBUS "hp" disk? */
164 		fhtype = FHT_AUSPEX;
165 	    }
166 	    else {
167 		/*
168 		 * bytes[2,3] != (0,0); rules out Auspex, could be
169 		 * DECOSF, SUNOS4, or IRIX4
170 		 */
171 		if ((fhp[4] != 0) && (fhp[5] == 0) &&
172 			(fhp[8] == 12) && (fhp[9] == 0)) {
173 		    /* seems to be DECOSF, with minor == 0 */
174 		    fhtype = FHT_DECOSF;
175 		}
176 		else {
177 		    /* could be SUNOS4 or IRIX4 */
178 		    /* XXX the test of fhp[5] == 8 could be wrong */
179 		    if ((fhp[4] == 0) && (fhp[5] == 8) && (fhp[6] == 0) &&
180 			(fhp[7] == 0)) {
181 			/* looks like a length, not a file system typecode */
182 			fhtype = FHT_IRIX4;
183 		    }
184 		    else {
185 			/* by elimination */
186 			fhtype = FHT_SUNOS4;
187 		    }
188 		}
189 	    }
190 	}
191 	else {
192 	    /*
193 	     * bytes[0,1] != (0,0); rules out Auspex, IRIX4, SUNOS4
194 	     * could be IRIX5, DECOSF, UCX, Ultrix, SUNOS5
195 	     * could be AIX, HP-UX
196 	     */
197 	    if ((fhp[2] == 0) && (fhp[3] == 0)) {
198 		/*
199 		 * bytes[2,3] == (0,0); rules out OSF, probably not UCX
200 		 * (unless the exported device name is just one letter!),
201 		 * could be Ultrix, IRIX5, AIX, or SUNOS5
202 		 * might be HP-UX (depends on their values for minor devs)
203 		 */
204 		if ((fhp[6] == 0) && (fhp[7] == 0)) {
205 		    fhtype = FHT_BSD44;
206 		}
207 		/*XXX we probably only need to test of these two bytes */
208 		else if ((fhp[21] == 0) && (fhp[23] == 0)) {
209 		    fhtype = FHT_ULTRIX;
210 		}
211 		else {
212 		    /* Could be SUNOS5/IRIX5, maybe AIX */
213 		    /* XXX no obvious difference between SUNOS5 and IRIX5 */
214 		    if (fhp[9] == 10)
215 			fhtype = FHT_SUNOS5;
216 		    /* XXX what about AIX? */
217 		}
218 	    }
219 	    else {
220 		/*
221 		 * bytes[2,3] != (0,0); rules out Ultrix, could be
222 		 * DECOSF, SUNOS5, IRIX5, AIX, HP-UX, or UCX
223 		 */
224 		if ((fhp[8] == 12) && (fhp[9] == 0)) {
225 		    fhtype = FHT_DECOSF;
226 		}
227 		else if ((fhp[8] == 0) && (fhp[9] == 10)) {
228 		    /* could be SUNOS5/IRIX5, AIX, HP-UX */
229 		    if ((fhp[7] == 0) && (fhp[6] == 0) &&
230 			(fhp[5] == 0) && (fhp[4] == 0)) {
231 			/* XXX is this always true of HP-UX? */
232 			fhtype = FHT_HPUX9;
233 		    }
234 		    else if (fhp[7] == 2) {
235 			/* This would be MNT_NFS on AIX, which is impossible */
236 			fhtype = FHT_SUNOS5;	/* or maybe IRIX5 */
237 		    }
238 		    else {
239 			/*
240 			 * XXX Could be SUNOS5/IRIX5 or AIX.  I don't
241 			 * XXX see any way to disambiguate these, so
242 			 * XXX I'm going with the more likely guess.
243 			 * XXX Sorry, Big Blue.
244 			 */
245 			fhtype = FHT_SUNOS5;	/* or maybe IRIX5 */
246 		    }
247 	        }
248 		else {
249 		    if (is_UCX(fhp)) {
250 			fhtype = FHT_VMSUCX;
251 		    }
252 		    else {
253 			fhtype = FHT_UNKNOWN;
254 		    }
255 		}
256 	    }
257 	}
258 
259 	/* XXX still needs to handle SUNOS3 */
260 
261 	switch (fhtype) {
262 	case FHT_AUSPEX:
263 	    fsidp->Fsid_dev.Minor = fhp[7];
264 	    fsidp->Fsid_dev.Major = fhp[6];
265 	    fsidp->fsid_code = 0;
266 
267 	    temp = make_uint32(fhp[12], fhp[13], fhp[14], fhp[15]);
268 	    *inop = temp;
269 
270 	    if (osnamep)
271 		*osnamep = "Auspex";
272 	    break;
273 
274 	case FHT_BSD44:
275 	    fsidp->Fsid_dev.Minor = fhp[0];
276 	    fsidp->Fsid_dev.Major = fhp[1];
277 	    fsidp->fsid_code = 0;
278 
279 	    temp = make_uint32(fhp[15], fhp[14], fhp[13], fhp[12]);
280 	    *inop = temp;
281 
282 	    if (osnamep)
283 		*osnamep = "BSD 4.4";
284 	    break;
285 
286 	case FHT_DECOSF:
287 	    fsidp->fsid_code = make_uint32(fhp[7], fhp[6], fhp[5], fhp[4]);
288 			/* XXX could ignore 3 high-order bytes */
289 
290 	    temp = make_uint32(fhp[3], fhp[2], fhp[1], fhp[0]);
291 	    fsidp->Fsid_dev.Minor = temp & 0xFFFFF;
292 	    fsidp->Fsid_dev.Major = (temp>>20) & 0xFFF;
293 
294 	    temp = make_uint32(fhp[15], fhp[14], fhp[13], fhp[12]);
295 	    *inop = temp;
296 	    if (osnamep)
297 		*osnamep = "OSF";
298 	    break;
299 
300 	case FHT_IRIX4:
301 	    fsidp->Fsid_dev.Minor = fhp[3];
302 	    fsidp->Fsid_dev.Major = fhp[2];
303 	    fsidp->fsid_code = 0;
304 
305 	    temp = make_uint32(fhp[8], fhp[9], fhp[10], fhp[11]);
306 	    *inop = temp;
307 
308 	    if (osnamep)
309 		*osnamep = "IRIX4";
310 	    break;
311 
312 	case FHT_IRIX5:
313 	    fsidp->Fsid_dev.Minor = make_uint16(fhp[2], fhp[3]);
314 	    fsidp->Fsid_dev.Major = make_uint16(fhp[0], fhp[1]);
315 	    fsidp->fsid_code = make_uint32(fhp[4], fhp[5], fhp[6], fhp[7]);
316 
317 	    temp = make_uint32(fhp[12], fhp[13], fhp[14], fhp[15]);
318 	    *inop = temp;
319 
320 	    if (osnamep)
321 		*osnamep = "IRIX5";
322 	    break;
323 
324 #ifdef notdef
325 	case FHT_SUNOS3:
326 	    /*
327 	     * XXX - none of the heuristics above return this.
328 	     * Are there any SunOS 3.x systems around to care about?
329 	     */
330 	    if (osnamep)
331 		*osnamep = "SUNOS3";
332 	    break;
333 #endif
334 
335 	case FHT_SUNOS4:
336 	    fsidp->Fsid_dev.Minor = fhp[3];
337 	    fsidp->Fsid_dev.Major = fhp[2];
338 	    fsidp->fsid_code = make_uint32(fhp[4], fhp[5], fhp[6], fhp[7]);
339 
340 	    temp = make_uint32(fhp[12], fhp[13], fhp[14], fhp[15]);
341 	    *inop = temp;
342 
343 	    if (osnamep)
344 		*osnamep = "SUNOS4";
345 	    break;
346 
347 	case FHT_SUNOS5:
348 	    temp = make_uint16(fhp[0], fhp[1]);
349 	    fsidp->Fsid_dev.Major = (temp>>2) &  0x3FFF;
350 	    temp = make_uint24(fhp[1], fhp[2], fhp[3]);
351 	    fsidp->Fsid_dev.Minor = temp & 0x3FFFF;
352 	    fsidp->fsid_code = make_uint32(fhp[4], fhp[5], fhp[6], fhp[7]);
353 
354 	    temp = make_uint32(fhp[12], fhp[13], fhp[14], fhp[15]);
355 	    *inop = temp;
356 
357 	    if (osnamep)
358 		*osnamep = "SUNOS5";
359 	    break;
360 
361 	case FHT_ULTRIX:
362 	    fsidp->fsid_code = 0;
363 	    fsidp->Fsid_dev.Minor = fhp[0];
364 	    fsidp->Fsid_dev.Major = fhp[1];
365 
366 	    temp = make_uint32(fhp[7], fhp[6], fhp[5], fhp[4]);
367 	    *inop = temp;
368 	    if (osnamep)
369 		*osnamep = "Ultrix";
370 	    break;
371 
372 	case FHT_VMSUCX:
373 	    /* No numeric file system ID, so hash on the device-name */
374 	    if (sizeof(*fsidp) >= 14) {
375 		if (sizeof(*fsidp) > 14)
376 		    memset((char *)fsidp, 0, sizeof(*fsidp));
377 		/* just use the whole thing */
378 		memcpy((char *)fsidp, (char *)fh, 14);
379 	    }
380 	    else {
381 		u_int32_t tempa[4];	/* at least 16 bytes, maybe more */
382 
383 		memset((char *)tempa, 0, sizeof(tempa));
384 		memcpy((char *)tempa, (char *)fh, 14); /* ensure alignment */
385 		fsidp->Fsid_dev.Minor = tempa[0] + (tempa[1]<<1);
386 		fsidp->Fsid_dev.Major = tempa[2] + (tempa[3]<<1);
387 		fsidp->fsid_code = 0;
388 	    }
389 
390 	    /* VMS file ID is: (RVN, FidHi, FidLo) */
391 	    *inop = make_uint32(fhp[26], fhp[27], fhp[23], fhp[22]);
392 
393 	    /* Caller must save (and null-terminate?) this value */
394 	    if (fsnamep)
395 		*fsnamep = (char *)&(fhp[1]);
396 
397 	    if (osnamep)
398 		*osnamep = "VMS";
399 	    break;
400 
401 	case FHT_AIX32:
402 	    fsidp->Fsid_dev.Minor = make_uint16(fhp[2], fhp[3]);
403 	    fsidp->Fsid_dev.Major = make_uint16(fhp[0], fhp[1]);
404 	    fsidp->fsid_code = make_uint32(fhp[4], fhp[5], fhp[6], fhp[7]);
405 
406 	    temp = make_uint32(fhp[12], fhp[13], fhp[14], fhp[15]);
407 	    *inop = temp;
408 
409 	    if (osnamep)
410 		*osnamep = "AIX32";
411 	    break;
412 
413 	case FHT_HPUX9:
414 	    fsidp->Fsid_dev.Major = fhp[0];
415 	    temp = make_uint24(fhp[1], fhp[2], fhp[3]);
416 	    fsidp->Fsid_dev.Minor = temp;
417 	    fsidp->fsid_code = make_uint32(fhp[4], fhp[5], fhp[6], fhp[7]);
418 
419 	    temp = make_uint32(fhp[12], fhp[13], fhp[14], fhp[15]);
420 	    *inop = temp;
421 
422 	    if (osnamep)
423 		*osnamep = "HPUX9";
424 	    break;
425 
426 	case FHT_UNKNOWN:
427 #ifdef DEBUG
428 	    /* XXX debugging */
429 	    for (i = 0; i < 32; i++)
430 		(void)fprintf(stderr, "%x.", fhp[i]);
431 	    (void)fprintf(stderr, "\n");
432 #endif
433 	    /* Save the actual handle, so it can be display with -u */
434 	    for (i = 0; i < 32; i++)
435 	    	(void)snprintf(&(fsidp->Opaque_Handle[i*2]), 3, "%.2X", fhp[i]);
436 
437 	    /* XXX for now, give "bogus" values to aid debugging */
438 	    fsidp->fsid_code = 0;
439 	    fsidp->Fsid_dev.Minor = 257;
440 	    fsidp->Fsid_dev.Major = 257;
441 	    *inop = 1;
442 
443 	    /* display will show this string instead of (257,257) */
444 	    if (fsnamep)
445 		*fsnamep = "Unknown";
446 
447 	    if (osnamep)
448 		*osnamep = "Unknown";
449 	    break;
450 
451 	}
452 }
453 
454 /*
455  * Is this a VMS UCX file handle?
456  *	Check for:
457  *	(1) leading code byte	[XXX not yet]
458  *	(2) followed by string of printing chars & spaces
459  *	(3) followed by string of nulls
460  */
461 static int
462 is_UCX(fhp)
463 const unsigned char *fhp;
464 {
465 	register int i;
466 	int seen_null = 0;
467 
468 	for (i = 1; i < 14; i++) {
469 	    if (isprint(fhp[i])) {
470 		if (seen_null)
471 		   return(0);
472 		else
473 		   continue;
474 	    }
475 	    else if (fhp[i] == 0) {
476 		seen_null = 1;
477 		continue;
478 	    }
479 	    else
480 		return(0);
481 	}
482 
483 	return(1);
484 }
485