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