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