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.
25 static const char rcsid[] _U_ =
26 "@(#) $Header: /tcpdump/master/tcpdump/print-atalk.c,v 1.81 2004/05/01 09:41:50 hannes Exp $ (LBL)";
33 #include <tcpdump-stdinc.h>
40 #include "interface.h"
41 #include "addrtoname.h"
42 #include "ethertype.h"
43 #include "extract.h" /* must come after interface.h */
44 #include "appletalk.h"
46 static struct tok type2str[] = {
48 { ddpRTMPrequest, "rtmpReq" },
57 u_int16_t htype, ptype;
58 u_int8_t halen, palen;
66 static char tstr[] = "[|atalk]";
68 static void atp_print(const struct atATP *, u_int);
69 static void atp_bitmap_print(u_char);
70 static void nbp_print(const struct atNBP *, u_int, u_short, u_char, u_char);
71 static const char *print_cstring(const char *, const u_char *);
72 static const struct atNBPtuple *nbp_tuple_print(const struct atNBPtuple *,
74 u_short, u_char, u_char);
75 static const struct atNBPtuple *nbp_name_print(const struct atNBPtuple *,
77 static const char *ataddr_string(u_short, u_char);
78 static void ddp_print(const u_char *, u_int, int, u_short, u_char, u_char);
79 static const char *ddpskt_string(int);
82 * Print LLAP packets received on a physical LocalTalk interface.
85 ltalk_if_print(const struct pcap_pkthdr *h, const u_char *p)
87 return (llap_print(p, h->caplen));
91 * Print AppleTalk LLAP packets.
94 llap_print(register const u_char *bp, u_int length)
96 register const struct LAP *lp;
97 register const struct atDDP *dp;
98 register const struct atShortDDP *sdp;
103 * Our packet is on a 4-byte boundary, as we're either called
104 * directly from a top-level link-layer printer (ltalk_if_print)
105 * or from the UDP printer. The LLAP+DDP header is a multiple
106 * of 4 bytes in length, so the DDP payload is also on a 4-byte
107 * boundary, and we don't need to align it before calling
110 lp = (const struct LAP *)bp;
112 length -= sizeof(*lp);
113 hdrlen = sizeof(*lp);
117 if (length < ddpSSize) {
118 (void)printf(" [|sddp %d]", length);
121 sdp = (const struct atShortDDP *)bp;
123 ataddr_string(0, lp->src), ddpskt_string(sdp->srcSkt));
125 ataddr_string(0, lp->dst), ddpskt_string(sdp->dstSkt));
129 ddp_print(bp, length, sdp->type, 0, lp->src, sdp->srcSkt);
133 if (length < ddpSize) {
134 (void)printf(" [|ddp %d]", length);
137 dp = (const struct atDDP *)bp;
138 snet = EXTRACT_16BITS(&dp->srcNet);
139 printf("%s.%s", ataddr_string(snet, dp->srcNode),
140 ddpskt_string(dp->srcSkt));
142 ataddr_string(EXTRACT_16BITS(&dp->dstNet), dp->dstNode),
143 ddpskt_string(dp->dstSkt));
147 ddp_print(bp, length, dp->type, snet, dp->srcNode, dp->srcSkt);
152 klap_print(bp, length);
157 printf("%d > %d at-lap#%d %d",
158 lp->src, lp->dst, lp->type, length);
165 * Print EtherTalk/TokenTalk packets (or FDDITalk, or whatever it's called
166 * when it runs over FDDI; yes, I've seen FDDI captures with AppleTalk
170 atalk_print(register const u_char *bp, u_int length)
172 register const struct atDDP *dp;
178 if (length < ddpSize) {
179 (void)printf(" [|ddp %d]", length);
182 dp = (const struct atDDP *)bp;
183 snet = EXTRACT_16BITS(&dp->srcNet);
184 printf("%s.%s", ataddr_string(snet, dp->srcNode),
185 ddpskt_string(dp->srcSkt));
187 ataddr_string(EXTRACT_16BITS(&dp->dstNet), dp->dstNode),
188 ddpskt_string(dp->dstSkt));
191 ddp_print(bp, length, dp->type, snet, dp->srcNode, dp->srcSkt);
194 /* XXX should probably pass in the snap header and do checks like arp_print() */
196 aarp_print(register const u_char *bp, u_int length)
198 register const struct aarp *ap;
200 #define AT(member) ataddr_string((ap->member[1]<<8)|ap->member[2],ap->member[3])
203 ap = (const struct aarp *)bp;
204 if (EXTRACT_16BITS(&ap->htype) == 1 &&
205 EXTRACT_16BITS(&ap->ptype) == ETHERTYPE_ATALK &&
206 ap->halen == 6 && ap->palen == 4 )
207 switch (EXTRACT_16BITS(&ap->op)) {
209 case 1: /* request */
210 (void)printf("who-has %s tell %s",
211 AT(pdaddr), AT(psaddr));
214 case 2: /* response */
215 (void)printf("reply %s is-at %s",
216 AT(pdaddr), etheraddr_string(ap->hdaddr));
219 case 3: /* probe (oy!) */
220 (void)printf("probe %s tell %s",
221 AT(pdaddr), AT(psaddr));
224 (void)printf("len %u op %u htype %u ptype %#x halen %u palen %u",
225 length, EXTRACT_16BITS(&ap->op), EXTRACT_16BITS(&ap->htype),
226 EXTRACT_16BITS(&ap->ptype), ap->halen, ap->palen);
230 * Print AppleTalk Datagram Delivery Protocol packets.
233 ddp_print(register const u_char *bp, register u_int length, register int t,
234 register u_short snet, register u_char snode, u_char skt)
240 nbp_print((const struct atNBP *)bp, length, snet, snode, skt);
244 atp_print((const struct atATP *)bp, length);
248 eigrp_print(bp, length);
252 (void)printf(" at-%s %d", tok2str(type2str, NULL, t), length);
258 atp_print(register const struct atATP *ap, u_int length)
263 if ((const u_char *)(ap + 1) > snapend) {
264 /* Just bail if we don't have the whole chunk. */
268 length -= sizeof(*ap);
269 switch (ap->control & 0xc0) {
272 (void)printf(" atp-req%s %d",
273 ap->control & atpXO? " " : "*",
274 EXTRACT_16BITS(&ap->transID));
276 atp_bitmap_print(ap->bitmap);
279 (void)printf(" [len=%d]", length);
281 switch (ap->control & (atpEOM|atpSTS)) {
283 (void)printf(" [EOM]");
286 (void)printf(" [STS]");
289 (void)printf(" [EOM,STS]");
295 (void)printf(" atp-resp%s%d:%d (%d)",
296 ap->control & atpEOM? "*" : " ",
297 EXTRACT_16BITS(&ap->transID), ap->bitmap, length);
298 switch (ap->control & (atpXO|atpSTS)) {
300 (void)printf(" [XO]");
303 (void)printf(" [STS]");
306 (void)printf(" [XO,STS]");
312 (void)printf(" atp-rel %d", EXTRACT_16BITS(&ap->transID));
314 atp_bitmap_print(ap->bitmap);
316 /* length should be zero */
318 (void)printf(" [len=%d]", length);
320 /* there shouldn't be any control flags */
321 if (ap->control & (atpXO|atpEOM|atpSTS)) {
323 if (ap->control & atpXO) {
324 (void)printf("%cXO", c);
327 if (ap->control & atpEOM) {
328 (void)printf("%cEOM", c);
331 if (ap->control & atpSTS) {
332 (void)printf("%cSTS", c);
340 (void)printf(" atp-0x%x %d (%d)", ap->control,
341 EXTRACT_16BITS(&ap->transID), length);
344 data = EXTRACT_32BITS(&ap->userData);
346 (void)printf(" 0x%x", data);
350 atp_bitmap_print(register u_char bm)
356 * The '& 0xff' below is needed for compilers that want to sign
357 * extend a u_char, which is the case with the Ultrix compiler.
358 * (gcc is smart enough to eliminate it, at least on the Sparc).
360 if ((bm + 1) & (bm & 0xff)) {
362 for (i = 0; bm; ++i) {
364 (void)printf("%c%d", c, i);
374 (void)printf("<0-%d>", i - 1);
381 nbp_print(register const struct atNBP *np, u_int length, register u_short snet,
382 register u_char snode, register u_char skt)
384 register const struct atNBPtuple *tp =
385 (const struct atNBPtuple *)((u_char *)np + nbpHeaderSize);
389 if (length < nbpHeaderSize) {
390 (void)printf(" truncated-nbp %d", length);
394 length -= nbpHeaderSize;
396 /* must be room for at least one tuple */
397 (void)printf(" truncated-nbp %d", length + nbpHeaderSize);
400 /* ep points to end of available data */
402 if ((const u_char *)tp > ep) {
406 switch (i = np->control & 0xf0) {
410 (void)printf(i == nbpLkUp? " nbp-lkup %d:":" nbp-brRq %d:",
412 if ((const u_char *)(tp + 1) > ep) {
416 (void)nbp_name_print(tp, ep);
418 * look for anomalies: the spec says there can only
419 * be one tuple, the address must match the source
420 * address and the enumerator should be zero.
422 if ((np->control & 0xf) != 1)
423 (void)printf(" [ntup=%d]", np->control & 0xf);
425 (void)printf(" [enum=%d]", tp->enumerator);
426 if (EXTRACT_16BITS(&tp->net) != snet ||
427 tp->node != snode || tp->skt != skt)
428 (void)printf(" [addr=%s.%d]",
429 ataddr_string(EXTRACT_16BITS(&tp->net),
434 (void)printf(" nbp-reply %d:", np->id);
436 /* print each of the tuples in the reply */
437 for (i = np->control & 0xf; --i >= 0 && tp; )
438 tp = nbp_tuple_print(tp, ep, snet, snode, skt);
442 (void)printf(" nbp-0x%x %d (%d)", np->control, np->id,
448 /* print a counted string */
450 print_cstring(register const char *cp, register const u_char *ep)
452 register u_int length;
454 if (cp >= (const char *)ep) {
460 /* Spec says string can be at most 32 bytes long */
462 (void)printf("[len=%u]", length);
465 while ((int)--length >= 0) {
466 if (cp >= (const char *)ep) {
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,
481 register const struct atNBPtuple *tpn;
483 if ((const u_char *)(tp + 1) > ep) {
487 tpn = nbp_name_print(tp, ep);
489 /* if the enumerator isn't 1, print it */
490 if (tp->enumerator != 1)
491 (void)printf("(%d)", tp->enumerator);
493 /* if the socket doesn't match the src socket, print it */
495 (void)printf(" %d", tp->skt);
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));
505 static const struct atNBPtuple *
506 nbp_name_print(const struct atNBPtuple *tp, register const u_char *ep)
508 register const char *cp = (const char *)tp + nbpTupleSize;
514 if ((cp = print_cstring(cp, ep)) != NULL) {
517 if ((cp = print_cstring(cp, ep)) != NULL) {
520 if ((cp = print_cstring(cp, ep)) != NULL)
524 return ((const struct atNBPtuple *)cp);
528 #define HASHNAMESIZE 4096
533 struct hnamemem *nxt;
536 static struct hnamemem hnametable[HASHNAMESIZE];
539 ataddr_string(u_short atnet, u_char athost)
541 register struct hnamemem *tp, *tp2;
542 register int i = (atnet << 8) | athost;
543 char nambuf[MAXHOSTNAMELEN + 20];
544 static int first = 1;
548 * if this is the first call, see if there's an AppleTalk
549 * number to name map file.
551 if (first && (first = 0, !nflag)
552 && (fp = fopen("/etc/atalk.names", "r"))) {
556 while (fgets(line, sizeof(line), fp)) {
557 if (line[0] == '\n' || line[0] == 0 || line[0] == '#')
559 if (sscanf(line, "%d.%d.%d %256s", &i1, &i2, &i3,
561 /* got a hostname. */
562 i3 |= ((i1 << 8) | i2) << 8;
563 else if (sscanf(line, "%d.%d %256s", &i1, &i2,
566 i3 = (((i1 << 8) | i2) << 8) | 255;
570 for (tp = &hnametable[i3 & (HASHNAMESIZE-1)];
571 tp->nxt; tp = tp->nxt)
574 tp->nxt = newhnamemem();
575 tp->name = strdup(nambuf);
580 for (tp = &hnametable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
584 /* didn't have the node name -- see if we've got the net name */
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",
592 tp->name = strdup(nambuf);
596 tp->addr = (atnet << 8) | athost;
597 tp->nxt = newhnamemem();
599 (void)snprintf(nambuf, sizeof(nambuf), "%d.%d.%d",
600 atnet >> 8, atnet & 0xff, athost);
602 (void)snprintf(nambuf, sizeof(nambuf), "%d.%d", atnet >> 8,
604 tp->name = strdup(nambuf);
609 static struct tok skt2str[] = {
610 { rtmpSkt, "rtmp" }, /* routing table maintenance */
611 { nbpSkt, "nis" }, /* name info socket */
612 { echoSkt, "echo" }, /* AppleTalk echo protocol */
613 { zipSkt, "zip" }, /* zone info protocol */
618 ddpskt_string(register int skt)
623 (void)snprintf(buf, sizeof(buf), "%d", skt);
626 return (tok2str(skt2str, "%d", skt));