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