4 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that: (1) source code distributions
9 * retain the above copyright notice and this paragraph in its entirety, (2)
10 * distributions including binary code include the above copyright notice and
11 * this paragraph in its entirety in the documentation or other materials
12 * provided with the distribution, and (3) all advertising materials mentioning
13 * features or use of this software display the following acknowledgement:
14 * ``This product includes software developed by the University of California,
15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16 * the University nor the names of its contributors may be used to endorse
17 * or promote products derived from this software without specific prior
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 * Format and print AppleTalk packets.
26 #include <sys/cdefs.h>
29 static const char rcsid
[] _U_
=
30 "@(#) Header: /tcpdump/master/tcpdump/print-atalk.c,v 1.81 2004/05/01 09:41:50 hannes Exp (LBL)";
32 __RCSID("$NetBSD: tcpdump2rcsid.ex,v 1.1 2001/06/25 20:09:58 itojun Exp $");
40 #include <tcpdump-stdinc.h>
47 #include "interface.h"
48 #include "addrtoname.h"
49 #include "ethertype.h"
50 #include "extract.h" /* must come after interface.h */
51 #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 LLAP packets received on a physical LocalTalk interface.
92 ltalk_if_print(const struct pcap_pkthdr
*h
, const u_char
*p
)
94 return (llap_print(p
, h
->caplen
));
98 * Print AppleTalk LLAP packets.
101 llap_print(register const u_char
*bp
, u_int length
)
103 register const struct LAP
*lp
;
104 register const struct atDDP
*dp
;
105 register const struct atShortDDP
*sdp
;
110 * Our packet is on a 4-byte boundary, as we're either called
111 * directly from a top-level link-layer printer (ltalk_if_print)
112 * or from the UDP printer. The LLAP+DDP header is a multiple
113 * of 4 bytes in length, so the DDP payload is also on a 4-byte
114 * boundary, and we don't need to align it before calling
117 lp
= (const struct LAP
*)bp
;
119 length
-= sizeof(*lp
);
120 hdrlen
= sizeof(*lp
);
124 if (length
< ddpSSize
) {
125 (void)printf(" [|sddp %d]", length
);
128 sdp
= (const struct atShortDDP
*)bp
;
130 ataddr_string(0, lp
->src
), ddpskt_string(sdp
->srcSkt
));
132 ataddr_string(0, lp
->dst
), ddpskt_string(sdp
->dstSkt
));
136 ddp_print(bp
, length
, sdp
->type
, 0, lp
->src
, sdp
->srcSkt
);
140 if (length
< ddpSize
) {
141 (void)printf(" [|ddp %d]", length
);
144 dp
= (const struct atDDP
*)bp
;
145 snet
= EXTRACT_16BITS(&dp
->srcNet
);
146 printf("%s.%s", ataddr_string(snet
, dp
->srcNode
),
147 ddpskt_string(dp
->srcSkt
));
149 ataddr_string(EXTRACT_16BITS(&dp
->dstNet
), dp
->dstNode
),
150 ddpskt_string(dp
->dstSkt
));
154 ddp_print(bp
, length
, dp
->type
, snet
, dp
->srcNode
, dp
->srcSkt
);
159 klap_print(bp
, length
);
164 printf("%d > %d at-lap#%d %d",
165 lp
->src
, lp
->dst
, lp
->type
, length
);
172 * Print EtherTalk/TokenTalk packets (or FDDITalk, or whatever it's called
173 * when it runs over FDDI; yes, I've seen FDDI captures with AppleTalk
177 atalk_print(register const u_char
*bp
, u_int length
)
179 register const struct atDDP
*dp
;
185 if (length
< ddpSize
) {
186 (void)printf(" [|ddp %d]", length
);
189 dp
= (const struct atDDP
*)bp
;
190 snet
= EXTRACT_16BITS(&dp
->srcNet
);
191 printf("%s.%s", ataddr_string(snet
, dp
->srcNode
),
192 ddpskt_string(dp
->srcSkt
));
194 ataddr_string(EXTRACT_16BITS(&dp
->dstNet
), dp
->dstNode
),
195 ddpskt_string(dp
->dstSkt
));
198 ddp_print(bp
, length
, dp
->type
, snet
, dp
->srcNode
, dp
->srcSkt
);
201 /* XXX should probably pass in the snap header and do checks like arp_print() */
203 aarp_print(register const u_char
*bp
, u_int length
)
205 register const struct aarp
*ap
;
207 #define AT(member) ataddr_string((ap->member[1]<<8)|ap->member[2],ap->member[3])
210 ap
= (const struct aarp
*)bp
;
211 if (EXTRACT_16BITS(&ap
->htype
) == 1 &&
212 EXTRACT_16BITS(&ap
->ptype
) == ETHERTYPE_ATALK
&&
213 ap
->halen
== 6 && ap
->palen
== 4 )
214 switch (EXTRACT_16BITS(&ap
->op
)) {
216 case 1: /* request */
217 (void)printf("who-has %s tell %s",
218 AT(pdaddr
), AT(psaddr
));
221 case 2: /* response */
222 (void)printf("reply %s is-at %s",
223 AT(pdaddr
), etheraddr_string(ap
->hdaddr
));
226 case 3: /* probe (oy!) */
227 (void)printf("probe %s tell %s",
228 AT(pdaddr
), AT(psaddr
));
231 (void)printf("len %u op %u htype %u ptype %#x halen %u palen %u",
232 length
, EXTRACT_16BITS(&ap
->op
), EXTRACT_16BITS(&ap
->htype
),
233 EXTRACT_16BITS(&ap
->ptype
), ap
->halen
, ap
->palen
);
237 * Print AppleTalk Datagram Delivery Protocol packets.
240 ddp_print(register const u_char
*bp
, register u_int length
, register int t
,
241 register u_short snet
, register u_char snode
, u_char skt
)
247 nbp_print((const struct atNBP
*)bp
, length
, snet
, snode
, skt
);
251 atp_print((const struct atATP
*)bp
, length
);
255 eigrp_print(bp
, length
);
259 (void)printf(" at-%s %d", tok2str(type2str
, NULL
, t
), length
);
265 atp_print(register const struct atATP
*ap
, u_int length
)
270 if ((const u_char
*)(ap
+ 1) > snapend
) {
271 /* Just bail if we don't have the whole chunk. */
275 length
-= sizeof(*ap
);
276 switch (ap
->control
& 0xc0) {
279 (void)printf(" atp-req%s %d",
280 ap
->control
& atpXO
? " " : "*",
281 EXTRACT_16BITS(&ap
->transID
));
283 atp_bitmap_print(ap
->bitmap
);
286 (void)printf(" [len=%d]", length
);
288 switch (ap
->control
& (atpEOM
|atpSTS
)) {
290 (void)printf(" [EOM]");
293 (void)printf(" [STS]");
296 (void)printf(" [EOM,STS]");
302 (void)printf(" atp-resp%s%d:%d (%d)",
303 ap
->control
& atpEOM
? "*" : " ",
304 EXTRACT_16BITS(&ap
->transID
), ap
->bitmap
, length
);
305 switch (ap
->control
& (atpXO
|atpSTS
)) {
307 (void)printf(" [XO]");
310 (void)printf(" [STS]");
313 (void)printf(" [XO,STS]");
319 (void)printf(" atp-rel %d", EXTRACT_16BITS(&ap
->transID
));
321 atp_bitmap_print(ap
->bitmap
);
323 /* length should be zero */
325 (void)printf(" [len=%d]", length
);
327 /* there shouldn't be any control flags */
328 if (ap
->control
& (atpXO
|atpEOM
|atpSTS
)) {
330 if (ap
->control
& atpXO
) {
331 (void)printf("%cXO", c
);
334 if (ap
->control
& atpEOM
) {
335 (void)printf("%cEOM", c
);
338 if (ap
->control
& atpSTS
) {
339 (void)printf("%cSTS", c
);
347 (void)printf(" atp-0x%x %d (%d)", ap
->control
,
348 EXTRACT_16BITS(&ap
->transID
), length
);
351 data
= EXTRACT_32BITS(&ap
->userData
);
353 (void)printf(" 0x%x", data
);
357 atp_bitmap_print(register u_char bm
)
363 * The '& 0xff' below is needed for compilers that want to sign
364 * extend a u_char, which is the case with the Ultrix compiler.
365 * (gcc is smart enough to eliminate it, at least on the Sparc).
367 if ((bm
+ 1) & (bm
& 0xff)) {
369 for (i
= 0; bm
; ++i
) {
371 (void)printf("%c%d", c
, i
);
381 (void)printf("<0-%d>", i
- 1);
388 nbp_print(register const struct atNBP
*np
, u_int length
, register u_short snet
,
389 register u_char snode
, register u_char skt
)
391 register const struct atNBPtuple
*tp
=
392 (const struct atNBPtuple
*)((u_char
*)np
+ nbpHeaderSize
);
396 if (length
< nbpHeaderSize
) {
397 (void)printf(" truncated-nbp %d", length
);
401 length
-= nbpHeaderSize
;
403 /* must be room for at least one tuple */
404 (void)printf(" truncated-nbp %d", length
+ nbpHeaderSize
);
407 /* ep points to end of available data */
409 if ((const u_char
*)tp
> ep
) {
413 switch (i
= np
->control
& 0xf0) {
417 (void)printf(i
== nbpLkUp
? " nbp-lkup %d:":" nbp-brRq %d:",
419 if ((const u_char
*)(tp
+ 1) > ep
) {
423 (void)nbp_name_print(tp
, ep
);
425 * look for anomalies: the spec says there can only
426 * be one tuple, the address must match the source
427 * address and the enumerator should be zero.
429 if ((np
->control
& 0xf) != 1)
430 (void)printf(" [ntup=%d]", np
->control
& 0xf);
432 (void)printf(" [enum=%d]", tp
->enumerator
);
433 if (EXTRACT_16BITS(&tp
->net
) != snet
||
434 tp
->node
!= snode
|| tp
->skt
!= skt
)
435 (void)printf(" [addr=%s.%d]",
436 ataddr_string(EXTRACT_16BITS(&tp
->net
),
441 (void)printf(" nbp-reply %d:", np
->id
);
443 /* print each of the tuples in the reply */
444 for (i
= np
->control
& 0xf; --i
>= 0 && tp
; )
445 tp
= nbp_tuple_print(tp
, ep
, snet
, snode
, skt
);
449 (void)printf(" nbp-0x%x %d (%d)", np
->control
, np
->id
,
455 /* print a counted string */
457 print_cstring(register const char *cp
, register const u_char
*ep
)
459 register u_int length
;
461 if (cp
>= (const char *)ep
) {
467 /* Spec says string can be at most 32 bytes long */
469 (void)printf("[len=%u]", length
);
472 while ((int)--length
>= 0) {
473 if (cp
>= (const char *)ep
) {
482 static const struct atNBPtuple
*
483 nbp_tuple_print(register const struct atNBPtuple
*tp
,
484 register const u_char
*ep
,
485 register u_short snet
, register u_char snode
,
488 register const struct atNBPtuple
*tpn
;
490 if ((const u_char
*)(tp
+ 1) > ep
) {
494 tpn
= nbp_name_print(tp
, ep
);
496 /* if the enumerator isn't 1, print it */
497 if (tp
->enumerator
!= 1)
498 (void)printf("(%d)", tp
->enumerator
);
500 /* if the socket doesn't match the src socket, print it */
502 (void)printf(" %d", tp
->skt
);
504 /* if the address doesn't match the src address, it's an anomaly */
505 if (EXTRACT_16BITS(&tp
->net
) != snet
|| tp
->node
!= snode
)
506 (void)printf(" [addr=%s]",
507 ataddr_string(EXTRACT_16BITS(&tp
->net
), tp
->node
));
512 static const struct atNBPtuple
*
513 nbp_name_print(const struct atNBPtuple
*tp
, register const u_char
*ep
)
515 register const char *cp
= (const char *)tp
+ nbpTupleSize
;
521 if ((cp
= print_cstring(cp
, ep
)) != NULL
) {
524 if ((cp
= print_cstring(cp
, ep
)) != NULL
) {
527 if ((cp
= print_cstring(cp
, ep
)) != NULL
)
531 return ((const struct atNBPtuple
*)cp
);
535 #define HASHNAMESIZE 4096
540 struct hnamemem
*nxt
;
543 static struct hnamemem hnametable
[HASHNAMESIZE
];
546 ataddr_string(u_short atnet
, u_char athost
)
548 register struct hnamemem
*tp
, *tp2
;
549 register int i
= (atnet
<< 8) | athost
;
550 char nambuf
[MAXHOSTNAMELEN
+ 20];
551 static int first
= 1;
555 * if this is the first call, see if there's an AppleTalk
556 * number to name map file.
558 if (first
&& (first
= 0, !nflag
)
559 && (fp
= fopen("/etc/atalk.names", "r"))) {
563 while (fgets(line
, sizeof(line
), fp
)) {
564 if (line
[0] == '\n' || line
[0] == 0 || line
[0] == '#')
566 if (sscanf(line
, "%d.%d.%d %256s", &i1
, &i2
, &i3
,
568 /* got a hostname. */
569 i3
|= ((i1
<< 8) | i2
) << 8;
570 else if (sscanf(line
, "%d.%d %256s", &i1
, &i2
,
573 i3
= (((i1
<< 8) | i2
) << 8) | 255;
577 for (tp
= &hnametable
[i3
& (HASHNAMESIZE
-1)];
578 tp
->nxt
; tp
= tp
->nxt
)
581 tp
->nxt
= newhnamemem();
582 tp
->name
= strdup(nambuf
);
587 for (tp
= &hnametable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
591 /* didn't have the node name -- see if we've got the net name */
593 for (tp2
= &hnametable
[i
& (HASHNAMESIZE
-1)]; tp2
->nxt
; tp2
= tp2
->nxt
)
594 if (tp2
->addr
== i
) {
595 tp
->addr
= (atnet
<< 8) | athost
;
596 tp
->nxt
= newhnamemem();
597 (void)snprintf(nambuf
, sizeof(nambuf
), "%s.%d",
599 tp
->name
= strdup(nambuf
);
603 tp
->addr
= (atnet
<< 8) | athost
;
604 tp
->nxt
= newhnamemem();
606 (void)snprintf(nambuf
, sizeof(nambuf
), "%d.%d.%d",
607 atnet
>> 8, atnet
& 0xff, athost
);
609 (void)snprintf(nambuf
, sizeof(nambuf
), "%d.%d", atnet
>> 8,
611 tp
->name
= strdup(nambuf
);
616 static struct tok skt2str
[] = {
617 { rtmpSkt
, "rtmp" }, /* routing table maintenance */
618 { nbpSkt
, "nis" }, /* name info socket */
619 { echoSkt
, "echo" }, /* AppleTalk echo protocol */
620 { zipSkt
, "zip" }, /* zone info protocol */
625 ddpskt_string(register int skt
)
630 (void)snprintf(buf
, sizeof(buf
), "%d", skt
);
633 return (tok2str(skt2str
, "%d", skt
));