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