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