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