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