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