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