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