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