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