2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
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
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.
21 * Format and print AppleTalk packets.
27 static const char rcsid[] =
28 "@(#) $Header: /tcpdump/master/tcpdump/print-atalk.c,v 1.70.2.1 2002/02/05 10:04:18 guy Exp $ (LBL)";
35 #include <sys/param.h>
37 #include <sys/socket.h>
39 #include <netinet/in.h>
44 #include <netdb.h> /* for MAXHOSTNAMELEN on some platforms */
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"
53 static struct tok type2str[] = {
55 { ddpRTMPrequest, "rtmpReq" },
64 u_int16_t htype, ptype;
65 u_int8_t halen, palen;
73 static char tstr[] = "[|atalk]";
75 static void atp_print(const struct atATP *, u_int);
76 static void atp_bitmap_print(u_char);
77 static void nbp_print(const struct atNBP *, u_int, u_short, u_char, u_char);
78 static const char *print_cstring(const char *, const u_char *);
79 static const struct atNBPtuple *nbp_tuple_print(const struct atNBPtuple *,
81 u_short, u_char, u_char);
82 static const struct atNBPtuple *nbp_name_print(const struct atNBPtuple *,
84 static const char *ataddr_string(u_short, u_char);
85 static void ddp_print(const u_char *, u_int, int, u_short, u_char, u_char);
86 static const char *ddpskt_string(int);
89 * Print LLAP packets received on a physical LocalTalk interface.
92 ltalk_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
94 snapend = p + h->caplen;
97 llap_print(p, h->caplen);
99 default_print(p, h->caplen);
107 * Print AppleTalk LLAP packets.
110 llap_print(register const u_char *bp, u_int length)
112 register const struct LAP *lp;
113 register const struct atDDP *dp;
114 register const struct atShortDDP *sdp;
119 * Our packet is on a 4-byte boundary, as we're either called
120 * directly from a top-level link-layer printer (ltalk_if_print)
121 * or from the UDP printer. The LLAP+DDP header is a multiple
122 * of 4 bytes in length, so the DDP payload is also on a 4-byte
123 * boundary, and we don't need to align it before calling
126 lp = (const struct LAP *)bp;
128 length -= sizeof(*lp);
131 static struct LAP lp_ = {0, 0, lapDDP};
138 if (length < ddpSSize) {
139 (void)printf(" [|sddp %d]", length);
142 sdp = (const struct atShortDDP *)bp;
144 ataddr_string(0, lp->src), ddpskt_string(sdp->srcSkt));
146 ataddr_string(0, lp->dst), ddpskt_string(sdp->dstSkt));
149 ddp_print(bp, length, sdp->type, 0, lp->src, sdp->srcSkt);
153 if (length < ddpSize) {
154 (void)printf(" [|ddp %d]", length);
157 dp = (const struct atDDP *)bp;
158 snet = EXTRACT_16BITS(&dp->srcNet);
159 printf("%s.%s", ataddr_string(snet, dp->srcNode),
160 ddpskt_string(dp->srcSkt));
162 ataddr_string(EXTRACT_16BITS(&dp->dstNet), dp->dstNode),
163 ddpskt_string(dp->dstSkt));
166 ddp_print(bp, length, dp->type, snet, dp->srcNode, dp->srcSkt);
171 klap_print(bp, length);
176 printf("%d > %d at-lap#%d %d",
177 lp->src, lp->dst, lp->type, length);
183 * Print EtherTalk/TokenTalk packets (or FDDITalk, or whatever it's called
184 * when it runs over FDDI; yes, I've seen FDDI captures with AppleTalk
188 atalk_print(register const u_char *bp, u_int length)
190 register const struct atDDP *dp;
193 if (length < ddpSize) {
194 (void)printf(" [|ddp %d]", length);
197 dp = (const struct atDDP *)bp;
198 snet = EXTRACT_16BITS(&dp->srcNet);
199 printf("%s.%s", ataddr_string(snet, dp->srcNode),
200 ddpskt_string(dp->srcSkt));
202 ataddr_string(EXTRACT_16BITS(&dp->dstNet), dp->dstNode),
203 ddpskt_string(dp->dstSkt));
208 static u_char *abuf = NULL;
211 abuf = (u_char *)malloc(snaplen);
213 error("atalk_print: malloc");
215 memcpy((char *)abuf, (char *)bp, min(length, snaplen));
216 snapend += abuf - (u_char *)bp;
221 ddp_print(bp, length, dp->type, snet, dp->srcNode, dp->srcSkt);
224 /* XXX should probably pass in the snap header and do checks like arp_print() */
226 aarp_print(register const u_char *bp, u_int length)
228 register const struct aarp *ap;
230 #define AT(member) ataddr_string((ap->member[1]<<8)|ap->member[2],ap->member[3])
233 ap = (const struct aarp *)bp;
234 if (ntohs(ap->htype) == 1 && ntohs(ap->ptype) == ETHERTYPE_ATALK &&
235 ap->halen == 6 && ap->palen == 4 )
236 switch (ntohs(ap->op)) {
238 case 1: /* request */
239 (void)printf("who-has %s tell %s",
240 AT(pdaddr), AT(psaddr));
243 case 2: /* response */
244 (void)printf("reply %s is-at %s",
245 AT(psaddr), etheraddr_string(ap->hsaddr));
248 case 3: /* probe (oy!) */
249 (void)printf("probe %s tell %s",
250 AT(pdaddr), AT(psaddr));
253 (void)printf("len %u op %u htype %u ptype %#x halen %u palen %u",
254 length, ntohs(ap->op), ntohs(ap->htype), ntohs(ap->ptype),
255 ap->halen, ap->palen);
259 * Print AppleTalk Datagram Delivery Protocol packets.
262 ddp_print(register const u_char *bp, register u_int length, register int t,
263 register u_short snet, register u_char snode, u_char skt)
269 nbp_print((const struct atNBP *)bp, length, snet, snode, skt);
273 atp_print((const struct atATP *)bp, length);
277 (void)printf(" at-%s %d", tok2str(type2str, NULL, t), length);
283 atp_print(register const struct atATP *ap, u_int length)
288 if ((const u_char *)(ap + 1) > snapend) {
289 /* Just bail if we don't have the whole chunk. */
293 length -= sizeof(*ap);
294 switch (ap->control & 0xc0) {
297 (void)printf(" atp-req%s %d",
298 ap->control & atpXO? " " : "*",
299 EXTRACT_16BITS(&ap->transID));
301 atp_bitmap_print(ap->bitmap);
304 (void)printf(" [len=%d]", length);
306 switch (ap->control & (atpEOM|atpSTS)) {
308 (void)printf(" [EOM]");
311 (void)printf(" [STS]");
314 (void)printf(" [EOM,STS]");
320 (void)printf(" atp-resp%s%d:%d (%d)",
321 ap->control & atpEOM? "*" : " ",
322 EXTRACT_16BITS(&ap->transID), ap->bitmap, length);
323 switch (ap->control & (atpXO|atpSTS)) {
325 (void)printf(" [XO]");
328 (void)printf(" [STS]");
331 (void)printf(" [XO,STS]");
337 (void)printf(" atp-rel %d", EXTRACT_16BITS(&ap->transID));
339 atp_bitmap_print(ap->bitmap);
341 /* length should be zero */
343 (void)printf(" [len=%d]", length);
345 /* there shouldn't be any control flags */
346 if (ap->control & (atpXO|atpEOM|atpSTS)) {
348 if (ap->control & atpXO) {
349 (void)printf("%cXO", c);
352 if (ap->control & atpEOM) {
353 (void)printf("%cEOM", c);
356 if (ap->control & atpSTS) {
357 (void)printf("%cSTS", c);
365 (void)printf(" atp-0x%x %d (%d)", ap->control,
366 EXTRACT_16BITS(&ap->transID), length);
369 data = EXTRACT_32BITS(&ap->userData);
371 (void)printf(" 0x%x", data);
375 atp_bitmap_print(register u_char bm)
381 * The '& 0xff' below is needed for compilers that want to sign
382 * extend a u_char, which is the case with the Ultrix compiler.
383 * (gcc is smart enough to eliminate it, at least on the Sparc).
385 if ((bm + 1) & (bm & 0xff)) {
387 for (i = 0; bm; ++i) {
389 (void)printf("%c%d", c, i);
399 (void)printf("<0-%d>", i - 1);
406 nbp_print(register const struct atNBP *np, u_int length, register u_short snet,
407 register u_char snode, register u_char skt)
409 register const struct atNBPtuple *tp =
410 (const struct atNBPtuple *)((u_char *)np + nbpHeaderSize);
414 if (length < nbpHeaderSize) {
415 (void)printf(" truncated-nbp %d", length);
419 length -= nbpHeaderSize;
421 /* must be room for at least one tuple */
422 (void)printf(" truncated-nbp %d", length + nbpHeaderSize);
425 /* ep points to end of available data */
427 if ((const u_char *)tp > ep) {
431 switch (i = np->control & 0xf0) {
435 (void)printf(i == nbpLkUp? " nbp-lkup %d:":" nbp-brRq %d:",
437 if ((const u_char *)(tp + 1) > ep) {
441 (void)nbp_name_print(tp, ep);
443 * look for anomalies: the spec says there can only
444 * be one tuple, the address must match the source
445 * address and the enumerator should be zero.
447 if ((np->control & 0xf) != 1)
448 (void)printf(" [ntup=%d]", np->control & 0xf);
450 (void)printf(" [enum=%d]", tp->enumerator);
451 if (EXTRACT_16BITS(&tp->net) != snet ||
452 tp->node != snode || tp->skt != skt)
453 (void)printf(" [addr=%s.%d]",
454 ataddr_string(EXTRACT_16BITS(&tp->net),
459 (void)printf(" nbp-reply %d:", np->id);
461 /* print each of the tuples in the reply */
462 for (i = np->control & 0xf; --i >= 0 && tp; )
463 tp = nbp_tuple_print(tp, ep, snet, snode, skt);
467 (void)printf(" nbp-0x%x %d (%d)", np->control, np->id,
473 /* print a counted string */
475 print_cstring(register const char *cp, register const u_char *ep)
477 register u_int length;
479 if (cp >= (const char *)ep) {
485 /* Spec says string can be at most 32 bytes long */
487 (void)printf("[len=%u]", length);
490 while ((int)--length >= 0) {
491 if (cp >= (const char *)ep) {
500 static const struct atNBPtuple *
501 nbp_tuple_print(register const struct atNBPtuple *tp,
502 register const u_char *ep,
503 register u_short snet, register u_char snode,
506 register const struct atNBPtuple *tpn;
508 if ((const u_char *)(tp + 1) > ep) {
512 tpn = nbp_name_print(tp, ep);
514 /* if the enumerator isn't 1, print it */
515 if (tp->enumerator != 1)
516 (void)printf("(%d)", tp->enumerator);
518 /* if the socket doesn't match the src socket, print it */
520 (void)printf(" %d", tp->skt);
522 /* if the address doesn't match the src address, it's an anomaly */
523 if (EXTRACT_16BITS(&tp->net) != snet || tp->node != snode)
524 (void)printf(" [addr=%s]",
525 ataddr_string(EXTRACT_16BITS(&tp->net), tp->node));
530 static const struct atNBPtuple *
531 nbp_name_print(const struct atNBPtuple *tp, register const u_char *ep)
533 register const char *cp = (const char *)tp + nbpTupleSize;
539 if ((cp = print_cstring(cp, ep)) != NULL) {
542 if ((cp = print_cstring(cp, ep)) != NULL) {
545 if ((cp = print_cstring(cp, ep)) != NULL)
549 return ((const struct atNBPtuple *)cp);
553 #define HASHNAMESIZE 4096
558 struct hnamemem *nxt;
561 static struct hnamemem hnametable[HASHNAMESIZE];
564 ataddr_string(u_short atnet, u_char athost)
566 register struct hnamemem *tp, *tp2;
567 register int i = (atnet << 8) | athost;
568 char nambuf[MAXHOSTNAMELEN + 20];
569 static int first = 1;
573 * if this is the first call, see if there's an AppleTalk
574 * number to name map file.
576 if (first && (first = 0, !nflag)
577 && (fp = fopen("/etc/atalk.names", "r"))) {
581 while (fgets(line, sizeof(line), fp)) {
582 if (line[0] == '\n' || line[0] == 0 || line[0] == '#')
584 if (sscanf(line, "%d.%d.%d %256s", &i1, &i2, &i3,
586 /* got a hostname. */
587 i3 |= ((i1 << 8) | i2) << 8;
588 else if (sscanf(line, "%d.%d %256s", &i1, &i2,
591 i3 = (((i1 << 8) | i2) << 8) | 255;
595 for (tp = &hnametable[i2 & (HASHNAMESIZE-1)];
596 tp->nxt; tp = tp->nxt)
599 tp->nxt = newhnamemem();
600 tp->name = strdup(nambuf);
605 for (tp = &hnametable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
609 /* didn't have the node name -- see if we've got the net name */
611 for (tp2 = &hnametable[i & (HASHNAMESIZE-1)]; tp2->nxt; tp2 = tp2->nxt)
612 if (tp2->addr == i) {
613 tp->addr = (atnet << 8) | athost;
614 tp->nxt = newhnamemem();
615 (void)snprintf(nambuf, sizeof(nambuf), "%s.%d",
617 tp->name = strdup(nambuf);
621 tp->addr = (atnet << 8) | athost;
622 tp->nxt = newhnamemem();
624 (void)snprintf(nambuf, sizeof(nambuf), "%d.%d.%d",
625 atnet >> 8, atnet & 0xff, athost);
627 (void)snprintf(nambuf, sizeof(nambuf), "%d.%d", atnet >> 8,
629 tp->name = strdup(nambuf);
634 static struct tok skt2str[] = {
635 { rtmpSkt, "rtmp" }, /* routing table maintenance */
636 { nbpSkt, "nis" }, /* name info socket */
637 { echoSkt, "echo" }, /* AppleTalk echo protocol */
638 { zipSkt, "zip" }, /* zone info protocol */
643 ddpskt_string(register int skt)
648 (void)snprintf(buf, sizeof(buf), "%d", skt);
651 return (tok2str(skt2str, "%d", skt));