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