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[] _U_ =
28 "@(#) $Header: /tcpdump/master/tcpdump/print-atalk.c,v 1.81 2004-05-01 09:41:50 hannes Exp $ (LBL)";
35 #include <tcpdump-stdinc.h>
42 #include "interface.h"
43 #include "addrtoname.h"
44 #include "ethertype.h"
45 #include "extract.h" /* must come after interface.h */
46 #include "appletalk.h"
48 static struct tok type2str[] = {
50 { ddpRTMPrequest, "rtmpReq" },
59 u_int16_t htype, ptype;
60 u_int8_t halen, palen;
68 static char tstr[] = "[|atalk]";
70 static void atp_print(const struct atATP *, u_int);
71 static void atp_bitmap_print(u_char);
72 static void nbp_print(const struct atNBP *, u_int, u_short, u_char, u_char);
73 static const char *print_cstring(const char *, const u_char *);
74 static const struct atNBPtuple *nbp_tuple_print(const struct atNBPtuple *,
76 u_short, u_char, u_char);
77 static const struct atNBPtuple *nbp_name_print(const struct atNBPtuple *,
79 static const char *ataddr_string(u_short, u_char);
80 static void ddp_print(const u_char *, u_int, int, u_short, u_char, u_char);
81 static const char *ddpskt_string(int);
84 * Print LLAP packets received on a physical LocalTalk interface.
87 ltalk_if_print(const struct pcap_pkthdr *h, const u_char *p)
89 return (llap_print(p, h->caplen));
93 * Print AppleTalk LLAP packets.
96 llap_print(register const u_char *bp, u_int length)
98 register const struct LAP *lp;
99 register const struct atDDP *dp;
100 register const struct atShortDDP *sdp;
104 if (length < sizeof(*lp)) {
105 (void)printf(" [|llap %u]", length);
108 lp = (const struct LAP *)bp;
110 length -= sizeof(*lp);
111 hdrlen = sizeof(*lp);
115 if (length < ddpSSize) {
116 (void)printf(" [|sddp %u]", length);
119 sdp = (const struct atShortDDP *)bp;
121 ataddr_string(0, lp->src), ddpskt_string(sdp->srcSkt));
123 ataddr_string(0, lp->dst), ddpskt_string(sdp->dstSkt));
127 ddp_print(bp, length, sdp->type, 0, lp->src, sdp->srcSkt);
131 if (length < ddpSize) {
132 (void)printf(" [|ddp %u]", length);
135 dp = (const struct atDDP *)bp;
136 snet = EXTRACT_16BITS(&dp->srcNet);
137 printf("%s.%s", ataddr_string(snet, dp->srcNode),
138 ddpskt_string(dp->srcSkt));
140 ataddr_string(EXTRACT_16BITS(&dp->dstNet), dp->dstNode),
141 ddpskt_string(dp->dstSkt));
145 ddp_print(bp, length, dp->type, snet, dp->srcNode, dp->srcSkt);
150 klap_print(bp, length);
155 printf("%d > %d at-lap#%d %u",
156 lp->src, lp->dst, lp->type, length);
163 * Print EtherTalk/TokenTalk packets (or FDDITalk, or whatever it's called
164 * when it runs over FDDI; yes, I've seen FDDI captures with AppleTalk
168 atalk_print(register const u_char *bp, u_int length)
170 register const struct atDDP *dp;
176 if (length < ddpSize) {
177 (void)printf(" [|ddp %u]", length);
180 dp = (const struct atDDP *)bp;
181 snet = EXTRACT_16BITS(&dp->srcNet);
182 printf("%s.%s", ataddr_string(snet, dp->srcNode),
183 ddpskt_string(dp->srcSkt));
185 ataddr_string(EXTRACT_16BITS(&dp->dstNet), dp->dstNode),
186 ddpskt_string(dp->dstSkt));
189 ddp_print(bp, length, dp->type, snet, dp->srcNode, dp->srcSkt);
192 /* XXX should probably pass in the snap header and do checks like arp_print() */
194 aarp_print(register const u_char *bp, u_int length)
196 register const struct aarp *ap;
198 #define AT(member) ataddr_string((ap->member[1]<<8)|ap->member[2],ap->member[3])
201 ap = (const struct aarp *)bp;
202 if (EXTRACT_16BITS(&ap->htype) == 1 &&
203 EXTRACT_16BITS(&ap->ptype) == ETHERTYPE_ATALK &&
204 ap->halen == 6 && ap->palen == 4 )
205 switch (EXTRACT_16BITS(&ap->op)) {
207 case 1: /* request */
208 (void)printf("who-has %s tell %s",
209 AT(pdaddr), AT(psaddr));
212 case 2: /* response */
213 (void)printf("reply %s is-at %s",
214 AT(psaddr), etheraddr_string(ap->hsaddr));
217 case 3: /* probe (oy!) */
218 (void)printf("probe %s tell %s",
219 AT(pdaddr), AT(psaddr));
222 (void)printf("len %u op %u htype %u ptype %#x halen %u palen %u",
223 length, EXTRACT_16BITS(&ap->op), EXTRACT_16BITS(&ap->htype),
224 EXTRACT_16BITS(&ap->ptype), ap->halen, ap->palen);
228 * Print AppleTalk Datagram Delivery Protocol packets.
231 ddp_print(register const u_char *bp, register u_int length, register int t,
232 register u_short snet, register u_char snode, u_char skt)
238 nbp_print((const struct atNBP *)bp, length, snet, snode, skt);
242 atp_print((const struct atATP *)bp, length);
246 eigrp_print(bp, length);
250 (void)printf(" at-%s %d", tok2str(type2str, NULL, t), length);
256 atp_print(register const struct atATP *ap, u_int length)
261 if ((const u_char *)(ap + 1) > snapend) {
262 /* Just bail if we don't have the whole chunk. */
266 if (length < sizeof(*ap)) {
267 (void)printf(" [|atp %u]", length);
270 length -= sizeof(*ap);
271 switch (ap->control & 0xc0) {
274 (void)printf(" atp-req%s %d",
275 ap->control & atpXO? " " : "*",
276 EXTRACT_16BITS(&ap->transID));
278 atp_bitmap_print(ap->bitmap);
281 (void)printf(" [len=%u]", length);
283 switch (ap->control & (atpEOM|atpSTS)) {
285 (void)printf(" [EOM]");
288 (void)printf(" [STS]");
291 (void)printf(" [EOM,STS]");
297 (void)printf(" atp-resp%s%d:%d (%u)",
298 ap->control & atpEOM? "*" : " ",
299 EXTRACT_16BITS(&ap->transID), ap->bitmap, length);
300 switch (ap->control & (atpXO|atpSTS)) {
302 (void)printf(" [XO]");
305 (void)printf(" [STS]");
308 (void)printf(" [XO,STS]");
314 (void)printf(" atp-rel %d", EXTRACT_16BITS(&ap->transID));
316 atp_bitmap_print(ap->bitmap);
318 /* length should be zero */
320 (void)printf(" [len=%u]", length);
322 /* there shouldn't be any control flags */
323 if (ap->control & (atpXO|atpEOM|atpSTS)) {
325 if (ap->control & atpXO) {
326 (void)printf("%cXO", c);
329 if (ap->control & atpEOM) {
330 (void)printf("%cEOM", c);
333 if (ap->control & atpSTS) {
334 (void)printf("%cSTS", c);
342 (void)printf(" atp-0x%x %d (%u)", ap->control,
343 EXTRACT_16BITS(&ap->transID), length);
346 data = EXTRACT_32BITS(&ap->userData);
348 (void)printf(" 0x%x", data);
352 atp_bitmap_print(register u_char bm)
358 * The '& 0xff' below is needed for compilers that want to sign
359 * extend a u_char, which is the case with the Ultrix compiler.
360 * (gcc is smart enough to eliminate it, at least on the Sparc).
362 if ((bm + 1) & (bm & 0xff)) {
364 for (i = 0; bm; ++i) {
366 (void)printf("%c%d", c, i);
376 (void)printf("<0-%d>", i - 1);
383 nbp_print(register const struct atNBP *np, u_int length, register u_short snet,
384 register u_char snode, register u_char skt)
386 register const struct atNBPtuple *tp =
387 (const struct atNBPtuple *)((u_char *)np + nbpHeaderSize);
391 if (length < nbpHeaderSize) {
392 (void)printf(" truncated-nbp %u", length);
396 length -= nbpHeaderSize;
398 /* must be room for at least one tuple */
399 (void)printf(" truncated-nbp %u", length + nbpHeaderSize);
402 /* ep points to end of available data */
404 if ((const u_char *)tp > ep) {
408 switch (i = np->control & 0xf0) {
412 (void)printf(i == nbpLkUp? " nbp-lkup %d:":" nbp-brRq %d:",
414 if ((const u_char *)(tp + 1) > ep) {
418 (void)nbp_name_print(tp, ep);
420 * look for anomalies: the spec says there can only
421 * be one tuple, the address must match the source
422 * address and the enumerator should be zero.
424 if ((np->control & 0xf) != 1)
425 (void)printf(" [ntup=%d]", np->control & 0xf);
427 (void)printf(" [enum=%d]", tp->enumerator);
428 if (EXTRACT_16BITS(&tp->net) != snet ||
429 tp->node != snode || tp->skt != skt)
430 (void)printf(" [addr=%s.%d]",
431 ataddr_string(EXTRACT_16BITS(&tp->net),
436 (void)printf(" nbp-reply %d:", np->id);
438 /* print each of the tuples in the reply */
439 for (i = np->control & 0xf; --i >= 0 && tp; )
440 tp = nbp_tuple_print(tp, ep, snet, snode, skt);
444 (void)printf(" nbp-0x%x %d (%u)", np->control, np->id,
450 /* print a counted string */
452 print_cstring(register const char *cp, register const u_char *ep)
454 register u_int length;
456 if (cp >= (const char *)ep) {
462 /* Spec says string can be at most 32 bytes long */
464 (void)printf("[len=%u]", length);
467 while ((int)--length >= 0) {
468 if (cp >= (const char *)ep) {
477 static const struct atNBPtuple *
478 nbp_tuple_print(register const struct atNBPtuple *tp,
479 register const u_char *ep,
480 register u_short snet, register u_char snode,
483 register const struct atNBPtuple *tpn;
485 if ((const u_char *)(tp + 1) > ep) {
489 tpn = nbp_name_print(tp, ep);
491 /* if the enumerator isn't 1, print it */
492 if (tp->enumerator != 1)
493 (void)printf("(%d)", tp->enumerator);
495 /* if the socket doesn't match the src socket, print it */
497 (void)printf(" %d", tp->skt);
499 /* if the address doesn't match the src address, it's an anomaly */
500 if (EXTRACT_16BITS(&tp->net) != snet || tp->node != snode)
501 (void)printf(" [addr=%s]",
502 ataddr_string(EXTRACT_16BITS(&tp->net), tp->node));
507 static const struct atNBPtuple *
508 nbp_name_print(const struct atNBPtuple *tp, register const u_char *ep)
510 register const char *cp = (const char *)tp + nbpTupleSize;
516 if ((cp = print_cstring(cp, ep)) != NULL) {
519 if ((cp = print_cstring(cp, ep)) != NULL) {
522 if ((cp = print_cstring(cp, ep)) != NULL)
526 return ((const struct atNBPtuple *)cp);
530 #define HASHNAMESIZE 4096
535 struct hnamemem *nxt;
538 static struct hnamemem hnametable[HASHNAMESIZE];
541 ataddr_string(u_short atnet, u_char athost)
543 register struct hnamemem *tp, *tp2;
544 register int i = (atnet << 8) | athost;
545 char nambuf[MAXHOSTNAMELEN + 20];
546 static int first = 1;
550 * if this is the first call, see if there's an AppleTalk
551 * number to name map file.
553 if (first && (first = 0, !nflag)
554 && (fp = fopen("/etc/atalk.names", "r"))) {
558 while (fgets(line, sizeof(line), fp)) {
559 if (line[0] == '\n' || line[0] == 0 || line[0] == '#')
561 if (sscanf(line, "%d.%d %256s", &i1, &i2, nambuf) == 3)
562 /* got a hostname. */
564 else if (sscanf(line, "%d %256s", &i1, nambuf) == 2)
566 i2 = (i1 << 8) | 255;
570 for (tp = &hnametable[i2 & (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", atnet, athost);
601 (void)snprintf(nambuf, sizeof(nambuf), "%d", atnet);
602 tp->name = strdup(nambuf);
607 static struct tok skt2str[] = {
608 { rtmpSkt, "rtmp" }, /* routing table maintenance */
609 { nbpSkt, "nis" }, /* name info socket */
610 { echoSkt, "echo" }, /* AppleTalk echo protocol */
611 { zipSkt, "zip" }, /* zone info protocol */
616 ddpskt_string(register int skt)
621 (void)snprintf(buf, sizeof(buf), "%d", skt);
624 return (tok2str(skt2str, "%d", skt));