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