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