1 /* $OpenBSD: print-atalk.c,v 1.18 2001/11/06 03:11:40 deraadt Exp $ */ 2 3 /* 4 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that: (1) source code distributions 9 * retain the above copyright notice and this paragraph in its entirety, (2) 10 * distributions including binary code include the above copyright notice and 11 * this paragraph in its entirety in the documentation or other materials 12 * provided with the distribution, and (3) all advertising materials mentioning 13 * features or use of this software display the following acknowledgement: 14 * ``This product includes software developed by the University of California, 15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 16 * the University nor the names of its contributors may be used to endorse 17 * or promote products derived from this software without specific prior 18 * written permission. 19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 22 * 23 * Format and print AppleTalk packets. 24 */ 25 26 #ifndef lint 27 static const char rcsid[] = 28 "@(#) $Header: /home/cvs/src/usr.sbin/tcpdump/print-atalk.c,v 1.18 2001/11/06 03:11:40 deraadt Exp $ (LBL)"; 29 #endif 30 31 #include <sys/param.h> 32 #include <sys/time.h> 33 #include <sys/socket.h> 34 35 #ifdef __STDC__ 36 struct mbuf; 37 struct rtentry; 38 #endif 39 #include <net/if.h> 40 41 #include <netinet/in.h> 42 #include <netinet/in_systm.h> 43 #include <netinet/ip.h> 44 #include <netinet/ip_var.h> 45 #include <netinet/if_ether.h> 46 #include <netinet/udp.h> 47 #include <netinet/udp_var.h> 48 #include <netinet/tcp.h> 49 #include <netinet/tcpip.h> 50 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <string.h> 54 55 #include "interface.h" 56 #include "addrtoname.h" 57 #include "ethertype.h" 58 #include "extract.h" /* must come after interface.h */ 59 #include "appletalk.h" 60 #include "savestr.h" 61 62 static struct tok type2str[] = { 63 { ddpRTMP, "rtmp" }, 64 { ddpRTMPrequest, "rtmpReq" }, 65 { ddpECHO, "echo" }, 66 { ddpIP, "IP" }, 67 { ddpARP, "ARP" }, 68 { ddpKLAP, "KLAP" }, 69 { 0, NULL } 70 }; 71 72 struct aarp { 73 u_int16_t htype, ptype; 74 u_int8_t halen, palen; 75 u_int16_t op; 76 u_int8_t hsaddr[6]; 77 u_int8_t psaddr[4]; 78 u_int8_t hdaddr[6]; 79 u_int8_t pdaddr[4]; 80 }; 81 82 static char tstr[] = "[|atalk]"; 83 84 static void atp_print(const struct atATP *, u_int); 85 static void atp_bitmap_print(u_char); 86 static void nbp_print(const struct atNBP *, u_int, u_short, u_char, u_char); 87 static const char *print_cstring(const char *, const u_char *); 88 static const struct atNBPtuple *nbp_tuple_print(const struct atNBPtuple *, 89 const u_char *, 90 u_short, u_char, u_char); 91 static const struct atNBPtuple *nbp_name_print(const struct atNBPtuple *, 92 const u_char *); 93 static const char *ataddr_string(u_short, u_char); 94 static void ddp_print(const u_char *, u_int, int, u_short, u_char, u_char); 95 static const char *ddpskt_string(int); 96 97 /* 98 * Print AppleTalk Datagram Delivery Protocol packets 99 * without the LLAP encapsulating header (i.e. 100 * from Ethertalk) 101 */ 102 void 103 atalk_print(register const u_char *bp, u_int length) 104 { 105 register const struct atDDP *dp; 106 u_short snet; 107 108 if (length < ddpSize) { 109 (void)printf(" [|ddp %d]", length); 110 return; 111 } 112 dp = (const struct atDDP *)bp; 113 snet = EXTRACT_16BITS(&dp->srcNet); 114 printf("%s.%s", ataddr_string(snet, dp->srcNode), 115 ddpskt_string(dp->srcSkt)); 116 printf(" > %s.%s:", 117 ataddr_string(EXTRACT_16BITS(&dp->dstNet), dp->dstNode), 118 ddpskt_string(dp->dstSkt)); 119 bp += ddpSize; 120 length -= ddpSize; 121 ddp_print(bp, length, dp->type, snet, dp->srcNode, dp->srcSkt); 122 } 123 124 /* 125 * Print AppleTalk Datagram Delivery Protocol packets 126 * from localtalk (i.e. the 230 Kbps net built into 127 * every Macintosh). We can get these from a localtalk 128 * interface if we have one, or from UDP encapsulated tunnels. 129 */ 130 void 131 atalk_print_llap(register const u_char *bp, u_int length) 132 { 133 register const struct LAP *lp; 134 register const struct atDDP *dp; 135 register const struct atShortDDP *sdp; 136 u_short snet; 137 138 lp = (struct LAP *)bp; 139 bp += sizeof(*lp); 140 length -= sizeof(*lp); 141 switch (lp->type) { 142 143 case lapShortDDP: 144 if (length < ddpSSize) { 145 (void)printf(" [|sddp %d]", length); 146 return; 147 } 148 sdp = (const struct atShortDDP *)bp; 149 printf("%s.%s", 150 ataddr_string(0, lp->src), ddpskt_string(sdp->srcSkt)); 151 printf(" > %s.%s:", 152 ataddr_string(0, lp->dst), ddpskt_string(sdp->dstSkt)); 153 bp += ddpSSize; 154 length -= ddpSSize; 155 ddp_print(bp, length, sdp->type, 0, lp->src, sdp->srcSkt); 156 break; 157 158 case lapDDP: 159 if (length < ddpSize) { 160 (void)printf(" [|ddp %d]", length); 161 return; 162 } 163 dp = (const struct atDDP *)bp; 164 snet = EXTRACT_16BITS(&dp->srcNet); 165 printf("%s.%s", ataddr_string(snet, dp->srcNode), 166 ddpskt_string(dp->srcSkt)); 167 printf(" > %s.%s:", 168 ataddr_string(EXTRACT_16BITS(&dp->dstNet), dp->dstNode), 169 ddpskt_string(dp->dstSkt)); 170 bp += ddpSize; 171 length -= ddpSize; 172 ddp_print(bp, length, dp->type, snet, dp->srcNode, dp->srcSkt); 173 break; 174 175 #ifdef notdef 176 case lapKLAP: 177 klap_print(bp, length); 178 break; 179 #endif 180 181 default: 182 printf("%d > %d at-lap#%d %d", 183 lp->src, lp->dst, lp->type, length); 184 break; 185 } 186 } 187 188 /* XXX should probably pass in the snap header and do checks like arp_print() */ 189 void 190 aarp_print(register const u_char *bp, u_int length) 191 { 192 register const struct aarp *ap; 193 194 #define AT(member) ataddr_string((ap->member[1]<<8)|ap->member[2],ap->member[3]) 195 196 printf("aarp "); 197 ap = (const struct aarp *)bp; 198 if (ntohs(ap->htype) == 1 && ntohs(ap->ptype) == ETHERTYPE_ATALK && 199 ap->halen == 6 && ap->palen == 4 ) 200 switch (ntohs(ap->op)) { 201 202 case 1: /* request */ 203 (void)printf("who-has %s tell %s", 204 AT(pdaddr), AT(psaddr)); 205 return; 206 207 case 2: /* response */ 208 (void)printf("reply %s is-at %s", 209 AT(pdaddr), etheraddr_string(ap->hdaddr)); 210 return; 211 212 case 3: /* probe (oy!) */ 213 (void)printf("probe %s tell %s", 214 AT(pdaddr), AT(psaddr)); 215 return; 216 } 217 (void)printf("len %u op %u htype %u ptype %#x halen %u palen %u", 218 length, ntohs(ap->op), ntohs(ap->htype), ntohs(ap->ptype), 219 ap->halen, ap->palen); 220 } 221 222 static void 223 ddp_print(register const u_char *bp, register u_int length, register int t, 224 register u_short snet, register u_char snode, u_char skt) 225 { 226 227 #ifdef LBL_ALIGN 228 if ((long)bp & 3) { 229 static u_char *abuf = NULL; 230 231 if (abuf == NULL) { 232 abuf = (u_char *)malloc(snaplen); 233 if (abuf == NULL) 234 error("ddp_print: malloc"); 235 } 236 memcpy(abuf, bp, min(length, snaplen)); 237 snapend += abuf - bp; 238 packetp = abuf; 239 bp = abuf; 240 } 241 #endif 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 (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 if (np->control == nbpNATLKerr) { 400 (void)printf(" nbp-netatalk_err"); 401 return; 402 } else if (np->control == nbpNATLKok) { 403 (void)printf(" nbp-netatalk_ok"); 404 return; 405 } 406 (void)printf(" truncated-nbp nbp-0x%x %d (%d)", 407 np->control, np->id, length + nbpHeaderSize); 408 return; 409 } 410 /* ep points to end of available data */ 411 ep = snapend; 412 if ((const u_char *)tp > ep) { 413 fputs(tstr, stdout); 414 return; 415 } 416 switch (i = np->control & 0xf0) { 417 418 case nbpBrRq: 419 case nbpLkUp: 420 (void)printf(i == nbpLkUp? " nbp-lkup %d:":" nbp-brRq %d:", 421 np->id); 422 if ((const u_char *)(tp + 1) > ep) { 423 fputs(tstr, stdout); 424 return; 425 } 426 (void)nbp_name_print(tp, ep); 427 /* 428 * look for anomalies: the spec says there can only 429 * be one tuple, the address must match the source 430 * address and the enumerator should be zero. 431 */ 432 if ((np->control & 0xf) != 1) 433 (void)printf(" [ntup=%d]", np->control & 0xf); 434 if (tp->enumerator) 435 (void)printf(" [enum=%d]", tp->enumerator); 436 if (EXTRACT_16BITS(&tp->net) != snet || 437 tp->node != snode || tp->skt != skt) 438 (void)printf(" [addr=%s.%d]", 439 ataddr_string(EXTRACT_16BITS(&tp->net), 440 tp->node), tp->skt); 441 break; 442 443 case nbpLkUpReply: 444 (void)printf(" nbp-reply %d:", np->id); 445 446 /* print each of the tuples in the reply */ 447 for (i = np->control & 0xf; --i >= 0 && tp; ) 448 tp = nbp_tuple_print(tp, ep, snet, snode, skt); 449 break; 450 451 case nbpNATLKrgstr: 452 case nbpNATLKunrgstr: 453 (void)printf((i == nbpNATLKrgstr) ? 454 " nbp-netatalk_rgstr %d:" : 455 " nbp-netatalk_unrgstr %d:", 456 np->id); 457 for (i = np->control & 0xf; --i >= 0 && tp; ) 458 tp = nbp_tuple_print(tp, ep, snet, snode, skt); 459 break; 460 461 default: 462 (void)printf(" nbp-0x%x %d (%d)", np->control, np->id, 463 length); 464 break; 465 } 466 } 467 468 /* print a counted string */ 469 static const char * 470 print_cstring(register const char *cp, register const u_char *ep) 471 { 472 register u_int length; 473 474 if (cp >= (const char *)ep) { 475 fputs(tstr, stdout); 476 return (0); 477 } 478 length = *cp++; 479 480 /* Spec says string can be at most 32 bytes long */ 481 if (length < 0 || length > 32) { 482 (void)printf("[len=%d]", length); 483 return (0); 484 } 485 while ((int)--length >= 0) { 486 if (cp >= (char *)ep) { 487 fputs(tstr, stdout); 488 return (0); 489 } 490 putchar(*cp++); 491 } 492 return (cp); 493 } 494 495 static const struct atNBPtuple * 496 nbp_tuple_print(register const struct atNBPtuple *tp, 497 register const u_char *ep, 498 register u_short snet, register u_char snode, 499 register u_char skt) 500 { 501 register const struct atNBPtuple *tpn; 502 503 if ((const u_char *)(tp + 1) > ep) { 504 fputs(tstr, stdout); 505 return 0; 506 } 507 tpn = nbp_name_print(tp, ep); 508 509 /* if the enumerator isn't 1, print it */ 510 if (tp->enumerator != 1) 511 (void)printf("(%d)", tp->enumerator); 512 513 /* if the socket doesn't match the src socket, print it */ 514 if (tp->skt != skt) 515 (void)printf(" %d", tp->skt); 516 517 /* if the address doesn't match the src address, it's an anomaly */ 518 if (EXTRACT_16BITS(&tp->net) != snet || tp->node != snode) 519 (void)printf(" [addr=%s]", 520 ataddr_string(EXTRACT_16BITS(&tp->net), tp->node)); 521 522 return (tpn); 523 } 524 525 static const struct atNBPtuple * 526 nbp_name_print(const struct atNBPtuple *tp, register const u_char *ep) 527 { 528 register const char *cp = (const char *)tp + nbpTupleSize; 529 530 putchar(' '); 531 532 /* Object */ 533 putchar('"'); 534 if ((cp = print_cstring(cp, ep)) != NULL) { 535 /* Type */ 536 putchar(':'); 537 if ((cp = print_cstring(cp, ep)) != NULL) { 538 /* Zone */ 539 putchar('@'); 540 if ((cp = print_cstring(cp, ep)) != NULL) 541 putchar('"'); 542 } 543 } 544 return ((const struct atNBPtuple *)cp); 545 } 546 547 548 #define HASHNAMESIZE 4096 549 550 struct hnamemem { 551 int addr; 552 char *name; 553 struct hnamemem *nxt; 554 }; 555 556 static struct hnamemem hnametable[HASHNAMESIZE]; 557 558 static const char * 559 ataddr_string(u_short atnet, u_char athost) 560 { 561 register struct hnamemem *tp, *tp2; 562 register int i = (atnet << 8) | athost; 563 char nambuf[MAXHOSTNAMELEN + 20]; 564 static int first = 1; 565 FILE *fp; 566 567 /* 568 * if this is the first call, see if there's an AppleTalk 569 * number to name map file. 570 */ 571 if (first && (first = 0, !nflag) 572 && (fp = fopen("/etc/atalk.names", "r"))) { 573 char line[256]; 574 int i1, i2, i3; 575 576 while (fgets(line, sizeof(line), fp)) { 577 if (line[0] == '\n' || line[0] == 0 || line[0] == '#') 578 continue; 579 if (sscanf(line, "%d.%d.%d %255s", &i1, &i2, &i3, 580 nambuf) == 4) 581 /* got a hostname. */ 582 i3 |= ((i1 << 8) | i2) << 8; 583 else if (sscanf(line, "%d.%d %255s", &i1, &i2, 584 nambuf) == 3) 585 /* got a net name */ 586 i3 = (((i1 << 8) | i2) << 8) | 255; 587 else 588 continue; 589 590 for (tp = &hnametable[i3 & (HASHNAMESIZE-1)]; 591 tp->nxt; tp = tp->nxt) 592 ; 593 tp->addr = i3; 594 tp->nxt = newhnamemem(); 595 tp->name = savestr(nambuf); 596 } 597 fclose(fp); 598 } 599 600 for (tp = &hnametable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt) 601 if (tp->addr == i) 602 return (tp->name); 603 604 /* didn't have the node name -- see if we've got the net name */ 605 i |= 255; 606 for (tp2 = &hnametable[i & (HASHNAMESIZE-1)]; tp2->nxt; tp2 = tp2->nxt) 607 if (tp2->addr == i) { 608 tp->addr = (atnet << 8) | athost; 609 tp->nxt = newhnamemem(); 610 (void)snprintf(nambuf, sizeof nambuf, "%s.%d", 611 tp2->name, athost); 612 tp->name = savestr(nambuf); 613 return (tp->name); 614 } 615 616 tp->addr = (atnet << 8) | athost; 617 tp->nxt = newhnamemem(); 618 if (athost != 255) 619 (void)snprintf(nambuf, sizeof nambuf, "%d.%d.%d", 620 atnet >> 8, atnet & 0xff, athost); 621 else 622 (void)snprintf(nambuf, sizeof nambuf, "%d.%d", 623 atnet >> 8, atnet & 0xff); 624 tp->name = savestr(nambuf); 625 626 return (tp->name); 627 } 628 629 static struct tok skt2str[] = { 630 { rtmpSkt, "rtmp" }, /* routing table maintenance */ 631 { nbpSkt, "nis" }, /* name info socket */ 632 { echoSkt, "echo" }, /* AppleTalk echo protocol */ 633 { zipSkt, "zip" }, /* zone info protocol */ 634 { 0, NULL } 635 }; 636 637 static const char * 638 ddpskt_string(register int skt) 639 { 640 static char buf[10]; 641 642 if (nflag) { 643 (void)snprintf(buf, sizeof buf, "%d", skt); 644 return (buf); 645 } 646 return (tok2str(skt2str, "%d", skt)); 647 } 648