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