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.
26 #define NETDISSECT_REWORKED
31 #include <tcpdump-stdinc.h>
36 #include "interface.h"
37 #include "addrtoname.h"
38 #include "ethertype.h"
39 #include "extract.h" /* must come after interface.h */
40 #include "appletalk.h"
42 static const char tstr[] = "[|atalk]";
44 static const struct tok type2str[] = {
46 { ddpRTMPrequest, "rtmpReq" },
55 uint16_t htype, ptype;
64 static void atp_print(netdissect_options *, const struct atATP *, u_int);
65 static void atp_bitmap_print(netdissect_options *, u_char);
66 static void nbp_print(netdissect_options *, const struct atNBP *, u_int, u_short, u_char, u_char);
67 static const struct atNBPtuple *nbp_tuple_print(netdissect_options *ndo, const struct atNBPtuple *,
69 u_short, u_char, u_char);
70 static const struct atNBPtuple *nbp_name_print(netdissect_options *, const struct atNBPtuple *,
72 static const char *ataddr_string(netdissect_options *, u_short, u_char);
73 static void ddp_print(netdissect_options *, const u_char *, u_int, int, u_short, u_char, u_char);
74 static const char *ddpskt_string(netdissect_options *, int);
77 * Print LLAP packets received on a physical LocalTalk interface.
80 ltalk_if_print(netdissect_options *ndo,
81 const struct pcap_pkthdr *h, const u_char *p)
83 return (llap_print(ndo, p, h->caplen));
87 * Print AppleTalk LLAP packets.
90 llap_print(netdissect_options *ndo,
91 register const u_char *bp, u_int length)
93 register const struct LAP *lp;
94 register const struct atDDP *dp;
95 register const struct atShortDDP *sdp;
99 if (length < sizeof(*lp)) {
100 ND_PRINT((ndo, " [|llap %u]", length));
103 lp = (const struct LAP *)bp;
105 length -= sizeof(*lp);
106 hdrlen = sizeof(*lp);
110 if (length < ddpSSize) {
111 ND_PRINT((ndo, " [|sddp %u]", length));
114 sdp = (const struct atShortDDP *)bp;
115 ND_PRINT((ndo, "%s.%s",
116 ataddr_string(ndo, 0, lp->src), ddpskt_string(ndo, sdp->srcSkt)));
117 ND_PRINT((ndo, " > %s.%s:",
118 ataddr_string(ndo, 0, lp->dst), ddpskt_string(ndo, sdp->dstSkt)));
122 ddp_print(ndo, bp, length, sdp->type, 0, lp->src, sdp->srcSkt);
126 if (length < ddpSize) {
127 ND_PRINT((ndo, " [|ddp %u]", length));
130 dp = (const struct atDDP *)bp;
131 snet = EXTRACT_16BITS(&dp->srcNet);
132 ND_PRINT((ndo, "%s.%s", ataddr_string(ndo, snet, dp->srcNode),
133 ddpskt_string(ndo, dp->srcSkt)));
134 ND_PRINT((ndo, " > %s.%s:",
135 ataddr_string(ndo, EXTRACT_16BITS(&dp->dstNet), dp->dstNode),
136 ddpskt_string(ndo, dp->dstSkt)));
140 ddp_print(ndo, bp, length, dp->type, snet, dp->srcNode, dp->srcSkt);
145 klap_print(bp, length);
150 ND_PRINT((ndo, "%d > %d at-lap#%d %u",
151 lp->src, lp->dst, lp->type, length));
158 * Print EtherTalk/TokenTalk packets (or FDDITalk, or whatever it's called
159 * when it runs over FDDI; yes, I've seen FDDI captures with AppleTalk
163 atalk_print(netdissect_options *ndo,
164 register const u_char *bp, u_int length)
166 register const struct atDDP *dp;
170 ND_PRINT((ndo, "AT "));
172 if (length < ddpSize) {
173 ND_PRINT((ndo, " [|ddp %u]", length));
176 dp = (const struct atDDP *)bp;
177 snet = EXTRACT_16BITS(&dp->srcNet);
178 ND_PRINT((ndo, "%s.%s", ataddr_string(ndo, snet, dp->srcNode),
179 ddpskt_string(ndo, dp->srcSkt)));
180 ND_PRINT((ndo, " > %s.%s: ",
181 ataddr_string(ndo, EXTRACT_16BITS(&dp->dstNet), dp->dstNode),
182 ddpskt_string(ndo, dp->dstSkt)));
185 ddp_print(ndo, bp, length, dp->type, snet, dp->srcNode, dp->srcSkt);
188 /* XXX should probably pass in the snap header and do checks like arp_print() */
190 aarp_print(netdissect_options *ndo,
191 register const u_char *bp, u_int length)
193 register const struct aarp *ap;
195 #define AT(member) ataddr_string(ndo, (ap->member[1]<<8)|ap->member[2],ap->member[3])
197 ND_PRINT((ndo, "aarp "));
198 ap = (const struct aarp *)bp;
199 if (EXTRACT_16BITS(&ap->htype) == 1 &&
200 EXTRACT_16BITS(&ap->ptype) == ETHERTYPE_ATALK &&
201 ap->halen == 6 && ap->palen == 4 )
202 switch (EXTRACT_16BITS(&ap->op)) {
204 case 1: /* request */
205 ND_PRINT((ndo, "who-has %s tell %s", AT(pdaddr), AT(psaddr)));
208 case 2: /* response */
209 ND_PRINT((ndo, "reply %s is-at %s", AT(psaddr), etheraddr_string(ndo, ap->hsaddr)));
212 case 3: /* probe (oy!) */
213 ND_PRINT((ndo, "probe %s tell %s", AT(pdaddr), AT(psaddr)));
216 ND_PRINT((ndo, "len %u op %u htype %u ptype %#x halen %u palen %u",
217 length, EXTRACT_16BITS(&ap->op), EXTRACT_16BITS(&ap->htype),
218 EXTRACT_16BITS(&ap->ptype), ap->halen, ap->palen));
222 * Print AppleTalk Datagram Delivery Protocol packets.
225 ddp_print(netdissect_options *ndo,
226 register const u_char *bp, register u_int length, register int t,
227 register u_short snet, register u_char snode, u_char skt)
233 nbp_print(ndo, (const struct atNBP *)bp, length, snet, snode, skt);
237 atp_print(ndo, (const struct atATP *)bp, length);
241 eigrp_print(ndo, bp, length);
245 ND_PRINT((ndo, " at-%s %d", tok2str(type2str, NULL, t), length));
251 atp_print(netdissect_options *ndo,
252 register const struct atATP *ap, u_int length)
257 if ((const u_char *)(ap + 1) > ndo->ndo_snapend) {
258 /* Just bail if we don't have the whole chunk. */
259 ND_PRINT((ndo, "%s", tstr));
262 if (length < sizeof(*ap)) {
263 ND_PRINT((ndo, " [|atp %u]", length));
266 length -= sizeof(*ap);
267 switch (ap->control & 0xc0) {
270 ND_PRINT((ndo, " atp-req%s %d",
271 ap->control & atpXO? " " : "*",
272 EXTRACT_16BITS(&ap->transID)));
274 atp_bitmap_print(ndo, ap->bitmap);
277 ND_PRINT((ndo, " [len=%u]", length));
279 switch (ap->control & (atpEOM|atpSTS)) {
281 ND_PRINT((ndo, " [EOM]"));
284 ND_PRINT((ndo, " [STS]"));
287 ND_PRINT((ndo, " [EOM,STS]"));
293 ND_PRINT((ndo, " atp-resp%s%d:%d (%u)",
294 ap->control & atpEOM? "*" : " ",
295 EXTRACT_16BITS(&ap->transID), ap->bitmap, length));
296 switch (ap->control & (atpXO|atpSTS)) {
298 ND_PRINT((ndo, " [XO]"));
301 ND_PRINT((ndo, " [STS]"));
304 ND_PRINT((ndo, " [XO,STS]"));
310 ND_PRINT((ndo, " atp-rel %d", EXTRACT_16BITS(&ap->transID)));
312 atp_bitmap_print(ndo, ap->bitmap);
314 /* length should be zero */
316 ND_PRINT((ndo, " [len=%u]", length));
318 /* there shouldn't be any control flags */
319 if (ap->control & (atpXO|atpEOM|atpSTS)) {
321 if (ap->control & atpXO) {
322 ND_PRINT((ndo, "%cXO", c));
325 if (ap->control & atpEOM) {
326 ND_PRINT((ndo, "%cEOM", c));
329 if (ap->control & atpSTS) {
330 ND_PRINT((ndo, "%cSTS", c));
333 ND_PRINT((ndo, "]"));
338 ND_PRINT((ndo, " atp-0x%x %d (%u)", ap->control,
339 EXTRACT_16BITS(&ap->transID), length));
342 data = EXTRACT_32BITS(&ap->userData);
344 ND_PRINT((ndo, " 0x%x", data));
348 atp_bitmap_print(netdissect_options *ndo,
355 * The '& 0xff' below is needed for compilers that want to sign
356 * extend a u_char, which is the case with the Ultrix compiler.
357 * (gcc is smart enough to eliminate it, at least on the Sparc).
359 if ((bm + 1) & (bm & 0xff)) {
361 for (i = 0; bm; ++i) {
363 ND_PRINT((ndo, "%c%d", c, i));
368 ND_PRINT((ndo, ">"));
373 ND_PRINT((ndo, "<0-%d>", i - 1));
375 ND_PRINT((ndo, "<0>"));
380 nbp_print(netdissect_options *ndo,
381 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 ND_PRINT((ndo, " truncated-nbp %u", length));
394 length -= nbpHeaderSize;
396 /* must be room for at least one tuple */
397 ND_PRINT((ndo, " truncated-nbp %u", length + nbpHeaderSize));
400 /* ep points to end of available data */
401 ep = ndo->ndo_snapend;
402 if ((const u_char *)tp > ep) {
403 ND_PRINT((ndo, "%s", tstr));
406 switch (i = np->control & 0xf0) {
410 ND_PRINT((ndo, i == nbpLkUp? " nbp-lkup %d:":" nbp-brRq %d:", np->id));
411 if ((const u_char *)(tp + 1) > ep) {
412 ND_PRINT((ndo, "%s", tstr));
415 (void)nbp_name_print(ndo, tp, ep);
417 * look for anomalies: the spec says there can only
418 * be one tuple, the address must match the source
419 * address and the enumerator should be zero.
421 if ((np->control & 0xf) != 1)
422 ND_PRINT((ndo, " [ntup=%d]", np->control & 0xf));
424 ND_PRINT((ndo, " [enum=%d]", tp->enumerator));
425 if (EXTRACT_16BITS(&tp->net) != snet ||
426 tp->node != snode || tp->skt != skt)
427 ND_PRINT((ndo, " [addr=%s.%d]",
428 ataddr_string(ndo, EXTRACT_16BITS(&tp->net),
429 tp->node), tp->skt));
433 ND_PRINT((ndo, " nbp-reply %d:", np->id));
435 /* print each of the tuples in the reply */
436 for (i = np->control & 0xf; --i >= 0 && tp; )
437 tp = nbp_tuple_print(ndo, tp, ep, snet, snode, skt);
441 ND_PRINT((ndo, " nbp-0x%x %d (%u)", np->control, np->id, length));
446 /* print a counted string */
448 print_cstring(netdissect_options *ndo,
449 register const char *cp, register const u_char *ep)
451 register u_int length;
453 if (cp >= (const char *)ep) {
454 ND_PRINT((ndo, "%s", tstr));
459 /* Spec says string can be at most 32 bytes long */
461 ND_PRINT((ndo, "[len=%u]", length));
464 while ((int)--length >= 0) {
465 if (cp >= (const char *)ep) {
466 ND_PRINT((ndo, "%s", tstr));
469 ND_PRINT((ndo, "%c", *cp++));
474 static const struct atNBPtuple *
475 nbp_tuple_print(netdissect_options *ndo,
476 register const struct atNBPtuple *tp, register const u_char *ep,
477 register u_short snet, register u_char snode, register u_char skt)
479 register const struct atNBPtuple *tpn;
481 if ((const u_char *)(tp + 1) > ep) {
482 ND_PRINT((ndo, "%s", tstr));
485 tpn = nbp_name_print(ndo, tp, ep);
487 /* if the enumerator isn't 1, print it */
488 if (tp->enumerator != 1)
489 ND_PRINT((ndo, "(%d)", tp->enumerator));
491 /* if the socket doesn't match the src socket, print it */
493 ND_PRINT((ndo, " %d", tp->skt));
495 /* if the address doesn't match the src address, it's an anomaly */
496 if (EXTRACT_16BITS(&tp->net) != snet || tp->node != snode)
497 ND_PRINT((ndo, " [addr=%s]",
498 ataddr_string(ndo, EXTRACT_16BITS(&tp->net), tp->node)));
503 static const struct atNBPtuple *
504 nbp_name_print(netdissect_options *ndo,
505 const struct atNBPtuple *tp, register const u_char *ep)
507 register const char *cp = (const char *)tp + nbpTupleSize;
509 ND_PRINT((ndo, " "));
512 ND_PRINT((ndo, "\""));
513 if ((cp = print_cstring(ndo, cp, ep)) != NULL) {
515 ND_PRINT((ndo, ":"));
516 if ((cp = print_cstring(ndo, cp, ep)) != NULL) {
518 ND_PRINT((ndo, "@"));
519 if ((cp = print_cstring(ndo, cp, ep)) != NULL)
520 ND_PRINT((ndo, "\""));
523 return ((const struct atNBPtuple *)cp);
527 #define HASHNAMESIZE 4096
532 struct hnamemem *nxt;
535 static struct hnamemem hnametable[HASHNAMESIZE];
538 ataddr_string(netdissect_options *ndo,
539 u_short atnet, u_char athost)
541 register struct hnamemem *tp, *tp2;
542 register int i = (atnet << 8) | athost;
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, !ndo->ndo_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 %256s", &i1, &i2, nambuf) == 3)
560 /* got a hostname. */
562 else if (sscanf(line, "%d %256s", &i1, nambuf) == 2)
564 i2 = (i1 << 8) | 255;
568 for (tp = &hnametable[i2 & (HASHNAMESIZE-1)];
569 tp->nxt; tp = tp->nxt)
572 tp->nxt = newhnamemem();
573 tp->name = strdup(nambuf);
578 for (tp = &hnametable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
582 /* didn't have the node name -- see if we've got the net name */
584 for (tp2 = &hnametable[i & (HASHNAMESIZE-1)]; tp2->nxt; tp2 = tp2->nxt)
585 if (tp2->addr == i) {
586 tp->addr = (atnet << 8) | athost;
587 tp->nxt = newhnamemem();
588 (void)snprintf(nambuf, sizeof(nambuf), "%s.%d",
590 tp->name = strdup(nambuf);
594 tp->addr = (atnet << 8) | athost;
595 tp->nxt = newhnamemem();
597 (void)snprintf(nambuf, sizeof(nambuf), "%d.%d", atnet, athost);
599 (void)snprintf(nambuf, sizeof(nambuf), "%d", atnet);
600 tp->name = strdup(nambuf);
605 static const struct tok skt2str[] = {
606 { rtmpSkt, "rtmp" }, /* routing table maintenance */
607 { nbpSkt, "nis" }, /* name info socket */
608 { echoSkt, "echo" }, /* AppleTalk echo protocol */
609 { zipSkt, "zip" }, /* zone info protocol */
614 ddpskt_string(netdissect_options *ndo,
619 if (ndo->ndo_nflag) {
620 (void)snprintf(buf, sizeof(buf), "%d", skt);
623 return (tok2str(skt2str, "%d", skt));