]> The Tcpdump Group git mirrors - tcpdump/blob - print-atalk.c
The "__attribute__((packed))" tag on structures causes some files not to
[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.77 2002-12-11 07:13:57 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 _U_, 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 ddp_print(bp, length, dp->type, snet, dp->srcNode, dp->srcSkt);
193 }
194
195 /* XXX should probably pass in the snap header and do checks like arp_print() */
196 void
197 aarp_print(register const u_char *bp, u_int length)
198 {
199 register const struct aarp *ap;
200
201 #define AT(member) ataddr_string((ap->member[1]<<8)|ap->member[2],ap->member[3])
202
203 printf("aarp ");
204 ap = (const struct aarp *)bp;
205 if (EXTRACT_16BITS(&ap->htype) == 1 &&
206 EXTRACT_16BITS(&ap->ptype) == ETHERTYPE_ATALK &&
207 ap->halen == 6 && ap->palen == 4 )
208 switch (EXTRACT_16BITS(&ap->op)) {
209
210 case 1: /* request */
211 (void)printf("who-has %s tell %s",
212 AT(pdaddr), AT(psaddr));
213 return;
214
215 case 2: /* response */
216 (void)printf("reply %s is-at %s",
217 AT(pdaddr), etheraddr_string(ap->hdaddr));
218 return;
219
220 case 3: /* probe (oy!) */
221 (void)printf("probe %s tell %s",
222 AT(pdaddr), AT(psaddr));
223 return;
224 }
225 (void)printf("len %u op %u htype %u ptype %#x halen %u palen %u",
226 length, EXTRACT_16BITS(&ap->op), EXTRACT_16BITS(&ap->htype),
227 EXTRACT_16BITS(&ap->ptype), ap->halen, ap->palen);
228 }
229
230 /*
231 * Print AppleTalk Datagram Delivery Protocol packets.
232 */
233 static void
234 ddp_print(register const u_char *bp, register u_int length, register int t,
235 register u_short snet, register u_char snode, u_char skt)
236 {
237
238 switch (t) {
239
240 case ddpNBP:
241 nbp_print((const struct atNBP *)bp, length, snet, snode, skt);
242 break;
243
244 case ddpATP:
245 atp_print((const struct atATP *)bp, length);
246 break;
247
248 default:
249 (void)printf(" at-%s %d", tok2str(type2str, NULL, t), length);
250 break;
251 }
252 }
253
254 static void
255 atp_print(register const struct atATP *ap, u_int length)
256 {
257 char c;
258 u_int32_t data;
259
260 if ((const u_char *)(ap + 1) > snapend) {
261 /* Just bail if we don't have the whole chunk. */
262 fputs(tstr, stdout);
263 return;
264 }
265 length -= sizeof(*ap);
266 switch (ap->control & 0xc0) {
267
268 case atpReqCode:
269 (void)printf(" atp-req%s %d",
270 ap->control & atpXO? " " : "*",
271 EXTRACT_16BITS(&ap->transID));
272
273 atp_bitmap_print(ap->bitmap);
274
275 if (length != 0)
276 (void)printf(" [len=%d]", length);
277
278 switch (ap->control & (atpEOM|atpSTS)) {
279 case atpEOM:
280 (void)printf(" [EOM]");
281 break;
282 case atpSTS:
283 (void)printf(" [STS]");
284 break;
285 case atpEOM|atpSTS:
286 (void)printf(" [EOM,STS]");
287 break;
288 }
289 break;
290
291 case atpRspCode:
292 (void)printf(" atp-resp%s%d:%d (%d)",
293 ap->control & atpEOM? "*" : " ",
294 EXTRACT_16BITS(&ap->transID), ap->bitmap, length);
295 switch (ap->control & (atpXO|atpSTS)) {
296 case atpXO:
297 (void)printf(" [XO]");
298 break;
299 case atpSTS:
300 (void)printf(" [STS]");
301 break;
302 case atpXO|atpSTS:
303 (void)printf(" [XO,STS]");
304 break;
305 }
306 break;
307
308 case atpRelCode:
309 (void)printf(" atp-rel %d", EXTRACT_16BITS(&ap->transID));
310
311 atp_bitmap_print(ap->bitmap);
312
313 /* length should be zero */
314 if (length)
315 (void)printf(" [len=%d]", length);
316
317 /* there shouldn't be any control flags */
318 if (ap->control & (atpXO|atpEOM|atpSTS)) {
319 c = '[';
320 if (ap->control & atpXO) {
321 (void)printf("%cXO", c);
322 c = ',';
323 }
324 if (ap->control & atpEOM) {
325 (void)printf("%cEOM", c);
326 c = ',';
327 }
328 if (ap->control & atpSTS) {
329 (void)printf("%cSTS", c);
330 c = ',';
331 }
332 (void)printf("]");
333 }
334 break;
335
336 default:
337 (void)printf(" atp-0x%x %d (%d)", ap->control,
338 EXTRACT_16BITS(&ap->transID), length);
339 break;
340 }
341 data = EXTRACT_32BITS(&ap->userData);
342 if (data != 0)
343 (void)printf(" 0x%x", data);
344 }
345
346 static void
347 atp_bitmap_print(register u_char bm)
348 {
349 register char c;
350 register int i;
351
352 /*
353 * The '& 0xff' below is needed for compilers that want to sign
354 * extend a u_char, which is the case with the Ultrix compiler.
355 * (gcc is smart enough to eliminate it, at least on the Sparc).
356 */
357 if ((bm + 1) & (bm & 0xff)) {
358 c = '<';
359 for (i = 0; bm; ++i) {
360 if (bm & 1) {
361 (void)printf("%c%d", c, i);
362 c = ',';
363 }
364 bm >>= 1;
365 }
366 (void)printf(">");
367 } else {
368 for (i = 0; bm; ++i)
369 bm >>= 1;
370 if (i > 1)
371 (void)printf("<0-%d>", i - 1);
372 else
373 (void)printf("<0>");
374 }
375 }
376
377 static void
378 nbp_print(register const struct atNBP *np, u_int length, register u_short snet,
379 register u_char snode, register u_char skt)
380 {
381 register const struct atNBPtuple *tp =
382 (const struct atNBPtuple *)((u_char *)np + nbpHeaderSize);
383 int i;
384 const u_char *ep;
385
386 if (length < nbpHeaderSize) {
387 (void)printf(" truncated-nbp %d", length);
388 return;
389 }
390
391 length -= nbpHeaderSize;
392 if (length < 8) {
393 /* must be room for at least one tuple */
394 (void)printf(" truncated-nbp %d", length + nbpHeaderSize);
395 return;
396 }
397 /* ep points to end of available data */
398 ep = snapend;
399 if ((const u_char *)tp > ep) {
400 fputs(tstr, stdout);
401 return;
402 }
403 switch (i = np->control & 0xf0) {
404
405 case nbpBrRq:
406 case nbpLkUp:
407 (void)printf(i == nbpLkUp? " nbp-lkup %d:":" nbp-brRq %d:",
408 np->id);
409 if ((const u_char *)(tp + 1) > ep) {
410 fputs(tstr, stdout);
411 return;
412 }
413 (void)nbp_name_print(tp, ep);
414 /*
415 * look for anomalies: the spec says there can only
416 * be one tuple, the address must match the source
417 * address and the enumerator should be zero.
418 */
419 if ((np->control & 0xf) != 1)
420 (void)printf(" [ntup=%d]", np->control & 0xf);
421 if (tp->enumerator)
422 (void)printf(" [enum=%d]", tp->enumerator);
423 if (EXTRACT_16BITS(&tp->net) != snet ||
424 tp->node != snode || tp->skt != skt)
425 (void)printf(" [addr=%s.%d]",
426 ataddr_string(EXTRACT_16BITS(&tp->net),
427 tp->node), tp->skt);
428 break;
429
430 case nbpLkUpReply:
431 (void)printf(" nbp-reply %d:", np->id);
432
433 /* print each of the tuples in the reply */
434 for (i = np->control & 0xf; --i >= 0 && tp; )
435 tp = nbp_tuple_print(tp, ep, snet, snode, skt);
436 break;
437
438 default:
439 (void)printf(" nbp-0x%x %d (%d)", np->control, np->id,
440 length);
441 break;
442 }
443 }
444
445 /* print a counted string */
446 static const char *
447 print_cstring(register const char *cp, register const u_char *ep)
448 {
449 register u_int length;
450
451 if (cp >= (const char *)ep) {
452 fputs(tstr, stdout);
453 return (0);
454 }
455 length = *cp++;
456
457 /* Spec says string can be at most 32 bytes long */
458 if (length > 32) {
459 (void)printf("[len=%u]", length);
460 return (0);
461 }
462 while ((int)--length >= 0) {
463 if (cp >= (const char *)ep) {
464 fputs(tstr, stdout);
465 return (0);
466 }
467 putchar(*cp++);
468 }
469 return (cp);
470 }
471
472 static const struct atNBPtuple *
473 nbp_tuple_print(register const struct atNBPtuple *tp,
474 register const u_char *ep,
475 register u_short snet, register u_char snode,
476 register u_char skt)
477 {
478 register const struct atNBPtuple *tpn;
479
480 if ((const u_char *)(tp + 1) > ep) {
481 fputs(tstr, stdout);
482 return 0;
483 }
484 tpn = nbp_name_print(tp, ep);
485
486 /* if the enumerator isn't 1, print it */
487 if (tp->enumerator != 1)
488 (void)printf("(%d)", tp->enumerator);
489
490 /* if the socket doesn't match the src socket, print it */
491 if (tp->skt != skt)
492 (void)printf(" %d", tp->skt);
493
494 /* if the address doesn't match the src address, it's an anomaly */
495 if (EXTRACT_16BITS(&tp->net) != snet || tp->node != snode)
496 (void)printf(" [addr=%s]",
497 ataddr_string(EXTRACT_16BITS(&tp->net), tp->node));
498
499 return (tpn);
500 }
501
502 static const struct atNBPtuple *
503 nbp_name_print(const struct atNBPtuple *tp, register const u_char *ep)
504 {
505 register const char *cp = (const char *)tp + nbpTupleSize;
506
507 putchar(' ');
508
509 /* Object */
510 putchar('"');
511 if ((cp = print_cstring(cp, ep)) != NULL) {
512 /* Type */
513 putchar(':');
514 if ((cp = print_cstring(cp, ep)) != NULL) {
515 /* Zone */
516 putchar('@');
517 if ((cp = print_cstring(cp, ep)) != NULL)
518 putchar('"');
519 }
520 }
521 return ((const struct atNBPtuple *)cp);
522 }
523
524
525 #define HASHNAMESIZE 4096
526
527 struct hnamemem {
528 int addr;
529 char *name;
530 struct hnamemem *nxt;
531 };
532
533 static struct hnamemem hnametable[HASHNAMESIZE];
534
535 static const char *
536 ataddr_string(u_short atnet, u_char athost)
537 {
538 register struct hnamemem *tp, *tp2;
539 register int i = (atnet << 8) | athost;
540 char nambuf[MAXHOSTNAMELEN + 20];
541 static int first = 1;
542 FILE *fp;
543
544 /*
545 * if this is the first call, see if there's an AppleTalk
546 * number to name map file.
547 */
548 if (first && (first = 0, !nflag)
549 && (fp = fopen("/etc/atalk.names", "r"))) {
550 char line[256];
551 int i1, i2, i3;
552
553 while (fgets(line, sizeof(line), fp)) {
554 if (line[0] == '\n' || line[0] == 0 || line[0] == '#')
555 continue;
556 if (sscanf(line, "%d.%d.%d %256s", &i1, &i2, &i3,
557 nambuf) == 4)
558 /* got a hostname. */
559 i3 |= ((i1 << 8) | i2) << 8;
560 else if (sscanf(line, "%d.%d %256s", &i1, &i2,
561 nambuf) == 3)
562 /* got a net name */
563 i3 = (((i1 << 8) | i2) << 8) | 255;
564 else
565 continue;
566
567 for (tp = &hnametable[i3 & (HASHNAMESIZE-1)];
568 tp->nxt; tp = tp->nxt)
569 ;
570 tp->addr = i3;
571 tp->nxt = newhnamemem();
572 tp->name = strdup(nambuf);
573 }
574 fclose(fp);
575 }
576
577 for (tp = &hnametable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
578 if (tp->addr == i)
579 return (tp->name);
580
581 /* didn't have the node name -- see if we've got the net name */
582 i |= 255;
583 for (tp2 = &hnametable[i & (HASHNAMESIZE-1)]; tp2->nxt; tp2 = tp2->nxt)
584 if (tp2->addr == i) {
585 tp->addr = (atnet << 8) | athost;
586 tp->nxt = newhnamemem();
587 (void)snprintf(nambuf, sizeof(nambuf), "%s.%d",
588 tp2->name, athost);
589 tp->name = strdup(nambuf);
590 return (tp->name);
591 }
592
593 tp->addr = (atnet << 8) | athost;
594 tp->nxt = newhnamemem();
595 if (athost != 255)
596 (void)snprintf(nambuf, sizeof(nambuf), "%d.%d.%d",
597 atnet >> 8, atnet & 0xff, athost);
598 else
599 (void)snprintf(nambuf, sizeof(nambuf), "%d.%d", atnet >> 8,
600 atnet & 0xff);
601 tp->name = strdup(nambuf);
602
603 return (tp->name);
604 }
605
606 static struct tok skt2str[] = {
607 { rtmpSkt, "rtmp" }, /* routing table maintenance */
608 { nbpSkt, "nis" }, /* name info socket */
609 { echoSkt, "echo" }, /* AppleTalk echo protocol */
610 { zipSkt, "zip" }, /* zone info protocol */
611 { 0, NULL }
612 };
613
614 static const char *
615 ddpskt_string(register int skt)
616 {
617 static char buf[8];
618
619 if (nflag) {
620 (void)snprintf(buf, sizeof(buf), "%d", skt);
621 return (buf);
622 }
623 return (tok2str(skt2str, "%d", skt));
624 }