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