]>
The Tcpdump Group git mirrors - tcpdump/blob - print-atalk.c
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.
28 #include <tcpdump-stdinc.h>
35 #include "interface.h"
36 #include "addrtoname.h"
37 #include "ethertype.h"
38 #include "extract.h" /* must come after interface.h */
39 #include "appletalk.h"
41 static const char tstr
[] = "[|atalk]";
43 static const struct tok type2str
[] = {
45 { ddpRTMPrequest
, "rtmpReq" },
54 u_int16_t htype
, ptype
;
55 u_int8_t halen
, palen
;
63 static void atp_print(const struct atATP
*, u_int
);
64 static void atp_bitmap_print(u_char
);
65 static void nbp_print(const struct atNBP
*, u_int
, u_short
, u_char
, u_char
);
66 static const char *print_cstring(const char *, const u_char
*);
67 static const struct atNBPtuple
*nbp_tuple_print(const struct atNBPtuple
*,
69 u_short
, u_char
, u_char
);
70 static const struct atNBPtuple
*nbp_name_print(const struct atNBPtuple
*,
72 static const char *ataddr_string(u_short
, u_char
);
73 static void ddp_print(const u_char
*, u_int
, int, u_short
, u_char
, u_char
);
74 static const char *ddpskt_string(int);
77 * Print LLAP packets received on a physical LocalTalk interface.
80 ltalk_if_print(const struct pcap_pkthdr
*h
, const u_char
*p
)
82 return (llap_print(p
, h
->caplen
));
86 * Print AppleTalk LLAP packets.
89 llap_print(register const u_char
*bp
, u_int length
)
91 register const struct LAP
*lp
;
92 register const struct atDDP
*dp
;
93 register const struct atShortDDP
*sdp
;
97 if (length
< sizeof(*lp
)) {
98 (void)printf(" [|llap %u]", length
);
101 lp
= (const struct LAP
*)bp
;
103 length
-= sizeof(*lp
);
104 hdrlen
= sizeof(*lp
);
108 if (length
< ddpSSize
) {
109 (void)printf(" [|sddp %u]", length
);
112 sdp
= (const struct atShortDDP
*)bp
;
114 ataddr_string(0, lp
->src
), ddpskt_string(sdp
->srcSkt
));
116 ataddr_string(0, lp
->dst
), ddpskt_string(sdp
->dstSkt
));
120 ddp_print(bp
, length
, sdp
->type
, 0, lp
->src
, sdp
->srcSkt
);
124 if (length
< ddpSize
) {
125 (void)printf(" [|ddp %u]", length
);
128 dp
= (const struct atDDP
*)bp
;
129 snet
= EXTRACT_16BITS(&dp
->srcNet
);
130 printf("%s.%s", ataddr_string(snet
, dp
->srcNode
),
131 ddpskt_string(dp
->srcSkt
));
133 ataddr_string(EXTRACT_16BITS(&dp
->dstNet
), dp
->dstNode
),
134 ddpskt_string(dp
->dstSkt
));
138 ddp_print(bp
, length
, dp
->type
, snet
, dp
->srcNode
, dp
->srcSkt
);
143 klap_print(bp
, length
);
148 printf("%d > %d at-lap#%d %u",
149 lp
->src
, lp
->dst
, lp
->type
, length
);
156 * Print EtherTalk/TokenTalk packets (or FDDITalk, or whatever it's called
157 * when it runs over FDDI; yes, I've seen FDDI captures with AppleTalk
161 atalk_print(register const u_char
*bp
, u_int length
)
163 register const struct atDDP
*dp
;
169 if (length
< ddpSize
) {
170 (void)printf(" [|ddp %u]", length
);
173 dp
= (const struct atDDP
*)bp
;
174 snet
= EXTRACT_16BITS(&dp
->srcNet
);
175 printf("%s.%s", ataddr_string(snet
, dp
->srcNode
),
176 ddpskt_string(dp
->srcSkt
));
178 ataddr_string(EXTRACT_16BITS(&dp
->dstNet
), dp
->dstNode
),
179 ddpskt_string(dp
->dstSkt
));
182 ddp_print(bp
, length
, dp
->type
, snet
, dp
->srcNode
, dp
->srcSkt
);
185 /* XXX should probably pass in the snap header and do checks like arp_print() */
187 aarp_print(register const u_char
*bp
, u_int length
)
189 register const struct aarp
*ap
;
191 #define AT(member) ataddr_string((ap->member[1]<<8)|ap->member[2],ap->member[3])
194 ap
= (const struct aarp
*)bp
;
195 if (EXTRACT_16BITS(&ap
->htype
) == 1 &&
196 EXTRACT_16BITS(&ap
->ptype
) == ETHERTYPE_ATALK
&&
197 ap
->halen
== 6 && ap
->palen
== 4 )
198 switch (EXTRACT_16BITS(&ap
->op
)) {
200 case 1: /* request */
201 (void)printf("who-has %s tell %s",
202 AT(pdaddr
), AT(psaddr
));
205 case 2: /* response */
206 (void)printf("reply %s is-at %s",
207 AT(psaddr
), etheraddr_string(ap
->hsaddr
));
210 case 3: /* probe (oy!) */
211 (void)printf("probe %s tell %s",
212 AT(pdaddr
), AT(psaddr
));
215 (void)printf("len %u op %u htype %u ptype %#x halen %u palen %u",
216 length
, EXTRACT_16BITS(&ap
->op
), EXTRACT_16BITS(&ap
->htype
),
217 EXTRACT_16BITS(&ap
->ptype
), ap
->halen
, ap
->palen
);
221 * Print AppleTalk Datagram Delivery Protocol packets.
224 ddp_print(register const u_char
*bp
, register u_int length
, register int t
,
225 register u_short snet
, register u_char snode
, u_char skt
)
231 nbp_print((const struct atNBP
*)bp
, length
, snet
, snode
, skt
);
235 atp_print((const struct atATP
*)bp
, length
);
239 eigrp_print(bp
, length
);
243 (void)printf(" at-%s %d", tok2str(type2str
, NULL
, t
), length
);
249 atp_print(register const struct atATP
*ap
, u_int length
)
254 if ((const u_char
*)(ap
+ 1) > snapend
) {
255 /* Just bail if we don't have the whole chunk. */
259 if (length
< sizeof(*ap
)) {
260 (void)printf(" [|atp %u]", length
);
263 length
-= sizeof(*ap
);
264 switch (ap
->control
& 0xc0) {
267 (void)printf(" atp-req%s %d",
268 ap
->control
& atpXO
? " " : "*",
269 EXTRACT_16BITS(&ap
->transID
));
271 atp_bitmap_print(ap
->bitmap
);
274 (void)printf(" [len=%u]", length
);
276 switch (ap
->control
& (atpEOM
|atpSTS
)) {
278 (void)printf(" [EOM]");
281 (void)printf(" [STS]");
284 (void)printf(" [EOM,STS]");
290 (void)printf(" atp-resp%s%d:%d (%u)",
291 ap
->control
& atpEOM
? "*" : " ",
292 EXTRACT_16BITS(&ap
->transID
), ap
->bitmap
, length
);
293 switch (ap
->control
& (atpXO
|atpSTS
)) {
295 (void)printf(" [XO]");
298 (void)printf(" [STS]");
301 (void)printf(" [XO,STS]");
307 (void)printf(" atp-rel %d", EXTRACT_16BITS(&ap
->transID
));
309 atp_bitmap_print(ap
->bitmap
);
311 /* length should be zero */
313 (void)printf(" [len=%u]", length
);
315 /* there shouldn't be any control flags */
316 if (ap
->control
& (atpXO
|atpEOM
|atpSTS
)) {
318 if (ap
->control
& atpXO
) {
319 (void)printf("%cXO", c
);
322 if (ap
->control
& atpEOM
) {
323 (void)printf("%cEOM", c
);
326 if (ap
->control
& atpSTS
) {
327 (void)printf("%cSTS", c
);
335 (void)printf(" atp-0x%x %d (%u)", ap
->control
,
336 EXTRACT_16BITS(&ap
->transID
), length
);
339 data
= EXTRACT_32BITS(&ap
->userData
);
341 (void)printf(" 0x%x", data
);
345 atp_bitmap_print(register u_char bm
)
351 * The '& 0xff' below is needed for compilers that want to sign
352 * extend a u_char, which is the case with the Ultrix compiler.
353 * (gcc is smart enough to eliminate it, at least on the Sparc).
355 if ((bm
+ 1) & (bm
& 0xff)) {
357 for (i
= 0; bm
; ++i
) {
359 (void)printf("%c%d", c
, i
);
369 (void)printf("<0-%d>", i
- 1);
376 nbp_print(register const struct atNBP
*np
, u_int length
, register u_short snet
,
377 register u_char snode
, register u_char skt
)
379 register const struct atNBPtuple
*tp
=
380 (const struct atNBPtuple
*)((u_char
*)np
+ nbpHeaderSize
);
384 if (length
< nbpHeaderSize
) {
385 (void)printf(" truncated-nbp %u", length
);
389 length
-= nbpHeaderSize
;
391 /* must be room for at least one tuple */
392 (void)printf(" truncated-nbp %u", length
+ nbpHeaderSize
);
395 /* ep points to end of available data */
397 if ((const u_char
*)tp
> ep
) {
401 switch (i
= np
->control
& 0xf0) {
405 (void)printf(i
== nbpLkUp
? " nbp-lkup %d:":" nbp-brRq %d:",
407 if ((const u_char
*)(tp
+ 1) > ep
) {
411 (void)nbp_name_print(tp
, ep
);
413 * look for anomalies: the spec says there can only
414 * be one tuple, the address must match the source
415 * address and the enumerator should be zero.
417 if ((np
->control
& 0xf) != 1)
418 (void)printf(" [ntup=%d]", np
->control
& 0xf);
420 (void)printf(" [enum=%d]", tp
->enumerator
);
421 if (EXTRACT_16BITS(&tp
->net
) != snet
||
422 tp
->node
!= snode
|| tp
->skt
!= skt
)
423 (void)printf(" [addr=%s.%d]",
424 ataddr_string(EXTRACT_16BITS(&tp
->net
),
429 (void)printf(" nbp-reply %d:", np
->id
);
431 /* print each of the tuples in the reply */
432 for (i
= np
->control
& 0xf; --i
>= 0 && tp
; )
433 tp
= nbp_tuple_print(tp
, ep
, snet
, snode
, skt
);
437 (void)printf(" nbp-0x%x %d (%u)", np
->control
, np
->id
,
443 /* print a counted string */
445 print_cstring(register const char *cp
, register const u_char
*ep
)
447 register u_int length
;
449 if (cp
>= (const char *)ep
) {
455 /* Spec says string can be at most 32 bytes long */
457 (void)printf("[len=%u]", length
);
460 while ((int)--length
>= 0) {
461 if (cp
>= (const char *)ep
) {
470 static const struct atNBPtuple
*
471 nbp_tuple_print(register const struct atNBPtuple
*tp
,
472 register const u_char
*ep
,
473 register u_short snet
, register u_char snode
,
476 register const struct atNBPtuple
*tpn
;
478 if ((const u_char
*)(tp
+ 1) > ep
) {
482 tpn
= nbp_name_print(tp
, ep
);
484 /* if the enumerator isn't 1, print it */
485 if (tp
->enumerator
!= 1)
486 (void)printf("(%d)", tp
->enumerator
);
488 /* if the socket doesn't match the src socket, print it */
490 (void)printf(" %d", tp
->skt
);
492 /* if the address doesn't match the src address, it's an anomaly */
493 if (EXTRACT_16BITS(&tp
->net
) != snet
|| tp
->node
!= snode
)
494 (void)printf(" [addr=%s]",
495 ataddr_string(EXTRACT_16BITS(&tp
->net
), tp
->node
));
500 static const struct atNBPtuple
*
501 nbp_name_print(const struct atNBPtuple
*tp
, register const u_char
*ep
)
503 register const char *cp
= (const char *)tp
+ nbpTupleSize
;
509 if ((cp
= print_cstring(cp
, ep
)) != NULL
) {
512 if ((cp
= print_cstring(cp
, ep
)) != NULL
) {
515 if ((cp
= print_cstring(cp
, ep
)) != NULL
)
519 return ((const struct atNBPtuple
*)cp
);
523 #define HASHNAMESIZE 4096
528 struct hnamemem
*nxt
;
531 static struct hnamemem hnametable
[HASHNAMESIZE
];
534 ataddr_string(u_short atnet
, u_char athost
)
536 register struct hnamemem
*tp
, *tp2
;
537 register int i
= (atnet
<< 8) | athost
;
539 static int first
= 1;
543 * if this is the first call, see if there's an AppleTalk
544 * number to name map file.
546 if (first
&& (first
= 0, !nflag
)
547 && (fp
= fopen("/etc/atalk.names", "r"))) {
551 while (fgets(line
, sizeof(line
), fp
)) {
552 if (line
[0] == '\n' || line
[0] == 0 || line
[0] == '#')
554 if (sscanf(line
, "%d.%d %256s", &i1
, &i2
, nambuf
) == 3)
555 /* got a hostname. */
557 else if (sscanf(line
, "%d %256s", &i1
, nambuf
) == 2)
559 i2
= (i1
<< 8) | 255;
563 for (tp
= &hnametable
[i2
& (HASHNAMESIZE
-1)];
564 tp
->nxt
; tp
= tp
->nxt
)
567 tp
->nxt
= newhnamemem();
568 tp
->name
= strdup(nambuf
);
573 for (tp
= &hnametable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
577 /* didn't have the node name -- see if we've got the net name */
579 for (tp2
= &hnametable
[i
& (HASHNAMESIZE
-1)]; tp2
->nxt
; tp2
= tp2
->nxt
)
580 if (tp2
->addr
== i
) {
581 tp
->addr
= (atnet
<< 8) | athost
;
582 tp
->nxt
= newhnamemem();
583 (void)snprintf(nambuf
, sizeof(nambuf
), "%s.%d",
585 tp
->name
= strdup(nambuf
);
589 tp
->addr
= (atnet
<< 8) | athost
;
590 tp
->nxt
= newhnamemem();
592 (void)snprintf(nambuf
, sizeof(nambuf
), "%d.%d", atnet
, athost
);
594 (void)snprintf(nambuf
, sizeof(nambuf
), "%d", atnet
);
595 tp
->name
= strdup(nambuf
);
600 static const struct tok skt2str
[] = {
601 { rtmpSkt
, "rtmp" }, /* routing table maintenance */
602 { nbpSkt
, "nis" }, /* name info socket */
603 { echoSkt
, "echo" }, /* AppleTalk echo protocol */
604 { zipSkt
, "zip" }, /* zone info protocol */
609 ddpskt_string(register int skt
)
614 (void)snprintf(buf
, sizeof(buf
), "%d", skt
);
617 return (tok2str(skt2str
, "%d", skt
));