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