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