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