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