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