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