1 /* 2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that: (1) source code distributions 7 * retain the above copyright notice and this paragraph in its entirety, (2) 8 * distributions including binary code include the above copyright notice and 9 * this paragraph in its entirety in the documentation or other materials 10 * provided with the distribution, and (3) all advertising materials mentioning 11 * features or use of this software display the following acknowledgement: 12 * ``This product includes software developed by the University of California, 13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 14 * the University nor the names of its contributors may be used to endorse 15 * or promote products derived from this software without specific prior 16 * written permission. 17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 20 * 21 * Format and print AppleTalk packets. 22 * 23 * $FreeBSD$ 24 */ 25 26 #ifndef lint 27 static const char rcsid[] _U_ = 28 "@(#) $Header: /tcpdump/master/tcpdump/print-atalk.c,v 1.78.2.2 2003/11/16 08:51:11 guy Exp $ (LBL)"; 29 #endif 30 31 #ifdef HAVE_CONFIG_H 32 #include "config.h" 33 #endif 34 35 #include <tcpdump-stdinc.h> 36 37 #include <stdio.h> 38 #include <stdlib.h> 39 #include <string.h> 40 #include <pcap.h> 41 42 #include "interface.h" 43 #include "addrtoname.h" 44 #include "ethertype.h" 45 #include "extract.h" /* must come after interface.h */ 46 #include "appletalk.h" 47 48 static struct tok type2str[] = { 49 { ddpRTMP, "rtmp" }, 50 { ddpRTMPrequest, "rtmpReq" }, 51 { ddpECHO, "echo" }, 52 { ddpIP, "IP" }, 53 { ddpARP, "ARP" }, 54 { ddpKLAP, "KLAP" }, 55 { 0, NULL } 56 }; 57 58 struct aarp { 59 u_int16_t htype, ptype; 60 u_int8_t halen, palen; 61 u_int16_t op; 62 u_int8_t hsaddr[6]; 63 u_int8_t psaddr[4]; 64 u_int8_t hdaddr[6]; 65 u_int8_t pdaddr[4]; 66 }; 67 68 static char tstr[] = "[|atalk]"; 69 70 static void atp_print(const struct atATP *, u_int); 71 static void atp_bitmap_print(u_char); 72 static void nbp_print(const struct atNBP *, u_int, u_short, u_char, u_char); 73 static const char *print_cstring(const char *, const u_char *); 74 static const struct atNBPtuple *nbp_tuple_print(const struct atNBPtuple *, 75 const u_char *, 76 u_short, u_char, u_char); 77 static const struct atNBPtuple *nbp_name_print(const struct atNBPtuple *, 78 const u_char *); 79 static const char *ataddr_string(u_short, u_char); 80 static void ddp_print(const u_char *, u_int, int, u_short, u_char, u_char); 81 static const char *ddpskt_string(int); 82 83 /* 84 * Print LLAP packets received on a physical LocalTalk interface. 85 */ 86 u_int 87 ltalk_if_print(const struct pcap_pkthdr *h, const u_char *p) 88 { 89 return (llap_print(p, h->caplen)); 90 } 91 92 /* 93 * Print AppleTalk LLAP packets. 94 */ 95 u_int 96 llap_print(register const u_char *bp, u_int length) 97 { 98 register const struct LAP *lp; 99 register const struct atDDP *dp; 100 register const struct atShortDDP *sdp; 101 u_short snet; 102 u_int hdrlen; 103 104 #if 0 105 /* 106 * Our packet is on a 4-byte boundary, as we're either called 107 * directly from a top-level link-layer printer (ltalk_if_print) 108 * or from the UDP printer. The LLAP+DDP header is a multiple 109 * of 4 bytes in length, so the DDP payload is also on a 4-byte 110 * boundary, and we don't need to align it before calling 111 * "ddp_print()". 112 */ 113 lp = (const struct LAP *)bp; 114 bp += sizeof(*lp); 115 length -= sizeof(*lp); 116 #else 117 { 118 static struct LAP lp_ = {0, 0, lapDDP}; 119 lp = &lp_; 120 } 121 #endif 122 hdrlen = sizeof(*lp); 123 switch (lp->type) { 124 125 case lapShortDDP: 126 if (length < ddpSSize) { 127 (void)printf(" [|sddp %d]", length); 128 return (length); 129 } 130 sdp = (const struct atShortDDP *)bp; 131 printf("%s.%s", 132 ataddr_string(0, lp->src), ddpskt_string(sdp->srcSkt)); 133 printf(" > %s.%s:", 134 ataddr_string(0, lp->dst), ddpskt_string(sdp->dstSkt)); 135 bp += ddpSSize; 136 length -= ddpSSize; 137 hdrlen += ddpSSize; 138 ddp_print(bp, length, sdp->type, 0, lp->src, sdp->srcSkt); 139 break; 140 141 case lapDDP: 142 if (length < ddpSize) { 143 (void)printf(" [|ddp %d]", length); 144 return (length); 145 } 146 dp = (const struct atDDP *)bp; 147 snet = EXTRACT_16BITS(&dp->srcNet); 148 printf("%s.%s", ataddr_string(snet, dp->srcNode), 149 ddpskt_string(dp->srcSkt)); 150 printf(" > %s.%s:", 151 ataddr_string(EXTRACT_16BITS(&dp->dstNet), dp->dstNode), 152 ddpskt_string(dp->dstSkt)); 153 bp += ddpSize; 154 length -= ddpSize; 155 hdrlen += ddpSize; 156 ddp_print(bp, length, dp->type, snet, dp->srcNode, dp->srcSkt); 157 break; 158 159 #ifdef notdef 160 case lapKLAP: 161 klap_print(bp, length); 162 break; 163 #endif 164 165 default: 166 printf("%d > %d at-lap#%d %d", 167 lp->src, lp->dst, lp->type, length); 168 break; 169 } 170 return (hdrlen); 171 } 172 173 /* 174 * Print EtherTalk/TokenTalk packets (or FDDITalk, or whatever it's called 175 * when it runs over FDDI; yes, I've seen FDDI captures with AppleTalk 176 * packets in them). 177 */ 178 void 179 atalk_print(register const u_char *bp, u_int length) 180 { 181 register const struct atDDP *dp; 182 u_short snet; 183 184 if (length < ddpSize) { 185 (void)printf(" [|ddp %d]", length); 186 return; 187 } 188 dp = (const struct atDDP *)bp; 189 snet = EXTRACT_16BITS(&dp->srcNet); 190 printf("%s.%s", ataddr_string(snet, dp->srcNode), 191 ddpskt_string(dp->srcSkt)); 192 printf(" > %s.%s:", 193 ataddr_string(EXTRACT_16BITS(&dp->dstNet), dp->dstNode), 194 ddpskt_string(dp->dstSkt)); 195 bp += ddpSize; 196 length -= ddpSize; 197 ddp_print(bp, length, dp->type, snet, dp->srcNode, dp->srcSkt); 198 } 199 200 /* XXX should probably pass in the snap header and do checks like arp_print() */ 201 void 202 aarp_print(register const u_char *bp, u_int length) 203 { 204 register const struct aarp *ap; 205 206 #define AT(member) ataddr_string((ap->member[1]<<8)|ap->member[2],ap->member[3]) 207 208 printf("aarp "); 209 ap = (const struct aarp *)bp; 210 if (EXTRACT_16BITS(&ap->htype) == 1 && 211 EXTRACT_16BITS(&ap->ptype) == ETHERTYPE_ATALK && 212 ap->halen == 6 && ap->palen == 4 ) 213 switch (EXTRACT_16BITS(&ap->op)) { 214 215 case 1: /* request */ 216 (void)printf("who-has %s tell %s", 217 AT(pdaddr), AT(psaddr)); 218 return; 219 220 case 2: /* response */ 221 (void)printf("reply %s is-at %s", 222 AT(psaddr), etheraddr_string(ap->hsaddr)); 223 return; 224 225 case 3: /* probe (oy!) */ 226 (void)printf("probe %s tell %s", 227 AT(pdaddr), AT(psaddr)); 228 return; 229 } 230 (void)printf("len %u op %u htype %u ptype %#x halen %u palen %u", 231 length, EXTRACT_16BITS(&ap->op), EXTRACT_16BITS(&ap->htype), 232 EXTRACT_16BITS(&ap->ptype), ap->halen, ap->palen); 233 } 234 235 /* 236 * Print AppleTalk Datagram Delivery Protocol packets. 237 */ 238 static void 239 ddp_print(register const u_char *bp, register u_int length, register int t, 240 register u_short snet, register u_char snode, u_char skt) 241 { 242 243 switch (t) { 244 245 case ddpNBP: 246 nbp_print((const struct atNBP *)bp, length, snet, snode, skt); 247 break; 248 249 case ddpATP: 250 atp_print((const struct atATP *)bp, length); 251 break; 252 253 default: 254 (void)printf(" at-%s %d", tok2str(type2str, NULL, t), length); 255 break; 256 } 257 } 258 259 static void 260 atp_print(register const struct atATP *ap, u_int length) 261 { 262 char c; 263 u_int32_t data; 264 265 if ((const u_char *)(ap + 1) > snapend) { 266 /* Just bail if we don't have the whole chunk. */ 267 fputs(tstr, stdout); 268 return; 269 } 270 length -= sizeof(*ap); 271 switch (ap->control & 0xc0) { 272 273 case atpReqCode: 274 (void)printf(" atp-req%s %d", 275 ap->control & atpXO? " " : "*", 276 EXTRACT_16BITS(&ap->transID)); 277 278 atp_bitmap_print(ap->bitmap); 279 280 if (length != 0) 281 (void)printf(" [len=%d]", length); 282 283 switch (ap->control & (atpEOM|atpSTS)) { 284 case atpEOM: 285 (void)printf(" [EOM]"); 286 break; 287 case atpSTS: 288 (void)printf(" [STS]"); 289 break; 290 case atpEOM|atpSTS: 291 (void)printf(" [EOM,STS]"); 292 break; 293 } 294 break; 295 296 case atpRspCode: 297 (void)printf(" atp-resp%s%d:%d (%d)", 298 ap->control & atpEOM? "*" : " ", 299 EXTRACT_16BITS(&ap->transID), ap->bitmap, length); 300 switch (ap->control & (atpXO|atpSTS)) { 301 case atpXO: 302 (void)printf(" [XO]"); 303 break; 304 case atpSTS: 305 (void)printf(" [STS]"); 306 break; 307 case atpXO|atpSTS: 308 (void)printf(" [XO,STS]"); 309 break; 310 } 311 break; 312 313 case atpRelCode: 314 (void)printf(" atp-rel %d", EXTRACT_16BITS(&ap->transID)); 315 316 atp_bitmap_print(ap->bitmap); 317 318 /* length should be zero */ 319 if (length) 320 (void)printf(" [len=%d]", length); 321 322 /* there shouldn't be any control flags */ 323 if (ap->control & (atpXO|atpEOM|atpSTS)) { 324 c = '['; 325 if (ap->control & atpXO) { 326 (void)printf("%cXO", c); 327 c = ','; 328 } 329 if (ap->control & atpEOM) { 330 (void)printf("%cEOM", c); 331 c = ','; 332 } 333 if (ap->control & atpSTS) { 334 (void)printf("%cSTS", c); 335 c = ','; 336 } 337 (void)printf("]"); 338 } 339 break; 340 341 default: 342 (void)printf(" atp-0x%x %d (%d)", ap->control, 343 EXTRACT_16BITS(&ap->transID), length); 344 break; 345 } 346 data = EXTRACT_32BITS(&ap->userData); 347 if (data != 0) 348 (void)printf(" 0x%x", data); 349 } 350 351 static void 352 atp_bitmap_print(register u_char bm) 353 { 354 register char c; 355 register int i; 356 357 /* 358 * The '& 0xff' below is needed for compilers that want to sign 359 * extend a u_char, which is the case with the Ultrix compiler. 360 * (gcc is smart enough to eliminate it, at least on the Sparc). 361 */ 362 if ((bm + 1) & (bm & 0xff)) { 363 c = '<'; 364 for (i = 0; bm; ++i) { 365 if (bm & 1) { 366 (void)printf("%c%d", c, i); 367 c = ','; 368 } 369 bm >>= 1; 370 } 371 (void)printf(">"); 372 } else { 373 for (i = 0; bm; ++i) 374 bm >>= 1; 375 if (i > 1) 376 (void)printf("<0-%d>", i - 1); 377 else 378 (void)printf("<0>"); 379 } 380 } 381 382 static void 383 nbp_print(register const struct atNBP *np, u_int length, register u_short snet, 384 register u_char snode, register u_char skt) 385 { 386 register const struct atNBPtuple *tp = 387 (const struct atNBPtuple *)((u_char *)np + nbpHeaderSize); 388 int i; 389 const u_char *ep; 390 391 if (length < nbpHeaderSize) { 392 (void)printf(" truncated-nbp %d", length); 393 return; 394 } 395 396 length -= nbpHeaderSize; 397 if (length < 8) { 398 /* must be room for at least one tuple */ 399 (void)printf(" truncated-nbp %d", length + nbpHeaderSize); 400 return; 401 } 402 /* ep points to end of available data */ 403 ep = snapend; 404 if ((const u_char *)tp > ep) { 405 fputs(tstr, stdout); 406 return; 407 } 408 switch (i = np->control & 0xf0) { 409 410 case nbpBrRq: 411 case nbpLkUp: 412 (void)printf(i == nbpLkUp? " nbp-lkup %d:":" nbp-brRq %d:", 413 np->id); 414 if ((const u_char *)(tp + 1) > ep) { 415 fputs(tstr, stdout); 416 return; 417 } 418 (void)nbp_name_print(tp, ep); 419 /* 420 * look for anomalies: the spec says there can only 421 * be one tuple, the address must match the source 422 * address and the enumerator should be zero. 423 */ 424 if ((np->control & 0xf) != 1) 425 (void)printf(" [ntup=%d]", np->control & 0xf); 426 if (tp->enumerator) 427 (void)printf(" [enum=%d]", tp->enumerator); 428 if (EXTRACT_16BITS(&tp->net) != snet || 429 tp->node != snode || tp->skt != skt) 430 (void)printf(" [addr=%s.%d]", 431 ataddr_string(EXTRACT_16BITS(&tp->net), 432 tp->node), tp->skt); 433 break; 434 435 case nbpLkUpReply: 436 (void)printf(" nbp-reply %d:", np->id); 437 438 /* print each of the tuples in the reply */ 439 for (i = np->control & 0xf; --i >= 0 && tp; ) 440 tp = nbp_tuple_print(tp, ep, snet, snode, skt); 441 break; 442 443 default: 444 (void)printf(" nbp-0x%x %d (%d)", np->control, np->id, 445 length); 446 break; 447 } 448 } 449 450 /* print a counted string */ 451 static const char * 452 print_cstring(register const char *cp, register const u_char *ep) 453 { 454 register u_int length; 455 456 if (cp >= (const char *)ep) { 457 fputs(tstr, stdout); 458 return (0); 459 } 460 length = *cp++; 461 462 /* Spec says string can be at most 32 bytes long */ 463 if (length > 32) { 464 (void)printf("[len=%u]", length); 465 return (0); 466 } 467 while ((int)--length >= 0) { 468 if (cp >= (const char *)ep) { 469 fputs(tstr, stdout); 470 return (0); 471 } 472 putchar(*cp++); 473 } 474 return (cp); 475 } 476 477 static const struct atNBPtuple * 478 nbp_tuple_print(register const struct atNBPtuple *tp, 479 register const u_char *ep, 480 register u_short snet, register u_char snode, 481 register u_char skt) 482 { 483 register const struct atNBPtuple *tpn; 484 485 if ((const u_char *)(tp + 1) > ep) { 486 fputs(tstr, stdout); 487 return 0; 488 } 489 tpn = nbp_name_print(tp, ep); 490 491 /* if the enumerator isn't 1, print it */ 492 if (tp->enumerator != 1) 493 (void)printf("(%d)", tp->enumerator); 494 495 /* if the socket doesn't match the src socket, print it */ 496 if (tp->skt != skt) 497 (void)printf(" %d", tp->skt); 498 499 /* if the address doesn't match the src address, it's an anomaly */ 500 if (EXTRACT_16BITS(&tp->net) != snet || tp->node != snode) 501 (void)printf(" [addr=%s]", 502 ataddr_string(EXTRACT_16BITS(&tp->net), tp->node)); 503 504 return (tpn); 505 } 506 507 static const struct atNBPtuple * 508 nbp_name_print(const struct atNBPtuple *tp, register const u_char *ep) 509 { 510 register const char *cp = (const char *)tp + nbpTupleSize; 511 512 putchar(' '); 513 514 /* Object */ 515 putchar('"'); 516 if ((cp = print_cstring(cp, ep)) != NULL) { 517 /* Type */ 518 putchar(':'); 519 if ((cp = print_cstring(cp, ep)) != NULL) { 520 /* Zone */ 521 putchar('@'); 522 if ((cp = print_cstring(cp, ep)) != NULL) 523 putchar('"'); 524 } 525 } 526 return ((const struct atNBPtuple *)cp); 527 } 528 529 530 #define HASHNAMESIZE 4096 531 532 struct hnamemem { 533 int addr; 534 char *name; 535 struct hnamemem *nxt; 536 }; 537 538 static struct hnamemem hnametable[HASHNAMESIZE]; 539 540 static const char * 541 ataddr_string(u_short atnet, u_char athost) 542 { 543 register struct hnamemem *tp, *tp2; 544 register int i = (atnet << 8) | athost; 545 char nambuf[MAXHOSTNAMELEN + 20]; 546 static int first = 1; 547 FILE *fp; 548 549 /* 550 * if this is the first call, see if there's an AppleTalk 551 * number to name map file. 552 */ 553 if (first && (first = 0, !nflag) 554 && (fp = fopen("/etc/atalk.names", "r"))) { 555 char line[256]; 556 int i1, i2, i3; 557 558 while (fgets(line, sizeof(line), fp)) { 559 if (line[0] == '\n' || line[0] == 0 || line[0] == '#') 560 continue; 561 if (sscanf(line, "%d.%d.%d %256s", &i1, &i2, &i3, 562 nambuf) == 4) 563 /* got a hostname. */ 564 i3 |= ((i1 << 8) | i2) << 8; 565 else if (sscanf(line, "%d.%d %256s", &i1, &i2, 566 nambuf) == 3) 567 /* got a net name */ 568 i3 = (((i1 << 8) | i2) << 8) | 255; 569 else 570 continue; 571 572 for (tp = &hnametable[i2 & (HASHNAMESIZE-1)]; 573 tp->nxt; tp = tp->nxt) 574 ; 575 tp->addr = i2; 576 tp->nxt = newhnamemem(); 577 tp->name = strdup(nambuf); 578 } 579 fclose(fp); 580 } 581 582 for (tp = &hnametable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt) 583 if (tp->addr == i) 584 return (tp->name); 585 586 /* didn't have the node name -- see if we've got the net name */ 587 i |= 255; 588 for (tp2 = &hnametable[i & (HASHNAMESIZE-1)]; tp2->nxt; tp2 = tp2->nxt) 589 if (tp2->addr == i) { 590 tp->addr = (atnet << 8) | athost; 591 tp->nxt = newhnamemem(); 592 (void)snprintf(nambuf, sizeof(nambuf), "%s.%d", 593 tp2->name, athost); 594 tp->name = strdup(nambuf); 595 return (tp->name); 596 } 597 598 tp->addr = (atnet << 8) | athost; 599 tp->nxt = newhnamemem(); 600 if (athost != 255) 601 (void)snprintf(nambuf, sizeof(nambuf), "%d.%d.%d", 602 atnet >> 8, atnet & 0xff, athost); 603 else 604 (void)snprintf(nambuf, sizeof(nambuf), "%d.%d", atnet >> 8, 605 atnet & 0xff); 606 tp->name = strdup(nambuf); 607 608 return (tp->name); 609 } 610 611 static struct tok skt2str[] = { 612 { rtmpSkt, "rtmp" }, /* routing table maintenance */ 613 { nbpSkt, "nis" }, /* name info socket */ 614 { echoSkt, "echo" }, /* AppleTalk echo protocol */ 615 { zipSkt, "zip" }, /* zone info protocol */ 616 { 0, NULL } 617 }; 618 619 static const char * 620 ddpskt_string(register int skt) 621 { 622 static char buf[8]; 623 624 if (nflag) { 625 (void)snprintf(buf, sizeof(buf), "%d", skt); 626 return (buf); 627 } 628 return (tok2str(skt2str, "%d", skt)); 629 } 630