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