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