xref: /openbsd-src/usr.sbin/tcpdump/print-atalk.c (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1 /*	$OpenBSD: print-atalk.c,v 1.26 2009/10/27 23:59:55 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 #include <sys/param.h>
27 #include <sys/time.h>
28 #include <sys/socket.h>
29 
30 struct mbuf;
31 struct rtentry;
32 #include <net/if.h>
33 
34 #include <netinet/in.h>
35 #include <netinet/in_systm.h>
36 #include <netinet/ip.h>
37 #include <netinet/ip_var.h>
38 #include <netinet/if_ether.h>
39 #include <netinet/udp.h>
40 #include <netinet/udp_var.h>
41 #include <netinet/tcp.h>
42 #include <netinet/tcpip.h>
43 
44 #include <inttypes.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 
49 #include "interface.h"
50 #include "addrtoname.h"
51 #include "ethertype.h"
52 #include "extract.h"			/* must come after interface.h */
53 #include "appletalk.h"
54 #include "savestr.h"
55 #include "privsep.h"
56 
57 static struct tok type2str[] = {
58 	{ ddpRTMP,		"rtmp" },
59 	{ ddpRTMPrequest,	"rtmpReq" },
60 	{ ddpECHO,		"echo" },
61 	{ ddpIP,		"IP" },
62 	{ ddpARP,		"ARP" },
63 	{ ddpKLAP,		"KLAP" },
64 	{ 0,			NULL }
65 };
66 
67 struct aarp {
68 	u_int16_t	htype, ptype;
69 	u_int8_t	halen, palen;
70 	u_int16_t	op;
71 	u_int8_t	hsaddr[6];
72 	u_int8_t	psaddr[4];
73 	u_int8_t	hdaddr[6];
74 	u_int8_t	pdaddr[4];
75 };
76 
77 static char tstr[] = "[|atalk]";
78 
79 static void atp_print(const struct atATP *, u_int);
80 static void atp_bitmap_print(u_char);
81 static void nbp_print(const struct atNBP *, u_int, u_short, u_char, u_char);
82 static const char *print_cstring(const char *, const u_char *);
83 static const struct atNBPtuple *nbp_tuple_print(const struct atNBPtuple *,
84 						const u_char *,
85 						u_short, u_char, u_char);
86 static const struct atNBPtuple *nbp_name_print(const struct atNBPtuple *,
87 					       const u_char *);
88 static const char *ataddr_string(u_short, u_char);
89 static void ddp_print(const u_char *, u_int, int, u_short, u_char, u_char);
90 static const char *ddpskt_string(int);
91 
92 /*
93  * Print AppleTalk Datagram Delivery Protocol packets
94  * without the LLAP encapsulating header (i.e.
95  * from Ethertalk)
96  */
97 void
98 atalk_print(register const u_char *bp, u_int length)
99 {
100 	register const struct atDDP *dp;
101 	u_short snet;
102 
103 	if (length < ddpSize) {
104 		(void)printf(" [|ddp %d]", length);
105 		return;
106 	}
107 	dp = (const struct atDDP *)bp;
108 	snet = EXTRACT_16BITS(&dp->srcNet);
109 	printf("%s.%s", ataddr_string(snet, dp->srcNode),
110 	    ddpskt_string(dp->srcSkt));
111 	printf(" > %s.%s:",
112 	    ataddr_string(EXTRACT_16BITS(&dp->dstNet), dp->dstNode),
113 	    ddpskt_string(dp->dstSkt));
114 	bp += ddpSize;
115 	length -= ddpSize;
116 	ddp_print(bp, length, dp->type, snet, dp->srcNode, dp->srcSkt);
117 }
118 
119 /*
120  * Print AppleTalk Datagram Delivery Protocol packets
121  * from localtalk (i.e. the 230 Kbps net built into
122  * every Macintosh). We can get these from a localtalk
123  * interface if we have one, or from UDP encapsulated tunnels.
124  */
125 void
126 atalk_print_llap(register const u_char *bp, u_int length)
127 {
128 	register const struct LAP *lp;
129 	register const struct atDDP *dp;
130 	register const struct atShortDDP *sdp;
131 	u_short snet;
132 
133 	lp = (struct LAP *)bp;
134 	bp += sizeof(*lp);
135 	length -= sizeof(*lp);
136 	switch (lp->type) {
137 
138 	case lapShortDDP:
139 		if (length < ddpSSize) {
140 			(void)printf(" [|sddp %d]", length);
141 			return;
142 		}
143 		sdp = (const struct atShortDDP *)bp;
144 		printf("%s.%s",
145 		    ataddr_string(0, lp->src), ddpskt_string(sdp->srcSkt));
146 		printf(" > %s.%s:",
147 		    ataddr_string(0, lp->dst), ddpskt_string(sdp->dstSkt));
148 		bp += ddpSSize;
149 		length -= ddpSSize;
150 		ddp_print(bp, length, sdp->type, 0, lp->src, sdp->srcSkt);
151 		break;
152 
153 	case lapDDP:
154 		if (length < ddpSize) {
155 			(void)printf(" [|ddp %d]", length);
156 			return;
157 		}
158 		dp = (const struct atDDP *)bp;
159 		snet = EXTRACT_16BITS(&dp->srcNet);
160 		printf("%s.%s", ataddr_string(snet, dp->srcNode),
161 		    ddpskt_string(dp->srcSkt));
162 		printf(" > %s.%s:",
163 		    ataddr_string(EXTRACT_16BITS(&dp->dstNet), dp->dstNode),
164 		    ddpskt_string(dp->dstSkt));
165 		bp += ddpSize;
166 		length -= ddpSize;
167 		ddp_print(bp, length, dp->type, snet, dp->srcNode, dp->srcSkt);
168 		break;
169 
170 #ifdef notdef
171 	case lapKLAP:
172 		klap_print(bp, length);
173 		break;
174 #endif
175 
176 	default:
177 		printf("%d > %d at-lap#%d %d",
178 		    lp->src, lp->dst, lp->type, length);
179 		break;
180 	}
181 }
182 
183 /* XXX should probably pass in the snap header and do checks like arp_print() */
184 void
185 aarp_print(register const u_char *bp, u_int length)
186 {
187 	register const struct aarp *ap;
188 
189 #define AT(member) ataddr_string((ap->member[1]<<8)|ap->member[2],ap->member[3])
190 
191 	printf("aarp ");
192 	ap = (const struct aarp *)bp;
193 	if (ntohs(ap->htype) == 1 && ntohs(ap->ptype) == ETHERTYPE_ATALK &&
194 	    ap->halen == 6 && ap->palen == 4 )
195 		switch (ntohs(ap->op)) {
196 
197 		case 1:				/* request */
198 			(void)printf("who-has %s tell %s",
199 			    AT(pdaddr), AT(psaddr));
200 			return;
201 
202 		case 2:				/* response */
203 			(void)printf("reply %s is-at %s",
204 			    AT(pdaddr), etheraddr_string(ap->hdaddr));
205 			return;
206 
207 		case 3:				/* probe (oy!) */
208 			(void)printf("probe %s tell %s",
209 			    AT(pdaddr), AT(psaddr));
210 			return;
211 		}
212 	(void)printf("len %u op %u htype %u ptype %#x halen %u palen %u",
213 	    length, ntohs(ap->op), ntohs(ap->htype), ntohs(ap->ptype),
214 	    ap->halen, ap->palen);
215 }
216 
217 static void
218 ddp_print(register const u_char *bp, register u_int length, register int t,
219 	  register u_short snet, register u_char snode, u_char skt)
220 {
221 
222 	if ((intptr_t)bp & (sizeof(long)-1)) {
223 		static u_char *abuf = NULL;
224 		int clen = snapend - bp;
225 		if (clen > snaplen)
226 			clen = snaplen;
227 
228 		if (abuf == NULL) {
229 			abuf = (u_char *)malloc(snaplen);
230 			if (abuf == NULL)
231 				error("ddp_print: malloc");
232 		}
233 		memmove((char *)abuf, (char *)bp, min(length, clen));
234 		snapend = abuf + clen;
235 		packetp = abuf;
236 		bp = abuf;
237 	}
238 
239 	switch (t) {
240 
241 	case ddpNBP:
242 		nbp_print((const struct atNBP *)bp, length, snet, snode, skt);
243 		break;
244 
245 	case ddpATP:
246 		atp_print((const struct atATP *)bp, length);
247 		break;
248 
249 	default:
250 		(void)printf(" at-%s %d", tok2str(type2str, NULL, t), length);
251 		break;
252 	}
253 }
254 
255 static void
256 atp_print(register const struct atATP *ap, u_int length)
257 {
258 	char c;
259 	u_int32_t data;
260 
261 	if ((const u_char *)(ap + 1) > snapend) {
262 		/* Just bail if we don't have the whole chunk. */
263 		fputs(tstr, stdout);
264 		return;
265 	}
266 	length -= sizeof(*ap);
267 	switch (ap->control & 0xc0) {
268 
269 	case atpReqCode:
270 		(void)printf(" atp-req%s %d",
271 			     ap->control & atpXO? " " : "*",
272 			     EXTRACT_16BITS(&ap->transID));
273 
274 		atp_bitmap_print(ap->bitmap);
275 
276 		if (length != 0)
277 			(void)printf(" [len=%d]", length);
278 
279 		switch (ap->control & (atpEOM|atpSTS)) {
280 		case atpEOM:
281 			(void)printf(" [EOM]");
282 			break;
283 		case atpSTS:
284 			(void)printf(" [STS]");
285 			break;
286 		case atpEOM|atpSTS:
287 			(void)printf(" [EOM,STS]");
288 			break;
289 		}
290 		break;
291 
292 	case atpRspCode:
293 		(void)printf(" atp-resp%s%d:%d (%d)",
294 			     ap->control & atpEOM? "*" : " ",
295 			     EXTRACT_16BITS(&ap->transID), ap->bitmap, length);
296 		switch (ap->control & (atpXO|atpSTS)) {
297 		case atpXO:
298 			(void)printf(" [XO]");
299 			break;
300 		case atpSTS:
301 			(void)printf(" [STS]");
302 			break;
303 		case atpXO|atpSTS:
304 			(void)printf(" [XO,STS]");
305 			break;
306 		}
307 		break;
308 
309 	case atpRelCode:
310 		(void)printf(" atp-rel  %d", EXTRACT_16BITS(&ap->transID));
311 
312 		atp_bitmap_print(ap->bitmap);
313 
314 		/* length should be zero */
315 		if (length)
316 			(void)printf(" [len=%d]", length);
317 
318 		/* there shouldn't be any control flags */
319 		if (ap->control & (atpXO|atpEOM|atpSTS)) {
320 			c = '[';
321 			if (ap->control & atpXO) {
322 				(void)printf("%cXO", c);
323 				c = ',';
324 			}
325 			if (ap->control & atpEOM) {
326 				(void)printf("%cEOM", c);
327 				c = ',';
328 			}
329 			if (ap->control & atpSTS) {
330 				(void)printf("%cSTS", c);
331 				c = ',';
332 			}
333 			(void)printf("]");
334 		}
335 		break;
336 
337 	default:
338 		(void)printf(" atp-0x%x  %d (%d)", ap->control,
339 			     EXTRACT_16BITS(&ap->transID), length);
340 		break;
341 	}
342 	data = EXTRACT_32BITS(&ap->userData);
343 	if (data != 0)
344 		(void)printf(" 0x%x", data);
345 }
346 
347 static void
348 atp_bitmap_print(register u_char bm)
349 {
350 	register char c;
351 	register int i;
352 
353 	/*
354 	 * The '& 0xff' below is needed for compilers that want to sign
355 	 * extend a u_char, which is the case with the Ultrix compiler.
356 	 * (gcc is smart enough to eliminate it, at least on the Sparc).
357 	 */
358 	if ((bm + 1) & (bm & 0xff)) {
359 		c = '<';
360 		for (i = 0; bm; ++i) {
361 			if (bm & 1) {
362 				(void)printf("%c%d", c, i);
363 				c = ',';
364 			}
365 			bm >>= 1;
366 		}
367 		(void)printf(">");
368 	} else {
369 		for (i = 0; bm; ++i)
370 			bm >>= 1;
371 		if (i > 1)
372 			(void)printf("<0-%d>", i - 1);
373 		else
374 			(void)printf("<0>");
375 	}
376 }
377 
378 static void
379 nbp_print(register const struct atNBP *np, u_int length, register u_short snet,
380 	  register u_char snode, register u_char skt)
381 {
382 	register const struct atNBPtuple *tp =
383 			(struct atNBPtuple *)((u_char *)np + nbpHeaderSize);
384 	int i;
385 	const u_char *ep;
386 
387 	if (length < nbpHeaderSize) {
388 		(void)printf(" truncated-nbp %d", length);
389 		return;
390 	}
391 
392 	length -= nbpHeaderSize;
393 	if (length < 8) {
394 		/* must be room for at least one tuple */
395 		if (np->control == nbpNATLKerr) {
396 			(void)printf(" nbp-netatalk_err");
397 			return;
398 		} else if (np->control == nbpNATLKok) {
399 			(void)printf(" nbp-netatalk_ok");
400 			return;
401 		}
402 		(void)printf(" truncated-nbp nbp-0x%x  %d (%d)",
403 			np->control, np->id, length + nbpHeaderSize);
404 		return;
405 	}
406 	/* ep points to end of available data */
407 	ep = snapend;
408 	if ((const u_char *)tp > ep) {
409 		fputs(tstr, stdout);
410 		return;
411 	}
412 	switch (i = np->control & 0xf0) {
413 
414 	case nbpBrRq:
415 	case nbpLkUp:
416 		(void)printf(i == nbpLkUp? " nbp-lkup %d:":" nbp-brRq %d:",
417 			     np->id);
418 		if ((const u_char *)(tp + 1) > ep) {
419 			fputs(tstr, stdout);
420 			return;
421 		}
422 		(void)nbp_name_print(tp, ep);
423 		/*
424 		 * look for anomalies: the spec says there can only
425 		 * be one tuple, the address must match the source
426 		 * address and the enumerator should be zero.
427 		 */
428 		if ((np->control & 0xf) != 1)
429 			(void)printf(" [ntup=%d]", np->control & 0xf);
430 		if (tp->enumerator)
431 			(void)printf(" [enum=%d]", tp->enumerator);
432 		if (EXTRACT_16BITS(&tp->net) != snet ||
433 		    tp->node != snode || tp->skt != skt)
434 			(void)printf(" [addr=%s.%d]",
435 			    ataddr_string(EXTRACT_16BITS(&tp->net),
436 			    tp->node), tp->skt);
437 		break;
438 
439 	case nbpLkUpReply:
440 		(void)printf(" nbp-reply %d:", np->id);
441 
442 		/* print each of the tuples in the reply */
443 		for (i = np->control & 0xf; --i >= 0 && tp; )
444 			tp = nbp_tuple_print(tp, ep, snet, snode, skt);
445 		break;
446 
447 	case nbpNATLKrgstr:
448 	case nbpNATLKunrgstr:
449 		(void)printf((i == nbpNATLKrgstr) ?
450 			" nbp-netatalk_rgstr %d:" :
451 			" nbp-netatalk_unrgstr %d:",
452 			np->id);
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 < 0 || length > 32) {
478 		(void)printf("[len=%d]", length);
479 		return (0);
480 	}
481 	while ((int)--length >= 0) {
482 		if (cp >= (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 /*
555  * see if there's an AppleTalk number to name map file.
556  */
557 static void
558 init_atalk(void)
559 {
560 	struct hnamemem *tp;
561 	char nambuf[MAXHOSTNAMELEN + 20];
562 	char line[BUFSIZ];
563 	int i1, i2, i3;
564 
565 	priv_getlines(FTAB_APPLETALK);
566 	while (priv_getline(line, sizeof(line)) > 0) {
567 		if (line[0] == '\n' || line[0] == 0 || line[0] == '#')
568 			continue;
569 		if (sscanf(line, "%d.%d.%d %255s", &i1, &i2, &i3, nambuf) == 4)
570 			/* got a hostname. */
571 			i3 |= ((i1 << 8) | i2) << 8;
572 		else if (sscanf(line, "%d.%d %255s", &i1, &i2, nambuf) == 3)
573 			/* got a net name */
574 			i3 = (((i1 << 8) | i2) << 8) | 255;
575 		else
576 			continue;
577 
578 		for (tp = &hnametable[i3 & (HASHNAMESIZE-1)];
579 		     tp->nxt; tp = tp->nxt)
580 			;
581 		tp->addr = i3;
582 		tp->nxt = newhnamemem();
583 		tp->name = savestr(nambuf);
584 	}
585 }
586 
587 static const char *
588 ataddr_string(u_short atnet, u_char athost)
589 {
590 	register struct hnamemem *tp, *tp2;
591 	register int i = (atnet << 8) | athost;
592 	char nambuf[MAXHOSTNAMELEN + 20];
593 	static int first = 1;
594 
595 	if (first) {
596 		first = 0;
597 		init_atalk();
598 	}
599 	for (tp = &hnametable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
600 		if (tp->addr == i)
601 			return (tp->name);
602 
603 	/* didn't have the node name -- see if we've got the net name */
604 	i |= 255;
605 	for (tp2 = &hnametable[i & (HASHNAMESIZE-1)]; tp2->nxt; tp2 = tp2->nxt)
606 		if (tp2->addr == i) {
607 			tp->addr = (atnet << 8) | athost;
608 			tp->nxt = newhnamemem();
609 			(void)snprintf(nambuf, sizeof nambuf, "%s.%d",
610 			    tp2->name, athost);
611 			tp->name = savestr(nambuf);
612 			return (tp->name);
613 		}
614 
615 	tp->addr = (atnet << 8) | athost;
616 	tp->nxt = newhnamemem();
617 	if (athost != 255)
618 		(void)snprintf(nambuf, sizeof nambuf, "%d.%d.%d",
619 			atnet >> 8, atnet & 0xff, athost);
620 	else
621 		(void)snprintf(nambuf, sizeof nambuf, "%d.%d",
622 			atnet >> 8, atnet & 0xff);
623 	tp->name = savestr(nambuf);
624 
625 	return (tp->name);
626 }
627 
628 static struct tok skt2str[] = {
629 	{ rtmpSkt,	"rtmp" },	/* routing table maintenance */
630 	{ nbpSkt,	"nis" },	/* name info socket */
631 	{ echoSkt,	"echo" },	/* AppleTalk echo protocol */
632 	{ zipSkt,	"zip" },	/* zone info protocol */
633 	{ 0,		NULL }
634 };
635 
636 static const char *
637 ddpskt_string(register int skt)
638 {
639 	static char buf[12];
640 
641 	if (nflag) {
642 		(void)snprintf(buf, sizeof buf, "%d", skt);
643 		return (buf);
644 	}
645 	return (tok2str(skt2str, "%d", skt));
646 }
647