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