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;
106 * Our packet is on a 4-byte boundary, as we're either called
107 * directly from a top-level link-layer printer (ltalk_if_print)
108 * or from the UDP printer. The LLAP+DDP header is a multiple
109 * of 4 bytes in length, so the DDP payload is also on a 4-byte
110 * boundary, and we don't need to align it before calling
113 lp = (const struct LAP *)bp;
115 length -= sizeof(*lp);
118 static struct LAP lp_ = {0, 0, lapDDP};
122 hdrlen = sizeof(*lp);
126 if (length < ddpSSize) {
127 (void)printf(" [|sddp %d]", length);
130 sdp = (const struct atShortDDP *)bp;
132 ataddr_string(0, lp->src), ddpskt_string(sdp->srcSkt));
134 ataddr_string(0, lp->dst), ddpskt_string(sdp->dstSkt));
138 ddp_print(bp, length, sdp->type, 0, lp->src, sdp->srcSkt);
142 if (length < ddpSize) {
143 (void)printf(" [|ddp %d]", length);
146 dp = (const struct atDDP *)bp;
147 snet = EXTRACT_16BITS(&dp->srcNet);
148 printf("%s.%s", ataddr_string(snet, dp->srcNode),
149 ddpskt_string(dp->srcSkt));
151 ataddr_string(EXTRACT_16BITS(&dp->dstNet), dp->dstNode),
152 ddpskt_string(dp->dstSkt));
156 ddp_print(bp, length, dp->type, snet, dp->srcNode, dp->srcSkt);
161 klap_print(bp, length);
166 printf("%d > %d at-lap#%d %d",
167 lp->src, lp->dst, lp->type, length);
174 * Print EtherTalk/TokenTalk packets (or FDDITalk, or whatever it's called
175 * when it runs over FDDI; yes, I've seen FDDI captures with AppleTalk
179 atalk_print(register const u_char *bp, u_int length)
181 register const struct atDDP *dp;
187 if (length < ddpSize) {
188 (void)printf(" [|ddp %d]", length);
191 dp = (const struct atDDP *)bp;
192 snet = EXTRACT_16BITS(&dp->srcNet);
193 printf("%s.%s", ataddr_string(snet, dp->srcNode),
194 ddpskt_string(dp->srcSkt));
196 ataddr_string(EXTRACT_16BITS(&dp->dstNet), dp->dstNode),
197 ddpskt_string(dp->dstSkt));
200 ddp_print(bp, length, dp->type, snet, dp->srcNode, dp->srcSkt);
203 /* XXX should probably pass in the snap header and do checks like arp_print() */
205 aarp_print(register const u_char *bp, u_int length)
207 register const struct aarp *ap;
209 #define AT(member) ataddr_string((ap->member[1]<<8)|ap->member[2],ap->member[3])
212 ap = (const struct aarp *)bp;
213 if (EXTRACT_16BITS(&ap->htype) == 1 &&
214 EXTRACT_16BITS(&ap->ptype) == ETHERTYPE_ATALK &&
215 ap->halen == 6 && ap->palen == 4 )
216 switch (EXTRACT_16BITS(&ap->op)) {
218 case 1: /* request */
219 (void)printf("who-has %s tell %s",
220 AT(pdaddr), AT(psaddr));
223 case 2: /* response */
224 (void)printf("reply %s is-at %s",
225 AT(psaddr), etheraddr_string(ap->hsaddr));
228 case 3: /* probe (oy!) */
229 (void)printf("probe %s tell %s",
230 AT(pdaddr), AT(psaddr));
233 (void)printf("len %u op %u htype %u ptype %#x halen %u palen %u",
234 length, EXTRACT_16BITS(&ap->op), EXTRACT_16BITS(&ap->htype),
235 EXTRACT_16BITS(&ap->ptype), ap->halen, ap->palen);
239 * Print AppleTalk Datagram Delivery Protocol packets.
242 ddp_print(register const u_char *bp, register u_int length, register int t,
243 register u_short snet, register u_char snode, u_char skt)
249 nbp_print((const struct atNBP *)bp, length, snet, snode, skt);
253 atp_print((const struct atATP *)bp, length);
257 eigrp_print(bp, length);
261 (void)printf(" at-%s %d", tok2str(type2str, NULL, t), length);
267 atp_print(register const struct atATP *ap, u_int length)
272 if ((const u_char *)(ap + 1) > snapend) {
273 /* Just bail if we don't have the whole chunk. */
277 length -= sizeof(*ap);
278 switch (ap->control & 0xc0) {
281 (void)printf(" atp-req%s %d",
282 ap->control & atpXO? " " : "*",
283 EXTRACT_16BITS(&ap->transID));
285 atp_bitmap_print(ap->bitmap);
288 (void)printf(" [len=%d]", length);
290 switch (ap->control & (atpEOM|atpSTS)) {
292 (void)printf(" [EOM]");
295 (void)printf(" [STS]");
298 (void)printf(" [EOM,STS]");
304 (void)printf(" atp-resp%s%d:%d (%d)",
305 ap->control & atpEOM? "*" : " ",
306 EXTRACT_16BITS(&ap->transID), ap->bitmap, length);
307 switch (ap->control & (atpXO|atpSTS)) {
309 (void)printf(" [XO]");
312 (void)printf(" [STS]");
315 (void)printf(" [XO,STS]");
321 (void)printf(" atp-rel %d", EXTRACT_16BITS(&ap->transID));
323 atp_bitmap_print(ap->bitmap);
325 /* length should be zero */
327 (void)printf(" [len=%d]", length);
329 /* there shouldn't be any control flags */
330 if (ap->control & (atpXO|atpEOM|atpSTS)) {
332 if (ap->control & atpXO) {
333 (void)printf("%cXO", c);
336 if (ap->control & atpEOM) {
337 (void)printf("%cEOM", c);
340 if (ap->control & atpSTS) {
341 (void)printf("%cSTS", c);
349 (void)printf(" atp-0x%x %d (%d)", ap->control,
350 EXTRACT_16BITS(&ap->transID), length);
353 data = EXTRACT_32BITS(&ap->userData);
355 (void)printf(" 0x%x", data);
359 atp_bitmap_print(register u_char bm)
365 * The '& 0xff' below is needed for compilers that want to sign
366 * extend a u_char, which is the case with the Ultrix compiler.
367 * (gcc is smart enough to eliminate it, at least on the Sparc).
369 if ((bm + 1) & (bm & 0xff)) {
371 for (i = 0; bm; ++i) {
373 (void)printf("%c%d", c, i);
383 (void)printf("<0-%d>", i - 1);
390 nbp_print(register const struct atNBP *np, u_int length, register u_short snet,
391 register u_char snode, register u_char skt)
393 register const struct atNBPtuple *tp =
394 (const struct atNBPtuple *)((u_char *)np + nbpHeaderSize);
398 if (length < nbpHeaderSize) {
399 (void)printf(" truncated-nbp %d", length);
403 length -= nbpHeaderSize;
405 /* must be room for at least one tuple */
406 (void)printf(" truncated-nbp %d", length + nbpHeaderSize);
409 /* ep points to end of available data */
411 if ((const u_char *)tp > ep) {
415 switch (i = np->control & 0xf0) {
419 (void)printf(i == nbpLkUp? " nbp-lkup %d:":" nbp-brRq %d:",
421 if ((const u_char *)(tp + 1) > ep) {
425 (void)nbp_name_print(tp, ep);
427 * look for anomalies: the spec says there can only
428 * be one tuple, the address must match the source
429 * address and the enumerator should be zero.
431 if ((np->control & 0xf) != 1)
432 (void)printf(" [ntup=%d]", np->control & 0xf);
434 (void)printf(" [enum=%d]", tp->enumerator);
435 if (EXTRACT_16BITS(&tp->net) != snet ||
436 tp->node != snode || tp->skt != skt)
437 (void)printf(" [addr=%s.%d]",
438 ataddr_string(EXTRACT_16BITS(&tp->net),
443 (void)printf(" nbp-reply %d:", np->id);
445 /* print each of the tuples in the reply */
446 for (i = np->control & 0xf; --i >= 0 && tp; )
447 tp = nbp_tuple_print(tp, ep, snet, snode, skt);
451 (void)printf(" nbp-0x%x %d (%d)", np->control, np->id,
457 /* print a counted string */
459 print_cstring(register const char *cp, register const u_char *ep)
461 register u_int length;
463 if (cp >= (const char *)ep) {
469 /* Spec says string can be at most 32 bytes long */
471 (void)printf("[len=%u]", length);
474 while ((int)--length >= 0) {
475 if (cp >= (const char *)ep) {
484 static const struct atNBPtuple *
485 nbp_tuple_print(register const struct atNBPtuple *tp,
486 register const u_char *ep,
487 register u_short snet, register u_char snode,
490 register const struct atNBPtuple *tpn;
492 if ((const u_char *)(tp + 1) > ep) {
496 tpn = nbp_name_print(tp, ep);
498 /* if the enumerator isn't 1, print it */
499 if (tp->enumerator != 1)
500 (void)printf("(%d)", tp->enumerator);
502 /* if the socket doesn't match the src socket, print it */
504 (void)printf(" %d", tp->skt);
506 /* if the address doesn't match the src address, it's an anomaly */
507 if (EXTRACT_16BITS(&tp->net) != snet || tp->node != snode)
508 (void)printf(" [addr=%s]",
509 ataddr_string(EXTRACT_16BITS(&tp->net), tp->node));
514 static const struct atNBPtuple *
515 nbp_name_print(const struct atNBPtuple *tp, register const u_char *ep)
517 register const char *cp = (const char *)tp + nbpTupleSize;
523 if ((cp = print_cstring(cp, ep)) != NULL) {
526 if ((cp = print_cstring(cp, ep)) != NULL) {
529 if ((cp = print_cstring(cp, ep)) != NULL)
533 return ((const struct atNBPtuple *)cp);
537 #define HASHNAMESIZE 4096
542 struct hnamemem *nxt;
545 static struct hnamemem hnametable[HASHNAMESIZE];
548 ataddr_string(u_short atnet, u_char athost)
550 register struct hnamemem *tp, *tp2;
551 register int i = (atnet << 8) | athost;
552 char nambuf[MAXHOSTNAMELEN + 20];
553 static int first = 1;
557 * if this is the first call, see if there's an AppleTalk
558 * number to name map file.
560 if (first && (first = 0, !nflag)
561 && (fp = fopen("/etc/atalk.names", "r"))) {
565 while (fgets(line, sizeof(line), fp)) {
566 if (line[0] == '\n' || line[0] == 0 || line[0] == '#')
568 if (sscanf(line, "%d.%d.%d %256s", &i1, &i2, &i3,
570 /* got a hostname. */
571 i3 |= ((i1 << 8) | i2) << 8;
572 else if (sscanf(line, "%d.%d %256s", &i1, &i2,
575 i3 = (((i1 << 8) | i2) << 8) | 255;
579 for (tp = &hnametable[i2 & (HASHNAMESIZE-1)];
580 tp->nxt; tp = tp->nxt)
583 tp->nxt = newhnamemem();
584 tp->name = strdup(nambuf);
589 for (tp = &hnametable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
593 /* didn't have the node name -- see if we've got the net name */
595 for (tp2 = &hnametable[i & (HASHNAMESIZE-1)]; tp2->nxt; tp2 = tp2->nxt)
596 if (tp2->addr == i) {
597 tp->addr = (atnet << 8) | athost;
598 tp->nxt = newhnamemem();
599 (void)snprintf(nambuf, sizeof(nambuf), "%s.%d",
601 tp->name = strdup(nambuf);
605 tp->addr = (atnet << 8) | athost;
606 tp->nxt = newhnamemem();
608 (void)snprintf(nambuf, sizeof(nambuf), "%d.%d.%d",
609 atnet >> 8, atnet & 0xff, athost);
611 (void)snprintf(nambuf, sizeof(nambuf), "%d.%d", atnet >> 8,
613 tp->name = strdup(nambuf);
618 static struct tok skt2str[] = {
619 { rtmpSkt, "rtmp" }, /* routing table maintenance */
620 { nbpSkt, "nis" }, /* name info socket */
621 { echoSkt, "echo" }, /* AppleTalk echo protocol */
622 { zipSkt, "zip" }, /* zone info protocol */
627 ddpskt_string(register int skt)
632 (void)snprintf(buf, sizeof(buf), "%d", skt);
635 return (tok2str(skt2str, "%d", skt));