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