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