]>
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.
22 /* \summary: AppleTalk printer */
28 #include "netdissect-stdinc.h"
33 #include "netdissect.h"
34 #include "addrtoname.h"
35 #include "ethertype.h"
37 #include "appletalk.h"
39 static const char tstr
[] = "[|atalk]";
41 static const struct tok type2str
[] = {
43 { ddpRTMPrequest
, "rtmpReq" },
52 nd_uint16_t htype
, ptype
;
53 nd_uint8_t halen
, palen
;
61 static void atp_print(netdissect_options
*, const struct atATP
*, u_int
);
62 static void atp_bitmap_print(netdissect_options
*, u_char
);
63 static void nbp_print(netdissect_options
*, const struct atNBP
*, u_int
, u_short
, u_char
, u_char
);
64 static const struct atNBPtuple
*nbp_tuple_print(netdissect_options
*ndo
, const struct atNBPtuple
*,
66 u_short
, u_char
, u_char
);
67 static const struct atNBPtuple
*nbp_name_print(netdissect_options
*, const struct atNBPtuple
*,
69 static const char *ataddr_string(netdissect_options
*, u_short
, u_char
);
70 static void ddp_print(netdissect_options
*, const u_char
*, u_int
, u_int
, u_short
, u_char
, u_char
);
71 static const char *ddpskt_string(netdissect_options
*, u_int
);
74 * Print LLAP packets received on a physical LocalTalk interface.
77 ltalk_if_print(netdissect_options
*ndo
,
78 const struct pcap_pkthdr
*h
, const u_char
*p
)
82 ndo
->ndo_protocol
= "ltalk_if";
83 hdrlen
= llap_print(ndo
, p
, h
->len
);
85 /* Cut short by the snapshot length. */
92 * Print AppleTalk LLAP packets.
95 llap_print(netdissect_options
*ndo
,
96 const u_char
*bp
, u_int length
)
99 const struct atDDP
*dp
;
100 const struct atShortDDP
*sdp
;
104 ndo
->ndo_protocol
= "llap";
105 if (length
< sizeof(*lp
)) {
106 ND_PRINT(" [|llap %u]", length
);
109 if (!ND_TTEST_LEN(bp
, sizeof(*lp
))) {
110 ND_PRINT(" [|llap]");
111 return (0); /* cut short by the snapshot length */
113 lp
= (const struct LAP
*)bp
;
115 length
-= sizeof(*lp
);
116 hdrlen
= sizeof(*lp
);
117 switch (EXTRACT_U_1(lp
->type
)) {
120 if (length
< ddpSSize
) {
121 ND_PRINT(" [|sddp %u]", length
);
124 if (!ND_TTEST_LEN(bp
, ddpSSize
)) {
125 ND_PRINT(" [|sddp]");
126 return (0); /* cut short by the snapshot length */
128 sdp
= (const struct atShortDDP
*)bp
;
130 ataddr_string(ndo
, 0, EXTRACT_U_1(lp
->src
)), ddpskt_string(ndo
, EXTRACT_U_1(sdp
->srcSkt
)));
131 ND_PRINT(" > %s.%s:",
132 ataddr_string(ndo
, 0, EXTRACT_U_1(lp
->dst
)), ddpskt_string(ndo
, EXTRACT_U_1(sdp
->dstSkt
)));
136 ddp_print(ndo
, bp
, length
, EXTRACT_U_1(sdp
->type
), 0, EXTRACT_U_1(lp
->src
), EXTRACT_U_1(sdp
->srcSkt
));
140 if (length
< ddpSize
) {
141 ND_PRINT(" [|ddp %u]", length
);
144 if (!ND_TTEST_LEN(bp
, ddpSize
)) {
146 return (0); /* cut short by the snapshot length */
148 dp
= (const struct atDDP
*)bp
;
149 snet
= EXTRACT_BE_U_2(dp
->srcNet
);
150 ND_PRINT("%s.%s", ataddr_string(ndo
, snet
, EXTRACT_U_1(dp
->srcNode
)),
151 ddpskt_string(ndo
, EXTRACT_U_1(dp
->srcSkt
)));
152 ND_PRINT(" > %s.%s:",
153 ataddr_string(ndo
, EXTRACT_BE_U_2(dp
->dstNet
), EXTRACT_U_1(dp
->dstNode
)),
154 ddpskt_string(ndo
, EXTRACT_U_1(dp
->dstSkt
)));
158 ddp_print(ndo
, bp
, length
, EXTRACT_U_1(dp
->type
), snet
, EXTRACT_U_1(dp
->srcNode
), EXTRACT_U_1(dp
->srcSkt
));
163 klap_print(bp
, length
);
168 ND_PRINT("%u > %u at-lap#%u %u",
169 EXTRACT_U_1(lp
->src
), EXTRACT_U_1(lp
->dst
), EXTRACT_U_1(lp
->type
), length
);
176 * Print EtherTalk/TokenTalk packets (or FDDITalk, or whatever it's called
177 * when it runs over FDDI; yes, I've seen FDDI captures with AppleTalk
181 atalk_print(netdissect_options
*ndo
,
182 const u_char
*bp
, u_int length
)
184 const struct atDDP
*dp
;
187 ndo
->ndo_protocol
= "atalk";
191 if (length
< ddpSize
) {
192 ND_PRINT(" [|ddp %u]", length
);
195 if (!ND_TTEST_LEN(bp
, ddpSize
)) {
199 dp
= (const struct atDDP
*)bp
;
200 snet
= EXTRACT_BE_U_2(dp
->srcNet
);
201 ND_PRINT("%s.%s", ataddr_string(ndo
, snet
, EXTRACT_U_1(dp
->srcNode
)),
202 ddpskt_string(ndo
, EXTRACT_U_1(dp
->srcSkt
)));
203 ND_PRINT(" > %s.%s: ",
204 ataddr_string(ndo
, EXTRACT_BE_U_2(dp
->dstNet
), EXTRACT_U_1(dp
->dstNode
)),
205 ddpskt_string(ndo
, EXTRACT_U_1(dp
->dstSkt
)));
208 ddp_print(ndo
, bp
, length
, EXTRACT_U_1(dp
->type
), snet
, EXTRACT_U_1(dp
->srcNode
), EXTRACT_U_1(dp
->srcSkt
));
211 /* XXX should probably pass in the snap header and do checks like arp_print() */
213 aarp_print(netdissect_options
*ndo
,
214 const u_char
*bp
, u_int length
)
216 const struct aarp
*ap
;
218 #define AT(member) ataddr_string(ndo, (ap->member[1]<<8)|ap->member[2],ap->member[3])
220 ndo
->ndo_protocol
= "aarp";
222 ap
= (const struct aarp
*)bp
;
223 if (!ND_TTEST_SIZE(ap
)) {
224 /* Just bail if we don't have the whole chunk. */
225 ND_PRINT(" [|aarp]");
228 if (length
< sizeof(*ap
)) {
229 ND_PRINT(" [|aarp %u]", length
);
232 if (EXTRACT_BE_U_2(ap
->htype
) == 1 &&
233 EXTRACT_BE_U_2(ap
->ptype
) == ETHERTYPE_ATALK
&&
234 EXTRACT_U_1(ap
->halen
) == 6 && EXTRACT_U_1(ap
->palen
) == 4 )
235 switch (EXTRACT_BE_U_2(ap
->op
)) {
237 case 1: /* request */
238 ND_PRINT("who-has %s tell %s", AT(pdaddr
), AT(psaddr
));
241 case 2: /* response */
242 ND_PRINT("reply %s is-at %s", AT(psaddr
), etheraddr_string(ndo
, ap
->hsaddr
));
245 case 3: /* probe (oy!) */
246 ND_PRINT("probe %s tell %s", AT(pdaddr
), AT(psaddr
));
249 ND_PRINT("len %u op %u htype %u ptype %#x halen %u palen %u",
250 length
, EXTRACT_BE_U_2(ap
->op
), EXTRACT_BE_U_2(ap
->htype
),
251 EXTRACT_BE_U_2(ap
->ptype
), EXTRACT_U_1(ap
->halen
), EXTRACT_U_1(ap
->palen
));
255 * Print AppleTalk Datagram Delivery Protocol packets.
258 ddp_print(netdissect_options
*ndo
,
259 const u_char
*bp
, u_int length
, u_int t
,
260 u_short snet
, u_char snode
, u_char skt
)
266 nbp_print(ndo
, (const struct atNBP
*)bp
, length
, snet
, snode
, skt
);
270 atp_print(ndo
, (const struct atATP
*)bp
, length
);
274 eigrp_print(ndo
, bp
, length
);
278 ND_PRINT(" at-%s %u", tok2str(type2str
, NULL
, t
), length
);
284 atp_print(netdissect_options
*ndo
,
285 const struct atATP
*ap
, u_int length
)
290 if ((const u_char
*)(ap
+ 1) > ndo
->ndo_snapend
) {
291 /* Just bail if we don't have the whole chunk. */
292 ND_PRINT("%s", tstr
);
295 if (length
< sizeof(*ap
)) {
296 ND_PRINT(" [|atp %u]", length
);
299 length
-= sizeof(*ap
);
300 control
= EXTRACT_U_1(ap
->control
);
301 switch (control
& 0xc0) {
304 ND_PRINT(" atp-req%s %u",
305 control
& atpXO
? " " : "*",
306 EXTRACT_BE_U_2(ap
->transID
));
308 atp_bitmap_print(ndo
, EXTRACT_U_1(ap
->bitmap
));
311 ND_PRINT(" [len=%u]", length
);
313 switch (control
& (atpEOM
|atpSTS
)) {
321 ND_PRINT(" [EOM,STS]");
327 ND_PRINT(" atp-resp%s%u:%u (%u)",
328 control
& atpEOM
? "*" : " ",
329 EXTRACT_BE_U_2(ap
->transID
), EXTRACT_U_1(ap
->bitmap
), length
);
330 switch (control
& (atpXO
|atpSTS
)) {
338 ND_PRINT(" [XO,STS]");
344 ND_PRINT(" atp-rel %u", EXTRACT_BE_U_2(ap
->transID
));
346 atp_bitmap_print(ndo
, EXTRACT_U_1(ap
->bitmap
));
348 /* length should be zero */
350 ND_PRINT(" [len=%u]", length
);
352 /* there shouldn't be any control flags */
353 if (control
& (atpXO
|atpEOM
|atpSTS
)) {
355 if (control
& atpXO
) {
359 if (control
& atpEOM
) {
360 ND_PRINT("%cEOM", c
);
363 if (control
& atpSTS
) {
364 ND_PRINT("%cSTS", c
);
371 ND_PRINT(" atp-0x%x %u (%u)", control
,
372 EXTRACT_BE_U_2(ap
->transID
), length
);
375 data
= EXTRACT_BE_U_4(ap
->userData
);
377 ND_PRINT(" 0x%x", data
);
381 atp_bitmap_print(netdissect_options
*ndo
,
387 * The '& 0xff' below is needed for compilers that want to sign
388 * extend a u_char, which is the case with the Ultrix compiler.
389 * (gcc is smart enough to eliminate it, at least on the Sparc).
391 if ((bm
+ 1) & (bm
& 0xff)) {
393 for (i
= 0; bm
; ++i
) {
395 ND_PRINT("%c%u", c
, i
);
405 ND_PRINT("<0-%u>", i
- 1);
412 nbp_print(netdissect_options
*ndo
,
413 const struct atNBP
*np
, u_int length
, u_short snet
,
414 u_char snode
, u_char skt
)
416 const struct atNBPtuple
*tp
=
417 (const struct atNBPtuple
*)((const u_char
*)np
+ nbpHeaderSize
);
422 if (length
< nbpHeaderSize
) {
423 ND_PRINT(" truncated-nbp %u", length
);
427 length
-= nbpHeaderSize
;
429 /* must be room for at least one tuple */
430 ND_PRINT(" truncated-nbp %u", length
+ nbpHeaderSize
);
433 /* ep points to end of available data */
434 ep
= ndo
->ndo_snapend
;
435 if ((const u_char
*)tp
> ep
) {
436 ND_PRINT("%s", tstr
);
439 control
= EXTRACT_U_1(np
->control
);
440 switch (i
= (control
& 0xf0)) {
444 ND_PRINT(i
== nbpLkUp
? " nbp-lkup %u:":" nbp-brRq %u:", EXTRACT_U_1(np
->id
));
445 if ((const u_char
*)(tp
+ 1) > ep
) {
446 ND_PRINT("%s", tstr
);
449 (void)nbp_name_print(ndo
, tp
, ep
);
451 * look for anomalies: the spec says there can only
452 * be one tuple, the address must match the source
453 * address and the enumerator should be zero.
455 if ((control
& 0xf) != 1)
456 ND_PRINT(" [ntup=%u]", control
& 0xf);
457 if (EXTRACT_U_1(tp
->enumerator
))
458 ND_PRINT(" [enum=%u]", EXTRACT_U_1(tp
->enumerator
));
459 if (EXTRACT_BE_U_2(tp
->net
) != snet
||
460 EXTRACT_U_1(tp
->node
) != snode
||
461 EXTRACT_U_1(tp
->skt
) != skt
)
462 ND_PRINT(" [addr=%s.%u]",
463 ataddr_string(ndo
, EXTRACT_BE_U_2(tp
->net
),
464 EXTRACT_U_1(tp
->node
)),
465 EXTRACT_U_1(tp
->skt
));
469 ND_PRINT(" nbp-reply %u:", EXTRACT_U_1(np
->id
));
471 /* print each of the tuples in the reply */
472 for (i
= control
& 0xf; i
!= 0 && tp
; i
--)
473 tp
= nbp_tuple_print(ndo
, tp
, ep
, snet
, snode
, skt
);
477 ND_PRINT(" nbp-0x%x %u (%u)", control
, EXTRACT_U_1(np
->id
), length
);
482 /* print a counted string */
484 print_cstring(netdissect_options
*ndo
,
485 const char *cp
, const u_char
*ep
)
489 if (cp
>= (const char *)ep
) {
490 ND_PRINT("%s", tstr
);
493 length
= EXTRACT_U_1(cp
);
496 /* Spec says string can be at most 32 bytes long */
498 ND_PRINT("[len=%u]", length
);
501 while (length
!= 0) {
502 if (cp
>= (const char *)ep
) {
503 ND_PRINT("%s", tstr
);
506 fn_print_char(ndo
, EXTRACT_U_1(cp
));
513 static const struct atNBPtuple
*
514 nbp_tuple_print(netdissect_options
*ndo
,
515 const struct atNBPtuple
*tp
, const u_char
*ep
,
516 u_short snet
, u_char snode
, u_char skt
)
518 const struct atNBPtuple
*tpn
;
520 if ((const u_char
*)(tp
+ 1) > ep
) {
521 ND_PRINT("%s", tstr
);
524 tpn
= nbp_name_print(ndo
, tp
, ep
);
526 /* if the enumerator isn't 1, print it */
527 if (EXTRACT_U_1(tp
->enumerator
) != 1)
528 ND_PRINT("(%u)", EXTRACT_U_1(tp
->enumerator
));
530 /* if the socket doesn't match the src socket, print it */
531 if (EXTRACT_U_1(tp
->skt
) != skt
)
532 ND_PRINT(" %u", EXTRACT_U_1(tp
->skt
));
534 /* if the address doesn't match the src address, it's an anomaly */
535 if (EXTRACT_BE_U_2(tp
->net
) != snet
||
536 EXTRACT_U_1(tp
->node
) != snode
)
537 ND_PRINT(" [addr=%s]",
538 ataddr_string(ndo
, EXTRACT_BE_U_2(tp
->net
), EXTRACT_U_1(tp
->node
)));
543 static const struct atNBPtuple
*
544 nbp_name_print(netdissect_options
*ndo
,
545 const struct atNBPtuple
*tp
, const u_char
*ep
)
547 const char *cp
= (const char *)tp
+ nbpTupleSize
;
553 if ((cp
= print_cstring(ndo
, cp
, ep
)) != NULL
) {
556 if ((cp
= print_cstring(ndo
, cp
, ep
)) != NULL
) {
559 if ((cp
= print_cstring(ndo
, cp
, ep
)) != NULL
)
563 return ((const struct atNBPtuple
*)cp
);
567 #define HASHNAMESIZE 4096
572 struct hnamemem
*nxt
;
575 static struct hnamemem hnametable
[HASHNAMESIZE
];
578 ataddr_string(netdissect_options
*ndo
,
579 u_short atnet
, u_char athost
)
581 struct hnamemem
*tp
, *tp2
;
582 u_int i
= (atnet
<< 8) | athost
;
584 static int first
= 1;
588 * if this is the first call, see if there's an AppleTalk
589 * number to name map file.
591 if (first
&& (first
= 0, !ndo
->ndo_nflag
)
592 && (fp
= fopen("/etc/atalk.names", "r"))) {
596 while (fgets(line
, sizeof(line
), fp
)) {
597 if (line
[0] == '\n' || line
[0] == 0 || line
[0] == '#')
599 if (sscanf(line
, "%u.%u %256s", &i1
, &i2
, nambuf
) == 3)
600 /* got a hostname. */
602 else if (sscanf(line
, "%u %256s", &i1
, nambuf
) == 2)
604 i2
= (i1
<< 8) | 255;
608 for (tp
= &hnametable
[i2
& (HASHNAMESIZE
-1)];
609 tp
->nxt
; tp
= tp
->nxt
)
612 tp
->nxt
= newhnamemem(ndo
);
613 tp
->name
= strdup(nambuf
);
614 if (tp
->name
== NULL
)
615 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_MEM_ALLOC
,
616 "ataddr_string: strdup(nambuf)");
621 for (tp
= &hnametable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
625 /* didn't have the node name -- see if we've got the net name */
627 for (tp2
= &hnametable
[i
& (HASHNAMESIZE
-1)]; tp2
->nxt
; tp2
= tp2
->nxt
)
628 if (tp2
->addr
== i
) {
629 tp
->addr
= (atnet
<< 8) | athost
;
630 tp
->nxt
= newhnamemem(ndo
);
631 (void)nd_snprintf(nambuf
, sizeof(nambuf
), "%s.%u",
633 tp
->name
= strdup(nambuf
);
634 if (tp
->name
== NULL
)
635 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_MEM_ALLOC
,
636 "ataddr_string: strdup(nambuf)");
640 tp
->addr
= (atnet
<< 8) | athost
;
641 tp
->nxt
= newhnamemem(ndo
);
643 (void)nd_snprintf(nambuf
, sizeof(nambuf
), "%u.%u", atnet
, athost
);
645 (void)nd_snprintf(nambuf
, sizeof(nambuf
), "%u", atnet
);
646 tp
->name
= strdup(nambuf
);
647 if (tp
->name
== NULL
)
648 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_MEM_ALLOC
,
649 "ataddr_string: strdup(nambuf)");
654 static const struct tok skt2str
[] = {
655 { rtmpSkt
, "rtmp" }, /* routing table maintenance */
656 { nbpSkt
, "nis" }, /* name info socket */
657 { echoSkt
, "echo" }, /* AppleTalk echo protocol */
658 { zipSkt
, "zip" }, /* zone info protocol */
663 ddpskt_string(netdissect_options
*ndo
,
668 if (ndo
->ndo_nflag
) {
669 (void)nd_snprintf(buf
, sizeof(buf
), "%u", skt
);
672 return (tok2str(skt2str
, "%u", skt
));