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