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