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