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