1 /* $OpenBSD: print-atalk.c,v 1.25 2007/10/07 16:41:05 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 "@(#) $Id: print-atalk.c,v 1.25 2007/10/07 16:41:05 deraadt Exp $ (LBL)"; 29 #endif 30 31 #include <sys/param.h> 32 #include <sys/time.h> 33 #include <sys/socket.h> 34 35 struct mbuf; 36 struct rtentry; 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 <inttypes.h> 50 #include <stdio.h> 51 #include <stdlib.h> 52 #include <string.h> 53 54 #include "interface.h" 55 #include "addrtoname.h" 56 #include "ethertype.h" 57 #include "extract.h" /* must come after interface.h */ 58 #include "appletalk.h" 59 #include "savestr.h" 60 #include "privsep.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 if ((intptr_t)bp & (sizeof(long)-1)) { 228 static u_char *abuf = NULL; 229 int clen = snapend - bp; 230 if (clen > snaplen) 231 clen = snaplen; 232 233 if (abuf == NULL) { 234 abuf = (u_char *)malloc(snaplen); 235 if (abuf == NULL) 236 error("ddp_print: malloc"); 237 } 238 memmove((char *)abuf, (char *)bp, min(length, clen)); 239 snapend = abuf + clen; 240 packetp = abuf; 241 bp = abuf; 242 } 243 244 switch (t) { 245 246 case ddpNBP: 247 nbp_print((const struct atNBP *)bp, length, snet, snode, skt); 248 break; 249 250 case ddpATP: 251 atp_print((const struct atATP *)bp, length); 252 break; 253 254 default: 255 (void)printf(" at-%s %d", tok2str(type2str, NULL, t), length); 256 break; 257 } 258 } 259 260 static void 261 atp_print(register const struct atATP *ap, u_int length) 262 { 263 char c; 264 u_int32_t data; 265 266 if ((const u_char *)(ap + 1) > snapend) { 267 /* Just bail if we don't have the whole chunk. */ 268 fputs(tstr, stdout); 269 return; 270 } 271 length -= sizeof(*ap); 272 switch (ap->control & 0xc0) { 273 274 case atpReqCode: 275 (void)printf(" atp-req%s %d", 276 ap->control & atpXO? " " : "*", 277 EXTRACT_16BITS(&ap->transID)); 278 279 atp_bitmap_print(ap->bitmap); 280 281 if (length != 0) 282 (void)printf(" [len=%d]", length); 283 284 switch (ap->control & (atpEOM|atpSTS)) { 285 case atpEOM: 286 (void)printf(" [EOM]"); 287 break; 288 case atpSTS: 289 (void)printf(" [STS]"); 290 break; 291 case atpEOM|atpSTS: 292 (void)printf(" [EOM,STS]"); 293 break; 294 } 295 break; 296 297 case atpRspCode: 298 (void)printf(" atp-resp%s%d:%d (%d)", 299 ap->control & atpEOM? "*" : " ", 300 EXTRACT_16BITS(&ap->transID), ap->bitmap, length); 301 switch (ap->control & (atpXO|atpSTS)) { 302 case atpXO: 303 (void)printf(" [XO]"); 304 break; 305 case atpSTS: 306 (void)printf(" [STS]"); 307 break; 308 case atpXO|atpSTS: 309 (void)printf(" [XO,STS]"); 310 break; 311 } 312 break; 313 314 case atpRelCode: 315 (void)printf(" atp-rel %d", EXTRACT_16BITS(&ap->transID)); 316 317 atp_bitmap_print(ap->bitmap); 318 319 /* length should be zero */ 320 if (length) 321 (void)printf(" [len=%d]", length); 322 323 /* there shouldn't be any control flags */ 324 if (ap->control & (atpXO|atpEOM|atpSTS)) { 325 c = '['; 326 if (ap->control & atpXO) { 327 (void)printf("%cXO", c); 328 c = ','; 329 } 330 if (ap->control & atpEOM) { 331 (void)printf("%cEOM", c); 332 c = ','; 333 } 334 if (ap->control & atpSTS) { 335 (void)printf("%cSTS", c); 336 c = ','; 337 } 338 (void)printf("]"); 339 } 340 break; 341 342 default: 343 (void)printf(" atp-0x%x %d (%d)", ap->control, 344 EXTRACT_16BITS(&ap->transID), length); 345 break; 346 } 347 data = EXTRACT_32BITS(&ap->userData); 348 if (data != 0) 349 (void)printf(" 0x%x", data); 350 } 351 352 static void 353 atp_bitmap_print(register u_char bm) 354 { 355 register char c; 356 register int i; 357 358 /* 359 * The '& 0xff' below is needed for compilers that want to sign 360 * extend a u_char, which is the case with the Ultrix compiler. 361 * (gcc is smart enough to eliminate it, at least on the Sparc). 362 */ 363 if ((bm + 1) & (bm & 0xff)) { 364 c = '<'; 365 for (i = 0; bm; ++i) { 366 if (bm & 1) { 367 (void)printf("%c%d", c, i); 368 c = ','; 369 } 370 bm >>= 1; 371 } 372 (void)printf(">"); 373 } else { 374 for (i = 0; bm; ++i) 375 bm >>= 1; 376 if (i > 1) 377 (void)printf("<0-%d>", i - 1); 378 else 379 (void)printf("<0>"); 380 } 381 } 382 383 static void 384 nbp_print(register const struct atNBP *np, u_int length, register u_short snet, 385 register u_char snode, register u_char skt) 386 { 387 register const struct atNBPtuple *tp = 388 (struct atNBPtuple *)((u_char *)np + nbpHeaderSize); 389 int i; 390 const u_char *ep; 391 392 if (length < nbpHeaderSize) { 393 (void)printf(" truncated-nbp %d", length); 394 return; 395 } 396 397 length -= nbpHeaderSize; 398 if (length < 8) { 399 /* must be room for at least one tuple */ 400 if (np->control == nbpNATLKerr) { 401 (void)printf(" nbp-netatalk_err"); 402 return; 403 } else if (np->control == nbpNATLKok) { 404 (void)printf(" nbp-netatalk_ok"); 405 return; 406 } 407 (void)printf(" truncated-nbp nbp-0x%x %d (%d)", 408 np->control, np->id, length + nbpHeaderSize); 409 return; 410 } 411 /* ep points to end of available data */ 412 ep = snapend; 413 if ((const u_char *)tp > ep) { 414 fputs(tstr, stdout); 415 return; 416 } 417 switch (i = np->control & 0xf0) { 418 419 case nbpBrRq: 420 case nbpLkUp: 421 (void)printf(i == nbpLkUp? " nbp-lkup %d:":" nbp-brRq %d:", 422 np->id); 423 if ((const u_char *)(tp + 1) > ep) { 424 fputs(tstr, stdout); 425 return; 426 } 427 (void)nbp_name_print(tp, ep); 428 /* 429 * look for anomalies: the spec says there can only 430 * be one tuple, the address must match the source 431 * address and the enumerator should be zero. 432 */ 433 if ((np->control & 0xf) != 1) 434 (void)printf(" [ntup=%d]", np->control & 0xf); 435 if (tp->enumerator) 436 (void)printf(" [enum=%d]", tp->enumerator); 437 if (EXTRACT_16BITS(&tp->net) != snet || 438 tp->node != snode || tp->skt != skt) 439 (void)printf(" [addr=%s.%d]", 440 ataddr_string(EXTRACT_16BITS(&tp->net), 441 tp->node), tp->skt); 442 break; 443 444 case nbpLkUpReply: 445 (void)printf(" nbp-reply %d:", np->id); 446 447 /* print each of the tuples in the reply */ 448 for (i = np->control & 0xf; --i >= 0 && tp; ) 449 tp = nbp_tuple_print(tp, ep, snet, snode, skt); 450 break; 451 452 case nbpNATLKrgstr: 453 case nbpNATLKunrgstr: 454 (void)printf((i == nbpNATLKrgstr) ? 455 " nbp-netatalk_rgstr %d:" : 456 " nbp-netatalk_unrgstr %d:", 457 np->id); 458 for (i = np->control & 0xf; --i >= 0 && tp; ) 459 tp = nbp_tuple_print(tp, ep, snet, snode, skt); 460 break; 461 462 default: 463 (void)printf(" nbp-0x%x %d (%d)", np->control, np->id, 464 length); 465 break; 466 } 467 } 468 469 /* print a counted string */ 470 static const char * 471 print_cstring(register const char *cp, register const u_char *ep) 472 { 473 register u_int length; 474 475 if (cp >= (const char *)ep) { 476 fputs(tstr, stdout); 477 return (0); 478 } 479 length = *cp++; 480 481 /* Spec says string can be at most 32 bytes long */ 482 if (length < 0 || length > 32) { 483 (void)printf("[len=%d]", length); 484 return (0); 485 } 486 while ((int)--length >= 0) { 487 if (cp >= (char *)ep) { 488 fputs(tstr, stdout); 489 return (0); 490 } 491 putchar(*cp++); 492 } 493 return (cp); 494 } 495 496 static const struct atNBPtuple * 497 nbp_tuple_print(register const struct atNBPtuple *tp, 498 register const u_char *ep, 499 register u_short snet, register u_char snode, 500 register u_char skt) 501 { 502 register const struct atNBPtuple *tpn; 503 504 if ((const u_char *)(tp + 1) > ep) { 505 fputs(tstr, stdout); 506 return 0; 507 } 508 tpn = nbp_name_print(tp, ep); 509 510 /* if the enumerator isn't 1, print it */ 511 if (tp->enumerator != 1) 512 (void)printf("(%d)", tp->enumerator); 513 514 /* if the socket doesn't match the src socket, print it */ 515 if (tp->skt != skt) 516 (void)printf(" %d", tp->skt); 517 518 /* if the address doesn't match the src address, it's an anomaly */ 519 if (EXTRACT_16BITS(&tp->net) != snet || tp->node != snode) 520 (void)printf(" [addr=%s]", 521 ataddr_string(EXTRACT_16BITS(&tp->net), tp->node)); 522 523 return (tpn); 524 } 525 526 static const struct atNBPtuple * 527 nbp_name_print(const struct atNBPtuple *tp, register const u_char *ep) 528 { 529 register const char *cp = (const char *)tp + nbpTupleSize; 530 531 putchar(' '); 532 533 /* Object */ 534 putchar('"'); 535 if ((cp = print_cstring(cp, ep)) != NULL) { 536 /* Type */ 537 putchar(':'); 538 if ((cp = print_cstring(cp, ep)) != NULL) { 539 /* Zone */ 540 putchar('@'); 541 if ((cp = print_cstring(cp, ep)) != NULL) 542 putchar('"'); 543 } 544 } 545 return ((const struct atNBPtuple *)cp); 546 } 547 548 549 #define HASHNAMESIZE 4096 550 551 struct hnamemem { 552 int addr; 553 char *name; 554 struct hnamemem *nxt; 555 }; 556 557 static struct hnamemem hnametable[HASHNAMESIZE]; 558 559 /* 560 * see if there's an AppleTalk number to name map file. 561 */ 562 static void 563 init_atalk(void) 564 { 565 struct hnamemem *tp; 566 char nambuf[MAXHOSTNAMELEN + 20]; 567 char line[BUFSIZ]; 568 int i1, i2, i3; 569 570 priv_getlines(FTAB_APPLETALK); 571 while (priv_getline(line, sizeof(line)) > 0) { 572 if (line[0] == '\n' || line[0] == 0 || line[0] == '#') 573 continue; 574 if (sscanf(line, "%d.%d.%d %255s", &i1, &i2, &i3, nambuf) == 4) 575 /* got a hostname. */ 576 i3 |= ((i1 << 8) | i2) << 8; 577 else if (sscanf(line, "%d.%d %255s", &i1, &i2, nambuf) == 3) 578 /* got a net name */ 579 i3 = (((i1 << 8) | i2) << 8) | 255; 580 else 581 continue; 582 583 for (tp = &hnametable[i3 & (HASHNAMESIZE-1)]; 584 tp->nxt; tp = tp->nxt) 585 ; 586 tp->addr = i3; 587 tp->nxt = newhnamemem(); 588 tp->name = savestr(nambuf); 589 } 590 } 591 592 static const char * 593 ataddr_string(u_short atnet, u_char athost) 594 { 595 register struct hnamemem *tp, *tp2; 596 register int i = (atnet << 8) | athost; 597 char nambuf[MAXHOSTNAMELEN + 20]; 598 static int first = 1; 599 600 if (first) { 601 first = 0; 602 init_atalk(); 603 } 604 for (tp = &hnametable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt) 605 if (tp->addr == i) 606 return (tp->name); 607 608 /* didn't have the node name -- see if we've got the net name */ 609 i |= 255; 610 for (tp2 = &hnametable[i & (HASHNAMESIZE-1)]; tp2->nxt; tp2 = tp2->nxt) 611 if (tp2->addr == i) { 612 tp->addr = (atnet << 8) | athost; 613 tp->nxt = newhnamemem(); 614 (void)snprintf(nambuf, sizeof nambuf, "%s.%d", 615 tp2->name, athost); 616 tp->name = savestr(nambuf); 617 return (tp->name); 618 } 619 620 tp->addr = (atnet << 8) | athost; 621 tp->nxt = newhnamemem(); 622 if (athost != 255) 623 (void)snprintf(nambuf, sizeof nambuf, "%d.%d.%d", 624 atnet >> 8, atnet & 0xff, athost); 625 else 626 (void)snprintf(nambuf, sizeof nambuf, "%d.%d", 627 atnet >> 8, atnet & 0xff); 628 tp->name = savestr(nambuf); 629 630 return (tp->name); 631 } 632 633 static struct tok skt2str[] = { 634 { rtmpSkt, "rtmp" }, /* routing table maintenance */ 635 { nbpSkt, "nis" }, /* name info socket */ 636 { echoSkt, "echo" }, /* AppleTalk echo protocol */ 637 { zipSkt, "zip" }, /* zone info protocol */ 638 { 0, NULL } 639 }; 640 641 static const char * 642 ddpskt_string(register int skt) 643 { 644 static char buf[12]; 645 646 if (nflag) { 647 (void)snprintf(buf, sizeof buf, "%d", skt); 648 return (buf); 649 } 650 return (tok2str(skt2str, "%d", skt)); 651 } 652