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