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