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