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