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