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