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