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