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