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