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