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.64 2000/10/30 06:22:14 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 */
46 #include "interface.h"
47 #include "addrtoname.h"
48 #include "ethertype.h"
49 #include "extract.h" /* must come after interface.h */
50 #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 AppleTalk LLAP packets.
92 llap_print(register const u_char *bp, u_int length)
94 register const struct LAP *lp;
95 register const struct atDDP *dp;
96 register const struct atShortDDP *sdp;
100 lp = (struct LAP *)bp;
102 length -= sizeof(*lp);
105 static struct LAP lp_ = {0, 0, lapDDP};
112 if (length < ddpSSize) {
113 (void)printf(" [|sddp %d]", length);
116 sdp = (const struct atShortDDP *)bp;
118 ataddr_string(0, lp->src), ddpskt_string(sdp->srcSkt));
120 ataddr_string(0, lp->dst), ddpskt_string(sdp->dstSkt));
123 ddp_print(bp, length, sdp->type, 0, lp->src, sdp->srcSkt);
127 if (length < ddpSize) {
128 (void)printf(" [|ddp %d]", length);
131 dp = (const struct atDDP *)bp;
132 snet = EXTRACT_16BITS(&dp->srcNet);
133 printf("%s.%s", ataddr_string(snet, dp->srcNode),
134 ddpskt_string(dp->srcSkt));
136 ataddr_string(EXTRACT_16BITS(&dp->dstNet), dp->dstNode),
137 ddpskt_string(dp->dstSkt));
140 ddp_print(bp, length, dp->type, snet, dp->srcNode, dp->srcSkt);
145 klap_print(bp, length);
150 printf("%d > %d at-lap#%d %d",
151 lp->src, lp->dst, lp->type, length);
157 * Print EtherTalk/TokenTalk packets (or FDDITalk, or whatever it's called
158 * when it runs over FDDI; yes, I've seen FDDI captures with AppleTalk
162 atalk_print(register const u_char *bp, u_int length)
164 register const struct atDDP *dp;
167 if (length < ddpSize) {
168 (void)printf(" [|ddp %d]", length);
171 dp = (const struct atDDP *)bp;
172 snet = EXTRACT_16BITS(&dp->srcNet);
173 printf("%s.%s", ataddr_string(snet, dp->srcNode),
174 ddpskt_string(dp->srcSkt));
176 ataddr_string(EXTRACT_16BITS(&dp->dstNet), dp->dstNode),
177 ddpskt_string(dp->dstSkt));
180 ddp_print(bp, length, dp->type, snet, dp->srcNode, dp->srcSkt);
183 /* XXX should probably pass in the snap header and do checks like arp_print() */
185 aarp_print(register const u_char *bp, u_int length)
187 register const struct aarp *ap;
189 #define AT(member) ataddr_string((ap->member[1]<<8)|ap->member[2],ap->member[3])
192 ap = (const struct aarp *)bp;
193 if (ntohs(ap->htype) == 1 && ntohs(ap->ptype) == ETHERTYPE_ATALK &&
194 ap->halen == 6 && ap->palen == 4 )
195 switch (ntohs(ap->op)) {
197 case 1: /* request */
198 (void)printf("who-has %s tell %s",
199 AT(pdaddr), AT(psaddr));
202 case 2: /* response */
203 (void)printf("reply %s is-at %s",
204 AT(psaddr), etheraddr_string(ap->hsaddr));
207 case 3: /* probe (oy!) */
208 (void)printf("probe %s tell %s",
209 AT(pdaddr), AT(psaddr));
212 (void)printf("len %u op %u htype %u ptype %#x halen %u palen %u",
213 length, ntohs(ap->op), ntohs(ap->htype), ntohs(ap->ptype),
214 ap->halen, ap->palen);
218 * Print AppleTalk Datagram Delivery Protocol packets.
221 ddp_print(register const u_char *bp, register u_int length, register int t,
222 register u_short snet, register u_char snode, u_char skt)
228 nbp_print((const struct atNBP *)bp, length, snet, snode, skt);
232 atp_print((const struct atATP *)bp, length);
236 (void)printf(" at-%s %d", tok2str(type2str, NULL, t), length);
242 atp_print(register const struct atATP *ap, u_int length)
247 if ((const u_char *)(ap + 1) > snapend) {
248 /* Just bail if we don't have the whole chunk. */
252 length -= sizeof(*ap);
253 switch (ap->control & 0xc0) {
256 (void)printf(" atp-req%s %d",
257 ap->control & atpXO? " " : "*",
258 EXTRACT_16BITS(&ap->transID));
260 atp_bitmap_print(ap->bitmap);
263 (void)printf(" [len=%d]", length);
265 switch (ap->control & (atpEOM|atpSTS)) {
267 (void)printf(" [EOM]");
270 (void)printf(" [STS]");
273 (void)printf(" [EOM,STS]");
279 (void)printf(" atp-resp%s%d:%d (%d)",
280 ap->control & atpEOM? "*" : " ",
281 EXTRACT_16BITS(&ap->transID), ap->bitmap, length);
282 switch (ap->control & (atpXO|atpSTS)) {
284 (void)printf(" [XO]");
287 (void)printf(" [STS]");
290 (void)printf(" [XO,STS]");
296 (void)printf(" atp-rel %d", EXTRACT_16BITS(&ap->transID));
298 atp_bitmap_print(ap->bitmap);
300 /* length should be zero */
302 (void)printf(" [len=%d]", length);
304 /* there shouldn't be any control flags */
305 if (ap->control & (atpXO|atpEOM|atpSTS)) {
307 if (ap->control & atpXO) {
308 (void)printf("%cXO", c);
311 if (ap->control & atpEOM) {
312 (void)printf("%cEOM", c);
315 if (ap->control & atpSTS) {
316 (void)printf("%cSTS", c);
324 (void)printf(" atp-0x%x %d (%d)", ap->control,
325 EXTRACT_16BITS(&ap->transID), length);
328 data = EXTRACT_32BITS(&ap->userData);
330 (void)printf(" 0x%x", data);
334 atp_bitmap_print(register u_char bm)
340 * The '& 0xff' below is needed for compilers that want to sign
341 * extend a u_char, which is the case with the Ultrix compiler.
342 * (gcc is smart enough to eliminate it, at least on the Sparc).
344 if ((bm + 1) & (bm & 0xff)) {
346 for (i = 0; bm; ++i) {
348 (void)printf("%c%d", c, i);
358 (void)printf("<0-%d>", i - 1);
365 nbp_print(register const struct atNBP *np, u_int length, register u_short snet,
366 register u_char snode, register u_char skt)
368 register const struct atNBPtuple *tp =
369 (struct atNBPtuple *)((u_char *)np + nbpHeaderSize);
373 length -= nbpHeaderSize;
375 /* must be room for at least one tuple */
376 (void)printf(" truncated-nbp %d", length + nbpHeaderSize);
379 /* ep points to end of available data */
381 if ((const u_char *)tp > ep) {
385 switch (i = np->control & 0xf0) {
389 (void)printf(i == nbpLkUp? " nbp-lkup %d:":" nbp-brRq %d:",
391 if ((const u_char *)(tp + 1) > ep) {
395 (void)nbp_name_print(tp, ep);
397 * look for anomalies: the spec says there can only
398 * be one tuple, the address must match the source
399 * address and the enumerator should be zero.
401 if ((np->control & 0xf) != 1)
402 (void)printf(" [ntup=%d]", np->control & 0xf);
404 (void)printf(" [enum=%d]", tp->enumerator);
405 if (EXTRACT_16BITS(&tp->net) != snet ||
406 tp->node != snode || tp->skt != skt)
407 (void)printf(" [addr=%s.%d]",
408 ataddr_string(EXTRACT_16BITS(&tp->net),
413 (void)printf(" nbp-reply %d:", np->id);
415 /* print each of the tuples in the reply */
416 for (i = np->control & 0xf; --i >= 0 && tp; )
417 tp = nbp_tuple_print(tp, ep, snet, snode, skt);
421 (void)printf(" nbp-0x%x %d (%d)", np->control, np->id,
427 /* print a counted string */
429 print_cstring(register const char *cp, register const u_char *ep)
431 register u_int length;
433 if (cp >= (const char *)ep) {
439 /* Spec says string can be at most 32 bytes long */
441 (void)printf("[len=%u]", length);
444 while ((int)--length >= 0) {
445 if (cp >= (char *)ep) {
454 static const struct atNBPtuple *
455 nbp_tuple_print(register const struct atNBPtuple *tp,
456 register const u_char *ep,
457 register u_short snet, register u_char snode,
460 register const struct atNBPtuple *tpn;
462 if ((const u_char *)(tp + 1) > ep) {
466 tpn = nbp_name_print(tp, ep);
468 /* if the enumerator isn't 1, print it */
469 if (tp->enumerator != 1)
470 (void)printf("(%d)", tp->enumerator);
472 /* if the socket doesn't match the src socket, print it */
474 (void)printf(" %d", tp->skt);
476 /* if the address doesn't match the src address, it's an anomaly */
477 if (EXTRACT_16BITS(&tp->net) != snet || tp->node != snode)
478 (void)printf(" [addr=%s]",
479 ataddr_string(EXTRACT_16BITS(&tp->net), tp->node));
484 static const struct atNBPtuple *
485 nbp_name_print(const struct atNBPtuple *tp, register const u_char *ep)
487 register const char *cp = (const char *)tp + nbpTupleSize;
493 if ((cp = print_cstring(cp, ep)) != NULL) {
496 if ((cp = print_cstring(cp, ep)) != NULL) {
499 if ((cp = print_cstring(cp, ep)) != NULL)
503 return ((const struct atNBPtuple *)cp);
507 #define HASHNAMESIZE 4096
512 struct hnamemem *nxt;
515 static struct hnamemem hnametable[HASHNAMESIZE];
518 ataddr_string(u_short atnet, u_char athost)
520 register struct hnamemem *tp, *tp2;
521 register int i = (atnet << 8) | athost;
522 char nambuf[MAXHOSTNAMELEN + 20];
523 static int first = 1;
527 * if this is the first call, see if there's an AppleTalk
528 * number to name map file.
530 if (first && (first = 0, !nflag)
531 && (fp = fopen("/etc/atalk.names", "r"))) {
535 while (fgets(line, sizeof(line), fp)) {
536 if (line[0] == '\n' || line[0] == 0 || line[0] == '#')
538 if (sscanf(line, "%d.%d.%d %256s", &i1, &i2, &i3,
540 /* got a hostname. */
541 i3 |= ((i1 << 8) | i2) << 8;
542 else if (sscanf(line, "%d.%d %256s", &i1, &i2,
545 i3 = (((i1 << 8) | i2) << 8) | 255;
549 for (tp = &hnametable[i2 & (HASHNAMESIZE-1)];
550 tp->nxt; tp = tp->nxt)
553 tp->nxt = newhnamemem();
554 tp->name = savestr(nambuf);
559 for (tp = &hnametable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
563 /* didn't have the node name -- see if we've got the net name */
565 for (tp2 = &hnametable[i & (HASHNAMESIZE-1)]; tp2->nxt; tp2 = tp2->nxt)
566 if (tp2->addr == i) {
567 tp->addr = (atnet << 8) | athost;
568 tp->nxt = newhnamemem();
569 (void)snprintf(nambuf, sizeof(nambuf), "%s.%d",
571 tp->name = savestr(nambuf);
575 tp->addr = (atnet << 8) | athost;
576 tp->nxt = newhnamemem();
578 (void)snprintf(nambuf, sizeof(nambuf), "%d.%d.%d",
579 atnet >> 8, atnet & 0xff, athost);
581 (void)snprintf(nambuf, sizeof(nambuf), "%d.%d", atnet >> 8,
583 tp->name = savestr(nambuf);
588 static struct tok skt2str[] = {
589 { rtmpSkt, "rtmp" }, /* routing table maintenance */
590 { nbpSkt, "nis" }, /* name info socket */
591 { echoSkt, "echo" }, /* AppleTalk echo protocol */
592 { zipSkt, "zip" }, /* zone info protocol */
597 ddpskt_string(register int skt)
602 (void)snprintf(buf, sizeof(buf), "%d", skt);
605 return (tok2str(skt2str, "%d", skt));