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