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