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