]> The Tcpdump Group git mirrors - tcpdump/blob - print-atalk.c
a94c29ce25fd3f7f573ded68f2644e3dabe76476
[tcpdump] / print-atalk.c
1 /*
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
4 *
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
16 * written permission.
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.
20 */
21
22 /* \summary: AppleTalk printer */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <netdissect-stdinc.h>
29
30 #include <stdio.h>
31 #include <string.h>
32
33 #include "netdissect.h"
34 #include "addrtoname.h"
35 #include "ethertype.h"
36 #include "extract.h"
37 #include "appletalk.h"
38
39 static const char tstr[] = "[|atalk]";
40
41 static const struct tok type2str[] = {
42 { ddpRTMP, "rtmp" },
43 { ddpRTMPrequest, "rtmpReq" },
44 { ddpECHO, "echo" },
45 { ddpIP, "IP" },
46 { ddpARP, "ARP" },
47 { ddpKLAP, "KLAP" },
48 { 0, NULL }
49 };
50
51 struct aarp {
52 uint16_t htype, ptype;
53 uint8_t halen, palen;
54 uint16_t op;
55 uint8_t hsaddr[6];
56 uint8_t psaddr[4];
57 uint8_t hdaddr[6];
58 uint8_t pdaddr[4];
59 };
60
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 *,
65 const u_char *,
66 u_short, u_char, u_char);
67 static const struct atNBPtuple *nbp_name_print(netdissect_options *, const struct atNBPtuple *,
68 const u_char *);
69 static const char *ataddr_string(netdissect_options *, u_short, u_char);
70 static void ddp_print(netdissect_options *, const u_char *, u_int, int, u_short, u_char, u_char);
71 static const char *ddpskt_string(netdissect_options *, int);
72
73 /*
74 * Print LLAP packets received on a physical LocalTalk interface.
75 */
76 u_int
77 ltalk_if_print(netdissect_options *ndo,
78 const struct pcap_pkthdr *h, const u_char *p)
79 {
80 u_int hdrlen;
81
82 hdrlen = llap_print(ndo, p, h->caplen);
83 if (hdrlen == 0) {
84 /* Cut short by the snapshot length. */
85 return (h->caplen);
86 }
87 return (hdrlen);
88 }
89
90 /*
91 * Print AppleTalk LLAP packets.
92 */
93 u_int
94 llap_print(netdissect_options *ndo,
95 register const u_char *bp, u_int length)
96 {
97 register const struct LAP *lp;
98 register const struct atDDP *dp;
99 register const struct atShortDDP *sdp;
100 u_short snet;
101 u_int hdrlen;
102
103 if (length < sizeof(*lp)) {
104 ND_PRINT((ndo, " [|llap %u]", length));
105 return (length);
106 }
107 if (!ND_TTEST2(*bp, sizeof(*lp))) {
108 ND_PRINT((ndo, " [|llap]"));
109 return (0); /* cut short by the snapshot length */
110 }
111 lp = (const struct LAP *)bp;
112 bp += sizeof(*lp);
113 length -= sizeof(*lp);
114 hdrlen = sizeof(*lp);
115 switch (lp->type) {
116
117 case lapShortDDP:
118 if (length < ddpSSize) {
119 ND_PRINT((ndo, " [|sddp %u]", length));
120 return (length);
121 }
122 if (!ND_TTEST2(*bp, ddpSSize)) {
123 ND_PRINT((ndo, " [|sddp]"));
124 return (0); /* cut short by the snapshot length */
125 }
126 sdp = (const struct atShortDDP *)bp;
127 ND_PRINT((ndo, "%s.%s",
128 ataddr_string(ndo, 0, lp->src), ddpskt_string(ndo, sdp->srcSkt)));
129 ND_PRINT((ndo, " > %s.%s:",
130 ataddr_string(ndo, 0, lp->dst), ddpskt_string(ndo, sdp->dstSkt)));
131 bp += ddpSSize;
132 length -= ddpSSize;
133 hdrlen += ddpSSize;
134 ddp_print(ndo, bp, length, sdp->type, 0, lp->src, sdp->srcSkt);
135 break;
136
137 case lapDDP:
138 if (length < ddpSize) {
139 ND_PRINT((ndo, " [|ddp %u]", length));
140 return (length);
141 }
142 if (!ND_TTEST2(*bp, ddpSize)) {
143 ND_PRINT((ndo, " [|ddp]"));
144 return (0); /* cut short by the snapshot length */
145 }
146 dp = (const struct atDDP *)bp;
147 snet = EXTRACT_16BITS(&dp->srcNet);
148 ND_PRINT((ndo, "%s.%s", ataddr_string(ndo, snet, dp->srcNode),
149 ddpskt_string(ndo, dp->srcSkt)));
150 ND_PRINT((ndo, " > %s.%s:",
151 ataddr_string(ndo, EXTRACT_16BITS(&dp->dstNet), dp->dstNode),
152 ddpskt_string(ndo, dp->dstSkt)));
153 bp += ddpSize;
154 length -= ddpSize;
155 hdrlen += ddpSize;
156 ddp_print(ndo, bp, length, dp->type, snet, dp->srcNode, dp->srcSkt);
157 break;
158
159 #ifdef notdef
160 case lapKLAP:
161 klap_print(bp, length);
162 break;
163 #endif
164
165 default:
166 ND_PRINT((ndo, "%d > %d at-lap#%d %u",
167 lp->src, lp->dst, lp->type, length));
168 break;
169 }
170 return (hdrlen);
171 }
172
173 /*
174 * Print EtherTalk/TokenTalk packets (or FDDITalk, or whatever it's called
175 * when it runs over FDDI; yes, I've seen FDDI captures with AppleTalk
176 * packets in them).
177 */
178 void
179 atalk_print(netdissect_options *ndo,
180 register const u_char *bp, u_int length)
181 {
182 register const struct atDDP *dp;
183 u_short snet;
184
185 if(!ndo->ndo_eflag)
186 ND_PRINT((ndo, "AT "));
187
188 if (length < ddpSize) {
189 ND_PRINT((ndo, " [|ddp %u]", length));
190 return;
191 }
192 if (!ND_TTEST2(*bp, ddpSize)) {
193 ND_PRINT((ndo, " [|ddp]"));
194 return;
195 }
196 dp = (const struct atDDP *)bp;
197 snet = EXTRACT_16BITS(&dp->srcNet);
198 ND_PRINT((ndo, "%s.%s", ataddr_string(ndo, snet, dp->srcNode),
199 ddpskt_string(ndo, dp->srcSkt)));
200 ND_PRINT((ndo, " > %s.%s: ",
201 ataddr_string(ndo, EXTRACT_16BITS(&dp->dstNet), dp->dstNode),
202 ddpskt_string(ndo, dp->dstSkt)));
203 bp += ddpSize;
204 length -= ddpSize;
205 ddp_print(ndo, bp, length, dp->type, snet, dp->srcNode, dp->srcSkt);
206 }
207
208 /* XXX should probably pass in the snap header and do checks like arp_print() */
209 void
210 aarp_print(netdissect_options *ndo,
211 register const u_char *bp, u_int length)
212 {
213 register const struct aarp *ap;
214
215 #define AT(member) ataddr_string(ndo, (ap->member[1]<<8)|ap->member[2],ap->member[3])
216
217 ND_PRINT((ndo, "aarp "));
218 ap = (const struct aarp *)bp;
219 if (EXTRACT_16BITS(&ap->htype) == 1 &&
220 EXTRACT_16BITS(&ap->ptype) == ETHERTYPE_ATALK &&
221 ap->halen == 6 && ap->palen == 4 )
222 switch (EXTRACT_16BITS(&ap->op)) {
223
224 case 1: /* request */
225 ND_PRINT((ndo, "who-has %s tell %s", AT(pdaddr), AT(psaddr)));
226 return;
227
228 case 2: /* response */
229 ND_PRINT((ndo, "reply %s is-at %s", AT(psaddr), etheraddr_string(ndo, ap->hsaddr)));
230 return;
231
232 case 3: /* probe (oy!) */
233 ND_PRINT((ndo, "probe %s tell %s", AT(pdaddr), AT(psaddr)));
234 return;
235 }
236 ND_PRINT((ndo, "len %u op %u htype %u ptype %#x halen %u palen %u",
237 length, EXTRACT_16BITS(&ap->op), EXTRACT_16BITS(&ap->htype),
238 EXTRACT_16BITS(&ap->ptype), ap->halen, ap->palen));
239 }
240
241 /*
242 * Print AppleTalk Datagram Delivery Protocol packets.
243 */
244 static void
245 ddp_print(netdissect_options *ndo,
246 register const u_char *bp, register u_int length, register int t,
247 register u_short snet, register u_char snode, u_char skt)
248 {
249
250 switch (t) {
251
252 case ddpNBP:
253 nbp_print(ndo, (const struct atNBP *)bp, length, snet, snode, skt);
254 break;
255
256 case ddpATP:
257 atp_print(ndo, (const struct atATP *)bp, length);
258 break;
259
260 case ddpEIGRP:
261 eigrp_print(ndo, bp, length);
262 break;
263
264 default:
265 ND_PRINT((ndo, " at-%s %d", tok2str(type2str, NULL, t), length));
266 break;
267 }
268 }
269
270 static void
271 atp_print(netdissect_options *ndo,
272 register const struct atATP *ap, u_int length)
273 {
274 char c;
275 uint32_t data;
276
277 if ((const u_char *)(ap + 1) > ndo->ndo_snapend) {
278 /* Just bail if we don't have the whole chunk. */
279 ND_PRINT((ndo, "%s", tstr));
280 return;
281 }
282 if (length < sizeof(*ap)) {
283 ND_PRINT((ndo, " [|atp %u]", length));
284 return;
285 }
286 length -= sizeof(*ap);
287 switch (ap->control & 0xc0) {
288
289 case atpReqCode:
290 ND_PRINT((ndo, " atp-req%s %d",
291 ap->control & atpXO? " " : "*",
292 EXTRACT_16BITS(&ap->transID)));
293
294 atp_bitmap_print(ndo, ap->bitmap);
295
296 if (length != 0)
297 ND_PRINT((ndo, " [len=%u]", length));
298
299 switch (ap->control & (atpEOM|atpSTS)) {
300 case atpEOM:
301 ND_PRINT((ndo, " [EOM]"));
302 break;
303 case atpSTS:
304 ND_PRINT((ndo, " [STS]"));
305 break;
306 case atpEOM|atpSTS:
307 ND_PRINT((ndo, " [EOM,STS]"));
308 break;
309 }
310 break;
311
312 case atpRspCode:
313 ND_PRINT((ndo, " atp-resp%s%d:%d (%u)",
314 ap->control & atpEOM? "*" : " ",
315 EXTRACT_16BITS(&ap->transID), ap->bitmap, length));
316 switch (ap->control & (atpXO|atpSTS)) {
317 case atpXO:
318 ND_PRINT((ndo, " [XO]"));
319 break;
320 case atpSTS:
321 ND_PRINT((ndo, " [STS]"));
322 break;
323 case atpXO|atpSTS:
324 ND_PRINT((ndo, " [XO,STS]"));
325 break;
326 }
327 break;
328
329 case atpRelCode:
330 ND_PRINT((ndo, " atp-rel %d", EXTRACT_16BITS(&ap->transID)));
331
332 atp_bitmap_print(ndo, ap->bitmap);
333
334 /* length should be zero */
335 if (length)
336 ND_PRINT((ndo, " [len=%u]", length));
337
338 /* there shouldn't be any control flags */
339 if (ap->control & (atpXO|atpEOM|atpSTS)) {
340 c = '[';
341 if (ap->control & atpXO) {
342 ND_PRINT((ndo, "%cXO", c));
343 c = ',';
344 }
345 if (ap->control & atpEOM) {
346 ND_PRINT((ndo, "%cEOM", c));
347 c = ',';
348 }
349 if (ap->control & atpSTS) {
350 ND_PRINT((ndo, "%cSTS", c));
351 c = ',';
352 }
353 ND_PRINT((ndo, "]"));
354 }
355 break;
356
357 default:
358 ND_PRINT((ndo, " atp-0x%x %d (%u)", ap->control,
359 EXTRACT_16BITS(&ap->transID), length));
360 break;
361 }
362 data = EXTRACT_32BITS(&ap->userData);
363 if (data != 0)
364 ND_PRINT((ndo, " 0x%x", data));
365 }
366
367 static void
368 atp_bitmap_print(netdissect_options *ndo,
369 register u_char bm)
370 {
371 register char c;
372 register int i;
373
374 /*
375 * The '& 0xff' below is needed for compilers that want to sign
376 * extend a u_char, which is the case with the Ultrix compiler.
377 * (gcc is smart enough to eliminate it, at least on the Sparc).
378 */
379 if ((bm + 1) & (bm & 0xff)) {
380 c = '<';
381 for (i = 0; bm; ++i) {
382 if (bm & 1) {
383 ND_PRINT((ndo, "%c%d", c, i));
384 c = ',';
385 }
386 bm >>= 1;
387 }
388 ND_PRINT((ndo, ">"));
389 } else {
390 for (i = 0; bm; ++i)
391 bm >>= 1;
392 if (i > 1)
393 ND_PRINT((ndo, "<0-%d>", i - 1));
394 else
395 ND_PRINT((ndo, "<0>"));
396 }
397 }
398
399 static void
400 nbp_print(netdissect_options *ndo,
401 register const struct atNBP *np, u_int length, register u_short snet,
402 register u_char snode, register u_char skt)
403 {
404 register const struct atNBPtuple *tp =
405 (const struct atNBPtuple *)((const u_char *)np + nbpHeaderSize);
406 int i;
407 const u_char *ep;
408
409 if (length < nbpHeaderSize) {
410 ND_PRINT((ndo, " truncated-nbp %u", length));
411 return;
412 }
413
414 length -= nbpHeaderSize;
415 if (length < 8) {
416 /* must be room for at least one tuple */
417 ND_PRINT((ndo, " truncated-nbp %u", length + nbpHeaderSize));
418 return;
419 }
420 /* ep points to end of available data */
421 ep = ndo->ndo_snapend;
422 if ((const u_char *)tp > ep) {
423 ND_PRINT((ndo, "%s", tstr));
424 return;
425 }
426 switch (i = np->control & 0xf0) {
427
428 case nbpBrRq:
429 case nbpLkUp:
430 ND_PRINT((ndo, i == nbpLkUp? " nbp-lkup %d:":" nbp-brRq %d:", np->id));
431 if ((const u_char *)(tp + 1) > ep) {
432 ND_PRINT((ndo, "%s", tstr));
433 return;
434 }
435 (void)nbp_name_print(ndo, tp, ep);
436 /*
437 * look for anomalies: the spec says there can only
438 * be one tuple, the address must match the source
439 * address and the enumerator should be zero.
440 */
441 if ((np->control & 0xf) != 1)
442 ND_PRINT((ndo, " [ntup=%d]", np->control & 0xf));
443 if (tp->enumerator)
444 ND_PRINT((ndo, " [enum=%d]", tp->enumerator));
445 if (EXTRACT_16BITS(&tp->net) != snet ||
446 tp->node != snode || tp->skt != skt)
447 ND_PRINT((ndo, " [addr=%s.%d]",
448 ataddr_string(ndo, EXTRACT_16BITS(&tp->net),
449 tp->node), tp->skt));
450 break;
451
452 case nbpLkUpReply:
453 ND_PRINT((ndo, " nbp-reply %d:", np->id));
454
455 /* print each of the tuples in the reply */
456 for (i = np->control & 0xf; --i >= 0 && tp; )
457 tp = nbp_tuple_print(ndo, tp, ep, snet, snode, skt);
458 break;
459
460 default:
461 ND_PRINT((ndo, " nbp-0x%x %d (%u)", np->control, np->id, length));
462 break;
463 }
464 }
465
466 /* print a counted string */
467 static const char *
468 print_cstring(netdissect_options *ndo,
469 register const char *cp, register const u_char *ep)
470 {
471 register u_int length;
472
473 if (cp >= (const char *)ep) {
474 ND_PRINT((ndo, "%s", tstr));
475 return (0);
476 }
477 length = *cp++;
478
479 /* Spec says string can be at most 32 bytes long */
480 if (length > 32) {
481 ND_PRINT((ndo, "[len=%u]", length));
482 return (0);
483 }
484 while ((int)--length >= 0) {
485 if (cp >= (const char *)ep) {
486 ND_PRINT((ndo, "%s", tstr));
487 return (0);
488 }
489 ND_PRINT((ndo, "%c", *cp++));
490 }
491 return (cp);
492 }
493
494 static const struct atNBPtuple *
495 nbp_tuple_print(netdissect_options *ndo,
496 register const struct atNBPtuple *tp, register const u_char *ep,
497 register u_short snet, register u_char snode, register u_char skt)
498 {
499 register const struct atNBPtuple *tpn;
500
501 if ((const u_char *)(tp + 1) > ep) {
502 ND_PRINT((ndo, "%s", tstr));
503 return 0;
504 }
505 tpn = nbp_name_print(ndo, tp, ep);
506
507 /* if the enumerator isn't 1, print it */
508 if (tp->enumerator != 1)
509 ND_PRINT((ndo, "(%d)", tp->enumerator));
510
511 /* if the socket doesn't match the src socket, print it */
512 if (tp->skt != skt)
513 ND_PRINT((ndo, " %d", tp->skt));
514
515 /* if the address doesn't match the src address, it's an anomaly */
516 if (EXTRACT_16BITS(&tp->net) != snet || tp->node != snode)
517 ND_PRINT((ndo, " [addr=%s]",
518 ataddr_string(ndo, EXTRACT_16BITS(&tp->net), tp->node)));
519
520 return (tpn);
521 }
522
523 static const struct atNBPtuple *
524 nbp_name_print(netdissect_options *ndo,
525 const struct atNBPtuple *tp, register const u_char *ep)
526 {
527 register const char *cp = (const char *)tp + nbpTupleSize;
528
529 ND_PRINT((ndo, " "));
530
531 /* Object */
532 ND_PRINT((ndo, "\""));
533 if ((cp = print_cstring(ndo, cp, ep)) != NULL) {
534 /* Type */
535 ND_PRINT((ndo, ":"));
536 if ((cp = print_cstring(ndo, cp, ep)) != NULL) {
537 /* Zone */
538 ND_PRINT((ndo, "@"));
539 if ((cp = print_cstring(ndo, cp, ep)) != NULL)
540 ND_PRINT((ndo, "\""));
541 }
542 }
543 return ((const struct atNBPtuple *)cp);
544 }
545
546
547 #define HASHNAMESIZE 4096
548
549 struct hnamemem {
550 int addr;
551 char *name;
552 struct hnamemem *nxt;
553 };
554
555 static struct hnamemem hnametable[HASHNAMESIZE];
556
557 static const char *
558 ataddr_string(netdissect_options *ndo,
559 u_short atnet, u_char athost)
560 {
561 register struct hnamemem *tp, *tp2;
562 register int i = (atnet << 8) | athost;
563 char nambuf[256+1];
564 static int first = 1;
565 FILE *fp;
566
567 /*
568 * if this is the first call, see if there's an AppleTalk
569 * number to name map file.
570 */
571 if (first && (first = 0, !ndo->ndo_nflag)
572 && (fp = fopen("/etc/atalk.names", "r"))) {
573 char line[256];
574 int i1, i2;
575
576 while (fgets(line, sizeof(line), fp)) {
577 if (line[0] == '\n' || line[0] == 0 || line[0] == '#')
578 continue;
579 if (sscanf(line, "%d.%d %256s", &i1, &i2, nambuf) == 3)
580 /* got a hostname. */
581 i2 |= (i1 << 8);
582 else if (sscanf(line, "%d %256s", &i1, nambuf) == 2)
583 /* got a net name */
584 i2 = (i1 << 8) | 255;
585 else
586 continue;
587
588 for (tp = &hnametable[i2 & (HASHNAMESIZE-1)];
589 tp->nxt; tp = tp->nxt)
590 ;
591 tp->addr = i2;
592 tp->nxt = newhnamemem(ndo);
593 tp->name = strdup(nambuf);
594 if (tp->name == NULL)
595 (*ndo->ndo_error)(ndo,
596 "ataddr_string: strdup(nambuf)");
597 }
598 fclose(fp);
599 }
600
601 for (tp = &hnametable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
602 if (tp->addr == i)
603 return (tp->name);
604
605 /* didn't have the node name -- see if we've got the net name */
606 i |= 255;
607 for (tp2 = &hnametable[i & (HASHNAMESIZE-1)]; tp2->nxt; tp2 = tp2->nxt)
608 if (tp2->addr == i) {
609 tp->addr = (atnet << 8) | athost;
610 tp->nxt = newhnamemem(ndo);
611 (void)snprintf(nambuf, sizeof(nambuf), "%s.%d",
612 tp2->name, athost);
613 tp->name = strdup(nambuf);
614 if (tp->name == NULL)
615 (*ndo->ndo_error)(ndo,
616 "ataddr_string: strdup(nambuf)");
617 return (tp->name);
618 }
619
620 tp->addr = (atnet << 8) | athost;
621 tp->nxt = newhnamemem(ndo);
622 if (athost != 255)
623 (void)snprintf(nambuf, sizeof(nambuf), "%d.%d", atnet, athost);
624 else
625 (void)snprintf(nambuf, sizeof(nambuf), "%d", atnet);
626 tp->name = strdup(nambuf);
627 if (tp->name == NULL)
628 (*ndo->ndo_error)(ndo, "ataddr_string: strdup(nambuf)");
629
630 return (tp->name);
631 }
632
633 static const struct tok skt2str[] = {
634 { rtmpSkt, "rtmp" }, /* routing table maintenance */
635 { nbpSkt, "nis" }, /* name info socket */
636 { echoSkt, "echo" }, /* AppleTalk echo protocol */
637 { zipSkt, "zip" }, /* zone info protocol */
638 { 0, NULL }
639 };
640
641 static const char *
642 ddpskt_string(netdissect_options *ndo,
643 register int skt)
644 {
645 static char buf[8];
646
647 if (ndo->ndo_nflag) {
648 (void)snprintf(buf, sizeof(buf), "%d", skt);
649 return (buf);
650 }
651 return (tok2str(skt2str, "%d", skt));
652 }